1 from tlib.base.ExceptionHelper import TLibException
2 import cx_Oracle
3
4
6 - def __init__(self, err_code, err_description):
7 self.code, self.description = err_code, err_description
9 return 'Connection to DB FAILURE with error(/code: description/) /%d: %s/' % (self.code, self.description)
10
11
16 return "Wrong argument '%s' passed in <get_autocommit_status> function. Valid args are str: 'local', 'global' or 'session'." % self.value
17
18
20 - def __init__(self, err_code, err_description):
21 self.code, self.description = err_code, err_description
23 return 'query function FAILURE with error(/code: description/) /%d: %s/ %d: %s' % (self.code, self.description)
24
25
27 - def __init__(self, err_code, err_description):
28 self.code, self.description = err_code, err_description
30 return 'execute function FAILURE with error (/code: description/) /%d: %s/' % (self.code, self.description)
31
32
34 """
35 Connects to MySQL DB, executes queries
36 Autocommit is disabled by default, commit is done with commit function
37 Warnings are turned into exceptions
38 """
39 - def __init__(self, sid, username=None, password=None, host=None, port=None):
40 """
41 Initialize DB connection. Turning warning into exceptions
42 """
43 self.db = None
44 try:
45 dsn_tns = sid
46 if host:
47 dsn_tns = cx_Oracle.makedsn(host, port, sid)
48 self.db = cx_Oracle.connect(user=username, password=password, dsn=dsn_tns)
49 self.cursor = self.db.cursor()
50 self.db.autocommit = 0
51 except cx_Oracle.Error, err:
52 raise ConnectionError(err.args[0], err.args[1])
53
54
56 """
57 returns tuple Autocommit status i.e. 1 for True and 0 for False
58 """
59 return self.db.autocommit
60
61
63 """
64 Send/execute SELECT queries, returns data from DB
65 :param: str MySQL query
66 :return: tuple with arguments dictionaries, each dictionary is a row of DB i.e. ({row_first}, {row_second}, ... ,{row_last})
67 """
68 try:
69 self.cursor.execute(query)
70 return self.cursor.fetchall()
71 except cx_Oracle.Error, err:
72 raise QueryError(err.args[0], err.args[1])
73
75 """
76 Sends query to DB. Can be DELETE, UPDATE
77 :param: str query
78 :return: dict {DictCursor}
79 """
80 try:
81 self.cursor.execute(query)
82 return self.cursor
83 except cx_Oracle.Error, err:
84 raise ExecuteError(err.args[0], err.args[1])
85
87 """
88 Commits all changes, returns Exception in case of failure
89 """
90 self.db.commit()
91
93 """
94 Rolls back all commits, returns exception in case of failure
95 """
96 self.db.rollback()
97
99 """
100 Close cursor. Connection to DB is still open, new cursor can be created
101 """
102 self.cursor.close()
103
105 """
106 Close DB connection. All uncommitted changes are lost
107 """
108 self.db.close()
109
111 self.cursor.close()
112 self.db.close()
113