2323
2424DB-API 2.0 specification: http://www.python.org/dev/peps/pep-0249/
2525
26- This module source should run correctly in CPython 3.4 or later.
26+ This module source should run correctly in CPython versions 2.7 and later,
27+ or IronPython version 2.7 and later,
28+ or, after running through 2to3.py, CPython 3.4 or later.
2729"""
30+
31+ __version__ = "2.6.2.0"
32+ version = "adodbapi v" + __version__
33+
2834import copy
2935import decimal
3036import os
3137import sys
3238import weakref
3339
34- import pythoncom
35- import pywintypes
36- from win32com .client import Dispatch
37-
3840from . import ado_consts as adc , apibase as api , process_connect_string
3941
40- try :
41- import pythoncom
42- import pywintypes
43- import win32com .client
44-
45- onWin32 = True
46- except ImportError :
47- import warnings
48-
49- warnings .warn ("pywin32 package required for adodbapi." , ImportWarning )
50- onWin32 = False # assume the worst
51-
52- __version__ = "3.7.0.0"
53- version = "adodbapi v" + __version__
54-
5542try :
5643 verbose = int (os .environ ["ADODBAPI_VERBOSE" ])
5744except :
5845 verbose = False
5946if verbose :
6047 print (version )
6148
49+ # --- define objects to smooth out IronPython <-> CPython differences
50+ onWin32 = False # assume the worst
51+ if api .onIronPython :
52+ from clr import Reference
53+ from System import (
54+ Activator ,
55+ Array ,
56+ Byte ,
57+ DateTime ,
58+ DBNull ,
59+ Decimal as SystemDecimal ,
60+ Type ,
61+ )
62+
63+ def Dispatch (dispatch ):
64+ type = Type .GetTypeFromProgID (dispatch )
65+ return Activator .CreateInstance (type )
66+
67+ def getIndexedValue (obj , index ):
68+ return obj .Item [index ]
69+
70+ else : # try pywin32
71+ try :
72+ import pythoncom
73+ import pywintypes
74+ import win32com .client
75+
76+ onWin32 = True
6277
63- def getIndexedValue (obj , index ):
64- return obj (index )
78+ def Dispatch (dispatch ):
79+ return win32com .client .Dispatch (dispatch )
80+
81+ except ImportError :
82+ import warnings
83+
84+ warnings .warn (
85+ "pywin32 package (or IronPython) required for adodbapi." , ImportWarning
86+ )
87+
88+ def getIndexedValue (obj , index ):
89+ return obj (index )
6590
6691
6792from collections .abc import Mapping
6893
94+ # --- define objects to smooth out Python3000 <-> Python 2.x differences
95+ unicodeType = str
96+ longType = int
97+ StringTypes = str
98+ maxint = sys .maxsize
99+
69100
70101# ----------------- The .connect method -----------------
71102def make_COM_connecter ():
72103 try :
73- pythoncom .CoInitialize () # v2.1 Paj
104+ if onWin32 :
105+ pythoncom .CoInitialize () # v2.1 Paj
74106 c = Dispatch ("ADODB.Connection" ) # connect _after_ CoIninialize v2.1.1 adamvan
75107 except :
76108 raise api .InterfaceError (
@@ -166,7 +198,7 @@ def _configure_parameter(p, value, adotype, settings_known):
166198 p .Size = len (value )
167199 p .AppendChunk (value )
168200
169- elif isinstance (value , str ): # v2.1 Jevon
201+ elif isinstance (value , StringTypes ): # v2.1 Jevon
170202 L = len (value )
171203 if adotype in api .adoStringTypes : # v2.2.1 Cole
172204 if settings_known :
@@ -178,7 +210,12 @@ def _configure_parameter(p, value, adotype, settings_known):
178210 p .Size = L # v2.1 Jevon
179211
180212 elif isinstance (value , decimal .Decimal ):
181- p .Value = value
213+ if api .onIronPython :
214+ s = str (value )
215+ p .Value = s
216+ p .Size = len (s )
217+ else :
218+ p .Value = value
182219 exponent = value .as_tuple ()[2 ]
183220 digit_count = len (value .as_tuple ()[1 ])
184221 p .Precision = digit_count
@@ -201,6 +238,10 @@ def _configure_parameter(p, value, adotype, settings_known):
201238 p .Value = s
202239 p .Size = len (s )
203240
241+ elif api .onIronPython and isinstance (value , longType ): # Iron Python Long
242+ s = str (value ) # feature workaround for IPy 2.0
243+ p .Value = s
244+
204245 elif adotype == adc .adEmpty : # ADO will not let you specify a null column
205246 p .Type = (
206247 adc .adInteger
@@ -213,7 +254,7 @@ def _configure_parameter(p, value, adotype, settings_known):
213254
214255
215256# # # # # ----- the Class that defines a connection ----- # # # # #
216- class Connection :
257+ class Connection ( object ) :
217258 # include connection attributes as class attributes required by api definition.
218259 Warning = api .Warning
219260 Error = api .Error
@@ -524,7 +565,7 @@ def get_table_names(self):
524565
525566
526567# # # # # ----- the Class that defines a cursor ----- # # # # #
527- class Cursor :
568+ class Cursor ( object ) :
528569 ## ** api required attributes:
529570 ## description...
530571 ## This read-only attribute is a sequence of 7-item sequences.
@@ -612,7 +653,7 @@ def build_column_info(self, recordset):
612653 self .numberOfColumns = 0
613654 return
614655 self .rs = recordset # v2.1.1 bkline
615- self .recordset_format = api .RS_WIN_32
656+ self .recordset_format = api .RS_ARRAY if api . onIronPython else api . RS_WIN_32
616657 self .numberOfColumns = recordset .Fields .Count
617658 try :
618659 varCon = self .connection .variantConversions
@@ -749,7 +790,12 @@ def _execute_command(self):
749790 print ('Executing command="%s"' % self .commandText )
750791 try :
751792 # ----- the actual SQL is executed here ---
752- recordset , count = self .cmd .Execute ()
793+ if api .onIronPython :
794+ ra = Reference [int ]()
795+ recordset = self .cmd .Execute (ra )
796+ count = ra .Value
797+ else : # pywin32
798+ recordset , count = self .cmd .Execute ()
753799 # ----- ------------------------------- ---
754800 except Exception as e :
755801 _message = ""
@@ -1134,11 +1180,21 @@ def nextset(self):
11341180 )
11351181 return None
11361182
1137- try : # [begin 2.1 ekelund]
1138- rsTuple = self .rs .NextRecordset () #
1139- except pywintypes .com_error as exc : # return appropriate error
1140- self ._raiseCursorError (api .NotSupportedError , exc .args ) # [end 2.1 ekelund]
1141- recordset = rsTuple [0 ]
1183+ if api .onIronPython :
1184+ try :
1185+ recordset = self .rs .NextRecordset ()
1186+ except TypeError :
1187+ recordset = None
1188+ except api .Error as exc :
1189+ self ._raiseCursorError (api .NotSupportedError , exc .args )
1190+ else : # pywin32
1191+ try : # [begin 2.1 ekelund]
1192+ rsTuple = self .rs .NextRecordset () #
1193+ except pywintypes .com_error as exc : # return appropriate error
1194+ self ._raiseCursorError (
1195+ api .NotSupportedError , exc .args
1196+ ) # [end 2.1 ekelund]
1197+ recordset = rsTuple [0 ]
11421198 if recordset is None :
11431199 return None
11441200 self .build_column_info (recordset )
0 commit comments