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