SHARE
TWEET

Database

a guest Nov 14th, 2019 209 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ConnectionFactory:
  2.     def get_connection(self, db_type, connection_params):
  3.         if(db_type == 'MySQL'):
  4.             return self.mysql_connection(connection_params)
  5.         if(db_type == 'Big Query'):
  6.             pass
  7.         else:
  8.             raise ValueError(db_type)
  9.  
  10.     def mysql_connection(self, connection_params):
  11.         import mysql.connector as mysql
  12.         return mysql.connect(
  13.             host=connection_params['host'],
  14.             user=connection_params['user'],
  15.             password=connection_params['password']
  16.         )
  17.  
  18. class QueryExecutor:
  19.     def __init__(self, connection, query):
  20.         self.connection = connection
  21.         self.query = query
  22.  
  23.     def set_filters(self, filters):
  24.         self.filters = filters
  25.         return self
  26.  
  27.     def set_sorting_order(self, sorting_order):
  28.         self.sorting_order = sorting_order
  29.         return self
  30.  
  31.     def execute(self):
  32.         import mysql.connector as mysql
  33.         if(type(self.connection == mysql.connection.MySQLConnection)):
  34.             return self.mysql_execute()
  35.  
  36.     def mysql_execute(self):
  37.         cursor = self.connection.cursor()
  38.         cursor.execute(self.query)
  39.         result = cursor.fetchall()
  40.         description = cursor.description
  41.         cursor.close()
  42.  
  43.         return result, [i[0] for i in description]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top