-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
125 lines (100 loc) · 3.43 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import traceback
try:
import cx_Oracle
except:
pass
try:
import mysql.connector
except:
pass
try:
import psycopg2
except:
pass
DB_ORACLE=0x0
DB_MYSQL=0x1
DB_POSTGRES=0x2
class mysql_database:
""" mysql connection """
def __init__(self, username, password, hostname, dbname):
self.__dbh = None
self.__cursor = None
self.__connect(username, password, hostname, dbname)
def __connect(self, username, password, hostname, dbname):
self.__dbh = mysql.connector.connect(host=hostname,
user=username,
passwd=password,
db=dbname)
self.__cursor = self.__dbh.cursor()
def get_cursor(self):
return self.__cursor
def __exit__(self, exc_type, exc_val, exc_tb):
self.__cursor.close()
self.__dbh.close()
class postgres_database:
""" postgres connection """
def __init__(self, username, password, hostname, dbname):
self.__dbh = None
self.__cursor = None
self.connect_str = "host='%s' dbname='%s' user='%s' password='%s'" % (hostname, dbname, username, password)
self.__connect(self.connect_str)
def __connect(self):
self.__dbh = psycopg2.connect(self.connect_str)
self.__cursor = self.__dbh.cursor()
def get_cursor(self):
return self.__cursor
class oracle_database:
"""
handels the database connection
we assume that all data is read in utf-8, so
we can translate it to any encoding... well, that's
not true, because utf-8 has its limits.
"""
def __init__(self, username="", password="", sid=""):
self.__dbh = None
self.__cursor = None
self.connect_str = "%s/%s@%s" % (username, password, sid)
self.__connect()
def __connect(self):
self.__dbh = cx_Oracle.connect(self.connect_str)
self.__cursor = self.__dbh.cursor()
def get_cursor(self):
return self.__cursor
def __exit__(self, exc_type, exc_val, exc_tb):
self.__cursor.close()
self.__dbh.close()
class generic_database:
"""
generic database interface
"""
def __init__(self, **args):
self.DATABASES = { DB_ORACLE:self.__oracle,DB_MYSQL:self.__mysql,DB_POSTGRES:self.__postgres }
self.host = args.pop('host', None)
self.user = args.pop('username', None)
self.passwd = args.pop('password', None)
self.dbname = args.pop('dbname', None)
self.get_driver = self.DATABASES[args.pop('db_type')]
def __oracle(self):
return oracle_database(self.user, self.passwd, self.dbname)
def __mysql(self):
return mysql_database(self.user, self.passwd, self.host, self.dbname)
def __postgres(self):
return postgres_database(self.user, self.passwd, self.host, self.dbname)
class query_handler:
"""
returns a normalized data set to a module like csv, json or xml
"""
def __init__(self, sql_statement=None, db_handler=None):
self.__db_handler = db_handler
self.__stmt = sql_statement
self.__cursor = self.__db_handler.get_cursor()
def execute_query(self):
self.__cursor.execute(self.__stmt)
def get_columns(self):
return [c[0] for c in self.__cursor.description]
def get_result(self):
for row in self.__cursor:
yield row
#def get_dict_result(self):
# for row in self.__cursor:
# yield row