Skip to content

Commit

Permalink
Revert adodbapi changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Aug 6, 2023
1 parent 337d9cc commit 7510c94
Show file tree
Hide file tree
Showing 14 changed files with 464 additions and 137 deletions.
2 changes: 0 additions & 2 deletions adodbapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
)


# -----------------------------------------------------------
# conversion functions mandated by PEP 249
def Binary(aString):
"""This function constructs an object capable of holding a binary (long) string value."""
return bytes(aString)
Expand Down
124 changes: 90 additions & 34 deletions adodbapi/adodbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,54 +23,86 @@
DB-API 2.0 specification: http://www.python.org/dev/peps/pep-0249/
This module source should run correctly in CPython 3.4 or later.
This module source should run correctly in CPython versions 2.7 and later,
or IronPython version 2.7 and later,
or, after running through 2to3.py, CPython 3.4 or later.
"""

__version__ = "2.6.2.0"
version = "adodbapi v" + __version__

import copy
import decimal
import os
import sys
import weakref

import pythoncom
import pywintypes
from win32com.client import Dispatch

from . import ado_consts as adc, apibase as api, process_connect_string

try:
import pythoncom
import pywintypes
import win32com.client

onWin32 = True
except ImportError:
import warnings

warnings.warn("pywin32 package required for adodbapi.", ImportWarning)
onWin32 = False # assume the worst

__version__ = "3.7.0.0"
version = "adodbapi v" + __version__

try:
verbose = int(os.environ["ADODBAPI_VERBOSE"])
except:
verbose = False
if verbose:
print(version)

# --- define objects to smooth out IronPython <-> CPython differences
onWin32 = False # assume the worst
if api.onIronPython:
from clr import Reference
from System import (
Activator,
Array,
Byte,
DateTime,
DBNull,
Decimal as SystemDecimal,
Type,
)

def Dispatch(dispatch):
type = Type.GetTypeFromProgID(dispatch)
return Activator.CreateInstance(type)

def getIndexedValue(obj, index):
return obj.Item[index]

else: # try pywin32
try:
import pythoncom
import pywintypes
import win32com.client

onWin32 = True

def getIndexedValue(obj, index):
return obj(index)
def Dispatch(dispatch):
return win32com.client.Dispatch(dispatch)

except ImportError:
import warnings

warnings.warn(
"pywin32 package (or IronPython) required for adodbapi.", ImportWarning
)

def getIndexedValue(obj, index):
return obj(index)


from collections.abc import Mapping

# --- define objects to smooth out Python3000 <-> Python 2.x differences
unicodeType = str
longType = int
StringTypes = str
maxint = sys.maxsize


# ----------------- The .connect method -----------------
def make_COM_connecter():
try:
pythoncom.CoInitialize() # v2.1 Paj
if onWin32:
pythoncom.CoInitialize() # v2.1 Paj
c = Dispatch("ADODB.Connection") # connect _after_ CoIninialize v2.1.1 adamvan
except:
raise api.InterfaceError(
Expand Down Expand Up @@ -166,7 +198,7 @@ def _configure_parameter(p, value, adotype, settings_known):
p.Size = len(value)
p.AppendChunk(value)

elif isinstance(value, str): # v2.1 Jevon
elif isinstance(value, StringTypes): # v2.1 Jevon
L = len(value)
if adotype in api.adoStringTypes: # v2.2.1 Cole
if settings_known:
Expand All @@ -178,7 +210,12 @@ def _configure_parameter(p, value, adotype, settings_known):
p.Size = L # v2.1 Jevon

elif isinstance(value, decimal.Decimal):
p.Value = value
if api.onIronPython:
s = str(value)
p.Value = s
p.Size = len(s)
else:
p.Value = value
exponent = value.as_tuple()[2]
digit_count = len(value.as_tuple()[1])
p.Precision = digit_count
Expand All @@ -201,6 +238,10 @@ def _configure_parameter(p, value, adotype, settings_known):
p.Value = s
p.Size = len(s)

elif api.onIronPython and isinstance(value, longType): # Iron Python Long
s = str(value) # feature workaround for IPy 2.0
p.Value = s

elif adotype == adc.adEmpty: # ADO will not let you specify a null column
p.Type = (
adc.adInteger
Expand All @@ -213,7 +254,7 @@ def _configure_parameter(p, value, adotype, settings_known):


# # # # # ----- the Class that defines a connection ----- # # # # #
class Connection:
class Connection(object):
# include connection attributes as class attributes required by api definition.
Warning = api.Warning
Error = api.Error
Expand Down Expand Up @@ -524,7 +565,7 @@ def get_table_names(self):


# # # # # ----- the Class that defines a cursor ----- # # # # #
class Cursor:
class Cursor(object):
## ** api required attributes:
## description...
## This read-only attribute is a sequence of 7-item sequences.
Expand Down Expand Up @@ -612,7 +653,7 @@ def build_column_info(self, recordset):
self.numberOfColumns = 0
return
self.rs = recordset # v2.1.1 bkline
self.recordset_format = api.RS_WIN_32
self.recordset_format = api.RS_ARRAY if api.onIronPython else api.RS_WIN_32
self.numberOfColumns = recordset.Fields.Count
try:
varCon = self.connection.variantConversions
Expand Down Expand Up @@ -749,7 +790,12 @@ def _execute_command(self):
print('Executing command="%s"' % self.commandText)
try:
# ----- the actual SQL is executed here ---
recordset, count = self.cmd.Execute()
if api.onIronPython:
ra = Reference[int]()
recordset = self.cmd.Execute(ra)
count = ra.Value
else: # pywin32
recordset, count = self.cmd.Execute()
# ----- ------------------------------- ---
except Exception as e:
_message = ""
Expand Down Expand Up @@ -1134,11 +1180,21 @@ def nextset(self):
)
return None

try: # [begin 2.1 ekelund]
rsTuple = self.rs.NextRecordset() #
except pywintypes.com_error as exc: # return appropriate error
self._raiseCursorError(api.NotSupportedError, exc.args) # [end 2.1 ekelund]
recordset = rsTuple[0]
if api.onIronPython:
try:
recordset = self.rs.NextRecordset()
except TypeError:
recordset = None
except api.Error as exc:
self._raiseCursorError(api.NotSupportedError, exc.args)
else: # pywin32
try: # [begin 2.1 ekelund]
rsTuple = self.rs.NextRecordset() #
except pywintypes.com_error as exc: # return appropriate error
self._raiseCursorError(
api.NotSupportedError, exc.args
) # [end 2.1 ekelund]
recordset = rsTuple[0]
if recordset is None:
return None
self.build_column_info(recordset)
Expand Down
Loading

0 comments on commit 7510c94

Please sign in to comment.