def create_connection(self, db_name=None): if self.db_type == IMPALA: connection_class = ImpalaDbConnection connection = impala_connect(host=self.host_name, port=self.port or 21050) elif self.db_type == POSTGRESQL: connection_class = PostgresqlDbConnection connection_args = {'user': self.user_name or 'postgres'} if self.password: connection_args['password'] = self.password if db_name: connection_args['database'] = db_name if self.host_name: connection_args['host'] = self.host_name if self.port: connection_args['port'] = self.port global postgresql_connect if not postgresql_connect: try: from psycopg2 import connect as postgresql_connect except: print('Error importing psycopg2. Please make sure it is installed. ' 'See the README for details.') raise connection = postgresql_connect(**connection_args) connection.autocommit = True elif self.db_type == MYSQL: connection_class = MySQLDbConnection connection_args = {'user': self.user_name or 'root'} if self.password: connection_args['passwd'] = self.password if db_name: connection_args['db'] = db_name if self.host_name: connection_args['host'] = self.host_name if self.port: connection_args['port'] = self.port global mysql_connect if not mysql_connect: try: from MySQLdb import connect as mysql_connect except: print('Error importing MySQLdb. Please make sure it is installed. ' 'See the README for details.') raise connection = mysql_connect(**connection_args) else: raise Exception('Unexpected database type: %s' % self.db_type) return connection_class(self, connection, db_name=db_name)
def __init__(self): self.db_conn = postgresql_connect("dbname=pguser user=pguser") self.db_cursor = self.db_conn.cursor() q_prepare_sreq = ( "PREPARE store_request AS " + "INSERT INTO request " + "(at_time, instance, idx, op, headers, " + "scheme, host, path, query, fragment, data, source)" + "values (now(), $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)") self.db_cursor.execute(q_prepare_sreq) q_prepare_srep = ("PREPARE store_reply AS " + "INSERT INTO reply " + "(at_time, instance, idx, " + "scheme, host, path, query, fragment, " + "status, headers)" + "values (now(), $1, $2, $3, $4, $5, $6, $7, $8, $9)") self.db_cursor.execute(q_prepare_srep)
def __init__(self): self.db_conn = postgresql_connect("dbname=pguser user=pguser") self.db_cursor = self.db_conn.cursor() q_prepare_sreq = ( "PREPARE store_request AS " + "INSERT INTO request " + "(at_time, instance, idx, op, headers, " + "scheme, host, path, query, fragment, data, source)" + "values (now(), $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)") self.db_cursor.execute(q_prepare_sreq) q_prepare_srep = ( "PREPARE store_reply AS " + "INSERT INTO reply " + "(at_time, instance, idx, " + "scheme, host, path, query, fragment, " + "status, headers)" + "values (now(), $1, $2, $3, $4, $5, $6, $7, $8, $9)") self.db_cursor.execute(q_prepare_srep)
def create_connection(self, db_name=None): if self.db_type == HIVE: connection_class = HiveDbConnection connection = impala_connect( host=self.host_name, port=self.port, user=self.user_name, password=self.password, timeout=maxint, auth_mechanism='PLAIN') return HiveDbConnection(self, connection, user_name=self.user_name, user_pass=self.password, db_name=db_name, hdfs_host=self.hdfs_host, hdfs_port=self.hdfs_port) elif self.db_type == IMPALA: connection_class = ImpalaDbConnection connection = impala_connect( host=self.host_name, port=self.port or 21050, timeout=maxint) elif self.db_type == ORACLE: connection_class = OracleDbConnection connection_str = '%(user)s/%(password)s@%(host)s:%(port)s/%(service)s' connection_args = { 'user': self.user_name or 'system', 'password': self.password or 'oracle', 'host': self.host_name or 'localhost', 'port': self.port or 1521, 'service': self.service or 'XE'} try: from cx_Oracle import connect as oracle_connect except: print('Error importing cx_Oracle. Please make sure it is installed. ' 'See the README for details.') raise connection = oracle_connect(connection_str % connection_args) connection.outputtypehandler = OracleDbConnection.type_converter connection.autocommit = True elif self.db_type == POSTGRESQL: connection_class = PostgresqlDbConnection connection_args = {'user': self.user_name or 'postgres'} if self.password: connection_args['password'] = self.password if db_name: connection_args['database'] = db_name if self.host_name: connection_args['host'] = self.host_name if self.port: connection_args['port'] = self.port connection = postgresql_connect(**connection_args) connection.autocommit = True elif self.db_type == MYSQL: connection_class = MySQLDbConnection connection_args = {'user': self.user_name or 'root'} if self.password: connection_args['passwd'] = self.password if db_name: connection_args['db'] = db_name if self.host_name: connection_args['host'] = self.host_name if self.port: connection_args['port'] = self.port try: from MySQLdb import connect as mysql_connect except: print('Error importing MySQLdb. Please make sure it is installed. ' 'See the README for details.') raise connection = mysql_connect(**connection_args) else: raise Exception('Unexpected database type: %s' % self.db_type) return connection_class(self, connection, db_name=db_name)
def connect_production() -> connection: with open("production.yml", "r", encoding="utf-8") as f: config = yaml.load(f) return postgresql_connect(**config)