Mercurial > p > roundup > code
comparison roundup/backends/rdbms_common.py @ 2734:1e70e58cf763 maint-0.7
merge from HEAD
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Fri, 08 Oct 2004 01:29:30 +0000 |
| parents | 0d8b3b5f40ea |
| children | b654a33346a6 |
comparison
equal
deleted
inserted
replaced
| 2732:0d8b3b5f40ea | 2734:1e70e58cf763 |
|---|---|
| 1 # $Id: rdbms_common.py,v 1.98.2.22 2004-10-08 00:57:22 richard Exp $ | 1 # $Id: rdbms_common.py,v 1.98.2.23 2004-10-08 01:29:30 richard Exp $ |
| 2 ''' Relational database (SQL) backend common code. | 2 ''' Relational database (SQL) backend common code. |
| 3 | 3 |
| 4 Basics: | 4 Basics: |
| 5 | 5 |
| 6 - map roundup classes to relational tables | 6 - map roundup classes to relational tables |
| 1951 if not isinstance(prop, Link) and not isinstance(prop, Multilink): | 1951 if not isinstance(prop, Link) and not isinstance(prop, Multilink): |
| 1952 raise TypeError, "'%s' not a Link/Multilink property"%propname | 1952 raise TypeError, "'%s' not a Link/Multilink property"%propname |
| 1953 | 1953 |
| 1954 # first, links | 1954 # first, links |
| 1955 a = self.db.arg | 1955 a = self.db.arg |
| 1956 allvalues = (1,) | 1956 allvalues = () |
| 1957 o = [] | 1957 sql = [] |
| 1958 where = [] | 1958 where = [] |
| 1959 for prop, values in propspec: | 1959 for prop, values in propspec: |
| 1960 if not isinstance(props[prop], hyperdb.Link): | 1960 if not isinstance(props[prop], hyperdb.Link): |
| 1961 continue | 1961 continue |
| 1962 if type(values) is type({}) and len(values) == 1: | 1962 if type(values) is type({}) and len(values) == 1: |
| 1973 values.remove(None) | 1973 values.remove(None) |
| 1974 s = '_%s is NULL or '%prop | 1974 s = '_%s is NULL or '%prop |
| 1975 allvalues += tuple(values) | 1975 allvalues += tuple(values) |
| 1976 s += '_%s in (%s)'%(prop, ','.join([a]*len(values))) | 1976 s += '_%s in (%s)'%(prop, ','.join([a]*len(values))) |
| 1977 where.append('(' + s +')') | 1977 where.append('(' + s +')') |
| 1978 tables = ['_%s'%self.classname] | |
| 1979 if where: | 1978 if where: |
| 1980 o.append('(' + ' and '.join(where) + ')') | 1979 allvalues = (1, ) + allvalues |
| 1980 sql.append('''select id from _%s where __retired__ <> %s | |
| 1981 and %s'''%(self.classname, a, ' and '.join(where))) | |
| 1981 | 1982 |
| 1982 # now multilinks | 1983 # now multilinks |
| 1983 for prop, values in propspec: | 1984 for prop, values in propspec: |
| 1984 if not isinstance(props[prop], hyperdb.Multilink): | 1985 if not isinstance(props[prop], hyperdb.Multilink): |
| 1985 continue | 1986 continue |
| 1986 if not values: | 1987 if not values: |
| 1987 continue | 1988 continue |
| 1989 allvalues += (1, ) | |
| 1988 if type(values) is type(''): | 1990 if type(values) is type(''): |
| 1989 allvalues += (values,) | 1991 allvalues += (values,) |
| 1990 s = a | 1992 s = a |
| 1991 else: | 1993 else: |
| 1992 allvalues += tuple(values.keys()) | 1994 allvalues += tuple(values.keys()) |
| 1993 s = ','.join([a]*len(values)) | 1995 s = ','.join([a]*len(values)) |
| 1994 tn = '%s_%s'%(self.classname, prop) | 1996 tn = '%s_%s'%(self.classname, prop) |
| 1995 tables.append(tn) | 1997 sql.append('''select id from _%s, %s where __retired__ <> %s |
| 1996 o.append('(id=%s.nodeid and %s.linkid in (%s))'%(tn, tn, s)) | 1998 and id = %s.nodeid and %s.linkid in (%s)'''%(self.classname, |
| 1997 | 1999 tn, a, tn, tn, s)) |
| 1998 if not o: | 2000 |
| 2001 if not sql: | |
| 1999 return [] | 2002 return [] |
| 2000 elif len(o) > 1: | 2003 sql = ' union '.join(sql) |
| 2001 o = '(' + ' or '.join(['(%s)'%i for i in o]) + ')' | |
| 2002 else: | |
| 2003 o = o[0] | |
| 2004 t = ', '.join(tables) | |
| 2005 sql = 'select distinct(id) from %s where __retired__ <> %s and %s'%( | |
| 2006 t, a, o) | |
| 2007 self.db.sql(sql, allvalues) | 2004 self.db.sql(sql, allvalues) |
| 2008 # XXX numeric ids | 2005 # XXX numeric ids |
| 2009 l = [str(x[0]) for x in self.db.sql_fetchall()] | 2006 l = [str(x[0]) for x in self.db.sql_fetchall()] |
| 2010 if __debug__: | 2007 if __debug__: |
| 2011 print >>hyperdb.DEBUG, 'find ... ', l | 2008 print >>hyperdb.DEBUG, 'find ... ', l |
