Package tlib :: Package base :: Module OracleConnector
[hide private]
[frames] | no frames]

Source Code for Module tlib.base.OracleConnector

  1  from tlib.base.ExceptionHelper import TLibException 
  2  import cx_Oracle 
  3   
  4   
5 -class ConnectionError(TLibException):
6 - def __init__(self, err_code, err_description):
7 self.code, self.description = err_code, err_description
8 - def __str__(self):
9 return 'Connection to DB FAILURE with error(/code: description/) /%d: %s/' % (self.code, self.description)
10 11
12 -class AutoCommitError(TLibException):
13 - def __init__(self, value):
14 self.value = value
15 - def __str__(self):
16 return "Wrong argument '%s' passed in <get_autocommit_status> function. Valid args are str: 'local', 'global' or 'session'." % self.value
17 18
19 -class QueryError(TLibException):
20 - def __init__(self, err_code, err_description):
21 self.code, self.description = err_code, err_description
22 - def __str__(self):
23 return 'query function FAILURE with error(/code: description/) /%d: %s/ %d: %s' % (self.code, self.description)
24 25
26 -class ExecuteError(TLibException):
27 - def __init__(self, err_code, err_description):
28 self.code, self.description = err_code, err_description
29 - def __str__(self):
30 return 'execute function FAILURE with error (/code: description/) /%d: %s/' % (self.code, self.description)
31 32
33 -class OracleConnector(object):
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 # in case of autocommit will change by default to ON in future 51 except cx_Oracle.Error, err: 52 raise ConnectionError(err.args[0], err.args[1])
53 54
55 - def get_autocommit_status(self):
56 """ 57 returns tuple Autocommit status i.e. 1 for True and 0 for False 58 """ 59 return self.db.autocommit
60 61
62 - def query_select(self, query):
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
74 - def query_execute(self, query):
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
86 - def commit(self):
87 """ 88 Commits all changes, returns Exception in case of failure 89 """ 90 self.db.commit()
91
92 - def rollback(self):
93 """ 94 Rolls back all commits, returns exception in case of failure 95 """ 96 self.db.rollback()
97
98 - def close_cursor(self):
99 """ 100 Close cursor. Connection to DB is still open, new cursor can be created 101 """ 102 self.cursor.close()
103
104 - def close_db(self):
105 """ 106 Close DB connection. All uncommitted changes are lost 107 """ 108 self.db.close()
109
110 - def __del__(self):
111 self.cursor.close() 112 self.db.close()
113