Skip to content

Commit e086904

Browse files
authored
add oracledb operator (#23)
* init oracledb * update cicd * add vscode settings
1 parent 8589ab4 commit e086904

26 files changed

Lines changed: 319 additions & 765 deletions

.github/workflows/pip.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ on:
1313
# Allows you to run this workflow manually from the Actions tab
1414
workflow_dispatch:
1515

16-
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
16+
1717
jobs:
18-
# This workflow contains a single job called "build"
19-
build:
18+
build-pypi-package:
2019
# The type of runner that the job will run on
2120
runs-on: ubuntu-latest
2221

23-
# Steps represent a sequence of tasks that will be executed as part of the job
2422
steps:
2523
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
2624
# sudo python setup.py install clean --all

.vscode/extensions.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"recommendations": [
3+
"charliermarsh.ruff",
4+
"dbcode.dbcode",
5+
"esbenp.prettier-vscode",
6+
"ms-python.python",
7+
"ms-python.vscode-pylance"
8+
],
9+
"unwantedRecommendations": []
10+
}

.vscode/settings.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"editor.formatOnSave": true,
3+
"files.exclude": {
4+
"**/.git": true,
5+
"**/node_modules": true,
6+
"**/__pycache__": true,
7+
"**/*.pyc": true
8+
},
9+
"[python]": {
10+
"editor.defaultFormatter": "ms-python.autopep8",
11+
"editor.codeActionsOnSave": {
12+
"source.organizeImports": "explicit",
13+
"source.fixAll.ruff": "explicit"
14+
}
15+
},
16+
"[sql]": {
17+
"editor.defaultFormatter": "dbcode.dbcode", // mtxr.sqltools, dbcode.dbcode, ReneSaarsoo.sql-formatter-vsc
18+
"editor.formatOnSave": true
19+
},
20+
"ruff.lineLength": 128,
21+
"ruff.configuration": {
22+
"format": {
23+
"quote-style": "double"
24+
}
25+
}
26+
}

demo/app_common/ainlp/__init__.py

Whitespace-only changes.

demo/app_common/ainlp/model_bert.py

Lines changed: 0 additions & 86 deletions
This file was deleted.

demo/app_common/ainlp/test-gpu-async.py

Whitespace-only changes.

demo/main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import importlib
2+
import sys
3+
4+
usage = """
5+
Usage: python3 main.py module.name
6+
the `module.name` should be a python package file or package which include a `main()` function
7+
"""
8+
9+
if len(sys.argv) < 2:
10+
print(usage)
11+
exit(-1)
12+
13+
m = importlib.import_module(sys.argv[1])
14+
f_main = getattr(m, "main")
15+
16+
if f_main is None:
17+
print("Given module does not provides a `main()` function!")
18+
else:
19+
f_main()

demo/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
aloha[all]

src/aloha/db/oracle.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
__all__ = ('OracledbOperator',)
2+
3+
import oracledb
4+
from sqlalchemy import create_engine
5+
from sqlalchemy.sql import text
6+
7+
from .base import PasswordVault
8+
from ..logger import LOG
9+
10+
LOG.debug("oracledb version = %s" % oracledb.__version__)
11+
12+
13+
class OracledbOperator:
14+
def __init__(self, db_config, **kwargs):
15+
"""example of db_config:
16+
{
17+
"host": "192.168.1.100",
18+
"port": 1521,
19+
"user": "PT_INDEX",
20+
"password": "vault_key_or_plain",
21+
"service_name": "orcl", # 推荐使用 service_name
22+
"sid": "orcl", # 或使用 sid
23+
"vault_type": "...",
24+
"vault_config": {...},
25+
"lib_dir": "/opt/oracle/instantclient" # optional, use THICK mode if defined.
26+
}
27+
"""
28+
29+
password_vault = PasswordVault.get_vault(db_config.get('vault_type'), db_config.get('vault_config'))
30+
self._config = {
31+
'host': db_config['host'],
32+
'port': db_config['port'],
33+
'user': db_config['user'],
34+
'password': password_vault.get_password(db_config.get('password')),
35+
}
36+
37+
if 'lib_dir' in db_config: # use Thick mode
38+
try:
39+
oracledb.init_oracle_client(lib_dir=db_config['lib_dir'])
40+
LOG.info("Oracle client initialized in THICK mode from: %s" % db_config["lib_dir"])
41+
except Exception as e:
42+
LOG.warning(f"Warning: {e}")
43+
raise RuntimeError(f"Failed to initialize Oracle client: {e}")
44+
45+
service_name = db_config.get("service_name")
46+
sid = db_config.get("sid")
47+
48+
if service_name: # using service_name (recommended)
49+
dsn = oracledb.makedsn(db_config["host"], db_config["port"], service_name=service_name)
50+
elif sid: # using SID
51+
dsn = oracledb.makedsn(db_config["host"], db_config["port"], sid=sid)
52+
else:
53+
raise ValueError("Oracle config must specify service_name or sid")
54+
55+
self._config["dsn"] = dsn
56+
try:
57+
self.engine = create_engine(
58+
"oracle+oracledb://{user}:{password}@".format(**self._config),
59+
connect_args={"dsn": dsn},
60+
pool_size=20, max_overflow=10, pool_pre_ping=True, **kwargs
61+
)
62+
msg = "OracleDB connected: {host}:{port}".format(**self._config)
63+
print(msg)
64+
except Exception as e:
65+
LOG.error(e)
66+
raise RuntimeError(f"Failed to connect to OracleDB")
67+
68+
@property
69+
def connection(self):
70+
return self.engine
71+
72+
def execute_query(self, sql, *args, **kwargs):
73+
with self.engine.connect() as conn:
74+
cur = conn.execute(text(sql), *args, **kwargs)
75+
return cur
76+
77+
@property
78+
def connection_str(self) -> str:
79+
return "oracle://{user}@{host}:{port}".format(**self._config)

src/aloha/db/postgres.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(self, db_config, **kwargs):
3333
LOG.debug("PostgresSQL connected: {host}:{port}/{dbname}".format(**self._config))
3434
except Exception as e:
3535
LOG.error(e)
36-
raise RuntimeError('Failed to connect to PostgresSQL')
36+
raise RuntimeError("Failed to connect to PostgresSQL")
3737

3838
@property
3939
def connection(self):

0 commit comments

Comments
 (0)