dmpython-jdbc 2.5.32.post2__tar.gz → 2.5.32.post3__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.
Files changed (44) hide show
  1. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/MANIFEST.in +1 -1
  2. dmpython_jdbc-2.5.32.post3/PKG-INFO +178 -0
  3. dmpython_jdbc-2.5.32.post2/README.md → dmpython_jdbc-2.5.32.post3/README.en.md +1 -1
  4. dmpython_jdbc-2.5.32.post2/README.zh-CN.md → dmpython_jdbc-2.5.32.post3/README.md +2 -16
  5. dmpython_jdbc-2.5.32.post3/_dmPython_jdbc/__init__.py +48 -0
  6. dmpython_jdbc-2.5.32.post3/_dmPython_jdbc/_sync.py +11 -0
  7. dmpython_jdbc-2.5.32.post3/_dmPython_jdbc/connection.py +174 -0
  8. dmpython_jdbc-2.5.32.post3/_dmPython_jdbc/cursor.py +244 -0
  9. dmpython_jdbc-2.5.32.post3/_dmPython_jdbc/dbtypes.py +68 -0
  10. dmpython_jdbc-2.5.32.post3/_dmPython_jdbc/dmsqlalchemy.py +63 -0
  11. dmpython_jdbc-2.5.32.post3/_dmPython_jdbc/exceptions.py +26 -0
  12. dmpython_jdbc-2.5.32.post3/_dmPython_jdbc/jdbc/__init__.py +11 -0
  13. dmpython_jdbc-2.5.32.post3/_dmPython_jdbc/jdbc/converters.py +74 -0
  14. dmpython_jdbc-2.5.32.post3/_dmPython_jdbc/jdbc/driver.py +49 -0
  15. dmpython_jdbc-2.5.32.post3/_dmPython_jdbc/jdbc/errors.py +16 -0
  16. dmpython_jdbc-2.5.32.post3/_dmPython_jdbc/jdbc/generated_keys.py +42 -0
  17. dmpython_jdbc-2.5.32.post3/_dmPython_jdbc/jdbc/sql.py +27 -0
  18. dmpython_jdbc-2.5.32.post3/dmpython_jdbc.egg-info/PKG-INFO +178 -0
  19. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/dmpython_jdbc.egg-info/SOURCES.txt +13 -1
  20. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/pyproject.toml +2 -2
  21. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/tests/e2e/test_live_dmasync.py +33 -1
  22. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/tests/test_dmasync.py +79 -4
  23. dmpython_jdbc-2.5.32.post2/PKG-INFO +0 -200
  24. dmpython_jdbc-2.5.32.post2/_dmPython_jdbc/__init__.py +0 -682
  25. dmpython_jdbc-2.5.32.post2/dmpython_jdbc.egg-info/PKG-INFO +0 -200
  26. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/LICENSE +0 -0
  27. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/NOTICE +0 -0
  28. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/_dmPython_jdbc/DmJdbcDriver18-8.1.3.140.jar +0 -0
  29. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/_dmpython_metadata.py +0 -0
  30. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/dmPython.py +0 -0
  31. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/dmpython-2.5.32.dist-info/METADATA +0 -0
  32. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/dmpython_jdbc.egg-info/dependency_links.txt +0 -0
  33. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/dmpython_jdbc.egg-info/requires.txt +0 -0
  34. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/dmpython_jdbc.egg-info/top_level.txt +0 -0
  35. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/setup.cfg +0 -0
  36. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/setup.py +0 -0
  37. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/tests/e2e/.env.example +0 -0
  38. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/tests/e2e/README.md +0 -0
  39. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/tests/e2e/bootstrap.py +0 -0
  40. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/tests/e2e/compose.yaml +0 -0
  41. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/tests/e2e/healthcheck.sh +0 -0
  42. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/tests/e2e/run.py +0 -0
  43. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/tests/e2e/start-dm.sh +0 -0
  44. {dmpython_jdbc-2.5.32.post2 → dmpython_jdbc-2.5.32.post3}/tests/e2e/test.sh +0 -0
@@ -1,5 +1,5 @@
1
1
  include README.md
2
- include README.zh-CN.md
2
+ include README.en.md
3
3
  include LICENSE
4
4
  include NOTICE
5
5
  include pyproject.toml
@@ -0,0 +1,178 @@
1
+ Metadata-Version: 2.4
2
+ Name: dmpython-jdbc
3
+ Version: 2.5.32.post3
4
+ Summary: macOS dmPython compatibility shim backed by JayDeBeApi
5
+ Author-email: Darley <darley.wey@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/Darley-Wey/dmpython-jdbc
8
+ Project-URL: Repository, https://github.com/Darley-Wey/dmpython-jdbc
9
+ Project-URL: Issues, https://github.com/Darley-Wey/dmpython-jdbc/issues
10
+ Keywords: dameng,dmPython,jdbc,macos
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Operating System :: MacOS
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Topic :: Database
18
+ Requires-Python: >=3.12
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+ Requires-Dist: JayDeBeApi>=1.2.3
22
+ Dynamic: license-file
23
+
24
+ # dmpython-jdbc
25
+
26
+ 简体中文 | [English](README.en.md)
27
+
28
+ macOS 上的达梦 `dmPython` 兼容层。官方驱动没有 macOS 版,这里用 JayDeBeApi + 达梦 JDBC 实现 DBAPI 子集,让 `dmSQLAlchemy` 的 `dm+dmPython://` 方言能在本机跑起来。
29
+
30
+ **发行名是 `dmpython-jdbc`,import 名仍是 `dmPython`。** 业务代码不用改。
31
+
32
+ - macOS:使用本包的 JDBC shim
33
+ - 其它平台:把 `import dmPython` 转发给已安装的官方驱动
34
+
35
+ ## 安装
36
+
37
+ PyPI:
38
+
39
+ ```bash
40
+ pip install dmpython-jdbc
41
+ ```
42
+
43
+ 跨平台项目(Linux/Windows 用官方驱动,macOS 用本包):
44
+
45
+ ```toml
46
+ [project]
47
+ dependencies = [
48
+ "dmpython>=2.5.32; sys_platform != 'darwin'",
49
+ "dmpython-jdbc>=2.5.32; sys_platform == 'darwin'",
50
+ ]
51
+ ```
52
+
53
+ 如果项目还依赖 `dmSQLAlchemy`,见 [配合 dmSQLAlchemy](#配合-dmsqlalchemy)。该包会无条件拉取官方 `dmpython`。
54
+
55
+ ## 运行依赖
56
+
57
+ - Python >= 3.12
58
+ - JDK(macOS 可用 `brew install openjdk`)。`JAVA_HOME` 会自动探测 `/opt/homebrew/opt/openjdk` 和 `/usr/local/opt/openjdk`
59
+ - 达梦 JDBC 驱动已打包进 wheel;也可用环境变量 `DM_JDBC_JAR` 覆盖
60
+
61
+
62
+ ## 配合 dmSQLAlchemy
63
+
64
+ `dmSQLAlchemy` 无条件依赖官方 `dmpython`(`Requires-Dist: dmPython`)。该包没有 macOS wheel,所以在 Mac 上 `pip install dmSQLAlchemy` 会尝试装达梦官方驱动并失败。
65
+
66
+ 要保留 `dmSQLAlchemy`,又阻止解析器在 macOS 上装官方 `dmpython`,改装 `dmpython-jdbc`。import 名仍是 `dmPython`。
67
+
68
+ ### uv
69
+
70
+ ```toml
71
+ [project]
72
+ dependencies = [
73
+ "dmsqlalchemy",
74
+ "dmpython>=2.5.32; sys_platform != 'darwin'",
75
+ "dmpython-jdbc>=2.5.32; sys_platform == 'darwin'",
76
+ ]
77
+
78
+ [tool.uv]
79
+ override-dependencies = [
80
+ "dmpython ; sys_platform != 'darwin'",
81
+ ]
82
+ ```
83
+
84
+ `override-dependencies` 会改写 `dmSQLAlchemy` 对 `dmpython` 的依赖,让它在 macOS 上被跳过;然后由 `dmpython-jdbc` 提供 `dmPython` 模块。
85
+
86
+ `dmSQLAlchemy.extensions` 会调用 `importlib.metadata.version("dmPython")`。本包会安装 `.pth` 钩子,让这次查询返回 shim 版本 `2.5.32`,不依赖 setuptools/`pkg_resources`。
87
+
88
+ ### macOS 上的 pip
89
+
90
+ ```bash
91
+ pip install "dmSQLAlchemy" --no-deps
92
+ pip install "SQLAlchemy>1.4.54" dmpython-jdbc
93
+ ```
94
+
95
+ ## 作为 dmAsync 的 dmPython 后端
96
+
97
+ 本包支持 `dmAsync==1.0.0` 使用的基础连接和游标接口:`connect`、`execute`、
98
+ `executemany`、`fetchone`/`fetchmany`/`fetchall`、异步迭代、`commit`、`rollback`
99
+ 和 `ping`。dmAsync 使用 `asyncio.to_thread` 调用同步 shim 完成这些数据库操作;
100
+ dmAsync 1.0.0 的建连和关闭仍是同步操作。
101
+
102
+ macOS 上在开发版仓库目录中安装:
103
+
104
+ ```bash
105
+ python -m pip install . "async-timeout>=4,<6"
106
+ python -m pip install "dmAsync==1.0.0" --no-deps
107
+ ```
108
+
109
+ `dmAsync` 无条件依赖官方 `dmPython`,因此使用 uv 时,需要保留上文的
110
+ `[tool.uv].override-dependencies` 配置,并在项目依赖中加入 `dmAsync==1.0.0`
111
+ 和 `async-timeout>=4,<6`。dmAsync 1.0.0 导入了 `async_timeout`,但没有在包元数据中
112
+ 声明该依赖。运行时的版本别名不能代替这些依赖解析配置。
113
+
114
+ ```python
115
+ import asyncio
116
+ import os
117
+ import dmAsync
118
+
119
+
120
+ async def main():
121
+ conn = await dmAsync.connect(
122
+ host=os.environ["DM_HOST"],
123
+ port=int(os.environ.get("DM_PORT", "5236")),
124
+ user=os.environ["DM_USER"],
125
+ password=os.environ["DM_PASSWORD"],
126
+ autoCommit=False,
127
+ login_timeout=5000, # 毫秒,直接传给 dmPython。
128
+ )
129
+ cur = await conn.cursor()
130
+ try:
131
+ await cur.execute("SELECT 1")
132
+ print(await cur.fetchone())
133
+ await conn.commit()
134
+ except Exception:
135
+ await conn.rollback()
136
+ raise
137
+ finally:
138
+ await cur.close()
139
+ await conn.close()
140
+
141
+
142
+ asyncio.run(main())
143
+ ```
144
+
145
+ `Connection.version` 返回服务端版本;标识符长度限制从 JDBC 元数据读取,未知时
146
+ 返回 `None`。shim 不实现 Python 层语句缓存,因此 `stmtcachesize` 为 `0`,
147
+ 不支持设为非零值。`status` 表示 shim 连接状态(`1` 开启、`0` 关闭),不是 DPI
148
+ 事务状态码。修改 `autocommit` 会同步到 JDBC 连接。
149
+
150
+ 同一连接的 JDBC 操作会在线程间串行执行。并发任务应各自持有连接:锁无法隔离事务,
151
+ 也无法保证多个任务共享游标时 execute/fetch 的先后关系。取消 await 不会终止线程中
152
+ 已经开始的 JDBC 操作;重新使用或关闭该连接前,应等待操作结束。`callproc`、
153
+ `callfunc`、显式 `prepare`、`mogrify`、游标滚动、跨提交保留游标以及多结果集仍不支持,
154
+ 调用会抛出 `NotSupportedError`。这些适配不代表已完整验证 SQLAlchemy 的
155
+ `dm+dmasync://` 方言或 dmAsync 连接池。
156
+
157
+ ### 适配测试
158
+
159
+ 安装上述依赖后执行:
160
+
161
+ ```bash
162
+ python -m unittest discover -s tests -v
163
+ ```
164
+
165
+ 测试使用真实 dmAsync 包,在 JayDeBeApi 边界用 SQLite 模拟数据库,覆盖初始化、
166
+ CRUD、事务、游标生命周期和连接级锁,不需要达梦服务器。
167
+
168
+ 真库验证见 [达梦端到端测试说明](tests/e2e/README.md)。配置专用测试账号后,执行
169
+ `bash tests/e2e/test.sh` 即可构建并测试实际安装的 wheel,覆盖二进制/LOB 数据往返、
170
+ 跨连接事务可见性等。
171
+
172
+ ## 限制
173
+
174
+ 不支持存储过程出参(`cursor.var`)、LOB 流式读写等高级特性,覆盖 ORM CRUD 即可。
175
+
176
+ ## 许可证
177
+
178
+ Python 兼容层使用 MIT。仓库中的 `DmJdbcDriver18-*.jar` 是达梦官方 JDBC 驱动,版权归达梦数据库所有,不受 MIT 覆盖。
@@ -1,6 +1,6 @@
1
1
  # dmpython-jdbc
2
2
 
3
- [English](README.md) | [简体中文](README.zh-CN.md)
3
+ [简体中文](README.md) | English
4
4
 
5
5
  macOS compatibility layer for Dameng `dmPython`. Dameng does not ship a macOS driver, so this package implements a DBAPI subset with JayDeBeApi + Dameng JDBC and makes dmSQLAlchemy's `dm+dmPython://` dialect work locally.
6
6
 
@@ -1,6 +1,6 @@
1
1
  # dmpython-jdbc
2
2
 
3
- [English](README.md) | [简体中文](README.zh-CN.md)
3
+ 简体中文 | [English](README.en.md)
4
4
 
5
5
  macOS 上的达梦 `dmPython` 兼容层。官方驱动没有 macOS 版,这里用 JayDeBeApi + 达梦 JDBC 实现 DBAPI 子集,让 `dmSQLAlchemy` 的 `dm+dmPython://` 方言能在本机跑起来。
6
6
 
@@ -29,21 +29,6 @@ dependencies = [
29
29
 
30
30
  如果项目还依赖 `dmSQLAlchemy`,见 [配合 dmSQLAlchemy](#配合-dmsqlalchemy)。该包会无条件拉取官方 `dmpython`。
31
31
 
32
- 从 Git 安装:
33
-
34
- ```bash
35
- pip install "dmpython-jdbc @ git+https://github.com/Darley-Wey/dmpython-jdbc.git"
36
- ```
37
-
38
- uv:
39
-
40
- ```toml
41
- [tool.uv.sources]
42
- dmpython-jdbc = { git = "https://github.com/Darley-Wey/dmpython-jdbc.git" }
43
- ```
44
-
45
- 需要钉到某个提交或 tag 时,在 URL 后加 `@<ref>`,例如 `@main` 或 `@v2.5.32`。
46
-
47
32
  ## 运行依赖
48
33
 
49
34
  - Python >= 3.12
@@ -160,6 +145,7 @@ CRUD、事务、游标生命周期和连接级锁,不需要达梦服务器。
160
145
  真库验证见 [达梦端到端测试说明](tests/e2e/README.md)。配置专用测试账号后,执行
161
146
  `bash tests/e2e/test.sh` 即可构建并测试实际安装的 wheel,覆盖二进制/LOB 数据往返、
162
147
  跨连接事务可见性等。
148
+
163
149
  ## 限制
164
150
 
165
151
  不支持存储过程出参(`cursor.var`)、LOB 流式读写等高级特性,覆盖 ORM CRUD 即可。
@@ -0,0 +1,48 @@
1
+ """
2
+ dmPython-compatible shim for local macOS development.
3
+
4
+ Dameng does not ship a macOS dmPython build (libdmdpi is closed-source),
5
+ so this module implements a DBAPI subset with JayDeBeApi + DmJdbcDriver
6
+ and makes dmSQLAlchemy's `dm+dmPython://` dialect usable on Mac.
7
+
8
+ Dependencies:
9
+ - JayDeBeApi is installed with this package (JPype1 comes with it)
10
+ - brew install openjdk (JAVA_HOME is probed at /opt/homebrew/opt/openjdk)
11
+ - Dameng JDBC driver JAR is packaged; override with DM_JDBC_JAR
12
+
13
+ Limits: no cursor.var / OUT binds, no streaming LOB I/O. ORM CRUD is enough.
14
+
15
+ Layout, top layer first; each imports only from those below it:
16
+ __init__ the public dmPython module surface
17
+ connection, cursor DBAPI objects
18
+ dmsqlalchemy dialect adjustments and stubs only dmSQLAlchemy needs
19
+ jdbc/ Java, JayDeBeApi and Dameng JDBC driver details
20
+ exceptions, dbtypes DBAPI vocabulary shared by every layer
21
+ """
22
+ from . import dbtypes, exceptions
23
+ from .connection import Connection, connect
24
+ from .cursor import Cursor
25
+ from .dbtypes import * # noqa: F401,F403
26
+ from .dmsqlalchemy import objedctvar, parse_mysql_stmt, parse_tsql_stmt
27
+ from .exceptions import * # noqa: F401,F403
28
+
29
+ version = "2.5.32" # spoof the official version; dmSQLAlchemy parses it
30
+ apilevel = "2.0"
31
+ threadsafety = 1
32
+ paramstyle = "qmark" # compiled to JDBC `?`; dialect do_execute uses positional binds
33
+
34
+ # dmPython.py re-exports exactly these names as the dmPython module on macOS.
35
+ __all__ = [
36
+ "version",
37
+ "apilevel",
38
+ "threadsafety",
39
+ "paramstyle",
40
+ "connect",
41
+ "Connection",
42
+ "Cursor",
43
+ *exceptions.__all__,
44
+ *dbtypes.__all__,
45
+ "parse_mysql_stmt",
46
+ "parse_tsql_stmt",
47
+ "objedctvar",
48
+ ]
@@ -0,0 +1,11 @@
1
+ """Per-connection locking shared by Connection and Cursor."""
2
+ from functools import wraps
3
+
4
+
5
+ def serialized(method):
6
+ """Serialize JDBC access across dmAsync's worker threads per connection."""
7
+ @wraps(method)
8
+ def locked(self, *args, **kwargs):
9
+ with self._lock:
10
+ return method(self, *args, **kwargs)
11
+ return locked
@@ -0,0 +1,174 @@
1
+ """DBAPI connection over a JayDeBeApi connection, and connect()."""
2
+ from threading import RLock
3
+
4
+ from ._sync import serialized
5
+ from .cursor import Cursor
6
+ from .dmsqlalchemy import patch_dialect_no_returning
7
+ from .exceptions import InterfaceError, NotSupportedError
8
+ from .jdbc.driver import open_connection
9
+
10
+
11
+ class Connection:
12
+ def __init__(self, jconn, schema=None):
13
+ self._jconn = jconn
14
+ self._lock = RLock()
15
+ self.outputtypehandler = None
16
+ self.inputtypehandler = None
17
+ self.server_version = "8.1.3.140"
18
+ self.max_identifier_length = None
19
+ self.current_schema = schema # dmSQLAlchemy reads the default schema here
20
+ self.local_code = 1 # 1 = utf-8; dmSQLAlchemy reads this
21
+ self._closed = False
22
+ try:
23
+ metadata = self._jconn.jconn.getMetaData()
24
+ self.server_version = str(metadata.getDatabaseProductVersion())
25
+ lengths = [int(metadata.getMaxColumnNameLength()),
26
+ int(metadata.getMaxTableNameLength())]
27
+ self.max_identifier_length = min((n for n in lengths if n > 0), default=None)
28
+ except Exception:
29
+ # Retain legacy version fallback if the driver cannot provide metadata.
30
+ pass
31
+ try:
32
+ cur = self._jconn.cursor()
33
+ try:
34
+ cur.execute("SELECT CASE_SENSITIVE()")
35
+ row = cur.fetchone()
36
+ finally:
37
+ cur.close()
38
+ self.str_case_sensitive = bool(row and row[0] in (1, 'Y', 'y', True))
39
+ except Exception:
40
+ self.str_case_sensitive = True
41
+
42
+ @property
43
+ def version(self):
44
+ """Server version consumed by dmAsync (not the Python package version)."""
45
+ return self.server_version
46
+
47
+ @property
48
+ def stmtcachesize(self):
49
+ """The shim does not maintain a Python statement cache."""
50
+ return 0
51
+
52
+ @stmtcachesize.setter
53
+ def stmtcachesize(self, value):
54
+ if value != 0:
55
+ raise NotSupportedError("JDBC shim does not support a Python statement cache")
56
+
57
+ @property
58
+ @serialized
59
+ def closed(self):
60
+ return self._closed or bool(self._jconn.jconn.isClosed())
61
+
62
+ @property
63
+ def status(self):
64
+ """Shim liveness: 1 while open, 0 when closed; not a DPI transaction code."""
65
+ return int(not self.closed)
66
+
67
+ @property
68
+ @serialized
69
+ def autocommit(self):
70
+ self._check_open()
71
+ return bool(self._jconn.jconn.getAutoCommit())
72
+
73
+ @autocommit.setter
74
+ @serialized
75
+ def autocommit(self, value):
76
+ self._check_open()
77
+ self._jconn.jconn.setAutoCommit(bool(value))
78
+
79
+ autoCommit = autocommit
80
+
81
+ def _check_open(self):
82
+ if self.closed:
83
+ raise InterfaceError("not connected")
84
+
85
+ @serialized
86
+ def cursor(self, *args, **kw):
87
+ self._check_open()
88
+ return Cursor(self._jconn.cursor(), self)
89
+
90
+ @serialized
91
+ def commit(self):
92
+ self._check_open()
93
+ self._jconn.commit()
94
+
95
+ @serialized
96
+ def rollback(self):
97
+ if self._closed:
98
+ return
99
+ try:
100
+ self._jconn.rollback()
101
+ except Exception:
102
+ pass
103
+
104
+ @serialized
105
+ def close(self):
106
+ if self._closed:
107
+ return
108
+ try:
109
+ self._jconn.close()
110
+ except Exception:
111
+ pass
112
+ self._closed = True
113
+
114
+ @serialized
115
+ def ping(self, *a, **kw):
116
+ cur = self.cursor()
117
+ try:
118
+ cur.execute("SELECT 1")
119
+ cur.fetchone()
120
+ finally:
121
+ cur.close()
122
+
123
+ def __enter__(self):
124
+ return self
125
+
126
+ def __exit__(self, *exc):
127
+ self.close()
128
+
129
+
130
+ def _jdbc_url(kw):
131
+ """JDBC URL for dmPython.connect() keyword arguments."""
132
+ dsn = kw.get("dsn")
133
+ if dsn:
134
+ host, _, port = dsn.partition(":")
135
+ else:
136
+ host, port = kw.get("host", "localhost"), str(kw.get("port", 5236))
137
+ url = f"jdbc:dm://{host}:{port}"
138
+ props = {}
139
+ if kw.get("schema"):
140
+ props["schema"] = kw["schema"]
141
+ # local_code is not forwarded: JDBC handles encoding.
142
+ # Official dmPython: login_timeout is milliseconds, connection_timeout is seconds.
143
+ # JDBC uses different property names. Forward them here; unknown kwargs stay ignored.
144
+ login_timeout = kw.get("login_timeout")
145
+ if login_timeout is not None:
146
+ props["loginTimeout"] = max(1, int(round(int(login_timeout) / 1000)))
147
+ connection_timeout = kw.get("connection_timeout")
148
+ if connection_timeout is not None:
149
+ props["socketTimeout"] = int(connection_timeout) * 1000
150
+ if props:
151
+ url += "?" + "&".join(f"{k}={v}" for k, v in props.items())
152
+ return url
153
+
154
+
155
+ def connect(*args, **kw):
156
+ """Compatible with dmPython.connect(user=..., password=..., dsn='host:port', schema=..., ...)."""
157
+ patch_dialect_no_returning()
158
+ user = kw.get("user") or kw.get("username") or "SYSDBA"
159
+ password = kw.get("password", "")
160
+ schema = kw.get("schema")
161
+ jconn = open_connection(_jdbc_url(kw), user, password)
162
+ autocommit = kw.get("autoCommit", kw.get("autocommit", False))
163
+ jconn.jconn.setAutoCommit(bool(autocommit))
164
+ conn = Connection(jconn, schema=schema)
165
+ if conn.current_schema is None:
166
+ try:
167
+ cur = conn.cursor()
168
+ cur.execute("SELECT SYS_CONTEXT('userenv','current_schema') FROM dual")
169
+ row = cur.fetchone()
170
+ cur.close()
171
+ conn.current_schema = row[0] if row else user
172
+ except Exception:
173
+ conn.current_schema = user
174
+ return conn