odb_mysql.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 MySQLdb
13 
15  def insert_id(self, tablename, colname):
16  return self.cursor.insert_id()
17 
19  def __init__(self, host, user, passwd, db):
20  odb.Connection.__init__(self)
21  self._conn = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)
22  self.SQLError = MySQLdb.Error
23 
24  def getConnType(self): return "mysql"
25 
26  def cursor(self):
27  return Cursor(self._conn.cursor())
28 
29  def escape(self,str):
30  if str is None: return None
31  return MySQLdb.escape_string(str)
32 
33  def listTables(self, cursor):
34  cursor.execute("show tables")
35  rows = cursor.fetchall()
36  tables = []
37  for row in rows:
38  tables.append(row[0])
39  return tables
40 
41  def listIndices(self, tableName, cursor):
42  cursor.execute("show index from %s" % tableName)
43  rows = cursor.fetchall()
44  tables = map(lambda row: row[2], rows)
45  return tables
46 
47  def listFieldsDict(self, table_name, cursor):
48  sql = "show columns from %s" % table_name
49  cursor.execute(sql)
50  rows = cursor.fetchall()
51 
52  columns = {}
53  for row in rows:
54  colname = row[0]
55  columns[colname] = row
56 
57  return columns
58 
59  def alterTableToMatch(self, table, cursor):
60  invalidAppCols, invalidDBCols = table.checkTable()
61  if not invalidAppCols: return
62 
63  defs = []
64  for colname in invalidAppCols.keys():
65  col = table.getColumnDef(colname)
66  colname = col[0]
67  coltype = col[1]
68  options = col[2]
69  defs.append(table._colTypeToSQLType(colname, coltype, options))
70 
71  defs = string.join(defs, ", ")
72 
73  sql = "alter table %s add column " % table.getTableName()
74  sql = sql + "(" + defs + ")"
75 
76  print sql
77 
78  cursor.execute(sql)
79 
80  def createTable(self, sql, cursor):
81  sql = sql + " TYPE=INNODB"
82  return sql
83 
84 
85  def supportsTriggers(self): return False
86 
def insert_id(self, tablename, colname)
Definition: odb_mysql.py:15
def alterTableToMatch(self, table, cursor)
Definition: odb_mysql.py:59
def listTables(self, cursor)
Definition: odb_mysql.py:33
def __init__(self, host, user, passwd, db)
Definition: odb_mysql.py:19
def listFieldsDict(self, table_name, cursor)
Definition: odb_mysql.py:47
def createTable(self, sql, cursor)
Definition: odb_mysql.py:80
def listIndices(self, tableName, cursor)
Definition: odb_mysql.py:41


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