comparison roundup/backends/rdbms_common.py @ 1168:94620e088e3a

fixes to the rdbms backends
author Richard Jones <richard@users.sourceforge.net>
date Wed, 18 Sep 2002 07:04:39 +0000
parents 14467c765167
children af104fa52746
comparison
equal deleted inserted replaced
1167:75e5f2055ca9 1168:94620e088e3a
1 # $Id: rdbms_common.py,v 1.1 2002-09-18 05:07:47 richard Exp $ 1 # $Id: rdbms_common.py,v 1.2 2002-09-18 07:04:38 richard Exp $
2 2
3 # standard python modules 3 # standard python modules
4 import sys, os, time, re, errno, weakref, copy 4 import sys, os, time, re, errno, weakref, copy
5 5
6 # roundup modules 6 # roundup modules
68 68
69 We should now confirm that the schema defined by our "classes" 69 We should now confirm that the schema defined by our "classes"
70 attribute actually matches the schema in the database. 70 attribute actually matches the schema in the database.
71 ''' 71 '''
72 # now detect changes in the schema 72 # now detect changes in the schema
73 save = 0
73 for classname, spec in self.classes.items(): 74 for classname, spec in self.classes.items():
74 if self.database_schema.has_key(classname): 75 if self.database_schema.has_key(classname):
75 dbspec = self.database_schema[classname] 76 dbspec = self.database_schema[classname]
76 self.update_class(spec, dbspec) 77 if self.update_class(spec, dbspec):
77 self.database_schema[classname] = spec.schema() 78 self.database_schema[classname] = spec.schema()
79 save = 1
78 else: 80 else:
79 self.create_class(spec) 81 self.create_class(spec)
80 self.database_schema[classname] = spec.schema() 82 self.database_schema[classname] = spec.schema()
83 save = 1
81 84
82 for classname in self.database_schema.keys(): 85 for classname in self.database_schema.keys():
83 if not self.classes.has_key(classname): 86 if not self.classes.has_key(classname):
84 self.drop_class(classname) 87 self.drop_class(classname)
85 88
86 # update the database version of the schema 89 # update the database version of the schema
87 cursor = self.conn.cursor() 90 if save:
88 self.sql(cursor, 'delete from schema') 91 cursor = self.conn.cursor()
89 self.save_dbschema(cursor, self.database_schema) 92 self.sql(cursor, 'delete from schema')
93 self.save_dbschema(cursor, self.database_schema)
90 94
91 # reindex the db if necessary 95 # reindex the db if necessary
92 if self.indexer.should_reindex(): 96 if self.indexer.should_reindex():
93 self.reindex() 97 self.reindex()
94 98
124 ''' Determine the differences between the current spec and the 128 ''' Determine the differences between the current spec and the
125 database version of the spec, and update where necessary 129 database version of the spec, and update where necessary
126 ''' 130 '''
127 spec_schema = spec.schema() 131 spec_schema = spec.schema()
128 if spec_schema == dbspec: 132 if spec_schema == dbspec:
129 return 133 # no save needed for this one
134 return 0
130 if __debug__: 135 if __debug__:
131 print >>hyperdb.DEBUG, 'update_class FIRING' 136 print >>hyperdb.DEBUG, 'update_class FIRING'
132 137
133 # key property changed? 138 # key property changed?
134 if dbspec[0] != spec_schema[0]: 139 if dbspec[0] != spec_schema[0]:
242 247
243 # 4. populate with the data from step one 248 # 4. populate with the data from step one
244 qs = ','.join([self.arg for x in cols]) 249 qs = ','.join([self.arg for x in cols])
245 sql = 'insert into _%s values (%s)'%(cn, s) 250 sql = 'insert into _%s values (%s)'%(cn, s)
246 cursor.execute(sql, olddata) 251 cursor.execute(sql, olddata)
252 return 1
247 253
248 def create_class_table(self, cursor, spec): 254 def create_class_table(self, cursor, spec):
249 ''' create the class table for the given spec 255 ''' create the class table for the given spec
250 ''' 256 '''
251 cols, mls = self.determine_columns(spec.properties.items()) 257 cols, mls = self.determine_columns(spec.properties.items())
1588 # figure the WHERE clause from the filterspec 1594 # figure the WHERE clause from the filterspec
1589 props = self.getprops() 1595 props = self.getprops()
1590 frum = ['_'+cn] 1596 frum = ['_'+cn]
1591 where = [] 1597 where = []
1592 args = [] 1598 args = []
1599 a = self.db.arg
1593 for k, v in filterspec.items(): 1600 for k, v in filterspec.items():
1594 propclass = props[k] 1601 propclass = props[k]
1595 if isinstance(propclass, Multilink): 1602 if isinstance(propclass, Multilink):
1596 tn = '%s_%s'%(cn, k) 1603 tn = '%s_%s'%(cn, k)
1597 frum.append(tn) 1604 frum.append(tn)
1603 where.append('id=%s.nodeid and %s.linkid = %s'%(tn, tn, 1610 where.append('id=%s.nodeid and %s.linkid = %s'%(tn, tn,
1604 self.arg)) 1611 self.arg))
1605 args.append(v) 1612 args.append(v)
1606 else: 1613 else:
1607 if isinstance(v, type([])): 1614 if isinstance(v, type([])):
1608 s = ','.join([self.arg for x in v]) 1615 s = ','.join([a for x in v])
1609 where.append('_%s in (%s)'%(k, s)) 1616 where.append('_%s in (%s)'%(k, s))
1610 args = args + v 1617 args = args + v
1611 else: 1618 else:
1612 where.append('_%s=%s'%(k, self.arg)) 1619 where.append('_%s=%s'%(k, a))
1613 args.append(v) 1620 args.append(v)
1614 1621
1615 # add results of full text search 1622 # add results of full text search
1616 if search_matches is not None: 1623 if search_matches is not None:
1617 v = search_matches.keys() 1624 v = search_matches.keys()
1618 s = ','.join([self.arg for x in v]) 1625 s = ','.join([a for x in v])
1619 where.append('id in (%s)'%s) 1626 where.append('id in (%s)'%s)
1620 args = args + v 1627 args = args + v
1621 1628
1622 # figure the order by clause 1629 # figure the order by clause
1623 orderby = [] 1630 orderby = []
1624 ordercols = [] 1631 ordercols = []
1625 if sort[0] is not None and sort[1] is not None: 1632 if sort[0] is not None and sort[1] is not None:
1626 if sort[0] != '-': 1633 direction, colname = sort
1627 orderby.append('_'+sort[1]) 1634 if direction != '-':
1628 ordercols.append(sort[1]) 1635 if colname == 'activity':
1636 orderby.append('activity')
1637 ordercols.append('max(%s__journal.date) as activity'%cn)
1638 frum.append('%s__journal'%cn)
1639 where.append('%s__journal.nodeid = _%s.id'%(cn, cn))
1640 else:
1641 orderby.append('_'+colname)
1642 ordercols.append('_'+colname)
1629 else: 1643 else:
1630 orderby.append('_'+sort[1]+' desc') 1644 if colname == 'activity':
1631 ordercols.append(sort[1]) 1645 orderby.append('activity desc')
1646 ordercols.append('max(%s__journal.date) as activity'%cn)
1647 frum.append('%s__journal'%cn)
1648 where.append('%s__journal.nodeid = _%s.id'%(cn, cn))
1649 else:
1650 orderby.append('_'+colname+' desc')
1651 ordercols.append('_'+colname)
1632 1652
1633 # figure the group by clause 1653 # figure the group by clause
1634 groupby = [] 1654 groupby = []
1635 groupcols = [] 1655 groupcols = []
1636 if group[0] is not None and group[1] is not None: 1656 if group[0] is not None and group[1] is not None:
1637 if group[0] != '-': 1657 if group[0] != '-':
1638 groupby.append('_'+group[1]) 1658 groupby.append('_'+group[1])
1639 groupcols.append(group[1]) 1659 groupcols.append('_'+group[1])
1640 else: 1660 else:
1641 groupby.append('_'+group[1]+' desc') 1661 groupby.append('_'+group[1]+' desc')
1642 groupcols.append(group[1]) 1662 groupcols.append('_'+group[1])
1643 1663
1644 # construct the SQL 1664 # construct the SQL
1645 frum = ','.join(frum) 1665 frum = ','.join(frum)
1646 where = ' and '.join(where) 1666 where = ' and '.join(where)
1647 cols = ['id'] 1667 cols = ['id']
1648 if orderby: 1668 if orderby:
1649 cols = cols + ordercols 1669 cols = cols + ordercols
1650 order = ' order by %s'%(','.join(orderby)) 1670 order = ' order by %s'%(','.join(orderby))
1651 else: 1671 else:
1652 order = '' 1672 order = ''
1653 if groupby: 1673 if 0: #groupby:
1654 cols = cols + groupcols 1674 cols = cols + groupcols
1655 group = ' group by %s'%(','.join(groupby)) 1675 group = ' group by %s'%(','.join(groupby))
1656 else: 1676 else:
1657 group = '' 1677 group = ''
1658 cols = ','.join(cols) 1678 cols = ','.join(cols)
1659 sql = 'select %s from %s where %s%s%s'%(cols, frum, where, order, 1679 sql = 'select %s from %s where %s%s%s'%(cols, frum, where, order,
1660 group) 1680 group)
1661 args = tuple(args) 1681 args = tuple(args)
1662 if __debug__: 1682 if __debug__:
1663 print >>hyperdb.DEBUG, 'find', (self, sql, args) 1683 print >>hyperdb.DEBUG, 'filter', (self, sql, args)
1664 cursor = self.db.conn.cursor() 1684 cursor = self.db.conn.cursor()
1665 cursor.execute(sql, args) 1685 cursor.execute(sql, args)
1686
1687 # return the IDs
1688 return [row[0] for row in cursor.fetchall()]
1666 1689
1667 def count(self): 1690 def count(self):
1668 '''Get the number of nodes in this class. 1691 '''Get the number of nodes in this class.
1669 1692
1670 If the returned integer is 'numnodes', the ids of all the nodes 1693 If the returned integer is 'numnodes', the ids of all the nodes

Roundup Issue Tracker: http://roundup-tracker.org/