| 1 |
import sys |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
sys.coinit_flags = 0 |
|---|
| 5 |
|
|---|
| 6 |
import pythoncom |
|---|
| 7 |
Empty = pythoncom.Empty |
|---|
| 8 |
clsctx = pythoncom.CLSCTX_SERVER |
|---|
| 9 |
|
|---|
| 10 |
import win32com.client |
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
BOF = (1002, 0, 2, (11, 0), ()) |
|---|
| 14 |
EOF = (1006, 0, 2, (11, 0), ()) |
|---|
| 15 |
Recordset_Fields = (0, 0, 2, (9, 0), ()) |
|---|
| 16 |
|
|---|
| 17 |
Recordset_GetRows = (1016, 0, 1, (12, 0), ((3, 49), (12, 17), (12, 17)), -1, Empty, Empty) |
|---|
| 18 |
Recordset_Close = (1014, 0, 1, (24, 0), (),) |
|---|
| 19 |
Fields_Count = (1, 0, 2, (3, 0), ()) |
|---|
| 20 |
Field_Name = (1100, 0, 2, (8, 0), ()) |
|---|
| 21 |
Field_Type = (1102, 0, 2, (3, 0), ()) |
|---|
| 22 |
Field_Properties = (500, 0, 2, (9, 0), ()) |
|---|
| 23 |
Property_Value = (0, 0, 2, (12, 0), ()) |
|---|
| 24 |
|
|---|
| 25 |
import pywintypes |
|---|
| 26 |
import datetime |
|---|
| 27 |
|
|---|
| 28 |
import time |
|---|
| 29 |
|
|---|
| 30 |
try: |
|---|
| 31 |
import cPickle as pickle |
|---|
| 32 |
except ImportError: |
|---|
| 33 |
import pickle |
|---|
| 34 |
|
|---|
| 35 |
import threading |
|---|
| 36 |
import warnings |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
import geniusql |
|---|
| 40 |
from geniusql import adapters, conns, decompile, errors, select, typerefs |
|---|
| 41 |
from geniusql import isolation as _isolation |
|---|
| 42 |
|
|---|
| 43 |
adOpenForwardOnly = 0 |
|---|
| 44 |
adOpenKeyset = 1 |
|---|
| 45 |
adOpenDynamic = 2 |
|---|
| 46 |
adOpenStatic = 3 |
|---|
| 47 |
|
|---|
| 48 |
adLockReadOnly = 1 |
|---|
| 49 |
adLockPessimistic = 2 |
|---|
| 50 |
adLockOptimistic = 3 |
|---|
| 51 |
adLockBatchOptimistic = 4 |
|---|
| 52 |
|
|---|
| 53 |
adSchemaColumns = 4 |
|---|
| 54 |
adSchemaIndexes = 12 |
|---|
| 55 |
adSchemaTables = 20 |
|---|
| 56 |
adSchemaPrimaryKeys = 28 |
|---|
| 57 |
|
|---|
| 58 |
adUseClient = 3 |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
dbtypes = { |
|---|
| 62 |
0: 'EMPTY', |
|---|
| 63 |
2: 'SMALLINT', |
|---|
| 64 |
3: 'INTEGER', |
|---|
| 65 |
4: 'SINGLE', |
|---|
| 66 |
5: 'DOUBLE', |
|---|
| 67 |
6: 'CURRENCY', |
|---|
| 68 |
7: 'DATE', |
|---|
| 69 |
8: 'BSTR', 9: 'IDISPATCH', 10: 'ERROR', |
|---|
| 70 |
11: 'BOOLEAN', |
|---|
| 71 |
12: 'VARIANT', |
|---|
| 72 |
13: 'IUNKNOWN', 14: 'DECIMAL', 16: 'TINYINT', |
|---|
| 73 |
17: 'UNSIGNEDTINYINT', |
|---|
| 74 |
18: 'UNSIGNEDSMALLINT', 19: 'UNSIGNEDINT', |
|---|
| 75 |
20: 'BIGINT', |
|---|
| 76 |
21: 'UNSIGNEDBIGINT', |
|---|
| 77 |
72: 'GUID', |
|---|
| 78 |
128: 'BINARY', |
|---|
| 79 |
129: 'CHAR', |
|---|
| 80 |
130: 'WCHAR', |
|---|
| 81 |
131: 'NUMERIC', |
|---|
| 82 |
132: 'USERDEFINED', |
|---|
| 83 |
133: 'DBDATE', 134: 'DBTIME', |
|---|
| 84 |
135: 'DBTIMESTAMP', |
|---|
| 85 |
200: 'VARCHAR', |
|---|
| 86 |
201: 'LONGVARCHAR', |
|---|
| 87 |
202: 'VARWCHAR', |
|---|
| 88 |
203: 'LONGVARWCHAR', |
|---|
| 89 |
204: 'VARBINARY', |
|---|
| 90 |
205: 'LONGVARBINARY', |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
DBCOLUMNFLAGS_WRITE = 0x4 |
|---|
| 94 |
DBCOLUMNFLAGS_WRITEUNKNOWN = 0x8 |
|---|
| 95 |
DBCOLUMNFLAGS_ISFIXEDLENGTH = 0x10 |
|---|
| 96 |
DBCOLUMNFLAGS_ISNULLABLE = 0x20 |
|---|
| 97 |
DBCOLUMNFLAGS_MAYBENULL = 0x40 |
|---|
| 98 |
DBCOLUMNFLAGS_ISLONG = 0x80 |
|---|
| 99 |
DBCOLUMNFLAGS_ISROWID = 0x100 |
|---|
| 100 |
DBCOLUMNFLAGS_ISROWVER = 0x200 |
|---|
| 101 |
DBCOLUMNFLAGS_CACHEDEFERRED = 0x1000 |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
class AdapterFromADO(adapters.AdapterFromDB): |
|---|
| 105 |
"""Coerce incoming values from ADO to Python datatypes.""" |
|---|
| 106 |
|
|---|
| 107 |
encoding = 'ISO-8859-1' |
|---|
| 108 |
epoch = datetime.datetime(1899, 12, 30) |
|---|
| 109 |
|
|---|
| 110 |
def timedelta_from_com(self, com_date): |
|---|
| 111 |
"""Return a valid datetime.timedelta from a COM date/time object.""" |
|---|
| 112 |
com_date = float(com_date) |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
neg = (com_date < 0) |
|---|
| 120 |
com_date = abs(com_date) |
|---|
| 121 |
|
|---|
| 122 |
days = int(com_date) |
|---|
| 123 |
|
|---|
| 124 |
secs = int(round(86400 * (com_date - days))) |
|---|
| 125 |
|
|---|
| 126 |
result = datetime.timedelta(days, secs) |
|---|
| 127 |
if neg: |
|---|
| 128 |
return -result |
|---|
| 129 |
else: |
|---|
| 130 |
return result |
|---|
| 131 |
|
|---|
| 132 |
def coerce_any_to_datetime_timedelta(self, value): |
|---|
| 133 |
|
|---|
| 134 |
return self.timedelta_from_com(value) |
|---|
| 135 |
|
|---|
| 136 |
def coerce_any_to_datetime_time(self, value): |
|---|
| 137 |
t = self.timedelta_from_com(value) |
|---|
| 138 |
if t.days: |
|---|
| 139 |
raise ValueError("Time values greater than 23:59:59 not allowed.") |
|---|
| 140 |
h, m = divmod(t.seconds, 3600) |
|---|
| 141 |
m, s = divmod(m, 60) |
|---|
| 142 |
return datetime.time(int(h), int(m), int(s)) |
|---|
| 143 |
|
|---|
| 144 |
def datetime_from_com(self, com_date): |
|---|
| 145 |
"""Return a valid datetime.datetime from a COM date/time object.""" |
|---|
| 146 |
com_date = float(com_date) |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
days = int(com_date) |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
secs = int(round(86400 * abs(com_date - days))) |
|---|
| 159 |
|
|---|
| 160 |
return self.epoch + datetime.timedelta(days, secs) |
|---|
| 161 |
|
|---|
| 162 |
def coerce_any_to_datetime_datetime(self, value): |
|---|
| 163 |
if isinstance(value, basestring): |
|---|
| 164 |
if value: |
|---|
| 165 |
try: |
|---|
| 166 |
return datetime.datetime(int(value[0:4]), int(value[4:6]), |
|---|
| 167 |
int(value[6:8])) |
|---|
| 168 |
except Exception: |
|---|
| 169 |
raise ValueError("'%s' %s" % (value, type(value))) |
|---|
| 170 |
else: |
|---|
| 171 |
return None |
|---|
| 172 |
else: |
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
return self.datetime_from_com(value) |
|---|
| 177 |
|
|---|
| 178 |
def coerce_any_to_datetime_date(self, value): |
|---|
| 179 |
if isinstance(value, basestring): |
|---|
| 180 |
if value: |
|---|
| 181 |
try: |
|---|
| 182 |
return datetime.date(int(value[0:4]), int(value[4:6]), |
|---|
| 183 |
int(value[6:8])) |
|---|
| 184 |
except Exception: |
|---|
| 185 |
raise ValueError("'%s' %s" % (value, type(value))) |
|---|
| 186 |
else: |
|---|
| 187 |
return None |
|---|
| 188 |
else: |
|---|
| 189 |
value = float(value) |
|---|
| 190 |
days = int(value) |
|---|
| 191 |
return self.epoch.date() + datetime.timedelta(days) |
|---|
| 192 |
|
|---|
| 193 |
def coerce_any_to_decimal_Decimal(self, value): |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
if not isinstance(value, typerefs.decimal.Decimal): |
|---|
| 198 |
value = str(value) |
|---|
| 199 |
value = typerefs.decimal.Decimal(value) |
|---|
| 200 |
return value |
|---|
| 201 |
|
|---|
| 202 |
def coerce_CURRENCY_to_float(self, value): |
|---|
| 203 |
if isinstance(value, tuple): |
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
value = map(long, value) |
|---|
| 207 |
return ((value[1] & 0xFFFFFFFFL) | (value[0] << 32)) / 1e4 |
|---|
| 208 |
return float(value) |
|---|
| 209 |
|
|---|
| 210 |
def coerce_CURRENCY_to_decimal_Decimal(self, value): |
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
if not isinstance(value, typerefs.decimal.Decimal): |
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
value = map(long, value) |
|---|
| 218 |
value = (value[1] & 0xFFFFFFFFL) | (value[0] << 32) |
|---|
| 219 |
return typerefs.decimal.Decimal(value) / 10000 |
|---|
| 220 |
return value |
|---|
| 221 |
|
|---|
| 222 |
def coerce_CURRENCY_to_fixedpoint_FixedPoint(self, value): |
|---|
| 223 |
if isinstance(value, typerefs.decimal.Decimal): |
|---|
| 224 |
value = str(value) |
|---|
| 225 |
scale = 0 |
|---|
| 226 |
atoms = value.rsplit(".", 1) |
|---|
| 227 |
if len(atoms) > 1: |
|---|
| 228 |
scale = len(atoms[-1]) |
|---|
| 229 |
return typerefs.fixedpoint.FixedPoint(value, scale) |
|---|
| 230 |
else: |
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
value = map(long, value) |
|---|
| 234 |
value = (value[1] & 0xFFFFFFFFL) | (value[0] << 32) |
|---|
| 235 |
return typerefs.fixedpoint.FixedPoint(value, 4) / 1e4 |
|---|
| 236 |
|
|---|
| 237 |
def coerce_any_to_unicode(self, value): |
|---|
| 238 |
if isinstance(value, unicode): |
|---|
| 239 |
|
|---|
| 240 |
return value |
|---|
| 241 |
|
|---|
| 242 |
if isinstance(value, (basestring, buffer)): |
|---|
| 243 |
try: |
|---|
| 244 |
return unicode(value, self.encoding) |
|---|
| 245 |
except UnicodeError, exc: |
|---|
| 246 |
exc.args += (type(value),) |
|---|
| 247 |
return unicode(value) |
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
class ADOSQLDecompiler(decompile.SQLDecompiler): |
|---|
| 252 |
|
|---|
| 253 |
def visit_COMPARE_OP(self, lo, hi): |
|---|
| 254 |
op2, op1 = self.stack.pop(), self.stack.pop() |
|---|
| 255 |
if op1 is decompile.cannot_represent or op2 is decompile.cannot_represent: |
|---|
| 256 |
self.stack.append(decompile.cannot_represent) |
|---|
| 257 |
return |
|---|
| 258 |
|
|---|
| 259 |
op = lo + (hi << 8) |
|---|
| 260 |
if op in (6, 7): |
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
value = self.builtins_icontainedby(op1, op2) |
|---|
| 266 |
if op == 7: |
|---|
| 267 |
value.sql = "NOT " + value.sql |
|---|
| 268 |
self.stack.append(value) |
|---|
| 269 |
self.imperfect = True |
|---|
| 270 |
elif op1.sql == 'NULL': |
|---|
| 271 |
if op in (2, 8): |
|---|
| 272 |
self.stack.append(self.get_expr(op2.sql + " IS NULL", bool)) |
|---|
| 273 |
elif op in (3, 9): |
|---|
| 274 |
self.stack.append(self.get_expr(op2.sql + " IS NOT NULL", bool)) |
|---|
| 275 |
else: |
|---|
| 276 |
raise ValueError("Non-equality Null comparisons not allowed.") |
|---|
| 277 |
elif op2.sql == 'NULL': |
|---|
| 278 |
if op in (2, 8): |
|---|
| 279 |
self.stack.append(self.get_expr(op1.sql + " IS NULL", bool)) |
|---|
| 280 |
elif op in (3, 9): |
|---|
| 281 |
self.stack.append(self.get_expr(op1.sql + " IS NOT NULL", bool)) |
|---|
| 282 |
else: |
|---|
| 283 |
raise ValueError("Non-equality Null comparisons not allowed.") |
|---|
| 284 |
else: |
|---|
| 285 |
try: |
|---|
| 286 |
op1, op2 = self._compare_constants(op1, op2) |
|---|
| 287 |
except TypeError: |
|---|
| 288 |
self.stack.append(decompile.cannot_represent) |
|---|
| 289 |
self.imperfect = True |
|---|
| 290 |
return |
|---|
| 291 |
|
|---|
| 292 |
if (isinstance(op2, decompile.SQLExpression) |
|---|
| 293 |
and issubclass(op2.pytype, basestring)): |
|---|
| 294 |
atom = self._compare_strings(op1, op, op2) |
|---|
| 295 |
if atom is not None: |
|---|
| 296 |
self.stack.append(atom) |
|---|
| 297 |
return |
|---|
| 298 |
|
|---|
| 299 |
e = op1.sql + " " + self.sql_cmp_op[op] + " " + op2.sql |
|---|
| 300 |
self.stack.append(self.get_expr(e, bool)) |
|---|
| 301 |
|
|---|
| 302 |
def _compare_strings(self, op1, op, op2): |
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
self.imperfect = True |
|---|
| 309 |
|
|---|
| 310 |
def binary_op(self, op): |
|---|
| 311 |
op2, op1 = self.stack.pop(), self.stack.pop() |
|---|
| 312 |
if op1 is decompile.cannot_represent or op2 is decompile.cannot_represent: |
|---|
| 313 |
self.stack.append(decompile.cannot_represent) |
|---|
| 314 |
return |
|---|
| 315 |
|
|---|
| 316 |
t1, t2 = op1.pytype, op2.pytype |
|---|
| 317 |
|
|---|
| 318 |
newsql = None |
|---|
| 319 |
if t1 is datetime.date: |
|---|
| 320 |
if t2 is datetime.date: |
|---|
| 321 |
if op == "-": |
|---|
| 322 |
newsql = self.DATEDIFF(op1.sql, op2.sql) |
|---|
| 323 |
elif t2 is datetime.timedelta: |
|---|
| 324 |
if op == "+": |
|---|
| 325 |
newsql = self.DATEADD(op1.sql, op2.sql) |
|---|
| 326 |
elif op == "-": |
|---|
| 327 |
newsql = self.DATESUB(op1.sql, op2.sql) |
|---|
| 328 |
elif t1 is datetime.datetime: |
|---|
| 329 |
if t2 is datetime.datetime: |
|---|
| 330 |
if op == "-": |
|---|
| 331 |
newsql = self.DATETIMEDIFF(op1.sql, op2.sql) |
|---|
| 332 |
elif t2 is datetime.timedelta: |
|---|
| 333 |
if op == "+": |
|---|
| 334 |
newsql = self.DATETIMEADD(op1.sql, op2.sql) |
|---|
| 335 |
elif op == "-": |
|---|
| 336 |
newsql = self.DATETIMESUB(op1.sql, op2.sql) |
|---|
| 337 |
elif t1 is datetime.timedelta: |
|---|
| 338 |
if t2 is datetime.timedelta: |
|---|
| 339 |
newsql = self.TIMEDELTAADD(op1, op, op2) |
|---|
| 340 |
else: |
|---|
| 341 |
if op == "+": |
|---|
| 342 |
if t2 is datetime.date: |
|---|
| 343 |
newsql = self.DATEADD(op2.sql, op1.sql) |
|---|
| 344 |
elif t2 is datetime.datetime: |
|---|
| 345 |
newsql = self.DATETIMEADD(op2.sql, op1.sql) |
|---|
| 346 |
else: |
|---|
| 347 |
newsql = "(%s %s %s)" % (op1.sql, op, op2.sql) |
|---|
| 348 |
|
|---|
| 349 |
if newsql is None: |
|---|
| 350 |
raise TypeError("unsupported operand type(s) for %s: " |
|---|
| 351 |
"%r and %r" % (op, t1, t2)) |
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 |
op1.pytype = self.result_type[(t1, op, t2)] |
|---|
| 355 |
op1.sql = newsql |
|---|
| 356 |
if not op1.name.startswith("expr_"): |
|---|
| 357 |
op1.name = "expr_%s" % op1.name |
|---|
| 358 |
self.stack.append(op1) |
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
def attr_startswith(self, tos, arg): |
|---|
| 363 |
self.imperfect = True |
|---|
| 364 |
return self.get_expr(tos.sql + " LIKE '" + self.adapter.escape_like(arg.sql) + "%'", bool) |
|---|
| 365 |
|
|---|
| 366 |
def attr_endswith(self, tos, arg): |
|---|
| 367 |
self.imperfect = True |
|---|
| 368 |
return self.get_expr(tos.sql + " LIKE '%" + self.adapter.escape_like(arg.sql) + "'", bool) |
|---|
| 369 |
|
|---|
| 370 |
def containedby(self, op1, op2): |
|---|
| 371 |
self.imperfect = True |
|---|
| 372 |
return decompile.SQLDecompiler.containedby(self, op1, op2) |
|---|
| 373 |
|
|---|
| 374 |
def builtins_icontainedby(self, op1, op2): |
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
if op1.value is not None: |
|---|
| 378 |
|
|---|
| 379 |
return self.get_expr(op2.sql + " LIKE '%" + |
|---|
| 380 |
self.adapter.escape_like(op1.sql) |
|---|
| 381 |
+ "%'", bool) |
|---|
| 382 |
else: |
|---|
| 383 |
|
|---|
| 384 |
atoms = [self.adapter.coerce(x) for x in op2.value] |
|---|
| 385 |
if atoms: |
|---|
| 386 |
return self.get_expr("%s IN (%s)" % |
|---|
| 387 |
(op1.sql, ", ".join(atoms)), bool) |
|---|
| 388 |
else: |
|---|
| 389 |
|
|---|
| 390 |
return self.adapter.false_expr |
|---|
| 391 |
return value |
|---|
| 392 |
|
|---|
| 393 |
def builtins_istartswith(self, x, y): |
|---|
| 394 |
|
|---|
| 395 |
return self.get_expr(x.sql + " LIKE '" + self.adapter.escape_like(y.sql) + "%'", bool) |
|---|
| 396 |
|
|---|
| 397 |
def builtins_iendswith(self, x, y): |
|---|
| 398 |
|
|---|
| 399 |
return self.get_expr(x.sql + " LIKE '%" + self.adapter.escape_like(y.sql) + "'", bool) |
|---|
| 400 |
|
|---|
| 401 |
def builtins_ieq(self, x, y): |
|---|
| 402 |
|
|---|
| 403 |
return self.get_expr(x.sql + " = " + y.sql, bool) |
|---|
| 404 |
|
|---|
| 405 |
def func__builtin___len(self, x): |
|---|
| 406 |
return self.get_expr("Len(" + x.sql + ")", int) |
|---|
| 407 |
|
|---|
| 408 |
|
|---|
| 409 |
class ADOTable(geniusql.Table): |
|---|
| 410 |
|
|---|
| 411 |
def _add_column(self, column): |
|---|
| 412 |
"""Internal function to add the column to the database.""" |
|---|
| 413 |
coldef = self.schema.columnclause(column) |
|---|
| 414 |
|
|---|
| 415 |
self.schema.db.execute_ddl("ALTER TABLE %s ADD %s;" % |
|---|
| 416 |
(self.qname, coldef)) |
|---|
| 417 |
|
|---|
| 418 |
def _rename(self, oldcol, newcol): |
|---|
| 419 |
conn = self.schema.db.connections.get() |
|---|
| 420 |
try: |
|---|
| 421 |
cat = win32com.client.Dispatch(r'ADOX.Catalog') |
|---|
| 422 |
cat.ActiveConnection = conn |
|---|
| 423 |
cat.Tables(self.name).Columns(oldcol.name).Name = newcol.name |
|---|
| 424 |
finally: |
|---|
| 425 |
conn = None |
|---|
| 426 |
cat = None |
|---|
| 427 |
|
|---|
| 428 |
def drop_primary(self): |
|---|
| 429 |
"""Remove any PRIMARY KEY for this Table.""" |
|---|
| 430 |
db = self.schema.db |
|---|
| 431 |
|
|---|
| 432 |
data, _ = db.fetch(adSchemaIndexes, schema=True) |
|---|
| 433 |
pknames = [row[5] for row in data |
|---|
| 434 |
if (self.name == row[2]) and row[6]] |
|---|
| 435 |
for name in pknames: |
|---|
| 436 |
db.execute('ALTER TABLE %s DROP CONSTRAINT %s;' |
|---|
| 437 |
% (self.qname, name)) |
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 |
def connatoms(connstring): |
|---|
| 441 |
atoms = {} |
|---|
| 442 |
for pair in connstring.split(";"): |
|---|
| 443 |
if pair: |
|---|
| 444 |
k, v = pair.split("=", 1) |
|---|
| 445 |
atoms[k.upper().strip()] = v.strip() |
|---|
| 446 |
return atoms |
|---|
| 447 |
|
|---|
| 448 |
|
|---|
| 449 |
class ADOConnectionManager(conns.ConnectionManager): |
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 |
|
|---|
| 453 |
shutdowntimeout = 1 |
|---|
| 454 |
|
|---|
| 455 |
ConnectionTimeout = None |
|---|
| 456 |
CommandTimeout = None |
|---|
| 457 |
|
|---|
| 458 |
def _get_conn(self, master=False): |
|---|
| 459 |
if master: |
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
self.shutdown() |
|---|
| 463 |
|
|---|
| 464 |
atoms = connatoms(self.Connect) |
|---|
| 465 |
atoms['INITIAL CATALOG'] = "tempdb" |
|---|
| 466 |
connectstr = "; ".join(["%s=%s" % (k, v) |
|---|
| 467 |
for k, v in atoms.iteritems()]) |
|---|
| 468 |
else: |
|---|
| 469 |
connectstr = self.Connect |
|---|
| 470 |
|
|---|
| 471 |
conn = win32com.client.Dispatch(r'ADODB.Connection') |
|---|
| 472 |
conn.Open(connectstr) |
|---|
| 473 |
if self.ConnectionTimeout is not None: |
|---|
| 474 |
conn.ConnectionTimeout = self.ConnectionTimeout |
|---|
| 475 |
if self.CommandTimeout is not None: |
|---|
| 476 |
conn.CommandTimeout = self.CommandTimeout |
|---|
| 477 |
return conn |
|---|
| 478 |
|
|---|
| 479 |
def _del_conn(self, conn): |
|---|
| 480 |
for trial in xrange(self.shutdowntimeout * 10): |
|---|
| 481 |
try: |
|---|
| 482 |
|
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 |
conn.Close() |
|---|
| 486 |
return |
|---|
| 487 |
except pywintypes.com_error, e: |
|---|
| 488 |
try: |
|---|
| 489 |
ecode = e.args[2][-1] |
|---|
| 490 |
except IndexError: |
|---|
| 491 |
ecode = None |
|---|
| 492 |
if ecode == -2146824577: |
|---|
| 493 |
|
|---|
| 494 |
|
|---|
| 495 |
time.sleep(0.1) |
|---|
| 496 |
continue |
|---|
| 497 |
raise |
|---|
| 498 |
|
|---|
| 499 |
|
|---|
| 500 |
|
|---|
| 501 |
def start(self, isolation=None): |
|---|
| 502 |
"""Start a transaction. Not needed if self.implicit_trans is True.""" |
|---|
| 503 |
conn = self.get(started=True) |
|---|
| 504 |
self.db.execute("BEGIN TRANSACTION;", conn) |
|---|
| 505 |
self.isolate(conn, isolation) |
|---|
| 506 |
|
|---|
| 507 |
|
|---|
| 508 |
class ADOSchema(geniusql.Schema): |
|---|
| 509 |
|
|---|
| 510 |
tableclass = ADOTable |
|---|
| 511 |
|
|---|
| 512 |
|
|---|
| 513 |
|
|---|
| 514 |
def _get_tables(self, conn=None): |
|---|
| 515 |
|
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 |
|
|---|
| 519 |
data, _ = self.db.fetch(adSchemaTables, conn=conn, schema=True) |
|---|
| 520 |
return [self.tableclass(str(row[2]), self.db.quote(str(row[2])), |
|---|
| 521 |
self, created=True) |
|---|
| 522 |
for row in data |
|---|
| 523 |
|
|---|
| 524 |
if row[3] == "TABLE"] |
|---|
| 525 |
|
|---|
| 526 |
def _get_table(self, tablename, conn=None): |
|---|
| 527 |
|
|---|
| 528 |
|
|---|
| 529 |
|
|---|
| 530 |
|
|---|
| 531 |
data, _ = self.db.fetch(adSchemaTables, conn=conn, schema=True) |
|---|
| 532 |
for row in data: |
|---|
| 533 |
name = str(row[2]) |
|---|
| 534 |
if name == tablename: |
|---|
| 535 |
return self.tableclass(name, self.db.quote(name), |
|---|
| 536 |
self, created=True) |
|---|
| 537 |
raise errors.MappingError(tablename) |
|---|
| 538 |
|
|---|
| 539 |
def _get_columns(self, tablename, conn=None): |
|---|
| 540 |
|
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 |
|
|---|
| 544 |
|
|---|
| 545 |
|
|---|
| 546 |
|
|---|
| 547 |
|
|---|
| 548 |
|
|---|
| 549 |
|
|---|
| 550 |
|
|---|
| 551 |
data, _ = self.db.fetch(adSchemaIndexes, conn=conn, schema=True) |
|---|
| 552 |
pknames = [row[17] for row in data |
|---|
| 553 |
if (tablename == row[2]) and row[6]] |
|---|
| 554 |
|
|---|
| 555 |
|
|---|
| 556 |
|
|---|
| 557 |
|
|---|
| 558 |
|
|---|
| 559 |
|
|---|
| 560 |
|
|---|
| 561 |
|
|---|
| 562 |
|
|---|
| 563 |
|
|---|
| 564 |
|
|---|
| 565 |
|
|---|
| 566 |
|
|---|
| 567 |
|
|---|
| 568 |
data, _ = self.db.fetch(adSchemaColumns, conn=conn, schema=True) |
|---|
| 569 |
|
|---|
| 570 |
cols = [] |
|---|
| 571 |
get_pytype = self.db.typeadapter.python_type |
|---|
| 572 |
for row in data: |
|---|
| 573 |
|
|---|
| 574 |
|
|---|
| 575 |
if row[2] != tablename: |
|---|
| 576 |
continue |
|---|
| 577 |
|
|---|
| 578 |
dbtype = dbtypes[row[11]] |
|---|
| 579 |
pytype = get_pytype(dbtype) |
|---|
| 580 |
|
|---|
| 581 |
default = row[8] |
|---|
| 582 |
if default is not None: |
|---|
| 583 |
if issubclass(pytype, (int, long, float)): |
|---|
| 584 |
|
|---|
| 585 |
|
|---|
| 586 |
if default.startswith("'") and default.endswith("'"): |
|---|
| 587 |
default = default[1:-1] |
|---|
| 588 |
default = pytype(default) |
|---|
| 589 |
|
|---|
| 590 |
name = str(row[3]) |
|---|
| 591 |
c = geniusql.Column(pytype, dbtype, |
|---|
| 592 |
default, hints={}, key=(name in pknames), |
|---|
| 593 |
name=name, qname=self.db.quote(name)) |
|---|
| 594 |
|
|---|
| 595 |
|
|---|
| 596 |
|
|---|
| 597 |
colflags = int(row[9]) |
|---|
| 598 |
if ((colflags & DBCOLUMNFLAGS_ISFIXEDLENGTH) |
|---|
| 599 |
and not (colflags & DBCOLUMNFLAGS_WRITE)): |
|---|
| 600 |
c.autoincrement = True |
|---|
| 601 |
|
|---|
| 602 |
if dbtype in ("SMALLINT", "INTEGER", "TINYINT", |
|---|
| 603 |
"UNSIGNEDTINYINT", "UNSIGNEDSMALLINT", |
|---|
| 604 |
"UNSIGNEDINT", "BIGINT", "UNSIGNEDBIGINT"): |
|---|
| 605 |
c.hints['bytes'] = row[15] |
|---|
| 606 |
elif dbtype in ("SINGLE", "DOUBLE"): |
|---|
| 607 |
c.hints['precision'] = row[15] |
|---|
| 608 |
c.hints['scale'] = row[16] |
|---|
| 609 |
elif dbtype == "CURRENCY": |
|---|
| 610 |
|
|---|
| 611 |
|
|---|
| 612 |
c.hints['precision'] = 19 |
|---|
| 613 |
c.hints['scale'] = 4 |
|---|
| 614 |
elif dbtype in ("DECIMAL", "NUMERIC"): |
|---|
| 615 |
c.hints['precision'] = row[15] |
|---|
| 616 |
c.hints['scale'] = row[16] |
|---|
| 617 |
c.dbtype = "%s(%s, %s)" % (dbtype, row[15], row[16]) |
|---|
| 618 |
elif dbtype in ("BSTR", "VARIANT", "BINARY", "CHAR", |
|---|
| 619 |
"VARCHAR", "VARBINARY", "WCHAR", "VARWCHAR"): |
|---|
| 620 |
if row[13]: |
|---|
| 621 |
|
|---|
| 622 |
c.hints['bytes'] = b = int(row[13]) |
|---|
| 623 |
else: |
|---|
| 624 |
|
|---|
| 625 |
|
|---|
| 626 |
|
|---|
| 627 |
c.hints['bytes'] = b = (2 ** 31) - 1 |
|---|
| 628 |
c.dbtype = "%s(%s)" % (c.dbtype, b) |
|---|
| 629 |
elif dbtype in ("LONGVARCHAR", "LONGVARBINARY", "LONGVARWCHAR"): |
|---|
| 630 |
if row[13]: |
|---|
| 631 |
|
|---|
| 632 |
c.hints['bytes'] = b = int(row[13]) |
|---|
| 633 |
c.dbtype = "%s(%s)" % (c.dbtype, b) |
|---|
| 634 |
else: |
|---|
| 635 |
c.hints['bytes'] = 65535 |
|---|
| 636 |
|
|---|
| 637 |
cols.append(c) |
|---|
| 638 |
return cols |
|---|
| 639 |
|
|---|
| 640 |
def _get_indices(self, tablename=None, conn=None): |
|---|
| 641 |
|
|---|
| 642 |
|
|---|
| 643 |
|
|---|
| 644 |
|
|---|
| 645 |
|
|---|
| 646 |
|
|---|
| 647 |
|
|---|
| 648 |
|
|---|
| 649 |
|
|---|
| 650 |
data, _ = self.db.fetch(adSchemaIndexes, conn=conn, schema=True) |
|---|
| 651 |
indices = [] |
|---|
| 652 |
for row in data: |
|---|
| 653 |
|
|---|
| 654 |
|
|---|
| 655 |
if tablename and row[2] != tablename: |
|---|
| 656 |
continue |
|---|
| 657 |
i = geniusql.Index(row[5], self.db.quote(row[5]), |
|---|
| 658 |
row[2], row[17], row[7]) |
|---|
| 659 |
indices.append(i) |
|---|
| 660 |
return indices |
|---|
| 661 |
|
|---|
| 662 |
|
|---|
| 663 |
|
|---|
| 664 |
def _rename(self, oldtable, newtable): |
|---|
| 665 |
conn = self.db.connections.get() |
|---|
| 666 |
try: |
|---|
| 667 |
cat = win32com.client.Dispatch(r'ADOX.Catalog') |
|---|
| 668 |
cat.ActiveConnection = conn |
|---|
| 669 |
cat.Tables(oldtable.name).Name = newtable.name |
|---|
| 670 |
finally: |
|---|
| 671 |
conn = None |
|---|
| 672 |
cat = None |
|---|
| 673 |
|
|---|
| 674 |
|
|---|
| 675 |
class ADOTypeAdapter(adapters.TypeAdapter): |
|---|
| 676 |
|
|---|
| 677 |
_reverse_types = adapters.TypeAdapter._reverse_types.copy() |
|---|
| 678 |
_reverse_types.update({ |
|---|
| 679 |
"DBDATE": datetime.date, |
|---|
| 680 |
"DBTIME": datetime.time, |
|---|
| 681 |
"DBTIMESTAMP": datetime.datetime, |
|---|
| 682 |
|
|---|
| 683 |
"UNSIGNEDTINYINT": int, |
|---|
| 684 |
"UNSIGNEDSMALLINT": int, |
|---|
| 685 |
"UNSIGNEDINT": int, |
|---|
| 686 |
"BIT": bool, |
|---|
| 687 |
|
|---|
| 688 |
"UNSIGNEDBIGINT": long, |
|---|
| 689 |
|
|---|
| 690 |
"BSTR": str, |
|---|
| 691 |
"VARIANT": str, |
|---|
| 692 |
"BINARY": str, |
|---|
| 693 |
"LONGVARCHAR": str, |
|---|
| 694 |
"VARBINARY": str, |
|---|
| 695 |
"LONGVARBINARY": str, |
|---|
| 696 |
|
|---|
| 697 |
"WCHAR": unicode, |
|---|
| 698 |
"VARWCHAR": unicode, |
|---|
| 699 |
"LONGVARWCHAR": unicode, |
|---|
| 700 |
}) |
|---|
| 701 |
|
|---|
| 702 |
if typerefs.decimal: |
|---|
| 703 |
_reverse_types["CURRENCY"] = typerefs.decimal.Decimal |
|---|
| 704 |
elif typerefs.fixedpoint: |
|---|
| 705 |
_reverse_types["CURRENCY"] = typerefs.fixedpoint.FixedPoint |
|---|
| 706 |
|
|---|
| 707 |
|
|---|
| 708 |
class ADODatabase(geniusql.Database): |
|---|
| 709 |
|
|---|
| 710 |
decompiler = ADOSQLDecompiler |
|---|
| 711 |
adapterfromdb = AdapterFromADO() |
|---|
| 712 |
typeadapter = ADOTypeAdapter() |
|---|
| 713 |
|
|---|
| 714 |
|
|---|
| 715 |
|
|---|
| 716 |
def quote(self, name): |
|---|
| 717 |
"""Return name, quoted for use in an SQL statement.""" |
|---|
| 718 |
return '[' + name + ']' |
|---|
| 719 |
|
|---|
| 720 |
def execute(self, query, conn=None): |
|---|
| 721 |
if conn is None: |
|---|
| 722 |
conn = self.connections.get() |
|---|
| 723 |
if isinstance(query, unicode): |
|---|
| 724 |
query = query.encode(self.adaptertosql.encoding) |
|---|
| 725 |
|
|---|
| 726 |
self.log(query) |
|---|
| 727 |
try: |
|---|
| 728 |
bareconn = conn |
|---|
| 729 |
if hasattr(conn, 'conn'): |
|---|
| 730 |
|
|---|
| 731 |
|
|---|
| 732 |
|
|---|
| 733 |
|
|---|
| 734 |
|
|---|
| 735 |
|
|---|
| 736 |
bareconn = conn.conn |
|---|
| 737 |
|
|---|
| 738 |
|
|---|
| 739 |
bareconn._oleobj_.InvokeTypes(6, 0, 1, (9, 0), |
|---|
| 740 |
((8, 1), (16396, 18), (3, 49)), |
|---|
| 741 |
query, pythoncom.Missing, -1) |
|---|
| 742 |
except pywintypes.com_error, x: |
|---|
| 743 |
x.args += (query, ) |
|---|
| 744 |
conn = None |
|---|
| 745 |
raise |
|---|
| 746 |
|
|---|
| 747 |
def fetch(self, query, conn=None, schema=False): |
|---|
| 748 |
"""fetch(query, conn=None) -> rowdata, columns.""" |
|---|
| 749 |
if conn is None: |
|---|
| 750 |
conn = self.connections.get() |
|---|
| 751 |
|
|---|
| 752 |
try: |
|---|
| 753 |
if schema: |
|---|
| 754 |
|
|---|
| 755 |
res = conn._oleobj_.InvokeTypes(19, 0, 1, (9, 0), |
|---|
| 756 |
((3, 1), (12, 17), (12, 17)), |
|---|
| 757 |
query, Empty, Empty) |
|---|
| 758 |
else: |
|---|
| 759 |
self.log(query) |
|---|
| 760 |
bareconn = conn |
|---|
| 761 |
if hasattr(conn, 'conn'): |
|---|
| 762 |
|
|---|
| 763 |
|
|---|
| 764 |
bareconn = conn.conn |
|---|
| 765 |
|
|---|
| 766 |
|
|---|
| 767 |
res, rows_affected = bareconn._oleobj_.InvokeTypes(6, 0, 1, (9, 0), |
|---|
| 768 |
((8, 1), (16396, 18), (3, 49)), |
|---|
| 769 |
|
|---|
| 770 |
query, pythoncom.Missing, -1) |
|---|
| 771 |
except pywintypes.com_error, x: |
|---|
| 772 |
try: |
|---|
| 773 |
|
|---|
| 774 |
res.InvokeTypes(*Recordset_Close) |
|---|
| 775 |
except: |
|---|
| 776 |
pass |
|---|
| 777 |
res = None |
|---|
| 778 |
x.args += (query, ) |
|---|
| 779 |
conn = None |
|---|
| 780 |
|
|---|
| 781 |
raise x |
|---|
| 782 |
|
|---|
| 783 |
|
|---|
| 784 |
resFields = res.InvokeTypes(*Recordset_Fields) |
|---|
| 785 |
fieldcount = resFields.InvokeTypes(*Fields_Count) |
|---|
| 786 |
columns = [] |
|---|
| 787 |
for i in xrange(fieldcount): |
|---|
| 788 |
|
|---|
| 789 |
|
|---|
| 790 |
x = resFields.InvokeTypes(0, 0, 2, (9, 0), ((12, 1),), i) |
|---|
| 791 |
|
|---|
| 792 |
|
|---|
| 793 |
|
|---|
| 794 |
name = x.InvokeTypes(*Field_Name) |
|---|
| 795 |
typ = x.InvokeTypes(*Field_Type) |
|---|
| 796 |
columns.append((name, typ)) |
|---|
| 797 |
|
|---|
| 798 |
data = [] |
|---|
| 799 |
if not (res.InvokeTypes(*BOF) and res.InvokeTypes(*EOF)): |
|---|
| 800 |
|
|---|
| 801 |
|
|---|
| 802 |
|
|---|
| 803 |
data = res.InvokeTypes(*Recordset_GetRows) |
|---|
| 804 |
|
|---|
| 805 |
|
|---|
| 806 |
data = zip(*data) |
|---|
| 807 |
try: |
|---|
| 808 |
|
|---|
| 809 |
res.InvokeTypes(*Recordset_Close) |
|---|
| 810 |
except: |
|---|
| 811 |
pass |
|---|
| 812 |
conn = None |
|---|
| 813 |
|
|---|
| 814 |
return data, columns |
|---|
| 815 |
|
|---|
| 816 |
|
|---|
| 817 |
|
|---|
| 818 |
|
|---|
| 819 |
|
|---|
| 820 |
|
|---|
| 821 |
|
|---|
| 822 |
|
|---|
| 823 |
|
|---|
| 824 |
|
|---|
| 825 |
|
|---|
| 826 |
|
|---|
| 827 |
|
|---|
| 828 |
|
|---|
| 829 |
|
|---|
| 830 |
|
|---|
| 831 |
|
|---|
| 832 |
|
|---|
| 833 |
|
|---|
| 834 |
|
|---|
| 835 |
|
|---|
| 836 |
class ADOSQLDecompiler_SQLServer(ADOSQLDecompiler): |
|---|
| 837 |
|
|---|
| 838 |
def _compare_strings(self, op1, op, op2): |
|---|
| 839 |
|
|---|
| 840 |
if op < 6: |
|---|
| 841 |
|
|---|
| 842 |
|
|---|
| 843 |
|
|---|
| 844 |
return self.get_expr("Convert(binary, %s) %s Convert(binary, %s)" |
|---|
| 845 |
% (op1.sql, self.sql_cmp_op[op], op2.sql), |
|---|
| 846 |
bool) |
|---|
| 847 |
else: |
|---|
| 848 |
return ADOSQLDecompiler._compare_strings(self, op1, op, op2) |
|---|
| 849 |
|
|---|
| 850 |
def DATEADD(dt, td): |
|---|
| 851 |
"""Return the SQL to add a timedelta to a date.""" |
|---|
| 852 |
|
|---|
| 853 |
return ("DATEADD(dd, FLOOR(%s / 86400), " |
|---|
| 854 |
"DATEADD(ss, (%s %% 86400), %s))" |
|---|
| 855 |
% (td, td, dt)) |
|---|
| 856 |
DATEADD = staticmethod(DATEADD) |
|---|
| 857 |
|
|---|
| 858 |
def DATESUB(dt, td): |
|---|
| 859 |
"""Return the SQL to subtract a timedelta from a date.""" |
|---|
| 860 |
return "(%s - FLOOR(%s / 86400.0))" % (dt, td) |
|---|
| 861 |
DATESUB = staticmethod(DATESUB) |
|---|
| 862 |
|
|---|
| 863 |
def DATEDIFF(d1, d2): |
|---|
| 864 |
"""Return the SQL to subtract one date from another.""" |
|---|
| 865 |
|
|---|
| 866 |
return "CAST(DATEDIFF(dd, %s, %s) * 86400.0 AS NUMERIC)" % (d2, d1) |
|---|
| 867 |
DATEDIFF = staticmethod(DATEDIFF) |
|---|
| 868 |
|
|---|
| 869 |
def DATETIMEADD(dt, td): |
|---|
| 870 |
"""Return the SQL to add a timedelta to a datetime.""" |
|---|
| 871 |
return "(%s + (%s / 86400.0))" % (dt, td) |
|---|
| 872 |
DATETIMEADD = staticmethod(DATETIMEADD) |
|---|
| 873 |
|
|---|
| 874 |
def DATETIMEDIFF(d1, d2): |
|---|
| 875 |
"""Return the SQL to subtract one datetime from another.""" |
|---|
| 876 |
return "CAST(CAST(%s - %s AS FLOAT) * 86400 AS NUMERIC)" % (d1, d2) |
|---|
| 877 |
DATETIMEDIFF = staticmethod(DATETIMEDIFF) |
|---|
| 878 |
|
|---|
| 879 |
def DATETIMESUB(dt, td): |
|---|
| 880 |
"""Return the SQL to subtract a timedelta from a datetime.""" |
|---|
| 881 |
return "(%s - (%s / 86400.0))" % (dt, td) |
|---|
| 882 |
DATETIMESUB = staticmethod(DATETIMESUB) |
|---|
| 883 |
|
|---|
| 884 |
def TIMEDELTAADD(op1, op, op2): |
|---|
| 885 |
return "(%s %s %s)" % (op1.sql, op, op2.sql) |
|---|
| 886 |
TIMEDELTAADD = staticmethod(TIMEDELTAADD) |
|---|
| 887 |
|
|---|
| 888 |
def builtins_now(self): |
|---|
| 889 |
return self.get_expr("GETDATE()", datetime.datetime) |
|---|
| 890 |
|
|---|
| 891 |
def builtins_today(self): |
|---|
| 892 |
return self.get_expr("DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)", |
|---|
| 893 |
datetime.date) |
|---|
| 894 |
|
|---|
| 895 |
def builtins_year(self, x): |
|---|
| 896 |
return self.get_expr("DATEPART(year, " + x.sql + ")", int) |
|---|
| 897 |
|
|---|
| 898 |
def builtins_month(self, x): |
|---|
| 899 |
return self.get_expr("DATEPART(month, " + x.sql + ")", int) |
|---|
| 900 |
|
|---|
| 901 |
def builtins_day(self, x): |
|---|
| 902 |
return self.get_expr("DATEPART(day, " + x.sql + ")", int) |
|---|
| 903 |
|
|---|
| 904 |
def builtins_utcnow(self): |
|---|
| 905 |
return self.get_expr("GETUTCDATE()", datetime.datetime) |
|---|
| 906 |
|
|---|
| 907 |
|
|---|
| 908 |
class AdapterToADOSQL_SQLServer(adapters.AdapterToSQL): |
|---|
| 909 |
|
|---|
| 910 |
encoding = 'ISO-8859-1' |
|---|
| 911 |
|
|---|
| 912 |
escapes = [("'", "''")] |
|---|
| 913 |
like_escapes = [("[", "[[]"), ("%", "[%]"), ("_", "[_]"), |
|---|
| 914 |
("?", "[?]"), ("#", "[#]")] |
|---|
| 915 |
|
|---|
| 916 |
|
|---|
| 917 |
|
|---|
| 918 |
|
|---|
| 919 |
bool_true = "(1=1)" |
|---|
| 920 |
bool_false = "(1=0)" |
|---|
| 921 |
|
|---|
| 922 |
def coerce_bool_to_any(self, value): |
|---|
| 923 |
if value: |
|---|
| 924 |
return '1' |
|---|
| 925 |
return '0' |
|---|
| 926 |
|
|---|
| 927 |
def cast_VARCHAR_to_int(self, colref): |
|---|
| 928 |
return ("(CASE WHEN ISNUMERIC(%s)=1 THEN CAST(%s AS int) END)" |
|---|
| 929 |
% (colref, colref)) |
|---|
| 930 |
|
|---|
| 931 |
|
|---|
| 932 |
class AdapterFromADOSQL_SQLServer(AdapterFromADO): |
|---|
| 933 |
|
|---|
| 934 |
def coerce_any_to_datetime_time(self, value): |
|---|
| 935 |
|
|---|
| 936 |
|
|---|
| 937 |
return AdapterFromADO.coerce_any_to_datetime_time(self, float(value) - 2) |
|---|
| 938 |
|
|---|
| 939 |
def coerce_any_to_datetime_timedelta(self, value): |
|---|
| 940 |
|
|---|
| 941 |
days, secs = divmod(long(value), 86400) |
|---|
| 942 |
return datetime.timedelta(int(days), int(secs)) |
|---|
| 943 |
|
|---|
| 944 |
|
|---|
| 945 |
class TypeAdapter_SQLServer(ADOTypeAdapter): |
|---|
| 946 |
|
|---|
| 947 |
|
|---|
| 948 |
|
|---|
| 949 |
numeric_max_precision = 12 |
|---|
| 950 |
numeric_max_bytes = 6 |
|---|
| 951 |
|
|---|
| 952 |
def coerce_bool(self, hints): |
|---|
| 953 |
return "BIT" |
|---|
| 954 |
|
|---|
| 955 |
def coerce_datetime_datetime(self, hints): |
|---|
| 956 |
return "DATETIME" |
|---|
| 957 |
|
|---|
| 958 |
def coerce_datetime_date(self, hints): |
|---|
| 959 |
return "DATETIME" |
|---|
| 960 |
|
|---|
| 961 |
def coerce_datetime_time(self, hints): |
|---|
| 962 |
return "DATETIME" |
|---|
| 963 |
|
|---|
| 964 |
def int_type(self, bytes): |
|---|
| 965 |
"""Return a datatype which can handle the given number of bytes.""" |
|---|
| 966 |
if bytes <= 2: |
|---|
| 967 |
return "SMALLINT" |
|---|
| 968 |
elif bytes <= 4: |
|---|
| 969 |
return "INTEGER" |
|---|
| 970 |
elif bytes <= 8: |
|---|
| 971 |
|
|---|
| 972 |
return "BIGINT" |
|---|
| 973 |
else: |
|---|
| 974 |
|
|---|
| 975 |
return "NUMERIC(%s, 0)" % (bytes * 2) |
|---|
| 976 |
|
|---|
| 977 |
def coerce_str(self, hints): |
|---|
| 978 |
|
|---|
| 979 |
bytes = int(hints.get('bytes', 255)) |
|---|
| 980 |
|
|---|
| 981 |
if bytes == 0 or bytes > 8000: |
|---|
| 982 |
|
|---|
| 983 |
|
|---|
| 984 |
|
|---|
| 985 |
errors.warn("You have defined a string property without " |
|---|
| 986 |
"limiting its length. Microsoft SQL Server does " |
|---|
| 987 |
"not allow comparisons on string fields larger " |
|---|
| 988 |
"than 8000 characters. Some of your data may be " |
|---|
| 989 |
"truncated.") |
|---|
| 990 |
bytes = 8000 |
|---|
| 991 |
|
|---|
| 992 |
|
|---|
| 993 |
|
|---|
| 994 |
|
|---|
| 995 |
|
|---|
| 996 |
|
|---|
| 997 |
return "VARCHAR(%s)" % bytes |
|---|
| 998 |
|
|---|
| 999 |
|
|---|
| 1000 |
class SQLServerTable(ADOTable): |
|---|
| 1001 |
|
|---|
| 1002 |
def _rename(self, oldcol, newcol): |
|---|
| 1003 |
self.schema.db.execute_ddl("EXEC sp_rename '%s.%s', '%s', 'COLUMN'" % |
|---|
| 1004 |
(self.name, oldcol.name, newcol.name)) |
|---|
| 1005 |
|
|---|
| 1006 |
def _grab_new_ids(self, idkeys, conn): |
|---|
| 1007 |
"""Insert a row using the table's SERIAL field.""" |
|---|
| 1008 |
|
|---|
| 1009 |
|
|---|
| 1010 |
|
|---|
| 1011 |
data, _ = self.schema.db.fetch("SELECT IDENT_CURRENT('%s');" |
|---|
| 1012 |
% self.qname, conn) |
|---|
| 1013 |
return {idkeys[0]: data[0][0]} |
|---|
| 1014 |
|
|---|
| 1015 |
|
|---|
| 1016 |
class SQLServerConnectionManager(ADOConnectionManager): |
|---|
| 1017 |
|
|---|
| 1018 |
default_isolation = "READ COMMITTED" |
|---|
| 1019 |
|
|---|
| 1020 |
|
|---|
| 1021 |
class SQLServerSchema(ADOSchema): |
|---|
| 1022 |
|
|---|
| 1023 |
tableclass = SQLServerTable |
|---|
| 1024 |
|
|---|
| 1025 |
def create_database(self): |
|---|
| 1026 |
conn = self.db.connections._get_conn(master=True) |
|---|
| 1027 |
self.db.execute_ddl("CREATE DATABASE %s;" % self.qname, conn) |
|---|
| 1028 |
conn.Close() |
|---|
| 1029 |
self.clear() |
|---|
| 1030 |
|
|---|
| 1031 |
def drop_database(self): |
|---|
| 1032 |
conn = self.db.connections._get_conn(master=True) |
|---|
| 1033 |
self.db.execute_ddl("DROP DATABASE %s;" % self.qname, conn) |
|---|
| 1034 |
conn.Close() |
|---|
| 1035 |
self.clear() |
|---|
| 1036 |
|
|---|
| 1037 |
def columnclause(self, column): |
|---|
| 1038 |
"""Return a clause for the given column for CREATE or ALTER TABLE. |
|---|
| 1039 |
|
|---|
| 1040 |
This will be of the form: |
|---|
| 1041 |
name type [DEFAULT x|IDENTITY(initial, 1) NOT NULL] |
|---|
| 1042 |
""" |
|---|
| 1043 |
dbtype = column.dbtype |
|---|
| 1044 |
|
|---|
| 1045 |
clause = "" |
|---|
| 1046 |
if column.autoincrement: |
|---|
| 1047 |
if dbtype not in ("BOOLEAN", "SMALLINT", "INTEGER", "BIGINT"): |
|---|
| 1048 |
raise ValueError("SQL Server does not allow IDENTITY " |
|---|
| 1049 |
"columns of type %r" % dbtype) |
|---|
| 1050 |
clause = " IDENTITY(%s, 1) NOT NULL" % column.initial |
|---|
| 1051 |
else: |
|---|
| 1052 |
|
|---|
| 1053 |
|
|---|
| 1054 |
default = column.default or "" |
|---|
| 1055 |
if default: |
|---|
| 1056 |
clause = self.db.adaptertosql.coerce(default, dbtype) |
|---|
| 1057 |
clause = " DEFAULT %s" % clause |
|---|
| 1058 |
|
|---|
| 1059 |
return '%s %s%s' % (column.qname, dbtype, clause) |
|---|
| 1060 |
|
|---|
| 1061 |
|
|---|
| 1062 |
class SQLServerDatabase(ADODatabase): |
|---|
| 1063 |
|
|---|
| 1064 |
decompiler = ADOSQLDecompiler_SQLServer |
|---|
| 1065 |
adaptertosql = AdapterToADOSQL_SQLServer() |
|---|
| 1066 |
adapterfromdb = AdapterFromADOSQL_SQLServer() |
|---|
| 1067 |
typeadapter = TypeAdapter_SQLServer() |
|---|
| 1068 |
connectionmanager = SQLServerConnectionManager |
|---|
| 1069 |
schemaclass = SQLServerSchema |
|---|
| 1070 |
|
|---|
| 1071 |
def __init__(self, **kwargs): |
|---|
| 1072 |
ADODatabase.__init__(self, **kwargs) |
|---|
| 1073 |
if "2005" in self.version(): |
|---|
| 1074 |
self.connections.isolation_levels.append("SNAPSHOT") |
|---|
| 1075 |
|
|---|
| 1076 |
def version(self): |
|---|
| 1077 |
conn = self.connections._get_conn(master=True) |
|---|
| 1078 |
adov = conn.Version |
|---|
| 1079 |
data, coldefs = self.fetch("SELECT @@VERSION;", conn) |
|---|
| 1080 |
sqlv, = data[0] |
|---|
| 1081 |
conn.Close() |
|---|
| 1082 |
del conn |
|---|
| 1083 |
return "ADO Version: %s\n%s" % (adov, sqlv) |
|---|
| 1084 |
|
|---|
| 1085 |
def is_timeout_error(self, exc): |
|---|
| 1086 |
"""If the given exception instance is a lock timeout, return True. |
|---|
| 1087 |
|
|---|
| 1088 |
This should return True for errors which arise from transaction |
|---|
| 1089 |
locking timeouts; for example, if the database prevents 'dirty |
|---|
| 1090 |
reads' by raising an error. |
|---|
| 1091 |
""" |
|---|
| 1092 |
|
|---|
| 1093 |
|
|---|
| 1094 |
|
|---|
| 1095 |
|
|---|
| 1096 |
if not isinstance(exc, pywintypes.com_error): |
|---|
| 1097 |
return False |
|---|
| 1098 |
return exc.args[2][5] == -2147217871 |
|---|
| 1099 |
|
|---|
| 1100 |
|
|---|
| 1101 |
|
|---|
| 1102 |
|
|---|
| 1103 |
|
|---|
| 1104 |
|
|---|
| 1105 |
|
|---|
| 1106 |
|
|---|
| 1107 |
|
|---|
| 1108 |
|
|---|
| 1109 |
class ADOSQLDecompiler_MSAccess(ADOSQLDecompiler): |
|---|
| 1110 |
sql_cmp_op = ('<', '<=', '=', '<>', '>', '>=', 'in', 'not in') |
|---|
| 1111 |
|
|---|
| 1112 |
epoch = datetime.datetime(1899, 12, 30) |
|---|
| 1113 |
|
|---|
| 1114 |
def _compare_strings(self, op1, op, op2): |
|---|
| 1115 |
|
|---|
| 1116 |
if op < 6: |
|---|
| 1117 |
|
|---|
| 1118 |
|
|---|
| 1119 |
|
|---|
| 1120 |
|
|---|
| 1121 |
return self.get_expr("StrComp(%s, %s) %s 0" % |
|---|
| 1122 |
(op1.sql, op2.sql, self.sql_cmp_op[op]), |
|---|
| 1123 |
bool) |
|---|
| 1124 |
else: |
|---|
| 1125 |
return ADOSQLDecompiler._compare_strings(self, op1, op, op2) |
|---|
| 1126 |
|
|---|
| 1127 |
def builtins_now(self): |
|---|
| 1128 |
return self.get_expr("Now()", datetime.datetime) |
|---|
| 1129 |
|
|---|
| 1130 |
def builtins_today(self): |
|---|
| 1131 |
return self.get_expr("DateValue(Now())", datetime.date) |
|---|
| 1132 |
|
|---|
| 1133 |
def builtins_year(self, x): |
|---|
| 1134 |
return self.get_expr("Year(" + x.sql + ")", int) |
|---|
| 1135 |
|
|---|
| 1136 |
def builtins_month(self, x): |
|---|
| 1137 |
return self.get_expr("Month(" + x.sql + ")", int) |
|---|
| 1138 |
|
|---|
| 1139 |
def builtins_day(self, x): |
|---|
| 1140 |
return self.get_expr("Day(" + x.sql + ")", int) |
|---|
| 1141 |
|
|---|
| 1142 |
def DATEADD(dt, td): |
|---|
| 1143 |
"""Return the SQL to add a timedelta to a date.""" |
|---|
| 1144 |
|
|---|
| 1145 |
|
|---|
| 1146 |
return "DateAdd('d', Fix(%s), %s)" % (td, dt) |
|---|
| 1147 |
DATEADD = staticmethod(DATEADD) |
|---|
| 1148 |
|
|---|
| 1149 |
def DATEDIFF(d1, d2): |
|---|
| 1150 |
"""Return the SQL to subtract one date from another.""" |
|---|
| 1151 |
|
|---|
| 1152 |
|
|---|
| 1153 |
return "CDate(Fix(%s) - Fix(%s))" % (d1, d2) |
|---|
| 1154 |
DATEDIFF = staticmethod(DATEDIFF) |
|---|
| 1155 |
DATESUB = DATEDIFF |
|---|
| 1156 |
|
|---|
| 1157 |
def DATETIMEADD(dt, td): |
|---|
| 1158 |
"""Return the SQL to add a timedelta to a datetime.""" |
|---|
| 1159 |
return "CDate(%s + %s)" % (dt, td) |
|---|
| 1160 |
DATETIMEADD = staticmethod(DATETIMEADD) |
|---|
| 1161 |
|
|---|
| 1162 |
def DATETIMEDIFF(d1, d2): |
|---|
| 1163 |
"""Return the SQL to subtract one (datetime or date expr) from another.""" |
|---|
| 1164 |
return "CDate(%s - %s)" % (d1, d2) |
|---|
| 1165 |
DATETIMEDIFF = staticmethod(DATETIMEDIFF) |
|---|
| 1166 |
DATETIMESUB = DATETIMEDIFF |
|---|
| 1167 |
|
|---|
| 1168 |
def TIMEDELTAADD(op1, op, op2): |
|---|
| 1169 |
return "CDate(%s %s %s)" % (op1.sql, op, op2.sql) |
|---|
| 1170 |
TIMEDELTAADD = staticmethod(TIMEDELTAADD) |
|---|
| 1171 |
|
|---|
| 1172 |
|
|---|
| 1173 |
class TypeAdapter_MSAccess(ADOTypeAdapter): |
|---|
| 1174 |
|
|---|
| 1175 |
|
|---|
| 1176 |
|
|---|
| 1177 |
|
|---|
| 1178 |
numeric_max_precision = 12 |
|---|
| 1179 |
numeric_max_bytes = 6 |
|---|
| 1180 |
|
|---|
| 1181 |
_reverse_types = ADOTypeAdapter._reverse_types.copy() |
|---|
| 1182 |
_reverse_types.update({ |
|---|
| 1183 |
"LONG": int, |
|---|
| 1184 |
"MEMO": str, |
|---|
| 1185 |
}) |
|---|
| 1186 |
|
|---|
| 1187 |
def coerce_bool(self, hints): return "BIT" |
|---|
| 1188 |
|
|---|
| 1189 |
def coerce_datetime_datetime(self, hints): return "DATETIME" |
|---|
| 1190 |
def coerce_datetime_date(self, hints): return "DATETIME" |
|---|
| 1191 |
def coerce_datetime_time(self, hints): return "DATETIME" |
|---|
| 1192 |
def coerce_datetime_timedelta(self, hints): return "DATETIME" |
|---|
| 1193 |
|
|---|
| 1194 |
def int_type(self, bytes): |
|---|
| 1195 |
if bytes <= 2: |
|---|
| 1196 |
return "INTEGER" |
|---|
| 1197 |
elif bytes <= 4: |
|---|
| 1198 |
return "LONG" |
|---|
| 1199 |
else: |
|---|
| 1200 |
|
|---|
| 1201 |
return "DECIMAL" |
|---|
| 1202 |
|
|---|
| 1203 |
def coerce_str(self, hints): |
|---|
| 1204 |
|
|---|
| 1205 |
bytes = int(hints.get('bytes', 255)) |
|---|
| 1206 |
|
|---|
| 1207 |
|
|---|
| 1208 |
if bytes == 0 or bytes > 255: |
|---|
| 1209 |
|
|---|
| 1210 |
|
|---|
| 1211 |
|
|---|
| 1212 |
return "MEMO" |
|---|
| 1213 |
|
|---|
| 1214 |
return "VARCHAR(%s)" % bytes |
|---|
| 1215 |
|
|---|
| 1216 |
|
|---|
| 1217 |
class AdapterToADOSQL_MSAccess(adapters.AdapterToSQL): |
|---|
| 1218 |
"""Coerce Expression constants to ADO SQL.""" |
|---|
| 1219 |
|
|---|
| 1220 |
encoding = 'ISO-8859-1' |
|---|
| 1221 |
|
|---|
| 1222 |
escapes = [("'", "''")] |
|---|
| 1223 |
like_escapes = [("[", "[[]"), ("%", "[%]"), ("_", "[_]"), |
|---|
| 1224 |
("?", "[?]"), ("#", "[#]")] |
|---|
| 1225 |
|
|---|
| 1226 |
def coerce_datetime_datetime_to_any(self, value): |
|---|
| 1227 |
return ('#%s/%s/%s %02d:%02d:%02d#' % |
|---|
| 1228 |
(value.month, value.day, value.year, |
|---|
| 1229 |
value.hour, value.minute, value.second)) |
|---|
| 1230 |
|
|---|
| 1231 |
def coerce_datetime_date_to_any(self, value): |
|---|
| 1232 |
return '#%s/%s/%s#' % (value.month, value.day, value.year) |
|---|
| 1233 |
|
|---|
| 1234 |
def coerce_datetime_time_to_any(self, value): |
|---|
| 1235 |
return '#%02d:%02d:%02d#' % (value.hour, value.minute, value.second) |
|---|
| 1236 |
|
|---|
| 1237 |
def coerce_datetime_timedelta_to_any(self, value): |
|---|
| 1238 |
|
|---|
| 1239 |
|
|---|
| 1240 |
|
|---|
| 1241 |
|
|---|
| 1242 |
|
|---|
| 1243 |
return ("CDate(#12/30/1899# + (%r) + %r)" % |
|---|
| 1244 |
(value.days, (value.seconds / 86400.0))) |
|---|
| 1245 |
|
|---|
| 1246 |
|
|---|
| 1247 |
class MSAccessTable(ADOTable): |
|---|
| 1248 |
|
|---|
| 1249 |
def delete(self, **inputs): |
|---|
| 1250 |
"""Delete all rows matching the given identifier inputs.""" |
|---|
| 1251 |
|
|---|
| 1252 |
self.schema.db.execute('DELETE * FROM %s WHERE %s;' % |
|---|
| 1253 |
(self.qname, self.id_clause(**inputs))) |
|---|
| 1254 |
|
|---|
| 1255 |
def delete_all(self, **inputs): |
|---|
| 1256 |
"""Delete all rows matching the given inputs.""" |
|---|
| 1257 |
|
|---|
| 1258 |
self.schema.db.execute('DELETE * FROM %s WHERE %s;' % |
|---|
| 1259 |
(self.qname, self.whereclause(**inputs))) |
|---|
| 1260 |
|
|---|
| 1261 |
def _grab_new_ids(self, idkeys, conn): |
|---|
| 1262 |
data, _ = self.schema.db.fetch("SELECT @@IDENTITY;", conn) |
|---|
| 1263 |
return {idkeys[0]: data[0][0]} |
|---|
| 1264 |
|
|---|
| 1265 |
|
|---|
| 1266 |
class MSAccessConnectionManager(ADOConnectionManager): |
|---|
| 1267 |
|
|---|
| 1268 |
poolsize = 0 |
|---|
| 1269 |
default_isolation = "READ UNCOMMITTED" |
|---|
| 1270 |
isolation_levels = ["READ UNCOMMITTED",] |
|---|
| 1271 |
|
|---|
| 1272 |
def _set_factory(self): |
|---|
| 1273 |
|
|---|
| 1274 |
|
|---|
| 1275 |
|
|---|
| 1276 |
self._factory = conns.SingleConnection(self._get_conn, self._del_conn) |
|---|
| 1277 |
|
|---|
| 1278 |
def isolate(self, conn, isolation=None): |
|---|
| 1279 |
"""Set the isolation level of the given connection. |
|---|
| 1280 |
|
|---|
| 1281 |
If 'isolation' is None, our default_isolation will be used for new |
|---|
| 1282 |
connections. Valid values for the 'isolation' argument may be native |
|---|
| 1283 |
values for your particular database. However, it is recommended you |
|---|
| 1284 |
pass items from the global 'levels' list instead; these will be |
|---|
| 1285 |
automatically replaced with native values. |
|---|
| 1286 |
|
|---|
| 1287 |
For many databases, this must be executed after START TRANSACTION. |
|---|
| 1288 |
""" |
|---|
| 1289 |
if isolation is None: |
|---|
| 1290 |
isolation = self.default_isolation |
|---|
| 1291 |
|
|---|
| 1292 |
if isinstance(isolation, _isolation.IsolationLevel): |
|---|
| 1293 |
|
|---|
| 1294 |
isolation = isolation.name |
|---|
| 1295 |
if isolation not in self.isolation_levels: |
|---|
| 1296 |
raise ValueError("IsolationLevel %r not allowed by %s." |
|---|
| 1297 |
% (isolation, self.__class__.__name__)) |
|---|
| 1298 |
|
|---|
| 1299 |
|
|---|
| 1300 |
pass |
|---|
| 1301 |
|
|---|
| 1302 |
|
|---|
| 1303 |
class MSAccessSchema(ADOSchema): |
|---|
| 1304 |
|
|---|
| 1305 |
tableclass = MSAccessTable |
|---|
| 1306 |
|
|---|
| 1307 |
def _get_columns(self, tablename, conn=None): |
|---|
| 1308 |
cols = ADOSchema._get_columns(self, tablename, conn) |
|---|
| 1309 |
if conn is None: |
|---|
| 1310 |
conn = self.db.connections._factory() |
|---|
| 1311 |
|
|---|
| 1312 |
try: |
|---|
| 1313 |
|
|---|
| 1314 |
query = "SELECT * FROM %s WHERE FALSE;" % self.db.quote(tablename) |
|---|
| 1315 |
bareconn = conn |
|---|
| 1316 |
if hasattr(conn, 'conn'): |
|---|
| 1317 |
|
|---|
| 1318 |
|
|---|
| 1319 |
bareconn = conn.conn |
|---|
| 1320 |
|
|---|
| 1321 |
|
|---|
| 1322 |
res, rows_affected = conn._oleobj_.InvokeTypes(6, 0, 1, (9, 0), |
|---|
| 1323 |
((8, 1), (16396, 18), (3, 49)), |
|---|
| 1324 |
|
|---|
| 1325 |
query, pythoncom.Missing, -1) |
|---|
| 1326 |
except pywintypes.com_error, x: |
|---|
| 1327 |
try: |
|---|
| 1328 |
res.InvokeTypes(*Recordset_Close) |
|---|
| 1329 |
except: |
|---|
| 1330 |
pass |
|---|
| 1331 |
res = None |
|---|
| 1332 |
x.args += (query, ) |
|---|
| 1333 |
conn = None |
|---|
| 1334 |
|
|---|
| 1335 |
try: |
|---|
| 1336 |
if "no read permission" in x.args[2][2]: |
|---|
| 1337 |
conn = None |
|---|
| 1338 |
return [] |
|---|
| 1339 |
except IndexError: |
|---|
| 1340 |
pass |
|---|
| 1341 |
|
|---|
| 1342 |
|
|---|
| 1343 |
raise x |
|---|
| 1344 |
|
|---|
| 1345 |
resFields = res.InvokeTypes(*Recordset_Fields) |
|---|
| 1346 |
for c in cols: |
|---|
| 1347 |
f = resFields.InvokeTypes(0, 0, 2, (9, 0), ((12, 1),), c.name) |
|---|
| 1348 |
fprops = f.InvokeTypes(*Field_Properties) |
|---|
| 1349 |
fprop = fprops.InvokeTypes(0, 0, 2, (9, 0), ((12, 1), ), "ISAUTOINCREMENT") |
|---|
| 1350 |
c.autoincrement = fprop.InvokeTypes(*Property_Value) |
|---|
| 1351 |
|
|---|
| 1352 |
try: |
|---|
| 1353 |
res.InvokeTypes(*Recordset_Close) |
|---|
| 1354 |
except: |
|---|
| 1355 |
pass |
|---|
| 1356 |
conn = None |
|---|
| 1357 |
|
|---|
| 1358 |
return cols |
|---|
| 1359 |
|
|---|
| 1360 |
def columnclause(self, column): |
|---|
| 1361 |
"""Return a clause for the given column for CREATE or ALTER TABLE. |
|---|
| 1362 |
|
|---|
| 1363 |
This will be of the form: |
|---|
| 1364 |
name type [DEFAULT x|AUTOINCREMENT(initial, 1)] |
|---|
| 1365 |
""" |
|---|
| 1366 |
dbtype = column.dbtype |
|---|
| 1367 |
|
|---|
| 1368 |
if column.autoincrement: |
|---|
| 1369 |
|
|---|
| 1370 |
|
|---|
| 1371 |
|
|---|
| 1372 |
dbtype = "AUTOINCREMENT(%s, 1)" % column.initial |
|---|
| 1373 |
else: |
|---|
| 1374 |
default = column.default or "" |
|---|
| 1375 |
if default: |
|---|
| 1376 |
defspec = self.db.adaptertosql.coerce(default, dbtype) |
|---|
| 1377 |
if isinstance(default, (int, long)): |
|---|
| 1378 |
|
|---|
| 1379 |
defspec = "'%s'" % defspec |
|---|
| 1380 |
dbtype = "%s DEFAULT %s" % (dbtype, defspec) |
|---|
| 1381 |
|
|---|
| 1382 |
return '%s %s' % (column.qname, dbtype) |
|---|
| 1383 |
|
|---|
| 1384 |
def create_database(self): |
|---|
| 1385 |
|
|---|
| 1386 |
cat = win32com.client.Dispatch(r'ADOX.Catalog') |
|---|
| 1387 |
cat.Create(self.db.connections.Connect) |
|---|
| 1388 |
cat.ActiveConnection.Close() |
|---|
| 1389 |
self.clear() |
|---|
| 1390 |
|
|---|
| 1391 |
def drop_database(self): |
|---|
| 1392 |
|
|---|
| 1393 |
|
|---|
| 1394 |
self.db.connections.shutdown() |
|---|
| 1395 |
|
|---|
| 1396 |
import os |
|---|
| 1397 |
|
|---|
| 1398 |
if os.path.exists(self.name): |
|---|
| 1399 |
os.remove(self.name) |
|---|
| 1400 |
|
|---|
| 1401 |
self.clear() |
|---|
| 1402 |
|
|---|
| 1403 |
|
|---|
| 1404 |
class MSAccessDatabase(ADODatabase): |
|---|
| 1405 |
|
|---|
| 1406 |
decompiler = ADOSQLDecompiler_MSAccess |
|---|
| 1407 |
adaptertosql = AdapterToADOSQL_MSAccess() |
|---|
| 1408 |
typeadapter = TypeAdapter_MSAccess() |
|---|
| 1409 |
connectionmanager = MSAccessConnectionManager |
|---|
| 1410 |
schemaclass = MSAccessSchema |
|---|
| 1411 |
|
|---|
| 1412 |
def version(self): |
|---|
| 1413 |
conn = win32com.client.Dispatch(r'ADODB.Connection') |
|---|
| 1414 |
v = conn.Version |
|---|
| 1415 |
del conn |
|---|
| 1416 |
return "ADO Version: %s" % v |
|---|
| 1417 |
|
|---|
| 1418 |
|
|---|
| 1419 |
def gen_py(): |
|---|
| 1420 |
"""Auto generate .py support for ADO 2.7+""" |
|---|
| 1421 |
print 'Please wait while support for ADO 2.7+ is verified...' |
|---|
| 1422 |
|
|---|
| 1423 |
|
|---|
| 1424 |
result = win32com.client.gencache.EnsureModule('{2A75196C-D9EB-4129-B803-931327F72D5C}', 0, 2, 8) |
|---|
| 1425 |
if result is not None: |
|---|
| 1426 |
return |
|---|
| 1427 |
|
|---|
| 1428 |
|
|---|
| 1429 |
result = win32com.client.gencache.EnsureModule('{EF53050B-882E-4776-B643-EDA472E8E3F2}', 0, 2, 7) |
|---|
| 1430 |
if result is not None: |
|---|
| 1431 |
return |
|---|
| 1432 |
|
|---|
| 1433 |
raise ImportError("ADO 2.7 support could not be imported/cached") |
|---|
| 1434 |
|
|---|
| 1435 |
|
|---|
| 1436 |
if __name__ == '__main__': |
|---|
| 1437 |
gen_py() |
|---|