[CHG] simplified dbsource.execute api to a single method with two possible behaviors

(../ext rev 351.3.4)
This commit is contained in:
Daniel Reis
2012-09-13 11:08:37 +01:00
committed by Ángel Rivas
parent f6926f82f8
commit 54f3a19293

View File

@@ -105,8 +105,8 @@ Sample connection strings:
return conn return conn
def execute_and_inspect(self, cr, uid, ids, sqlquery, sqlparams=None, context=None): def execute(self, cr, uid, ids, sqlquery, sqlparams=None, metadata=False, context=None):
"""Executes SQL and returns a dict with the rows and the list of columns. """Executes SQL and returns a list of rows.
"sqlparams" can be a dict of values, that can be referenced in the SQL statement "sqlparams" can be a dict of values, that can be referenced in the SQL statement
using "%(key)s" or, in the case of Oracle, ":key". using "%(key)s" or, in the case of Oracle, ":key".
@@ -114,30 +114,31 @@ Sample connection strings:
sqlquery = "select * from mytable where city = %(city)s and date > %(dt)s" sqlquery = "select * from mytable where city = %(city)s and date > %(dt)s"
params = {'city': 'Lisbon', 'dt': datetime.datetime(2000, 12, 31)} params = {'city': 'Lisbon', 'dt': datetime.datetime(2000, 12, 31)}
Sample result: { 'colnames'; ['col_a', 'col_b', ...] If metadata=True, it will instead return a dict containing the rows list and the columns list,
, 'rows': [ (a0, b0, ...), (a1, b1, ...), ...] } in the format:
{ 'cols': [ 'col_a', 'col_b', ...]
, 'rows': [ (a0, b0, ...), (a1, b1, ...), ...] }
""" """
data = self.browse(cr, uid, ids) data = self.browse(cr, uid, ids)
nams = dict() rows, cols = list(), list()
rows = list()
for obj in data: for obj in data:
conn = self.conn_open(cr, uid, obj.id) conn = self.conn_open(cr, uid, obj.id)
if obj.connector in ["sqlite","mysql","mssql"]: if obj.connector in ["sqlite","mysql","mssql"]:
#using sqlalchemy #using sqlalchemy
cur = conn.execute(sqlquery, sqlparams) cur = conn.execute(sqlquery, sqlparams)
nams = cur.keys() if metadata: cols = cur.keys()
rows = [r for r in cur] rows = [r for r in cur]
else: else:
#using other db connectors #using other db connectors
cur = conn.cursor() cur = conn.cursor()
cur.execute(sqlquery, sqlparams) cur.execute(sqlquery, sqlparams)
nams = [d[0] for d in cur.description] if metadata: cols = [d[0] for d in cur.description]
rows = cur.fetchall() rows = cur.fetchall()
conn.close() conn.close()
return {'colnames': nams, 'rows': rows} if metadata:
return{'cols': cols, 'rows': rows}
def execute(self, cr, uid, ids, sqlquery, sqlparams=None, context=None): else:
return self.execute_and_inspect(self, cr, uid, ids, sqlquery, sqlparams)['rows'] return rows
def connection_test(self, cr, uid, ids, context=None): def connection_test(self, cr, uid, ids, context=None):
for obj in self.browse(cr, uid, ids, context): for obj in self.browse(cr, uid, ids, context):