oceanbase-sqlalchemy 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- oceanbase_sqlalchemy/__init__.py +20 -0
- oceanbase_sqlalchemy/cx_oceanbase.py +15 -0
- oceanbase_sqlalchemy/cx_oracle.py +154 -0
- oceanbase_sqlalchemy/requirements.py +202 -0
- oceanbase_sqlalchemy/utils.py +42 -0
- oceanbase_sqlalchemy-0.2.0.dist-info/METADATA +182 -0
- oceanbase_sqlalchemy-0.2.0.dist-info/RECORD +16 -0
- oceanbase_sqlalchemy-0.2.0.dist-info/WHEEL +5 -0
- oceanbase_sqlalchemy-0.2.0.dist-info/entry_points.txt +3 -0
- oceanbase_sqlalchemy-0.2.0.dist-info/licenses/LICENSE +201 -0
- oceanbase_sqlalchemy-0.2.0.dist-info/top_level.txt +2 -0
- test/__init__.py +4 -0
- test/conftest.py +15 -0
- test/test_basic.py +91 -0
- test/test_optimization.py +203 -0
- test/test_suite.py +50 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
SQLAlchemy dialect for OceanBase Oracle mode.
|
|
4
|
+
|
|
5
|
+
This dialect extends SQLAlchemy's Oracle dialect with OceanBase-specific
|
|
6
|
+
optimizations and features for cx_oracle driver.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
# Import the cx_oracle dialect class
|
|
10
|
+
from .cx_oracle import OceanBaseDialect_cx_oracle
|
|
11
|
+
from .cx_oceanbase import OceanBaseDialect_cx_oceanbase
|
|
12
|
+
|
|
13
|
+
# Manually register the dialect for SQLAlchemy 2.0 compatibility
|
|
14
|
+
try:
|
|
15
|
+
from sqlalchemy.dialects import registry
|
|
16
|
+
|
|
17
|
+
registry.impls["oceanbase.cx_oracle"] = lambda: OceanBaseDialect_cx_oracle
|
|
18
|
+
registry.impls["oceanbase.cx_oceanbase"] = lambda: OceanBaseDialect_cx_oceanbase
|
|
19
|
+
except ImportError:
|
|
20
|
+
pass
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
OceanBase dialect for cx_oceanbase driver.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from .cx_oracle import OceanBaseDialect_cx_oracle
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class OceanBaseDialect_cx_oceanbase(OceanBaseDialect_cx_oracle):
|
|
10
|
+
"""
|
|
11
|
+
OceanBase dialect for cx_oracle driver.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
name = "oceanbase"
|
|
15
|
+
driver = "cx_oceanbase"
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
OceanBase dialect for cx_oracle driver.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from sqlalchemy.dialects.oracle.cx_oracle import OracleDialect_cx_oracle
|
|
7
|
+
from sqlalchemy import select, and_, or_, bindparam, sql
|
|
8
|
+
from functools import lru_cache
|
|
9
|
+
from sqlalchemy.engine import reflection
|
|
10
|
+
import urllib.parse
|
|
11
|
+
|
|
12
|
+
# SQLAlchemy version compatibility
|
|
13
|
+
import sqlalchemy
|
|
14
|
+
|
|
15
|
+
SA_VERSION = sqlalchemy.__version__
|
|
16
|
+
SA_20_PLUS = SA_VERSION.startswith("2.")
|
|
17
|
+
|
|
18
|
+
if SA_20_PLUS:
|
|
19
|
+
from sqlalchemy.dialects.oracle import dictionary
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class OceanBaseDialect_cx_oracle(OracleDialect_cx_oracle):
|
|
23
|
+
"""
|
|
24
|
+
OceanBase dialect for cx_oracle driver.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
name = "oceanbase"
|
|
28
|
+
driver = "cx_oracle"
|
|
29
|
+
|
|
30
|
+
def __init__(self, **kwargs):
|
|
31
|
+
super().__init__(**kwargs)
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def _supports_except_all(self):
|
|
35
|
+
return False
|
|
36
|
+
|
|
37
|
+
def create_connect_args(self, url):
|
|
38
|
+
if SA_20_PLUS:
|
|
39
|
+
username = url.username
|
|
40
|
+
password = url.password
|
|
41
|
+
|
|
42
|
+
if username and "%" in username:
|
|
43
|
+
username = urllib.parse.unquote(username)
|
|
44
|
+
if password and "%" in password:
|
|
45
|
+
password = urllib.parse.unquote(password)
|
|
46
|
+
|
|
47
|
+
if username or password:
|
|
48
|
+
from sqlalchemy.engine import make_url
|
|
49
|
+
|
|
50
|
+
new_url = make_url(str(url))
|
|
51
|
+
if username:
|
|
52
|
+
new_url = new_url.set(username=username)
|
|
53
|
+
if password:
|
|
54
|
+
new_url = new_url.set(password=password)
|
|
55
|
+
url = new_url
|
|
56
|
+
|
|
57
|
+
return super().create_connect_args(url)
|
|
58
|
+
|
|
59
|
+
@reflection.cache
|
|
60
|
+
def _get_constraint_data(
|
|
61
|
+
self, connection, table_name, schema=None, dblink="", **kw
|
|
62
|
+
):
|
|
63
|
+
params = {"table_name": table_name}
|
|
64
|
+
|
|
65
|
+
text = (
|
|
66
|
+
"SELECT /*+opt_param('XSOLAPI_GENERATE_WITH_CLAUSE','false') */"
|
|
67
|
+
"\nac.constraint_name,"
|
|
68
|
+
"\nac.constraint_type,"
|
|
69
|
+
"\nloc.column_name AS local_column,"
|
|
70
|
+
"\nrem.table_name AS remote_table,"
|
|
71
|
+
"\nrem.column_name AS remote_column,"
|
|
72
|
+
"\nrem.owner AS remote_owner,"
|
|
73
|
+
"\nloc.position as loc_pos,"
|
|
74
|
+
"\nrem.position as rem_pos,"
|
|
75
|
+
"\nac.search_condition,"
|
|
76
|
+
"\nac.delete_rule"
|
|
77
|
+
"\nFROM all_cons_columns loc,"
|
|
78
|
+
"\nall_constraints ac"
|
|
79
|
+
"\nLEFT JOIN all_cons_columns rem"
|
|
80
|
+
"\nON ac.r_owner = rem.owner"
|
|
81
|
+
"\nAND ac.r_constraint_name = rem.constraint_name"
|
|
82
|
+
"\nAND rem.table_name = :table_name"
|
|
83
|
+
"\nWHERE ac.table_name = :table_name"
|
|
84
|
+
"\nAND loc.table_name = :table_name"
|
|
85
|
+
"\nAND ac.constraint_type IN ('R','P', 'U', 'C')"
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
if schema is not None:
|
|
89
|
+
params["owner"] = schema
|
|
90
|
+
text += "\nAND ac.owner = :owner"
|
|
91
|
+
|
|
92
|
+
text += (
|
|
93
|
+
"\nAND ac.owner = loc.owner"
|
|
94
|
+
"\nAND ac.constraint_name = loc.constraint_name"
|
|
95
|
+
"\nAND (rem.position IS NULL or loc.position=rem.position)"
|
|
96
|
+
"\nORDER BY ac.constraint_name, loc.position"
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
text = text % {"dblink": dblink}
|
|
100
|
+
rp = connection.execute(sql.text(text), **params)
|
|
101
|
+
constraint_data = rp.fetchall()
|
|
102
|
+
return constraint_data
|
|
103
|
+
|
|
104
|
+
@lru_cache()
|
|
105
|
+
def _constraint_query(self, owner):
|
|
106
|
+
local = dictionary.all_cons_columns.alias("local")
|
|
107
|
+
remote = dictionary.all_cons_columns.alias("remote")
|
|
108
|
+
table_names = bindparam("all_objects")
|
|
109
|
+
|
|
110
|
+
return (
|
|
111
|
+
select(
|
|
112
|
+
dictionary.all_constraints.c.table_name,
|
|
113
|
+
dictionary.all_constraints.c.constraint_type,
|
|
114
|
+
dictionary.all_constraints.c.constraint_name,
|
|
115
|
+
local.c.column_name.label("local_column"),
|
|
116
|
+
remote.c.table_name.label("remote_table"),
|
|
117
|
+
remote.c.column_name.label("remote_column"),
|
|
118
|
+
remote.c.owner.label("remote_owner"),
|
|
119
|
+
dictionary.all_constraints.c.search_condition,
|
|
120
|
+
dictionary.all_constraints.c.delete_rule,
|
|
121
|
+
)
|
|
122
|
+
.select_from(dictionary.all_constraints)
|
|
123
|
+
.join(
|
|
124
|
+
local,
|
|
125
|
+
and_(
|
|
126
|
+
local.c.owner == dictionary.all_constraints.c.owner,
|
|
127
|
+
dictionary.all_constraints.c.constraint_name
|
|
128
|
+
== local.c.constraint_name,
|
|
129
|
+
local.c.table_name.in_(table_names),
|
|
130
|
+
),
|
|
131
|
+
)
|
|
132
|
+
.outerjoin(
|
|
133
|
+
remote,
|
|
134
|
+
and_(
|
|
135
|
+
dictionary.all_constraints.c.r_owner == remote.c.owner,
|
|
136
|
+
dictionary.all_constraints.c.r_constraint_name
|
|
137
|
+
== remote.c.constraint_name,
|
|
138
|
+
remote.c.table_name.in_(table_names),
|
|
139
|
+
or_(
|
|
140
|
+
remote.c.position.is_(sql.null()),
|
|
141
|
+
local.c.position == remote.c.position,
|
|
142
|
+
),
|
|
143
|
+
),
|
|
144
|
+
)
|
|
145
|
+
.where(
|
|
146
|
+
dictionary.all_constraints.c.table_name.in_(table_names),
|
|
147
|
+
dictionary.all_constraints.c.owner == owner,
|
|
148
|
+
dictionary.all_constraints.c.constraint_type.in_(("R", "P", "U", "C")),
|
|
149
|
+
)
|
|
150
|
+
.order_by(
|
|
151
|
+
dictionary.all_constraints.c.constraint_name,
|
|
152
|
+
local.c.position,
|
|
153
|
+
)
|
|
154
|
+
)
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
OceanBase Dialect Requirements
|
|
4
|
+
|
|
5
|
+
This file defines the capabilities and requirements for the OceanBase dialect,
|
|
6
|
+
which extends SQLAlchemy's Oracle dialect with OceanBase-specific optimizations.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from sqlalchemy.testing.requirements import SuiteRequirements
|
|
10
|
+
from sqlalchemy.testing import exclusions
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Requirements(SuiteRequirements):
|
|
14
|
+
"""
|
|
15
|
+
OceanBase Oracle mode dialect requirements.
|
|
16
|
+
|
|
17
|
+
This class defines what features are supported or not supported
|
|
18
|
+
by the OceanBase Oracle mode dialect.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
@property
|
|
22
|
+
def ctes_with_update_delete(self):
|
|
23
|
+
"""
|
|
24
|
+
OceanBase supports CTEs with UPDATE/DELETE.
|
|
25
|
+
"""
|
|
26
|
+
return exclusions.open()
|
|
27
|
+
|
|
28
|
+
@property
|
|
29
|
+
def except_all(self):
|
|
30
|
+
"""
|
|
31
|
+
OceanBase does not support EXCEPT ALL syntax.
|
|
32
|
+
This causes OBE-00900: invalid SQL statement error.
|
|
33
|
+
"""
|
|
34
|
+
return exclusions.closed()
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def intersect_all(self):
|
|
38
|
+
"""
|
|
39
|
+
OceanBase may not support INTERSECT ALL syntax.
|
|
40
|
+
"""
|
|
41
|
+
return exclusions.closed()
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def union_all(self):
|
|
45
|
+
"""
|
|
46
|
+
OceanBase supports UNION ALL syntax.
|
|
47
|
+
"""
|
|
48
|
+
return exclusions.open()
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def returning(self):
|
|
52
|
+
"""
|
|
53
|
+
OceanBase supports RETURNING clause.
|
|
54
|
+
"""
|
|
55
|
+
return exclusions.open()
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def autoincrement_without_sequence(self):
|
|
59
|
+
"""
|
|
60
|
+
OceanBase requires sequences for auto-increment columns.
|
|
61
|
+
"""
|
|
62
|
+
return exclusions.closed()
|
|
63
|
+
|
|
64
|
+
@property
|
|
65
|
+
def sequences(self):
|
|
66
|
+
"""
|
|
67
|
+
OceanBase supports sequences.
|
|
68
|
+
"""
|
|
69
|
+
return exclusions.open()
|
|
70
|
+
|
|
71
|
+
@property
|
|
72
|
+
def schemas(self):
|
|
73
|
+
"""
|
|
74
|
+
OceanBase supports schemas.
|
|
75
|
+
"""
|
|
76
|
+
return exclusions.open()
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def cross_schema_fk_reflection(self):
|
|
80
|
+
"""
|
|
81
|
+
OceanBase supports cross-schema foreign key reflection.
|
|
82
|
+
"""
|
|
83
|
+
return exclusions.open()
|
|
84
|
+
|
|
85
|
+
@property
|
|
86
|
+
def foreign_key_constraint_reflection(self):
|
|
87
|
+
"""
|
|
88
|
+
OceanBase supports foreign key constraint reflection.
|
|
89
|
+
"""
|
|
90
|
+
return exclusions.open()
|
|
91
|
+
|
|
92
|
+
@property
|
|
93
|
+
def primary_key_constraint_reflection(self):
|
|
94
|
+
"""
|
|
95
|
+
OceanBase supports primary key constraint reflection.
|
|
96
|
+
"""
|
|
97
|
+
return exclusions.open()
|
|
98
|
+
|
|
99
|
+
@property
|
|
100
|
+
def unique_constraint_reflection(self):
|
|
101
|
+
"""
|
|
102
|
+
OceanBase supports unique constraint reflection.
|
|
103
|
+
"""
|
|
104
|
+
return exclusions.open()
|
|
105
|
+
|
|
106
|
+
@property
|
|
107
|
+
def check_constraint_reflection(self):
|
|
108
|
+
"""
|
|
109
|
+
OceanBase supports check constraint reflection.
|
|
110
|
+
"""
|
|
111
|
+
return exclusions.open()
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def index_reflection(self):
|
|
115
|
+
"""
|
|
116
|
+
OceanBase supports index reflection.
|
|
117
|
+
"""
|
|
118
|
+
return exclusions.open()
|
|
119
|
+
|
|
120
|
+
@property
|
|
121
|
+
def view_reflection(self):
|
|
122
|
+
"""
|
|
123
|
+
OceanBase supports view reflection.
|
|
124
|
+
"""
|
|
125
|
+
return exclusions.open()
|
|
126
|
+
|
|
127
|
+
@property
|
|
128
|
+
def temp_table_reflection(self):
|
|
129
|
+
"""
|
|
130
|
+
OceanBase supports temporary table reflection.
|
|
131
|
+
"""
|
|
132
|
+
return exclusions.open()
|
|
133
|
+
|
|
134
|
+
@property
|
|
135
|
+
def table_reflection(self):
|
|
136
|
+
"""
|
|
137
|
+
OceanBase supports table reflection.
|
|
138
|
+
"""
|
|
139
|
+
return exclusions.open()
|
|
140
|
+
|
|
141
|
+
@property
|
|
142
|
+
def column_reflection(self):
|
|
143
|
+
"""
|
|
144
|
+
OceanBase supports column reflection.
|
|
145
|
+
"""
|
|
146
|
+
return exclusions.open()
|
|
147
|
+
|
|
148
|
+
@property
|
|
149
|
+
def oracle(self):
|
|
150
|
+
"""
|
|
151
|
+
OceanBase is Oracle-compatible.
|
|
152
|
+
"""
|
|
153
|
+
return exclusions.open()
|
|
154
|
+
|
|
155
|
+
@property
|
|
156
|
+
def cx_oracle(self):
|
|
157
|
+
"""
|
|
158
|
+
OceanBase dialect supports cx_oracle driver.
|
|
159
|
+
"""
|
|
160
|
+
return exclusions.open()
|
|
161
|
+
|
|
162
|
+
@property
|
|
163
|
+
def ctes(self):
|
|
164
|
+
"""
|
|
165
|
+
OceanBase supports Common Table Expressions (CTEs).
|
|
166
|
+
"""
|
|
167
|
+
return exclusions.open()
|
|
168
|
+
|
|
169
|
+
@property
|
|
170
|
+
def cte_recursive(self):
|
|
171
|
+
"""
|
|
172
|
+
OceanBase supports recursive CTEs.
|
|
173
|
+
"""
|
|
174
|
+
return exclusions.open()
|
|
175
|
+
|
|
176
|
+
@property
|
|
177
|
+
def cte_works_with_dml(self):
|
|
178
|
+
"""
|
|
179
|
+
OceanBase supports CTEs with DML statements.
|
|
180
|
+
"""
|
|
181
|
+
return exclusions.open()
|
|
182
|
+
|
|
183
|
+
@property
|
|
184
|
+
def cte_works_with_subqueries(self):
|
|
185
|
+
"""
|
|
186
|
+
OceanBase supports CTEs with subqueries.
|
|
187
|
+
"""
|
|
188
|
+
return exclusions.open()
|
|
189
|
+
|
|
190
|
+
@property
|
|
191
|
+
def cte_works_with_aggregates(self):
|
|
192
|
+
"""
|
|
193
|
+
OceanBase supports CTEs with aggregate functions.
|
|
194
|
+
"""
|
|
195
|
+
return exclusions.open()
|
|
196
|
+
|
|
197
|
+
@property
|
|
198
|
+
def cte_works_with_window_functions(self):
|
|
199
|
+
"""
|
|
200
|
+
OceanBase supports CTEs with window functions.
|
|
201
|
+
"""
|
|
202
|
+
return exclusions.open()
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
"""
|
|
4
|
+
OceanBase Dialect Utility Module
|
|
5
|
+
|
|
6
|
+
Provides functionality for building secure connection strings.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import urllib.parse
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def build_safe_connection_string(
|
|
13
|
+
username: str,
|
|
14
|
+
password: str,
|
|
15
|
+
host: str,
|
|
16
|
+
port: str,
|
|
17
|
+
service_name: str,
|
|
18
|
+
dialect: str = "oceanbase+cx_oceanbase",
|
|
19
|
+
) -> str:
|
|
20
|
+
"""
|
|
21
|
+
Build a secure connection string, ensuring proper parsing in SQLAlchemy 2.0
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
username: Database username
|
|
25
|
+
password: Database password
|
|
26
|
+
host: Database host
|
|
27
|
+
port: Database port
|
|
28
|
+
service_name: Service name
|
|
29
|
+
dialect: Database dialect, defaults to oceanbase+cx_oracle
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
str: Secure connection string
|
|
33
|
+
"""
|
|
34
|
+
# URL encode special characters in username and password
|
|
35
|
+
safe_username = urllib.parse.quote(username, safe="")
|
|
36
|
+
safe_password = urllib.parse.quote(password, safe="")
|
|
37
|
+
|
|
38
|
+
# Build connection string
|
|
39
|
+
connection_string = (
|
|
40
|
+
f"{dialect}://{safe_username}:{safe_password}@{host}:{port}/{service_name}"
|
|
41
|
+
)
|
|
42
|
+
return connection_string
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: oceanbase-sqlalchemy
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: SQLAlchemy dialect for OceanBase Oracle mode (supports SQLAlchemy 1.3.x and 2.0+)
|
|
5
|
+
Home-page: https://github.com/oceanbase/ecology-plugins
|
|
6
|
+
Author: OceanBase Team
|
|
7
|
+
Author-email: support@oceanbase.com
|
|
8
|
+
Project-URL: Bug Reports, https://github.com/oceanbase/ecology-plugins/issues
|
|
9
|
+
Project-URL: Source, https://github.com/oceanbase/ecology-plugins
|
|
10
|
+
Project-URL: Documentation, https://github.com/oceanbase/ecology-plugins#readme
|
|
11
|
+
Keywords: sqlalchemy oceanbase oracle dialect database
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Topic :: Database
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
25
|
+
Requires-Python: >=3.7
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Requires-Dist: sqlalchemy>=1.3.0
|
|
29
|
+
Requires-Dist: cx_oracle>=8.0.0
|
|
30
|
+
Dynamic: author
|
|
31
|
+
Dynamic: author-email
|
|
32
|
+
Dynamic: classifier
|
|
33
|
+
Dynamic: description
|
|
34
|
+
Dynamic: description-content-type
|
|
35
|
+
Dynamic: home-page
|
|
36
|
+
Dynamic: keywords
|
|
37
|
+
Dynamic: license-file
|
|
38
|
+
Dynamic: project-url
|
|
39
|
+
Dynamic: requires-dist
|
|
40
|
+
Dynamic: requires-python
|
|
41
|
+
Dynamic: summary
|
|
42
|
+
|
|
43
|
+
# SQLAlchemy OceanBase Dialect
|
|
44
|
+
|
|
45
|
+
[](https://badge.fury.io/py/oceanbase-sqlalchemy)
|
|
46
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
47
|
+
[](https://www.python.org/downloads/)
|
|
48
|
+
|
|
49
|
+
A SQLAlchemy dialect for OceanBase Oracle mode, providing seamless integration between SQLAlchemy and OceanBase database.
|
|
50
|
+
|
|
51
|
+
## Features
|
|
52
|
+
|
|
53
|
+
- **Full Compatibility**: Supports SQLAlchemy 1.3.x and 2.0+
|
|
54
|
+
- **Optimized Performance**: Enhanced SQL queries specifically optimized for OceanBase
|
|
55
|
+
- **Advanced Reflection**: Enhanced constraint reflection with OceanBase-specific optimizations
|
|
56
|
+
- **Connection Safety**: Secure connection string building utilities
|
|
57
|
+
|
|
58
|
+
## Installation
|
|
59
|
+
|
|
60
|
+
### From PyPI (Recommended)
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
pip install oceanbase-sqlalchemy
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### From Source
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
git clone https://github.com/oceanbase/ecology-plugins.git
|
|
70
|
+
cd ecology-plugins/oceanbase-sqlalchemy-plugin
|
|
71
|
+
pip install -e .
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Quick Start
|
|
75
|
+
|
|
76
|
+
### Basic Usage
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
from sqlalchemy import create_engine, text
|
|
80
|
+
from oceanbase_sqlalchemy.utils import build_safe_connection_string
|
|
81
|
+
|
|
82
|
+
# Build connection string
|
|
83
|
+
connection_string = build_safe_connection_string(
|
|
84
|
+
username="your_username",
|
|
85
|
+
password="your_password",
|
|
86
|
+
host="your_host",
|
|
87
|
+
port="2881",
|
|
88
|
+
service_name="your_service_name"
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
# Create engine
|
|
92
|
+
engine = create_engine(connection_string)
|
|
93
|
+
|
|
94
|
+
# Test connection
|
|
95
|
+
with engine.connect() as conn:
|
|
96
|
+
result = conn.execute(text("SELECT 1 FROM DUAL"))
|
|
97
|
+
print(result.fetchone())
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Using with SQLAlchemy ORM
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from sqlalchemy import create_engine, Column, Integer, String, MetaData, Table
|
|
104
|
+
from sqlalchemy.ext.declarative import declarative_base
|
|
105
|
+
from oceanbase_sqlalchemy.utils import build_safe_connection_string
|
|
106
|
+
|
|
107
|
+
# Create engine
|
|
108
|
+
connection_string = build_safe_connection_string(
|
|
109
|
+
username="your_username",
|
|
110
|
+
password="your_password",
|
|
111
|
+
host="your_host",
|
|
112
|
+
port="2881",
|
|
113
|
+
service_name="your_service_name"
|
|
114
|
+
)
|
|
115
|
+
engine = create_engine(connection_string)
|
|
116
|
+
|
|
117
|
+
# Define model
|
|
118
|
+
Base = declarative_base()
|
|
119
|
+
|
|
120
|
+
class User(Base):
|
|
121
|
+
__tablename__ = 'users'
|
|
122
|
+
|
|
123
|
+
id = Column(Integer, primary_key=True)
|
|
124
|
+
name = Column(String(50))
|
|
125
|
+
email = Column(String(100))
|
|
126
|
+
|
|
127
|
+
# Create tables
|
|
128
|
+
Base.metadata.create_all(engine)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
## Requirements
|
|
133
|
+
|
|
134
|
+
- Python 3.7+
|
|
135
|
+
- SQLAlchemy 1.3.x or 2.0+
|
|
136
|
+
- cx_Oracle (for Oracle mode)
|
|
137
|
+
|
|
138
|
+
## Contributing
|
|
139
|
+
|
|
140
|
+
We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details.
|
|
141
|
+
|
|
142
|
+
1. Fork the repository
|
|
143
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
144
|
+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
145
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
146
|
+
5. Open a Pull Request
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
## License
|
|
150
|
+
|
|
151
|
+
This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details.
|
|
152
|
+
|
|
153
|
+
## Support
|
|
154
|
+
|
|
155
|
+
- **Issues**: [GitHub Issues](https://github.com/oceanbase/ecology-plugins/issues)
|
|
156
|
+
- **Community**: [OceanBase Community](https://www.oceanbase.com/community)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
## FAQ
|
|
160
|
+
|
|
161
|
+
### Q: How to connect to OceanBase MySQL mode?
|
|
162
|
+
A: The current version only supports OceanBase Oracle mode. For MySQL mode, please use the standard MySQL driver.
|
|
163
|
+
|
|
164
|
+
### Q: Which SQLAlchemy versions are supported?
|
|
165
|
+
A: Supports SQLAlchemy 1.3.x and 2.0+ versions.
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
## Troubleshooting
|
|
169
|
+
|
|
170
|
+
### Connection Issues
|
|
171
|
+
- Check network connection and firewall settings
|
|
172
|
+
- Verify username, password and service name
|
|
173
|
+
- Confirm the port number is correct (default 2881)
|
|
174
|
+
|
|
175
|
+
### Performance Issues
|
|
176
|
+
- Enable SQL logging (`echo=True`) to view executed SQL
|
|
177
|
+
- Analyze slow query logs
|
|
178
|
+
|
|
179
|
+
### Compatibility Issues
|
|
180
|
+
- Ensure using supported SQLAlchemy version
|
|
181
|
+
- Check cx_Oracle driver version
|
|
182
|
+
- Review OceanBase version compatibility
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
oceanbase_sqlalchemy/__init__.py,sha256=mCE7IcU7PRASDf_TI5BURcbljcyNYRJFWmbvxc-jtbE,655
|
|
2
|
+
oceanbase_sqlalchemy/cx_oceanbase.py,sha256=0qbEebkCWA39yX_N7dryLxIC3qDxsZbWisDSuR93ZIg,305
|
|
3
|
+
oceanbase_sqlalchemy/cx_oracle.py,sha256=NsSqRJS4Dz1EJxQsxslLWaDXwfkDu8g_sgESaaslCGg,5354
|
|
4
|
+
oceanbase_sqlalchemy/requirements.py,sha256=OyEgg5dg8ec6TEm_PRKBGvGbDDpbDZU5TFoqxLIuviw,4710
|
|
5
|
+
oceanbase_sqlalchemy/utils.py,sha256=kROkLk05FZOqf6XQCP6ZmqAFs925w6LQ-d_v00oqviQ,1075
|
|
6
|
+
oceanbase_sqlalchemy-0.2.0.dist-info/licenses/LICENSE,sha256=uDYFRU5mAAiQ7-9K720F0mCkKNy3eiNVysVw3dvLGE4,11339
|
|
7
|
+
test/__init__.py,sha256=OPgsRcwz1Z7YFFHpZuf3MlnKEMXSVT_mOsCo_GNmsJw,68
|
|
8
|
+
test/conftest.py,sha256=Frid8k2P6qlkSbnz0Z_qZP_Lz9TN_wW5W8Vbxlpo2r0,386
|
|
9
|
+
test/test_basic.py,sha256=GQBc_xbQ2wdX73MpHALbQL9PtqQIfl0EpLc0h3qMix8,2899
|
|
10
|
+
test/test_optimization.py,sha256=AbVev-vlM-9wHWyzl8rz6w6Z2DLlQ7kZPxRY4n_3_H8,8286
|
|
11
|
+
test/test_suite.py,sha256=_rD8thZ_MajpFStfzl6gzxL3ekc9W2iAQoq6-D59vZk,1650
|
|
12
|
+
oceanbase_sqlalchemy-0.2.0.dist-info/METADATA,sha256=n4Lc538zXfJbay4sxxsrCkl5CXi2K9jSOoLqgo-smEk,5432
|
|
13
|
+
oceanbase_sqlalchemy-0.2.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
14
|
+
oceanbase_sqlalchemy-0.2.0.dist-info/entry_points.txt,sha256=4m2qOkJ5kvYDnTiJK5E3YPI7QSrkCqykIdFNn_nB7v4,191
|
|
15
|
+
oceanbase_sqlalchemy-0.2.0.dist-info/top_level.txt,sha256=Qoa-iEBezXn0U1bvTIyFDGsC8A3H-yHZMnCbG3BN2DU,26
|
|
16
|
+
oceanbase_sqlalchemy-0.2.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2024 OceanBase
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
test/__init__.py
ADDED
test/conftest.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
Pytest Configuration File - OceanBase Dialect Testing
|
|
4
|
+
|
|
5
|
+
This file configures the pytest environment and registers the OceanBase dialect for testing.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from sqlalchemy.dialects import registry
|
|
9
|
+
|
|
10
|
+
# Register OceanBase dialect to SQLAlchemy
|
|
11
|
+
registry.register(
|
|
12
|
+
"oceanbase.cx_oracle",
|
|
13
|
+
"sqlalchemy_oceanbase.cx_oracle",
|
|
14
|
+
"OceanBaseDialect_cx_oracle",
|
|
15
|
+
)
|
test/test_basic.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
Basic tests for OceanBase SQLAlchemy dialect
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import pytest
|
|
7
|
+
import sqlalchemy as sa
|
|
8
|
+
from sqlalchemy import create_engine
|
|
9
|
+
from sqlalchemy.testing import fixtures
|
|
10
|
+
|
|
11
|
+
from oceanbase_sqlalchemy.cx_oracle import OceanBaseDialect_cx_oracle
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class TestOceanBaseDialect(fixtures.TestBase):
|
|
15
|
+
"""Test basic OceanBase dialect functionality."""
|
|
16
|
+
|
|
17
|
+
def test_dialect_import(self):
|
|
18
|
+
"""Test that the dialect can be imported."""
|
|
19
|
+
assert OceanBaseDialect_cx_oracle is not None
|
|
20
|
+
assert issubclass(OceanBaseDialect_cx_oracle, sa.engine.Dialect)
|
|
21
|
+
|
|
22
|
+
def test_dialect_name(self):
|
|
23
|
+
"""Test dialect name."""
|
|
24
|
+
dialect = OceanBaseDialect_cx_oracle()
|
|
25
|
+
assert dialect.name == "oceanbase"
|
|
26
|
+
|
|
27
|
+
def test_driver_name(self):
|
|
28
|
+
"""Test driver name."""
|
|
29
|
+
dialect = OceanBaseDialect_cx_oracle()
|
|
30
|
+
assert dialect.driver == "cx_oracle"
|
|
31
|
+
|
|
32
|
+
def test_connection_string_building(self):
|
|
33
|
+
"""Test connection string building."""
|
|
34
|
+
from oceanbase_sqlalchemy.utils import build_safe_connection_string
|
|
35
|
+
|
|
36
|
+
conn_str = build_safe_connection_string(
|
|
37
|
+
username="test_user",
|
|
38
|
+
password="test_pass",
|
|
39
|
+
host="localhost",
|
|
40
|
+
port="2881",
|
|
41
|
+
service_name="test_service",
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
assert "oceanbase+cx_oceanbase://" in conn_str
|
|
45
|
+
assert "test_user:test_pass@localhost:2881" in conn_str
|
|
46
|
+
assert "test_service" in conn_str
|
|
47
|
+
|
|
48
|
+
def test_engine_creation(self):
|
|
49
|
+
"""Test engine creation with OceanBase dialect."""
|
|
50
|
+
# This test will only work if OceanBase is available
|
|
51
|
+
try:
|
|
52
|
+
engine = create_engine(
|
|
53
|
+
"oceanbase+cx_oracle://test:test@localhost:2881/?service_name=test"
|
|
54
|
+
)
|
|
55
|
+
assert engine.dialect.name == "oceanbase"
|
|
56
|
+
except Exception as e:
|
|
57
|
+
pytest.skip(f"OceanBase not available: {e}")
|
|
58
|
+
|
|
59
|
+
def test_sql_generation(self):
|
|
60
|
+
"""Test basic SQL generation."""
|
|
61
|
+
# Test table creation
|
|
62
|
+
table = sa.Table(
|
|
63
|
+
"test_table",
|
|
64
|
+
sa.MetaData(),
|
|
65
|
+
sa.Column("id", sa.Integer, primary_key=True),
|
|
66
|
+
sa.Column("name", sa.String(50)),
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
# This should not raise an exception
|
|
70
|
+
create_stmt = sa.schema.CreateTable(table)
|
|
71
|
+
assert create_stmt is not None
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class TestOceanBaseRequirements(fixtures.TestBase):
|
|
75
|
+
"""Test OceanBase dialect requirements."""
|
|
76
|
+
|
|
77
|
+
def test_requirements_class(self):
|
|
78
|
+
"""Test that requirements class can be instantiated."""
|
|
79
|
+
from oceanbase_sqlalchemy.requirements import Requirements
|
|
80
|
+
|
|
81
|
+
req = Requirements()
|
|
82
|
+
assert req is not None
|
|
83
|
+
|
|
84
|
+
# Test some basic properties
|
|
85
|
+
assert hasattr(req, "ctes_with_update_delete")
|
|
86
|
+
assert hasattr(req, "oracle")
|
|
87
|
+
assert hasattr(req, "cx_oracle")
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
if __name__ == "__main__":
|
|
91
|
+
pytest.main([__file__])
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
OceanBase Dialect Optimization Task Testing
|
|
5
|
+
|
|
6
|
+
This file specifically tests whether the optimization tasks mentioned in Task.md are completed:
|
|
7
|
+
1. Verify that optimized SQL queries are used during actual connections
|
|
8
|
+
2. Verify the performance and correctness of metadata queries
|
|
9
|
+
3. Validate optimization effects through real database connections
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import pytest
|
|
13
|
+
import os
|
|
14
|
+
from sqlalchemy import (
|
|
15
|
+
create_engine,
|
|
16
|
+
inspect,
|
|
17
|
+
text,
|
|
18
|
+
MetaData,
|
|
19
|
+
Table,
|
|
20
|
+
Column,
|
|
21
|
+
String,
|
|
22
|
+
Numeric,
|
|
23
|
+
)
|
|
24
|
+
from sqlalchemy.schema import CreateTable, DropTable
|
|
25
|
+
from sqlalchemy.exc import OperationalError
|
|
26
|
+
|
|
27
|
+
# Load .env file
|
|
28
|
+
try:
|
|
29
|
+
from dotenv import load_dotenv
|
|
30
|
+
|
|
31
|
+
load_dotenv(os.path.join(os.path.dirname(__file__), ".env"))
|
|
32
|
+
print("โ
.env file loaded successfully")
|
|
33
|
+
except ImportError:
|
|
34
|
+
print("โ ๏ธ python-dotenv not installed, unable to load .env file")
|
|
35
|
+
except Exception as e:
|
|
36
|
+
print(f"โ ๏ธ Failed to load .env file: {e}")
|
|
37
|
+
|
|
38
|
+
# Print current SQLAlchemy version
|
|
39
|
+
try:
|
|
40
|
+
import sqlalchemy
|
|
41
|
+
|
|
42
|
+
print(f"๐ Current SQLAlchemy version: {sqlalchemy.__version__}")
|
|
43
|
+
except ImportError:
|
|
44
|
+
print("โ ๏ธ Unable to get SQLAlchemy version information")
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class TestOceanBaseOptimization:
|
|
48
|
+
"""Test OceanBase optimization functionality"""
|
|
49
|
+
|
|
50
|
+
def test_actual_connection_optimization(self):
|
|
51
|
+
"""Test whether optimized queries are used during actual connections (requires real database connection)"""
|
|
52
|
+
# Check if OceanBase connection configuration exists
|
|
53
|
+
host = os.getenv("OCEANBASE_HOST")
|
|
54
|
+
port = os.getenv("OCEANBASE_PORT")
|
|
55
|
+
username = os.getenv("OCEANBASE_USERNAME")
|
|
56
|
+
password = os.getenv("OCEANBASE_PASSWORD")
|
|
57
|
+
service_name = os.getenv("OCEANBASE_SERVICE_NAME")
|
|
58
|
+
|
|
59
|
+
if not all([host, port, username, password, service_name]):
|
|
60
|
+
pytest.skip(
|
|
61
|
+
"Skipping actual connection test: environment variables not configured"
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
try:
|
|
65
|
+
# Use secure connection string building method
|
|
66
|
+
from oceanbase_sqlalchemy.utils import build_safe_connection_string
|
|
67
|
+
|
|
68
|
+
connection_string = build_safe_connection_string(
|
|
69
|
+
username, password, host, port, service_name
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
# Create engine
|
|
73
|
+
engine = create_engine(connection_string, echo=True) # Enable SQL logging
|
|
74
|
+
|
|
75
|
+
with engine.connect() as conn:
|
|
76
|
+
# Check dialect attribute
|
|
77
|
+
assert engine.dialect.name == "oceanbase"
|
|
78
|
+
|
|
79
|
+
# 1. Use SQLAlchemy API to create test table
|
|
80
|
+
import uuid
|
|
81
|
+
|
|
82
|
+
test_table_name = (
|
|
83
|
+
f"TEST_OPTIMIZATION_TABLE_{uuid.uuid4().hex[:8].upper()}"
|
|
84
|
+
)
|
|
85
|
+
metadata = MetaData()
|
|
86
|
+
|
|
87
|
+
# Define test table structure - use simpler structure to avoid compatibility issues
|
|
88
|
+
test_table = Table(
|
|
89
|
+
test_table_name,
|
|
90
|
+
metadata,
|
|
91
|
+
Column("id", Numeric(10, 0), primary_key=True),
|
|
92
|
+
Column("name", String(100), nullable=False),
|
|
93
|
+
Column("description", String(500)),
|
|
94
|
+
Column("status", String(20)),
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
print(
|
|
98
|
+
f"๐จ Using SQLAlchemy API to create test table: {test_table_name}"
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
# Create table
|
|
102
|
+
create_ddl = CreateTable(test_table).compile(dialect=engine.dialect)
|
|
103
|
+
print(f"Generated DDL: {create_ddl}")
|
|
104
|
+
conn.execute(text(str(create_ddl)))
|
|
105
|
+
# In SQLAlchemy 1.3, transactions are needed to commit
|
|
106
|
+
conn.execute(text("COMMIT"))
|
|
107
|
+
print("โ
Test table created successfully")
|
|
108
|
+
|
|
109
|
+
# 2. Use SQLAlchemy API to insert test data
|
|
110
|
+
print("๐ Inserting test data")
|
|
111
|
+
insert_stmt = test_table.insert().values(
|
|
112
|
+
id=1,
|
|
113
|
+
name="Test Data",
|
|
114
|
+
description="This is test data for testing optimization functionality",
|
|
115
|
+
status="ACTIVE",
|
|
116
|
+
)
|
|
117
|
+
conn.execute(insert_stmt)
|
|
118
|
+
conn.execute(text("COMMIT"))
|
|
119
|
+
print("โ
Test data inserted successfully")
|
|
120
|
+
|
|
121
|
+
# 3. Use SQLAlchemy API to verify data insertion
|
|
122
|
+
print("๐ Verifying data insertion")
|
|
123
|
+
select_stmt = test_table.select().where(test_table.c.id == 1)
|
|
124
|
+
result = conn.execute(select_stmt)
|
|
125
|
+
row = result.fetchone()
|
|
126
|
+
assert row is not None, "Inserted data not found"
|
|
127
|
+
assert row.name == "Test Data", "Inserted data is incorrect"
|
|
128
|
+
print("โ
Data verification successful")
|
|
129
|
+
|
|
130
|
+
# 4. Test metadata queries (this will trigger optimized SQL queries)
|
|
131
|
+
inspector = inspect(engine)
|
|
132
|
+
|
|
133
|
+
# Get table list
|
|
134
|
+
tables = inspector.get_table_names()
|
|
135
|
+
print(f"Found tables: {tables}")
|
|
136
|
+
# OceanBase may return table names in lowercase, so we need case-insensitive comparison
|
|
137
|
+
table_found = any(
|
|
138
|
+
table.upper() == test_table_name.upper() for table in tables
|
|
139
|
+
)
|
|
140
|
+
assert table_found, (
|
|
141
|
+
f"Test table {test_table_name} not found in table list"
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
# Get table primary key constraints
|
|
145
|
+
pk_info = inspector.get_pk_constraint(test_table_name)
|
|
146
|
+
print(f"Primary key info: {pk_info}")
|
|
147
|
+
# OceanBase may return column names in lowercase, so we need case-insensitive comparison
|
|
148
|
+
assert "id" in [
|
|
149
|
+
col.lower() for col in pk_info["constrained_columns"]
|
|
150
|
+
], "Primary key constraint incorrect"
|
|
151
|
+
|
|
152
|
+
# Get foreign key constraints
|
|
153
|
+
fk_info = inspector.get_foreign_keys(test_table_name)
|
|
154
|
+
print(f"Foreign key info: {fk_info}")
|
|
155
|
+
assert fk_info == [], (
|
|
156
|
+
"Newly created table should not have foreign key constraints"
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
# Get unique constraints
|
|
160
|
+
unique_info = inspector.get_unique_constraints(test_table_name)
|
|
161
|
+
print(f"Unique constraint info: {unique_info}")
|
|
162
|
+
# Simplified table has no unique constraints, so should be empty
|
|
163
|
+
assert len(unique_info) == 0, (
|
|
164
|
+
"Simplified table should not have unique constraints"
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
# Get check constraints
|
|
168
|
+
check_info = inspector.get_check_constraints(test_table_name)
|
|
169
|
+
print(f"Check constraint info: {check_info}")
|
|
170
|
+
# Simplified table has no check constraints, so should be empty
|
|
171
|
+
assert len(check_info) == 0, (
|
|
172
|
+
"Simplified table should not have check constraints"
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
print(
|
|
176
|
+
"โ
Actual connection test successful, all constraint queries used optimization methods"
|
|
177
|
+
)
|
|
178
|
+
print(
|
|
179
|
+
"Note: Please check the SQL logs above to confirm whether optimized SQL statements were used"
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
# 5. Use SQLAlchemy API to clean up test table
|
|
183
|
+
print(f"๐งน Cleaning up test table: {test_table_name}")
|
|
184
|
+
drop_ddl = DropTable(test_table).compile(dialect=engine.dialect)
|
|
185
|
+
print(f"Generated DROP DDL: {drop_ddl}")
|
|
186
|
+
conn.execute(text(str(drop_ddl)))
|
|
187
|
+
conn.execute(text("COMMIT"))
|
|
188
|
+
print("โ
Test table cleanup successful")
|
|
189
|
+
|
|
190
|
+
except OperationalError as e:
|
|
191
|
+
if "ORA-12541" in str(e):
|
|
192
|
+
pytest.skip("Skipping connection test: OceanBase service unreachable")
|
|
193
|
+
elif "ORA-01017" in str(e):
|
|
194
|
+
pytest.skip("Skipping connection test: Username or password incorrect")
|
|
195
|
+
else:
|
|
196
|
+
pytest.fail(f"OceanBase connection failed: {e}")
|
|
197
|
+
except Exception as e:
|
|
198
|
+
pytest.fail(f"Connection test exception: {e}")
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
if __name__ == "__main__":
|
|
202
|
+
# Run tests directly
|
|
203
|
+
pytest.main([__file__, "-v", "-s"])
|
test/test_suite.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
OceanBase Dialect Test Suite
|
|
4
|
+
|
|
5
|
+
This module provides a basic test framework for the OceanBase dialect.
|
|
6
|
+
Note: Full SQLAlchemy test suite integration is not available in SQLAlchemy 2.0.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import sqlalchemy as sa
|
|
10
|
+
from sqlalchemy.testing import fixtures
|
|
11
|
+
|
|
12
|
+
# Import OceanBase dialect for testing
|
|
13
|
+
from oceanbase_sqlalchemy.cx_oracle import OceanBaseDialect_cx_oracle
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class TestOceanBaseBasicSuite(fixtures.TestBase):
|
|
17
|
+
"""Basic test suite for OceanBase dialect."""
|
|
18
|
+
|
|
19
|
+
def test_dialect_basic_functionality(self):
|
|
20
|
+
"""Test basic dialect functionality."""
|
|
21
|
+
dialect = OceanBaseDialect_cx_oracle()
|
|
22
|
+
assert dialect.name == "oceanbase"
|
|
23
|
+
assert dialect.driver == "cx_oracle"
|
|
24
|
+
|
|
25
|
+
def test_dialect_compatibility(self):
|
|
26
|
+
"""Test dialect compatibility with SQLAlchemy."""
|
|
27
|
+
assert issubclass(OceanBaseDialect_cx_oracle, sa.engine.Dialect)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class TestOceanBaseRequirements(fixtures.TestBase):
|
|
31
|
+
"""Test OceanBase dialect requirements."""
|
|
32
|
+
|
|
33
|
+
def test_requirements_instantiation(self):
|
|
34
|
+
"""Test that requirements can be instantiated."""
|
|
35
|
+
from oceanbase_sqlalchemy.requirements import Requirements
|
|
36
|
+
|
|
37
|
+
req = Requirements()
|
|
38
|
+
assert req is not None
|
|
39
|
+
|
|
40
|
+
# Test key properties
|
|
41
|
+
assert hasattr(req, "ctes_with_update_delete")
|
|
42
|
+
assert hasattr(req, "oracle")
|
|
43
|
+
assert hasattr(req, "cx_oracle")
|
|
44
|
+
assert hasattr(req, "ctes")
|
|
45
|
+
assert hasattr(req, "cte_recursive")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
# Note: The original SQLAlchemy test suite import has been removed
|
|
49
|
+
# due to compatibility issues with SQLAlchemy 2.0.
|
|
50
|
+
# Use test_basic.py for more comprehensive testing.
|