Mercurial > p > roundup > code
comparison roundup/backends/rdbms_common.py @ 3156:e1da7b5b04ab maint-0.8
merge from HEAD
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Mon, 14 Feb 2005 02:55:31 +0000 |
| parents | 0a652c47bc9e |
| children | 5bd7a4caba0d |
comparison
equal
deleted
inserted
replaced
| 3154:62b1a54107e6 | 3156:e1da7b5b04ab |
|---|---|
| 1 # $Id: rdbms_common.py,v 1.142.2.4 2005-02-13 22:40:53 richard Exp $ | 1 # $Id: rdbms_common.py,v 1.142.2.5 2005-02-14 02:55:31 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 |
| 27 allows us to upgrade the database schema when necessary. See upgrade_db(). | 27 allows us to upgrade the database schema when necessary. See upgrade_db(). |
| 28 ''' | 28 ''' |
| 29 __docformat__ = 'restructuredtext' | 29 __docformat__ = 'restructuredtext' |
| 30 | 30 |
| 31 # standard python modules | 31 # standard python modules |
| 32 import sys, os, time, re, errno, weakref, copy | 32 import sys, os, time, re, errno, weakref, copy, logging |
| 33 | 33 |
| 34 # roundup modules | 34 # roundup modules |
| 35 from roundup import hyperdb, date, password, roundupdb, security | 35 from roundup import hyperdb, date, password, roundupdb, security |
| 36 from roundup.hyperdb import String, Password, Date, Interval, Link, \ | 36 from roundup.hyperdb import String, Password, Date, Interval, Link, \ |
| 37 Multilink, DatabaseError, Boolean, Number, Node | 37 Multilink, DatabaseError, Boolean, Number, Node |
| 126 | 126 |
| 127 def sql(self, sql, args=None): | 127 def sql(self, sql, args=None): |
| 128 ''' Execute the sql with the optional args. | 128 ''' Execute the sql with the optional args. |
| 129 ''' | 129 ''' |
| 130 if __debug__: | 130 if __debug__: |
| 131 self.config.logging.getLogger('hyperdb').debug('SQL %r %r'%(sql, args)) | 131 logging.getLogger('hyperdb').debug('SQL %r %r'%(sql, args)) |
| 132 if args: | 132 if args: |
| 133 self.cursor.execute(sql, args) | 133 self.cursor.execute(sql, args) |
| 134 else: | 134 else: |
| 135 self.cursor.execute(sql) | 135 self.cursor.execute(sql) |
| 136 | 136 |
| 223 # nothing to do | 223 # nothing to do |
| 224 return 0 | 224 return 0 |
| 225 | 225 |
| 226 if version < 2: | 226 if version < 2: |
| 227 if __debug__: | 227 if __debug__: |
| 228 self.config.logging.getLogger('hyperdb').info('upgrade to version 2') | 228 logging.getLogger('hyperdb').info('upgrade to version 2') |
| 229 # change the schema structure | 229 # change the schema structure |
| 230 self.database_schema = {'tables': self.database_schema} | 230 self.database_schema = {'tables': self.database_schema} |
| 231 | 231 |
| 232 # version 1 didn't have the actor column (note that in | 232 # version 1 didn't have the actor column (note that in |
| 233 # MySQL this will also transition the tables to typed columns) | 233 # MySQL this will also transition the tables to typed columns) |
| 237 # database | 237 # database |
| 238 self.create_version_2_tables() | 238 self.create_version_2_tables() |
| 239 | 239 |
| 240 if version < 3: | 240 if version < 3: |
| 241 if __debug__: | 241 if __debug__: |
| 242 self.config.logging.getLogger('hyperdb').info('upgrade to version 3') | 242 logging.getLogger('hyperdb').info('upgrade to version 3') |
| 243 self.fix_version_2_tables() | 243 self.fix_version_2_tables() |
| 244 | 244 |
| 245 if version < 4: | 245 if version < 4: |
| 246 self.fix_version_3_tables() | 246 self.fix_version_3_tables() |
| 247 | 247 |
| 378 old_spec[1].sort() | 378 old_spec[1].sort() |
| 379 if not force and new_spec == old_spec: | 379 if not force and new_spec == old_spec: |
| 380 # no changes | 380 # no changes |
| 381 return 0 | 381 return 0 |
| 382 | 382 |
| 383 logger = self.config.logging.getLogger('hyperdb') | 383 logger = logging.getLogger('hyperdb') |
| 384 logger.info('update_class %s'%spec.classname) | 384 logger.info('update_class %s'%spec.classname) |
| 385 | 385 |
| 386 logger.debug('old_spec %r'%(old_spec,)) | 386 logger.debug('old_spec %r'%(old_spec,)) |
| 387 logger.debug('new_spec %r'%(new_spec,)) | 387 logger.debug('new_spec %r'%(new_spec,)) |
| 388 | 388 |
| 666 '''Delete all database contents. | 666 '''Delete all database contents. |
| 667 | 667 |
| 668 Note: I don't commit here, which is different behaviour to the | 668 Note: I don't commit here, which is different behaviour to the |
| 669 "nuke from orbit" behaviour in the dbs. | 669 "nuke from orbit" behaviour in the dbs. |
| 670 ''' | 670 ''' |
| 671 self.config.logging.getLogger('hyperdb').info('clear') | 671 logging.getLogger('hyperdb').info('clear') |
| 672 for cn in self.classes.keys(): | 672 for cn in self.classes.keys(): |
| 673 sql = 'delete from _%s'%cn | 673 sql = 'delete from _%s'%cn |
| 674 self.sql(sql) | 674 self.sql(sql) |
| 675 self.setid(cn, 1) | 675 self.setid(cn, 1) |
| 676 | 676 |
| 691 } | 691 } |
| 692 def addnode(self, classname, nodeid, node): | 692 def addnode(self, classname, nodeid, node): |
| 693 ''' Add the specified node to its class's db. | 693 ''' Add the specified node to its class's db. |
| 694 ''' | 694 ''' |
| 695 if __debug__: | 695 if __debug__: |
| 696 self.config.logging.getLogger('hyperdb').debug('addnode %s%s %r'%(classname, | 696 logging.getLogger('hyperdb').debug('addnode %s%s %r'%(classname, |
| 697 nodeid, node)) | 697 nodeid, node)) |
| 698 | 698 |
| 699 # determine the column definitions and multilink tables | 699 # determine the column definitions and multilink tables |
| 700 cl = self.classes[classname] | 700 cl = self.classes[classname] |
| 701 cols, mls = self.determine_columns(cl.properties.items()) | 701 cols, mls = self.determine_columns(cl.properties.items()) |
| 766 | 766 |
| 767 def setnode(self, classname, nodeid, values, multilink_changes={}): | 767 def setnode(self, classname, nodeid, values, multilink_changes={}): |
| 768 ''' Change the specified node. | 768 ''' Change the specified node. |
| 769 ''' | 769 ''' |
| 770 if __debug__: | 770 if __debug__: |
| 771 self.config.logging.getLogger('hyperdb').debug('setnode %s%s %r' | 771 logging.getLogger('hyperdb').debug('setnode %s%s %r' |
| 772 % (classname, nodeid, values)) | 772 % (classname, nodeid, values)) |
| 773 | 773 |
| 774 # clear this node out of the cache if it's in there | 774 # clear this node out of the cache if it's in there |
| 775 key = (classname, nodeid) | 775 key = (classname, nodeid) |
| 776 if self.cache.has_key(key): | 776 if self.cache.has_key(key): |
| 951 | 951 |
| 952 def destroynode(self, classname, nodeid): | 952 def destroynode(self, classname, nodeid): |
| 953 '''Remove a node from the database. Called exclusively by the | 953 '''Remove a node from the database. Called exclusively by the |
| 954 destroy() method on Class. | 954 destroy() method on Class. |
| 955 ''' | 955 ''' |
| 956 self.config.logging.getLogger('hyperdb').info('destroynode %s%s'%(classname, nodeid)) | 956 logging.getLogger('hyperdb').info('destroynode %s%s'%(classname, nodeid)) |
| 957 | 957 |
| 958 # make sure the node exists | 958 # make sure the node exists |
| 959 if not self.hasnode(classname, nodeid): | 959 if not self.hasnode(classname, nodeid): |
| 960 raise IndexError, '%s has no node %s'%(classname, nodeid) | 960 raise IndexError, '%s has no node %s'%(classname, nodeid) |
| 961 | 961 |
| 1020 | 1020 |
| 1021 # create the journal entry | 1021 # create the journal entry |
| 1022 cols = 'nodeid,date,tag,action,params' | 1022 cols = 'nodeid,date,tag,action,params' |
| 1023 | 1023 |
| 1024 if __debug__: | 1024 if __debug__: |
| 1025 self.config.logging.getLogger('hyperdb').debug('addjournal %s%s %r %s %s %r'%(classname, | 1025 logging.getLogger('hyperdb').debug('addjournal %s%s %r %s %s %r'%(classname, |
| 1026 nodeid, journaldate, journaltag, action, params)) | 1026 nodeid, journaldate, journaltag, action, params)) |
| 1027 | 1027 |
| 1028 # make the journalled data marshallable | 1028 # make the journalled data marshallable |
| 1029 if isinstance(params, type({})): | 1029 if isinstance(params, type({})): |
| 1030 self._journal_marshal(params, classname) | 1030 self._journal_marshal(params, classname) |
| 1047 cols = 'nodeid,date,tag,action,params' | 1047 cols = 'nodeid,date,tag,action,params' |
| 1048 | 1048 |
| 1049 dc = self.hyperdb_to_sql_value[hyperdb.Date] | 1049 dc = self.hyperdb_to_sql_value[hyperdb.Date] |
| 1050 for nodeid, journaldate, journaltag, action, params in journal: | 1050 for nodeid, journaldate, journaltag, action, params in journal: |
| 1051 if __debug__: | 1051 if __debug__: |
| 1052 self.config.logging.getLogger('hyperdb').debug('addjournal %s%s %r %s %s %r'%( | 1052 logging.getLogger('hyperdb').debug('addjournal %s%s %r %s %s %r'%( |
| 1053 classname, nodeid, journaldate, journaltag, action, | 1053 classname, nodeid, journaldate, journaltag, action, |
| 1054 params)) | 1054 params)) |
| 1055 | 1055 |
| 1056 # make the journalled data marshallable | 1056 # make the journalled data marshallable |
| 1057 if isinstance(params, type({})): | 1057 if isinstance(params, type({})): |
| 1149 self.sql(sql, (date_stamp,)) | 1149 self.sql(sql, (date_stamp,)) |
| 1150 | 1150 |
| 1151 def sql_commit(self): | 1151 def sql_commit(self): |
| 1152 ''' Actually commit to the database. | 1152 ''' Actually commit to the database. |
| 1153 ''' | 1153 ''' |
| 1154 self.config.logging.getLogger('hyperdb').info('commit') | 1154 logging.getLogger('hyperdb').info('commit') |
| 1155 self.conn.commit() | 1155 self.conn.commit() |
| 1156 | 1156 |
| 1157 # open a new cursor for subsequent work | 1157 # open a new cursor for subsequent work |
| 1158 self.cursor = self.conn.cursor() | 1158 self.cursor = self.conn.cursor() |
| 1159 | 1159 |
| 1183 ''' Reverse all actions from the current transaction. | 1183 ''' Reverse all actions from the current transaction. |
| 1184 | 1184 |
| 1185 Undo all the changes made since the database was opened or the last | 1185 Undo all the changes made since the database was opened or the last |
| 1186 commit() or rollback() was performed. | 1186 commit() or rollback() was performed. |
| 1187 ''' | 1187 ''' |
| 1188 self.config.logging.getLogger('hyperdb').info('rollback') | 1188 logging.getLogger('hyperdb').info('rollback') |
| 1189 | 1189 |
| 1190 self.sql_rollback() | 1190 self.sql_rollback() |
| 1191 | 1191 |
| 1192 # roll back "other" transaction stuff | 1192 # roll back "other" transaction stuff |
| 1193 for method, args in self.transactions: | 1193 for method, args in self.transactions: |
| 1198 | 1198 |
| 1199 # clear the cache | 1199 # clear the cache |
| 1200 self.clearCache() | 1200 self.clearCache() |
| 1201 | 1201 |
| 1202 def sql_close(self): | 1202 def sql_close(self): |
| 1203 self.config.logging.getLogger('hyperdb').info('close') | 1203 logging.getLogger('hyperdb').info('close') |
| 1204 self.conn.close() | 1204 self.conn.close() |
| 1205 | 1205 |
| 1206 def close(self): | 1206 def close(self): |
| 1207 ''' Close off the connection. | 1207 ''' Close off the connection. |
| 1208 ''' | 1208 ''' |
