1- # $Id: rdbms_common.py,v 1.98.2.5 2004-05-29 02:09:13 richard Exp $
1+ # $Id: rdbms_common.py,v 1.98.2.6 2004-06-09 06:16:56 richard Exp $
22''' Relational database (SQL) backend common code.
33
44Basics:
@@ -143,10 +143,11 @@ def load_dbschema(self):
143143 else :
144144 self .database_schema = {}
145145
146- def save_dbschema (self , schema ):
146+ def save_dbschema (self ):
147147 ''' Save the schema definition that the database currently implements
148148 '''
149149 s = repr (self .database_schema )
150+ self .sql ('delete from schema' )
150151 self .sql ('insert into schema values (%s)' , (s ,))
151152
152153 def post_init (self ):
@@ -178,8 +179,7 @@ def post_init(self):
178179
179180 # update the database version of the schema
180181 if save :
181- self .sql ('delete from schema' )
182- self .save_dbschema (self .database_schema )
182+ self .save_dbschema ()
183183
184184 # reindex the db if necessary
185185 if self .indexer .should_reindex ():
@@ -190,7 +190,7 @@ def post_init(self):
190190
191191 # update this number when we need to make changes to the SQL structure
192192 # of the backen database
193- current_db_version = 2
193+ current_db_version = 3
194194 def upgrade_db (self ):
195195 ''' Update the SQL database to reflect changes in the backend code.
196196
@@ -213,9 +213,65 @@ def upgrade_db(self):
213213 # database
214214 self .create_version_2_tables ()
215215
216+ if version == 2 :
217+ self .fix_version_2_tables ()
218+
216219 self .database_schema ['version' ] = self .current_db_version
217220 return 1
218221
222+ def fix_version_2_tables (self ):
223+ '''Default (used by sqlite): NOOP'''
224+ pass
225+
226+ def _convert_journal_tables (self ):
227+ '''Get current journal table contents, drop the table and re-create'''
228+ c = self .cursor
229+ cols = ',' .join ('nodeid date tag action params' .split ())
230+ for klass in self .classes .values ():
231+ # slurp and drop
232+ sql = 'select %s from %s__journal order by date' % (cols ,
233+ klass .classname )
234+ c .execute (sql )
235+ contents = c .fetchall ()
236+ self .drop_journal_table_indexes (klass .classname )
237+ c .execute ('drop table %s__journal' % klass .classname )
238+
239+ # re-create and re-populate
240+ self .create_journal_table (klass )
241+ a = self .arg
242+ sql = 'insert into %s__journal (%s) values (%s,%s,%s,%s,%s)' % (
243+ klass .classname , cols , a , a , a , a , a )
244+ for row in contents :
245+ # no data conversion needed
246+ self .cursor .execute (sql , row )
247+
248+ def _convert_string_properties (self ):
249+ '''Get current Class tables that contain String properties, and
250+ convert the VARCHAR columns to TEXT'''
251+ c = self .cursor
252+ for klass in self .classes .values ():
253+ # slurp and drop
254+ cols , mls = self .determine_columns (klass .properties .items ())
255+ scols = ',' .join ([i [0 ] for i in cols ])
256+ sql = 'select id,%s from _%s' % (scols , klass .classname )
257+ c .execute (sql )
258+ contents = c .fetchall ()
259+ self .drop_class_table_indexes (klass .classname , klass .getkey ())
260+ c .execute ('drop table _%s' % klass .classname )
261+
262+ # re-create and re-populate
263+ self .create_class_table (klass , create_sequence = False )
264+ a = ',' .join ([self .arg for i in range (len (cols )+ 1 )])
265+ sql = 'insert into _%s (id,%s) values (%s)' % (klass .classname ,
266+ scols , a )
267+ for row in contents :
268+ l = []
269+ for entry in row :
270+ # mysql will already be a string - psql needs "help"
271+ if entry is not None and not isinstance (entry , type ('' )):
272+ entry = str (entry )
273+ l .append (entry )
274+ self .cursor .execute (sql , l )
219275
220276 def refresh_database (self ):
221277 self .post_init ()
@@ -227,7 +283,7 @@ def reindex(self):
227283 self .indexer .save_index ()
228284
229285 hyperdb_to_sql_datatypes = {
230- hyperdb .String : 'VARCHAR(255) ' ,
286+ hyperdb .String : 'TEXT ' ,
231287 hyperdb .Date : 'TIMESTAMP' ,
232288 hyperdb .Link : 'INTEGER' ,
233289 hyperdb .Interval : 'VARCHAR(255)' ,
@@ -466,7 +522,7 @@ def create_multilink_table(self, spec, ml):
466522 given by the spec
467523 '''
468524 # create the table
469- sql = 'create table %s_%s (linkid varchar, nodeid varchar)' % (
525+ sql = 'create table %s_%s (linkid varchar(255) , nodeid varchar(255) )' % (
470526 spec .classname , ml )
471527 if __debug__ :
472528 print >> hyperdb .DEBUG , 'create_class' , (self , sql )
@@ -1092,8 +1148,7 @@ def load_journal(self, classname, cols, nodeid):
10921148 def pack (self , pack_before ):
10931149 ''' Delete all journal entries except "create" before 'pack_before'.
10941150 '''
1095- # get a 'yyyymmddhhmmss' version of the date
1096- date_stamp = pack_before .serialise ()
1151+ date_stamp = self .hyperdb_to_sql_value [Date ](pack_before )
10971152
10981153 # do the delete
10991154 for classname in self .classes .keys ():
0 commit comments