Mercurial > p > roundup > code
comparison roundup/backends/rdbms_common.py @ 2075:b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Fri, 12 Mar 2004 04:09:00 +0000 |
| parents | 261c2e6ceb1e |
| children | 2a4309450202 |
comparison
equal
deleted
inserted
replaced
| 2073:261c2e6ceb1e | 2075:b1704ba7be41 |
|---|---|
| 1 # $Id: rdbms_common.py,v 1.76 2004-03-05 00:08:09 richard Exp $ | 1 # $Id: rdbms_common.py,v 1.77 2004-03-12 04:08:59 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 |
| 21 everything as a string.) | 21 everything as a string.) |
| 22 | 22 |
| 23 The schema of the hyperdb being mapped to the database is stored in the | 23 The schema of the hyperdb being mapped to the database is stored in the |
| 24 database itself as a repr()'ed dictionary of information about each Class | 24 database itself as a repr()'ed dictionary of information about each Class |
| 25 that maps to a table. If that information differs from the hyperdb schema, | 25 that maps to a table. If that information differs from the hyperdb schema, |
| 26 then we update it. We also store in the schema dict a __version__ which | 26 then we update it. We also store in the schema dict a version which |
| 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 |
| 112 def sql_stringquote(self, value): | 112 def sql_stringquote(self, value): |
| 113 ''' Quote the string so it's safe to put in the 'sql quotes' | 113 ''' Quote the string so it's safe to put in the 'sql quotes' |
| 114 ''' | 114 ''' |
| 115 return re.sub("'", "''", str(value)) | 115 return re.sub("'", "''", str(value)) |
| 116 | 116 |
| 117 def init_dbschema(self): | |
| 118 self.database_schema = { | |
| 119 'version': self.current_db_version, | |
| 120 'tables': {} | |
| 121 } | |
| 122 | |
| 117 def load_dbschema(self): | 123 def load_dbschema(self): |
| 118 ''' Load the schema definition that the database currently implements | 124 ''' Load the schema definition that the database currently implements |
| 119 ''' | 125 ''' |
| 120 self.cursor.execute('select schema from schema') | 126 self.cursor.execute('select schema from schema') |
| 121 self.database_schema = eval(self.cursor.fetchone()[0]) | 127 schema = self.cursor.fetchone() |
| 128 if schema: | |
| 129 self.database_schema = eval(schema[0]) | |
| 130 else: | |
| 131 self.database_schema = {} | |
| 122 | 132 |
| 123 def save_dbschema(self, schema): | 133 def save_dbschema(self, schema): |
| 124 ''' Save the schema definition that the database currently implements | 134 ''' Save the schema definition that the database currently implements |
| 125 ''' | 135 ''' |
| 126 s = repr(self.database_schema) | 136 s = repr(self.database_schema) |
| 130 ''' Called once the schema initialisation has finished. | 140 ''' Called once the schema initialisation has finished. |
| 131 | 141 |
| 132 We should now confirm that the schema defined by our "classes" | 142 We should now confirm that the schema defined by our "classes" |
| 133 attribute actually matches the schema in the database. | 143 attribute actually matches the schema in the database. |
| 134 ''' | 144 ''' |
| 135 self.upgrade_db() | 145 save = self.upgrade_db() |
| 136 | 146 |
| 137 # now detect changes in the schema | 147 # now detect changes in the schema |
| 138 save = 0 | 148 tables = self.database_schema['tables'] |
| 139 for classname, spec in self.classes.items(): | 149 for classname, spec in self.classes.items(): |
| 140 if self.database_schema.has_key(classname): | 150 if tables.has_key(classname): |
| 141 dbspec = self.database_schema[classname] | 151 dbspec = tables[classname] |
| 142 if self.update_class(spec, dbspec): | 152 if self.update_class(spec, dbspec): |
| 143 self.database_schema[classname] = spec.schema() | 153 tables[classname] = spec.schema() |
| 144 save = 1 | 154 save = 1 |
| 145 else: | 155 else: |
| 146 self.create_class(spec) | 156 self.create_class(spec) |
| 147 self.database_schema[classname] = spec.schema() | 157 tables[classname] = spec.schema() |
| 148 save = 1 | 158 save = 1 |
| 149 | 159 |
| 150 for classname, spec in self.database_schema.items(): | 160 for classname, spec in tables.items(): |
| 151 if not self.classes.has_key(classname): | 161 if not self.classes.has_key(classname): |
| 152 self.drop_class(classname, spec) | 162 self.drop_class(classname, tables[classname]) |
| 153 del self.database_schema[classname] | 163 del tables[classname] |
| 154 save = 1 | 164 save = 1 |
| 155 | 165 |
| 156 # update the database version of the schema | 166 # update the database version of the schema |
| 157 if save: | 167 if save: |
| 158 self.sql('delete from schema') | 168 self.sql('delete from schema') |
| 168 # update this number when we need to make changes to the SQL structure | 178 # update this number when we need to make changes to the SQL structure |
| 169 # of the backen database | 179 # of the backen database |
| 170 current_db_version = 2 | 180 current_db_version = 2 |
| 171 def upgrade_db(self): | 181 def upgrade_db(self): |
| 172 ''' Update the SQL database to reflect changes in the backend code. | 182 ''' Update the SQL database to reflect changes in the backend code. |
| 173 ''' | 183 |
| 174 version = self.database_schema.get('__version', 1) | 184 Return boolean whether we need to save the schema. |
| 185 ''' | |
| 186 version = self.database_schema.get('version', 1) | |
| 175 if version == 1: | 187 if version == 1: |
| 176 # version 1 doesn't have the OTK, session and indexing in the | 188 # version 1 doesn't have the OTK, session and indexing in the |
| 177 # database | 189 # database |
| 178 self.create_version_2_tables() | 190 self.create_version_2_tables() |
| 179 | 191 else: |
| 180 self.database_schema['__version'] = self.current_db_version | 192 return 0 |
| 193 | |
| 194 self.database_schema['version'] = self.current_db_version | |
| 195 return 1 | |
| 181 | 196 |
| 182 | 197 |
| 183 def refresh_database(self): | 198 def refresh_database(self): |
| 184 self.post_init() | 199 self.post_init() |
| 185 | 200 |
| 948 self.cursor.execute(sql, (date_stamp,)) | 963 self.cursor.execute(sql, (date_stamp,)) |
| 949 | 964 |
| 950 def sql_commit(self): | 965 def sql_commit(self): |
| 951 ''' Actually commit to the database. | 966 ''' Actually commit to the database. |
| 952 ''' | 967 ''' |
| 968 if __debug__: | |
| 969 print >>hyperdb.DEBUG, '+++ commit database connection +++' | |
| 953 self.conn.commit() | 970 self.conn.commit() |
| 954 | 971 |
| 955 def commit(self): | 972 def commit(self): |
| 956 ''' Commit the current transactions. | 973 ''' Commit the current transactions. |
| 957 | 974 |
| 1009 ''' | 1026 ''' |
| 1010 # return the classname, nodeid so we reindex this content | 1027 # return the classname, nodeid so we reindex this content |
| 1011 return (classname, nodeid) | 1028 return (classname, nodeid) |
| 1012 | 1029 |
| 1013 def sql_close(self): | 1030 def sql_close(self): |
| 1031 if __debug__: | |
| 1032 print >>hyperdb.DEBUG, '+++ close database connection +++' | |
| 1014 self.conn.close() | 1033 self.conn.close() |
| 1015 | 1034 |
| 1016 def close(self): | 1035 def close(self): |
| 1017 ''' Close off the connection. | 1036 ''' Close off the connection. |
| 1018 ''' | 1037 ''' |
