| 1 |
def get_all(com, sql, *params): |
|---|
| 2 |
"""Executes the query and returns the (description, data)""" |
|---|
| 3 |
db = com.env.get_db_cnx() |
|---|
| 4 |
cur = db.cursor() |
|---|
| 5 |
desc = None |
|---|
| 6 |
data = None |
|---|
| 7 |
try: |
|---|
| 8 |
cur.execute(sql, params) |
|---|
| 9 |
data = list(cur.fetchall()) |
|---|
| 10 |
desc = cur.description |
|---|
| 11 |
db.commit(); |
|---|
| 12 |
except Exception, e: |
|---|
| 13 |
com.log.error('There was a problem executing sql:%s \n \ |
|---|
| 14 |
with parameters:%s\nException:%s'%(sql, params, e)); |
|---|
| 15 |
db.rollback(); |
|---|
| 16 |
try: |
|---|
| 17 |
db.close() |
|---|
| 18 |
except: |
|---|
| 19 |
pass |
|---|
| 20 |
|
|---|
| 21 |
return (desc, data) |
|---|
| 22 |
|
|---|
| 23 |
def execute_non_query(com, sql, *params): |
|---|
| 24 |
"""Executes the query on the given project""" |
|---|
| 25 |
db = com.env.get_db_cnx() |
|---|
| 26 |
cur = db.cursor() |
|---|
| 27 |
try: |
|---|
| 28 |
cur.execute(sql, params) |
|---|
| 29 |
db.commit() |
|---|
| 30 |
except Exception, e: |
|---|
| 31 |
com.log.error('There was a problem executing sql:%s \n \ |
|---|
| 32 |
with parameters:%s\nException:%s'%(sql, params, e)); |
|---|
| 33 |
db.rollback(); |
|---|
| 34 |
try: |
|---|
| 35 |
db.close() |
|---|
| 36 |
except: |
|---|
| 37 |
pass |
|---|
| 38 |
|
|---|
| 39 |
def get_first_row(com, sql,*params): |
|---|
| 40 |
""" Returns the first row of the query results as a tuple of values (or None)""" |
|---|
| 41 |
db = com.env.get_db_cnx() |
|---|
| 42 |
cur = db.cursor() |
|---|
| 43 |
data = None; |
|---|
| 44 |
try: |
|---|
| 45 |
cur.execute(sql, params) |
|---|
| 46 |
data = cur.fetchone(); |
|---|
| 47 |
db.commit(); |
|---|
| 48 |
except Exception, e: |
|---|
| 49 |
com.log.error('There was a problem executing sql:%s \n \ |
|---|
| 50 |
with parameters:%s\nException:%s'%(sql, params, e)); |
|---|
| 51 |
db.rollback() |
|---|
| 52 |
try: |
|---|
| 53 |
db.close() |
|---|
| 54 |
except: |
|---|
| 55 |
pass |
|---|
| 56 |
return data; |
|---|
| 57 |
|
|---|
| 58 |
def get_scalar(com, sql, col=0, *params): |
|---|
| 59 |
""" Gets a single value (in the specified column) from the result set of the query""" |
|---|
| 60 |
data = get_first_row(com, sql, *params); |
|---|
| 61 |
if data: |
|---|
| 62 |
return data[col] |
|---|
| 63 |
else: |
|---|
| 64 |
return None; |
|---|
| 65 |
|
|---|
| 66 |
def execute_in_trans(com, *args): |
|---|
| 67 |
db = com.env.get_db_cnx() |
|---|
| 68 |
cur = db.cursor() |
|---|
| 69 |
result = True |
|---|
| 70 |
try: |
|---|
| 71 |
for sql, params in args: |
|---|
| 72 |
cur.execute(sql, params) |
|---|
| 73 |
db.commit() |
|---|
| 74 |
except Exception, e: |
|---|
| 75 |
com.log.error('There was a problem executing sql:%s \n \ |
|---|
| 76 |
with parameters:%s\nException:%s'%(sql, params, e)); |
|---|
| 77 |
db.rollback(); |
|---|
| 78 |
result = e |
|---|
| 79 |
try: |
|---|
| 80 |
db.close() |
|---|
| 81 |
except: |
|---|
| 82 |
pass |
|---|
| 83 |
return result |
|---|
| 84 |
|
|---|
| 85 |
def db_table_exists(com, table): |
|---|
| 86 |
db = com.env.get_db_cnx() |
|---|
| 87 |
sql = "SELECT * FROM %s LIMIT 1" % table; |
|---|
| 88 |
cur = db.cursor() |
|---|
| 89 |
has_table = True; |
|---|
| 90 |
try: |
|---|
| 91 |
cur.execute(sql) |
|---|
| 92 |
db.commit() |
|---|
| 93 |
except Exception, e: |
|---|
| 94 |
has_table = False |
|---|
| 95 |
db.rollback() |
|---|
| 96 |
|
|---|
| 97 |
try: |
|---|
| 98 |
db.close() |
|---|
| 99 |
except: |
|---|
| 100 |
pass |
|---|
| 101 |
return has_table |
|---|
| 102 |
|
|---|
| 103 |
def get_column_as_list(com, sql, col=0, *params): |
|---|
| 104 |
data = get_all(com, sql, *params)[1] or () |
|---|
| 105 |
return [valueList[col] for valueList in data] |
|---|
| 106 |
|
|---|
| 107 |
def get_system_value(com, key): |
|---|
| 108 |
return get_scalar(com, "SELECT value FROM system WHERE name=%s", 0, key) |
|---|
| 109 |
|
|---|
| 110 |
def set_system_value(com, key, value): |
|---|
| 111 |
if get_system_value(com, key): |
|---|
| 112 |
execute_non_query(com, "UPDATE system SET value=%s WHERE name=%s", value, key) |
|---|
| 113 |
else: |
|---|
| 114 |
execute_non_query(com, "INSERT INTO system (value, name) VALUES (%s, %s)", |
|---|
| 115 |
value, key) |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
def get_result_set(com, sql, *params): |
|---|
| 119 |
"""Executes the query and returns a Result Set""" |
|---|
| 120 |
tpl = get_all(com, sql, *params); |
|---|
| 121 |
if tpl and tpl[0] and tpl[1]: |
|---|
| 122 |
return ResultSet(tpl) |
|---|
| 123 |
else: |
|---|
| 124 |
return None |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
class ResultSet: |
|---|
| 128 |
""" the result of calling getResultSet """ |
|---|
| 129 |
def __init__ (self, (columnDescription, rows)): |
|---|
| 130 |
self.columnDescription, self.rows = columnDescription, rows |
|---|
| 131 |
self.columnMap = self.get_column_map() |
|---|
| 132 |
|
|---|
| 133 |
def get_column_map ( self ): |
|---|
| 134 |
"""This function will take the result set from getAll and will |
|---|
| 135 |
return a hash of the column names to their index """ |
|---|
| 136 |
h = {} |
|---|
| 137 |
i = 0 |
|---|
| 138 |
if self.columnDescription: |
|---|
| 139 |
for col in self.columnDescription: |
|---|
| 140 |
h[ col[0] ] = i |
|---|
| 141 |
i+=1 |
|---|
| 142 |
return h; |
|---|
| 143 |
|
|---|
| 144 |
def value(self, col, row ): |
|---|
| 145 |
""" given a row(list or idx) and a column( name or idx ), retrieve the appropriate value""" |
|---|
| 146 |
tcol = type(col) |
|---|
| 147 |
trow = type(row) |
|---|
| 148 |
if tcol == str: |
|---|
| 149 |
if(trow == list or trow == tuple): |
|---|
| 150 |
return row[self.columnMap[col]] |
|---|
| 151 |
elif(trow == int): |
|---|
| 152 |
return self.rows[row][self.columnMap[col]] |
|---|
| 153 |
else: |
|---|
| 154 |
print ("rs.value Type Failed col:%s row:%s" % (type(col), type(row))) |
|---|
| 155 |
elif tcol == int: |
|---|
| 156 |
if(trow == list or trow == tuple): |
|---|
| 157 |
return row[col] |
|---|
| 158 |
elif(trow == int): |
|---|
| 159 |
return self.rows[row][col] |
|---|
| 160 |
else: |
|---|
| 161 |
print ("rs.value Type Failed col:%s row:%s" % (type(col), type(row))) |
|---|
| 162 |
else: |
|---|
| 163 |
print ("rs.value Type Failed col:%s row:%s" % (type(col), type(row))) |
|---|
| 164 |
|
|---|
| 165 |
def json_out(self): |
|---|
| 166 |
json = "[%s]" % ',\r\n'. join( |
|---|
| 167 |
[("{%s}" % ','.join( |
|---|
| 168 |
["'%s':'%s'" % |
|---|
| 169 |
(key, str(self.value(val, row)). |
|---|
| 170 |
replace("'","\\'"). |
|---|
| 171 |
replace('"','\\"'). |
|---|
| 172 |
replace('\r','\\r'). |
|---|
| 173 |
replace('\n','\\n')) |
|---|
| 174 |
for (key, val) in self.columnMap.items()])) |
|---|
| 175 |
for row in self.rows]) |
|---|
| 176 |
#mylog.debug('serializing to json : %s'% json) |
|---|
| 177 |
return json |
|---|