You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2011 Daniel Reis
  3. # Copyright 2016 LasLabs Inc.
  4. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  5. import logging
  6. from odoo import api, models
  7. _logger = logging.getLogger(__name__)
  8. try:
  9. from odoo.addons.base_external_dbsource.models import (
  10. base_external_dbsource,
  11. )
  12. CONNECTORS = base_external_dbsource.BaseExternalDbsource.CONNECTORS
  13. try:
  14. import sqlalchemy
  15. CONNECTORS.append(('sqlite', 'SQLite'))
  16. except ImportError:
  17. _logger.info('SQLAlchemy library not available. Please '
  18. 'install "sqlalchemy" python package.')
  19. except ImportError:
  20. _logger.info('base_external_dbsource Odoo module not found.')
  21. class BaseExternalDbsource(models.Model):
  22. """ It provides logic for connection to a SQLite data source. """
  23. _inherit = "base.external.dbsource"
  24. PWD_STRING_SQLITE = 'Password=%s;'
  25. @api.multi
  26. def connection_close_sqlite(self, connection):
  27. return connection.close()
  28. @api.multi
  29. def connection_open_sqlite(self):
  30. return self._connection_open_sqlalchemy()
  31. @api.multi
  32. def execute_sqlite(self, sqlquery, sqlparams, metadata):
  33. return self._execute_sqlalchemy(sqlquery, sqlparams, metadata)
  34. @api.multi
  35. def _connection_open_sqlalchemy(self):
  36. return sqlalchemy.create_engine(self.conn_string_full).connect()
  37. @api.multi
  38. def _execute_sqlalchemy(self, sqlquery, sqlparams, metadata):
  39. rows, cols = list(), list()
  40. for record in self:
  41. with record.connection_open() as connection:
  42. cur = connection.execute(sqlquery, sqlparams)
  43. if metadata:
  44. cols = cur.keys()
  45. rows = [r for r in cur]
  46. return rows, cols