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