django-mssql / sqlserver_ado / dbapi.py

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
"""A python DB API 2.0 (PEP 249) SQL Server for Django

Forked from: adodbapi v2.1 (merged forward to v2.4)
Copyright (C) 2002 Henrik Ekelund, version 2.1 by Vernon Cole
* http://sourceforge.net/projects/pywin32
* http://sourceforge.net/projects/adodbapi

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

* Version 2.1D by Adam Vandenberg, forked for Django backend use.
  This module is a db-api 2 interface for ADO, but is Django & SQL Server.
  It won't work against other ADO sources (like Access.)

DB-API 2.0 specification: http://www.python.org/dev/peps/pep-0249/

This module source should run correctly in CPython versions 2.3 and later,
or IronPython version 2.6 and later,
or, after running through 2to3.py, CPython 3.0 or later.
"""
# N.O.T.E.:...
# if you have been using an older version of adodbapi and are getting errors because
# numeric and monitary data columns are now returned as Decimal data,
# try adding the following line to get that data as strings: ...
#adodbapi.variantConversions[adodbapi.adoCurrencyTypes]=adodbapi.cvtString # get currency as strings
#adodbapi.variantConversions[adodbapi.adoExactNumericTypes]=adodbapi.cvtString

import sys
import time
#import traceback
import datetime

try:
    import decimal
except ImportError:  #perhaps running Cpython 2.3 
    import win32com.decimal_23 as decimal
# or # from django.utils import _decimal as decimal

from django.conf import settings
from django.db.utils import IntegrityError as DjangoIntegrityError, \
    DatabaseError as DjangoDatabaseError

try:
    from django.utils import timezone
except ImportError:
    # timezone added in Django 1.4
    timezone = None

onIronPython = sys.platform == 'cli'
if not onIronPython:
    try:
        import win32com.client
    except ImportError:
        import warnings
        warnings.warn("pywin32 package required but not found.",ImportWarning)

# --- define objects to smooth out IronPython <-> CPython differences    
if onIronPython: 
    from System import Activator, Type, DBNull, DateTime, Array, Byte
    from System import Decimal as SystemDecimal
    from clr import Reference
    def Dispatch(dispatch):
        type = Type.GetTypeFromProgID(dispatch)
        return Activator.CreateInstance(type)
    def getIndexedValue(obj,index):
        return obj.Item[index]
else: #pywin32
    try:
        import pythoncom
        import pywintypes
        pythoncom.__future_currency__ = True
        def Dispatch(dispatch):
            return win32com.client.Dispatch(dispatch)
    except:
        pass #warning already given above
    def getIndexedValue(obj,index):
        return obj(index) 
    DBNull = type(None)
    DateTime = type(NotImplemented) #impossible value 

import ado_consts as adc  #internal to this module, use the new sub_module

# --- define objects to smooth out Python3000 <-> Python 2.x differences
unicodeType = unicode  #this line will be altered by 2to3.py to '= str'
longType = long        #this line will be altered by 2to3.py to '= int'
if sys.version[0] >= '3': #python 3.x
    StringTypes = str
    makeByteBuffer = bytes
    memoryViewType = memoryview
    _BaseException = Exception
else:                   #python 2.x
    from exceptions import StandardError as _BaseException
    memoryViewType = type(buffer(''))
    makeByteBuffer = buffer
    try:                #jdhardy -- handle bytes under IronPython
        bytes
    except NameError:
        bytes = str
    StringTypes = (str,unicode)  # will be messed up by 2to3 but never used
    from ado_consts import *  #define old way of getting constants (for callers)


# Used for COM to Python date conversions.
_milliseconds_per_day = 24*60*60*1000

class MultiMap(object):
    def __init__(self, mapping, default=None):
        """Defines a mapping with multiple keys per value.

        mapping is a dict of: tuple(key, key, key...) => value
        """
        self.storage = dict()
        self.default = default

        for keys, value in mapping.iteritems():
            for key in keys:
                self.storage[key] = value

    def __getitem__(self, key):
        return self.storage.get(key, self.default)


def standardErrorHandler(connection, cursor, errorclass, errorvalue):
    err = (errorclass, errorvalue)
    connection.messages.append(err)
    if cursor is not None:
        cursor.messages.append(err)
    raise errorclass(errorvalue)

# -----     Time converters ----------------------------------------------
class TimeConverter(object):  # this is a generic time converter skeleton
    def __init__(self):       # the details will be filled in by instances
        self._ordinal_1899_12_31=datetime.date(1899,12,31).toordinal()-1
        #Use self.types to compare if an input parameter is a datetime 
        self.types = (type(self.Date(2000,1,1)),
                      type(self.Time(12,1,1)),
                      type(self.Timestamp(2000,1,1,12,1,1)))
    def COMDate(self,obj):
        'Returns a ComDate from a datetime in inputformat'
        raise NotImplementedError   #"Abstract class"
    def DateObjectFromCOMDate(self,comDate):
        'Returns an object of the wanted type from a ComDate'
        raise NotImplementedError   #"Abstract class"
    def Date(self,year,month,day):
        "This function constructs an object holding a date value. "
        raise NotImplementedError   #"Abstract class"
    def Time(self,hour,minute,second):
        "This function constructs an object holding a time value. "
        raise NotImplementedError   #"Abstract class"
    def Timestamp(self,year,month,day,hour,minute,second):
        "This function constructs an object holding a time stamp value. "
        raise NotImplementedError   #"Abstract class"
    def DateObjectToIsoFormatString(self,obj):
        "This function should return a string in the format 'YYYY-MM-dd HH:MM:SS:ms' (ms optional) "
        raise NotImplementedError   #"Abstract class"        

# -- Optional: if mx extensions are installed you may use mxDateTime ----
try:
    import mx.DateTime
    mxDateTime = True
# to change the default time converter, use something like:
# adodbapi.adodbapi.dateconverter = adodbapi.mxDateTimeConverter()
except:
    mxDateTime = False
if mxDateTime:
    class mxDateTimeConverter(TimeConverter): # used optionally if
        def COMDate(self,obj):
            return obj.COMDate()
        def DateObjectFromCOMDate(self,comDate):
            return mx.DateTime.DateTimeFromCOMDate(comDate)
        def DateObjectFromCOMDate(self,comDate):
            return mx.DateTime.DateTimeFromCOMDate(comDate)
        def Date(self,year,month,day):
            return mx.DateTime.Date(year,month,day)
        def Time(self,hour,minute,second):
            return mx.DateTime.Time(hour,minute,second)
        def Timestamp(self,year,month,day,hour,minute,second):
            return mx.DateTime.Timestamp(year,month,day,hour,minute,second)
        def DateObjectToIsoFormatString(self,obj):
            return obj.Format('%Y-%m-%d %H:%M:%S')
else:
    class mxDateTimeConverter(TimeConverter):
        pass    # if no mx is installed

class pythonDateTimeConverter(TimeConverter): # standard since Python 2.3
    def __init__(self):       
        TimeConverter.__init__(self)
    def COMDate(self,obj):
        tt=obj.timetuple()
        try:
            ms=obj.microsecond
        except:
            ms=0
        YMDHMSmsTuple=tuple(tt[:6] + (ms,))
        return self.COMDateFromTuple(YMDHMSmsTuple)
    def DateObjectFromCOMDate(self,comDate):
        if isinstance(comDate,datetime.datetime):
            return comDate.replace(tzinfo=None) # make non aware
        elif isinstance(comDate,DateTime):
            fComDate = comDate.ToOADate() # ironPython clr Date/Time
        else:
            fComDate=float(comDate) #ComDate is number of days since 1899-12-31
        integerPart = int(fComDate)
        floatpart=fComDate-integerPart
        ##if floatpart == 0.0:
        ##    return datetime.date.fromordinal(integerPart + self._ordinal_1899_12_31)
        dte=datetime.datetime.fromordinal(integerPart + self._ordinal_1899_12_31) \
           + datetime.timedelta(milliseconds=floatpart*86400000)
               # millisecondsperday=86400000 # 24*60*60*1000
        return dte
    def Date(self,year,month,day):
        return datetime.date(year,month,day)
    def Time(self,hour,minute,second):
        return datetime.time(hour,minute,second)
    def Timestamp(self,year,month,day,hour,minute,second):
        return datetime.datetime(year,month,day,hour,minute,second)
    def COMDateFromTuple(self,YMDHMSmsTuple):
        d = datetime.date(YMDHMSmsTuple[0],YMDHMSmsTuple[1],YMDHMSmsTuple[2])
        integerPart = d.toordinal() - self._ordinal_1899_12_31
        ms = ((YMDHMSmsTuple[3]*60 \
               +YMDHMSmsTuple[4])*60 \
              +YMDHMSmsTuple[5])*1000 \
           +YMDHMSmsTuple[6]
        fractPart = ms / 86400000.0
        return integerPart + fractPart
    def DateObjectToIsoFormatString(self,obj):
        if isinstance(obj,datetime.datetime):
            s = obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj,datetime.date): #exact midnight
            s = obj.strftime('%Y-%m-%d 00:00:00')
        else:
            try:  #usually datetime.datetime
                s = obj.isoformat()
            except:  #but may be mxdatetime
                s = obj.Format('%Y-%m-%d %H:%M:%S')
        return s

class pythonTimeConverter(TimeConverter): # the old, ?nix type date and time 
    def __init__(self): #caution: this Class gets confised by timezones and DST
        TimeConverter.__init__(self)
    def COMDate(self,timeobj):
        return self.COMDateFromTuple(timeobj)
    def COMDateFromTuple(self,t):
        d = datetime.date(t[0],t[1],t[2])
        integerPart = d.toordinal() - self._ordinal_1899_12_31
        sec = (t[3]*60 + t[4])*60 + t[5]
        fractPart = sec / 86400.0
        return integerPart + fractPart
    def DateObjectFromCOMDate(self,comDate):
        'Returns ticks since 1970'
        if isinstance(comDate,datetime.datetime):
            return comDate.timetuple()
        elif isinstance(comDate,DateTime): # ironPython clr date/time
            fcomDate = comDate.ToOADate()
        else:
            fcomDate = float(comDate)
        secondsperday=86400 # 24*60*60
        #ComDate is number of days since 1899-12-31, gmtime epoch is 1970-1-1 = 25569 days
        t=time.gmtime(secondsperday*(fcomDate-25569.0))
        return t  #year,month,day,hour,minute,second,weekday,julianday,daylightsaving=t
    def Date(self,year,month,day):
        return self.Timestamp(year,month,day,0,0,0)
    def Time(self,hour,minute,second):
        return time.gmtime((hour*60+minute)*60 + second)
    def Timestamp(self,year,month,day,hour,minute,second):
        return time.localtime(time.mktime((year,month,day,hour,minute,second,0,0,-1)))
    def DateObjectToIsoFormatString(self,obj):
        try:
            s = time.strftime('%Y-%m-%d %H:%M:%S',obj)
        except:
            s = obj.strftime('%Y-%m-%d')
        return s

dateconverter = pythonDateTimeConverter() # default
# -----------------------------------------------------------
# ------- Error handlers ------
# Note: _BaseException is defined differently between Python 2.x and 3.x
class Error(_BaseException):
    pass   #Exception that is the base class of all other error
            #exceptions. You can use this to catch all errors with one
            #single 'except' statement. Warnings are not considered
            #errors and thus should not use this class as base. It must
            #be a subclass of the Python StandardError (defined in the
            #module exceptions).
class Warning(_BaseException):
    pass
class InterfaceError(Error):
    pass
class DatabaseError(Error):
    pass
class InternalError(DatabaseError):
    pass
class OperationalError(DatabaseError):
    pass
class ProgrammingError(DatabaseError):
    pass
class IntegrityError(DatabaseError, DjangoIntegrityError):
    pass
class DataError(DatabaseError):
    pass
class NotSupportedError(DatabaseError):
    pass
verbose = False

# -----------------  The .connect method -----------------
def connect(connection_string, timeout=30, use_transactions=None):
    """Connect to a database.

    connection_string -- An ADODB formatted connection string, see:
         * http://www.connectionstrings.com
         * http://www.asp101.com/articles/john/connstring/default.asp
    timeout -- A command timeout value, in seconds (default 30 seconds)
    """
    try:
        if not onIronPython:
            pythoncom.CoInitialize()             #v2.1 Paj
        c=Dispatch('ADODB.Connection') #connect _after_ CoIninialize v2.1.1 adamvan
    except:
        raise InterfaceError #Probably COM Error
    try:
        c.CommandTimeout = timeout
        c.ConnectionString = connection_string
        c.Open()
        if use_transactions is None:
            useTransactions = _use_transactions(c)
        else:
            useTransactions = use_transactions
        return Connection(c, useTransactions)
    except (Exception), e:
        raise OperationalError(e, "Error opening connection: " + connection_string)

def _use_transactions(c):
    """Return True if the given ADODB.Connection supports transactions."""
    for prop in c.Properties:
        if prop.Name == 'Transaction DDL':
            return prop.Value > 0
    return False
# ------ DB API required module attributes ---------------------
apilevel='2.0' #String constant stating the supported DB API level.

threadsafety=1 
# Integer constant stating the level of thread safety the interface supports,
# 1 = Threads may share the module, but not connections. 
# TODO: Have not tried this, maybe it is better than 1?
## 
## Possible values are:
##0 = Threads may not share the module. 
##1 = Threads may share the module, but not connections. 
##2 = Threads may share the module and connections. 
##3 = Threads may share the module, connections and cursors. 

paramstyle='format' # the default parameter style
# the API defines this as a constant:
#String constant stating the type of parameter marker formatting expected by the interface. 
# -- but as an extension, adodbapi will allow the programmer to change paramstyles
# by making the paramstyle also an attribute of the connection,
# and allowing the programmer to one of the permitted values:
# 'qmark' = Question mark style, e.g. '...WHERE name=?'
# 'named' = Named style, e.g. '...WHERE name=:name'
# 'format' = ANSI C printf format codes, e.g. '...WHERE name=%s'
_accepted_paramstyles = ('qmark','named','format')
# so you could use something like:
#   myConnection.paramstyle = 'named'
# The programmer may also change the default.
#   For example, if I were using django, I would say:
#     import adodbapi as Database
#     Database.adodbapi.paramstyle = 'format'

# ------- other module level defaults --------
defaultIsolationLevel = adc.adXactReadCommitted
#  Set defaultIsolationLevel on module level before creating the connection.
#   For example:
#   import adodbapi, ado_consts
#   adodbapi.adodbapi.defaultIsolationLevel=ado_consts.adXactBrowse"
#
#  Set defaultCursorLocation on module level before creating the connection.
# It may be one of the "adUse..." consts.
defaultCursorLocation = adUseServer

# ----- handy constansts --------
# Used for COM to Python date conversions.
_ordinal_1899_12_31 = datetime.date(1899,12,31).toordinal()-1

def format_parameters(parameters, show_value=False):
    """Format a collection of ADO Command Parameters.

    Used by error reporting in _execute_command.
    """
    if show_value:
        desc = [
            "Name: %s, Dir.: %s, Type: %s, Size: %s, Value: \"%s\", Precision: %s, NumericScale: %s" %\
            (p.Name, adc.directions[p.Direction], adc.adTypeNames.get(p.Type, str(p.Type)+' (unknown type)'), p.Size, p.Value, p.Precision, p.NumericScale)
            for p in parameters ]
    else:
        desc = [
            "Name: %s, Dir.: %s, Type: %s, Size: %s, Precision: %s, NumericScale: %s" %\
            (p.Name, adc.directions[p.Direction], adTypeNames.get(p.Type, str(p.Type)+' (unknown type)'), p.Size, p.Precision, p.NumericScale)
            for p in parameters ]

    return '[' + '\n'.join(desc) + ']'

def _configure_parameter(p, value):
    """Configure the given ADO Parameter 'p' with the Python 'value'."""
    if p.Direction not in [adc.adParamInput, adc.adParamInputOutput, adc.adParamUnknown]:
        return

    if isinstance(value,StringTypes):            #v2.1 Jevon
        p.Value = value
        p.Size = len(value)

    elif isinstance(value, memoryViewType):
        p.Size = len(value)
        p.AppendChunk(value)

    elif isinstance(value, decimal.Decimal):
        p.Value = value
        exponent = value.as_tuple()[2]
        digit_count = len(value.as_tuple()[1])
        
        if exponent == 0:
            p.NumericScale = 0
            p.Precision =  digit_count
        elif exponent < 0:
            p.NumericScale = -exponent
            p.Precision = digit_count
            if p.Precision < p.NumericScale:
                p.Precision = p.NumericScale            
        elif exponent > 0:
            p.NumericScale = 0
            p.Precision = digit_count + exponent

    elif isinstance(value, datetime.time):
            p.Value = datetime.datetime(1,1,1, value.hour, value.minute, value.second)

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

    else:
        # For any other type, set the value and let pythoncom do the right thing.
        p.Value = value

    # Use -1 instead of 0 for empty strings and buffers
    if p.Size == 0:
        p.Size = -1

VERSION_SQL2005 = 9
VERSION_SQL2008 = 10

# # # # # ----- the Class that defines a connection ----- # # # # # 
class Connection(object):
    # include connection attributes required by api definition.
    Warning = Warning
    Error = Error
    InterfaceError = InterfaceError
    DataError = DataError
    DatabaseError = DatabaseError
    OperationalError = OperationalError
    IntegrityError = IntegrityError
    InternalError = InternalError
    NotSupportedError = NotSupportedError
    ProgrammingError = ProgrammingError

    def __init__(self, adoConn, useTransactions=False):
        self.adoConn = adoConn
        self.paramstyle = paramstyle
        self.supportsTransactions = useTransactions
        self.adoConn.CursorLocation = defaultCursorLocation #v2.1 Rose
        if self.supportsTransactions:
            self.adoConn.IsolationLevel = defaultIsolationLevel
            self.adoConn.BeginTrans() #Disables autocommit per DBPAI
        self.errorhandler = None
        self.messages = []
        self.adoConnProperties = dict([(x.Name, x.Value) for x in self.adoConn.Properties])


    @property
    def is_sql2005(self):
        v = self.adoConnProperties.get('DBMS Version', '')
        return v.startswith(unicode(VERSION_SQL2005))
    
    @property
    def is_sql2008(self):
        v = self.adoConnProperties.get('DBMS Version', '')
        return v.startswith(unicode(VERSION_SQL2008))

    def _raiseConnectionError(self, errorclass, errorvalue):
        eh = self.errorhandler
        if eh is None:
            eh = standardErrorHandler
        eh(self, None, errorclass, errorvalue)

    def _closeAdoConnection(self):                  #all v2.1 Rose
        """close the underlying ADO Connection object,
           rolling it back first if it supports transactions."""
        if self.supportsTransactions:
            self.adoConn.RollbackTrans()
        self.adoConn.Close()

    def close(self):
        """Close the connection now (rather than whenever __del__ is called).

        The connection will be unusable from this point forward;
        an Error (or subclass) exception will be raised if any operation is attempted with the connection.
        The same applies to all cursor objects trying to use the connection. 
        """
        self.messages = []

        try:
            self._closeAdoConnection()                      #v2.1 Rose
        except (Exception), e:
            self._raiseConnectionError(InternalError, e)
        if not onIronPython:
            pythoncom.CoUninitialize()                             #v2.1 Paj

    def commit(self):
        """Commit any pending transaction to the database.

        Note that if the database supports an auto-commit feature,
        this must be initially off. An interface method may be provided to turn it back on. 
        Database modules that do not support transactions should implement this method with void functionality. 
        """
        self.messages = []
        if not self.supportsTransactions:
            return

        try:
            self.adoConn.CommitTrans()
            if not(self.adoConn.Attributes & adc.adXactCommitRetaining):
                #If attributes has adXactCommitRetaining it performs retaining commits that is,
                #calling CommitTrans automatically starts a new transaction. Not all providers support this.
                #If not, we will have to start a new transaction by this command:
                self.adoConn.BeginTrans()
        except (Exception), e:
            self._raiseConnectionError(Error, e)

    def rollback(self):
        """In case a database does provide transactions this method causes the the database to roll back to
        the start of any pending transaction. Closing a connection without committing the changes first will
        cause an implicit rollback to be performed.

        If the database does not support the functionality required by the method, the interface should
        throw an exception in case the method is used. 
        The preferred approach is to not implement the method and thus have Python generate
        an AttributeError in case the method is requested. This allows the programmer to check for database
        capabilities using the standard hasattr() function. 

        For some dynamically configured interfaces it may not be appropriate to require dynamically making
        the method available. These interfaces should then raise a NotSupportedError to indicate the
        non-ability to perform the roll back when the method is invoked. 
        """
        self.messages = []
        if not self.supportsTransactions:
            self._raiseConnectionError(NotSupportedError, None)
        self.adoConn.RollbackTrans()
        if not(self.adoConn.Attributes & adc.adXactAbortRetaining):
            #If attributes has adXactAbortRetaining it performs retaining aborts that is,
            #calling RollbackTrans automatically starts a new transaction. Not all providers support this.
            #If not, we will have to start a new transaction by this command:
            self.adoConn.BeginTrans()

        #TODO: Could implement the prefered method by havins two classes,
        # one with trans and one without, and make the connect function choose which one.
        # the one without transactions should not implement rollback

    def cursor(self):
        "Return a new Cursor Object using the connection."
        self.messages = []
        return Cursor(self)

    def printADOerrors(self):
        j=self.adoConn.Errors.Count
        if j:
            print 'ADO Errors:(%i)' % j
        for e in self.adoConn.Errors:
            print 'Description: %s' % e.Description
            print 'Error: %s %s ' % (e.Number, adoErrors.get(e.Number, "unknown"))
            if e.Number == ado_error_TIMEOUT:
                print 'Timeout Error: Try using adodbpi.connect(constr,timeout=Nseconds)'
            print 'Source: %s' % e.Source
            print 'NativeError: %s' % e.NativeError
            print 'SQL State: %s' % e.SQLState
            
    def _suggest_error_class(self):
        """Introspect the current ADO Errors and determine an appropriate error class.
        
        Error.SQLState is a SQL-defined error condition, per the SQL specification:
        http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt
        
        The 23000 class of errors are integrity errors.
        Error 40002 is a transactional integrity error.
        """
        if self.adoConn is not None:
            for e in self.adoConn.Errors:
                state = str(e.SQLState)
                if state.startswith('23') or state=='40002':
                    return IntegrityError
        return DatabaseError

    def __del__(self):
        try:
            self._closeAdoConnection()                  #v2.1 Rose
        except:
            pass
        self.adoConn = None

    def __setattr__(self,name,value):
        if name == 'paramstyle':
            if value not in _accepted_paramstyles:
                self._raiseConnectionError(NotSupportedError,
                                           'paramstyle="'+value+'" not in:'+repr(_accepted_paramstyles))
        object.__setattr__(self, name, value)

# # # # # classes to emulate the result of cursor.fetchxxx() as a sequence of sequences # # # # #
class _SQLrow(object): # a single database row
    # class to emulate a sequence, so that a column may be retrieved by either number or name
    def __init__(self,rows,index): # "rows" is an _SQLrows object, index is which row
        # note: self.__setattr__ is disabled to prevent users from trying to store in a row 
        object.__setattr__(self,'rows',rows) # parent 'fetch' container object
        object.__setattr__(self,'index',index) # row number within parent
    def __getattr__(self,name): # used for row.columnName type of value access
        return self._getValue(self.rows.columnNames[name])
    def __setattr__(self,name,value):
        raise NotSupportedError('Cannot assign value to SQL record column')
    def _getValue(self,key):  # key must be an integer
        if onIronPython: # retrieve from two-dimensional array
            v = self.rows.ado_results[key,self.index]
        else: # pywin32 - retrieve from tuple of tuples
            v = self.rows.ado_results[key][self.index]
        return _convert_to_python(v,self.rows.converters[key])
    def __len__(self):
        return len(self.rows.converters)
    def __getitem__(self,key):       # used for row[key] type of value access
        if isinstance(key,int):       # normal row[1] designation
            try:
                return self._getValue(key)
            except IndexError:
                raise
        if isinstance(key, slice):
            indices = key.indices(len(self.rows.converters))
            vl = [self._getValue(i) for i in range(*indices)]
            return tuple(vl)
        try:
            return self._getValue(self.rows.columnNames[key])  # extension row[columnName] designation
        except (KeyError,TypeError):
            er, st, tr = sys.exc_info()
            raise er,'No such key as "%s" in %s'%(repr(key),self.__repr__())
    def __iter__(self):
        return iter(self.__next__())
    def __next__(self):
        for n in range(len(self.rows.converters)):
             yield self._getValue(n)
    def __repr__(self): # create a human readable representation
        try: #python 2.4 and later
            taglist = sorted(self.rows.columnNames.items(),key=lambda x:x[1])
        except NameError: # no such function as "sorted" on 2.3
            taglist = list(self.rows.columnNames.iteritems())
            taglist.sort(lambda x, y: cmp(x[1], y[1])) #deprecated on 3.0
        s = "<SQLrow={"
        for name, i in taglist:
            s += name + ':' + repr(self._getValue(i)) + ', '
        return s[:-2] + '}>'
    def __str__(self): # create a pretty human readable representation
        return str(tuple([str(self._getValue(i)) for i in range(len(self.rows.converters))]))
# # # #
class _SQLrows(object):
    # class to emulate a sequence for multiple rows using a container object
    def __init__(self,ado_results,numberOfRows,cursor):
        self.ado_results = ado_results # raw result of SQL get
        self.numberOfRows = numberOfRows
        self.converters = []  # convertion function for each column
        self.columnNames = {} # names of columns {name:number,...}
        for i,desc in enumerate(cursor.description):
            self.columnNames[desc[0]] = i
            self.converters.append(variantConversions[desc[1]]) # default convertion function
    def __len__(self):
         return self.numberOfRows
    def __getitem__(self,item):     # used for row or row,column access
        if isinstance(item, slice): # will return a tuple of row objects 
            indices = item.indices(self.numberOfRows)
            l = [_SQLrow(self,k) for k in range(*indices)]
            return tuple(l) #no generator expressions in Python 2.3
        elif isinstance(item, tuple) and len(item)==2:
            # d = some_rowsObject[i,j] will return a datum from a two-dimension address
            i,j = item
            if not isinstance(j,int):
                try:
                    j = self.columnNames[j] # convert named column to numeric
                except KeyError:
                    raise KeyError, 'adodbapi: no such column name as "%s"'%repr(j)
            if onIronPython: # retrieve from two-dimensional array
                v = self.ado_results[j,i]
            else: # pywin32 - retrieve from tuple of tuples
                v = self.ado_results[j][i]
            return _convert_to_python(v,self.converters[j])
        else:
            row = _SQLrow(self,item) # new row descriptor
            return row
    def __iter__(self):
        return iter(self.__next__())
    def __next__(self):
        for n in range(self.numberOfRows):
            row = _SQLrow(self,n)
            yield row
# # # # #
def _convert_to_python(variant, function): # convert DB value into Python value
    if isinstance(variant,DBNull):
        return None
    return function(variant)
# # # # # ----- the Class that defines a cursor ----- # # # # #
class Cursor(object):
## ** api required attributes:
## description...
##    This read-only attribute is a sequence of 7-item sequences.
##    Each of these sequences contains information describing one result column:
##        (name, type_code, display_size, internal_size, precision, scale, null_ok).
##    This attribute will be None for operations that do not return rows or if the
##    cursor has not had an operation invoked via the executeXXX() method yet.
##    The type_code can be interpreted by comparing it to the Type Objects specified in the section below.
## rowcount...
##    This read-only attribute specifies the number of rows that the last executeXXX() produced
##    (for DQL statements like select) or affected (for DML statements like update or insert). 
##    The attribute is -1 in case no executeXXX() has been performed on the cursor or
##    the rowcount of the last operation is not determinable by the interface.[7]
##    NOTE: -- adodbapi returns "-1" by default for all select statements
## arraysize...
##    This read/write attribute specifies the number of rows to fetch at a time with fetchmany().
##    It defaults to 1 meaning to fetch a single row at a time. 
##    Implementations must observe this value with respect to the fetchmany() method,
##    but are free to interact with the database a single row at a time.
##    It may also be used in the implementation of executemany(). 
## ** extension attributes:
## paramstyle...
##   allows the programmer to override the connection's default paramstyle
## errorhandler...
##   allows the programmer to override the connection's default error handler

    def __init__(self, connection):
        self.messages = []
        self.connection = connection
        self.paramstyle = connection.paramstyle  # used for overriding the paramstyle
        self.rs = None  # the ADO recordset for this cursor
        self.description = None
        self.errorhandler = connection.errorhandler
        self.arraysize = 1
        self.rowcount = -1
        self.description = None

    def __iter__(self):                   # [2.1 Zamarev]
        return iter(self.fetchone, None)  # [2.1 Zamarev]
        
    def next(self):
        r = self.fetchone()
        if r:
            return r
        raise StopIteration

    def __enter__(self):
        "Allow database cursors to be used with context managers."
        return self
        
    def __exit__(self, exc_type, exc_val, exc_tb):
        "Allow database cursors to be used with context managers."
        self.close()

    def _raiseCursorError(self, errorclass, errorvalue):
        eh = self.errorhandler
        if eh is None:
            eh = standardErrorHandler
        eh(self.connection, self, errorclass, errorvalue)

    def format_description(self,d):
        """Format db_api description tuple for printing."""
        if isinstance(d,int):
            d = self.description[d]
        desc = "Name= %s, Type= %s, DispSize= %s, IntSize= %s, Precision= %s, Scale= %s NullOK=%s" %\
             (d[0], adc.adTypeNames.get(d[1], str(d[1])+' (unknown type)'),
              d[2], d[3], d[4], d[5], d[6])
        return desc

    def _namedToQmark(self,op,parms):  #convert from 'named' paramstyle to ADO required '?'mark parameters
        if type(parms) != type({}):
            raise ProgrammingError("paramstyle 'named' requires dict parameters")
        outOp = ''
        outparms=[]
        chunks = op.split("'")   #quote all literals -- odd numbered list results are literals.
        inQuotes = False
        for chunk in chunks:
            if inQuotes: # this is inside a quote
                if inQuotes != '' and chunk == '': # double apostrophe to quote one apostrophe
                    outOp = outOp[:-1]  # so take one away
                else:
                    outOp += "'"+chunk+"'" # else pass the quoted string as is.
            else: # is SQL code -- look for a :namedParameter
                while chunk: # some SQL string remains
                    sp = chunk.split(':',1)
                    outOp += sp[0]  # concat the part up to the :
                    if len(sp)>1:
                        chunk = sp[1]
                    else:
                        chunk = None
                    s = ''
                    while chunk:  # there was a parameter - parse it out letter by letter
                        c = chunk[0]
                        if c.isalnum() or c == '_':
                            s += c
                            chunk = chunk[1:]
                        else: #end of parameter
                            break
                    if s:
                        outparms.append(parms[s]) # list the parameters in order
                        outOp += '?'  # put in the Qmark
            inQuotes = not inQuotes
        return (outOp,outparms)

    def _formatToQmark(self,op):  #convert from 'format' paramstyle to ADO required '?'mark parameters
        outOp = ''
        chunks = op.split("'")   #quote all literals -- odd numbered list results are literals.
        inQuotes = False
        for chunk in chunks:
            if inQuotes:
                if outOp != '' and chunk=='': # double apostrophe to quote one apostrophe
                    outOp = outOp[:-1]  # so take one away
                else:
                    outOp += "'"+chunk+"'" # else pass the quoted string as is.
            else: # is SQL code -- look for a %s parameter
                sp = chunk.split('%s')  # make each %s
                outOp += "?".join(sp)   # into ?
            inQuotes =  not inQuotes # every other chunk is a quoted string
        return outOp

    def _makeDescriptionFromRS(self,recordset):
        # Abort if closed or no recordset.
        if (recordset is None) or (recordset.State == adc.adStateClosed):
            self.rs = None
            self.description = None
            return
        self.rs = recordset        #v2.1.1 bkline

        # The ADO documentation hints that obtaining the recordcount may be timeconsuming
        #   "If the Recordset object does not support approximate positioning, this property
        #    may be a significant drain on resources # [ekelund]
        # Therefore, COM will not return rowcount for server-side cursors. [Cole]
        # Client-side cursors (the default since v2.8) will force a static
        # cursor, and rowcount will then be set accurately [Cole]
        nOfFields=recordset.Fields.Count
        desc = []
        for i in range(nOfFields):
            f=getIndexedValue(recordset.Fields,i)
            if not(recordset.EOF or recordset.BOF):
                display_size=f.ActualSize #TODO: Is this the correct defintion according to the DB API 2 Spec ?
            else:
                display_size=None
            null_ok= bool(f.Attributes & adc.adFldMayBeNull)          #v2.1 Cole 
            desc.append((f.Name, f.Type, display_size, f.DefinedSize, f.Precision, f.NumericScale, null_ok))
        self.description = desc

    def close(self):
        """Close the cursor now (rather than whenever __del__ is called).
            The cursor will be unusable from this point forward; an Error (or subclass)
            exception will be raised if any operation is attempted with the cursor.
        """
        self.messages = []
        self.connection = None    #this will make all future method calls on me throw an exception
        if self.rs and self.rs.State != adc.adStateClosed: # rs exists and is open      #v2.1 Rose
            self.rs.Close()                                                         #v2.1 Rose
            self.rs = None  #let go of the recordset so ADO will let it be disposed #v2.1 Rose

    def _new_command(self, command_type=adc.adCmdText):
        self.cmd = None
        self.messages = []

        if self.connection is None:
            self._raiseCursorError(Error, None)
            return
        try:
            self.cmd = Dispatch("ADODB.Command")
            self.cmd.ActiveConnection = self.connection.adoConn
            self.cmd.CommandTimeout = self.connection.adoConn.CommandTimeout  #v2.1 Simons
            self.cmd.CommandType = command_type
        except:
            self._raiseCursorError(DatabaseError, None)

    def _execute_command(self):
        # Sprocs may have an integer return value
        self.return_value = None
        recordset = None; count = -1 #default value
        try:
            # ----- the actual SQL is executed here ---
            if onIronPython:
                ra = Reference[int]()
                recordset = self.cmd.Execute(ra)
                count = ra.Value 
            else: #pywin32
                recordset, count = self.cmd.Execute()
            # ----- ------------------------------- ---
        except (Exception), e:
            _message = ""
            if hasattr(e, 'args'): _message += str(e.args)+"\n"
            _message += "Command:\n%s\nParameters:\n%s" %  (self.cmd.CommandText, format_parameters(self.cmd.Parameters, True))
            klass = self.connection._suggest_error_class()
            self._raiseCursorError(klass, _message)
        try:
            self.rowcount = recordset.RecordCount
        except:
            self.rowcount = count
        self._makeDescriptionFromRS(recordset)

    def callproc(self, procname, parameters=None):
        """Call a stored database procedure with the given name.
        The sequence of parameters must contain one entry for each
        argument that the sproc expects. The result of the
        call is returned as modified copy of the input
        sequence. Input parameters are left untouched, output and
        input/output parameters replaced with possibly new values.

        The sproc may also provide a result set as output,
        which is available through the standard .fetch*() methods.
        Extension: A "return_value" property may be set on the
        cursor if the sproc defines an integer return value.
        """
        self._new_command(adc.adCmdStoredProc)
        self.cmd.CommandText = procname
        self.cmd.Parameters.Refresh()

        try:
            # Return value is 0th ADO parameter. Skip it.
            for i, p in enumerate(tuple(self.cmd.Parameters)[1:]):
                _configure_parameter(p, parameters[i])
        except:
            _message = u'Converting Parameter %s: %s, %s\n' %\
                (p.Name, ado_type_name(p.Type), repr(parameters[i]))

            self._raiseCursorError(DataError, _message)

        self._execute_command()

        p_return_value = self.cmd.Parameters(0)
        self.return_value = _convert_to_python(p_return_value.Value, p_return_value.Type)

        return [_convert_to_python(p.Value, p.Type)
            for p in tuple(self.cmd.Parameters)[1:] ]

    def _reformat_operation(self,operation,parameters):
        if parameters:
            if self.paramstyle == 'format': # convert %s to ?
                operation = self._formatToQmark(operation)
            elif self.paramstyle == 'named':  # convert :name to ?
                operation, parameters = self._namedToQmark(operation,parameters)
        return operation,parameters

    def _buildADOparameterList(self, operation, parameters):
        self.cmd.CommandText = operation
        if parameters != None:
            try: # attempt to use ADO's parameter list
                self.cmd.Parameters.Refresh()
                self.parameters_known = True
            except: # if it blows up
                self.parameters_known = False
            if not self.parameters_known:  #-- build own parameter list
                if verbose:
                    print('error in COM Refresh(), so adodbapi is building a parameter list')
                for i,elem in enumerate(parameters):
                    name='p%i' % i
                    adotype = pyTypeToADOType(elem)
                    p=self.cmd.CreateParameter(name,adotype) # Name, Type, Direction, Size, Value
                    ##if self.cmd.CommandType == adc.adCmdStoredProc:
                    ##    p.Direction=adParamUnknown
                    self.cmd.Parameters.Append(p)  
                if verbose > 2:
                    for i in range(self.cmd.Parameters.Count):
                        P = self.cmd.Parameters[i]
                        print 'adodbapi parameter attributes=', P.Name, P.Type, P.Direction, P.Size

            ##parameter_replacements = list()
            i = 0
            for value in parameters:
                ##if value is None:
                ##    parameter_replacements.append('NULL')
                ##    continue
                ##if isinstance(value, basestring) and value == "":
                ##    parameter_replacements.append("''")
                ##    continue
                ##parameter_replacements.append('?')
                p=getIndexedValue(self.cmd.Parameters,i)
                if p.Direction == adc.adParamReturnValue:
                    i += 1
                    p=getIndexedValue(self.cmd.Parameters,i)
                try:
                    _configure_parameter(p, value, self.parameters_known)
                except (Exception), e:
                    _message = u'Converting Parameter %s: %s, %s <- %s\n' %\
                             (p.Name, adc.ado_type_name(p.Type), p.Value, repr(value))
                    self._raiseCursorError(DataError, _message+'->'+repr(e.args))
                i += 1
        ## # Replace params with ? or NULL
        ##if parameter_replacements:
        ##    operation = operation % tuple(parameter_replacements)

    def execute(self, operation, parameters=None):
        """Prepare and execute a database operation (query or command).

        Return value is not defined.
        """
        self._new_command()

        if parameters is None:
            parameters = list()

        parameter_replacements = list()
        for i, value in enumerate(parameters):
            if value is None:
                parameter_replacements.append('NULL')
                continue
                
            if isinstance(value, basestring) and value == "":
                parameter_replacements.append("''")
                continue

            # Otherwise, process the non-NULL, non-empty string parameter.
            parameter_replacements.append('?')
            try:
                p = self.cmd.CreateParameter('p%i' % i, _ado_type(value))
            except KeyError:
                _message = u'Failed to map python type "%s" to an ADO type' % (value.__class__.__name__,)
                self._raiseCursorError(DataError, _message)
            except:
                _message = u'Creating Parameter p%i, %s' % (i, _ado_type(value))
                self._raiseCursorError(DataError, _message)

            try:
                _configure_parameter(p, value)
                self.cmd.Parameters.Append(p)
            except Exception as e:
                _message = u'Converting Parameter %s: %s, %s\n' %\
                    (p.Name, ado_type_name(p.Type), repr(value))

                self._raiseCursorError(DataError, _message)

        # Replace params with ? or NULL
        if parameter_replacements:
            operation = operation % tuple(parameter_replacements)

        self.cmd.CommandText = operation
        self._execute_command()

    def executemany(self, operation, seq_of_parameters):
        """Prepare a database operation (query or command)
        and then execute it against all parameter sequences or mappings found in the sequence seq_of_parameters.

            Return values are not defined. 
        """
        self.messages = list()
        total_recordcount = 0

        for params in seq_of_parameters:
            self.execute(operation, params)

            if self.rowcount == -1:
                total_recordcount = -1

            if total_recordcount != -1:
                total_recordcount += self.rowcount

        self.rowcount = total_recordcount

    def _fetch(self, limit=None):
        """Fetch rows from the current recordset.

        limit -- Number of rows to fetch, or None (default) to fetch all rows.
        """
        if self.connection is None or self.rs is None:
            self._raiseCursorError(Error, None)
            return

        if self.rs.State == adc.adStateClosed or self.rs.BOF or self.rs.EOF:
            if limit == 1: # fetchone returns None
                return None
            else: # fetchall and fetchmany return empty lists
                return list()

        if limit: # limit number of rows retrieved
            ado_results = self.rs.GetRows(limit)
        else:    # get all rows
            ado_results = self.rs.GetRows()
        if onIronPython:  # result of GetRows is a two-dimension array
            length = len(ado_results)//len(self.description) # length of first dimension
            fetchObject = _SQLrows(ado_results,length,self) # new object to hold the results of the fetch
            return fetchObject
        py_columns = list()
        column_types = [column_desc[1] for column_desc in self.description]
        for ado_type, column in zip(column_types, ado_results):
            py_columns.append( [_convert_to_python(cell, ado_type) for cell in column] )

        return tuple(zip(*py_columns))

    def fetchone(self):
        """ Fetch the next row of a query result set, returning a single sequence,
            or None when no more data is available.

            An Error (or subclass) exception is raised if the previous call to executeXXX()
            did not produce any result set or no call was issued yet.
        """
        self.messages = []
        result = self._fetch(1)
        if result: # return record (not list of records)
            return result[0]
        return None


    def fetchmany(self, size=None):
        """Fetch the next set of rows of a query result, returning a list of tuples. An empty sequence is returned when no more rows are available.

        The number of rows to fetch per call is specified by the parameter.
        If it is not given, the cursor's arraysize determines the number of rows to be fetched.
        The method should try to fetch as many rows as indicated by the size parameter.
        If this is not possible due to the specified number of rows not being available,
        fewer rows may be returned. 

        An Error (or subclass) exception is raised if the previous call to executeXXX()
        did not produce any result set or no call was issued yet. 

        Note there are performance considerations involved with the size parameter.
        For optimal performance, it is usually best to use the arraysize attribute.
        If the size parameter is used, then it is best for it to retain the same value from
        one fetchmany() call to the next. 
        """
        self.messages = []
        if size is None:
            size = self.arraysize
        return self._fetch(size)

    def fetchall(self):
        """Fetch all (remaining) rows of a query result, returning them as a sequence of sequences (e.g. a list of tuples).

            Note that the cursor's arraysize attribute
            can affect the performance of this operation. 
            An Error (or subclass) exception is raised if the previous call to executeXXX()
            did not produce any result set or no call was issued yet. 
        """
        self.messages=[]
        return self._fetch()

    def nextset(self):
        """Skip to the next available recordset, discarding any remaining rows from the current recordset.

        If there are no more sets, the method returns None. Otherwise, it returns a true
        value and subsequent calls to the fetch methods will return rows from the next result set.

            An Error (or subclass) exception is raised if the previous call to executeXXX()
            did not produce any result set or no call was issued yet.
        """
        self.messages = []
        if self.connection is None or self.rs is None:
            self._raiseCursorError(Error, None)
            return None

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

    def setinputsizes(self, sizes):
        pass

    def setoutputsize(self, size, column=None):
        pass

# # # # # ----- Type Objects and Constructors ----- # # # # #
#Many databases need to have the input in a particular format for binding to an operation's input parameters.
#For example, if an input is destined for a DATE column, then it must be bound to the database in a particular
#string format. Similar problems exist for "Row ID" columns or large binary items (e.g. blobs or RAW columns).
#This presents problems for Python since the parameters to the executeXXX() method are untyped.
#When the database module sees a Python string object, it doesn't know if it should be bound as a simple CHAR
#column, as a raw BINARY item, or as a DATE. 
#
#To overcome this problem, a module must provide the constructors defined below to create objects that can
#hold special values. When passed to the cursor methods, the module can then detect the proper type of
#the input parameter and bind it accordingly. 

#A Cursor Object's description attribute returns information about each of the result columns of a query.
#The type_code must compare equal to one of Type Objects defined below. Type Objects may be equal to more than
#one type code (e.g. DATETIME could be equal to the type codes for date, time and timestamp columns;
#see the Implementation Hints below for details). 

def Date(year,month,day):
    "This function constructs an object holding a date value. "
    return dateconverter.Date(year,month,day)

def Time(hour,minute,second):
    "This function constructs an object holding a time value. "
    return dateconverter.Time(hour,minute,second)

def Timestamp(year,month,day,hour,minute,second):
    "This function constructs an object holding a time stamp value. "
    return dateconverter.Timestamp(year,month,day,hour,minute,second)

Binary = buffer

def DateFromTicks(ticks):
    """This function constructs an object holding a date value from the given ticks value
    (number of seconds since the epoch; see the documentation of the standard Python time module for details). """
    return Date(*time.localtime(ticks)[:3])

def TimeFromTicks(ticks):
    """This function constructs an object holding a time value from the given ticks value
    (number of seconds since the epoch; see the documentation of the standard Python time module for details). """
    return Time(*time.localtime(ticks)[3:6])

def TimestampFromTicks(ticks):
    """This function constructs an object holding a time stamp value from the given
    ticks value (number of seconds since the epoch;
    see the documentation of the standard Python time module for details). """
    return Timestamp(*time.localtime(ticks)[:6])

def Binary(aString):
    """This function constructs an object capable of holding a binary (long) string value. """
    return makeByteBuffer(aString)

#SQL NULL values are represented by the Python None singleton on input and output. 

#Note: Usage of Unix ticks for database interfacing can cause troubles because of the limited date range they cover. 

# ------- utilities for converting python data to ADO data
def pyTypeToADOType(d):
    tp=type(d)
    try:
        return typeMap[tp]
    except KeyError:
        if isinstance(d,datetime.datetime):
            return adc.adDBTimeStamp
        if isinstance(d,datetime.time):
            return adc.adDBTime
        if tp in dateconverter.types:
            return adc.adDate
        if isinstance(d,decimal.Decimal):
            return adc.adDecimal
    raise DataError('cannot convert "%s" (type=%s) to ADO'%(repr(d),tp))

class DBAPITypeObject(object):
    def __init__(self,valuesTuple):
        self.values = valuesTuple

    def __eq__(self,other):
        return other in self.values

    def __ne__(self, other):
        return other not in self.values

# define similar types for generic convertion routines
adoIntegerTypes=(adc.adInteger,adc.adSmallInt,adc.adTinyInt,adc.adUnsignedInt,
                 adc.adUnsignedSmallInt,adc.adUnsignedTinyInt,
                 adc.adBoolean,adc.adError) #max 32 bits
adoRowIdTypes=(adc.adChapter,)          #v2.1 Rose
adoLongTypes=(adc.adBigInt,adc.adFileTime,adc.adUnsignedBigInt)
adoExactNumericTypes=(adc.adDecimal,adc.adNumeric,adc.adVarNumeric,adc.adCurrency)      #v2.3 Cole     
adoApproximateNumericTypes=(adc.adDouble,adc.adSingle)                          #v2.1 Cole
adoStringTypes=(adc.adBSTR,adc.adChar,adc.adLongVarChar,adc.adLongVarWChar,
                adc.adVarChar,adc.adVarWChar,adc.adWChar,adc.adGUID)
adoBinaryTypes=(adc.adBinary,adc.adLongVarBinary,adc.adVarBinary)
adoDateTimeTypes=(adc.adDBTime, adc.adDBTimeStamp, adc.adDate, adc.adDBDate)            
adoRemainingTypes=(adc.adEmpty,adc.adIDispatch,adc.adIUnknown,
                   adc.adPropVariant,adc.adArray,adc.adUserDefined,
                   adc.adVariant)

"""This type object is used to describe columns in a database that are string-based (e.g. CHAR). """
STRING   = DBAPITypeObject(adoStringTypes)

"""This type object is used to describe (long) binary columns in a database (e.g. LONG, RAW, BLOBs). """
BINARY   = DBAPITypeObject(adoBinaryTypes)

"""This type object is used to describe numeric columns in a database. """
NUMBER   = DBAPITypeObject(adoIntegerTypes + adoLongTypes + \
                           adoExactNumericTypes + adoApproximateNumericTypes)

"""This type object is used to describe date/time columns in a database. """

DATETIME = DBAPITypeObject(adoDateTimeTypes)
"""This type object is used to describe the "Row ID" column in a database. """
ROWID    = DBAPITypeObject(adoRowIdTypes)

typeMap= { memoryViewType: adc.adBinary,
           float: adc.adNumeric,
           type(None): adc.adEmpty,
           unicode: adc.adBSTR, # this line will be altered by 2to3 to 'str:'
           bool:adc.adBoolean          #v2.1 Cole
           }
if longType != int: #not Python 3
    typeMap[longType] = adc.adBigInt  #works in python 2.x
    typeMap[int] = adc.adInteger
    typeMap[bytes] = adc.adBSTR     # 2.x string type
else:             #python 3.0 integrated integers
    ## Should this differentiote between an int that fits in a long and one that requires 64 bit datatype?
    typeMap[int] = adc.adBigInt

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# functions to convert database values to Python objects

# variant type : function converting variant to Python value
def variantConvertDate(v):
    return dateconverter.DateObjectFromCOMDate(v)

def cvtString(variant):  # use to get old action of adodbapi v1 if desired
    if onIronPython:
        try: 
            return variant.ToString()
        except:
            pass
    return str(variant)

# Mapping ADO data types to Python objects.
def _convert_to_python(variant, adType):
    if variant is None:
        return None
    return _variantConversions[adType](variant)

def cvtDecimal(variant): #better name
    return _convertNumberWithCulture(variant, decimal.Decimal)
_cvtDecimal = cvtDecimal
def cvtNumeric(variant): #older name - don't break old code
    return cvtDecimal(variant)

def cvtFloat(variant):
    return _convertNumberWithCulture(variant, float)
_cvtFloat = cvtFloat

def _convertNumberWithCulture(variant, f):
    try:
        return f(variant)
    except (ValueError,TypeError,decimal.InvalidOperation):
        try:
            europeVsUS = str(variant).replace(",",".")
            return f(europeVsUS)
        except (ValueError,TypeError,decimal.InvalidOperation): pass

def cvtInt(variant):
    return int(variant)

def cvtLong(variant):  # only important in old versions where long and int differ
    return long(variant)

def cvtBuffer(variant):
    return makeByteBuffer(variant)

def cvtUnicode(variant):
    return unicode(variant) # will be altered by 2to3 to 'str(variant)'

def identity(x): return x

class VariantConversionMap(dict): #builds a dictionary from {[list,of,keys]function}
    #useful for defining conversion functions for groups of similar data types.
    def __init__(self, aDict):
        for k, v in aDict.items():
            self[k] = v # we must call __setitem__
    def __setitem__(self, adoType, cvtFn):
        "don't make adoType a string :-)"
        try: # user passed us a tuple, set them individually
            for type in adoType:
                dict.__setitem__(self, type, cvtFn)
        except TypeError: # single value
            dict.__setitem__(self, adoType, cvtFn)
    def __getitem__(self, fromType):
        try:
            return dict.__getitem__(self, fromType)
        except KeyError:
            return identity

##def _convert_to_python(variant, adType):
##    if verbose > 3:
##        print 'Converting type_code=%s, val=%s'%(adType,repr(variant))
##        print 'conversion function=',repr(variantConversions[adType])
##        print '                     output=%s'%repr(variantConversions[adType](variant))
##    if isinstance(variant,DBNull):
##        return None
##    return variantConversions[adType](variant)

#initialize variantConversions dictionary used to convert SQL to Python
# this is the dictionary of default convertion functions, built by the class above.
variantConversions = VariantConversionMap( {
    adoDateTimeTypes : variantConvertDate,
    adoApproximateNumericTypes: cvtFloat,
    adoExactNumericTypes: cvtDecimal, # use to force decimal rather than unicode
    adoLongTypes : cvtLong,
    adoIntegerTypes: cvtInt,
    adoRowIdTypes: cvtInt,
    adoStringTypes: identity,
    adoBinaryTypes: cvtBuffer,
    adoRemainingTypes: identity })

def _cvtComDate(comDate):
    date_as_float = float(comDate)
    day_count = int(date_as_float)
    fraction_of_day = abs(date_as_float - day_count)

    dt = (datetime.datetime.fromordinal(day_count + _ordinal_1899_12_31) +
        datetime.timedelta(milliseconds=fraction_of_day * _milliseconds_per_day))
    
    if timezone and getattr(settings, 'USE_TZ', False):
        dt = dt.replace(tzinfo=timezone.utc)
    return dt

_variantConversions = MultiMap(
    {
        adoDateTimeTypes : _cvtComDate,
        adoExactNumericTypes: _cvtDecimal,
        adoApproximateNumericTypes: _cvtFloat,
        (adBoolean,): bool,
        adoLongTypes+adoRowIdTypes : long,
        adoIntegerTypes: int,
        adoBinaryTypes: buffer, 
    }, 
    lambda x: x)

# Mapping Python data types to ADO type codes
def _ado_type(data):
    if isinstance(data, basestring):
        return adBSTR
    return _map_to_adotype[type(data)]

_map_to_adotype = {
    buffer: adBinary,
    float: adDouble,
    int: adInteger,
    long: adBigInt,
    bool: adBoolean,
    decimal.Decimal: adDecimal,
    datetime.date: adDate,
    datetime.datetime: adDate,
    datetime.time: adDate, 
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.