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