Mercurial > p > roundup > code
comparison roundup/backends/back_sqlite.py @ 2217:98d3bf8ffb19
store Intervals as two columns (and other fixes
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Sun, 18 Apr 2004 05:31:03 +0000 |
| parents | f7f6b6981a13 |
| children | 69f16bb16f83 |
comparison
equal
deleted
inserted
replaced
| 2216:759ed26e24dd | 2217:98d3bf8ffb19 |
|---|---|
| 1 # $Id: back_sqlite.py,v 1.24 2004-04-07 01:12:26 richard Exp $ | 1 # $Id: back_sqlite.py,v 1.25 2004-04-18 05:31:02 richard Exp $ |
| 2 '''Implements a backend for SQLite. | 2 '''Implements a backend for SQLite. |
| 3 | 3 |
| 4 See https://pysqlite.sourceforge.net/ for pysqlite info | 4 See https://pysqlite.sourceforge.net/ for pysqlite info |
| 5 | 5 |
| 6 | 6 |
| 30 } | 30 } |
| 31 hyperdb_to_sql_value = { | 31 hyperdb_to_sql_value = { |
| 32 hyperdb.String : str, | 32 hyperdb.String : str, |
| 33 hyperdb.Date : lambda x: x.serialise(), | 33 hyperdb.Date : lambda x: x.serialise(), |
| 34 hyperdb.Link : int, | 34 hyperdb.Link : int, |
| 35 hyperdb.Interval : lambda x: x.serialise(), | 35 hyperdb.Interval : str, |
| 36 hyperdb.Password : str, | 36 hyperdb.Password : str, |
| 37 hyperdb.Boolean : int, | 37 hyperdb.Boolean : int, |
| 38 hyperdb.Number : lambda x: x, | 38 hyperdb.Number : lambda x: x, |
| 39 } | 39 } |
| 40 sql_to_hyperdb_value = { | 40 sql_to_hyperdb_value = { |
| 41 hyperdb.String : str, | 41 hyperdb.String : str, |
| 42 hyperdb.Date : lambda x: date.Date(str(x)), | 42 hyperdb.Date : lambda x: date.Date(str(x)), |
| 43 # hyperdb.Link : int, # XXX numeric ids | 43 hyperdb.Link : str, # XXX numeric ids |
| 44 hyperdb.Link : str, | |
| 45 hyperdb.Interval : date.Interval, | 44 hyperdb.Interval : date.Interval, |
| 46 hyperdb.Password : lambda x: password.Password(encrypted=x), | 45 hyperdb.Password : lambda x: password.Password(encrypted=x), |
| 47 hyperdb.Boolean : int, | 46 hyperdb.Boolean : int, |
| 48 hyperdb.Number : rdbms_common._num_cvt, | 47 hyperdb.Number : rdbms_common._num_cvt, |
| 49 } | 48 } |
| 93 '_textid integer)') | 92 '_textid integer)') |
| 94 self.cursor.execute('CREATE INDEX words_word_ids ON __words(_word)') | 93 self.cursor.execute('CREATE INDEX words_word_ids ON __words(_word)') |
| 95 sql = 'insert into ids (name, num) values (%s,%s)'%(self.arg, self.arg) | 94 sql = 'insert into ids (name, num) values (%s,%s)'%(self.arg, self.arg) |
| 96 self.cursor.execute(sql, ('__textids', 1)) | 95 self.cursor.execute(sql, ('__textids', 1)) |
| 97 | 96 |
| 98 def add_actor_column(self): | 97 def add_new_columns_v2(self): |
| 99 # update existing tables to have the new actor column | 98 # update existing tables to have the new actor column |
| 100 tables = self.database_schema['tables'] | 99 tables = self.database_schema['tables'] |
| 101 for classname, spec in self.classes.items(): | 100 for classname, spec in self.classes.items(): |
| 102 if tables.has_key(classname): | 101 if tables.has_key(classname): |
| 103 dbspec = tables[classname] | 102 dbspec = tables[classname] |
| 104 self.update_class(spec, dbspec, force=1, adding_actor=1) | 103 self.update_class(spec, dbspec, force=1, adding_v2=1) |
| 105 # we've updated - don't try again | 104 # we've updated - don't try again |
| 106 tables[classname] = spec.schema() | 105 tables[classname] = spec.schema() |
| 107 | 106 |
| 108 def update_class(self, spec, old_spec, force=0, adding_actor=0): | 107 def update_class(self, spec, old_spec, force=0, adding_v2=0): |
| 109 ''' Determine the differences between the current spec and the | 108 ''' Determine the differences between the current spec and the |
| 110 database version of the spec, and update where necessary. | 109 database version of the spec, and update where necessary. |
| 111 | 110 |
| 112 If 'force' is true, update the database anyway. | 111 If 'force' is true, update the database anyway. |
| 113 | 112 |
| 139 print >>hyperdb.DEBUG, 'update_class', (self, sql) | 138 print >>hyperdb.DEBUG, 'update_class', (self, sql) |
| 140 self.cursor.execute(sql) | 139 self.cursor.execute(sql) |
| 141 old_has = old_has.has_key | 140 old_has = old_has.has_key |
| 142 | 141 |
| 143 # now figure how we populate the new table | 142 # now figure how we populate the new table |
| 144 if adding_actor: | 143 if adding_v2: |
| 145 fetch = ['_activity', '_creation', '_creator'] | 144 fetch = ['_activity', '_creation', '_creator'] |
| 146 else: | 145 else: |
| 147 fetch = ['_actor', '_activity', '_creation', '_creator'] | 146 fetch = ['_actor', '_activity', '_creation', '_creator'] |
| 148 properties = spec.getprops() | 147 properties = spec.getprops() |
| 149 for propname,x in new_spec[1]: | 148 for propname,x in new_spec[1]: |
| 199 | 198 |
| 200 # create the new table | 199 # create the new table |
| 201 self.create_class_table(spec) | 200 self.create_class_table(spec) |
| 202 | 201 |
| 203 if olddata: | 202 if olddata: |
| 203 inscols = [] | |
| 204 for propname,x in new_spec[1]: | |
| 205 prop = properties[propname] | |
| 206 if isinstance(prop, hyperdb.Multilink): | |
| 207 continue | |
| 208 elif isinstance(prop, hyperdb.Interval): | |
| 209 inscols.append('_'+propname) | |
| 210 inscols.append('__'+propname+'_int__') | |
| 211 elif old_has(propname): | |
| 212 # we copy this col over from the old table | |
| 213 inscols.append('_'+propname) | |
| 214 | |
| 204 # do the insert of the old data - the new columns will have | 215 # do the insert of the old data - the new columns will have |
| 205 # NULL values | 216 # NULL values |
| 206 args = ','.join([self.arg for x in fetch]) | 217 args = ','.join([self.arg for x in inscols]) |
| 207 sql = 'insert into _%s (%s) values (%s)'%(cn, fetchcols, args) | 218 cols = ','.join(inscols) |
| 219 sql = 'insert into _%s (%s) values (%s)'%(cn, cols, args) | |
| 208 if __debug__: | 220 if __debug__: |
| 209 print >>hyperdb.DEBUG, 'update_class', (self, sql, olddata[0]) | 221 print >>hyperdb.DEBUG, 'update_class', (self, sql, olddata[0]) |
| 210 for entry in olddata: | 222 for entry in olddata: |
| 211 self.cursor.execute(sql, tuple(entry)) | 223 d = [] |
| 224 for name in inscols: | |
| 225 # generate the new value for the Interval int column | |
| 226 if name.endswith('_int__'): | |
| 227 name = name[2:-6] | |
| 228 if entry.has_key(name): | |
| 229 v = hyperdb.Interval(entry[name]).as_seconds() | |
| 230 else: | |
| 231 v = None | |
| 232 elif entry.has_key(name): | |
| 233 v = entry[name] | |
| 234 else: | |
| 235 v = None | |
| 236 d.append(v) | |
| 237 self.cursor.execute(sql, tuple(d)) | |
| 212 | 238 |
| 213 return 1 | 239 return 1 |
| 214 | 240 |
| 215 def sql_close(self): | 241 def sql_close(self): |
| 216 ''' Squash any error caused by us already having closed the | 242 ''' Squash any error caused by us already having closed the |
