oceanbase-sqlalchemy 0.6.0__tar.gz → 0.8.0__tar.gz
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-0.6.0 → oceanbase_sqlalchemy-0.8.0}/PKG-INFO +9 -8
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/README.md +8 -6
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/oceanbase_sqlalchemy/__init__.py +1 -2
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/oceanbase_sqlalchemy/cx_oceanbase.py +2 -2
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/oceanbase_sqlalchemy/cx_oracle.py +59 -7
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/oceanbase_sqlalchemy/requirements.py +1 -2
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/oceanbase_sqlalchemy/utils.py +0 -2
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/oceanbase_sqlalchemy.egg-info/PKG-INFO +9 -8
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/oceanbase_sqlalchemy.egg-info/SOURCES.txt +2 -0
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/oceanbase_sqlalchemy.egg-info/requires.txt +0 -1
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/setup.py +4 -6
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/test/__init__.py +0 -1
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/test/conftest.py +0 -1
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/test/test_basic.py +3 -3
- oceanbase_sqlalchemy-0.8.0/test/test_bind_parameters.py +159 -0
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/test/test_optimization.py +9 -12
- oceanbase_sqlalchemy-0.8.0/test/test_query_cache.py +76 -0
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/test/test_suite.py +0 -1
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/LICENSE +0 -0
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/oceanbase_sqlalchemy.egg-info/dependency_links.txt +0 -0
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/oceanbase_sqlalchemy.egg-info/entry_points.txt +0 -0
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/oceanbase_sqlalchemy.egg-info/not-zip-safe +0 -0
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/oceanbase_sqlalchemy.egg-info/top_level.txt +0 -0
- {oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: oceanbase-sqlalchemy
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.8.0
|
|
4
4
|
Summary: SQLAlchemy dialect for OceanBase Oracle mode (supports SQLAlchemy 1.3.x and 2.0+)
|
|
5
5
|
Home-page: https://github.com/oceanbase/ecology-plugins
|
|
6
6
|
Author: OceanBase Team
|
|
@@ -26,7 +26,6 @@ Requires-Python: >=3.7
|
|
|
26
26
|
Description-Content-Type: text/markdown
|
|
27
27
|
License-File: LICENSE
|
|
28
28
|
Requires-Dist: sqlalchemy>=1.3.0
|
|
29
|
-
Requires-Dist: cx_oracle>=8.0.0
|
|
30
29
|
Dynamic: author
|
|
31
30
|
Dynamic: author-email
|
|
32
31
|
Dynamic: classifier
|
|
@@ -82,10 +81,10 @@ from oceanbase_sqlalchemy.utils import build_safe_connection_string
|
|
|
82
81
|
# Build connection string
|
|
83
82
|
connection_string = build_safe_connection_string(
|
|
84
83
|
username="your_username",
|
|
85
|
-
password="your_password",
|
|
84
|
+
password="your_password",
|
|
86
85
|
host="your_host",
|
|
87
86
|
port="2881",
|
|
88
|
-
service_name="your_service_name"
|
|
87
|
+
service_name="your_service_name",
|
|
89
88
|
)
|
|
90
89
|
|
|
91
90
|
# Create engine
|
|
@@ -108,22 +107,24 @@ from oceanbase_sqlalchemy.utils import build_safe_connection_string
|
|
|
108
107
|
connection_string = build_safe_connection_string(
|
|
109
108
|
username="your_username",
|
|
110
109
|
password="your_password",
|
|
111
|
-
host="your_host",
|
|
110
|
+
host="your_host",
|
|
112
111
|
port="2881",
|
|
113
|
-
service_name="your_service_name"
|
|
112
|
+
service_name="your_service_name",
|
|
114
113
|
)
|
|
115
114
|
engine = create_engine(connection_string)
|
|
116
115
|
|
|
117
116
|
# Define model
|
|
118
117
|
Base = declarative_base()
|
|
119
118
|
|
|
119
|
+
|
|
120
120
|
class User(Base):
|
|
121
|
-
__tablename__ =
|
|
122
|
-
|
|
121
|
+
__tablename__ = "users"
|
|
122
|
+
|
|
123
123
|
id = Column(Integer, primary_key=True)
|
|
124
124
|
name = Column(String(50))
|
|
125
125
|
email = Column(String(100))
|
|
126
126
|
|
|
127
|
+
|
|
127
128
|
# Create tables
|
|
128
129
|
Base.metadata.create_all(engine)
|
|
129
130
|
```
|
|
@@ -40,10 +40,10 @@ from oceanbase_sqlalchemy.utils import build_safe_connection_string
|
|
|
40
40
|
# Build connection string
|
|
41
41
|
connection_string = build_safe_connection_string(
|
|
42
42
|
username="your_username",
|
|
43
|
-
password="your_password",
|
|
43
|
+
password="your_password",
|
|
44
44
|
host="your_host",
|
|
45
45
|
port="2881",
|
|
46
|
-
service_name="your_service_name"
|
|
46
|
+
service_name="your_service_name",
|
|
47
47
|
)
|
|
48
48
|
|
|
49
49
|
# Create engine
|
|
@@ -66,22 +66,24 @@ from oceanbase_sqlalchemy.utils import build_safe_connection_string
|
|
|
66
66
|
connection_string = build_safe_connection_string(
|
|
67
67
|
username="your_username",
|
|
68
68
|
password="your_password",
|
|
69
|
-
host="your_host",
|
|
69
|
+
host="your_host",
|
|
70
70
|
port="2881",
|
|
71
|
-
service_name="your_service_name"
|
|
71
|
+
service_name="your_service_name",
|
|
72
72
|
)
|
|
73
73
|
engine = create_engine(connection_string)
|
|
74
74
|
|
|
75
75
|
# Define model
|
|
76
76
|
Base = declarative_base()
|
|
77
77
|
|
|
78
|
+
|
|
78
79
|
class User(Base):
|
|
79
|
-
__tablename__ =
|
|
80
|
-
|
|
80
|
+
__tablename__ = "users"
|
|
81
|
+
|
|
81
82
|
id = Column(Integer, primary_key=True)
|
|
82
83
|
name = Column(String(50))
|
|
83
84
|
email = Column(String(100))
|
|
84
85
|
|
|
86
|
+
|
|
85
87
|
# Create tables
|
|
86
88
|
Base.metadata.create_all(engine)
|
|
87
89
|
```
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
1
|
"""
|
|
3
2
|
SQLAlchemy dialect for OceanBase Oracle mode.
|
|
4
3
|
|
|
@@ -7,8 +6,8 @@ optimizations and features for cx_oracle driver.
|
|
|
7
6
|
"""
|
|
8
7
|
|
|
9
8
|
# Import the cx_oracle dialect class
|
|
10
|
-
from .cx_oracle import OceanBaseDialect_cx_oracle
|
|
11
9
|
from .cx_oceanbase import OceanBaseDialect_cx_oceanbase
|
|
10
|
+
from .cx_oracle import OceanBaseDialect_cx_oracle
|
|
12
11
|
|
|
13
12
|
# Manually register the dialect for SQLAlchemy 2.0 compatibility
|
|
14
13
|
try:
|
{oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/oceanbase_sqlalchemy/cx_oceanbase.py
RENAMED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
1
|
"""
|
|
3
2
|
OceanBase dialect for cx_oceanbase driver.
|
|
4
3
|
"""
|
|
5
4
|
|
|
6
|
-
from .cx_oracle import OceanBaseDialect_cx_oracle
|
|
7
5
|
import cx_Oceanbase
|
|
8
6
|
|
|
7
|
+
from .cx_oracle import OceanBaseDialect_cx_oracle
|
|
8
|
+
|
|
9
9
|
|
|
10
10
|
class OceanBaseDialect_cx_oceanbase(OceanBaseDialect_cx_oracle):
|
|
11
11
|
"""
|
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
1
|
"""
|
|
3
2
|
OceanBase dialect for cx_oracle driver.
|
|
4
3
|
"""
|
|
5
4
|
|
|
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
5
|
import urllib.parse
|
|
6
|
+
from functools import lru_cache
|
|
11
7
|
|
|
12
8
|
# SQLAlchemy version compatibility
|
|
13
9
|
import sqlalchemy
|
|
10
|
+
from sqlalchemy import and_, bindparam, or_, select, sql
|
|
11
|
+
from sqlalchemy.dialects.oracle.base import OracleCompiler
|
|
12
|
+
from sqlalchemy.dialects.oracle.cx_oracle import (
|
|
13
|
+
OracleCompiler_cx_oracle,
|
|
14
|
+
OracleDialect_cx_oracle,
|
|
15
|
+
)
|
|
16
|
+
from sqlalchemy.engine import reflection
|
|
14
17
|
|
|
15
18
|
SA_VERSION = sqlalchemy.__version__
|
|
16
19
|
SA_20_PLUS = SA_VERSION.startswith("2.")
|
|
@@ -18,6 +21,42 @@ SA_20_PLUS = SA_VERSION.startswith("2.")
|
|
|
18
21
|
if SA_20_PLUS:
|
|
19
22
|
from sqlalchemy.dialects.oracle import dictionary
|
|
20
23
|
|
|
24
|
+
_dictionary_has_ind_expressions = (
|
|
25
|
+
getattr(dictionary, "all_ind_expressions", None) is not None
|
|
26
|
+
)
|
|
27
|
+
else:
|
|
28
|
+
_dictionary_has_ind_expressions = False
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class OceanBaseCompiler_cx_oracle(OracleCompiler_cx_oracle):
|
|
32
|
+
"""
|
|
33
|
+
Custom compiler for OceanBase to handle bind parameter naming.
|
|
34
|
+
|
|
35
|
+
Keep ASCII bind names, including reserved words, unquoted for OceanBase.
|
|
36
|
+
Preserve Oracle's Unicode quoting and parameter-key mapping for drivers
|
|
37
|
+
that require quoted non-ASCII bind names.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
def bindparam_string(self, name, **kw):
|
|
41
|
+
"""
|
|
42
|
+
Restore Oracle's non-ASCII bind handling without changing ASCII binds.
|
|
43
|
+
|
|
44
|
+
Delegating keeps quoted SQL placeholders and DBAPI parameter keys in
|
|
45
|
+
sync on both SQLAlchemy 1.3 and 2.x. ASCII names retain the reserved-word
|
|
46
|
+
fix and the existing version-specific prefix normalization.
|
|
47
|
+
"""
|
|
48
|
+
if not name.isascii():
|
|
49
|
+
return super().bindparam_string(name, **kw)
|
|
50
|
+
|
|
51
|
+
if SA_20_PLUS:
|
|
52
|
+
escaped_from = kw.get("escaped_from", None)
|
|
53
|
+
if not escaped_from and name and (name[0].isdigit() or name[0] == "_"):
|
|
54
|
+
new_name = "D" + name
|
|
55
|
+
kw["escaped_from"] = name
|
|
56
|
+
name = new_name
|
|
57
|
+
|
|
58
|
+
return OracleCompiler.bindparam_string(self, name, **kw)
|
|
59
|
+
|
|
21
60
|
|
|
22
61
|
class OceanBaseDialect_cx_oracle(OracleDialect_cx_oracle):
|
|
23
62
|
"""
|
|
@@ -27,8 +66,15 @@ class OceanBaseDialect_cx_oracle(OracleDialect_cx_oracle):
|
|
|
27
66
|
name = "oceanbase"
|
|
28
67
|
driver = "cx_oracle"
|
|
29
68
|
|
|
69
|
+
# Keep ASCII bind names unquoted while preserving Unicode bind compatibility
|
|
70
|
+
statement_compiler = OceanBaseCompiler_cx_oracle
|
|
71
|
+
|
|
30
72
|
def __init__(self, **kwargs):
|
|
31
73
|
super().__init__(**kwargs)
|
|
74
|
+
# Keep bounded query caches on the dialect instance so discarded
|
|
75
|
+
# engines/dialects are not retained by a class-level method cache.
|
|
76
|
+
self._constraint_query = lru_cache(maxsize=128)(self._constraint_query)
|
|
77
|
+
self._index_query = lru_cache(maxsize=128)(self._index_query)
|
|
32
78
|
|
|
33
79
|
@property
|
|
34
80
|
def _supports_except_all(self):
|
|
@@ -132,7 +178,6 @@ class OceanBaseDialect_cx_oracle(OracleDialect_cx_oracle):
|
|
|
132
178
|
constraint_data = rp.fetchall()
|
|
133
179
|
return constraint_data
|
|
134
180
|
|
|
135
|
-
@lru_cache()
|
|
136
181
|
def _constraint_query(self, owner):
|
|
137
182
|
local = dictionary.all_cons_columns.alias("local")
|
|
138
183
|
remote = dictionary.all_cons_columns.alias("remote")
|
|
@@ -184,7 +229,6 @@ class OceanBaseDialect_cx_oracle(OracleDialect_cx_oracle):
|
|
|
184
229
|
)
|
|
185
230
|
)
|
|
186
231
|
|
|
187
|
-
@lru_cache()
|
|
188
232
|
def _index_query(self, owner):
|
|
189
233
|
"""
|
|
190
234
|
Override _index_query to optimize performance in OceanBase Oracle mode.
|
|
@@ -194,6 +238,14 @@ class OceanBaseDialect_cx_oracle(OracleDialect_cx_oracle):
|
|
|
194
238
|
|
|
195
239
|
Only for sqlalchemy 2.x compatibility.
|
|
196
240
|
"""
|
|
241
|
+
if not _dictionary_has_ind_expressions:
|
|
242
|
+
parent_query = super()._index_query
|
|
243
|
+
# The instance cache already owns this result. Avoid retaining
|
|
244
|
+
# this dialect in SQLAlchemy's class-level LRU cache as well.
|
|
245
|
+
uncached_query = getattr(parent_query, "__wrapped__", None)
|
|
246
|
+
if uncached_query is not None:
|
|
247
|
+
return uncached_query(self, owner)
|
|
248
|
+
return parent_query(owner)
|
|
197
249
|
return (
|
|
198
250
|
select(
|
|
199
251
|
dictionary.all_ind_columns.c.table_name,
|
{oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/oceanbase_sqlalchemy/requirements.py
RENAMED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
1
|
"""
|
|
3
2
|
OceanBase Dialect Requirements
|
|
4
3
|
|
|
@@ -6,8 +5,8 @@ This file defines the capabilities and requirements for the OceanBase dialect,
|
|
|
6
5
|
which extends SQLAlchemy's Oracle dialect with OceanBase-specific optimizations.
|
|
7
6
|
"""
|
|
8
7
|
|
|
9
|
-
from sqlalchemy.testing.requirements import SuiteRequirements
|
|
10
8
|
from sqlalchemy.testing import exclusions
|
|
9
|
+
from sqlalchemy.testing.requirements import SuiteRequirements
|
|
11
10
|
|
|
12
11
|
|
|
13
12
|
class Requirements(SuiteRequirements):
|
{oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/oceanbase_sqlalchemy.egg-info/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: oceanbase-sqlalchemy
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.8.0
|
|
4
4
|
Summary: SQLAlchemy dialect for OceanBase Oracle mode (supports SQLAlchemy 1.3.x and 2.0+)
|
|
5
5
|
Home-page: https://github.com/oceanbase/ecology-plugins
|
|
6
6
|
Author: OceanBase Team
|
|
@@ -26,7 +26,6 @@ Requires-Python: >=3.7
|
|
|
26
26
|
Description-Content-Type: text/markdown
|
|
27
27
|
License-File: LICENSE
|
|
28
28
|
Requires-Dist: sqlalchemy>=1.3.0
|
|
29
|
-
Requires-Dist: cx_oracle>=8.0.0
|
|
30
29
|
Dynamic: author
|
|
31
30
|
Dynamic: author-email
|
|
32
31
|
Dynamic: classifier
|
|
@@ -82,10 +81,10 @@ from oceanbase_sqlalchemy.utils import build_safe_connection_string
|
|
|
82
81
|
# Build connection string
|
|
83
82
|
connection_string = build_safe_connection_string(
|
|
84
83
|
username="your_username",
|
|
85
|
-
password="your_password",
|
|
84
|
+
password="your_password",
|
|
86
85
|
host="your_host",
|
|
87
86
|
port="2881",
|
|
88
|
-
service_name="your_service_name"
|
|
87
|
+
service_name="your_service_name",
|
|
89
88
|
)
|
|
90
89
|
|
|
91
90
|
# Create engine
|
|
@@ -108,22 +107,24 @@ from oceanbase_sqlalchemy.utils import build_safe_connection_string
|
|
|
108
107
|
connection_string = build_safe_connection_string(
|
|
109
108
|
username="your_username",
|
|
110
109
|
password="your_password",
|
|
111
|
-
host="your_host",
|
|
110
|
+
host="your_host",
|
|
112
111
|
port="2881",
|
|
113
|
-
service_name="your_service_name"
|
|
112
|
+
service_name="your_service_name",
|
|
114
113
|
)
|
|
115
114
|
engine = create_engine(connection_string)
|
|
116
115
|
|
|
117
116
|
# Define model
|
|
118
117
|
Base = declarative_base()
|
|
119
118
|
|
|
119
|
+
|
|
120
120
|
class User(Base):
|
|
121
|
-
__tablename__ =
|
|
122
|
-
|
|
121
|
+
__tablename__ = "users"
|
|
122
|
+
|
|
123
123
|
id = Column(Integer, primary_key=True)
|
|
124
124
|
name = Column(String(50))
|
|
125
125
|
email = Column(String(100))
|
|
126
126
|
|
|
127
|
+
|
|
127
128
|
# Create tables
|
|
128
129
|
Base.metadata.create_all(engine)
|
|
129
130
|
```
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
1
|
+
from pathlib import Path
|
|
3
2
|
|
|
4
|
-
from setuptools import
|
|
3
|
+
from setuptools import find_packages, setup
|
|
5
4
|
|
|
6
5
|
setup(
|
|
7
6
|
name="oceanbase-sqlalchemy",
|
|
8
|
-
version="0.
|
|
7
|
+
version="0.8.0",
|
|
9
8
|
description="SQLAlchemy dialect for OceanBase Oracle mode (supports SQLAlchemy 1.3.x and 2.0+)",
|
|
10
|
-
long_description=
|
|
9
|
+
long_description=Path("README.md").read_text(encoding="utf-8"),
|
|
11
10
|
long_description_content_type="text/markdown",
|
|
12
11
|
author="OceanBase Team",
|
|
13
12
|
author_email="support@oceanbase.com",
|
|
@@ -18,7 +17,6 @@ setup(
|
|
|
18
17
|
keywords="sqlalchemy oceanbase oracle dialect database",
|
|
19
18
|
install_requires=[
|
|
20
19
|
"sqlalchemy>=1.3.0",
|
|
21
|
-
"cx_oracle>=8.0.0",
|
|
22
20
|
],
|
|
23
21
|
entry_points={
|
|
24
22
|
"sqlalchemy.dialects": [
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
1
|
"""
|
|
3
2
|
Basic tests for OceanBase SQLAlchemy dialect
|
|
4
3
|
"""
|
|
@@ -47,13 +46,14 @@ class TestOceanBaseDialect(fixtures.TestBase):
|
|
|
47
46
|
|
|
48
47
|
def test_engine_creation(self):
|
|
49
48
|
"""Test engine creation with OceanBase dialect."""
|
|
50
|
-
#
|
|
49
|
+
# Skip missing drivers or dialects; unexpected implementation errors
|
|
50
|
+
# must fail the test instead of being mistaken for an unavailable DB.
|
|
51
51
|
try:
|
|
52
52
|
engine = create_engine(
|
|
53
53
|
"oceanbase+cx_oracle://test:test@localhost:2881/?service_name=test"
|
|
54
54
|
)
|
|
55
55
|
assert engine.dialect.name == "oceanbase"
|
|
56
|
-
except
|
|
56
|
+
except (ImportError, sa.exc.NoSuchModuleError) as e:
|
|
57
57
|
pytest.skip(f"OceanBase not available: {e}")
|
|
58
58
|
|
|
59
59
|
def test_sql_generation(self):
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"""Binding regressions at the SQLAlchemy-to-DBAPI boundary, without a server."""
|
|
2
|
+
|
|
3
|
+
import importlib
|
|
4
|
+
import sys
|
|
5
|
+
import types
|
|
6
|
+
from unittest import mock
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
import sqlalchemy as sa
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@pytest.fixture(params=["cx_oracle", "cx_oceanbase"])
|
|
13
|
+
def dbapi_engine(request, monkeypatch):
|
|
14
|
+
# Package initialization eagerly imports the optional native driver. Only
|
|
15
|
+
# stub that import; execution below uses SQLAlchemy's real dialect/context.
|
|
16
|
+
with mock.patch.dict(
|
|
17
|
+
sys.modules, {"cx_Oceanbase": types.ModuleType("cx_Oceanbase")}
|
|
18
|
+
):
|
|
19
|
+
importlib.import_module("oceanbase_sqlalchemy." + request.param)
|
|
20
|
+
|
|
21
|
+
dbapi = mock.Mock(paramstyle="named", version="8.3.0")
|
|
22
|
+
dbapi.__future__ = types.SimpleNamespace(dml_ret_array_val=False)
|
|
23
|
+
dbapi.Error = type("DBAPIError", (Exception,), {})
|
|
24
|
+
dbapi.DatabaseError = type("DatabaseError", (dbapi.Error,), {})
|
|
25
|
+
cursor = dbapi.connect.return_value.cursor.return_value
|
|
26
|
+
cursor.description = None
|
|
27
|
+
cursor.rowcount = 1
|
|
28
|
+
|
|
29
|
+
engine = sa.create_engine("oceanbase+" + request.param + "://", module=dbapi)
|
|
30
|
+
# Bypass server discovery, while retaining compilation, parameter mapping,
|
|
31
|
+
# pre_exec, and the actual execute/executemany calls into the mock DBAPI.
|
|
32
|
+
monkeypatch.setattr(engine.dialect, "initialize", lambda connection: None)
|
|
33
|
+
try:
|
|
34
|
+
yield engine, cursor
|
|
35
|
+
finally:
|
|
36
|
+
engine.dispose()
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@pytest.mark.parametrize(
|
|
40
|
+
"name, rendered_name",
|
|
41
|
+
[
|
|
42
|
+
("姓名", '"姓名"'),
|
|
43
|
+
("p姓名", '"p姓名"'),
|
|
44
|
+
("姓名_1", '"姓名_1"'),
|
|
45
|
+
("start", "start"),
|
|
46
|
+
("START", "START"),
|
|
47
|
+
("end", "end"),
|
|
48
|
+
("p_name", "p_name"),
|
|
49
|
+
],
|
|
50
|
+
)
|
|
51
|
+
def test_text_bind_names_and_parameter_keys(dbapi_engine, name, rendered_name):
|
|
52
|
+
engine, cursor = dbapi_engine
|
|
53
|
+
with engine.connect() as connection:
|
|
54
|
+
connection.execute(sa.text("UPDATE demo SET name=:" + name), {name: "张三"})
|
|
55
|
+
|
|
56
|
+
cursor.execute.assert_called_once_with(
|
|
57
|
+
"UPDATE demo SET name=:" + rendered_name, {rendered_name: "张三"}
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_mixed_and_repeated_binds_reuse_statement(dbapi_engine):
|
|
62
|
+
engine, cursor = dbapi_engine
|
|
63
|
+
statement = sa.text(
|
|
64
|
+
"UPDATE demo SET name=:姓名, alias=:p姓名 "
|
|
65
|
+
"WHERE id BETWEEN :start AND :end OR name=:姓名"
|
|
66
|
+
)
|
|
67
|
+
expected_sql = (
|
|
68
|
+
'UPDATE demo SET name=:"姓名", alias=:"p姓名" '
|
|
69
|
+
'WHERE id BETWEEN :start AND :end OR name=:"姓名"'
|
|
70
|
+
)
|
|
71
|
+
with engine.connect() as connection:
|
|
72
|
+
for value in ["张三", "李四"]:
|
|
73
|
+
connection.execute(
|
|
74
|
+
statement, {"姓名": value, "p姓名": value, "start": 1, "end": 2}
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
assert cursor.execute.call_args_list == [
|
|
78
|
+
mock.call(
|
|
79
|
+
expected_sql, {'"姓名"': value, '"p姓名"': value, "start": 1, "end": 2}
|
|
80
|
+
)
|
|
81
|
+
for value in ["张三", "李四"]
|
|
82
|
+
]
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def test_executemany_maps_every_parameter_set(dbapi_engine):
|
|
86
|
+
engine, cursor = dbapi_engine
|
|
87
|
+
with engine.connect() as connection:
|
|
88
|
+
connection.execute(
|
|
89
|
+
sa.text("UPDATE demo SET name=:姓名 WHERE id=:start"),
|
|
90
|
+
[{"姓名": "张三", "start": 1}, {"姓名": "李四", "start": 2}],
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
cursor.executemany.assert_called_once_with(
|
|
94
|
+
'UPDATE demo SET name=:"姓名" WHERE id=:start',
|
|
95
|
+
[{'"姓名"': "张三", "start": 1}, {'"姓名"': "李四", "start": 2}],
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
@pytest.mark.parametrize("operation", ["insert", "update"])
|
|
100
|
+
def test_generated_dml_binds(dbapi_engine, operation):
|
|
101
|
+
engine, cursor = dbapi_engine
|
|
102
|
+
table = sa.Table(
|
|
103
|
+
"demo",
|
|
104
|
+
sa.MetaData(),
|
|
105
|
+
sa.Column("姓名", sa.String(20)),
|
|
106
|
+
sa.Column("start", sa.String(20)),
|
|
107
|
+
)
|
|
108
|
+
statement = getattr(table, operation)()
|
|
109
|
+
with engine.connect() as connection:
|
|
110
|
+
connection.execute(statement, {"姓名": "张三", "start": "before"})
|
|
111
|
+
|
|
112
|
+
expected_sql = {
|
|
113
|
+
"insert": 'INSERT INTO demo ("姓名", "start") VALUES (:"姓名", :start)',
|
|
114
|
+
"update": 'UPDATE demo SET "姓名"=:"姓名", "start"=:start',
|
|
115
|
+
}[operation]
|
|
116
|
+
cursor.execute.assert_called_once_with(
|
|
117
|
+
expected_sql, {'"姓名"': "张三", "start": "before"}
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
@pytest.mark.parametrize("name", ["姓名", "p姓名", "start"])
|
|
122
|
+
def test_explicit_quote_preserves_unicode_and_reserved_word_fix(dbapi_engine, name):
|
|
123
|
+
engine, cursor = dbapi_engine
|
|
124
|
+
statement = sa.text("UPDATE demo SET name=:" + name).bindparams(
|
|
125
|
+
sa.bindparam(name, quote=True)
|
|
126
|
+
)
|
|
127
|
+
rendered_name = "start" if name == "start" else '"' + name + '"'
|
|
128
|
+
with engine.connect() as connection:
|
|
129
|
+
connection.execute(statement, {name: "张三"})
|
|
130
|
+
|
|
131
|
+
cursor.execute.assert_called_once_with(
|
|
132
|
+
"UPDATE demo SET name=:" + rendered_name, {rendered_name: "张三"}
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
@pytest.mark.parametrize("name", ["123start", "_name"])
|
|
137
|
+
def test_ascii_prefix_normalization(dbapi_engine, name):
|
|
138
|
+
engine, cursor = dbapi_engine
|
|
139
|
+
rendered_name = "D" + name if sa.__version__.startswith("2.") else name
|
|
140
|
+
with engine.connect() as connection:
|
|
141
|
+
connection.execute(sa.text("UPDATE demo SET name=:" + name), {name: "value"})
|
|
142
|
+
|
|
143
|
+
cursor.execute.assert_called_once_with(
|
|
144
|
+
"UPDATE demo SET name=:" + rendered_name, {rendered_name: "value"}
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def test_expanding_reserved_word_bind(dbapi_engine):
|
|
149
|
+
engine, cursor = dbapi_engine
|
|
150
|
+
statement = sa.text("UPDATE demo SET name=:value WHERE id IN :start").bindparams(
|
|
151
|
+
sa.bindparam("start", expanding=True)
|
|
152
|
+
)
|
|
153
|
+
with engine.connect() as connection:
|
|
154
|
+
connection.execute(statement, {"value": "changed", "start": [1, 2]})
|
|
155
|
+
|
|
156
|
+
cursor.execute.assert_called_once_with(
|
|
157
|
+
"UPDATE demo SET name=:value WHERE id IN (:start_1, :start_2)",
|
|
158
|
+
{"value": "changed", "start_1": 1, "start_2": 2},
|
|
159
|
+
)
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
#!/usr/bin/env python
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
1
|
"""
|
|
4
2
|
OceanBase Dialect Optimization Task Testing
|
|
5
3
|
|
|
@@ -9,20 +7,21 @@ This file specifically tests whether the optimization tasks mentioned in Task.md
|
|
|
9
7
|
3. Validate optimization effects through real database connections
|
|
10
8
|
"""
|
|
11
9
|
|
|
12
|
-
import pytest
|
|
13
10
|
import os
|
|
11
|
+
|
|
12
|
+
import pytest
|
|
14
13
|
from sqlalchemy import (
|
|
14
|
+
Column,
|
|
15
|
+
MetaData,
|
|
16
|
+
Numeric,
|
|
17
|
+
String,
|
|
18
|
+
Table,
|
|
15
19
|
create_engine,
|
|
16
20
|
inspect,
|
|
17
21
|
text,
|
|
18
|
-
MetaData,
|
|
19
|
-
Table,
|
|
20
|
-
Column,
|
|
21
|
-
String,
|
|
22
|
-
Numeric,
|
|
23
22
|
)
|
|
24
|
-
from sqlalchemy.schema import CreateTable, DropTable
|
|
25
23
|
from sqlalchemy.exc import OperationalError
|
|
24
|
+
from sqlalchemy.schema import CreateTable, DropTable
|
|
26
25
|
|
|
27
26
|
# Load .env file
|
|
28
27
|
try:
|
|
@@ -32,7 +31,7 @@ try:
|
|
|
32
31
|
print("✅ .env file loaded successfully")
|
|
33
32
|
except ImportError:
|
|
34
33
|
print("⚠️ python-dotenv not installed, unable to load .env file")
|
|
35
|
-
except
|
|
34
|
+
except (OSError, UnicodeError) as e:
|
|
36
35
|
print(f"⚠️ Failed to load .env file: {e}")
|
|
37
36
|
|
|
38
37
|
# Print current SQLAlchemy version
|
|
@@ -194,8 +193,6 @@ class TestOceanBaseOptimization:
|
|
|
194
193
|
pytest.skip("Skipping connection test: Username or password incorrect")
|
|
195
194
|
else:
|
|
196
195
|
pytest.fail(f"OceanBase connection failed: {e}")
|
|
197
|
-
except Exception as e:
|
|
198
|
-
pytest.fail(f"Connection test exception: {e}")
|
|
199
196
|
|
|
200
197
|
|
|
201
198
|
if __name__ == "__main__":
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""Reflection query caches belong to each dialect's lifecycle."""
|
|
2
|
+
|
|
3
|
+
import gc
|
|
4
|
+
import sys
|
|
5
|
+
import types
|
|
6
|
+
import weakref
|
|
7
|
+
from unittest import mock
|
|
8
|
+
|
|
9
|
+
import pytest
|
|
10
|
+
import sqlalchemy as sa
|
|
11
|
+
|
|
12
|
+
pytestmark = pytest.mark.skipif(
|
|
13
|
+
not sa.__version__.startswith("2."),
|
|
14
|
+
reason="Dictionary query builders are used by SQLAlchemy 2.x",
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@pytest.fixture
|
|
19
|
+
def dialect_class():
|
|
20
|
+
with mock.patch.dict(
|
|
21
|
+
sys.modules, {"cx_Oceanbase": types.ModuleType("cx_Oceanbase")}
|
|
22
|
+
):
|
|
23
|
+
from oceanbase_sqlalchemy.cx_oracle import OceanBaseDialect_cx_oracle
|
|
24
|
+
|
|
25
|
+
yield OceanBaseDialect_cx_oracle
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@pytest.mark.parametrize("method", ["_constraint_query", "_index_query"])
|
|
29
|
+
def test_query_cache_isolated_by_owner_and_dialect(dialect_class, method):
|
|
30
|
+
first = dialect_class()
|
|
31
|
+
second = dialect_class()
|
|
32
|
+
query = getattr(first, method)("OWNER_A")
|
|
33
|
+
|
|
34
|
+
assert getattr(first, method)("OWNER_A") is query
|
|
35
|
+
assert getattr(first, method)("OWNER_B") is not query
|
|
36
|
+
assert getattr(second, method)("OWNER_A") is not query
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@pytest.mark.parametrize("method", ["_constraint_query", "_index_query"])
|
|
40
|
+
def test_query_cache_bounded_per_dialect(dialect_class, method):
|
|
41
|
+
first = dialect_class()
|
|
42
|
+
second = dialect_class()
|
|
43
|
+
first_query = getattr(first, method)("OWNER_A")
|
|
44
|
+
oldest_query = getattr(second, method)("OWNER_0")
|
|
45
|
+
for index in range(1, 129):
|
|
46
|
+
getattr(second, method)("OWNER_" + str(index))
|
|
47
|
+
|
|
48
|
+
# A busy dialect evicts its own oldest query, without evicting other
|
|
49
|
+
# dialects' cached queries.
|
|
50
|
+
assert getattr(first, method)("OWNER_A") is first_query
|
|
51
|
+
assert getattr(second, method)("OWNER_0") is not oldest_query
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@pytest.mark.parametrize(
|
|
55
|
+
"method, use_parent_query",
|
|
56
|
+
[("_constraint_query", False), ("_index_query", False), ("_index_query", True)],
|
|
57
|
+
)
|
|
58
|
+
def test_query_cache_does_not_retain_dialect(
|
|
59
|
+
dialect_class, method, use_parent_query, monkeypatch
|
|
60
|
+
):
|
|
61
|
+
if use_parent_query:
|
|
62
|
+
monkeypatch.setattr(
|
|
63
|
+
sys.modules[dialect_class.__module__],
|
|
64
|
+
"_dictionary_has_ind_expressions",
|
|
65
|
+
False,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
def cached_dialect_reference():
|
|
69
|
+
dialect = dialect_class()
|
|
70
|
+
getattr(dialect, method)("OWNER_A")
|
|
71
|
+
return weakref.ref(dialect)
|
|
72
|
+
|
|
73
|
+
reference = cached_dialect_reference()
|
|
74
|
+
gc.collect()
|
|
75
|
+
|
|
76
|
+
assert reference() is None
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{oceanbase_sqlalchemy-0.6.0 → oceanbase_sqlalchemy-0.8.0}/oceanbase_sqlalchemy.egg-info/not-zip-safe
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|