$search
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 MySQLdb 00013 00014 class Cursor(odb.Cursor): 00015 def insert_id(self, tablename, colname): 00016 return self.cursor.insert_id() 00017 00018 class Connection(odb.Connection): 00019 def __init__(self, host, user, passwd, db): 00020 odb.Connection.__init__(self) 00021 self._conn = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db) 00022 self.SQLError = MySQLdb.Error 00023 00024 def getConnType(self): return "mysql" 00025 00026 def cursor(self): 00027 return Cursor(self._conn.cursor()) 00028 00029 def escape(self,str): 00030 if str is None: return None 00031 return MySQLdb.escape_string(str) 00032 00033 def listTables(self, cursor): 00034 cursor.execute("show tables") 00035 rows = cursor.fetchall() 00036 tables = [] 00037 for row in rows: 00038 tables.append(row[0]) 00039 return tables 00040 00041 def listIndices(self, tableName, cursor): 00042 cursor.execute("show index from %s" % tableName) 00043 rows = cursor.fetchall() 00044 tables = map(lambda row: row[2], rows) 00045 return tables 00046 00047 def listFieldsDict(self, table_name, cursor): 00048 sql = "show columns from %s" % table_name 00049 cursor.execute(sql) 00050 rows = cursor.fetchall() 00051 00052 columns = {} 00053 for row in rows: 00054 colname = row[0] 00055 columns[colname] = row 00056 00057 return columns 00058 00059 def alterTableToMatch(self, table, cursor): 00060 invalidAppCols, invalidDBCols = table.checkTable() 00061 if not invalidAppCols: return 00062 00063 defs = [] 00064 for colname in invalidAppCols.keys(): 00065 col = table.getColumnDef(colname) 00066 colname = col[0] 00067 coltype = col[1] 00068 options = col[2] 00069 defs.append(table._colTypeToSQLType(colname, coltype, options)) 00070 00071 defs = string.join(defs, ", ") 00072 00073 sql = "alter table %s add column " % table.getTableName() 00074 sql = sql + "(" + defs + ")" 00075 00076 print sql 00077 00078 cursor.execute(sql) 00079 00080 def createTable(self, sql, cursor): 00081 sql = sql + " TYPE=INNODB" 00082 return sql 00083 00084 00085 def supportsTriggers(self): return False 00086