Re-org folders, onboard test folder, test config
[optf/osdf.git] / osdf / adapters / database / VerticaDB.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2015-2017 AT&T Intellectual Property
3 #
4 #   Licensed under the Apache License, Version 2.0 (the "License");
5 #   you may not use this file except in compliance with the License.
6 #   You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #   Unless required by applicable law or agreed to in writing, software
11 #   distributed under the License is distributed on an "AS IS" BASIS,
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #   See the License for the specific language governing permissions and
14 #   limitations under the License.
15 #
16 # -------------------------------------------------------------------------
17 #
18
19 import jaydebeapi
20 import sqlalchemy.pool as pool
21
22 from jaydebeapi import _DEFAULT_CONVERTERS, _java_to_py
23 from osdf.utils.programming_utils import MetaSingleton
24 from osdf.config.base import osdf_config
25
26 _DEFAULT_CONVERTERS.update({'BIGINT': _java_to_py('longValue')})
27
28
29 class VerticaDB(metaclass=MetaSingleton):
30     connection_pool = None
31
32     def get_connection(self):
33         p = self.get_config_params()
34         c = jaydebeapi.connect(
35             'com.vertica.jdbc.Driver',
36             'jdbc:vertica://{}:{}/{}'.format(p['host'], p['port'], p['db']),
37             {'user': p['user'], 'password': p['passwd'], 'CHARSET': 'UTF8'},
38             jars=[p['db_driver']]
39         )
40         return c
41
42     def get_config_params(self):
43         config = osdf_config["deployment"]
44         host, port, db = config["verticaHost"], config["verticaPort"], config.get("verticaDB")
45         user, passwd = config["verticaUsername"], config["verticaPassword"]
46         jar_path = osdf_config['core']['osdf_system']['vertica_jar']
47         params = dict(host=host, db=db, user=user, passwd=passwd, port=port, db_driver=jar_path)
48         return params
49
50     def connect(self):
51         if self.connection_pool is None:
52             self.connection_pool = pool.QueuePool(self.get_connection, max_overflow=10, pool_size=5, recycle=600)
53         conn = self.connection_pool.connect()
54         cursor = conn.cursor()
55         return conn, cursor