odb_sqlite.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 """
00004 usage: %(progname)s [args]
00005 """
00006 
00007 
00008 import os, sys, string, time, getopt
00009 from log import *
00010 
00011 import odb
00012 #import sqliterep as sqlite
00013 import sqlite
00014 
00015 
00016 class Cursor(odb.Cursor):
00017   def insert_id(self, tablename, colname):
00018     return self.cursor.lastrowid
00019 
00020   def execute(self, sql):
00021 ##     if sql[:6] != "select":
00022 ##       warn(repr(sql))
00023     return self.cursor.execute(sql)
00024 
00025 class Connection(odb.Connection):
00026   def __init__(self, *args, **kwargs):
00027     odb.Connection.__init__(self)
00028 
00029     self._conn = apply(sqlite.connect, args, kwargs)
00030 
00031     self.SQLError = sqlite.Error
00032 
00033   def getConnType(self): return "sqlite"
00034     
00035   def cursor(self):
00036     return Cursor(self._conn.cursor())
00037 
00038   def escape(self,str):
00039     if str is None:
00040       return None
00041     elif type(str) == type(""):
00042       return string.replace(str,"'","''")
00043     elif type(str) == type(1):
00044       return str
00045     else:
00046       raise "unknown column data type: %s" % type(str)
00047 
00048 
00049   def listTables(self, cursor):
00050     cursor.execute("select name from sqlite_master where type='table'")
00051     rows = cursor.fetchall()
00052     tables = []
00053     for row in rows: tables.append(row[0])
00054     return tables
00055 
00056   def supportsTriggers(self): return True
00057 
00058   def listTriggers(self, cursor):
00059     cursor.execute("select name from sqlite_master where type='trigger'")
00060     rows = cursor.fetchall()
00061     tables = []
00062     for row in rows: tables.append(row[0])
00063     return tables
00064 
00065   def listIndices(self, tableName, cursor):
00066     cursor.execute("select name from sqlite_master where type='index'")
00067     rows = cursor.fetchall()
00068     tables = []
00069     for row in rows: tables.append(row[0])
00070     return tables
00071 
00072   def listFieldsDict(self, table_name, cursor):
00073     sql = "pragma table_info(%s)" % table_name
00074     cursor.execute(sql)
00075     rows = cursor.fetchall()
00076 
00077     columns = {}
00078     for row in rows:
00079       colname = row[1]
00080       columns[colname] = row
00081     return columns
00082 
00083   def _tableCreateStatement(self, table_name, cursor):
00084     sql = "select sql from sqlite_master where type='table' and name='%s'" % table_name
00085     print sql
00086     cursor.execute(sql)
00087     row = cursor.fetchone()
00088     sqlstatement = row[0]
00089     return sqlstatement
00090     
00091 
00092   def alterTableToMatch(self, table, cursor):
00093     tableName = table.getTableName()
00094     tmpTableName = tableName + "_" + str(os.getpid())
00095 
00096     invalidAppCols, invalidDBCols = table.checkTable(warnflag=0)
00097 #    warn(invalidAppCols, invalidDBCols)
00098          
00099 
00100 ##     if invalidAppCols or invalidDBCols:
00101 ##       return
00102 
00103     if not invalidAppCols and not invalidDBCols:
00104       return
00105 
00106 
00107     oldcols = self.listFieldsDict(tableName, cursor)
00108 #    tmpcols = oldcols.keys()
00109     
00110     tmpcols = []
00111     newcols = table.getAppColumnList()
00112     for colname, coltype, options in newcols:
00113       if oldcols.has_key(colname): tmpcols.append(colname)
00114     
00115     tmpcolnames = string.join(tmpcols, ",")
00116       
00117     statements = []
00118 
00119 #    sql = "begin transaction"
00120 #    statements.append(sql)
00121 
00122     sql = "create temporary table %s (%s)" % (tmpTableName, tmpcolnames)
00123     statements.append(sql)
00124 
00125     sql = "insert into %s select %s from %s" % (tmpTableName, tmpcolnames, tableName)
00126     statements.append(sql)
00127 
00128     sql = "drop table %s" % tableName
00129     statements.append(sql)
00130     
00131     sql = table._createTableSQL()
00132     statements.append(sql)
00133 
00134     sql = "insert into %s(%s) select %s from %s" % (tableName, tmpcolnames, tmpcolnames, tmpTableName)
00135     statements.append(sql)
00136 
00137     sql = "drop table %s" % tmpTableName
00138     statements.append(sql)
00139 
00140 #    sql = "commit"
00141 #    statements.append(sql)
00142 
00143     for statement in statements:
00144       print statement
00145       cursor.execute(statement)
00146 
00147 
00148 def test():
00149   pass
00150 
00151 def usage(progname):
00152   print __doc__ % vars()
00153 
00154 def main(argv, stdout, environ):
00155   progname = argv[0]
00156   optlist, args = getopt.getopt(argv[1:], "", ["help", "test", "debug"])
00157 
00158   testflag = 0
00159   if len(args) == 0:
00160     usage(progname)
00161     return
00162   for (field, val) in optlist:
00163     if field == "--help":
00164       usage(progname)
00165       return
00166     elif field == "--debug":
00167       debugfull()
00168     elif field == "--test":
00169       testflag = 1
00170 
00171   if testflag:
00172     test()
00173     return
00174 
00175 
00176 if __name__ == "__main__":
00177   main(sys.argv, sys.stdout, os.environ)


pyclearsilver
Author(s): Scott Hassan/hassan@willowgarage.com
autogenerated on Wed Apr 23 2014 10:35:42