dbapi.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. """A DB API 2.0 interface to SQL Server for Django
  2. Forked from: adodbapi v2.1
  3. Copyright (C) 2002 Henrik Ekelund, version 2.1 by Vernon Cole
  4. * http://adodbapi.sourceforge.net/
  5. * http://sourceforge.net/projects/pywin32/
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. * Version 2.1D by Adam Vandenberg, forked for Django backend use.
  18. This module is a db-api 2 interface for ADO, but is Django & SQL Server.
  19. It won't work against other ADO sources (like Access.)
  20. DB-API 2.0 specification: http://www.python.org/dev/peps/pep-0249/
  21. """
  22. from __future__ import absolute_import, unicode_literals
  23. import time
  24. import datetime
  25. import re
  26. import uuid
  27. import decimal
  28. from pprint import pformat
  29. from django.conf import settings
  30. from django.db.utils import (IntegrityError as DjangoIntegrityError,
  31. DatabaseError as DjangoDatabaseError)
  32. from django.utils import six
  33. from django.utils import timezone
  34. from .ado_consts import (adBigInt, adBinary, adBoolean, adBSTR, adChapter,
  35. adChar, adCmdStoredProc, adCmdText, adCurrency, adDate, adDBDate, adDBTime,
  36. adDBTimeStamp, adDecimal, adDouble, adError, adFileTime, adFldMayBeNull,
  37. adGUID, adInteger, adLongVarBinary, adLongVarChar, adLongVarWChar,
  38. adNumeric, ado_error_TIMEOUT, ado_type_name, adoErrors, adParamInput,
  39. adParamInputOutput, adParamUnknown, adSingle, adSmallInt, adStateClosed,
  40. adTinyInt, adTypeNames, adUnsignedBigInt, adUnsignedInt, adUnsignedSmallInt,
  41. adUnsignedTinyInt, adUseServer, adVarBinary, adVarChar, adVarNumeric,
  42. adVarWChar, adWChar, adXactAbortRetaining, adXactCommitRetaining,
  43. adXactReadCommitted)
  44. # DB API default values
  45. apilevel = '2.0'
  46. # 1: Threads may share the module, but not connections.
  47. threadsafety = 1
  48. # The underlying ADO library expects parameters as '?', but this wrapper
  49. # expects '%s' parameters. This wrapper takes care of the conversion.
  50. paramstyle = 'format'
  51. # Set defaultIsolationLevel on module level before creating the connection.
  52. # It may be one of "adXact..." consts.
  53. defaultIsolationLevel = adXactReadCommitted
  54. # Set defaultCursorLocation on module level before creating the connection.
  55. # It may be one of the "adUse..." consts.
  56. defaultCursorLocation = adUseServer
  57. # Used for COM to Python date conversions.
  58. _ordinal_1899_12_31 = datetime.date(1899, 12, 31).toordinal() - 1
  59. _milliseconds_per_day = 24 * 60 * 60 * 1000
  60. class MultiMap(object):
  61. def __init__(self, mapping, default=None):
  62. """Defines a mapping with multiple keys per value.
  63. mapping is a dict of: tuple(key, key, key...) => value
  64. """
  65. self.storage = dict()
  66. self.default = default
  67. for keys, value in six.iteritems(mapping):
  68. for key in keys:
  69. self.storage[key] = value
  70. def __getitem__(self, key):
  71. return self.storage.get(key, self.default)
  72. def standardErrorHandler(connection, cursor, errorclass, errorvalue):
  73. err = (errorclass, errorvalue)
  74. if connection is not None:
  75. connection.messages.append(err)
  76. if cursor is not None:
  77. cursor.messages.append(err)
  78. raise errorclass(errorvalue)
  79. class Error(Exception if six.PY3 else StandardError):
  80. pass
  81. class Warning(Exception if six.PY3 else StandardError):
  82. pass
  83. class InterfaceError(Error):
  84. pass
  85. class DatabaseError(DjangoDatabaseError, Error):
  86. pass
  87. class InternalError(DatabaseError):
  88. pass
  89. class OperationalError(DatabaseError):
  90. pass
  91. class ProgrammingError(DatabaseError):
  92. pass
  93. class IntegrityError(DatabaseError, DjangoIntegrityError):
  94. pass
  95. class DataError(DatabaseError):
  96. pass
  97. class NotSupportedError(DatabaseError):
  98. pass
  99. class FetchFailedError(Error):
  100. """
  101. Error is used by RawStoredProcedureQuerySet to determine when a fetch
  102. failed due to a connection being closed or there is no record set
  103. returned.
  104. """
  105. pass
  106. class _DbType(object):
  107. def __init__(self, valuesTuple):
  108. self.values = valuesTuple
  109. def __eq__(self, other):
  110. return other in self.values
  111. def __ne__(self, other):
  112. return other not in self.values
  113. _re_find_password = re.compile('(pwd|password)=[^;]*;', re.IGNORECASE)
  114. def mask_connection_string_password(s, mask='******'):
  115. """
  116. Look for a connection string password in 's' and mask it.
  117. """
  118. return re.sub(_re_find_password, '\g<1>=%s;' % mask, s)
  119. def connect(connection_string, timeout=30, use_transactions=None):
  120. """Connect to a database.
  121. connection_string -- An ADODB formatted connection string, see:
  122. http://www.connectionstrings.com/?carrier=sqlserver2005
  123. timeout -- A command timeout value, in seconds (default 30 seconds)
  124. """
  125. # Inner imports to make this module importable on non-Windows platforms.
  126. import pythoncom
  127. import win32com.client
  128. try:
  129. pythoncom.CoInitialize()
  130. c = win32com.client.Dispatch('ADODB.Connection')
  131. c.CommandTimeout = timeout
  132. c.ConnectionString = connection_string
  133. c.Open()
  134. if use_transactions is None:
  135. useTransactions = _use_transactions(c)
  136. else:
  137. useTransactions = use_transactions
  138. return Connection(c, useTransactions)
  139. except Exception as e:
  140. raise OperationalError(e,
  141. "Error opening connection: {0}".format(
  142. mask_connection_string_password(connection_string)
  143. )
  144. )
  145. def _use_transactions(c):
  146. """Return True if the given ADODB.Connection supports transactions."""
  147. for prop in c.Properties:
  148. if prop.Name == 'Transaction DDL':
  149. return prop.Value > 0
  150. return False
  151. def format_parameters(parameters, show_value=False):
  152. """
  153. Format a collection of ADO Command Parameters.
  154. Used by error reporting in _execute_command.
  155. """
  156. directions = {
  157. 0: 'Unknown',
  158. 1: 'Input',
  159. 2: 'Output',
  160. 3: 'In/Out',
  161. 4: 'Return',
  162. }
  163. if show_value:
  164. desc = [
  165. "Name: %s, Dir.: %s, Type: %s, Size: %s, Value: \"%s\", Precision: %s, NumericScale: %s" %
  166. (p.Name, directions[p.Direction], adTypeNames.get(p.Type, str(p.Type) + ' (unknown type)'),
  167. p.Size, p.Value, p.Precision, p.NumericScale)
  168. for p in parameters
  169. ]
  170. else:
  171. desc = [
  172. "Name: %s, Dir.: %s, Type: %s, Size: %s, Precision: %s, NumericScale: %s" %
  173. (p.Name, directions[p.Direction], adTypeNames.get(p.Type, str(p.Type) + ' (unknown type)'),
  174. p.Size, p.Precision, p.NumericScale)
  175. for p in parameters
  176. ]
  177. return pformat(desc)
  178. # return '[' + ', '.join(desc) + ']'
  179. def format_decimal_as_string(value):
  180. """
  181. Convert a decimal.Decimal to a fixed point string. Code borrowed from
  182. Python's moneyfmt recipe.
  183. https://docs.python.org/2/library/decimal.html#recipes
  184. """
  185. sign, digits, exp = value.as_tuple()
  186. if exp > 0:
  187. # Handle Decimals like '2.82E+3'
  188. return "%d" % value
  189. result = []
  190. digits = list(map(str, digits))
  191. build, next = result.append, digits.pop
  192. for i in range(-exp):
  193. build(next() if digits else '0')
  194. build('.')
  195. if not digits:
  196. build('0')
  197. while digits:
  198. build(next())
  199. if sign:
  200. build('-')
  201. return ''.join(reversed(result))
  202. def _configure_parameter(p, value):
  203. """Configure the given ADO Parameter 'p' with the Python 'value'."""
  204. if p.Direction not in [adParamInput, adParamInputOutput, adParamUnknown]:
  205. return
  206. if isinstance(value, six.string_types):
  207. p.Value = value
  208. p.Size = len(value)
  209. elif isinstance(value, six.memoryview):
  210. p.Size = len(value)
  211. p.AppendChunk(value)
  212. elif isinstance(value, decimal.Decimal):
  213. p.Type = adBSTR
  214. p.Value = format_decimal_as_string(value)
  215. elif isinstance(value, datetime.datetime):
  216. p.Type = adBSTR
  217. if timezone.is_aware(value):
  218. value = timezone.make_naive(value, timezone.utc)
  219. # Strip '-' so SQL Server parses as YYYYMMDD for all languages/formats
  220. s = value.isoformat(' ' if six.PY3 else b' ').replace('-', '')
  221. p.Value = s
  222. p.Size = len(s)
  223. elif isinstance(value, (datetime.time, datetime.date)):
  224. p.Type = adBSTR
  225. s = value.isoformat()
  226. p.Value = s
  227. p.Size = len(s)
  228. elif isinstance(value, uuid.UUID):
  229. p.Type = adBSTR
  230. s = str(value)
  231. p.Value = s
  232. p.Size = len(s)
  233. else:
  234. # For any other type, set the value and let pythoncom do the right thing.
  235. p.Value = value
  236. # Use -1 instead of 0 for empty strings and buffers
  237. if p.Size == 0:
  238. p.Size = -1
  239. class Connection(object):
  240. def __init__(self, adoConn, useTransactions=False):
  241. self.adoConn = adoConn
  242. self.errorhandler = None
  243. self.messages = []
  244. self.adoConn.CursorLocation = defaultCursorLocation
  245. self.supportsTransactions = useTransactions
  246. self.transaction_level = 0 # 0 == Not in a transaction, at the top level
  247. if self.supportsTransactions:
  248. self.adoConn.IsolationLevel = defaultIsolationLevel
  249. self.transaction_level = self.adoConn.BeginTrans() # Disables autocommit per DBPAI
  250. def set_autocommit(self, value):
  251. if self.supportsTransactions == (not value):
  252. return
  253. if self.supportsTransactions:
  254. self.transaction_level = self.adoConn.RollbackTrans() # Disables autocommit per DBPAI
  255. else:
  256. self.adoConn.IsolationLevel = defaultIsolationLevel
  257. self.transaction_level = self.adoConn.BeginTrans() # Disables autocommit per DBPAI
  258. self.supportsTransactions = not value
  259. def _raiseConnectionError(self, errorclass, errorvalue):
  260. eh = self.errorhandler
  261. if eh is None:
  262. eh = standardErrorHandler
  263. eh(self, None, errorclass, errorvalue)
  264. def _close_connection(self):
  265. """Close the underlying ADO Connection object, rolling back an active transaction if supported."""
  266. if self.supportsTransactions:
  267. self.transaction_level = self.adoConn.RollbackTrans()
  268. self.adoConn.Close()
  269. def close(self):
  270. """Close the database connection."""
  271. self.messages = []
  272. try:
  273. self._close_connection()
  274. except Exception as e:
  275. self._raiseConnectionError(InternalError, e)
  276. self.adoConn = None
  277. # Inner import to make this module importable on non-Windows platforms.
  278. import pythoncom
  279. pythoncom.CoUninitialize()
  280. def commit(self):
  281. """Commit a pending transaction to the database.
  282. Note that if the database supports an auto-commit feature, this must
  283. be initially off.
  284. """
  285. self.messages = []
  286. if not self.supportsTransactions:
  287. return
  288. try:
  289. self.transaction_level = self.adoConn.CommitTrans()
  290. if not(self.adoConn.Attributes & adXactCommitRetaining):
  291. # If attributes has adXactCommitRetaining it performs retaining commits that is,
  292. # calling CommitTrans automatically starts a new transaction. Not all providers support this.
  293. # If not, we will have to start a new transaction by this command:
  294. self.adoConn.BeginTrans()
  295. except Exception as e:
  296. self._raiseConnectionError(Error, e)
  297. def rollback(self):
  298. """Abort a pending transaction."""
  299. self.messages = []
  300. with self.cursor() as cursor:
  301. cursor.execute("select @@TRANCOUNT")
  302. trancount, = cursor.fetchone()
  303. if trancount == 0:
  304. return
  305. self.transaction_level = self.adoConn.RollbackTrans()
  306. if not(self.adoConn.Attributes & adXactAbortRetaining):
  307. # If attributes has adXactAbortRetaining it performs retaining aborts that is,
  308. # calling RollbackTrans automatically starts a new transaction. Not all providers support this.
  309. # If not, we will have to start a new transaction by this command:
  310. self.transaction_level = self.adoConn.BeginTrans()
  311. def cursor(self):
  312. """Return a new Cursor object using the current connection."""
  313. self.messages = []
  314. return Cursor(self)
  315. def printADOerrors(self):
  316. print('ADO Errors (%i):' % self.adoConn.Errors.Count)
  317. for e in self.adoConn.Errors:
  318. print('Description: %s' % e.Description)
  319. print('Error: %s %s ' % (e.Number, adoErrors.get(e.Number, "unknown")))
  320. if e.Number == ado_error_TIMEOUT:
  321. print('Timeout Error: Try using adodbpi.connect(constr,timeout=Nseconds)')
  322. print('Source: %s' % e.Source)
  323. print('NativeError: %s' % e.NativeError)
  324. print('SQL State: %s' % e.SQLState)
  325. def _suggest_error_class(self):
  326. """
  327. Introspect the current ADO Errors and determine an appropriate error class.
  328. Error.SQLState is a SQL-defined error condition, per the SQL specification:
  329. http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt
  330. The 23000 class of errors are integrity errors.
  331. Error 40002 is a transactional integrity error.
  332. """
  333. if self.adoConn is not None:
  334. for e in self.adoConn.Errors:
  335. state = str(e.SQLState)
  336. if state.startswith('23') or state == '40002':
  337. return IntegrityError
  338. return DatabaseError
  339. def __del__(self):
  340. try:
  341. self._close_connection()
  342. except:
  343. pass
  344. self.adoConn = None
  345. class Cursor(object):
  346. # This read-only attribute is a sequence of 7-item sequences.
  347. # Each of these sequences contains information describing one result column:
  348. # (name, type_code, display_size, internal_size, precision, scale, null_ok).
  349. # This attribute will be None for operations that do not return rows or if the
  350. # cursor has not had an operation invoked via the executeXXX() method yet.
  351. # The type_code can be interpreted by comparing it to the Type Objects specified in the section below.
  352. description = None
  353. # This read-only attribute specifies the number of rows that the last executeXXX() produced
  354. # (for DQL statements like select) or affected (for DML statements like update or insert).
  355. # The attribute is -1 in case no executeXXX() has been performed on the cursor or
  356. # the rowcount of the last operation is not determinable by the interface.[7]
  357. # NOTE: -- adodbapi returns "-1" by default for all select statements
  358. rowcount = -1
  359. # Arraysize specifies the number of rows to fetch at a time with fetchmany().
  360. arraysize = 1
  361. def __init__(self, connection):
  362. self.messages = []
  363. self.connection = connection
  364. self.rs = None
  365. self.description = None
  366. self.errorhandler = connection.errorhandler
  367. def __iter__(self):
  368. return iter(self.fetchone, None)
  369. def __enter__(self):
  370. "Allow database cursors to be used with context managers."
  371. return self
  372. def __exit__(self, exc_type, exc_val, exc_tb):
  373. "Allow database cursors to be used with context managers."
  374. self.close()
  375. def __del__(self):
  376. try:
  377. self.close()
  378. except:
  379. pass
  380. def _raiseCursorError(self, errorclass, errorvalue):
  381. eh = self.errorhandler
  382. if eh is None:
  383. eh = standardErrorHandler
  384. eh(self.connection, self, errorclass, errorvalue)
  385. def _description_from_recordset(self, recordset):
  386. # Abort if closed or no recordset.
  387. if (recordset is None) or (recordset.State == adStateClosed):
  388. self.rs = None
  389. self.description = None
  390. return
  391. # Since we use a forward-only cursor, rowcount will always return -1
  392. self.rowcount = -1
  393. self.rs = recordset
  394. desc = list()
  395. for f in self.rs.Fields:
  396. display_size = None
  397. if not(self.rs.EOF or self.rs.BOF):
  398. display_size = f.ActualSize
  399. null_ok = bool(f.Attributes & adFldMayBeNull)
  400. desc.append(
  401. (f.Name, f.Type, display_size, f.DefinedSize, f.Precision, f.NumericScale, null_ok)
  402. )
  403. self.description = desc
  404. def close(self):
  405. """Close the cursor."""
  406. self.messages = []
  407. self.connection = None
  408. if self.rs and self.rs.State != adStateClosed:
  409. self.rs.Close()
  410. self.rs = None
  411. def _new_command(self, command_type=adCmdText):
  412. self.cmd = None
  413. self.messages = []
  414. if self.connection is None:
  415. self._raiseCursorError(InterfaceError, None)
  416. return
  417. # Inner import to make this module importable on non-Windows platforms.
  418. import win32com.client
  419. try:
  420. self.cmd = win32com.client.Dispatch("ADODB.Command")
  421. self.cmd.ActiveConnection = self.connection.adoConn
  422. self.cmd.CommandTimeout = self.connection.adoConn.CommandTimeout
  423. self.cmd.CommandType = command_type
  424. except:
  425. self._raiseCursorError(DatabaseError, None)
  426. def _execute_command(self):
  427. # Sprocs may have an integer return value
  428. self.return_value = None
  429. try:
  430. recordset = self.cmd.Execute()
  431. self.rowcount = recordset[1]
  432. self._description_from_recordset(recordset[0])
  433. except Exception as e:
  434. _message = ""
  435. if hasattr(e, 'args'):
  436. _message += str(e.args) + "\n"
  437. _message += "Command:\n{}\nParameters:\n{}".format(
  438. self.cmd.CommandText, format_parameters(self.cmd.Parameters, True)
  439. )
  440. klass = self.connection._suggest_error_class()
  441. self._raiseCursorError(klass, _message)
  442. def callproc(self, procname, parameters=None):
  443. """Call a stored database procedure with the given name.
  444. The sequence of parameters must contain one entry for each
  445. argument that the sproc expects. The result of the
  446. call is returned as modified copy of the input
  447. sequence. Input parameters are left untouched, output and
  448. input/output parameters replaced with possibly new values.
  449. The sproc may also provide a result set as output,
  450. which is available through the standard .fetch*() methods.
  451. Extension: A "return_value" property may be set on the
  452. cursor if the sproc defines an integer return value.
  453. """
  454. self._new_command(adCmdStoredProc)
  455. self.cmd.CommandText = procname
  456. self.cmd.Parameters.Refresh()
  457. try:
  458. # Return value is 0th ADO parameter. Skip it.
  459. for i, p in enumerate(tuple(self.cmd.Parameters)[1:]):
  460. _configure_parameter(p, parameters[i])
  461. except:
  462. _message = 'Converting Parameter %s: %s, %s\n' %\
  463. (p.Name, ado_type_name(p.Type), repr(parameters[i]))
  464. self._raiseCursorError(DataError, _message)
  465. self._execute_command()
  466. p_return_value = self.cmd.Parameters(0)
  467. self.return_value = _convert_to_python(p_return_value.Value, p_return_value.Type)
  468. return [_convert_to_python(p.Value, p.Type)
  469. for p in tuple(self.cmd.Parameters)[1:]]
  470. def execute(self, operation, parameters=None):
  471. """Prepare and execute a database operation (query or command).
  472. Return value is not defined.
  473. """
  474. self._new_command()
  475. if parameters is None:
  476. parameters = list()
  477. parameter_replacements = list()
  478. for i, value in enumerate(parameters):
  479. if value is None:
  480. parameter_replacements.append('NULL')
  481. continue
  482. if isinstance(value, six.string_types) and value == "":
  483. parameter_replacements.append("''")
  484. continue
  485. # Otherwise, process the non-NULL, non-empty string parameter.
  486. parameter_replacements.append('?')
  487. try:
  488. p = self.cmd.CreateParameter('p%i' % i, _ado_type(value))
  489. except KeyError:
  490. _message = 'Failed to map python type "%s" to an ADO type' % (value.__class__.__name__,)
  491. self._raiseCursorError(DataError, _message)
  492. except:
  493. _message = 'Creating Parameter p%i, %s' % (i, _ado_type(value))
  494. self._raiseCursorError(DataError, _message)
  495. try:
  496. _configure_parameter(p, value)
  497. self.cmd.Parameters.Append(p)
  498. except Exception:
  499. _message = 'Converting Parameter %s: %s, %s\n' %\
  500. (p.Name, ado_type_name(p.Type), repr(value))
  501. self._raiseCursorError(DataError, _message)
  502. # Replace params with ? or NULL
  503. if parameter_replacements:
  504. operation = operation % tuple(parameter_replacements)
  505. # Django will pass down many '%%' values. Need to convert these back to
  506. # a single '%'. This will break raw SQL that includes '%%' as part of an
  507. # inlined value. Those queries should use params.
  508. operation = operation.replace('%%', '%')
  509. self.cmd.CommandText = operation
  510. self._execute_command()
  511. def executemany(self, operation, seq_of_parameters):
  512. """Execute the given command against all parameter sequences or mappings given in seq_of_parameters."""
  513. self.messages = list()
  514. total_recordcount = 0
  515. for params in seq_of_parameters:
  516. self.execute(operation, params)
  517. if self.rowcount == -1:
  518. total_recordcount = -1
  519. if total_recordcount != -1:
  520. total_recordcount += self.rowcount
  521. self.rowcount = total_recordcount
  522. def _fetch(self, rows=None):
  523. """Fetch rows from the current recordset.
  524. rows -- Number of rows to fetch, or None (default) to fetch all rows.
  525. """
  526. if self.connection is None or self.rs is None:
  527. self._raiseCursorError(FetchFailedError, 'Attempting to fetch from a closed connection or empty record set')
  528. return
  529. if self.rs.State == adStateClosed or self.rs.BOF or self.rs.EOF:
  530. if rows == 1: # fetchone returns None
  531. return None
  532. else: # fetchall and fetchmany return empty lists
  533. return list()
  534. if rows:
  535. ado_results = self.rs.GetRows(rows)
  536. else:
  537. ado_results = self.rs.GetRows()
  538. py_columns = list()
  539. column_types = [column_desc[1] for column_desc in self.description]
  540. for ado_type, column in zip(column_types, ado_results):
  541. py_columns.append([_convert_to_python(cell, ado_type) for cell in column])
  542. return tuple(zip(*py_columns))
  543. def fetchone(self):
  544. """
  545. Fetch the next row of a query result set, returning a single sequence,
  546. or None when no more data is available.
  547. An Error (or subclass) exception is raised if the previous call to executeXXX()
  548. did not produce any result set or no call was issued yet.
  549. """
  550. self.messages = list()
  551. result = self._fetch(1)
  552. if result: # return record (not list of records)
  553. return result[0]
  554. return None
  555. def fetchmany(self, size=None):
  556. """
  557. Fetch the next set of rows of a query result, returning a list of
  558. tuples. An empty sequence is returned when no more rows are available.
  559. """
  560. self.messages = list()
  561. if size is None:
  562. size = self.arraysize
  563. return self._fetch(size)
  564. def fetchall(self):
  565. """Fetch all remaining rows of a query result, returning them as a sequence of sequences."""
  566. self.messages = list()
  567. return self._fetch()
  568. def nextset(self):
  569. """Skip to the next available recordset, discarding any remaining rows from the current recordset.
  570. If there are no more sets, the method returns None. Otherwise, it returns a true
  571. value and subsequent calls to the fetch methods will return rows from the next result set.
  572. """
  573. self.messages = list()
  574. if self.connection is None or self.rs is None:
  575. self._raiseCursorError(Error, None)
  576. return None
  577. recordset = self.rs.NextRecordset()[0]
  578. if recordset is None:
  579. return None
  580. self._description_from_recordset(recordset)
  581. return True
  582. def setinputsizes(self, sizes):
  583. pass
  584. def setoutputsize(self, size, column=None):
  585. pass
  586. # Type specific constructors as required by the DB-API 2 specification.
  587. Date = datetime.date
  588. Time = datetime.time
  589. Timestamp = datetime.datetime
  590. Binary = six.memoryview
  591. def DateFromTicks(ticks):
  592. """Construct an object holding a date value from the given # of ticks."""
  593. return Date(*time.localtime(ticks)[:3])
  594. def TimeFromTicks(ticks):
  595. """Construct an object holding a time value from the given # of ticks."""
  596. return Time(*time.localtime(ticks)[3:6])
  597. def TimestampFromTicks(ticks):
  598. """Construct an object holding a timestamp value from the given # of ticks."""
  599. return Timestamp(*time.localtime(ticks)[:6])
  600. adoIntegerTypes = (adInteger, adSmallInt, adTinyInt, adUnsignedInt, adUnsignedSmallInt, adUnsignedTinyInt, adError)
  601. adoRowIdTypes = (adChapter,)
  602. adoLongTypes = (adBigInt, adUnsignedBigInt, adFileTime)
  603. adoExactNumericTypes = (adDecimal, adNumeric, adVarNumeric, adCurrency)
  604. adoApproximateNumericTypes = (adDouble, adSingle)
  605. adoStringTypes = (adBSTR, adChar, adLongVarChar, adLongVarWChar, adVarChar, adVarWChar, adWChar, adGUID)
  606. adoBinaryTypes = (adBinary, adLongVarBinary, adVarBinary)
  607. adoDateTimeTypes = (adDBTime, adDBTimeStamp, adDate, adDBDate)
  608. # Required DBAPI type specifiers
  609. STRING = _DbType(adoStringTypes)
  610. BINARY = _DbType(adoBinaryTypes)
  611. NUMBER = _DbType((adBoolean,) + adoIntegerTypes + adoLongTypes + adoExactNumericTypes + adoApproximateNumericTypes)
  612. DATETIME = _DbType(adoDateTimeTypes)
  613. # Not very useful for SQL Server, as normal row ids are usually just integers.
  614. ROWID = _DbType(adoRowIdTypes)
  615. # Mapping ADO data types to Python objects.
  616. def _convert_to_python(variant, adType):
  617. if variant is None:
  618. return None
  619. return _variantConversions[adType](variant)
  620. def _cvtDecimal(variant):
  621. return _convertNumberWithCulture(variant, decimal.Decimal)
  622. def _cvtFloat(variant):
  623. return _convertNumberWithCulture(variant, float)
  624. def _convertNumberWithCulture(variant, f):
  625. try:
  626. return f(variant)
  627. except (ValueError, TypeError, decimal.InvalidOperation):
  628. try:
  629. europeVsUS = str(variant).replace(",", ".")
  630. return f(europeVsUS)
  631. except (ValueError, TypeError):
  632. pass
  633. def _cvtComDate(comDate):
  634. import pywintypes
  635. if isinstance(comDate, pywintypes.TimeType):
  636. # Django and everything else expects a datetime.datetime, instead of the
  637. # invalid pathed com subclassed "pywintypes.datetime"
  638. dt = datetime.datetime(
  639. year=comDate.year,
  640. month=comDate.month,
  641. day=comDate.day,
  642. hour=comDate.hour,
  643. minute=comDate.minute,
  644. second=comDate.second,
  645. microsecond=comDate.microsecond
  646. )
  647. elif isinstance(comDate, datetime.datetime):
  648. dt = comDate
  649. else:
  650. date_as_float = float(comDate)
  651. day_count = int(date_as_float)
  652. fraction_of_day = abs(date_as_float - day_count)
  653. dt = (datetime.datetime.fromordinal(day_count + _ordinal_1899_12_31) +
  654. datetime.timedelta(milliseconds=fraction_of_day * _milliseconds_per_day))
  655. if getattr(settings, 'USE_TZ', False):
  656. dt = dt.replace(tzinfo=timezone.utc)
  657. return dt
  658. _variantConversions = MultiMap(
  659. {
  660. adoDateTimeTypes: _cvtComDate,
  661. adoExactNumericTypes: _cvtDecimal,
  662. adoApproximateNumericTypes: _cvtFloat,
  663. (adBoolean,): bool,
  664. adoLongTypes + adoRowIdTypes: int if six.PY3 else long,
  665. adoIntegerTypes: int,
  666. adoBinaryTypes: six.memoryview,
  667. },
  668. lambda x: x)
  669. # Mapping Python data types to ADO type codes
  670. def _ado_type(data):
  671. if isinstance(data, six.string_types):
  672. return adVarWChar
  673. return _map_to_adotype[type(data)]
  674. _map_to_adotype = {
  675. six.memoryview: adBinary,
  676. float: adDouble,
  677. int: adInteger if six.PY2 else adBigInt,
  678. bool: adBoolean,
  679. decimal.Decimal: adDecimal,
  680. datetime.date: adDate,
  681. datetime.datetime: adDate,
  682. datetime.time: adDate,
  683. uuid.UUID: adGUID,
  684. }
  685. if six.PY3:
  686. _map_to_adotype[bytes] = adBinary
  687. if six.PY2:
  688. _map_to_adotype[long] = adBigInt