#!/usr/bin/python  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								# -*- coding: utf-8 -*-  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								# ---------------------------------------------------------------------------------------------------------  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								# Author : Ulrich Carmesin  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								# Source : gitea.ucarmesin.de  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								# ---------------------------------------------------------------------------------------------------------  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								import  basic . program  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								import  utils . config_tool  
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								import  utils . dbrel_tool  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								import  mysql . connector  
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								import  basic . constants  as  B  
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								import  utils . data_const  as  D  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								class  DbFcts ( utils . dbrel_tool . DbFcts ) :  
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    """ 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    This  interface  defines  each  necessary  connection  to  any  kind  of  database . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    The  specific  technique  how  to  connect  to  the  concrete  DBMS  has  to  be  implemented  in  the  specific  tool . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    """ 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    def  __init__ ( self ) : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        pass 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    def  selectRows ( self ,  table ,  job ,  where = " " ) : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        """  method to select rows from a database 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        statement  written  in  sql  """ 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        tdata  =  { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        verify  =  - 1 + job . getDebugLevel ( " db_tool " ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        attr  =  self . getDbAttributes ( B . SVAL_NULL ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        sql  =  " SELECT * FROM  " + attr [ B . ATTR_DB_DATABASE ] + " . " + table 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if  len ( where )  >  3 : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            sql  + =  "   " + where 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        sql  + =  " ; " 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        self . comp . m . logInfo ( sql ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        connector  =  self . getConnector ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        mycursor  =  connector . cursor ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        mycursor . execute ( sql ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        myresult  =  mycursor . fetchall ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        tdata [ B . DATA_NODE_HEADER ]  =  [ ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        for  f  in  self . comp . conf [ B . DATA_NODE_DDL ] [ table ] [ B . DATA_NODE_HEADER ] : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            tdata [ B . DATA_NODE_HEADER ] . append ( f ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        tdata [ B . DATA_NODE_DATA ]  =  [ ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        for  x  in  myresult : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            r  =  { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            i  =  0 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            for  f  in  self . comp . conf [ B . DATA_NODE_DDL ] [ table ] [ B . DATA_NODE_HEADER ] : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                r [ f ]  =  x [ i ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                i  + =  1 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            tdata [ B . DATA_NODE_DATA ] . append ( r ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        self . comp . m . logInfo ( str ( tdata ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return  tdata 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    def  deleteRows ( self ,  table ,  job ,  where = " " ) : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        """  method to delete rows from a database 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        statement  written  in  sql  """ 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        verify  =  - 1 + job . getDebugLevel ( " db_tool " ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        attr  =  self . getDbAttributes ( B . SVAL_NULL ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        sql  =  " DELETE FROM  " + attr [ B . ATTR_DB_DATABASE ] + " . " + table 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if  len ( where )  >  3 : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            sql  + =  "   " + where 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        sql  + =  " ; " 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        self . comp . m . logInfo ( sql ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        self . execStatement ( sql ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    def  updateRows ( self ,  statement ,  job ) : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        """  method to delete rows from a database 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        statement  written  in  sql  """ 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        raise  Exception ( B . EXCEPT_NOT_IMPLEMENT ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    def  insertRows ( self ,  table ,  rows ,  job ) : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        """  method to insert rows into a database 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        the  rows  will  be  interpreted  by  the  ddl  of  the  component 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        """ 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        verify  =  - 1 + job . getDebugLevel ( " db_tool " ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        attr  =  self . getDbAttributes ( B . SVAL_NULL ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        sql  =  " INSERT INTO  " + attr [ B . ATTR_DB_DATABASE ] + " . " + table 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        sql  + =  "  (  " + " , " . join ( self . comp . conf [ B . DATA_NODE_DDL ] [ table ] [ B . DATA_NODE_HEADER ] )  +  "  )  " 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        sql  + =  "  VALUES (  " 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        for  x  in  self . comp . conf [ B . DATA_NODE_DDL ] [ table ] [ B . DATA_NODE_HEADER ] : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            sql  + =  " %s ,  " 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        sql  =  sql [ 0 : - 2 ]  +  "  ) " 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        self . comp . m . logInfo ( sql ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        values  =  [ ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        for  r  in  rows : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            rowvalues  =  [ ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            for  h  in  self . comp . conf [ B . DATA_NODE_DDL ] [ table ] [ B . DATA_NODE_HEADER ] : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                if  ( self . comp . conf [ B . DATA_NODE_DDL ] [ table ] [ h ]  ==  D . TYPE_PK ) : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    continue 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                if  ( h  in  r ) : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    rowvalues . append ( self . getDbValue ( self . comp . conf [ B . DATA_NODE_DDL ] [ table ] [ h ] ,  r [ h ] ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                else : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    rowvalues . append ( self . getDbValue ( self . comp . conf [ B . DATA_NODE_DDL ] [ table ] [ h ] ,  " " ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            values . append (  tuple ( rowvalues ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        self . comp . m . logInfo ( str ( values ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        try : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            connector  =  self . getConnector ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            mycursor  =  connector . cursor ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            mycursor . executemany ( sql ,  values ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            connector . commit ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        except  Exception  as  e : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            self . comp . m . setError ( " " ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            return 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        self . comp . m . setMsg ( str ( len ( values ) ) + "  rows inserted into  " + table ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    def  execStatement ( self ,  statement ) : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        """  add-on-method to execute the statement 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        this  method  should  only  called  by  the  class  itself  """ 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        connector  =  self . getConnector ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        cursor  =  connector . cursor ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        try : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            cursor . execute ( statement ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            connector . commit ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        except  Exception  as  e : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            if  " CREATE INDEX  "  in  statement : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                return 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            raise  Exception ( " DB-Exception  " + statement + " \n " + e . __str__ ( ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        print ( " Statement executed  " + statement ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        self . comp . m . setMsg ( " Statement executed " ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    def  getConnector ( self ) : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        """  add-on-method to get the connector 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        this  method  should  only  called  by  the  class  itself  """ 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        job  =  self . job  # basic.program.Job.getInstance() 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        attr  =  self . getDbAttributes ( B . SVAL_NULL ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        cnx  =  mysql . connector . connect ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								          host = attr [ B . ATTR_DB_HOST ] , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								          user = attr [ B . ATTR_DB_USER ] , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								          password = attr [ B . ATTR_DB_PASSWD ] , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								          database = attr [ B . ATTR_DB_DATABASE ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return  cnx