Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2..../bsddb
File: dbtables.py
#-----------------------------------------------------------------------
[0] Fix | Delete
#
[1] Fix | Delete
# Copyright (C) 2000, 2001 by Autonomous Zone Industries
[2] Fix | Delete
# Copyright (C) 2002 Gregory P. Smith
[3] Fix | Delete
#
[4] Fix | Delete
# License: This is free software. You may use this software for any
[5] Fix | Delete
# purpose including modification/redistribution, so long as
[6] Fix | Delete
# this header remains intact and that you do not claim any
[7] Fix | Delete
# rights of ownership or authorship of this software. This
[8] Fix | Delete
# software has been tested, but no warranty is expressed or
[9] Fix | Delete
# implied.
[10] Fix | Delete
#
[11] Fix | Delete
# -- Gregory P. Smith <greg@krypto.org>
[12] Fix | Delete
[13] Fix | Delete
# This provides a simple database table interface built on top of
[14] Fix | Delete
# the Python Berkeley DB 3 interface.
[15] Fix | Delete
#
[16] Fix | Delete
_cvsid = '$Id$'
[17] Fix | Delete
[18] Fix | Delete
import re
[19] Fix | Delete
import sys
[20] Fix | Delete
import copy
[21] Fix | Delete
import random
[22] Fix | Delete
import struct
[23] Fix | Delete
[24] Fix | Delete
[25] Fix | Delete
if sys.version_info[0] >= 3 :
[26] Fix | Delete
import pickle
[27] Fix | Delete
else :
[28] Fix | Delete
if sys.version_info < (2, 6) :
[29] Fix | Delete
import cPickle as pickle
[30] Fix | Delete
else :
[31] Fix | Delete
# When we drop support for python 2.4
[32] Fix | Delete
# we could use: (in 2.5 we need a __future__ statement)
[33] Fix | Delete
#
[34] Fix | Delete
# with warnings.catch_warnings():
[35] Fix | Delete
# warnings.filterwarnings(...)
[36] Fix | Delete
# ...
[37] Fix | Delete
#
[38] Fix | Delete
# We can not use "with" as is, because it would be invalid syntax
[39] Fix | Delete
# in python 2.4 and (with no __future__) 2.5.
[40] Fix | Delete
# Here we simulate "with" following PEP 343 :
[41] Fix | Delete
import warnings
[42] Fix | Delete
w = warnings.catch_warnings()
[43] Fix | Delete
w.__enter__()
[44] Fix | Delete
try :
[45] Fix | Delete
warnings.filterwarnings('ignore',
[46] Fix | Delete
message='the cPickle module has been removed in Python 3.0',
[47] Fix | Delete
category=DeprecationWarning)
[48] Fix | Delete
import cPickle as pickle
[49] Fix | Delete
finally :
[50] Fix | Delete
w.__exit__()
[51] Fix | Delete
del w
[52] Fix | Delete
[53] Fix | Delete
try:
[54] Fix | Delete
# For Pythons w/distutils pybsddb
[55] Fix | Delete
from bsddb3 import db
[56] Fix | Delete
except ImportError:
[57] Fix | Delete
# For Python 2.3
[58] Fix | Delete
from bsddb import db
[59] Fix | Delete
[60] Fix | Delete
class TableDBError(StandardError):
[61] Fix | Delete
pass
[62] Fix | Delete
class TableAlreadyExists(TableDBError):
[63] Fix | Delete
pass
[64] Fix | Delete
[65] Fix | Delete
[66] Fix | Delete
class Cond:
[67] Fix | Delete
"""This condition matches everything"""
[68] Fix | Delete
def __call__(self, s):
[69] Fix | Delete
return 1
[70] Fix | Delete
[71] Fix | Delete
class ExactCond(Cond):
[72] Fix | Delete
"""Acts as an exact match condition function"""
[73] Fix | Delete
def __init__(self, strtomatch):
[74] Fix | Delete
self.strtomatch = strtomatch
[75] Fix | Delete
def __call__(self, s):
[76] Fix | Delete
return s == self.strtomatch
[77] Fix | Delete
[78] Fix | Delete
class PrefixCond(Cond):
[79] Fix | Delete
"""Acts as a condition function for matching a string prefix"""
[80] Fix | Delete
def __init__(self, prefix):
[81] Fix | Delete
self.prefix = prefix
[82] Fix | Delete
def __call__(self, s):
[83] Fix | Delete
return s[:len(self.prefix)] == self.prefix
[84] Fix | Delete
[85] Fix | Delete
class PostfixCond(Cond):
[86] Fix | Delete
"""Acts as a condition function for matching a string postfix"""
[87] Fix | Delete
def __init__(self, postfix):
[88] Fix | Delete
self.postfix = postfix
[89] Fix | Delete
def __call__(self, s):
[90] Fix | Delete
return s[-len(self.postfix):] == self.postfix
[91] Fix | Delete
[92] Fix | Delete
class LikeCond(Cond):
[93] Fix | Delete
"""
[94] Fix | Delete
Acts as a function that will match using an SQL 'LIKE' style
[95] Fix | Delete
string. Case insensitive and % signs are wild cards.
[96] Fix | Delete
This isn't perfect but it should work for the simple common cases.
[97] Fix | Delete
"""
[98] Fix | Delete
def __init__(self, likestr, re_flags=re.IGNORECASE):
[99] Fix | Delete
# escape python re characters
[100] Fix | Delete
chars_to_escape = '.*+()[]?'
[101] Fix | Delete
for char in chars_to_escape :
[102] Fix | Delete
likestr = likestr.replace(char, '\\'+char)
[103] Fix | Delete
# convert %s to wildcards
[104] Fix | Delete
self.likestr = likestr.replace('%', '.*')
[105] Fix | Delete
self.re = re.compile('^'+self.likestr+'$', re_flags)
[106] Fix | Delete
def __call__(self, s):
[107] Fix | Delete
return self.re.match(s)
[108] Fix | Delete
[109] Fix | Delete
#
[110] Fix | Delete
# keys used to store database metadata
[111] Fix | Delete
#
[112] Fix | Delete
_table_names_key = '__TABLE_NAMES__' # list of the tables in this db
[113] Fix | Delete
_columns = '._COLUMNS__' # table_name+this key contains a list of columns
[114] Fix | Delete
[115] Fix | Delete
def _columns_key(table):
[116] Fix | Delete
return table + _columns
[117] Fix | Delete
[118] Fix | Delete
#
[119] Fix | Delete
# these keys are found within table sub databases
[120] Fix | Delete
#
[121] Fix | Delete
_data = '._DATA_.' # this+column+this+rowid key contains table data
[122] Fix | Delete
_rowid = '._ROWID_.' # this+rowid+this key contains a unique entry for each
[123] Fix | Delete
# row in the table. (no data is stored)
[124] Fix | Delete
_rowid_str_len = 8 # length in bytes of the unique rowid strings
[125] Fix | Delete
[126] Fix | Delete
[127] Fix | Delete
def _data_key(table, col, rowid):
[128] Fix | Delete
return table + _data + col + _data + rowid
[129] Fix | Delete
[130] Fix | Delete
def _search_col_data_key(table, col):
[131] Fix | Delete
return table + _data + col + _data
[132] Fix | Delete
[133] Fix | Delete
def _search_all_data_key(table):
[134] Fix | Delete
return table + _data
[135] Fix | Delete
[136] Fix | Delete
def _rowid_key(table, rowid):
[137] Fix | Delete
return table + _rowid + rowid + _rowid
[138] Fix | Delete
[139] Fix | Delete
def _search_rowid_key(table):
[140] Fix | Delete
return table + _rowid
[141] Fix | Delete
[142] Fix | Delete
def contains_metastrings(s) :
[143] Fix | Delete
"""Verify that the given string does not contain any
[144] Fix | Delete
metadata strings that might interfere with dbtables database operation.
[145] Fix | Delete
"""
[146] Fix | Delete
if (s.find(_table_names_key) >= 0 or
[147] Fix | Delete
s.find(_columns) >= 0 or
[148] Fix | Delete
s.find(_data) >= 0 or
[149] Fix | Delete
s.find(_rowid) >= 0):
[150] Fix | Delete
# Then
[151] Fix | Delete
return 1
[152] Fix | Delete
else:
[153] Fix | Delete
return 0
[154] Fix | Delete
[155] Fix | Delete
[156] Fix | Delete
class bsdTableDB :
[157] Fix | Delete
def __init__(self, filename, dbhome, create=0, truncate=0, mode=0600,
[158] Fix | Delete
recover=0, dbflags=0):
[159] Fix | Delete
"""bsdTableDB(filename, dbhome, create=0, truncate=0, mode=0600)
[160] Fix | Delete
[161] Fix | Delete
Open database name in the dbhome Berkeley DB directory.
[162] Fix | Delete
Use keyword arguments when calling this constructor.
[163] Fix | Delete
"""
[164] Fix | Delete
self.db = None
[165] Fix | Delete
myflags = db.DB_THREAD
[166] Fix | Delete
if create:
[167] Fix | Delete
myflags |= db.DB_CREATE
[168] Fix | Delete
flagsforenv = (db.DB_INIT_MPOOL | db.DB_INIT_LOCK | db.DB_INIT_LOG |
[169] Fix | Delete
db.DB_INIT_TXN | dbflags)
[170] Fix | Delete
# DB_AUTO_COMMIT isn't a valid flag for env.open()
[171] Fix | Delete
try:
[172] Fix | Delete
dbflags |= db.DB_AUTO_COMMIT
[173] Fix | Delete
except AttributeError:
[174] Fix | Delete
pass
[175] Fix | Delete
if recover:
[176] Fix | Delete
flagsforenv = flagsforenv | db.DB_RECOVER
[177] Fix | Delete
self.env = db.DBEnv()
[178] Fix | Delete
# enable auto deadlock avoidance
[179] Fix | Delete
self.env.set_lk_detect(db.DB_LOCK_DEFAULT)
[180] Fix | Delete
self.env.open(dbhome, myflags | flagsforenv)
[181] Fix | Delete
if truncate:
[182] Fix | Delete
myflags |= db.DB_TRUNCATE
[183] Fix | Delete
self.db = db.DB(self.env)
[184] Fix | Delete
# this code relies on DBCursor.set* methods to raise exceptions
[185] Fix | Delete
# rather than returning None
[186] Fix | Delete
self.db.set_get_returns_none(1)
[187] Fix | Delete
# allow duplicate entries [warning: be careful w/ metadata]
[188] Fix | Delete
self.db.set_flags(db.DB_DUP)
[189] Fix | Delete
self.db.open(filename, db.DB_BTREE, dbflags | myflags, mode)
[190] Fix | Delete
self.dbfilename = filename
[191] Fix | Delete
[192] Fix | Delete
if sys.version_info[0] >= 3 :
[193] Fix | Delete
class cursor_py3k(object) :
[194] Fix | Delete
def __init__(self, dbcursor) :
[195] Fix | Delete
self._dbcursor = dbcursor
[196] Fix | Delete
[197] Fix | Delete
def close(self) :
[198] Fix | Delete
return self._dbcursor.close()
[199] Fix | Delete
[200] Fix | Delete
def set_range(self, search) :
[201] Fix | Delete
v = self._dbcursor.set_range(bytes(search, "iso8859-1"))
[202] Fix | Delete
if v is not None :
[203] Fix | Delete
v = (v[0].decode("iso8859-1"),
[204] Fix | Delete
v[1].decode("iso8859-1"))
[205] Fix | Delete
return v
[206] Fix | Delete
[207] Fix | Delete
def __next__(self) :
[208] Fix | Delete
v = getattr(self._dbcursor, "next")()
[209] Fix | Delete
if v is not None :
[210] Fix | Delete
v = (v[0].decode("iso8859-1"),
[211] Fix | Delete
v[1].decode("iso8859-1"))
[212] Fix | Delete
return v
[213] Fix | Delete
[214] Fix | Delete
class db_py3k(object) :
[215] Fix | Delete
def __init__(self, db) :
[216] Fix | Delete
self._db = db
[217] Fix | Delete
[218] Fix | Delete
def cursor(self, txn=None) :
[219] Fix | Delete
return cursor_py3k(self._db.cursor(txn=txn))
[220] Fix | Delete
[221] Fix | Delete
def has_key(self, key, txn=None) :
[222] Fix | Delete
return getattr(self._db,"has_key")(bytes(key, "iso8859-1"),
[223] Fix | Delete
txn=txn)
[224] Fix | Delete
[225] Fix | Delete
def put(self, key, value, flags=0, txn=None) :
[226] Fix | Delete
key = bytes(key, "iso8859-1")
[227] Fix | Delete
if value is not None :
[228] Fix | Delete
value = bytes(value, "iso8859-1")
[229] Fix | Delete
return self._db.put(key, value, flags=flags, txn=txn)
[230] Fix | Delete
[231] Fix | Delete
def put_bytes(self, key, value, txn=None) :
[232] Fix | Delete
key = bytes(key, "iso8859-1")
[233] Fix | Delete
return self._db.put(key, value, txn=txn)
[234] Fix | Delete
[235] Fix | Delete
def get(self, key, txn=None, flags=0) :
[236] Fix | Delete
key = bytes(key, "iso8859-1")
[237] Fix | Delete
v = self._db.get(key, txn=txn, flags=flags)
[238] Fix | Delete
if v is not None :
[239] Fix | Delete
v = v.decode("iso8859-1")
[240] Fix | Delete
return v
[241] Fix | Delete
[242] Fix | Delete
def get_bytes(self, key, txn=None, flags=0) :
[243] Fix | Delete
key = bytes(key, "iso8859-1")
[244] Fix | Delete
return self._db.get(key, txn=txn, flags=flags)
[245] Fix | Delete
[246] Fix | Delete
def delete(self, key, txn=None) :
[247] Fix | Delete
key = bytes(key, "iso8859-1")
[248] Fix | Delete
return self._db.delete(key, txn=txn)
[249] Fix | Delete
[250] Fix | Delete
def close (self) :
[251] Fix | Delete
return self._db.close()
[252] Fix | Delete
[253] Fix | Delete
self.db = db_py3k(self.db)
[254] Fix | Delete
else : # Python 2.x
[255] Fix | Delete
pass
[256] Fix | Delete
[257] Fix | Delete
# Initialize the table names list if this is a new database
[258] Fix | Delete
txn = self.env.txn_begin()
[259] Fix | Delete
try:
[260] Fix | Delete
if not getattr(self.db, "has_key")(_table_names_key, txn):
[261] Fix | Delete
getattr(self.db, "put_bytes", self.db.put) \
[262] Fix | Delete
(_table_names_key, pickle.dumps([], 1), txn=txn)
[263] Fix | Delete
# Yes, bare except
[264] Fix | Delete
except:
[265] Fix | Delete
txn.abort()
[266] Fix | Delete
raise
[267] Fix | Delete
else:
[268] Fix | Delete
txn.commit()
[269] Fix | Delete
# TODO verify more of the database's metadata?
[270] Fix | Delete
self.__tablecolumns = {}
[271] Fix | Delete
[272] Fix | Delete
def __del__(self):
[273] Fix | Delete
self.close()
[274] Fix | Delete
[275] Fix | Delete
def close(self):
[276] Fix | Delete
if self.db is not None:
[277] Fix | Delete
self.db.close()
[278] Fix | Delete
self.db = None
[279] Fix | Delete
if self.env is not None:
[280] Fix | Delete
self.env.close()
[281] Fix | Delete
self.env = None
[282] Fix | Delete
[283] Fix | Delete
def checkpoint(self, mins=0):
[284] Fix | Delete
self.env.txn_checkpoint(mins)
[285] Fix | Delete
[286] Fix | Delete
def sync(self):
[287] Fix | Delete
self.db.sync()
[288] Fix | Delete
[289] Fix | Delete
def _db_print(self) :
[290] Fix | Delete
"""Print the database to stdout for debugging"""
[291] Fix | Delete
print "******** Printing raw database for debugging ********"
[292] Fix | Delete
cur = self.db.cursor()
[293] Fix | Delete
try:
[294] Fix | Delete
key, data = cur.first()
[295] Fix | Delete
while 1:
[296] Fix | Delete
print repr({key: data})
[297] Fix | Delete
next = cur.next()
[298] Fix | Delete
if next:
[299] Fix | Delete
key, data = next
[300] Fix | Delete
else:
[301] Fix | Delete
cur.close()
[302] Fix | Delete
return
[303] Fix | Delete
except db.DBNotFoundError:
[304] Fix | Delete
cur.close()
[305] Fix | Delete
[306] Fix | Delete
[307] Fix | Delete
def CreateTable(self, table, columns):
[308] Fix | Delete
"""CreateTable(table, columns) - Create a new table in the database.
[309] Fix | Delete
[310] Fix | Delete
raises TableDBError if it already exists or for other DB errors.
[311] Fix | Delete
"""
[312] Fix | Delete
assert isinstance(columns, list)
[313] Fix | Delete
[314] Fix | Delete
txn = None
[315] Fix | Delete
try:
[316] Fix | Delete
# checking sanity of the table and column names here on
[317] Fix | Delete
# table creation will prevent problems elsewhere.
[318] Fix | Delete
if contains_metastrings(table):
[319] Fix | Delete
raise ValueError(
[320] Fix | Delete
"bad table name: contains reserved metastrings")
[321] Fix | Delete
for column in columns :
[322] Fix | Delete
if contains_metastrings(column):
[323] Fix | Delete
raise ValueError(
[324] Fix | Delete
"bad column name: contains reserved metastrings")
[325] Fix | Delete
[326] Fix | Delete
columnlist_key = _columns_key(table)
[327] Fix | Delete
if getattr(self.db, "has_key")(columnlist_key):
[328] Fix | Delete
raise TableAlreadyExists, "table already exists"
[329] Fix | Delete
[330] Fix | Delete
txn = self.env.txn_begin()
[331] Fix | Delete
# store the table's column info
[332] Fix | Delete
getattr(self.db, "put_bytes", self.db.put)(columnlist_key,
[333] Fix | Delete
pickle.dumps(columns, 1), txn=txn)
[334] Fix | Delete
[335] Fix | Delete
# add the table name to the tablelist
[336] Fix | Delete
tablelist = pickle.loads(getattr(self.db, "get_bytes",
[337] Fix | Delete
self.db.get) (_table_names_key, txn=txn, flags=db.DB_RMW))
[338] Fix | Delete
tablelist.append(table)
[339] Fix | Delete
# delete 1st, in case we opened with DB_DUP
[340] Fix | Delete
self.db.delete(_table_names_key, txn=txn)
[341] Fix | Delete
getattr(self.db, "put_bytes", self.db.put)(_table_names_key,
[342] Fix | Delete
pickle.dumps(tablelist, 1), txn=txn)
[343] Fix | Delete
[344] Fix | Delete
txn.commit()
[345] Fix | Delete
txn = None
[346] Fix | Delete
except db.DBError, dberror:
[347] Fix | Delete
if txn:
[348] Fix | Delete
txn.abort()
[349] Fix | Delete
if sys.version_info < (2, 6) :
[350] Fix | Delete
raise TableDBError, dberror[1]
[351] Fix | Delete
else :
[352] Fix | Delete
raise TableDBError, dberror.args[1]
[353] Fix | Delete
[354] Fix | Delete
[355] Fix | Delete
def ListTableColumns(self, table):
[356] Fix | Delete
"""Return a list of columns in the given table.
[357] Fix | Delete
[] if the table doesn't exist.
[358] Fix | Delete
"""
[359] Fix | Delete
assert isinstance(table, str)
[360] Fix | Delete
if contains_metastrings(table):
[361] Fix | Delete
raise ValueError, "bad table name: contains reserved metastrings"
[362] Fix | Delete
[363] Fix | Delete
columnlist_key = _columns_key(table)
[364] Fix | Delete
if not getattr(self.db, "has_key")(columnlist_key):
[365] Fix | Delete
return []
[366] Fix | Delete
pickledcolumnlist = getattr(self.db, "get_bytes",
[367] Fix | Delete
self.db.get)(columnlist_key)
[368] Fix | Delete
if pickledcolumnlist:
[369] Fix | Delete
return pickle.loads(pickledcolumnlist)
[370] Fix | Delete
else:
[371] Fix | Delete
return []
[372] Fix | Delete
[373] Fix | Delete
def ListTables(self):
[374] Fix | Delete
"""Return a list of tables in this database."""
[375] Fix | Delete
pickledtablelist = self.db.get_get(_table_names_key)
[376] Fix | Delete
if pickledtablelist:
[377] Fix | Delete
return pickle.loads(pickledtablelist)
[378] Fix | Delete
else:
[379] Fix | Delete
return []
[380] Fix | Delete
[381] Fix | Delete
def CreateOrExtendTable(self, table, columns):
[382] Fix | Delete
"""CreateOrExtendTable(table, columns)
[383] Fix | Delete
[384] Fix | Delete
Create a new table in the database.
[385] Fix | Delete
[386] Fix | Delete
If a table of this name already exists, extend it to have any
[387] Fix | Delete
additional columns present in the given list as well as
[388] Fix | Delete
all of its current columns.
[389] Fix | Delete
"""
[390] Fix | Delete
assert isinstance(columns, list)
[391] Fix | Delete
[392] Fix | Delete
try:
[393] Fix | Delete
self.CreateTable(table, columns)
[394] Fix | Delete
except TableAlreadyExists:
[395] Fix | Delete
# the table already existed, add any new columns
[396] Fix | Delete
txn = None
[397] Fix | Delete
try:
[398] Fix | Delete
columnlist_key = _columns_key(table)
[399] Fix | Delete
txn = self.env.txn_begin()
[400] Fix | Delete
[401] Fix | Delete
# load the current column list
[402] Fix | Delete
oldcolumnlist = pickle.loads(
[403] Fix | Delete
getattr(self.db, "get_bytes",
[404] Fix | Delete
self.db.get)(columnlist_key, txn=txn, flags=db.DB_RMW))
[405] Fix | Delete
# create a hash table for fast lookups of column names in the
[406] Fix | Delete
# loop below
[407] Fix | Delete
oldcolumnhash = {}
[408] Fix | Delete
for c in oldcolumnlist:
[409] Fix | Delete
oldcolumnhash[c] = c
[410] Fix | Delete
[411] Fix | Delete
# create a new column list containing both the old and new
[412] Fix | Delete
# column names
[413] Fix | Delete
newcolumnlist = copy.copy(oldcolumnlist)
[414] Fix | Delete
for c in columns:
[415] Fix | Delete
if not c in oldcolumnhash:
[416] Fix | Delete
newcolumnlist.append(c)
[417] Fix | Delete
[418] Fix | Delete
# store the table's new extended column list
[419] Fix | Delete
if newcolumnlist != oldcolumnlist :
[420] Fix | Delete
# delete the old one first since we opened with DB_DUP
[421] Fix | Delete
self.db.delete(columnlist_key, txn=txn)
[422] Fix | Delete
getattr(self.db, "put_bytes", self.db.put)(columnlist_key,
[423] Fix | Delete
pickle.dumps(newcolumnlist, 1),
[424] Fix | Delete
txn=txn)
[425] Fix | Delete
[426] Fix | Delete
txn.commit()
[427] Fix | Delete
txn = None
[428] Fix | Delete
[429] Fix | Delete
self.__load_column_info(table)
[430] Fix | Delete
except db.DBError, dberror:
[431] Fix | Delete
if txn:
[432] Fix | Delete
txn.abort()
[433] Fix | Delete
if sys.version_info < (2, 6) :
[434] Fix | Delete
raise TableDBError, dberror[1]
[435] Fix | Delete
else :
[436] Fix | Delete
raise TableDBError, dberror.args[1]
[437] Fix | Delete
[438] Fix | Delete
[439] Fix | Delete
def __load_column_info(self, table) :
[440] Fix | Delete
"""initialize the self.__tablecolumns dict"""
[441] Fix | Delete
# check the column names
[442] Fix | Delete
try:
[443] Fix | Delete
tcolpickles = getattr(self.db, "get_bytes",
[444] Fix | Delete
self.db.get)(_columns_key(table))
[445] Fix | Delete
except db.DBNotFoundError:
[446] Fix | Delete
raise TableDBError, "unknown table: %r" % (table,)
[447] Fix | Delete
if not tcolpickles:
[448] Fix | Delete
raise TableDBError, "unknown table: %r" % (table,)
[449] Fix | Delete
self.__tablecolumns[table] = pickle.loads(tcolpickles)
[450] Fix | Delete
[451] Fix | Delete
def __new_rowid(self, table, txn) :
[452] Fix | Delete
"""Create a new unique row identifier"""
[453] Fix | Delete
unique = 0
[454] Fix | Delete
while not unique:
[455] Fix | Delete
# Generate a random 64-bit row ID string
[456] Fix | Delete
# (note: might have <64 bits of true randomness
[457] Fix | Delete
# but it's plenty for our database id needs!)
[458] Fix | Delete
blist = []
[459] Fix | Delete
for x in xrange(_rowid_str_len):
[460] Fix | Delete
blist.append(random.randint(0,255))
[461] Fix | Delete
newid = struct.pack('B'*_rowid_str_len, *blist)
[462] Fix | Delete
[463] Fix | Delete
if sys.version_info[0] >= 3 :
[464] Fix | Delete
newid = newid.decode("iso8859-1") # 8 bits
[465] Fix | Delete
[466] Fix | Delete
# Guarantee uniqueness by adding this key to the database
[467] Fix | Delete
try:
[468] Fix | Delete
self.db.put(_rowid_key(table, newid), None, txn=txn,
[469] Fix | Delete
flags=db.DB_NOOVERWRITE)
[470] Fix | Delete
except db.DBKeyExistError:
[471] Fix | Delete
pass
[472] Fix | Delete
else:
[473] Fix | Delete
unique = 1
[474] Fix | Delete
[475] Fix | Delete
return newid
[476] Fix | Delete
[477] Fix | Delete
[478] Fix | Delete
def Insert(self, table, rowdict) :
[479] Fix | Delete
"""Insert(table, datadict) - Insert a new row into the table
[480] Fix | Delete
using the keys+values from rowdict as the column values.
[481] Fix | Delete
"""
[482] Fix | Delete
[483] Fix | Delete
txn = None
[484] Fix | Delete
try:
[485] Fix | Delete
if not getattr(self.db, "has_key")(_columns_key(table)):
[486] Fix | Delete
raise TableDBError, "unknown table"
[487] Fix | Delete
[488] Fix | Delete
# check the validity of each column name
[489] Fix | Delete
if not table in self.__tablecolumns:
[490] Fix | Delete
self.__load_column_info(table)
[491] Fix | Delete
for column in rowdict.keys() :
[492] Fix | Delete
if not self.__tablecolumns[table].count(column):
[493] Fix | Delete
raise TableDBError, "unknown column: %r" % (column,)
[494] Fix | Delete
[495] Fix | Delete
# get a unique row identifier for this row
[496] Fix | Delete
txn = self.env.txn_begin()
[497] Fix | Delete
rowid = self.__new_rowid(table, txn=txn)
[498] Fix | Delete
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function