|
|
@ -20,10 +20,10 @@ |
|
|
|
############################################################################## |
|
|
|
|
|
|
|
import os |
|
|
|
from osv import fields, osv |
|
|
|
import logging |
|
|
|
from openerp.osv import orm, fields |
|
|
|
from openerp.tools.translate import _ |
|
|
|
import openerp.tools as tools |
|
|
|
import logging |
|
|
|
_logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
CONNECTORS = [] |
|
|
@ -33,26 +33,30 @@ try: |
|
|
|
import pymssql |
|
|
|
CONNECTORS.append(('mssql', 'Microsoft SQL Server')) |
|
|
|
except: |
|
|
|
_logger.info('MS SQL Server not available. Please install "slqalchemy" and "pymssql" python package.') |
|
|
|
_logger.info('MS SQL Server not available. Please install "slqalchemy"\ |
|
|
|
and "pymssql" python package.') |
|
|
|
|
|
|
|
try: |
|
|
|
import sqlalchemy |
|
|
|
import MySQLdb |
|
|
|
CONNECTORS.append(('mysql', 'MySQL')) |
|
|
|
except: |
|
|
|
_logger.info('MySQL not available. Please install "slqalchemy" and "mysqldb" python package.') |
|
|
|
_logger.info('MySQL not available. Please install "slqalchemy" and\ |
|
|
|
"mysqldb" python package.') |
|
|
|
|
|
|
|
try: |
|
|
|
import pyodbc |
|
|
|
CONNECTORS.append(('pyodbc', 'ODBC')) |
|
|
|
except: |
|
|
|
_logger.info('ODBC libraries not available. Please install "unixodbc" and "python-pyodbc" packages.') |
|
|
|
_logger.info('ODBC libraries not available. Please install "unixodbc"\ |
|
|
|
and "python-pyodbc" packages.') |
|
|
|
|
|
|
|
try: |
|
|
|
import cx_Oracle |
|
|
|
CONNECTORS.append(('cx_Oracle', 'Oracle')) |
|
|
|
except: |
|
|
|
_logger.info('Oracle libraries not available. Please install "cx_Oracle" python package.') |
|
|
|
_logger.info('Oracle libraries not available. Please install "cx_Oracle"\ |
|
|
|
python package.') |
|
|
|
|
|
|
|
import psycopg2 |
|
|
|
CONNECTORS.append(('postgresql', 'PostgreSQL')) |
|
|
@ -61,26 +65,33 @@ try: |
|
|
|
import sqlalchemy |
|
|
|
CONNECTORS.append(('sqlite', 'SQLite')) |
|
|
|
except: |
|
|
|
_logger.info('SQLAlchemy not available. Please install "slqalchemy" python package.') |
|
|
|
_logger.info('SQLAlchemy not available. Please install "slqalchemy" python\ |
|
|
|
package.') |
|
|
|
|
|
|
|
|
|
|
|
class base_external_dbsource(osv.osv): |
|
|
|
class base_external_dbsource(orm.Model): |
|
|
|
_name = "base.external.dbsource" |
|
|
|
_description = 'External Database Sources' |
|
|
|
_columns = { |
|
|
|
'name': fields.char('Datasource name', required=True, size=64), |
|
|
|
'conn_string': fields.text('Connection string', help="""\ |
|
|
|
'conn_string': fields.text('Connection string', help=""" |
|
|
|
Sample connection strings: |
|
|
|
- Microsoft SQL Server: mssql+pymssql://username:%s@server:port/dbname?charset=utf8 |
|
|
|
- Microsoft SQL Server: |
|
|
|
mssql+pymssql://username:%s@server:port/dbname?charset=utf8 |
|
|
|
- MySQL: mysql://user:%s@server:port/dbname |
|
|
|
- ODBC: DRIVER={FreeTDS};SERVER=server.address;Database=mydb;UID=sa |
|
|
|
- ORACLE: username/%s@//server.address:port/instance |
|
|
|
- PostgreSQL: dbname='template1' user='dbuser' host='localhost' port='5432' password=%s |
|
|
|
- PostgreSQL: |
|
|
|
dbname='template1' user='dbuser' host='localhost' port='5432' password=%s |
|
|
|
- SQLite: sqlite:///test.db |
|
|
|
"""), |
|
|
|
'password': fields.char('Password', size=40), |
|
|
|
'connector': fields.selection(CONNECTORS, 'Connector', required=True, |
|
|
|
help = "If a connector is missing from the list, check the " \ |
|
|
|
+ "server log to confirm that the required componentes were detected."), |
|
|
|
'connector': fields.selection(CONNECTORS, 'Connector', |
|
|
|
required=True, |
|
|
|
help="If a connector is missing from the\ |
|
|
|
list, check the server log to confirm\ |
|
|
|
that the required components were\ |
|
|
|
detected."), |
|
|
|
} |
|
|
|
|
|
|
|
def conn_open(self, cr, uid, id1): |
|
|
@ -105,17 +116,21 @@ Sample connection strings: |
|
|
|
|
|
|
|
return conn |
|
|
|
|
|
|
|
def execute(self, cr, uid, ids, sqlquery, sqlparams=None, metadata=False, context=None): |
|
|
|
def execute(self, cr, uid, ids, sqlquery, sqlparams=None, metadata=False, |
|
|
|
context=None): |
|
|
|
"""Executes SQL and returns a list of rows. |
|
|
|
|
|
|
|
"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". |
|
|
|
"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". |
|
|
|
Example: |
|
|
|
sqlquery = "select * from mytable where city = %(city)s and date > %(dt)s" |
|
|
|
params = {'city': 'Lisbon', 'dt': datetime.datetime(2000, 12, 31)} |
|
|
|
sqlquery = "select * from mytable where city = %(city)s and |
|
|
|
date > %(dt)s" |
|
|
|
params = {'city': 'Lisbon', |
|
|
|
'dt': datetime.datetime(2000, 12, 31)} |
|
|
|
|
|
|
|
If metadata=True, it will instead return a dict containing the rows list and the columns list, |
|
|
|
in the format: |
|
|
|
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, ...), ...] } |
|
|
|
""" |
|
|
@ -126,13 +141,15 @@ Sample connection strings: |
|
|
|
if obj.connector in ["sqlite", "mysql", "mssql"]: |
|
|
|
#using sqlalchemy |
|
|
|
cur = conn.execute(sqlquery, sqlparams) |
|
|
|
if metadata: cols = cur.keys() |
|
|
|
if metadata: |
|
|
|
cols = cur.keys() |
|
|
|
rows = [r for r in cur] |
|
|
|
else: |
|
|
|
#using other db connectors |
|
|
|
cur = conn.cursor() |
|
|
|
cur.execute(sqlquery, sqlparams) |
|
|
|
if metadata: cols = [d[0] for d in cur.description] |
|
|
|
if metadata: |
|
|
|
cols = [d[0] for d in cur.description] |
|
|
|
rows = cur.fetchall() |
|
|
|
conn.close() |
|
|
|
if metadata: |
|
|
@ -146,14 +163,18 @@ Sample connection strings: |
|
|
|
try: |
|
|
|
conn = self.conn_open(cr, uid, obj.id) |
|
|
|
except Exception, e: |
|
|
|
raise osv.except_osv(_("Connection test failed!"), _("Here is what we got instead:\n %s") % tools.ustr(e)) |
|
|
|
raise osv.except_osv(_("Connection test failed!"), |
|
|
|
_("Here is what we got instead:\n %s") |
|
|
|
% tools.ustr(e)) |
|
|
|
finally: |
|
|
|
try: |
|
|
|
if conn: conn.close() |
|
|
|
if conn: |
|
|
|
conn.close() |
|
|
|
except Exception: |
|
|
|
# ignored, just a consequence of the previous exception |
|
|
|
pass |
|
|
|
#TODO: if OK a (wizard) message box should be displayed |
|
|
|
raise osv.except_osv(_("Connection test succeeded!"), _("Everything seems properly set up!")) |
|
|
|
raise osv.except_osv(_("Connection test succeeded!"), |
|
|
|
_("Everything seems properly set up!")) |
|
|
|
|
|
|
|
base_external_dbsource() |
|
|
|
#EOF |