/
db_query.py
203 lines (156 loc) · 5.59 KB
/
db_query.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/python
'''@db_query
This file contains various generic SQL-related methods.
'''
import MySQLdb as DB
from log.logger import Logger
from brain.database.db_settings import Database
class SQL(object):
'''@SQL
This class provides an interface to connect, execute commands, and
disconnect from a SQL database. It explicitly inherits pythons 'new-style'
class.
Note: this class is invoked within 'save_xx.py', and 'retrieve_xx.py'
modules.
Note: this class explicitly inherits the 'new-style' class.
'''
def __init__(self, host=None, user=None, passwd=None):
'''@__init__
This constructor is responsible for defining class variables.
'''
self.db_settings = Database()
self.list_error = []
self.proceed = True
# database logger
self.logger = Logger(__name__, 'database', 'database', 'debug')
# host address
if host:
self.host = host
else:
self.host = self.db_settings.get_db_host()
# sql username for above host address
if user:
self.user = user
else:
self.user = self.db_settings.get_db_username()
# sql password for above username
if passwd:
self.passwd = passwd
else:
self.passwd = self.db_settings.get_db_password()
def sql_connect(self, database=None):
'''@sql_connect
This method is responsible for defining the necessary interface to
connect to a SQL database.
'''
try:
if database is None:
self.conn = DB.connect(
self.host,
self.user,
self.passwd,
)
else:
self.conn = DB.connect(
self.host,
self.user,
self.passwd,
db=database,
)
self.cursor = self.conn.cursor()
# log successful connection
if (self.cursor):
self.logger.log('database connected: success')
return {
'status': True,
'error': None,
'id': None,
}
except DB.Error, error:
self.proceed = False
self.list_error.append(error)
# log unsuccessful connection
if (self.cursor):
self.logger.log('database connected: fail')
return {
'status': False,
'error': self.list_error,
'id': None,
}
def sql_command(self, sql_statement, sql_type, sql_args=None):
'''@sql_connect
This method is responsible for defining the necessary interface to
perform SQL commands.
@sql_args, is a tuple used for argument substitution with the supplied
'sql_statement'.
'''
if self.proceed:
try:
self.cursor.execute(sql_statement, sql_args)
# commit change(s), return lastrowid
if sql_type in ['insert', 'delete', 'update']:
self.conn.commit()
# fetch all the rows, return as list of lists.
elif sql_type == 'select':
result = self.cursor.fetchall()
# log transaction
arguments = sql_args if sql_args else 'None'
self.logger.log(
'transaction: success, statement: %s, arguments: %s' %
(sql_statement, arguments))
except DB.Error, error:
self.conn.rollback()
self.list_error.append(error)
return {
'status': False,
'error': self.list_error,
'result': None,
}
# log transaction
arguments = sql_args if sql_args else 'None'
self.logger.log(
'transaction: success, statement' + sql_statement +
', arguments: ' + arguments
)
if sql_type in ['insert', 'delete', 'update']:
return {
'status': False,
'error': self.list_error,
'id': self.cursor.lastrowid,
}
elif sql_type == 'select':
return {
'status': False,
'error': self.list_error,
'result': result,
}
def sql_disconnect(self):
'''@sql_disconnect
This method is responsible for defining the necessary interface to
disconnect from a SQL database.
'''
if self.proceed:
try:
if self.conn:
self.conn.close()
# log disconnection
self.logger.log('database disconnected: success')
return {
'status': True,
'error': None,
'id': self.cursor.lastrowid,
}
except DB.Error, error:
self.list_error.append(error)
# log disconnection
self.logger.log('database disconnected: fail')
return {
'status': False,
'error': self.list_error,
'id': self.cursor.lastrowid,
}
def get_errors(self):
'''@get_errors
This method returns all errors pertaining to the instantiated class.
'''
return self.list_error