-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery_runner.py
65 lines (58 loc) · 2.45 KB
/
query_runner.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
import re
from odbc_helper import ODBCHelper
from results_container import ResultsContainer
from query_reader import QueryReader
from collections import namedtuple
import pyodbc
'''
Class QueryRunner is used to process sql queries on specified
ODBC enabled datasource. It requires existing odbc driver created.
The connection is determined by the constructor parameters.
'''
DatabaseConnection = namedtuple('DatabaseConnection',
'driver server port database user password')
class QueryRunner(object):
helper = None
def __init__(self, db):
self.helper = ODBCHelper(db.driver, db.server, db.port, db.database,
db.user, db.password)
self.helper.connect()
def destroy(self):
self.helper.disconnect()
self.helper.destroy()
# Method runs given query with specified name. Query is forwarded
# to odbc driver as is, the specified name is used to name the xml
# file with query output.
def run(self, query_name, query):
print "performing query: " + query_name
rows = []
columns = []
exc_class = None
exc_type = None
exc_message = None
query_type = None
update_count = None
try:
rows = self.helper.execute_query(query.encode('UTF-8'))
columns = self.helper.parse_columns_from_query(
query.encode('UTF-8'))
update_count = self.helper.get_update_count()
query_type = self.helper.get_query_type()
except Exception as e:
try:
print e
exc_class = re.findall(r"([.*\.]*.*Exception)", e.args[1])[0]
exc_message_with_sql_state = e.args[1].split(exc_class)[
0].replace("\n", "")
exc_message = re.split("\[.*\]\s*ERROR:\s*(.*)",
exc_message_with_sql_state)[1]
exc_type = exc_class.split(".")[-1]
print "exception during query: " + query_name
except Exception as e:
print e
if exc_message is None:
exc_message = "Unexpected exception occured."
print "Error occured during connecting to database. Check the server is up."
return ResultsContainer(query_name, query, query_type, update_count,
columns, rows, exc_type, exc_message, None,
exc_class)