odb_sqlite.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 """
4 usage: %(progname)s [args]
5 """
6 
7 
8 import os, sys, string, time, getopt
9 from log import *
10 
11 import odb
12 #import sqliterep as sqlite
13 import sqlite
14 
15 
17  def insert_id(self, tablename, colname):
18  return self.cursor.lastrowid
19 
20  def execute(self, sql):
21 ## if sql[:6] != "select":
22 ## warn(repr(sql))
23  return self.cursor.execute(sql)
24 
26  def __init__(self, *args, **kwargs):
27  odb.Connection.__init__(self)
28 
29  self._conn = apply(sqlite.connect, args, kwargs)
30 
31  self.SQLError = sqlite.Error
32 
33  def getConnType(self): return "sqlite"
34 
35  def cursor(self):
36  return Cursor(self._conn.cursor())
37 
38  def escape(self,str):
39  if str is None:
40  return None
41  elif type(str) == type(""):
42  return string.replace(str,"'","''")
43  elif type(str) == type(1):
44  return str
45  else:
46  raise "unknown column data type: %s" % type(str)
47 
48 
49  def listTables(self, cursor):
50  cursor.execute("select name from sqlite_master where type='table'")
51  rows = cursor.fetchall()
52  tables = []
53  for row in rows: tables.append(row[0])
54  return tables
55 
56  def supportsTriggers(self): return True
57 
58  def listTriggers(self, cursor):
59  cursor.execute("select name from sqlite_master where type='trigger'")
60  rows = cursor.fetchall()
61  tables = []
62  for row in rows: tables.append(row[0])
63  return tables
64 
65  def listIndices(self, tableName, cursor):
66  cursor.execute("select name from sqlite_master where type='index'")
67  rows = cursor.fetchall()
68  tables = []
69  for row in rows: tables.append(row[0])
70  return tables
71 
72  def listFieldsDict(self, table_name, cursor):
73  sql = "pragma table_info(%s)" % table_name
74  cursor.execute(sql)
75  rows = cursor.fetchall()
76 
77  columns = {}
78  for row in rows:
79  colname = row[1]
80  columns[colname] = row
81  return columns
82 
83  def _tableCreateStatement(self, table_name, cursor):
84  sql = "select sql from sqlite_master where type='table' and name='%s'" % table_name
85  print sql
86  cursor.execute(sql)
87  row = cursor.fetchone()
88  sqlstatement = row[0]
89  return sqlstatement
90 
91 
92  def alterTableToMatch(self, table, cursor):
93  tableName = table.getTableName()
94  tmpTableName = tableName + "_" + str(os.getpid())
95 
96  invalidAppCols, invalidDBCols = table.checkTable(warnflag=0)
97 # warn(invalidAppCols, invalidDBCols)
98 
99 
100 ## if invalidAppCols or invalidDBCols:
101 ## return
102 
103  if not invalidAppCols and not invalidDBCols:
104  return
105 
106 
107  oldcols = self.listFieldsDict(tableName, cursor)
108 # tmpcols = oldcols.keys()
109 
110  tmpcols = []
111  newcols = table.getAppColumnList()
112  for colname, coltype, options in newcols:
113  if oldcols.has_key(colname): tmpcols.append(colname)
114 
115  tmpcolnames = string.join(tmpcols, ",")
116 
117  statements = []
118 
119 # sql = "begin transaction"
120 # statements.append(sql)
121 
122  sql = "create temporary table %s (%s)" % (tmpTableName, tmpcolnames)
123  statements.append(sql)
124 
125  sql = "insert into %s select %s from %s" % (tmpTableName, tmpcolnames, tableName)
126  statements.append(sql)
127 
128  sql = "drop table %s" % tableName
129  statements.append(sql)
130 
131  sql = table._createTableSQL()
132  statements.append(sql)
133 
134  sql = "insert into %s(%s) select %s from %s" % (tableName, tmpcolnames, tmpcolnames, tmpTableName)
135  statements.append(sql)
136 
137  sql = "drop table %s" % tmpTableName
138  statements.append(sql)
139 
140 # sql = "commit"
141 # statements.append(sql)
142 
143  for statement in statements:
144  print statement
145  cursor.execute(statement)
146 
147 
148 def test():
149  pass
150 
151 def usage(progname):
152  print __doc__ % vars()
153 
154 def main(argv, stdout, environ):
155  progname = argv[0]
156  optlist, args = getopt.getopt(argv[1:], "", ["help", "test", "debug"])
157 
158  testflag = 0
159  if len(args) == 0:
160  usage(progname)
161  return
162  for (field, val) in optlist:
163  if field == "--help":
164  usage(progname)
165  return
166  elif field == "--debug":
167  debugfull()
168  elif field == "--test":
169  testflag = 1
170 
171  if testflag:
172  test()
173  return
174 
175 
176 if __name__ == "__main__":
177  main(sys.argv, sys.stdout, os.environ)
def alterTableToMatch(self, table, cursor)
Definition: odb_sqlite.py:92
def insert_id(self, tablename, colname)
Definition: odb_sqlite.py:17
def main(argv, stdout, environ)
Definition: odb_sqlite.py:154
def listIndices(self, tableName, cursor)
Definition: odb_sqlite.py:65
def debugfull()
Definition: log.py:120
if sql[:6] != "select": warn(repr(sql))
Definition: odb_sqlite.py:25
def _tableCreateStatement(self, table_name, cursor)
Definition: odb_sqlite.py:83
def listFieldsDict(self, table_name, cursor)
Definition: odb_sqlite.py:72
def __init__(self, args, kwargs)
Definition: odb_sqlite.py:26


pyclearsilver
Author(s): Scott Noob Hassan
autogenerated on Mon Jun 10 2019 15:51:13