[CHG] simplified dbsource.execute api to a single method with two possible behaviors
(../ext rev 351.3.4)pull/4/head
parent
ec57bb3bb8
commit
ea3ec2acfd
|
@ -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,
|
||||||
|
in the format:
|
||||||
|
{ 'cols': [ 'col_a', 'col_b', ...]
|
||||||
, 'rows': [ (a0, b0, ...), (a1, b1, ...), ...] }
|
, '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):
|
||||||
|
|
|
@ -19,13 +19,13 @@
|
||||||
#
|
#
|
||||||
##############################################################################
|
##############################################################################
|
||||||
|
|
||||||
import os
|
|
||||||
import sys
|
import sys
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from osv import fields, osv
|
from osv import fields, osv
|
||||||
import logging
|
import logging
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
_loglvl = _logger.getEffectiveLevel()
|
_loglvl = _logger.getEffectiveLevel()
|
||||||
|
SEP = '|'
|
||||||
|
|
||||||
class import_odbc_dbtable(osv.osv):
|
class import_odbc_dbtable(osv.osv):
|
||||||
_name="import.odbc.dbtable"
|
_name="import.odbc.dbtable"
|
||||||
|
@ -50,7 +50,7 @@ class import_odbc_dbtable(osv.osv):
|
||||||
help = "On error try to reimport rows ignoring relationships."),
|
help = "On error try to reimport rows ignoring relationships."),
|
||||||
'raise_import_errors': fields.boolean('Raise import errors',
|
'raise_import_errors': fields.boolean('Raise import errors',
|
||||||
help = "Import errors not handled, intended for debugging purposes."
|
help = "Import errors not handled, intended for debugging purposes."
|
||||||
+ "\nAlso forces debug messages to be written to the serevr log."),
|
+ "\nAlso forces debug messages to be written to the server log."),
|
||||||
}
|
}
|
||||||
_defaults = {
|
_defaults = {
|
||||||
'enabled': True,
|
'enabled': True,
|
||||||
|
@ -80,24 +80,23 @@ class import_odbc_dbtable(osv.osv):
|
||||||
|
|
||||||
_logger.debug( data )
|
_logger.debug( data )
|
||||||
cols = list(flds) #copy to avoid side effects
|
cols = list(flds) #copy to avoid side effects
|
||||||
err = list()
|
errmsg = str()
|
||||||
msg = str()
|
|
||||||
if table_obj.raise_import_errors:
|
if table_obj.raise_import_errors:
|
||||||
model_obj.import_data(cr, uid, cols, [data], noupdate=table_obj.noupdate)
|
model_obj.import_data(cr, uid, cols, [data], noupdate=table_obj.noupdate)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
model_obj.import_data(cr, uid, cols, [data], noupdate=table_obj.noupdate)
|
model_obj.import_data(cr, uid, cols, [data], noupdate=table_obj.noupdate)
|
||||||
except:
|
except:
|
||||||
msg = str(sys.exc_info()[1])
|
errmsg = str(sys.exc_info()[1])
|
||||||
|
|
||||||
if err and not table_obj.ignore_rel_errors:
|
if errmsg and not table_obj.ignore_rel_errors:
|
||||||
#Fail
|
#Fail
|
||||||
append_to_log(log, 'ERROR', data, msg )
|
append_to_log(log, 'ERROR', data, errmsg )
|
||||||
log['last_error_count'] += 1
|
log['last_error_count'] += 1
|
||||||
return False
|
return False
|
||||||
if err and table_obj.ignore_rel_errors:
|
if errmsg and table_obj.ignore_rel_errors:
|
||||||
#Warn and retry ignoring many2one fields...
|
#Warn and retry ignoring many2one fields...
|
||||||
append_to_log(log, 'WARN', data, msg )
|
append_to_log(log, 'WARN', data, errmsg )
|
||||||
log['last_warn_count'] += 1
|
log['last_warn_count'] += 1
|
||||||
#Try ignoring each many2one (tip: in the SQL sentence select more problematic FKs first)
|
#Try ignoring each many2one (tip: in the SQL sentence select more problematic FKs first)
|
||||||
i = find_m2o(cols)
|
i = find_m2o(cols)
|
||||||
|
@ -147,11 +146,11 @@ class import_odbc_dbtable(osv.osv):
|
||||||
if obj.last_sync: sync = datetime.strptime(obj.last_sync, "%Y-%m-%d %H:%M:%S")
|
if obj.last_sync: sync = datetime.strptime(obj.last_sync, "%Y-%m-%d %H:%M:%S")
|
||||||
else: sync = datetime.datetime(1900, 1, 1, 0, 0, 0)
|
else: sync = datetime.datetime(1900, 1, 1, 0, 0, 0)
|
||||||
params = {'sync': sync}
|
params = {'sync': sync}
|
||||||
res = db_model.execute_and_inspect(cr, uid, [obj.dbsource_id.id], obj.sql_source, params)
|
res = db_model.execute(cr, uid, [obj.dbsource_id.id], obj.sql_source, params, metadata=True)
|
||||||
|
|
||||||
#Exclude columns titled "None"; add (xml_)"id" column
|
#Exclude columns titled "None"; add (xml_)"id" column
|
||||||
cidx = [i for i, x in enumerate(res['colnames']) if x.upper() != 'NONE']
|
cidx = [i for i, x in enumerate(res['cols']) if x.upper() != 'NONE']
|
||||||
cols = [x for i, x in enumerate(res['colnames']) if x.upper() != 'NONE'] + ['id']
|
cols = [x for i, x in enumerate(res['cols']) if x.upper() != 'NONE'] + ['id']
|
||||||
|
|
||||||
#Import each row:
|
#Import each row:
|
||||||
for row in res['rows']:
|
for row in res['rows']:
|
||||||
|
|
Loading…
Reference in New Issue