00001
00002
00003
00004
00005
00006
00007 import adodb,re
00008 import MySQLdb
00009
00010
00011
00012
00013 try:
00014 True, False
00015 except NameError:
00016
00017 True, False = 1, 0
00018
00019 class adodb_mysql(adodb.ADOConnection):
00020 databaseType = 'mysql'
00021 dataProvider = 'mysql'
00022 metaColSQL = "SHOW COLUMNS FROM %s"
00023
00024 sysDate = 'CURDATE()'
00025 sysTimeStamp = 'NOW()'
00026
00027
00028 def __init__(self):
00029 pass
00030
00031 def Module(self):
00032 return MySQLdb
00033
00034 def _connect(self,host=None,user=None,password=None,database=None):
00035 self._conn = MySQLdb.connect(host, user, password, database)
00036
00037 def _newcursor(self,rs):
00038 return cursor_mysql(rs,self)
00039
00040 def BeginTrans(self):
00041 if self._autocommit:
00042 self._autocommit = False
00043 self.Execute('set autocommit=0')
00044
00045
00046 def RollbackTrans(self):
00047 self.Execute('rollback')
00048 self.Execute('set autocommit=1')
00049 self._autocommit = True
00050
00051 def SelectLimit(self,sql,limit,offset=-1,params=None):
00052 if (offset >= 0): offset = str(offset)+","
00053 else: offset = ""
00054 return self.Execute(sql+" LIMIT "+offset+str(limit),params)
00055
00056 def CommitTrans(self):
00057 self.Execute('commit')
00058 self.Execute('set autocommit=1')
00059 self._autocommit = True
00060
00061 def UpdateBlob(self,table,field,blob,where,blobtype='BLOB'):
00062 self.Execute("update %s set %s='%s' WHERE %s" % (table,field,self.addq(blob),where))
00063
00064 def qstr(self,s):
00065 return "'%s'" % self._conn.escape_string(s)
00066
00067 def MetaColumns(self, table):
00068 sql = self.metaColSQL % table
00069 rs = self.Execute(sql)
00070 arr = []
00071 reFloat = re.compile("^(.+)\\((\\d+),(\\d+)")
00072 reInt = re.compile("^(.+)\\((\\d+)")
00073 while not rs.EOF:
00074 typeF = rs.fields[1]
00075 if typeF.find('(')>=0:
00076 if typeF.find(',')>=0:
00077 m = reFloat.search(typeF)
00078 else:
00079 m = reInt.search(typeF)
00080 if m:
00081 gps = m.groups()
00082 type = gps[0]
00083 size = gps[1]
00084 else:
00085 type = typeF
00086 size = -1
00087 else:
00088 type = typeF
00089 size = -1
00090
00091 arr.append((rs.fields[0],type,size))
00092 rs.MoveNext()
00093 return arr
00094
00095 class cursor_mysql(adodb.ADOCursor):
00096 def __init__(self,rs,conn):
00097 adodb.ADOCursor.__init__(self,rs,conn)
00098
00099 self._insertid = rs.lastrowid
00100 if __name__ == '__main__':
00101 db = adodb.NewADOConnection('mysql')
00102 db.Connect('localhost','root','','northwind')
00103 for r in db.Execute('select * from adoxyz'):
00104 print r
00105 adodb.Test(db)