Menu

[r18]: / mysql / __init__.py  Maximize  Restore  History

Download this file

329 lines (280 with data), 7.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#
# Copyright (C) 2008, 2009, 2010 Bozhin Zafirov
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from datetime import date as Date
from datetime import time as Time
from datetime import datetime as Timestamp
import time
from mysql import _mysql
# global variables
apilevel = '1.0'
threadsafety = 1
paramstyle = 'pyformat'
# date and time
def DateFromTicks(ticks):
return Date(*time.localtime(ticks)[:3])
def TimeFromTicks(ticks):
return Time(*time.localtime(ticks)[3:6])
def TimestampFromTicks(ticks):
return Time(*time.localtime(ticks)[:6])
# exception types
class Warning(Exception): pass
class Error(Exception): pass
class InterfaceError(Error): pass
class DatabaseError(Error): pass
class DataError(DatabaseError): pass
class OperationalError(DatabaseError): pass
class IntegrityError(DatabaseError): pass
class InternalError(DatabaseError): pass
class ProgrammingError(DatabaseError): pass
class NotSupportedError(DatabaseError): pass
# data types
class DBAPITypeObject(object):
def __init__(self, *values):
self.__values = values
def __eq__(self, other):
return other in self.__values
# type objects
STRING = DBAPITypeObject(
_mysql.MYSQL_TYPE_VARCHAR,
_mysql.MYSQL_TYPE_ENUM,
_mysql.MYSQL_TYPE_STRING,
_mysql.MYSQL_TYPE_VAR_STRING,
)
BINARY = DBAPITypeObject(
_mysql.MYSQL_TYPE_TINY_BLOB,
_mysql.MYSQL_TYPE_MEDIUM_BLOB,
_mysql.MYSQL_TYPE_LONG_BLOB,
_mysql.MYSQL_TYPE_BLOB,
)
NUMBER = DBAPITypeObject(
_mysql.MYSQL_TYPE_DECIMAL,
_mysql.MYSQL_TYPE_TINY,
_mysql.MYSQL_TYPE_SHORT,
_mysql.MYSQL_TYPE_LONG,
_mysql.MYSQL_TYPE_FLOAT,
_mysql.MYSQL_TYPE_DOUBLE,
_mysql.MYSQL_TYPE_LONGLONG,
_mysql.MYSQL_TYPE_INT24,
_mysql.MYSQL_TYPE_BIT,
)
DATETIME = DBAPITypeObject(
_mysql.MYSQL_TYPE_TIMESTAMP,
_mysql.MYSQL_TYPE_DATE,
_mysql.MYSQL_TYPE_TIME,
_mysql.MYSQL_TYPE_DATETIME,
_mysql.MYSQL_TYPE_YEAR,
_mysql.MYSQL_TYPE_NEWDATE,
)
# does this have a type for MySQL?
ROWID = DBAPITypeObject()
# cursor object definition
class CursorObject(object):
# class constructor
def __init__(self, m, conn):
self.__mysql = m
# array size
self.arraysize = 1
self.__res = []
self.__connection = conn
self.__rownumber = 0
self.__rowcount = 0
self.__lastrowid = None
def callproc(self, *_):
raise NotSupportedError('callproc is not supported.')
# close cursor
def close(self):
self.__mysql = None
# set result descriptions
def __setDescription(self):
description = []
if len(self.__res) and self.__res[0] is not None:
while True:
f = _mysql.fetch_field(self.__res[0])
if f is None:
break
description.append(
(f.name, f.type, f.max_length, f.length, f.length, f.decimals, None)
)
self.__description = tuple(description)
# execute query
def __execute(self, operation, parameters):
query = operation % parameters
if not _mysql.real_query(self.__mysql, query):
raise OperationalError(_mysql.error(self.__mysql))
# get some useful data
self.__res.append(_mysql.store_result(self.__mysql))
self.__rowcount = _mysql.affected_rows(self.__mysql)
# execute query
def execute(self, operation, **parameters):
self.__res = []
self.__execute(operation, parameters)
# set description
self.__setDescription()
self.__rownumber = 0
self.__lastrowid = _mysql.insert_id(self.__mysql)
# execute many queries
def executemany(self, operation, seq_of_parameters):
if not type(seq_of_parameters) in (tuple, list):
raise InterfaceError()
self.__res = []
for p in seq_of_parameters:
# execute all
self.__execute(operation, p)
self.__setDescription()
self.__rownumber = 0
self.__lastrowid = _mysql.insert_id(self.__mysql)
# fetch one row
def fetchone(self):
if not len(self.__res) or self.__res[0] is None:
raise Error('No resultset available')
eerr = None
try:
_row = _mysql.fetch_row(self.__res[0])
except UnicodeDecodeError as err:
eerr = err
if eerr:
raise DataError(eerr)
if _row is None:
return None
row = []
for i in range(0, len(_row)):
item = _row[i]
itype = self.description[i][1]
if item is None:
row.append(None)
elif itype == BINARY:
row.append(item.encode())
elif itype == NUMBER:
if float(item) == int(float(item)):
row.append(int(item))
else:
row.append(float(item))
else:
row.append(item)
self.__rownumber = self.__rownumber + 1
return tuple(row)
# fetch many rows
def fetchmany(self, size=None):
if size is None:
size = self.arraysize
data = []
for i in range(0, size):
item = self.fetchone()
if item is None:
break
data.append(item)
return data
# fetch all remaining results
def fetchall(self):
rows = []
while True:
row = self.fetchone()
if row is None:
break
rows.append(row)
return rows
# set next result set
def nextset(self):
if len(self.__res):
# get next result set
self.__res.pop(0)
# set descriptions
self.__setDescription()
self.__rownumber = 0
return True
# no more result sets
return None
# scroll data result set
def scroll(self, value, mode='relative'):
if mode == 'relative':
value = self.rownumber + int(value)
if value < 0 or value > self.rowcount:
raise IndexError('scroll value is outside allowed range.')
_mysql.data_seek(self.__res[0], value)
self.__rownumber = value
# iteration protocol
# dbapi 2.0
def __next__(self):
data = self.fetchone()
if data is None:
raise StopIteration
return data
def __iter__(self):
return self
# returns number of rows changed/deleted/inserted
@property
def rowcount(self):
return self.__rowcount
# do nothing
def setinputsizes(self, *_):
pass
# do nothing
def setoutputsize(self, *_):
pass
# get query rows description
@property
def description(self):
return self.__description
@property
def connection(self):
return self.__connection
@property
def rownumber(self):
return self.__rownumber
@property
def lastrowid(self):
return self.__lastrowid
# Connection object definition
class ConnectionObject(object):
# class constructor
def __init__(self, m):
self.__mysql = m
# exceptions
self.Warning = Warning
self.Error = Error
self.InterfaceError = InterfaceError
self.DatabaseError = DatabaseError
self.DataError = DataError
self.OperationalError = OperationalError
self.IntegrityError = IntegrityError
self.InternalError = InternalError
self.ProgrammingError = ProgrammingError
self.NotSupportedError = NotSupportedError
# class destructor
def __del__(self):
self.close()
# close connection to database
def close(self):
_mysql.close(self.__mysql)
# commit database transaction
def commit(self):
if _mysql.commit(self.__mysql):
raise DatabaseError(_mysql.error(self.__mysql))
# rollback database transaction
def rollback(self):
if _mysql.rollback(self.__mysql):
raise DatabaseError(_mysql.error(self.__mysql))
# return cursor object
def cursor(self):
return CursorObject(self.__mysql, self)
# connect to database server
def connect(dsn=None, host=None, user=None, passwd=None, db=None, port=0, unixsocket=None):
m = _mysql.init()
if not _mysql.real_connect(m, host, user, passwd, db, port, unixsocket):
raise DatabaseError(_mysql.error(m))
return ConnectionObject(m)
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.