comparison roundup/backends/rdbms_common.py @ 2073:261c2e6ceb1e

*** empty log message ***
author Richard Jones <richard@users.sourceforge.net>
date Fri, 05 Mar 2004 00:08:09 +0000
parents fc52d57c6c3e
children b1704ba7be41
comparison
equal deleted inserted replaced
2069:d8c7fd205cd8 2073:261c2e6ceb1e
1 # $Id: rdbms_common.py,v 1.75 2004-02-11 23:55:09 richard Exp $ 1 # $Id: rdbms_common.py,v 1.76 2004-03-05 00:08:09 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
17 Database-specific changes may generally be pushed out to the overridable 17 Database-specific changes may generally be pushed out to the overridable
18 sql_* methods, since everything else should be fairly generic. There's 18 sql_* methods, since everything else should be fairly generic. There's
19 probably a bit of work to be done if a database is used that actually 19 probably a bit of work to be done if a database is used that actually
20 honors column typing, since the initial databases don't (sqlite stores 20 honors column typing, since the initial databases don't (sqlite stores
21 everything as a string.) 21 everything as a string.)
22
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
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
27 allows us to upgrade the database schema when necessary. See upgrade_db().
22 ''' 28 '''
23 __docformat__ = 'restructuredtext' 29 __docformat__ = 'restructuredtext'
24 30
25 # standard python modules 31 # standard python modules
26 import sys, os, time, re, errno, weakref, copy 32 import sys, os, time, re, errno, weakref, copy
75 def clearCache(self): 81 def clearCache(self):
76 self.cache = {} 82 self.cache = {}
77 self.cache_lru = [] 83 self.cache_lru = []
78 84
79 def sql_open_connection(self): 85 def sql_open_connection(self):
80 ''' Open a connection to the database, creating it if necessary 86 ''' Open a connection to the database, creating it if necessary.
87
88 Must call self.load_dbschema()
81 ''' 89 '''
82 raise NotImplemented 90 raise NotImplemented
83 91
84 def sql(self, sql, args=None): 92 def sql(self, sql, args=None):
85 ''' Execute the sql with the optional args. 93 ''' Execute the sql with the optional args.
104 def sql_stringquote(self, value): 112 def sql_stringquote(self, value):
105 ''' 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'
106 ''' 114 '''
107 return re.sub("'", "''", str(value)) 115 return re.sub("'", "''", str(value))
108 116
117 def load_dbschema(self):
118 ''' Load the schema definition that the database currently implements
119 '''
120 self.cursor.execute('select schema from schema')
121 self.database_schema = eval(self.cursor.fetchone()[0])
122
109 def save_dbschema(self, schema): 123 def save_dbschema(self, schema):
110 ''' Save the schema definition that the database currently implements 124 ''' Save the schema definition that the database currently implements
111 ''' 125 '''
112 s = repr(self.database_schema) 126 s = repr(self.database_schema)
113 self.sql('insert into schema values (%s)', (s,)) 127 self.sql('insert into schema values (%s)', (s,))
114 128
115 def load_dbschema(self):
116 ''' Load the schema definition that the database currently implements
117 '''
118 self.cursor.execute('select schema from schema')
119 return eval(self.cursor.fetchone()[0])
120
121 def post_init(self): 129 def post_init(self):
122 ''' Called once the schema initialisation has finished. 130 ''' Called once the schema initialisation has finished.
123 131
124 We should now confirm that the schema defined by our "classes" 132 We should now confirm that the schema defined by our "classes"
125 attribute actually matches the schema in the database. 133 attribute actually matches the schema in the database.
126 ''' 134 '''
135 self.upgrade_db()
136
127 # now detect changes in the schema 137 # now detect changes in the schema
128 save = 0 138 save = 0
129 for classname, spec in self.classes.items(): 139 for classname, spec in self.classes.items():
130 if self.database_schema.has_key(classname): 140 if self.database_schema.has_key(classname):
131 dbspec = self.database_schema[classname] 141 dbspec = self.database_schema[classname]
152 if self.indexer.should_reindex(): 162 if self.indexer.should_reindex():
153 self.reindex() 163 self.reindex()
154 164
155 # commit 165 # commit
156 self.conn.commit() 166 self.conn.commit()
167
168 # update this number when we need to make changes to the SQL structure
169 # of the backen database
170 current_db_version = 2
171 def upgrade_db(self):
172 ''' Update the SQL database to reflect changes in the backend code.
173 '''
174 version = self.database_schema.get('__version', 1)
175 if version == 1:
176 # version 1 doesn't have the OTK, session and indexing in the
177 # database
178 self.create_version_2_tables()
179
180 self.database_schema['__version'] = self.current_db_version
181
157 182
158 def refresh_database(self): 183 def refresh_database(self):
159 self.post_init() 184 self.post_init()
160 185
161 def reindex(self): 186 def reindex(self):

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