@@ -89,8 +89,6 @@ class Database(object):
8989 OS_API_CERTIFICATE_FILE = 'API_CERTIFICATE_FILE'
9090 OS_DB_CERTIFICATE_FILE = 'DB_CERTIFICATE_FILE'
9191
92- KITT_MAX_CONNECTION_RETRY_COUNT = 5
93-
9492 def __init__ (self , credentials = None , start_session = False , echo = False , tenant_id = None , entity_metadata = None ,
9593 entity_type_id = None , model_store = None ):
9694
@@ -571,6 +569,54 @@ def __init__(self, credentials=None, start_session=False, echo=False, tenant_id=
571569 if entity_type_id is not None :
572570 self .init_model_store (self .schema )
573571
572+ # Determine the precision of fractional seconds for timestamps in database
573+ self .precision_fractional_seconds = self .get_precision_fractional_seconds ()
574+
575+ def get_precision_fractional_seconds (self ):
576+
577+ if self .db_type == 'db2' :
578+ sql_statement = f'SELECT CAST(TIMESTAMP(\' 1970-01-01-00.00.00.123456789\' ) AS CHAR(31)) ' \
579+ f'FROM "IOTANALYTICS"."KPI_FUNCTION"'
580+ logger .debug (f"Sql statement to determine the precision of fractional seconds for timestamps in DB2: "
581+ f"{ sql_statement } " )
582+
583+ # Execute sql statement and get the single result of select statement
584+ timestamp_str = None
585+ try :
586+ result_handle = ibm_db .exec_immediate (self .native_connection , sql_statement )
587+
588+ try :
589+ row = ibm_db .fetch_tuple (result_handle )
590+ if row is not False :
591+ timestamp_str = str (row [0 ]).strip ()
592+ except Exception as ex :
593+ raise RuntimeError (f"The retrieval of the result of sql statement to determine the precision of "
594+ f"fractional seconds in DB2 failed." ) from ex
595+ finally :
596+ ibm_db .free_result (result_handle )
597+
598+ except Exception as ex :
599+ raise RuntimeError (f"The sql statement to determine the precision of fractional seconds in DB2 "
600+ f"failed." ) from ex
601+
602+ # Timestamp is supposed to look like this: 1970-01-01-00.00.00.123456
603+ if timestamp_str is not None and len (timestamp_str ) > 18 :
604+ if len (timestamp_str ) > 20 :
605+ fractional_str = timestamp_str [20 :]
606+ precision = len (fractional_str )
607+ else :
608+ precision = 0
609+ else :
610+ precision = 6
611+ logger .warning (f"Unexpected format of timestamp string '{ timestamp_str } '. Precision of fractional "
612+ f"seconds for timestamps set to default of 6" )
613+ else :
614+ precision = 6
615+
616+ logger .debug (f"Precision of fractional seconds for timestamps in DB2: { precision } " )
617+
618+ return precision
619+
574620 def set_entity_type (self , entity_type , entity_type_id ):
575621
576622 self .entity_type_id = entity_type_id
0 commit comments