diff --git a/base_external_dbsource_sap_hana/README.rst b/base_external_dbsource_sap_hana/README.rst new file mode 100644 index 00000000..e145416e --- /dev/null +++ b/base_external_dbsource_sap_hana/README.rst @@ -0,0 +1,105 @@ +===================================== +External Database Source - SAP - Hana +===================================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--backend-lightgray.png?logo=github + :target: https://github.com/OCA/server-backend/tree/15.0/base_external_dbsource_sap_hana + :alt: OCA/server-backend +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/server-backend-15-0/server-backend-15-0-base_external_dbsource_sap_hana + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/253/15.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module extends ``base_external_dbsource``, allowing you to connect to +foreign SAP HANA databases using SQLAlchemy. + +**Table of contents** + +.. contents:: + :local: + +Installation +============ + +To install this module, you need to: + +* Install ``sqlalchemy_hana`` and ``hdbcli`` python libraries + +Configuration +============= + +To configure this module, you need to: + +#. Database sources can be configured in Settings > Technical > + Database Structure > Database sources. + +Usage +===== + +To use this module: + +* Go to Settings > Technical > Database Structure > Database Sources +* Click on Create to enter the following information: + +* Datasource name?? +* Pasword +* Connector: Choose the database to which you want to connect +* Connection string: Specify how to connect to database + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Tecnativa + +Contributors +~~~~~~~~~~~~ + +* `Tecnativa `__: + + * Carlos Dauden + * Sergio Teruel + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/server-backend `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/base_external_dbsource_sap_hana/__init__.py b/base_external_dbsource_sap_hana/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/base_external_dbsource_sap_hana/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/base_external_dbsource_sap_hana/__manifest__.py b/base_external_dbsource_sap_hana/__manifest__.py new file mode 100644 index 00000000..c8e724c4 --- /dev/null +++ b/base_external_dbsource_sap_hana/__manifest__.py @@ -0,0 +1,14 @@ +# Copyright 2022 Tecnativa - Sergio Teruel +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +{ + "name": "External Database Source - SAP - Hana", + "version": "15.0.1.0.0", + "category": "Tools", + "author": "Tecnativa, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/server-backend", + "license": "LGPL-3", + "depends": ["base_external_dbsource"], + "external_dependencies": {"python": ["sqlalchemy", "sqlalchemy-hana", "hdbcli"]}, + "demo": ["demo/base_external_dbsource.xml"], + "installable": True, +} diff --git a/base_external_dbsource_sap_hana/demo/base_external_dbsource.xml b/base_external_dbsource_sap_hana/demo/base_external_dbsource.xml new file mode 100644 index 00000000..23fc1c5a --- /dev/null +++ b/base_external_dbsource_sap_hana/demo/base_external_dbsource.xml @@ -0,0 +1,9 @@ + + + + SAP Hana Demo + hana://username:password@example.de:30015 + password + sap_hana + + diff --git a/base_external_dbsource_sap_hana/models/__init__.py b/base_external_dbsource_sap_hana/models/__init__.py new file mode 100644 index 00000000..91032fcb --- /dev/null +++ b/base_external_dbsource_sap_hana/models/__init__.py @@ -0,0 +1 @@ +from . import base_external_dbsource diff --git a/base_external_dbsource_sap_hana/models/base_external_dbsource.py b/base_external_dbsource_sap_hana/models/base_external_dbsource.py new file mode 100644 index 00000000..d386558a --- /dev/null +++ b/base_external_dbsource_sap_hana/models/base_external_dbsource.py @@ -0,0 +1,37 @@ +# Copyright 2022 Tecnativa - Sergio Teruel +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + + +import sqlalchemy + +from odoo import models + +from odoo.addons.base_external_dbsource.models import base_external_dbsource + +base_external_dbsource.BaseExternalDbsource.CONNECTORS.append(("sap_hana", "SAP-Hana")) + + +class BaseExternalDbsource(models.Model): + """It provides logic for connection to a SAP Hana data source.""" + + _inherit = "base.external.dbsource" + + def connection_close_sap_hana(self, connection): + return connection.close() + + def connection_open_sap_hana(self): + return sqlalchemy.create_engine(self.conn_string_full).connect() + + def execute_sap_hana(self, sqlquery, sqlparams, metadata): + # FIXME: Duplicated method in modules to be consolidated in base + rows, cols = list(), list() + for record in self: + with record.connection_open() as connection: + if sqlparams is None: + cur = connection.execute(sqlquery) + else: + cur = connection.execute(sqlquery, sqlparams) + if metadata: + cols = list(cur.keys()) + rows = [r for r in cur] + return rows, cols diff --git a/base_external_dbsource_sap_hana/readme/CONFIGURE.rst b/base_external_dbsource_sap_hana/readme/CONFIGURE.rst new file mode 100644 index 00000000..f8fb7dd3 --- /dev/null +++ b/base_external_dbsource_sap_hana/readme/CONFIGURE.rst @@ -0,0 +1,4 @@ +To configure this module, you need to: + +#. Database sources can be configured in Settings > Technical > + Database Structure > Database sources. diff --git a/base_external_dbsource_sap_hana/readme/CONTRIBUTORS.rst b/base_external_dbsource_sap_hana/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..46fa878c --- /dev/null +++ b/base_external_dbsource_sap_hana/readme/CONTRIBUTORS.rst @@ -0,0 +1,4 @@ +* `Tecnativa `__: + + * Carlos Dauden + * Sergio Teruel diff --git a/base_external_dbsource_sap_hana/readme/DESCRIPTION.rst b/base_external_dbsource_sap_hana/readme/DESCRIPTION.rst new file mode 100644 index 00000000..f9f7b12d --- /dev/null +++ b/base_external_dbsource_sap_hana/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +This module extends ``base_external_dbsource``, allowing you to connect to +foreign SAP HANA databases using SQLAlchemy. diff --git a/base_external_dbsource_sap_hana/readme/INSTALL.rst b/base_external_dbsource_sap_hana/readme/INSTALL.rst new file mode 100644 index 00000000..4a976e6f --- /dev/null +++ b/base_external_dbsource_sap_hana/readme/INSTALL.rst @@ -0,0 +1,3 @@ +To install this module, you need to: + +* Install ``sqlalchemy_hana`` and ``hdbcli`` python libraries diff --git a/base_external_dbsource_sap_hana/readme/USAGE.rst b/base_external_dbsource_sap_hana/readme/USAGE.rst new file mode 100644 index 00000000..e91b212b --- /dev/null +++ b/base_external_dbsource_sap_hana/readme/USAGE.rst @@ -0,0 +1,9 @@ +To use this module: + +* Go to Settings > Technical > Database Structure > Database Sources +* Click on Create to enter the following information: + +* Datasource name?? +* Pasword +* Connector: Choose the database to which you want to connect +* Connection string: Specify how to connect to database diff --git a/base_external_dbsource_sap_hana/static/description/icon.png b/base_external_dbsource_sap_hana/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/base_external_dbsource_sap_hana/static/description/icon.png differ diff --git a/base_external_dbsource_sap_hana/static/description/index.html b/base_external_dbsource_sap_hana/static/description/index.html new file mode 100644 index 00000000..9f920442 --- /dev/null +++ b/base_external_dbsource_sap_hana/static/description/index.html @@ -0,0 +1,454 @@ + + + + + + +External Database Source - SAP - Hana + + + +
+

External Database Source - SAP - Hana

+ + +

Beta License: LGPL-3 OCA/server-backend Translate me on Weblate Try me on Runbot

+

This module extends base_external_dbsource, allowing you to connect to +foreign SAP HANA databases using SQLAlchemy.

+

Table of contents

+ +
+

Installation

+

To install this module, you need to:

+
    +
  • Install sqlalchemy_hana and hdbcli python libraries
  • +
+
+
+

Configuration

+

To configure this module, you need to:

+
    +
  1. Database sources can be configured in Settings > Technical > +Database Structure > Database sources.
  2. +
+
+
+

Usage

+

To use this module:

+
    +
  • Go to Settings > Technical > Database Structure > Database Sources
  • +
  • Click on Create to enter the following information:
  • +
  • Datasource name??
  • +
  • Pasword
  • +
  • Connector: Choose the database to which you want to connect
  • +
  • Connection string: Specify how to connect to database
  • +
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Tecnativa
  • +
+
+
+

Contributors

+
    +
  • Tecnativa:
      +
    • Carlos Dauden
    • +
    • Sergio Teruel
    • +
    +
  • +
+
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/server-backend project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/base_external_dbsource_sap_hana/tests/__init__.py b/base_external_dbsource_sap_hana/tests/__init__.py new file mode 100644 index 00000000..c48ef497 --- /dev/null +++ b/base_external_dbsource_sap_hana/tests/__init__.py @@ -0,0 +1 @@ +from . import test_base_external_dbsource_sap_hana diff --git a/base_external_dbsource_sap_hana/tests/test_base_external_dbsource_sap_hana.py b/base_external_dbsource_sap_hana/tests/test_base_external_dbsource_sap_hana.py new file mode 100644 index 00000000..68904494 --- /dev/null +++ b/base_external_dbsource_sap_hana/tests/test_base_external_dbsource_sap_hana.py @@ -0,0 +1,56 @@ +# Copyright 2022 Tecnativa - Sergio Teruel +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + +from unittest import mock + +from odoo.tests import common + +ADAPTER = "odoo.addons.base_external_dbsource_sap_hana.models.base_external_dbsource.SAPhanaDB" + + +class TestBaseExternalDbsource(common.TransactionCase): + def setUp(self): + super(TestBaseExternalDbsource, self).setUp() + self.dbsource = self.env.ref("base_external_dbsource_sap_hana.demo_sap_hana") + + def _test_adapter_method( + self, + method_name, + side_effect=None, + return_value=None, + create=False, + args=None, + kwargs=None, + ): + if args is None: + args = [] + if kwargs is None: + kwargs = {} + adapter = "%s_sap_hana" % method_name + with mock.patch.object(type(self.dbsource), adapter, create=create) as adapter: + if side_effect is not None: + adapter.side_effect = side_effect + elif return_value is not None: + adapter.return_value = return_value + res = getattr(self.dbsource, method_name)(*args, **kwargs) + return res, adapter + + def test_connection_close_sap_hana(self): + """It should close the connection""" + args = [mock.MagicMock()] + res, adapter = self._test_adapter_method("connection_close", args=args) + adapter.assert_called_once_with(args[0]) + + def test_connection_open_sap_hana(self): + """It should call SQLAlchemy open""" + with mock.patch.object(type(self.dbsource), "connection_open") as connection: + res = self.dbsource.conn_open() + self.assertEqual(res, connection().__enter__()) + + def test_excecute_sap_hana(self): + """It should pass args to SQLAlchemy execute""" + expect = "sqlquery", "sqlparams", "metadata" + with mock.patch.object(type(self.dbsource), "execute_sap_hana") as execute: + execute.return_value = "rows", "cols" + self.dbsource.execute(*expect) + execute.assert_called_once_with(*expect) diff --git a/requirements.txt b/requirements.txt index 09c20b17..f3a98949 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,6 @@ # generated from manifests external_dependencies +hdbcli mysqlclient pymssql<=2.2.5 sqlalchemy +sqlalchemy-hana diff --git a/setup/base_external_dbsource_sap_hana/odoo/addons/base_external_dbsource_sap_hana b/setup/base_external_dbsource_sap_hana/odoo/addons/base_external_dbsource_sap_hana new file mode 120000 index 00000000..7e4a0a71 --- /dev/null +++ b/setup/base_external_dbsource_sap_hana/odoo/addons/base_external_dbsource_sap_hana @@ -0,0 +1 @@ +../../../../base_external_dbsource_sap_hana \ No newline at end of file diff --git a/setup/base_external_dbsource_sap_hana/setup.py b/setup/base_external_dbsource_sap_hana/setup.py new file mode 100644 index 00000000..28c57bb6 --- /dev/null +++ b/setup/base_external_dbsource_sap_hana/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)