Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1 | #----------------------------------------------------------------------- |
| 2 | # |
| 3 | # Copyright (C) 2000, 2001 by Autonomous Zone Industries |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 4 | # Copyright (C) 2002 Gregory P. Smith |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5 | # |
| 6 | # License: This is free software. You may use this software for any |
| 7 | # purpose including modification/redistribution, so long as |
| 8 | # this header remains intact and that you do not claim any |
| 9 | # rights of ownership or authorship of this software. This |
| 10 | # software has been tested, but no warranty is expressed or |
| 11 | # implied. |
| 12 | # |
| 13 | # -- Gregory P. Smith <greg@electricrain.com> |
| 14 | |
| 15 | # This provides a simple database table interface built on top of |
| 16 | # the Python BerkeleyDB 3 interface. |
| 17 | # |
| 18 | _cvsid = '$Id$' |
| 19 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 20 | import re |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 21 | import sys |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 22 | import copy |
Gregory P. Smith | 0dcc3cc | 2007-10-18 17:15:20 +0000 | [diff] [blame] | 23 | import struct |
Tim Peters | 95334a5 | 2004-08-08 00:54:21 +0000 | [diff] [blame] | 24 | import random |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 25 | from types import ListType, StringType |
| 26 | import cPickle as pickle |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 27 | |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 28 | try: |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 29 | # For Pythons w/distutils pybsddb |
| 30 | from bsddb3.db import * |
| 31 | except ImportError: |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 32 | # For Python 2.3 |
| 33 | from bsddb.db import * |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 34 | |
Neal Norwitz | 6aaccc6 | 2006-06-11 08:35:14 +0000 | [diff] [blame] | 35 | # XXX(nnorwitz): is this correct? DBIncompleteError is conditional in _bsddb.c |
| 36 | try: |
| 37 | DBIncompleteError |
| 38 | except NameError: |
| 39 | class DBIncompleteError(Exception): |
| 40 | pass |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 41 | |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 42 | class TableDBError(StandardError): |
| 43 | pass |
| 44 | class TableAlreadyExists(TableDBError): |
| 45 | pass |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | class Cond: |
| 49 | """This condition matches everything""" |
| 50 | def __call__(self, s): |
| 51 | return 1 |
| 52 | |
| 53 | class ExactCond(Cond): |
| 54 | """Acts as an exact match condition function""" |
| 55 | def __init__(self, strtomatch): |
| 56 | self.strtomatch = strtomatch |
| 57 | def __call__(self, s): |
| 58 | return s == self.strtomatch |
| 59 | |
| 60 | class PrefixCond(Cond): |
| 61 | """Acts as a condition function for matching a string prefix""" |
| 62 | def __init__(self, prefix): |
| 63 | self.prefix = prefix |
| 64 | def __call__(self, s): |
| 65 | return s[:len(self.prefix)] == self.prefix |
| 66 | |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 67 | class PostfixCond(Cond): |
| 68 | """Acts as a condition function for matching a string postfix""" |
| 69 | def __init__(self, postfix): |
| 70 | self.postfix = postfix |
| 71 | def __call__(self, s): |
| 72 | return s[-len(self.postfix):] == self.postfix |
| 73 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 74 | class LikeCond(Cond): |
| 75 | """ |
| 76 | Acts as a function that will match using an SQL 'LIKE' style |
| 77 | string. Case insensitive and % signs are wild cards. |
| 78 | This isn't perfect but it should work for the simple common cases. |
| 79 | """ |
| 80 | def __init__(self, likestr, re_flags=re.IGNORECASE): |
| 81 | # escape python re characters |
| 82 | chars_to_escape = '.*+()[]?' |
| 83 | for char in chars_to_escape : |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 84 | likestr = likestr.replace(char, '\\'+char) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 85 | # convert %s to wildcards |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 86 | self.likestr = likestr.replace('%', '.*') |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 87 | self.re = re.compile('^'+self.likestr+'$', re_flags) |
| 88 | def __call__(self, s): |
| 89 | return self.re.match(s) |
| 90 | |
| 91 | # |
| 92 | # keys used to store database metadata |
| 93 | # |
| 94 | _table_names_key = '__TABLE_NAMES__' # list of the tables in this db |
| 95 | _columns = '._COLUMNS__' # table_name+this key contains a list of columns |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 96 | |
| 97 | def _columns_key(table): |
| 98 | return table + _columns |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 99 | |
| 100 | # |
| 101 | # these keys are found within table sub databases |
| 102 | # |
| 103 | _data = '._DATA_.' # this+column+this+rowid key contains table data |
| 104 | _rowid = '._ROWID_.' # this+rowid+this key contains a unique entry for each |
| 105 | # row in the table. (no data is stored) |
| 106 | _rowid_str_len = 8 # length in bytes of the unique rowid strings |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 107 | |
| 108 | def _data_key(table, col, rowid): |
| 109 | return table + _data + col + _data + rowid |
| 110 | |
| 111 | def _search_col_data_key(table, col): |
| 112 | return table + _data + col + _data |
| 113 | |
| 114 | def _search_all_data_key(table): |
| 115 | return table + _data |
| 116 | |
| 117 | def _rowid_key(table, rowid): |
| 118 | return table + _rowid + rowid + _rowid |
| 119 | |
| 120 | def _search_rowid_key(table): |
| 121 | return table + _rowid |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 122 | |
| 123 | def contains_metastrings(s) : |
| 124 | """Verify that the given string does not contain any |
| 125 | metadata strings that might interfere with dbtables database operation. |
| 126 | """ |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 127 | if (s.find(_table_names_key) >= 0 or |
| 128 | s.find(_columns) >= 0 or |
| 129 | s.find(_data) >= 0 or |
| 130 | s.find(_rowid) >= 0): |
| 131 | # Then |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 132 | return 1 |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 133 | else: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 134 | return 0 |
| 135 | |
| 136 | |
| 137 | class bsdTableDB : |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 138 | def __init__(self, filename, dbhome, create=0, truncate=0, mode=0600, |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 139 | recover=0, dbflags=0): |
Gregory P. Smith | ff7d991 | 2006-06-08 05:17:08 +0000 | [diff] [blame] | 140 | """bsdTableDB(filename, dbhome, create=0, truncate=0, mode=0600) |
| 141 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 142 | Open database name in the dbhome BerkeleyDB directory. |
| 143 | Use keyword arguments when calling this constructor. |
| 144 | """ |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 145 | self.db = None |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 146 | myflags = DB_THREAD |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 147 | if create: |
| 148 | myflags |= DB_CREATE |
| 149 | flagsforenv = (DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_LOG | |
| 150 | DB_INIT_TXN | dbflags) |
| 151 | # DB_AUTO_COMMIT isn't a valid flag for env.open() |
| 152 | try: |
| 153 | dbflags |= DB_AUTO_COMMIT |
| 154 | except AttributeError: |
| 155 | pass |
| 156 | if recover: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 157 | flagsforenv = flagsforenv | DB_RECOVER |
| 158 | self.env = DBEnv() |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 159 | # enable auto deadlock avoidance |
| 160 | self.env.set_lk_detect(DB_LOCK_DEFAULT) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 161 | self.env.open(dbhome, myflags | flagsforenv) |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 162 | if truncate: |
| 163 | myflags |= DB_TRUNCATE |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 164 | self.db = DB(self.env) |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 165 | # this code relies on DBCursor.set* methods to raise exceptions |
| 166 | # rather than returning None |
| 167 | self.db.set_get_returns_none(1) |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 168 | # allow duplicate entries [warning: be careful w/ metadata] |
| 169 | self.db.set_flags(DB_DUP) |
| 170 | self.db.open(filename, DB_BTREE, dbflags | myflags, mode) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 171 | self.dbfilename = filename |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 172 | # Initialize the table names list if this is a new database |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 173 | txn = self.env.txn_begin() |
| 174 | try: |
| 175 | if not self.db.has_key(_table_names_key, txn): |
| 176 | self.db.put(_table_names_key, pickle.dumps([], 1), txn=txn) |
| 177 | # Yes, bare except |
| 178 | except: |
| 179 | txn.abort() |
| 180 | raise |
| 181 | else: |
| 182 | txn.commit() |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 183 | # TODO verify more of the database's metadata? |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 184 | self.__tablecolumns = {} |
| 185 | |
| 186 | def __del__(self): |
| 187 | self.close() |
| 188 | |
| 189 | def close(self): |
| 190 | if self.db is not None: |
| 191 | self.db.close() |
| 192 | self.db = None |
| 193 | if self.env is not None: |
| 194 | self.env.close() |
| 195 | self.env = None |
| 196 | |
| 197 | def checkpoint(self, mins=0): |
| 198 | try: |
| 199 | self.env.txn_checkpoint(mins) |
| 200 | except DBIncompleteError: |
| 201 | pass |
| 202 | |
| 203 | def sync(self): |
| 204 | try: |
| 205 | self.db.sync() |
| 206 | except DBIncompleteError: |
| 207 | pass |
| 208 | |
| 209 | def _db_print(self) : |
| 210 | """Print the database to stdout for debugging""" |
| 211 | print "******** Printing raw database for debugging ********" |
| 212 | cur = self.db.cursor() |
| 213 | try: |
| 214 | key, data = cur.first() |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 215 | while 1: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 216 | print repr({key: data}) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 217 | next = cur.next() |
| 218 | if next: |
| 219 | key, data = next |
| 220 | else: |
| 221 | cur.close() |
| 222 | return |
| 223 | except DBNotFoundError: |
| 224 | cur.close() |
| 225 | |
| 226 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 227 | def CreateTable(self, table, columns): |
Gregory P. Smith | ff7d991 | 2006-06-08 05:17:08 +0000 | [diff] [blame] | 228 | """CreateTable(table, columns) - Create a new table in the database. |
| 229 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 230 | raises TableDBError if it already exists or for other DB errors. |
| 231 | """ |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 232 | assert isinstance(columns, ListType) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 233 | txn = None |
| 234 | try: |
| 235 | # checking sanity of the table and column names here on |
| 236 | # table creation will prevent problems elsewhere. |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 237 | if contains_metastrings(table): |
| 238 | raise ValueError( |
| 239 | "bad table name: contains reserved metastrings") |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 240 | for column in columns : |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 241 | if contains_metastrings(column): |
| 242 | raise ValueError( |
| 243 | "bad column name: contains reserved metastrings") |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 244 | |
| 245 | columnlist_key = _columns_key(table) |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 246 | if self.db.has_key(columnlist_key): |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 247 | raise TableAlreadyExists, "table already exists" |
| 248 | |
| 249 | txn = self.env.txn_begin() |
| 250 | # store the table's column info |
| 251 | self.db.put(columnlist_key, pickle.dumps(columns, 1), txn=txn) |
| 252 | |
| 253 | # add the table name to the tablelist |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 254 | tablelist = pickle.loads(self.db.get(_table_names_key, txn=txn, |
| 255 | flags=DB_RMW)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 256 | tablelist.append(table) |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 257 | # delete 1st, in case we opened with DB_DUP |
| 258 | self.db.delete(_table_names_key, txn) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 259 | self.db.put(_table_names_key, pickle.dumps(tablelist, 1), txn=txn) |
| 260 | |
| 261 | txn.commit() |
| 262 | txn = None |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 263 | except DBError, dberror: |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 264 | if txn: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 265 | txn.abort() |
| 266 | raise TableDBError, dberror[1] |
| 267 | |
| 268 | |
| 269 | def ListTableColumns(self, table): |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 270 | """Return a list of columns in the given table. |
| 271 | [] if the table doesn't exist. |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 272 | """ |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 273 | assert isinstance(table, StringType) |
| 274 | if contains_metastrings(table): |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 275 | raise ValueError, "bad table name: contains reserved metastrings" |
| 276 | |
| 277 | columnlist_key = _columns_key(table) |
| 278 | if not self.db.has_key(columnlist_key): |
| 279 | return [] |
| 280 | pickledcolumnlist = self.db.get(columnlist_key) |
| 281 | if pickledcolumnlist: |
| 282 | return pickle.loads(pickledcolumnlist) |
| 283 | else: |
| 284 | return [] |
| 285 | |
| 286 | def ListTables(self): |
| 287 | """Return a list of tables in this database.""" |
| 288 | pickledtablelist = self.db.get(_table_names_key) |
| 289 | if pickledtablelist: |
| 290 | return pickle.loads(pickledtablelist) |
| 291 | else: |
| 292 | return [] |
| 293 | |
| 294 | def CreateOrExtendTable(self, table, columns): |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 295 | """CreateOrExtendTable(table, columns) |
| 296 | |
Gregory P. Smith | ff7d991 | 2006-06-08 05:17:08 +0000 | [diff] [blame] | 297 | Create a new table in the database. |
| 298 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 299 | If a table of this name already exists, extend it to have any |
| 300 | additional columns present in the given list as well as |
| 301 | all of its current columns. |
| 302 | """ |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 303 | assert isinstance(columns, ListType) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 304 | try: |
| 305 | self.CreateTable(table, columns) |
| 306 | except TableAlreadyExists: |
| 307 | # the table already existed, add any new columns |
| 308 | txn = None |
| 309 | try: |
| 310 | columnlist_key = _columns_key(table) |
| 311 | txn = self.env.txn_begin() |
| 312 | |
| 313 | # load the current column list |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 314 | oldcolumnlist = pickle.loads( |
| 315 | self.db.get(columnlist_key, txn=txn, flags=DB_RMW)) |
| 316 | # create a hash table for fast lookups of column names in the |
| 317 | # loop below |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 318 | oldcolumnhash = {} |
| 319 | for c in oldcolumnlist: |
| 320 | oldcolumnhash[c] = c |
| 321 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 322 | # create a new column list containing both the old and new |
| 323 | # column names |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 324 | newcolumnlist = copy.copy(oldcolumnlist) |
| 325 | for c in columns: |
| 326 | if not oldcolumnhash.has_key(c): |
| 327 | newcolumnlist.append(c) |
| 328 | |
| 329 | # store the table's new extended column list |
| 330 | if newcolumnlist != oldcolumnlist : |
| 331 | # delete the old one first since we opened with DB_DUP |
| 332 | self.db.delete(columnlist_key, txn) |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 333 | self.db.put(columnlist_key, |
| 334 | pickle.dumps(newcolumnlist, 1), |
| 335 | txn=txn) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 336 | |
| 337 | txn.commit() |
| 338 | txn = None |
| 339 | |
| 340 | self.__load_column_info(table) |
| 341 | except DBError, dberror: |
| 342 | if txn: |
| 343 | txn.abort() |
| 344 | raise TableDBError, dberror[1] |
| 345 | |
| 346 | |
| 347 | def __load_column_info(self, table) : |
| 348 | """initialize the self.__tablecolumns dict""" |
| 349 | # check the column names |
| 350 | try: |
| 351 | tcolpickles = self.db.get(_columns_key(table)) |
| 352 | except DBNotFoundError: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 353 | raise TableDBError, "unknown table: %r" % (table,) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 354 | if not tcolpickles: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 355 | raise TableDBError, "unknown table: %r" % (table,) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 356 | self.__tablecolumns[table] = pickle.loads(tcolpickles) |
| 357 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 358 | def __new_rowid(self, table, txn) : |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 359 | """Create a new unique row identifier""" |
| 360 | unique = 0 |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 361 | while not unique: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 362 | # Generate a random 64-bit row ID string |
| 363 | # (note: this code has <64 bits of randomness |
| 364 | # but it's plenty for our database id needs!) |
Gregory P. Smith | 0dcc3cc | 2007-10-18 17:15:20 +0000 | [diff] [blame] | 365 | # We must ensure that no null bytes are in the id value. |
| 366 | blist = [] |
| 367 | for x in xrange(_rowid_str_len): |
| 368 | blist.append(random.randint(1,255)) |
| 369 | newid = struct.pack('B'*_rowid_str_len, *blist) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 370 | |
| 371 | # Guarantee uniqueness by adding this key to the database |
| 372 | try: |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 373 | self.db.put(_rowid_key(table, newid), None, txn=txn, |
| 374 | flags=DB_NOOVERWRITE) |
| 375 | except DBKeyExistError: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 376 | pass |
| 377 | else: |
| 378 | unique = 1 |
| 379 | |
| 380 | return newid |
| 381 | |
| 382 | |
| 383 | def Insert(self, table, rowdict) : |
| 384 | """Insert(table, datadict) - Insert a new row into the table |
| 385 | using the keys+values from rowdict as the column values. |
| 386 | """ |
| 387 | txn = None |
| 388 | try: |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 389 | if not self.db.has_key(_columns_key(table)): |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 390 | raise TableDBError, "unknown table" |
| 391 | |
| 392 | # check the validity of each column name |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 393 | if not self.__tablecolumns.has_key(table): |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 394 | self.__load_column_info(table) |
| 395 | for column in rowdict.keys() : |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 396 | if not self.__tablecolumns[table].count(column): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 397 | raise TableDBError, "unknown column: %r" % (column,) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 398 | |
| 399 | # get a unique row identifier for this row |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 400 | txn = self.env.txn_begin() |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 401 | rowid = self.__new_rowid(table, txn=txn) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 402 | |
| 403 | # insert the row values into the table database |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 404 | for column, dataitem in rowdict.items(): |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 405 | # store the value |
| 406 | self.db.put(_data_key(table, column, rowid), dataitem, txn=txn) |
| 407 | |
| 408 | txn.commit() |
| 409 | txn = None |
| 410 | |
| 411 | except DBError, dberror: |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 412 | # WIBNI we could just abort the txn and re-raise the exception? |
| 413 | # But no, because TableDBError is not related to DBError via |
| 414 | # inheritance, so it would be backwards incompatible. Do the next |
| 415 | # best thing. |
| 416 | info = sys.exc_info() |
| 417 | if txn: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 418 | txn.abort() |
| 419 | self.db.delete(_rowid_key(table, rowid)) |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 420 | raise TableDBError, dberror[1], info[2] |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 421 | |
| 422 | |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 423 | def Modify(self, table, conditions={}, mappings={}): |
Gregory P. Smith | ff7d991 | 2006-06-08 05:17:08 +0000 | [diff] [blame] | 424 | """Modify(table, conditions={}, mappings={}) - Modify items in rows matching 'conditions' using mapping functions in 'mappings' |
| 425 | |
| 426 | * table - the table name |
| 427 | * conditions - a dictionary keyed on column names containing |
| 428 | a condition callable expecting the data string as an |
| 429 | argument and returning a boolean. |
| 430 | * mappings - a dictionary keyed on column names containing a |
| 431 | condition callable expecting the data string as an argument and |
| 432 | returning the new string for that column. |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 433 | """ |
| 434 | try: |
| 435 | matching_rowids = self.__Select(table, [], conditions) |
| 436 | |
| 437 | # modify only requested columns |
| 438 | columns = mappings.keys() |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 439 | for rowid in matching_rowids.keys(): |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 440 | txn = None |
| 441 | try: |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 442 | for column in columns: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 443 | txn = self.env.txn_begin() |
| 444 | # modify the requested column |
| 445 | try: |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 446 | dataitem = self.db.get( |
| 447 | _data_key(table, column, rowid), |
Gregory P. Smith | 0dcc3cc | 2007-10-18 17:15:20 +0000 | [diff] [blame] | 448 | txn=txn) |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 449 | self.db.delete( |
| 450 | _data_key(table, column, rowid), |
| 451 | txn) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 452 | except DBNotFoundError: |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 453 | # XXXXXXX row key somehow didn't exist, assume no |
| 454 | # error |
| 455 | dataitem = None |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 456 | dataitem = mappings[column](dataitem) |
| 457 | if dataitem <> None: |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 458 | self.db.put( |
| 459 | _data_key(table, column, rowid), |
| 460 | dataitem, txn=txn) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 461 | txn.commit() |
| 462 | txn = None |
| 463 | |
Gregory P. Smith | ff7d991 | 2006-06-08 05:17:08 +0000 | [diff] [blame] | 464 | # catch all exceptions here since we call unknown callables |
| 465 | except: |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 466 | if txn: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 467 | txn.abort() |
| 468 | raise |
| 469 | |
| 470 | except DBError, dberror: |
| 471 | raise TableDBError, dberror[1] |
| 472 | |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 473 | def Delete(self, table, conditions={}): |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 474 | """Delete(table, conditions) - Delete items matching the given |
| 475 | conditions from the table. |
Gregory P. Smith | ff7d991 | 2006-06-08 05:17:08 +0000 | [diff] [blame] | 476 | |
| 477 | * conditions - a dictionary keyed on column names containing |
| 478 | condition functions expecting the data string as an |
| 479 | argument and returning a boolean. |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 480 | """ |
| 481 | try: |
| 482 | matching_rowids = self.__Select(table, [], conditions) |
| 483 | |
| 484 | # delete row data from all columns |
| 485 | columns = self.__tablecolumns[table] |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 486 | for rowid in matching_rowids.keys(): |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 487 | txn = None |
| 488 | try: |
| 489 | txn = self.env.txn_begin() |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 490 | for column in columns: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 491 | # delete the data key |
| 492 | try: |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 493 | self.db.delete(_data_key(table, column, rowid), |
| 494 | txn) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 495 | except DBNotFoundError: |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 496 | # XXXXXXX column may not exist, assume no error |
| 497 | pass |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 498 | |
| 499 | try: |
| 500 | self.db.delete(_rowid_key(table, rowid), txn) |
| 501 | except DBNotFoundError: |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 502 | # XXXXXXX row key somehow didn't exist, assume no error |
| 503 | pass |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 504 | txn.commit() |
| 505 | txn = None |
| 506 | except DBError, dberror: |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 507 | if txn: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 508 | txn.abort() |
| 509 | raise |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 510 | except DBError, dberror: |
| 511 | raise TableDBError, dberror[1] |
| 512 | |
| 513 | |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 514 | def Select(self, table, columns, conditions={}): |
Gregory P. Smith | ff7d991 | 2006-06-08 05:17:08 +0000 | [diff] [blame] | 515 | """Select(table, columns, conditions) - retrieve specific row data |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 516 | Returns a list of row column->value mapping dictionaries. |
Gregory P. Smith | ff7d991 | 2006-06-08 05:17:08 +0000 | [diff] [blame] | 517 | |
| 518 | * columns - a list of which column data to return. If |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 519 | columns is None, all columns will be returned. |
Gregory P. Smith | ff7d991 | 2006-06-08 05:17:08 +0000 | [diff] [blame] | 520 | * conditions - a dictionary keyed on column names |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 521 | containing callable conditions expecting the data string as an |
| 522 | argument and returning a boolean. |
| 523 | """ |
| 524 | try: |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 525 | if not self.__tablecolumns.has_key(table): |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 526 | self.__load_column_info(table) |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 527 | if columns is None: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 528 | columns = self.__tablecolumns[table] |
| 529 | matching_rowids = self.__Select(table, columns, conditions) |
| 530 | except DBError, dberror: |
| 531 | raise TableDBError, dberror[1] |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 532 | # return the matches as a list of dictionaries |
| 533 | return matching_rowids.values() |
| 534 | |
| 535 | |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 536 | def __Select(self, table, columns, conditions): |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 537 | """__Select() - Used to implement Select and Delete (above) |
| 538 | Returns a dictionary keyed on rowids containing dicts |
| 539 | holding the row data for columns listed in the columns param |
| 540 | that match the given conditions. |
| 541 | * conditions is a dictionary keyed on column names |
| 542 | containing callable conditions expecting the data string as an |
| 543 | argument and returning a boolean. |
| 544 | """ |
| 545 | # check the validity of each column name |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 546 | if not self.__tablecolumns.has_key(table): |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 547 | self.__load_column_info(table) |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 548 | if columns is None: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 549 | columns = self.tablecolumns[table] |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 550 | for column in (columns + conditions.keys()): |
| 551 | if not self.__tablecolumns[table].count(column): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 552 | raise TableDBError, "unknown column: %r" % (column,) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 553 | |
| 554 | # keyed on rows that match so far, containings dicts keyed on |
| 555 | # column names containing the data for that row and column. |
| 556 | matching_rowids = {} |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 557 | # keys are rowids that do not match |
| 558 | rejected_rowids = {} |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 559 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 560 | # attempt to sort the conditions in such a way as to minimize full |
| 561 | # column lookups |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 562 | def cmp_conditions(atuple, btuple): |
| 563 | a = atuple[1] |
| 564 | b = btuple[1] |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 565 | if type(a) is type(b): |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 566 | if isinstance(a, PrefixCond) and isinstance(b, PrefixCond): |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 567 | # longest prefix first |
| 568 | return cmp(len(b.prefix), len(a.prefix)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 569 | if isinstance(a, LikeCond) and isinstance(b, LikeCond): |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 570 | # longest likestr first |
| 571 | return cmp(len(b.likestr), len(a.likestr)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 572 | return 0 |
| 573 | if isinstance(a, ExactCond): |
| 574 | return -1 |
| 575 | if isinstance(b, ExactCond): |
| 576 | return 1 |
| 577 | if isinstance(a, PrefixCond): |
| 578 | return -1 |
| 579 | if isinstance(b, PrefixCond): |
| 580 | return 1 |
| 581 | # leave all unknown condition callables alone as equals |
| 582 | return 0 |
| 583 | |
| 584 | conditionlist = conditions.items() |
| 585 | conditionlist.sort(cmp_conditions) |
| 586 | |
| 587 | # Apply conditions to column data to find what we want |
| 588 | cur = self.db.cursor() |
| 589 | column_num = -1 |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 590 | for column, condition in conditionlist: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 591 | column_num = column_num + 1 |
| 592 | searchkey = _search_col_data_key(table, column) |
| 593 | # speedup: don't linear search columns within loop |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 594 | if column in columns: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 595 | savethiscolumndata = 1 # save the data for return |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 596 | else: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 597 | savethiscolumndata = 0 # data only used for selection |
| 598 | |
| 599 | try: |
| 600 | key, data = cur.set_range(searchkey) |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 601 | while key[:len(searchkey)] == searchkey: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 602 | # extract the rowid from the key |
| 603 | rowid = key[-_rowid_str_len:] |
| 604 | |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 605 | if not rejected_rowids.has_key(rowid): |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 606 | # if no condition was specified or the condition |
| 607 | # succeeds, add row to our match list. |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 608 | if not condition or condition(data): |
| 609 | if not matching_rowids.has_key(rowid): |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 610 | matching_rowids[rowid] = {} |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 611 | if savethiscolumndata: |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 612 | matching_rowids[rowid][column] = data |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 613 | else: |
| 614 | if matching_rowids.has_key(rowid): |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 615 | del matching_rowids[rowid] |
| 616 | rejected_rowids[rowid] = rowid |
| 617 | |
| 618 | key, data = cur.next() |
| 619 | |
| 620 | except DBError, dberror: |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 621 | if dberror[0] != DB_NOTFOUND: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 622 | raise |
| 623 | continue |
| 624 | |
| 625 | cur.close() |
| 626 | |
| 627 | # we're done selecting rows, garbage collect the reject list |
| 628 | del rejected_rowids |
| 629 | |
| 630 | # extract any remaining desired column data from the |
| 631 | # database for the matching rows. |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 632 | if len(columns) > 0: |
| 633 | for rowid, rowdata in matching_rowids.items(): |
| 634 | for column in columns: |
| 635 | if rowdata.has_key(column): |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 636 | continue |
| 637 | try: |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 638 | rowdata[column] = self.db.get( |
| 639 | _data_key(table, column, rowid)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 640 | except DBError, dberror: |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 641 | if dberror[0] != DB_NOTFOUND: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 642 | raise |
| 643 | rowdata[column] = None |
| 644 | |
| 645 | # return the matches |
| 646 | return matching_rowids |
| 647 | |
| 648 | |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 649 | def Drop(self, table): |
| 650 | """Remove an entire table from the database""" |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 651 | txn = None |
| 652 | try: |
| 653 | txn = self.env.txn_begin() |
| 654 | |
| 655 | # delete the column list |
| 656 | self.db.delete(_columns_key(table), txn) |
| 657 | |
| 658 | cur = self.db.cursor(txn) |
| 659 | |
| 660 | # delete all keys containing this tables column and row info |
| 661 | table_key = _search_all_data_key(table) |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 662 | while 1: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 663 | try: |
| 664 | key, data = cur.set_range(table_key) |
| 665 | except DBNotFoundError: |
| 666 | break |
| 667 | # only delete items in this table |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 668 | if key[:len(table_key)] != table_key: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 669 | break |
| 670 | cur.delete() |
| 671 | |
| 672 | # delete all rowids used by this table |
| 673 | table_key = _search_rowid_key(table) |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 674 | while 1: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 675 | try: |
| 676 | key, data = cur.set_range(table_key) |
| 677 | except DBNotFoundError: |
| 678 | break |
| 679 | # only delete items in this table |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 680 | if key[:len(table_key)] != table_key: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 681 | break |
| 682 | cur.delete() |
| 683 | |
| 684 | cur.close() |
| 685 | |
| 686 | # delete the tablename from the table name list |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 687 | tablelist = pickle.loads( |
| 688 | self.db.get(_table_names_key, txn=txn, flags=DB_RMW)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 689 | try: |
| 690 | tablelist.remove(table) |
| 691 | except ValueError: |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 692 | # hmm, it wasn't there, oh well, that's what we want. |
| 693 | pass |
| 694 | # delete 1st, incase we opened with DB_DUP |
| 695 | self.db.delete(_table_names_key, txn) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 696 | self.db.put(_table_names_key, pickle.dumps(tablelist, 1), txn=txn) |
| 697 | |
| 698 | txn.commit() |
| 699 | txn = None |
| 700 | |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 701 | if self.__tablecolumns.has_key(table): |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 702 | del self.__tablecolumns[table] |
| 703 | |
| 704 | except DBError, dberror: |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 705 | if txn: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 706 | txn.abort() |
| 707 | raise TableDBError, dberror[1] |