[pybot-commits] CVS: pybot/pybot __init__.py,1.7,1.8 sqlitedb.py,1.3,1.4
Brought to you by:
niemeyer
From: Gustavo N. <nie...@us...> - 2003-08-24 23:23:27
|
Update of /cvsroot/pybot/pybot/pybot In directory sc8-pr-cvs1:/tmp/cvs-serv24986/pybot Modified Files: __init__.py sqlitedb.py Log Message: * pybot/{sqlite.db,__init__.py}: Improved SQLiteDB.table(). Now it handles spaces in the fields list gracefully without changing the table. Introduced new oncreate parameter to table() that accepts random sql commands on table creation. Changed SQLiteDB constructor to accept the path as a parameter, so that sqlitedb doesn't import pybot anymore. Index: __init__.py =================================================================== RCS file: /cvsroot/pybot/pybot/pybot/__init__.py,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** __init__.py 23 Aug 2003 22:04:09 -0000 1.7 --- __init__.py 24 Aug 2003 23:23:16 -0000 1.8 *************** *** 51,55 **** defaults["datadir"] = ("/var/lib/pybot") ! db = SQLiteDB() if config.has_option("global", "http_proxy"): --- 51,55 ---- defaults["datadir"] = ("/var/lib/pybot") ! db = SQLiteDB(config.get("sqlite", "path")) if config.has_option("global", "http_proxy"): Index: sqlitedb.py =================================================================== RCS file: /cvsroot/pybot/pybot/pybot/sqlitedb.py,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** sqlitedb.py 12 May 2003 20:42:20 -0000 1.3 --- sqlitedb.py 24 Aug 2003 23:23:16 -0000 1.4 *************** *** 17,29 **** # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - import pybot import sqlite import re - FIELDS = re.compile(r"\((.*)\)") - class SQLiteDB: ! def __init__(self): ! self._path = pybot.config.get("sqlite", "path") self._conn = sqlite.connect(self._path) self.error = sqlite.DatabaseError --- 17,26 ---- # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import sqlite import re class SQLiteDB: ! def __init__(self, path): ! self._path = path self._conn = sqlite.connect(self._path) self.error = sqlite.DatabaseError *************** *** 32,36 **** def copy(self): ! return SQLiteDB() def cursor(self): --- 29,33 ---- def copy(self): ! return SQLiteDB(self._path) def cursor(self): *************** *** 43,47 **** self._conn.autocommit = enable ! def table(self, name, fields): """\ Besides creating tables when they don't exist, this function --- 40,44 ---- self._conn.autocommit = enable ! def table(self, name, fields, oncreate=[]): """\ Besides creating tables when they don't exist, this function *************** *** 57,70 **** # No, it doesn't exist yet. cursor.execute("create table %s (%s)" % (name, fields)) ! elif row.sql.find("(%s)" % fields) == -1: # It exist, but is invalid. We'll have to fix it. self.autocommit(0) cursor.execute("create temporary table temp_table (%s)" % fields) ! m = FIELDS.search(row.sql) ! if not m: ! raise ValueError, "invalid sql in table '%s'" % name ! oldfields = [x.strip() for x in m.group(1).split(",")] ! copyfields = ",".join([x for x in fields.split(",") ! if x in oldfields]) cursor.execute("insert into temp_table (%s) select %s from %s" % (copyfields, copyfields, name)) --- 54,68 ---- # No, it doesn't exist yet. cursor.execute("create table %s (%s)" % (name, fields)) ! for sql in oncreate: cursor.execute(sql) ! elif getxform(fields) != getfields(name, row.sql): # It exist, but is invalid. We'll have to fix it. self.autocommit(0) cursor.execute("create temporary table temp_table (%s)" % fields) ! oldfieldnames = [getname(x) for x in ! getfields(name, row.sql).split(",")] ! newfieldnames = [getname(x) for x in ! fields.split(",")] ! copyfields = ",".join([x for x in newfieldnames ! if x in oldfieldnames]) cursor.execute("insert into temp_table (%s) select %s from %s" % (copyfields, copyfields, name)) *************** *** 72,77 **** cursor.execute("create table %s (%s)" % (name, fields)) cursor.execute("insert into %s select %s from temp_table" ! % (name, fields)) cursor.execute("drop table temp_table") self.commit() self.autocommit(1) --- 70,76 ---- cursor.execute("create table %s (%s)" % (name, fields)) cursor.execute("insert into %s select %s from temp_table" ! % (name, ",".join(newfieldnames))) cursor.execute("drop table temp_table") + for sql in oncreate: cursor.execute(sql) self.commit() self.autocommit(1) *************** *** 92,95 **** --- 91,108 ---- cursor = self.cursor() cursor.execute("delete from dict where name=%s", name) + + def getxform(fields): + return ",".join([x.strip() for x in fields.split(",")]) + + GETFIELDS = re.compile(r"\((.*)\)") + def getfields(name, sql): + m = GETFIELDS.search(sql) + if not m: + raise ValueError, "invalid sql in table '%s': %s" % (name, sql) + return getxform(m.group(1)) + + GETNAME = re.compile(r"^\s*(\S+).*$") + def getname(field): + return GETNAME.sub(r"\1", field) # vim:ts=4:sw=4:et |