4 usage: %(progname)s [args] 8 import os, sys, string, time, getopt
19 odb.OperationalError = _sqlite3.OperationalError
20 odb.DatabaseError = _sqlite3.DatabaseError
21 odb.DataError = _sqlite3.DataError
22 odb.IntegrityError = _sqlite3.IntegrityError
23 odb.NotSupportedError = _sqlite3.NotSupportedError
27 return self.cursor.lastrowid
31 return self.cursor.execute(sql)
32 except _sqlite3.DatabaseError, reason:
33 reason = str(reason) +
"(%s)" % sql
34 raise _sqlite3.DatabaseError, reason
42 class Connection(odb.Connection):
44 odb.Connection.__init__(self)
47 self.
_conn = apply(sqlite.connect, args, kwargs)
49 warn(
"unable to open db", args)
57 return Cursor(self._conn.cursor())
60 return sqlite3.encode(str)
62 return sqlite3.decode(str)
67 elif type(s) == types.StringType:
68 return string.replace(s,
"'",
"''")
69 elif type(s)
in (types.IntType, types.FloatType):
71 elif type(s) == types.UnicodeType:
74 warn(
"unknown column data type: <%s> value=%s" % (type(s), s[:100]))
79 cursor.execute(
"select name from sqlite_master where type='table'")
80 rows = cursor.fetchall()
82 for row
in rows: tables.append(row[0])
87 def listTriggers(self, cursor):
88 cursor.execute(
"select name from sqlite_master where type='trigger'")
89 rows = cursor.fetchall()
91 for row
in rows: tables.append(row[0])
95 cursor.execute(
"select name from sqlite_master where type='index'")
96 rows = cursor.fetchall()
99 if row[0].find(
"sqlite_autoindex_") != -1:
101 tables.append(row[0])
105 sql =
"pragma table_info(%s)" % table_name
107 rows = cursor.fetchall()
112 columns[colname] = row
116 sql =
"select sql from sqlite_master where type='table' and name='%s'" % table_name
119 row = cursor.fetchone()
120 sqlstatement = row[0]
125 tableName = table.getTableName()
126 debug(
"alterTableToMatch", tableName)
127 tmpTableName = tableName +
"_" + str(os.getpid())
129 invalidAppCols, invalidDBCols = table.checkTable(warnflag=0)
136 if not invalidAppCols
and not invalidDBCols:
140 oldcols = self.listFieldsDict(tableName, cursor)
144 newcols = table.getAppColumnList()
145 for colname, coltype, options
in newcols:
146 if oldcols.has_key(colname): tmpcols.append(colname)
148 tmpcolnames = string.join(tmpcols,
",")
150 count = table.fetchRowCount()
151 warn(
"count for %s=" % table.getTableName(), count)
159 sql =
"create table %s (%s)" % (tmpTableName, tmpcolnames)
160 statements.append(sql)
162 sql =
"insert into %s select %s from %s" % (tmpTableName, tmpcolnames, tableName)
163 statements.append(sql)
165 sql =
"drop table %s" % tableName
166 statements.append(sql)
168 sql = table._createTableSQL()
169 statements.append(sql)
171 sql =
"insert into %s(%s) select %s from %s" % (tableName, tmpcolnames, tmpcolnames, tmpTableName)
172 statements.append(sql)
174 sql =
"drop table %s" % tmpTableName
175 statements.append(sql)
181 for statement
in statements:
183 cursor.execute(statement)
191 for colname, coltype, options
in column_list:
192 if colname
in (
"rowid",
"docid"):
continue 194 defs = string.join(defs,
", ")
196 return "CREATE virtual TABLE %s using FTS3(%s)" % (tableName, defs)
202 print __doc__ % vars()
204 def main(argv, stdout, environ):
206 optlist, args = getopt.getopt(argv[1:],
"", [
"help",
"test",
"debug"])
212 for (field, val)
in optlist:
213 if field ==
"--help":
216 elif field ==
"--debug":
218 elif field ==
"--test":
226 if __name__ ==
"__main__":
227 main(sys.argv, sys.stdout, os.environ)
def auto_increment(self, coltype)
def insert_id(self, tablename, colname)
def __init__(self, args, kwargs)
def main(argv, stdout, environ)
def listIndices(self, tableName, cursor)
def listFieldsDict(self, table_name, cursor)
def alterTableToMatch(self, table, cursor)
def listTables(self, cursor)
def create_fullTextSearchTable(self, tableName, column_list)
def supportsTriggers(self)
def _tableCreateStatement(self, table_name, cursor)