2002-11-19 04:09:52 -04:00
|
|
|
#-----------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Copyright (C) 2000, 2001 by Autonomous Zone Industries
|
2002-11-23 07:26:07 -04:00
|
|
|
# Copyright (C) 2002 Gregory P. Smith
|
2002-11-19 04:09:52 -04:00
|
|
|
#
|
|
|
|
# License: This is free software. You may use this software for any
|
|
|
|
# purpose including modification/redistribution, so long as
|
|
|
|
# this header remains intact and that you do not claim any
|
|
|
|
# rights of ownership or authorship of this software. This
|
|
|
|
# software has been tested, but no warranty is expressed or
|
|
|
|
# implied.
|
|
|
|
#
|
2007-09-09 17:25:00 -03:00
|
|
|
# -- Gregory P. Smith <greg@krypto.org>
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
# This provides a simple database table interface built on top of
|
2008-05-22 12:27:38 -03:00
|
|
|
# the Python Berkeley DB 3 interface.
|
2002-11-19 04:09:52 -04:00
|
|
|
#
|
|
|
|
_cvsid = '$Id$'
|
|
|
|
|
|
|
|
import re
|
2003-01-28 13:20:44 -04:00
|
|
|
import sys
|
2002-11-19 04:09:52 -04:00
|
|
|
import copy
|
2004-08-07 21:54:21 -03:00
|
|
|
import random
|
2007-10-18 04:56:54 -03:00
|
|
|
import struct
|
2003-01-28 13:20:44 -04:00
|
|
|
import cPickle as pickle
|
2002-11-19 04:09:52 -04:00
|
|
|
|
2003-01-28 13:20:44 -04:00
|
|
|
try:
|
2003-09-20 21:08:14 -03:00
|
|
|
# For Pythons w/distutils pybsddb
|
2008-08-31 11:00:51 -03:00
|
|
|
from bsddb3 import db
|
2003-09-20 21:08:14 -03:00
|
|
|
except ImportError:
|
2003-01-28 13:20:44 -04:00
|
|
|
# For Python 2.3
|
2008-08-31 11:00:51 -03:00
|
|
|
from bsddb import db
|
2002-11-19 04:09:52 -04:00
|
|
|
|
2006-06-11 05:35:14 -03:00
|
|
|
# XXX(nnorwitz): is this correct? DBIncompleteError is conditional in _bsddb.c
|
2008-08-31 11:00:51 -03:00
|
|
|
if not hasattr(db,"DBIncompleteError") :
|
2006-06-11 05:35:14 -03:00
|
|
|
class DBIncompleteError(Exception):
|
|
|
|
pass
|
2008-08-31 11:00:51 -03:00
|
|
|
db.DBIncompleteError = DBIncompleteError
|
2002-11-19 04:09:52 -04:00
|
|
|
|
2003-01-28 13:20:44 -04:00
|
|
|
class TableDBError(StandardError):
|
|
|
|
pass
|
|
|
|
class TableAlreadyExists(TableDBError):
|
|
|
|
pass
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Cond:
|
|
|
|
"""This condition matches everything"""
|
|
|
|
def __call__(self, s):
|
|
|
|
return 1
|
|
|
|
|
|
|
|
class ExactCond(Cond):
|
|
|
|
"""Acts as an exact match condition function"""
|
|
|
|
def __init__(self, strtomatch):
|
|
|
|
self.strtomatch = strtomatch
|
|
|
|
def __call__(self, s):
|
|
|
|
return s == self.strtomatch
|
|
|
|
|
|
|
|
class PrefixCond(Cond):
|
|
|
|
"""Acts as a condition function for matching a string prefix"""
|
|
|
|
def __init__(self, prefix):
|
|
|
|
self.prefix = prefix
|
|
|
|
def __call__(self, s):
|
|
|
|
return s[:len(self.prefix)] == self.prefix
|
|
|
|
|
2002-11-23 07:26:07 -04:00
|
|
|
class PostfixCond(Cond):
|
|
|
|
"""Acts as a condition function for matching a string postfix"""
|
|
|
|
def __init__(self, postfix):
|
|
|
|
self.postfix = postfix
|
|
|
|
def __call__(self, s):
|
|
|
|
return s[-len(self.postfix):] == self.postfix
|
|
|
|
|
2002-11-19 04:09:52 -04:00
|
|
|
class LikeCond(Cond):
|
|
|
|
"""
|
|
|
|
Acts as a function that will match using an SQL 'LIKE' style
|
|
|
|
string. Case insensitive and % signs are wild cards.
|
|
|
|
This isn't perfect but it should work for the simple common cases.
|
|
|
|
"""
|
|
|
|
def __init__(self, likestr, re_flags=re.IGNORECASE):
|
|
|
|
# escape python re characters
|
|
|
|
chars_to_escape = '.*+()[]?'
|
|
|
|
for char in chars_to_escape :
|
2003-01-28 13:20:44 -04:00
|
|
|
likestr = likestr.replace(char, '\\'+char)
|
2002-11-19 04:09:52 -04:00
|
|
|
# convert %s to wildcards
|
2003-01-28 13:20:44 -04:00
|
|
|
self.likestr = likestr.replace('%', '.*')
|
2002-11-19 04:09:52 -04:00
|
|
|
self.re = re.compile('^'+self.likestr+'$', re_flags)
|
|
|
|
def __call__(self, s):
|
|
|
|
return self.re.match(s)
|
|
|
|
|
|
|
|
#
|
|
|
|
# keys used to store database metadata
|
|
|
|
#
|
|
|
|
_table_names_key = '__TABLE_NAMES__' # list of the tables in this db
|
|
|
|
_columns = '._COLUMNS__' # table_name+this key contains a list of columns
|
2003-01-28 13:20:44 -04:00
|
|
|
|
|
|
|
def _columns_key(table):
|
|
|
|
return table + _columns
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# these keys are found within table sub databases
|
|
|
|
#
|
|
|
|
_data = '._DATA_.' # this+column+this+rowid key contains table data
|
|
|
|
_rowid = '._ROWID_.' # this+rowid+this key contains a unique entry for each
|
|
|
|
# row in the table. (no data is stored)
|
|
|
|
_rowid_str_len = 8 # length in bytes of the unique rowid strings
|
2003-01-28 13:20:44 -04:00
|
|
|
|
2008-08-31 11:00:51 -03:00
|
|
|
|
2003-01-28 13:20:44 -04:00
|
|
|
def _data_key(table, col, rowid):
|
|
|
|
return table + _data + col + _data + rowid
|
|
|
|
|
|
|
|
def _search_col_data_key(table, col):
|
|
|
|
return table + _data + col + _data
|
|
|
|
|
|
|
|
def _search_all_data_key(table):
|
|
|
|
return table + _data
|
|
|
|
|
|
|
|
def _rowid_key(table, rowid):
|
|
|
|
return table + _rowid + rowid + _rowid
|
|
|
|
|
|
|
|
def _search_rowid_key(table):
|
|
|
|
return table + _rowid
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
def contains_metastrings(s) :
|
|
|
|
"""Verify that the given string does not contain any
|
|
|
|
metadata strings that might interfere with dbtables database operation.
|
|
|
|
"""
|
2003-01-28 13:20:44 -04:00
|
|
|
if (s.find(_table_names_key) >= 0 or
|
|
|
|
s.find(_columns) >= 0 or
|
|
|
|
s.find(_data) >= 0 or
|
|
|
|
s.find(_rowid) >= 0):
|
|
|
|
# Then
|
2002-11-19 04:09:52 -04:00
|
|
|
return 1
|
2003-01-28 13:20:44 -04:00
|
|
|
else:
|
2002-11-19 04:09:52 -04:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
class bsdTableDB :
|
2002-12-30 16:53:52 -04:00
|
|
|
def __init__(self, filename, dbhome, create=0, truncate=0, mode=0600,
|
2003-01-28 13:20:44 -04:00
|
|
|
recover=0, dbflags=0):
|
2006-06-08 02:17:08 -03:00
|
|
|
"""bsdTableDB(filename, dbhome, create=0, truncate=0, mode=0600)
|
|
|
|
|
2008-05-22 12:27:38 -03:00
|
|
|
Open database name in the dbhome Berkeley DB directory.
|
2002-11-19 04:09:52 -04:00
|
|
|
Use keyword arguments when calling this constructor.
|
|
|
|
"""
|
2002-12-30 16:53:52 -04:00
|
|
|
self.db = None
|
2008-08-31 11:00:51 -03:00
|
|
|
myflags = db.DB_THREAD
|
2002-12-30 16:53:52 -04:00
|
|
|
if create:
|
2008-08-31 11:00:51 -03:00
|
|
|
myflags |= db.DB_CREATE
|
|
|
|
flagsforenv = (db.DB_INIT_MPOOL | db.DB_INIT_LOCK | db.DB_INIT_LOG |
|
|
|
|
db.DB_INIT_TXN | dbflags)
|
2002-12-30 16:53:52 -04:00
|
|
|
# DB_AUTO_COMMIT isn't a valid flag for env.open()
|
|
|
|
try:
|
2008-08-31 11:00:51 -03:00
|
|
|
dbflags |= db.DB_AUTO_COMMIT
|
2002-12-30 16:53:52 -04:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
if recover:
|
2008-08-31 11:00:51 -03:00
|
|
|
flagsforenv = flagsforenv | db.DB_RECOVER
|
|
|
|
self.env = db.DBEnv()
|
2002-12-30 16:53:52 -04:00
|
|
|
# enable auto deadlock avoidance
|
2008-08-31 11:00:51 -03:00
|
|
|
self.env.set_lk_detect(db.DB_LOCK_DEFAULT)
|
2002-11-19 04:09:52 -04:00
|
|
|
self.env.open(dbhome, myflags | flagsforenv)
|
2002-12-30 16:53:52 -04:00
|
|
|
if truncate:
|
2008-08-31 11:00:51 -03:00
|
|
|
myflags |= db.DB_TRUNCATE
|
|
|
|
self.db = db.DB(self.env)
|
2003-07-09 01:45:59 -03:00
|
|
|
# this code relies on DBCursor.set* methods to raise exceptions
|
|
|
|
# rather than returning None
|
|
|
|
self.db.set_get_returns_none(1)
|
2002-12-30 16:53:52 -04:00
|
|
|
# allow duplicate entries [warning: be careful w/ metadata]
|
2008-08-31 11:00:51 -03:00
|
|
|
self.db.set_flags(db.DB_DUP)
|
|
|
|
self.db.open(filename, db.DB_BTREE, dbflags | myflags, mode)
|
2002-11-19 04:09:52 -04:00
|
|
|
self.dbfilename = filename
|
2008-08-31 11:00:51 -03:00
|
|
|
|
|
|
|
if sys.version_info[0] >= 3 :
|
|
|
|
class cursor_py3k(object) :
|
|
|
|
def __init__(self, dbcursor) :
|
|
|
|
self._dbcursor = dbcursor
|
|
|
|
|
|
|
|
def close(self) :
|
|
|
|
return self._dbcursor.close()
|
|
|
|
|
|
|
|
def set_range(self, search) :
|
|
|
|
v = self._dbcursor.set_range(bytes(search, "iso8859-1"))
|
|
|
|
if v != None :
|
|
|
|
v = (v[0].decode("iso8859-1"),
|
|
|
|
v[1].decode("iso8859-1"))
|
|
|
|
return v
|
|
|
|
|
|
|
|
def __next__(self) :
|
|
|
|
v = getattr(self._dbcursor, "next")()
|
|
|
|
if v != None :
|
|
|
|
v = (v[0].decode("iso8859-1"),
|
|
|
|
v[1].decode("iso8859-1"))
|
|
|
|
return v
|
|
|
|
|
|
|
|
class db_py3k(object) :
|
|
|
|
def __init__(self, db) :
|
|
|
|
self._db = db
|
|
|
|
|
|
|
|
def cursor(self, txn=None) :
|
|
|
|
return cursor_py3k(self._db.cursor(txn=txn))
|
|
|
|
|
|
|
|
def has_key(self, key, txn=None) :
|
|
|
|
return getattr(self._db,"has_key")(bytes(key, "iso8859-1"),
|
|
|
|
txn=txn)
|
|
|
|
|
|
|
|
def put(self, key, value, flags=0, txn=None) :
|
|
|
|
key = bytes(key, "iso8859-1")
|
|
|
|
if value != None :
|
|
|
|
value = bytes(value, "iso8859-1")
|
|
|
|
return self._db.put(key, value, flags=flags, txn=txn)
|
|
|
|
|
|
|
|
def put_bytes(self, key, value, txn=None) :
|
|
|
|
key = bytes(key, "iso8859-1")
|
|
|
|
return self._db.put(key, value, txn=txn)
|
|
|
|
|
|
|
|
def get(self, key, txn=None, flags=0) :
|
|
|
|
key = bytes(key, "iso8859-1")
|
|
|
|
v = self._db.get(key, txn=txn, flags=flags)
|
|
|
|
if v != None :
|
|
|
|
v = v.decode("iso8859-1")
|
|
|
|
return v
|
|
|
|
|
|
|
|
def get_bytes(self, key, txn=None, flags=0) :
|
|
|
|
key = bytes(key, "iso8859-1")
|
|
|
|
return self._db.get(key, txn=txn, flags=flags)
|
|
|
|
|
|
|
|
def delete(self, key, txn=None) :
|
|
|
|
key = bytes(key, "iso8859-1")
|
|
|
|
return self._db.delete(key, txn=txn)
|
|
|
|
|
|
|
|
def close (self) :
|
|
|
|
return self._db.close()
|
|
|
|
|
|
|
|
self.db = db_py3k(self.db)
|
|
|
|
else : # Python 2.x
|
|
|
|
pass
|
|
|
|
|
2002-11-19 04:09:52 -04:00
|
|
|
# Initialize the table names list if this is a new database
|
2002-12-30 16:53:52 -04:00
|
|
|
txn = self.env.txn_begin()
|
|
|
|
try:
|
2008-08-31 11:00:51 -03:00
|
|
|
if not getattr(self.db, "has_key")(_table_names_key, txn):
|
|
|
|
getattr(self.db, "put_bytes", self.db.put) \
|
|
|
|
(_table_names_key, pickle.dumps([], 1), txn=txn)
|
2002-12-30 16:53:52 -04:00
|
|
|
# Yes, bare except
|
|
|
|
except:
|
|
|
|
txn.abort()
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
txn.commit()
|
2002-11-19 04:09:52 -04:00
|
|
|
# TODO verify more of the database's metadata?
|
|
|
|
self.__tablecolumns = {}
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
if self.db is not None:
|
|
|
|
self.db.close()
|
|
|
|
self.db = None
|
|
|
|
if self.env is not None:
|
|
|
|
self.env.close()
|
|
|
|
self.env = None
|
|
|
|
|
|
|
|
def checkpoint(self, mins=0):
|
|
|
|
try:
|
|
|
|
self.env.txn_checkpoint(mins)
|
2008-08-31 11:00:51 -03:00
|
|
|
except db.DBIncompleteError:
|
2002-11-19 04:09:52 -04:00
|
|
|
pass
|
|
|
|
|
|
|
|
def sync(self):
|
|
|
|
try:
|
|
|
|
self.db.sync()
|
2008-08-31 11:00:51 -03:00
|
|
|
except db.DBIncompleteError:
|
2002-11-19 04:09:52 -04:00
|
|
|
pass
|
|
|
|
|
|
|
|
def _db_print(self) :
|
|
|
|
"""Print the database to stdout for debugging"""
|
|
|
|
print "******** Printing raw database for debugging ********"
|
|
|
|
cur = self.db.cursor()
|
|
|
|
try:
|
|
|
|
key, data = cur.first()
|
2003-01-28 13:20:44 -04:00
|
|
|
while 1:
|
2004-02-12 13:35:32 -04:00
|
|
|
print repr({key: data})
|
2002-11-19 04:09:52 -04:00
|
|
|
next = cur.next()
|
|
|
|
if next:
|
|
|
|
key, data = next
|
|
|
|
else:
|
|
|
|
cur.close()
|
|
|
|
return
|
2008-08-31 11:00:51 -03:00
|
|
|
except db.DBNotFoundError:
|
2002-11-19 04:09:52 -04:00
|
|
|
cur.close()
|
|
|
|
|
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
def CreateTable(self, table, columns):
|
2006-06-08 02:17:08 -03:00
|
|
|
"""CreateTable(table, columns) - Create a new table in the database.
|
|
|
|
|
2002-11-19 04:09:52 -04:00
|
|
|
raises TableDBError if it already exists or for other DB errors.
|
|
|
|
"""
|
2008-07-23 08:38:42 -03:00
|
|
|
assert isinstance(columns, list)
|
2008-08-31 11:00:51 -03:00
|
|
|
|
2002-11-19 04:09:52 -04:00
|
|
|
txn = None
|
|
|
|
try:
|
|
|
|
# checking sanity of the table and column names here on
|
|
|
|
# table creation will prevent problems elsewhere.
|
2002-12-30 16:53:52 -04:00
|
|
|
if contains_metastrings(table):
|
|
|
|
raise ValueError(
|
|
|
|
"bad table name: contains reserved metastrings")
|
2002-11-19 04:09:52 -04:00
|
|
|
for column in columns :
|
2002-12-30 16:53:52 -04:00
|
|
|
if contains_metastrings(column):
|
|
|
|
raise ValueError(
|
|
|
|
"bad column name: contains reserved metastrings")
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
columnlist_key = _columns_key(table)
|
2008-08-31 11:00:51 -03:00
|
|
|
if getattr(self.db, "has_key")(columnlist_key):
|
2002-11-19 04:09:52 -04:00
|
|
|
raise TableAlreadyExists, "table already exists"
|
|
|
|
|
|
|
|
txn = self.env.txn_begin()
|
|
|
|
# store the table's column info
|
2008-08-31 11:00:51 -03:00
|
|
|
getattr(self.db, "put_bytes", self.db.put)(columnlist_key,
|
|
|
|
pickle.dumps(columns, 1), txn=txn)
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
# add the table name to the tablelist
|
2008-08-31 11:00:51 -03:00
|
|
|
tablelist = pickle.loads(getattr(self.db, "get_bytes",
|
|
|
|
self.db.get) (_table_names_key, txn=txn, flags=db.DB_RMW))
|
2002-11-19 04:09:52 -04:00
|
|
|
tablelist.append(table)
|
2002-12-30 16:53:52 -04:00
|
|
|
# delete 1st, in case we opened with DB_DUP
|
2007-10-18 04:56:54 -03:00
|
|
|
self.db.delete(_table_names_key, txn=txn)
|
2008-08-31 11:00:51 -03:00
|
|
|
getattr(self.db, "put_bytes", self.db.put)(_table_names_key,
|
|
|
|
pickle.dumps(tablelist, 1), txn=txn)
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
txn.commit()
|
|
|
|
txn = None
|
2008-08-31 11:00:51 -03:00
|
|
|
except db.DBError, dberror:
|
2003-01-28 13:20:44 -04:00
|
|
|
if txn:
|
2002-11-19 04:09:52 -04:00
|
|
|
txn.abort()
|
2008-08-31 11:00:51 -03:00
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
raise TableDBError, dberror[1]
|
|
|
|
else :
|
|
|
|
raise TableDBError, dberror.args[1]
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
def ListTableColumns(self, table):
|
2002-12-30 16:53:52 -04:00
|
|
|
"""Return a list of columns in the given table.
|
|
|
|
[] if the table doesn't exist.
|
2002-11-19 04:09:52 -04:00
|
|
|
"""
|
2008-07-23 08:38:42 -03:00
|
|
|
assert isinstance(table, str)
|
2003-01-28 13:20:44 -04:00
|
|
|
if contains_metastrings(table):
|
2002-11-19 04:09:52 -04:00
|
|
|
raise ValueError, "bad table name: contains reserved metastrings"
|
|
|
|
|
|
|
|
columnlist_key = _columns_key(table)
|
2008-08-31 11:00:51 -03:00
|
|
|
if not getattr(self.db, "has_key")(columnlist_key):
|
2002-11-19 04:09:52 -04:00
|
|
|
return []
|
2008-08-31 11:00:51 -03:00
|
|
|
pickledcolumnlist = getattr(self.db, "get_bytes",
|
|
|
|
self.db.get)(columnlist_key)
|
2002-11-19 04:09:52 -04:00
|
|
|
if pickledcolumnlist:
|
|
|
|
return pickle.loads(pickledcolumnlist)
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
def ListTables(self):
|
|
|
|
"""Return a list of tables in this database."""
|
2008-08-31 11:00:51 -03:00
|
|
|
pickledtablelist = self.db.get_get(_table_names_key)
|
2002-11-19 04:09:52 -04:00
|
|
|
if pickledtablelist:
|
|
|
|
return pickle.loads(pickledtablelist)
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
def CreateOrExtendTable(self, table, columns):
|
2002-12-30 16:53:52 -04:00
|
|
|
"""CreateOrExtendTable(table, columns)
|
|
|
|
|
2006-06-08 02:17:08 -03:00
|
|
|
Create a new table in the database.
|
|
|
|
|
2002-11-19 04:09:52 -04:00
|
|
|
If a table of this name already exists, extend it to have any
|
|
|
|
additional columns present in the given list as well as
|
|
|
|
all of its current columns.
|
|
|
|
"""
|
2008-07-23 08:38:42 -03:00
|
|
|
assert isinstance(columns, list)
|
2008-08-31 11:00:51 -03:00
|
|
|
|
2002-11-19 04:09:52 -04:00
|
|
|
try:
|
|
|
|
self.CreateTable(table, columns)
|
|
|
|
except TableAlreadyExists:
|
|
|
|
# the table already existed, add any new columns
|
|
|
|
txn = None
|
|
|
|
try:
|
|
|
|
columnlist_key = _columns_key(table)
|
|
|
|
txn = self.env.txn_begin()
|
|
|
|
|
|
|
|
# load the current column list
|
2002-12-30 16:53:52 -04:00
|
|
|
oldcolumnlist = pickle.loads(
|
2008-08-31 11:00:51 -03:00
|
|
|
getattr(self.db, "get_bytes",
|
|
|
|
self.db.get)(columnlist_key, txn=txn, flags=db.DB_RMW))
|
2002-12-30 16:53:52 -04:00
|
|
|
# create a hash table for fast lookups of column names in the
|
|
|
|
# loop below
|
2002-11-19 04:09:52 -04:00
|
|
|
oldcolumnhash = {}
|
|
|
|
for c in oldcolumnlist:
|
|
|
|
oldcolumnhash[c] = c
|
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
# create a new column list containing both the old and new
|
|
|
|
# column names
|
2002-11-19 04:09:52 -04:00
|
|
|
newcolumnlist = copy.copy(oldcolumnlist)
|
|
|
|
for c in columns:
|
|
|
|
if not oldcolumnhash.has_key(c):
|
|
|
|
newcolumnlist.append(c)
|
|
|
|
|
|
|
|
# store the table's new extended column list
|
|
|
|
if newcolumnlist != oldcolumnlist :
|
|
|
|
# delete the old one first since we opened with DB_DUP
|
2007-10-18 04:56:54 -03:00
|
|
|
self.db.delete(columnlist_key, txn=txn)
|
2008-08-31 11:00:51 -03:00
|
|
|
getattr(self.db, "put_bytes", self.db.put)(columnlist_key,
|
2002-12-30 16:53:52 -04:00
|
|
|
pickle.dumps(newcolumnlist, 1),
|
|
|
|
txn=txn)
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
txn.commit()
|
|
|
|
txn = None
|
|
|
|
|
|
|
|
self.__load_column_info(table)
|
2008-08-31 11:00:51 -03:00
|
|
|
except db.DBError, dberror:
|
2002-11-19 04:09:52 -04:00
|
|
|
if txn:
|
|
|
|
txn.abort()
|
2008-08-31 11:00:51 -03:00
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
raise TableDBError, dberror[1]
|
|
|
|
else :
|
|
|
|
raise TableDBError, dberror.args[1]
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
def __load_column_info(self, table) :
|
|
|
|
"""initialize the self.__tablecolumns dict"""
|
|
|
|
# check the column names
|
|
|
|
try:
|
2008-08-31 11:00:51 -03:00
|
|
|
tcolpickles = getattr(self.db, "get_bytes",
|
|
|
|
self.db.get)(_columns_key(table))
|
|
|
|
except db.DBNotFoundError:
|
2004-02-12 13:35:32 -04:00
|
|
|
raise TableDBError, "unknown table: %r" % (table,)
|
2002-11-19 04:09:52 -04:00
|
|
|
if not tcolpickles:
|
2004-02-12 13:35:32 -04:00
|
|
|
raise TableDBError, "unknown table: %r" % (table,)
|
2002-11-19 04:09:52 -04:00
|
|
|
self.__tablecolumns[table] = pickle.loads(tcolpickles)
|
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
def __new_rowid(self, table, txn) :
|
2002-11-19 04:09:52 -04:00
|
|
|
"""Create a new unique row identifier"""
|
|
|
|
unique = 0
|
2003-01-28 13:20:44 -04:00
|
|
|
while not unique:
|
2002-11-19 04:09:52 -04:00
|
|
|
# Generate a random 64-bit row ID string
|
2007-11-01 18:15:36 -03:00
|
|
|
# (note: might have <64 bits of true randomness
|
2002-11-19 04:09:52 -04:00
|
|
|
# but it's plenty for our database id needs!)
|
2007-10-18 13:32:02 -03:00
|
|
|
blist = []
|
|
|
|
for x in xrange(_rowid_str_len):
|
2007-11-01 18:15:36 -03:00
|
|
|
blist.append(random.randint(0,255))
|
2007-10-18 13:32:02 -03:00
|
|
|
newid = struct.pack('B'*_rowid_str_len, *blist)
|
2002-11-19 04:09:52 -04:00
|
|
|
|
2008-08-31 11:00:51 -03:00
|
|
|
if sys.version_info[0] >= 3 :
|
|
|
|
newid = newid.decode("iso8859-1") # 8 bits
|
|
|
|
|
2002-11-19 04:09:52 -04:00
|
|
|
# Guarantee uniqueness by adding this key to the database
|
|
|
|
try:
|
2002-12-30 16:53:52 -04:00
|
|
|
self.db.put(_rowid_key(table, newid), None, txn=txn,
|
2008-08-31 11:00:51 -03:00
|
|
|
flags=db.DB_NOOVERWRITE)
|
|
|
|
except db.DBKeyExistError:
|
2002-11-19 04:09:52 -04:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
unique = 1
|
|
|
|
|
|
|
|
return newid
|
|
|
|
|
|
|
|
|
|
|
|
def Insert(self, table, rowdict) :
|
|
|
|
"""Insert(table, datadict) - Insert a new row into the table
|
|
|
|
using the keys+values from rowdict as the column values.
|
|
|
|
"""
|
2008-08-31 11:00:51 -03:00
|
|
|
|
2002-11-19 04:09:52 -04:00
|
|
|
txn = None
|
|
|
|
try:
|
2008-08-31 11:00:51 -03:00
|
|
|
if not getattr(self.db, "has_key")(_columns_key(table)):
|
2002-11-19 04:09:52 -04:00
|
|
|
raise TableDBError, "unknown table"
|
|
|
|
|
|
|
|
# check the validity of each column name
|
2003-01-28 13:20:44 -04:00
|
|
|
if not self.__tablecolumns.has_key(table):
|
2002-11-19 04:09:52 -04:00
|
|
|
self.__load_column_info(table)
|
|
|
|
for column in rowdict.keys() :
|
2003-01-28 13:20:44 -04:00
|
|
|
if not self.__tablecolumns[table].count(column):
|
2004-02-12 13:35:32 -04:00
|
|
|
raise TableDBError, "unknown column: %r" % (column,)
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
# get a unique row identifier for this row
|
|
|
|
txn = self.env.txn_begin()
|
2002-12-30 16:53:52 -04:00
|
|
|
rowid = self.__new_rowid(table, txn=txn)
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
# insert the row values into the table database
|
2003-01-28 13:20:44 -04:00
|
|
|
for column, dataitem in rowdict.items():
|
2002-11-19 04:09:52 -04:00
|
|
|
# store the value
|
|
|
|
self.db.put(_data_key(table, column, rowid), dataitem, txn=txn)
|
|
|
|
|
|
|
|
txn.commit()
|
|
|
|
txn = None
|
|
|
|
|
2008-08-31 11:00:51 -03:00
|
|
|
except db.DBError, dberror:
|
2002-12-30 16:53:52 -04:00
|
|
|
# WIBNI we could just abort the txn and re-raise the exception?
|
|
|
|
# But no, because TableDBError is not related to DBError via
|
|
|
|
# inheritance, so it would be backwards incompatible. Do the next
|
|
|
|
# best thing.
|
|
|
|
info = sys.exc_info()
|
|
|
|
if txn:
|
2002-11-19 04:09:52 -04:00
|
|
|
txn.abort()
|
|
|
|
self.db.delete(_rowid_key(table, rowid))
|
2008-08-31 11:00:51 -03:00
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
raise TableDBError, dberror[1], info[2]
|
|
|
|
else :
|
|
|
|
raise TableDBError, dberror.args[1], info[2]
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
|
2003-01-28 13:20:44 -04:00
|
|
|
def Modify(self, table, conditions={}, mappings={}):
|
2006-06-08 02:17:08 -03:00
|
|
|
"""Modify(table, conditions={}, mappings={}) - Modify items in rows matching 'conditions' using mapping functions in 'mappings'
|
|
|
|
|
|
|
|
* table - the table name
|
|
|
|
* conditions - a dictionary keyed on column names containing
|
|
|
|
a condition callable expecting the data string as an
|
|
|
|
argument and returning a boolean.
|
|
|
|
* mappings - a dictionary keyed on column names containing a
|
|
|
|
condition callable expecting the data string as an argument and
|
|
|
|
returning the new string for that column.
|
2002-11-19 04:09:52 -04:00
|
|
|
"""
|
2008-08-31 11:00:51 -03:00
|
|
|
|
2002-11-19 04:09:52 -04:00
|
|
|
try:
|
|
|
|
matching_rowids = self.__Select(table, [], conditions)
|
|
|
|
|
|
|
|
# modify only requested columns
|
|
|
|
columns = mappings.keys()
|
2003-01-28 13:20:44 -04:00
|
|
|
for rowid in matching_rowids.keys():
|
2002-11-19 04:09:52 -04:00
|
|
|
txn = None
|
|
|
|
try:
|
2003-01-28 13:20:44 -04:00
|
|
|
for column in columns:
|
2002-11-19 04:09:52 -04:00
|
|
|
txn = self.env.txn_begin()
|
|
|
|
# modify the requested column
|
|
|
|
try:
|
2002-12-30 16:53:52 -04:00
|
|
|
dataitem = self.db.get(
|
|
|
|
_data_key(table, column, rowid),
|
2007-10-18 04:56:54 -03:00
|
|
|
txn=txn)
|
2002-12-30 16:53:52 -04:00
|
|
|
self.db.delete(
|
|
|
|
_data_key(table, column, rowid),
|
2007-10-18 04:56:54 -03:00
|
|
|
txn=txn)
|
2008-08-31 11:00:51 -03:00
|
|
|
except db.DBNotFoundError:
|
2002-12-30 16:53:52 -04:00
|
|
|
# XXXXXXX row key somehow didn't exist, assume no
|
|
|
|
# error
|
|
|
|
dataitem = None
|
2002-11-19 04:09:52 -04:00
|
|
|
dataitem = mappings[column](dataitem)
|
|
|
|
if dataitem <> None:
|
2002-12-30 16:53:52 -04:00
|
|
|
self.db.put(
|
|
|
|
_data_key(table, column, rowid),
|
|
|
|
dataitem, txn=txn)
|
2002-11-19 04:09:52 -04:00
|
|
|
txn.commit()
|
|
|
|
txn = None
|
|
|
|
|
2006-06-08 02:17:08 -03:00
|
|
|
# catch all exceptions here since we call unknown callables
|
|
|
|
except:
|
2003-01-28 13:20:44 -04:00
|
|
|
if txn:
|
2002-11-19 04:09:52 -04:00
|
|
|
txn.abort()
|
|
|
|
raise
|
|
|
|
|
2008-08-31 11:00:51 -03:00
|
|
|
except db.DBError, dberror:
|
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
raise TableDBError, dberror[1]
|
|
|
|
else :
|
|
|
|
raise TableDBError, dberror.args[1]
|
2002-11-19 04:09:52 -04:00
|
|
|
|
2003-01-28 13:20:44 -04:00
|
|
|
def Delete(self, table, conditions={}):
|
2002-11-19 04:09:52 -04:00
|
|
|
"""Delete(table, conditions) - Delete items matching the given
|
|
|
|
conditions from the table.
|
2006-06-08 02:17:08 -03:00
|
|
|
|
|
|
|
* conditions - a dictionary keyed on column names containing
|
|
|
|
condition functions expecting the data string as an
|
|
|
|
argument and returning a boolean.
|
2002-11-19 04:09:52 -04:00
|
|
|
"""
|
2008-08-31 11:00:51 -03:00
|
|
|
|
2002-11-19 04:09:52 -04:00
|
|
|
try:
|
|
|
|
matching_rowids = self.__Select(table, [], conditions)
|
|
|
|
|
|
|
|
# delete row data from all columns
|
|
|
|
columns = self.__tablecolumns[table]
|
2003-01-28 13:20:44 -04:00
|
|
|
for rowid in matching_rowids.keys():
|
2002-11-19 04:09:52 -04:00
|
|
|
txn = None
|
|
|
|
try:
|
|
|
|
txn = self.env.txn_begin()
|
2003-01-28 13:20:44 -04:00
|
|
|
for column in columns:
|
2002-11-19 04:09:52 -04:00
|
|
|
# delete the data key
|
|
|
|
try:
|
2002-12-30 16:53:52 -04:00
|
|
|
self.db.delete(_data_key(table, column, rowid),
|
2007-10-18 04:56:54 -03:00
|
|
|
txn=txn)
|
2008-08-31 11:00:51 -03:00
|
|
|
except db.DBNotFoundError:
|
2002-12-30 16:53:52 -04:00
|
|
|
# XXXXXXX column may not exist, assume no error
|
|
|
|
pass
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
try:
|
2007-10-18 04:56:54 -03:00
|
|
|
self.db.delete(_rowid_key(table, rowid), txn=txn)
|
2008-08-31 11:00:51 -03:00
|
|
|
except db.DBNotFoundError:
|
2002-12-30 16:53:52 -04:00
|
|
|
# XXXXXXX row key somehow didn't exist, assume no error
|
|
|
|
pass
|
2002-11-19 04:09:52 -04:00
|
|
|
txn.commit()
|
|
|
|
txn = None
|
2008-08-31 11:00:51 -03:00
|
|
|
except db.DBError, dberror:
|
2003-01-28 13:20:44 -04:00
|
|
|
if txn:
|
2002-11-19 04:09:52 -04:00
|
|
|
txn.abort()
|
|
|
|
raise
|
2008-08-31 11:00:51 -03:00
|
|
|
except db.DBError, dberror:
|
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
raise TableDBError, dberror[1]
|
|
|
|
else :
|
|
|
|
raise TableDBError, dberror.args[1]
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
|
2003-01-28 13:20:44 -04:00
|
|
|
def Select(self, table, columns, conditions={}):
|
2006-06-08 02:17:08 -03:00
|
|
|
"""Select(table, columns, conditions) - retrieve specific row data
|
2002-11-19 04:09:52 -04:00
|
|
|
Returns a list of row column->value mapping dictionaries.
|
2006-06-08 02:17:08 -03:00
|
|
|
|
|
|
|
* columns - a list of which column data to return. If
|
2002-11-19 04:09:52 -04:00
|
|
|
columns is None, all columns will be returned.
|
2006-06-08 02:17:08 -03:00
|
|
|
* conditions - a dictionary keyed on column names
|
2002-11-19 04:09:52 -04:00
|
|
|
containing callable conditions expecting the data string as an
|
|
|
|
argument and returning a boolean.
|
|
|
|
"""
|
|
|
|
try:
|
2003-01-28 13:20:44 -04:00
|
|
|
if not self.__tablecolumns.has_key(table):
|
2002-11-19 04:09:52 -04:00
|
|
|
self.__load_column_info(table)
|
2003-01-28 13:20:44 -04:00
|
|
|
if columns is None:
|
2002-11-19 04:09:52 -04:00
|
|
|
columns = self.__tablecolumns[table]
|
|
|
|
matching_rowids = self.__Select(table, columns, conditions)
|
2008-08-31 11:00:51 -03:00
|
|
|
except db.DBError, dberror:
|
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
raise TableDBError, dberror[1]
|
|
|
|
else :
|
|
|
|
raise TableDBError, dberror.args[1]
|
2002-11-19 04:09:52 -04:00
|
|
|
# return the matches as a list of dictionaries
|
|
|
|
return matching_rowids.values()
|
|
|
|
|
|
|
|
|
2003-01-28 13:20:44 -04:00
|
|
|
def __Select(self, table, columns, conditions):
|
2002-11-19 04:09:52 -04:00
|
|
|
"""__Select() - Used to implement Select and Delete (above)
|
|
|
|
Returns a dictionary keyed on rowids containing dicts
|
|
|
|
holding the row data for columns listed in the columns param
|
|
|
|
that match the given conditions.
|
|
|
|
* conditions is a dictionary keyed on column names
|
|
|
|
containing callable conditions expecting the data string as an
|
|
|
|
argument and returning a boolean.
|
|
|
|
"""
|
|
|
|
# check the validity of each column name
|
2003-01-28 13:20:44 -04:00
|
|
|
if not self.__tablecolumns.has_key(table):
|
2002-11-19 04:09:52 -04:00
|
|
|
self.__load_column_info(table)
|
2003-01-28 13:20:44 -04:00
|
|
|
if columns is None:
|
2002-11-19 04:09:52 -04:00
|
|
|
columns = self.tablecolumns[table]
|
2003-01-28 13:20:44 -04:00
|
|
|
for column in (columns + conditions.keys()):
|
|
|
|
if not self.__tablecolumns[table].count(column):
|
2004-02-12 13:35:32 -04:00
|
|
|
raise TableDBError, "unknown column: %r" % (column,)
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
# keyed on rows that match so far, containings dicts keyed on
|
|
|
|
# column names containing the data for that row and column.
|
|
|
|
matching_rowids = {}
|
2003-01-28 13:20:44 -04:00
|
|
|
# keys are rowids that do not match
|
|
|
|
rejected_rowids = {}
|
2002-11-19 04:09:52 -04:00
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
# attempt to sort the conditions in such a way as to minimize full
|
|
|
|
# column lookups
|
2002-11-19 04:09:52 -04:00
|
|
|
def cmp_conditions(atuple, btuple):
|
|
|
|
a = atuple[1]
|
|
|
|
b = btuple[1]
|
2003-01-28 13:20:44 -04:00
|
|
|
if type(a) is type(b):
|
2002-11-19 04:09:52 -04:00
|
|
|
if isinstance(a, PrefixCond) and isinstance(b, PrefixCond):
|
2002-12-30 16:53:52 -04:00
|
|
|
# longest prefix first
|
|
|
|
return cmp(len(b.prefix), len(a.prefix))
|
2002-11-19 04:09:52 -04:00
|
|
|
if isinstance(a, LikeCond) and isinstance(b, LikeCond):
|
2002-12-30 16:53:52 -04:00
|
|
|
# longest likestr first
|
|
|
|
return cmp(len(b.likestr), len(a.likestr))
|
2002-11-19 04:09:52 -04:00
|
|
|
return 0
|
|
|
|
if isinstance(a, ExactCond):
|
|
|
|
return -1
|
|
|
|
if isinstance(b, ExactCond):
|
|
|
|
return 1
|
|
|
|
if isinstance(a, PrefixCond):
|
|
|
|
return -1
|
|
|
|
if isinstance(b, PrefixCond):
|
|
|
|
return 1
|
|
|
|
# leave all unknown condition callables alone as equals
|
|
|
|
return 0
|
|
|
|
|
2008-08-31 11:00:51 -03:00
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
conditionlist = conditions.items()
|
|
|
|
conditionlist.sort(cmp_conditions)
|
|
|
|
else : # Insertion Sort. Please, improve
|
|
|
|
conditionlist = []
|
|
|
|
for i in conditions.items() :
|
|
|
|
for j, k in enumerate(conditionlist) :
|
|
|
|
r = cmp_conditions(k, i)
|
|
|
|
if r == 1 :
|
|
|
|
conditionlist.insert(j, i)
|
|
|
|
break
|
|
|
|
else :
|
|
|
|
conditionlist.append(i)
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
# Apply conditions to column data to find what we want
|
|
|
|
cur = self.db.cursor()
|
|
|
|
column_num = -1
|
2003-01-28 13:20:44 -04:00
|
|
|
for column, condition in conditionlist:
|
2002-11-19 04:09:52 -04:00
|
|
|
column_num = column_num + 1
|
|
|
|
searchkey = _search_col_data_key(table, column)
|
|
|
|
# speedup: don't linear search columns within loop
|
2003-01-28 13:20:44 -04:00
|
|
|
if column in columns:
|
2002-11-19 04:09:52 -04:00
|
|
|
savethiscolumndata = 1 # save the data for return
|
2003-01-28 13:20:44 -04:00
|
|
|
else:
|
2002-11-19 04:09:52 -04:00
|
|
|
savethiscolumndata = 0 # data only used for selection
|
|
|
|
|
|
|
|
try:
|
|
|
|
key, data = cur.set_range(searchkey)
|
2003-01-28 13:20:44 -04:00
|
|
|
while key[:len(searchkey)] == searchkey:
|
2002-11-19 04:09:52 -04:00
|
|
|
# extract the rowid from the key
|
|
|
|
rowid = key[-_rowid_str_len:]
|
|
|
|
|
2003-01-28 13:20:44 -04:00
|
|
|
if not rejected_rowids.has_key(rowid):
|
2002-11-19 04:09:52 -04:00
|
|
|
# if no condition was specified or the condition
|
|
|
|
# succeeds, add row to our match list.
|
2003-01-28 13:20:44 -04:00
|
|
|
if not condition or condition(data):
|
|
|
|
if not matching_rowids.has_key(rowid):
|
2002-11-23 07:26:07 -04:00
|
|
|
matching_rowids[rowid] = {}
|
2003-01-28 13:20:44 -04:00
|
|
|
if savethiscolumndata:
|
2002-11-23 07:26:07 -04:00
|
|
|
matching_rowids[rowid][column] = data
|
2003-01-28 13:20:44 -04:00
|
|
|
else:
|
|
|
|
if matching_rowids.has_key(rowid):
|
2002-11-19 04:09:52 -04:00
|
|
|
del matching_rowids[rowid]
|
|
|
|
rejected_rowids[rowid] = rowid
|
|
|
|
|
|
|
|
key, data = cur.next()
|
|
|
|
|
2008-08-31 11:00:51 -03:00
|
|
|
except db.DBError, dberror:
|
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
if dberror[0] != db.DB_NOTFOUND:
|
|
|
|
raise
|
|
|
|
else :
|
|
|
|
if dberror.args[0] != db.DB_NOTFOUND:
|
|
|
|
raise
|
2002-11-19 04:09:52 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
cur.close()
|
|
|
|
|
|
|
|
# we're done selecting rows, garbage collect the reject list
|
|
|
|
del rejected_rowids
|
|
|
|
|
|
|
|
# extract any remaining desired column data from the
|
|
|
|
# database for the matching rows.
|
2003-01-28 13:20:44 -04:00
|
|
|
if len(columns) > 0:
|
|
|
|
for rowid, rowdata in matching_rowids.items():
|
|
|
|
for column in columns:
|
|
|
|
if rowdata.has_key(column):
|
2002-11-19 04:09:52 -04:00
|
|
|
continue
|
|
|
|
try:
|
2002-12-30 16:53:52 -04:00
|
|
|
rowdata[column] = self.db.get(
|
|
|
|
_data_key(table, column, rowid))
|
2008-08-31 11:00:51 -03:00
|
|
|
except db.DBError, dberror:
|
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
if dberror[0] != db.DB_NOTFOUND:
|
|
|
|
raise
|
|
|
|
else :
|
|
|
|
if dberror.args[0] != db.DB_NOTFOUND:
|
|
|
|
raise
|
2002-11-19 04:09:52 -04:00
|
|
|
rowdata[column] = None
|
|
|
|
|
|
|
|
# return the matches
|
|
|
|
return matching_rowids
|
|
|
|
|
|
|
|
|
2003-01-28 13:20:44 -04:00
|
|
|
def Drop(self, table):
|
|
|
|
"""Remove an entire table from the database"""
|
2002-11-19 04:09:52 -04:00
|
|
|
txn = None
|
|
|
|
try:
|
|
|
|
txn = self.env.txn_begin()
|
|
|
|
|
|
|
|
# delete the column list
|
2007-10-18 04:56:54 -03:00
|
|
|
self.db.delete(_columns_key(table), txn=txn)
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
cur = self.db.cursor(txn)
|
|
|
|
|
|
|
|
# delete all keys containing this tables column and row info
|
|
|
|
table_key = _search_all_data_key(table)
|
2003-01-28 13:20:44 -04:00
|
|
|
while 1:
|
2002-11-19 04:09:52 -04:00
|
|
|
try:
|
|
|
|
key, data = cur.set_range(table_key)
|
2008-08-31 11:00:51 -03:00
|
|
|
except db.DBNotFoundError:
|
2002-11-19 04:09:52 -04:00
|
|
|
break
|
|
|
|
# only delete items in this table
|
2003-01-28 13:20:44 -04:00
|
|
|
if key[:len(table_key)] != table_key:
|
2002-11-19 04:09:52 -04:00
|
|
|
break
|
|
|
|
cur.delete()
|
|
|
|
|
|
|
|
# delete all rowids used by this table
|
|
|
|
table_key = _search_rowid_key(table)
|
2003-01-28 13:20:44 -04:00
|
|
|
while 1:
|
2002-11-19 04:09:52 -04:00
|
|
|
try:
|
|
|
|
key, data = cur.set_range(table_key)
|
2008-08-31 11:00:51 -03:00
|
|
|
except db.DBNotFoundError:
|
2002-11-19 04:09:52 -04:00
|
|
|
break
|
|
|
|
# only delete items in this table
|
2003-01-28 13:20:44 -04:00
|
|
|
if key[:len(table_key)] != table_key:
|
2002-11-19 04:09:52 -04:00
|
|
|
break
|
|
|
|
cur.delete()
|
|
|
|
|
|
|
|
cur.close()
|
|
|
|
|
|
|
|
# delete the tablename from the table name list
|
2002-12-30 16:53:52 -04:00
|
|
|
tablelist = pickle.loads(
|
2008-08-31 11:00:51 -03:00
|
|
|
getattr(self.db, "get_bytes", self.db.get)(_table_names_key,
|
|
|
|
txn=txn, flags=db.DB_RMW))
|
2002-11-19 04:09:52 -04:00
|
|
|
try:
|
|
|
|
tablelist.remove(table)
|
|
|
|
except ValueError:
|
2002-12-30 16:53:52 -04:00
|
|
|
# hmm, it wasn't there, oh well, that's what we want.
|
|
|
|
pass
|
|
|
|
# delete 1st, incase we opened with DB_DUP
|
2007-10-18 04:56:54 -03:00
|
|
|
self.db.delete(_table_names_key, txn=txn)
|
2008-08-31 11:00:51 -03:00
|
|
|
getattr(self.db, "put_bytes", self.db.put)(_table_names_key,
|
|
|
|
pickle.dumps(tablelist, 1), txn=txn)
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
txn.commit()
|
|
|
|
txn = None
|
|
|
|
|
2003-01-28 13:20:44 -04:00
|
|
|
if self.__tablecolumns.has_key(table):
|
2002-11-19 04:09:52 -04:00
|
|
|
del self.__tablecolumns[table]
|
|
|
|
|
2008-08-31 11:00:51 -03:00
|
|
|
except db.DBError, dberror:
|
2003-01-28 13:20:44 -04:00
|
|
|
if txn:
|
2002-11-19 04:09:52 -04:00
|
|
|
txn.abort()
|
2008-08-31 11:00:51 -03:00
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
raise TableDBError, dberror[1]
|
|
|
|
else :
|
|
|
|
raise TableDBError, dberror.args[1]
|