Skip to content

Commit bf2cefc

Browse files
authored
fix: Force enable Unicode string binding to fix NVARCHAR2 query failures (#34)
* fix: update .gitignore and cx_oracle.py * chore: bump version to 0.5.0
1 parent ceee4b4 commit bf2cefc

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
git-pre-push-hook-ban.log
2+
.github/.DS_Store
3+
.github/workflows/.DS_Store
4+
.idea/.gitignore
5+
.idea/ecology-plugins.iml
6+
.idea/MarsCodeWorkspaceAppSettings.xml
7+
.idea/misc.xml
8+
.idea/modules.xml
9+
.idea/vcs.xml
10+
.idea/inspectionProfiles/profiles_settings.xml
11+
.idea/inspectionProfiles/Project_Default.xml

oceanbase-sqlalchemy-plugin/oceanbase_sqlalchemy/cx_oracle.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,37 @@ def create_connect_args(self, url):
5656

5757
return super().create_connect_args(url)
5858

59-
@reflection.cache
59+
def _get_server_version_info(self, connection):
60+
"""
61+
Get OceanBase server version information
62+
63+
Issue: cx_Oceanbase driver returns "0.0.0.0.1" for connection.version,
64+
causing SQLAlchemy to misidentify it as Oracle 8 and set supports_unicode_binds to False,
65+
which encodes all string parameters as bytes, resulting in NVARCHAR2 query failures.
66+
67+
Solution: Extract the real OceanBase version number from v$version banner.
68+
"""
69+
70+
# Fallback: Return a reasonable default version
71+
# Note: Due to Python tuple comparison (4, x, x) < (9,) evaluates to True
72+
# Use (21, 0, 0, 0, 0) to simulate Oracle 21g, ensuring it won't be misidentified as Oracle 8
73+
return (21, 0, 0, 0, 0)
74+
75+
def initialize(self, connection):
76+
"""
77+
Initialize dialect to ensure OceanBase uses Unicode string binding
78+
79+
OceanBase is compatible with Oracle 10g+, supporting Unicode binding.
80+
Even if the version number might be misjudged, force enable supports_unicode_binds.
81+
"""
82+
# Call parent class initialization
83+
super().initialize(connection)
84+
85+
# Force enable Unicode binding support
86+
# OceanBase fully supports Unicode and should not use bytes encoding
87+
self.supports_unicode_binds = True
88+
89+
@reflection.cache()
6090
def _get_constraint_data(
6191
self, connection, table_name, schema=None, dblink="", **kw
6292
):

oceanbase-sqlalchemy-plugin/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="oceanbase-sqlalchemy",
8-
version="0.4.0",
8+
version="0.5.0",
99
description="SQLAlchemy dialect for OceanBase Oracle mode (supports SQLAlchemy 1.3.x and 2.0+)",
1010
long_description=open("README.md", "r", encoding="utf-8").read(),
1111
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)