1
1
import datetime
2
- from telnetlib import BINARY
2
+ from decimal import Decimal
3
3
import intersystems_iris .dbapi ._DBAPI as dbapi
4
4
from . import information_schema as ischema
5
5
from sqlalchemy import exc
24
24
from sqlalchemy .types import TIME
25
25
from sqlalchemy .types import NUMERIC
26
26
from sqlalchemy .types import FLOAT
27
+ from sqlalchemy .types import BINARY
27
28
from sqlalchemy .types import VARBINARY
28
29
from sqlalchemy .types import TEXT
29
30
from sqlalchemy .types import SMALLINT
@@ -440,6 +441,14 @@ def order_by_clause(self, select, **kw):
440
441
else :
441
442
return ""
442
443
444
+ def visit_column (self , column , within_columns_clause = False , ** kwargs ):
445
+ text = super ().visit_column (column , within_columns_clause = within_columns_clause , ** kwargs )
446
+ if within_columns_clause :
447
+ return text
448
+ if isinstance (column .type , sqltypes .Text ):
449
+ text = 'CONVERT(VARCHAR, %s)' % (text , )
450
+ return text
451
+
443
452
444
453
class IRISDDLCompiler (sql .compiler .DDLCompiler ):
445
454
"""IRIS syntactic idiosyncrasies"""
@@ -487,12 +496,12 @@ def get_column_specification(self, column, **kwargs):
487
496
literal = self .sql_compiler .render_literal_value (
488
497
comment , sqltypes .String ()
489
498
)
490
- colspec .append ("%% DESCRIPTION " + literal )
499
+ colspec .append ("%DESCRIPTION " + literal )
491
500
492
501
return " " .join (colspec )
493
502
494
503
def post_create_table (self , table ):
495
- return " WITH %% CLASSPARAMETER ALLOWIDENTITYINSERT = 1"
504
+ return " WITH %CLASSPARAMETER ALLOWIDENTITYINSERT = 1"
496
505
497
506
498
507
class IRISTypeCompiler (compiler .GenericTypeCompiler ):
@@ -555,19 +564,47 @@ def process(value):
555
564
return process
556
565
557
566
567
+ class _IRISTimeStamp (sqltypes .DateTime ):
568
+ def bind_processor (self , dialect ):
569
+ def process (value : datetime .datetime ):
570
+ if value is not None :
571
+ # value = int(value.timestamp() * 1000000)
572
+ # value += (2 ** 60) if value > 0 else -(2 ** 61 * 3)
573
+ return value .strftime ('%Y-%m-%d %H:%M:%S.%f' )
574
+ return value
575
+
576
+ return process
577
+
578
+ def result_processor (self , dialect , coltype ):
579
+ def process (value ):
580
+ if isinstance (value , str ):
581
+ if '.' not in value :
582
+ value += '.0'
583
+ return datetime .datetime .strptime (value , '%Y-%m-%d %H:%M:%S.%f' )
584
+ if isinstance (value , int ):
585
+ value -= (2 ** 60 ) if value > 0 else - (2 ** 61 * 3 )
586
+ value = value / 1000000
587
+ value = datetime .datetime .utcfromtimestamp (value )
588
+ return value
589
+
590
+ return process
591
+
592
+
558
593
class _IRISDateTime (sqltypes .DateTime ):
559
594
def bind_processor (self , dialect ):
560
595
def process (value ):
561
596
if value is not None :
562
- return value .strftime ('%Y-%m-%d %H:%M:%S' )
597
+ return value .strftime ('%Y-%m-%d %H:%M:%S.%f ' )
563
598
return value
564
599
565
600
return process
566
601
567
602
def result_processor (self , dialect , coltype ):
568
603
def process (value ):
569
- if value is not None :
570
- return datetime .datetime .strptime (value , '%Y-%m-%d %H:%M:%S' )
604
+ if isinstance (value , str ):
605
+ if '.' not in value :
606
+ value += '.0'
607
+ return datetime .datetime .strptime (value , '%Y-%m-%d %H:%M:%S.%f' )
571
608
return value
572
609
573
610
return process
@@ -577,20 +614,25 @@ class _IRISTime(sqltypes.DateTime):
577
614
def bind_processor (self , dialect ):
578
615
def process (value ):
579
616
if value is not None :
580
- return value .strftime ('%H:%M:%S' )
617
+ return value .strftime ('%H:%M:%S.%f ' )
581
618
return value
582
619
583
620
return process
584
621
585
622
def result_processor (self , dialect , coltype ):
586
623
def process (value ):
587
- if value is not None :
624
+ if isinstance (value , str ):
625
+ if '.' not in value :
626
+ value += '.0'
627
+ return datetime .datetime .strptime (value , '%H:%M:%S.%f' ).time ()
628
+ if isinstance (value , int ) or isinstance (value , Decimal ):
588
629
horolog = value
589
- hour = horolog // 3600
590
- horolog -= hour * 3600
591
- minute = horolog // 60
592
- second = horolog % 60
593
- return datetime .time (hour , minute , second )
630
+ hour = int (horolog // 3600 )
631
+ horolog -= int (hour * 3600 )
632
+ minute = int (horolog // 60 )
633
+ second = int (horolog % 60 )
634
+ micro = int (value % 1 * 1000000 )
635
+ return datetime .time (hour , minute , second , micro )
594
636
return value
595
637
596
638
return process
@@ -599,6 +641,7 @@ def process(value):
599
641
colspecs = {
600
642
sqltypes .Date : _IRISDate ,
601
643
sqltypes .DateTime : _IRISDateTime ,
644
+ sqltypes .TIMESTAMP : _IRISTimeStamp ,
602
645
sqltypes .Time : _IRISTime ,
603
646
}
604
647
@@ -653,7 +696,7 @@ def __init__(self, **kwargs):
653
696
def _get_option (self , connection , option ):
654
697
cursor = connection .cursor ()
655
698
# cursor = connection.cursor()
656
- cursor .execute ('SELECT %SYSTEM_SQL.Util_GetOption(?)' , [ option , ] )
699
+ cursor .execute ('SELECT %SYSTEM_SQL.Util_GetOption(?)' , option )
657
700
row = cursor .fetchone ()
658
701
if row :
659
702
return row [0 ]
@@ -662,7 +705,7 @@ def _get_option(self, connection, option):
662
705
def _set_option (self , connection , option , value ):
663
706
cursor = connection .cursor ()
664
707
# cursor = connection.cursor()
665
- cursor .execute ('SELECT %SYSTEM_SQL.Util_SetOption(?, ?)' , [ option , value , ] )
708
+ cursor .execute ('SELECT %SYSTEM_SQL.Util_SetOption(?, ?)' , option , value )
666
709
row = cursor .fetchone ()
667
710
if row :
668
711
return row [0 ]
@@ -692,8 +735,7 @@ def set_isolation_level(self, connection, level_str):
692
735
693
736
@classmethod
694
737
def dbapi (cls ):
695
- dbapi .connect = irisnative .connect
696
- dbapi .paramstyle = "format"
738
+ # dbapi.paramstyle = "format"
697
739
return dbapi
698
740
699
741
def create_connect_args (self , url ):
@@ -708,36 +750,15 @@ def create_connect_args(self, url):
708
750
709
751
return ([], opts )
710
752
711
- def _fix_for_params (self , query , params , many = False ):
712
- if query .endswith (';' ):
713
- query = query [:- 1 ]
714
- if params is None :
715
- params = []
716
- elif hasattr (params , 'keys' ):
717
- # Handle params as dict
718
- args = {k : "?" % k for k in params }
719
- query = query % args
720
- else :
721
- # Handle params as sequence
722
- args = ['?' for i in range (len (params if not many else params [0 ]))]
723
- query = query % tuple (args )
724
- newparams = list ()
725
- for p in params :
726
- newparams .append (p if not many else list (p )
727
- if len (p ) > 1 else p [0 ])
728
- return query , newparams
729
-
730
753
def do_execute (self , cursor , query , params , context = None ):
731
- query , params = self ._fix_for_params (query , params )
732
- # print('do_execute', query, params)
733
- cursor .execute (query , params )
754
+ cursor .execute (query , * params )
734
755
735
756
def do_executemany (self , cursor , query , params , context = None ):
736
- query , params = self ._fix_for_params (query , params , True )
737
757
cursor .executemany (query , params )
738
758
739
759
def do_begin (self , connection ):
740
760
pass
761
+ # connection.cursor().execute("START TRANSACTION")
741
762
742
763
def do_rollback (self , connection ):
743
764
connection .rollback ()
0 commit comments