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 $
22
33# standard python modules
44import sys , os , time , re , errno , weakref , copy
@@ -70,23 +70,27 @@ def post_init(self):
7070 attribute actually matches the schema in the database.
7171 '''
7272 # now detect changes in the schema
73+ save = 0
7374 for classname , spec in self .classes .items ():
7475 if self .database_schema .has_key (classname ):
7576 dbspec = self .database_schema [classname ]
76- self .update_class (spec , dbspec )
77- self .database_schema [classname ] = spec .schema ()
77+ if self .update_class (spec , dbspec ):
78+ self .database_schema [classname ] = spec .schema ()
79+ save = 1
7880 else :
7981 self .create_class (spec )
8082 self .database_schema [classname ] = spec .schema ()
83+ save = 1
8184
8285 for classname in self .database_schema .keys ():
8386 if not self .classes .has_key (classname ):
8487 self .drop_class (classname )
8588
8689 # update the database version of the schema
87- cursor = self .conn .cursor ()
88- self .sql (cursor , 'delete from schema' )
89- self .save_dbschema (cursor , self .database_schema )
90+ if save :
91+ cursor = self .conn .cursor ()
92+ self .sql (cursor , 'delete from schema' )
93+ self .save_dbschema (cursor , self .database_schema )
9094
9195 # reindex the db if necessary
9296 if self .indexer .should_reindex ():
@@ -126,7 +130,8 @@ def update_class(self, spec, dbspec):
126130 '''
127131 spec_schema = spec .schema ()
128132 if spec_schema == dbspec :
129- return
133+ # no save needed for this one
134+ return 0
130135 if __debug__ :
131136 print >> hyperdb .DEBUG , 'update_class FIRING'
132137
@@ -244,6 +249,7 @@ def update_class(self, spec, dbspec):
244249 qs = ',' .join ([self .arg for x in cols ])
245250 sql = 'insert into _%s values (%s)' % (cn , s )
246251 cursor .execute (sql , olddata )
252+ return 1
247253
248254 def create_class_table (self , cursor , spec ):
249255 ''' create the class table for the given spec
@@ -1590,6 +1596,7 @@ def filter(self, search_matches, filterspec, sort, group):
15901596 frum = ['_' + cn ]
15911597 where = []
15921598 args = []
1599+ a = self .db .arg
15931600 for k , v in filterspec .items ():
15941601 propclass = props [k ]
15951602 if isinstance (propclass , Multilink ):
@@ -1605,41 +1612,54 @@ def filter(self, search_matches, filterspec, sort, group):
16051612 args .append (v )
16061613 else :
16071614 if isinstance (v , type ([])):
1608- s = ',' .join ([self . arg for x in v ])
1615+ s = ',' .join ([a for x in v ])
16091616 where .append ('_%s in (%s)' % (k , s ))
16101617 args = args + v
16111618 else :
1612- where .append ('_%s=%s' % (k , self . arg ))
1619+ where .append ('_%s=%s' % (k , a ))
16131620 args .append (v )
16141621
16151622 # add results of full text search
16161623 if search_matches is not None :
16171624 v = search_matches .keys ()
1618- s = ',' .join ([self . arg for x in v ])
1625+ s = ',' .join ([a for x in v ])
16191626 where .append ('id in (%s)' % s )
16201627 args = args + v
16211628
16221629 # figure the order by clause
16231630 orderby = []
16241631 ordercols = []
16251632 if sort [0 ] is not None and sort [1 ] is not None :
1626- if sort [0 ] != '-' :
1627- orderby .append ('_' + sort [1 ])
1628- ordercols .append (sort [1 ])
1633+ direction , colname = sort
1634+ if direction != '-' :
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 )
16291643 else :
1630- orderby .append ('_' + sort [1 ]+ ' desc' )
1631- ordercols .append (sort [1 ])
1644+ if colname == 'activity' :
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 )
16321652
16331653 # figure the group by clause
16341654 groupby = []
16351655 groupcols = []
16361656 if group [0 ] is not None and group [1 ] is not None :
16371657 if group [0 ] != '-' :
16381658 groupby .append ('_' + group [1 ])
1639- groupcols .append (group [1 ])
1659+ groupcols .append ('_' + group [1 ])
16401660 else :
16411661 groupby .append ('_' + group [1 ]+ ' desc' )
1642- groupcols .append (group [1 ])
1662+ groupcols .append ('_' + group [1 ])
16431663
16441664 # construct the SQL
16451665 frum = ',' .join (frum )
@@ -1650,7 +1670,7 @@ def filter(self, search_matches, filterspec, sort, group):
16501670 order = ' order by %s' % (',' .join (orderby ))
16511671 else :
16521672 order = ''
1653- if groupby :
1673+ if 0 : # groupby:
16541674 cols = cols + groupcols
16551675 group = ' group by %s' % (',' .join (groupby ))
16561676 else :
@@ -1660,10 +1680,13 @@ def filter(self, search_matches, filterspec, sort, group):
16601680 group )
16611681 args = tuple (args )
16621682 if __debug__ :
1663- print >> hyperdb .DEBUG , 'find ' , (self , sql , args )
1683+ print >> hyperdb .DEBUG , 'filter ' , (self , sql , args )
16641684 cursor = self .db .conn .cursor ()
16651685 cursor .execute (sql , args )
16661686
1687+ # return the IDs
1688+ return [row [0 ] for row in cursor .fetchall ()]
1689+
16671690 def count (self ):
16681691 '''Get the number of nodes in this class.
16691692
0 commit comments