mirror of
https://github.com/PyMySQL/mysqlclient.git
synced 2025-08-16 12:27:03 +08:00
Modified to use new _mysql result object methods.
A few changes to bring back into line with the (still-changing) API.
This commit is contained in:
@ -1,10 +1,16 @@
|
|||||||
import _mysql
|
import _mysql
|
||||||
from _mysql import *
|
from _mysql import *
|
||||||
from DateTime import Date, Time, Timestamp, ISO
|
from DateTime import Date, Time, Timestamp, ISO
|
||||||
from types import StringType, ListType, TupleType
|
|
||||||
|
|
||||||
threadsafety = 1
|
threadsafety = 1
|
||||||
apllevel = "1.1"
|
apllevel = "1.1"
|
||||||
|
paramstyle = "percent"
|
||||||
|
|
||||||
|
def Binary(x): return str(x)
|
||||||
|
|
||||||
|
def DATE(d): return d.Format("%Y-%m-%d")
|
||||||
|
def TIME(d): return d.Format("%H:%M:%S")
|
||||||
|
def TIMESTAMP(d): return d.Format("%Y-%m-%d %H:%M:%S")
|
||||||
|
|
||||||
def mysql_timestamp_converter(s):
|
def mysql_timestamp_converter(s):
|
||||||
parts = map(int, filter(None, (s[:4],s[4:6],s[6:8],s[8:10],s[10:12],s[12:14])))
|
parts = map(int, filter(None, (s[:4],s[4:6],s[6:8],s[8:10],s[10:12],s[12:14])))
|
||||||
@ -15,43 +21,49 @@ type_conv[FIELD_TYPE.DATETIME] = ISO.ParseDateTime
|
|||||||
type_conv[FIELD_TYPE.TIME] = ISO.ParseTime
|
type_conv[FIELD_TYPE.TIME] = ISO.ParseTime
|
||||||
type_conv[FIELD_TYPE.DATE] = ISO.ParseDate
|
type_conv[FIELD_TYPE.DATE] = ISO.ParseDate
|
||||||
|
|
||||||
|
|
||||||
class Cursor:
|
class Cursor:
|
||||||
|
|
||||||
def __init__(self, connection, name=''):
|
def __init__(self, connection, name='', use=0, warnings=1):
|
||||||
self.connection = connection
|
self.connection = connection
|
||||||
self.name = name
|
self.name = name
|
||||||
self.description = None
|
self.description = None
|
||||||
self.rowcount = -1
|
self.rowcount = -1
|
||||||
self.result = None
|
self.result = None
|
||||||
self.arraysize = None
|
self.arraysize = None
|
||||||
self.warnings = 1
|
self.warnings = warnings
|
||||||
|
self.use = use
|
||||||
|
|
||||||
## def __del__(self):
|
|
||||||
## self.result = None
|
|
||||||
##
|
|
||||||
def setinputsizes(self, size): pass
|
def setinputsizes(self, size): pass
|
||||||
|
|
||||||
def setoutputsizes(self, size): pass
|
def setoutputsizes(self, size): pass
|
||||||
|
|
||||||
def execute(self, query, args=None):
|
def execute(self, query, args=None):
|
||||||
from types import ListType, TupleType
|
from types import TupleType
|
||||||
from string import rfind, join, split, atoi
|
from string import rfind, join, split, atoi
|
||||||
db = self.connection.db
|
|
||||||
if not args:
|
if not args:
|
||||||
db.query(query)
|
self._query(query)
|
||||||
elif type(args) is not ListType:
|
elif type(args) is TupleType:
|
||||||
db.query(query % escape_row(args))
|
self._query(query % escape_row(args))
|
||||||
else:
|
else:
|
||||||
|
self.executemany(query, args) # deprecated
|
||||||
|
|
||||||
|
def executemany(self, query, args=None):
|
||||||
|
from string import rfind, join
|
||||||
p = rfind(query, '(')
|
p = rfind(query, '(')
|
||||||
if p == -1: raise ProgrammingError, "can't find values"
|
if p == -1: raise ProgrammingError, "can't find values"
|
||||||
n = len(args)-1
|
n = len(args)-1
|
||||||
q = [query % escape_row(args[0])]
|
q = [query % escape_row(args[0])]
|
||||||
qv = query[p:]
|
qv = query[p:]
|
||||||
for a in args[1:]: q.append(qv % escape_row(a))
|
for a in args[1:]: q.append(qv % escape_row(a))
|
||||||
q = join(q, ',\n')
|
self._query(join(q, ',\n'))
|
||||||
print q
|
|
||||||
|
def _query(self, q):
|
||||||
|
from string import split, atoi
|
||||||
|
db = self.connection.db
|
||||||
db.query(q)
|
db.query(q)
|
||||||
self.result = db.store_result()
|
if self.use: self.result = db.use_result()
|
||||||
|
else: self.result = db.store_result()
|
||||||
if self.result:
|
if self.result:
|
||||||
self.description = self.result.describe()
|
self.description = self.result.describe()
|
||||||
self.rowcount = self.result.num_rows()
|
self.rowcount = self.result.num_rows()
|
||||||
@ -65,7 +77,6 @@ class Cursor:
|
|||||||
if warnings:
|
if warnings:
|
||||||
raise Warning, w
|
raise Warning, w
|
||||||
|
|
||||||
|
|
||||||
def fetchone(self):
|
def fetchone(self):
|
||||||
try:
|
try:
|
||||||
return self.result.fetch_row()
|
return self.result.fetch_row()
|
||||||
@ -74,32 +85,13 @@ class Cursor:
|
|||||||
|
|
||||||
def fetchmany(self, size=None):
|
def fetchmany(self, size=None):
|
||||||
size = size or self.inputsizes or 1
|
size = size or self.inputsizes or 1
|
||||||
rows = []
|
return self.result.fetch_rows(size)
|
||||||
for i in range(size):
|
|
||||||
row = self.fetchone()
|
|
||||||
if not row: break
|
|
||||||
rows.append(row)
|
|
||||||
return rows
|
|
||||||
|
|
||||||
def fetchall(self):
|
def fetchall(self): return self.result.fetch_all_rows()
|
||||||
rows = []
|
|
||||||
while 1:
|
|
||||||
row = self.fetchone()
|
|
||||||
if not row: break
|
|
||||||
rows.append(row)
|
|
||||||
return rows
|
|
||||||
|
|
||||||
def nextset(self): pass
|
def nextset(self): pass
|
||||||
|
|
||||||
|
|
||||||
def Raw(s): return s
|
|
||||||
|
|
||||||
STRING = FIELD_TYPE.STRING
|
|
||||||
NUMBER = FIELD_TYPE.LONG
|
|
||||||
TIME = FIELD_TYPE.TIME
|
|
||||||
TIMESTAMP = FIELD_TYPE.TIMESTAMP
|
|
||||||
ROW_ID = FIELD_TYPE.LONG
|
|
||||||
|
|
||||||
class Connection:
|
class Connection:
|
||||||
|
|
||||||
CursorClass = Cursor
|
CursorClass = Cursor
|
||||||
@ -119,9 +111,10 @@ class Connection:
|
|||||||
|
|
||||||
def commit(self): pass
|
def commit(self): pass
|
||||||
|
|
||||||
def rollback(self): raise OperationalError, "transactions not supported"
|
# def rollback(self): raise OperationalError, "transactions not supported"
|
||||||
|
|
||||||
def cursor(self, name=''):
|
def cursor(self, name=''):
|
||||||
return self.CursorClass(self, name)
|
return self.CursorClass(self, name)
|
||||||
|
|
||||||
|
|
||||||
Connect = connect = Connection
|
Connect = connect = Connection
|
||||||
|
Reference in New Issue
Block a user