Skip to content

Commit 42c574d

Browse files
committed
feat: changed directory structure for ansible yaml files
1 parent e96a9fd commit 42c574d

File tree

1 file changed

+88
-35
lines changed

1 file changed

+88
-35
lines changed

telemetry_f1_2021/oracledb.py

Lines changed: 88 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
1-
import cx_Oracle
2-
import yaml
31
import os
42
from pathlib import Path
5-
home = str(Path.home())
3+
from dotenv import load_dotenv
4+
import oracledb
65

7-
def process_yaml():
8-
with open("../config.yaml") as file:
9-
return yaml.safe_load(file)
6+
load_dotenv()
107

8+
home = str(Path.home())
119

12-
class OracleJSONDatabaseConnection:
13-
def __init__(self, data=process_yaml()):
14-
# wallet location (default is HOME/wallets/wallet_X)
15-
os.environ['TNS_ADMIN'] = '{}/{}'.format(home, process_yaml()['WALLET_DIR'])
16-
print(os.environ['TNS_ADMIN'])
17-
self.pool = cx_Oracle.SessionPool(data['db']['username'], data['db']['password'], data['db']['dsn'],
18-
min=1, max=4, increment=1, threaded=True,
19-
getmode=cx_Oracle.SPOOL_ATTRVAL_WAIT
20-
)
21-
print('Connection successful.')
10+
oracledb_user = os.getenv("DB_USER")
11+
oracledb_password = os.getenv("DB_PASSWORD")
12+
oracledb_connection_string = os.getenv("CONNECTION_STRING")
13+
instant_client_lib_dir = os.getenv("INSTANT_CLIENT_LIB_DIR")
2214

2315

16+
class OracleJSONDatabaseThinConnection:
17+
def __init__(self):
18+
self.pool = oracledb.create_pool(user=oracledb_user, password=oracledb_password, dsn=oracledb_connection_string,
19+
min=1, max=4, increment=1, getmode=oracledb.POOL_GETMODE_WAIT)
20+
print('Connection successful.')
2421

2522
def close_pool(self):
2623
self.pool.close()
2724
print('Connection pool closed.')
2825

29-
30-
3126
def insert(self, collection_name, json_object_to_insert):
3227
connection = self.pool.acquire()
3328
connection.autocommit = True
@@ -36,52 +31,110 @@ def insert(self, collection_name, json_object_to_insert):
3631

3732
try:
3833
x_collection.insertOne(json_object_to_insert)
39-
#print('[DBG] INSERT {} OK'.format(json_object_to_insert))
40-
except cx_Oracle.IntegrityError:
41-
#print('[DBG] INSERT {} ERR'.format(json_object_to_insert))
42-
return 0
43-
except cx_Oracle.DatabaseError:
44-
print('PACKET STRUCTURAL ERROR. SKIPPING...')
34+
print('[DBG] INSERT {} OK'.format(json_object_to_insert))
35+
except cx_Oracle.IntegrityError as e:
36+
print('[DBG] INSERT {} ERR: {} '.format(json_object_to_insert, e))
4537
return -1
4638
self.pool.release(connection)
4739
return 1
4840

49-
50-
5141
def delete(self, collection_name, on_column, on_value):
5242
connection = self.pool.acquire()
5343
connection.autocommit = True
5444
soda = connection.getSodaDatabase()
5545
x_collection = soda.createCollection(collection_name)
5646
qbe = {on_column: on_value}
57-
print('Found {} elements'.format(x_collection.find().filter(qbe).count()))
58-
res = x_collection.find().filter(qbe).remove()
59-
print('Removed {} documents'.format(res))
47+
x_collection.find().filter(qbe).remove()
6048
self.pool.release(connection)
6149

50+
def get_connection(self):
51+
connection = self.pool.acquire()
52+
connection.autocommit = True
53+
return connection
54+
55+
def close_connection(self, conn_object):
56+
self.pool.release(conn_object)
57+
58+
def get_collection_names(self):
59+
connection = self.pool.acquire()
60+
connection.autocommit = True
61+
returning_object = connection.getSodaDatabase(
62+
).getCollectionNames(startName=None, limit=0)
63+
self.pool.release(connection)
64+
return returning_object
65+
66+
def open_collection(self, collection_name):
67+
connection = self.pool.acquire()
68+
returning_object = self.pool.acquire(
69+
).getSodaDatabase().openCollection(collection_name)
70+
self.pool.release(connection)
71+
return returning_object
72+
73+
74+
class OracleJSONDatabaseThickConnection:
75+
def __init__(self):
76+
oracledb.init_oracle_client(lib_dir=instant_client_lib_dir)
77+
self.pool = oracledb.create_pool(user=oracledb_user, password=oracledb_password, dsn=oracledb_connection_string,
78+
min=1, max=4, increment=1, getmode=oracledb.POOL_GETMODE_WAIT)
79+
print('Connection successful.')
80+
81+
def close_pool(self):
82+
self.pool.close()
83+
print('Connection pool closed.')
84+
85+
def insert(self, collection_name, json_object_to_insert):
86+
connection = self.pool.acquire()
87+
connection.autocommit = True
88+
soda = connection.getSodaDatabase()
89+
x_collection = soda.createCollection(collection_name)
6290

63-
def get_count(self, collection_name):
91+
try:
92+
x_collection.insertOne(json_object_to_insert)
93+
print('[DBG] INSERT {} OK'.format(json_object_to_insert))
94+
except cx_Oracle.IntegrityError as e:
95+
print('[DBG] INSERT {} ERR: {} '.format(json_object_to_insert, e))
96+
return -1
97+
self.pool.release(connection)
98+
return 1
99+
100+
def delete(self, collection_name, on_column, on_value):
64101
connection = self.pool.acquire()
102+
connection.autocommit = True
65103
soda = connection.getSodaDatabase()
66104
x_collection = soda.createCollection(collection_name)
67-
count = x_collection.find().count()
68-
print('{}'.format(count))
105+
qbe = {on_column: on_value}
106+
x_collection.find().filter(qbe).remove()
69107
self.pool.release(connection)
70-
return count
71108

72109
def get_connection(self):
73110
connection = self.pool.acquire()
74111
connection.autocommit = True
75112
return connection
76113

114+
def close_connection(self, conn_object):
115+
self.pool.release(conn_object)
116+
117+
def get_collection_names(self):
118+
connection = self.pool.acquire()
119+
connection.autocommit = True
120+
returning_object = connection.getSodaDatabase(
121+
).getCollectionNames(startName=None, limit=0)
122+
self.pool.release(connection)
123+
return returning_object
124+
125+
def open_collection(self, collection_name):
126+
connection = self.pool.acquire()
127+
returning_object = self.pool.acquire(
128+
).getSodaDatabase().openCollection(collection_name)
129+
self.pool.release(connection)
130+
return returning_object
77131

78132

79133
def test_class():
80-
object = OracleJSONDatabaseConnection()
134+
object = OracleJSONDatabaseThickConnection()
81135
print(object.pool)
82136
object.close_pool()
83137

84138

85-
86139
if __name__ == '__main__':
87140
test_class()

0 commit comments

Comments
 (0)