Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.5 kB
1
Indexable
Never
import pandas as pd
import mysql.connector
import json
from base64 import b64decode as base_b64decode
from base64 import b64encode as base_b64encode
import logging
import datetime
import os
from sqlalchemy import create_engine as sqlalchemy_create_engine

releaseId = '275.2'
releaseType = 'DEPLOYED'
replicationTarget = 'TESTING'
catalogId = ''
opId = 'HOB'
buId = 'DEFAULT'
replicationJobId = 'REP_990_234'
json_file_path = r"C:\Users\2009927\Downloads\ppm_reply.json"
sql_log_file = r"C:\Users\2009927\Downloads\ppm_reply.sql"
logging.basicConfig(filename=r'C:\Users\2009927\Downloads\ppm_reply.log',
                    level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
query_count = 0



def write_sql(query_info):
    with open(sql_log_file, "w") as log_file:
        log_file.write(query_info)
        log_file.write('\n')


try:
    def connect_to_database(encrypt, host, port, user, db_type, schema):
        if encrypt == 'Y':
            password = base_b64decode(json_data.get('PPM_PC', {}).get('DB_PASSWORD')).decode('utf-8')
        else:
            password = json_data.get('PPM_PC', {}).get('DB_PASSWORD')
        if db_type == 'MYSQL':
            cnx = mysql.connector.connect(user=user, password=password, host=host, port=port)
            cursor = cnx.cursor()
            logging.info(f"connected to database server PPM_PC: {host}:{port}")
            return cnx, cursor
        elif db_type == 'ORACLE':
            import oracledb
            oracle_mode = oracledb.is_thin_mode()
            print("Oracle mode: %s" % oracle_mode)
            if oracle_mode:
                oracledb.init_oracle_client()
                print("Enabled python-oracledb Thick mode")
            else:
                print("Default python-oracledb Thick mode")

            cnx = ('oracle://%s:%s@%s:%s/?service_name=%s' % (user, password, host, port, schema))
            connection = sqlalchemy_create_engine(cnx, encoding="utf8")
            connection = connection.raw_connection()
            cursor = connection.cursor()

        return cursor


    def connect_to_database_source(encrypt_tar, host_tar, port_tar, user_tar, db_type_tar, schema_tar):
        if encrypt_tar == 'Y':
            password_tar = base_b64decode(json_data.get('TESTING', {}).get('DB_PASSWORD')).decode('utf-8')
        else:
            password_tar = json_data.get('TESTING', {}).get('DB_PASSWORD')
        if db_type_tar == 'MYSQL':
            cnx_tar = mysql.connector.connect(user_tar=user_tar, password_tar=password_tar, host_tar=host_tar, port_tar=port_tar)
            cursor_tar = cnx_tar.cursor()
            logging.info(f"connected to database server PPM_PC: {host}:{port}")
            return cnx_tar, cursor_tar
        elif db_type_tar == 'ORACLE':
            import oracledb
            oracle_mode = oracledb.is_thin_mode()
            print("Oracle mode: %s" % oracle_mode)
            if oracle_mode:
                oracledb.init_oracle_client()
                print("Enabled python-oracledb Thick mode")
            else:
                print("Default python-oracledb Thick mode")

            cnx_tar = ('oracle://%s:%s@%s:%s/?service_name=%s' % (user, password, host, port, schema))
            connection_tar = sqlalchemy_create_engine(cnx, encoding="utf8")
            connection_tar = connection.raw_connection()
            cursor_tar = connection.cursor()

        return cursor_tar


    with open(json_file_path) as json_file:
        json_data = json.load(json_file)
    encrypt = json_data.get('PPM_PC', {}).get('ENCRYPT')
    host = json_data.get('PPM_PC', {}).get('DB_HOST')
    port = json_data.get('PPM_PC', {}).get('DB_PORT')
    user = json_data.get('PPM_PC', {}).get('DB_USER')
    db_type = json_data.get('PPM_PC', {}).get('DB_TYPE')
    schema = json_data.get(replicationTarget, {}).get('DB_SCHEMA')
    logging.info("DATABASE - HOST FOR PPM_PC - " + host)
    logging.info("DATABASE - PORT FOR PPM_PC - " + port)
    logging.info("DATABASE - USER - PPM_PC" + user)
    connection, cursor = connect_to_database(encrypt, host, port, user, db_type, schema)
    cursor = connection.cursor()
    primary_query = f"SELECT * FROM {schema}.etl_ppm_replication_master"
    cursor.execute(primary_query)
    logging.info(f"executed primary query - {primary_query}")
    query_info = f"-- ++++++++++++++++++++|PRIMARY_QUERY\n"
    query_info += f"-- #Query:\n{primary_query};\n"
    write_sql(query_info)

    rows = cursor.fetchall()
    columns = [desc[0] for desc in cursor.description]
    df = pd.DataFrame(rows, columns=columns)

    # replicationTarget
    encrypt_tar = json_data.get(replicationTarget, {}).get('ENCRYPT')
    host_tar = json_data.get(replicationTarget, {}).get('DB_HOST')
    port_tar = json_data.get(replicationTarget, {}).get('DB_PORT')
    user_tar = json_data.get(replicationTarget, {}).get('DB_USER')
    logging.info("DATABASE - HOST FOR REPLICATION_TARGET - " + host_tar)
    logging.info("DATABASE - PORT FOR REPLICATION_TARGET - " + port_tar)
    logging.info("DATABASE - USER - REPLICATION_TARGET -" + user_tar)

    connection_tar, cursor_tar = connect_to_database_source(encrypt_tar, host_tar, port_tar, user_tar, db_type_tar,
                                                            schema_tar)
    cursor_tar = connection_tar.cursor()