justybase-db2 0.1.0a1__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.
Files changed (42) hide show
  1. justybase_db2/__init__.py +224 -0
  2. justybase_db2/_cancellation.py +113 -0
  3. justybase_db2/_rows.py +899 -0
  4. justybase_db2/_session.py +499 -0
  5. justybase_db2/async_/__init__.py +47 -0
  6. justybase_db2/async_/connection.py +2102 -0
  7. justybase_db2/async_/cursor.py +211 -0
  8. justybase_db2/async_/pool.py +558 -0
  9. justybase_db2/async_/transport.py +160 -0
  10. justybase_db2/auth/handshake.py +336 -0
  11. justybase_db2/auth/secmec9.py +1009 -0
  12. justybase_db2/batch.py +67 -0
  13. justybase_db2/codecs/decfloat.py +367 -0
  14. justybase_db2/codecs/decimal.py +110 -0
  15. justybase_db2/codecs/ebcdic.py +289 -0
  16. justybase_db2/command_options.py +95 -0
  17. justybase_db2/config.py +518 -0
  18. justybase_db2/connection.py +2064 -0
  19. justybase_db2/cursor.py +249 -0
  20. justybase_db2/diagnostics.py +72 -0
  21. justybase_db2/errors.py +321 -0
  22. justybase_db2/import_.py +688 -0
  23. justybase_db2/lob.py +316 -0
  24. justybase_db2/pool.py +613 -0
  25. justybase_db2/procedure.py +574 -0
  26. justybase_db2/protocol/codepoints.py +252 -0
  27. justybase_db2/protocol/ddm.py +1287 -0
  28. justybase_db2/protocol/dss.py +351 -0
  29. justybase_db2/protocol/sql_dta.py +741 -0
  30. justybase_db2/protocol/sql_types.py +133 -0
  31. justybase_db2/py.typed +0 -0
  32. justybase_db2/resources.py +230 -0
  33. justybase_db2/statement.py +19 -0
  34. justybase_db2/transport/__init__.py +7 -0
  35. justybase_db2/transport/tcp.py +182 -0
  36. justybase_db2/transport/tls.py +28 -0
  37. justybase_db2-0.1.0a1.dist-info/METADATA +474 -0
  38. justybase_db2-0.1.0a1.dist-info/RECORD +42 -0
  39. justybase_db2-0.1.0a1.dist-info/WHEEL +5 -0
  40. justybase_db2-0.1.0a1.dist-info/licenses/LICENSE +21 -0
  41. justybase_db2-0.1.0a1.dist-info/licenses/THIRD-PARTY-NOTICES.md +35 -0
  42. justybase_db2-0.1.0a1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,224 @@
1
+ """JustyBase.Db2: a native IBM Db2 LUW driver for Python.
2
+
3
+ The driver speaks DRDA/DDM directly over TCP/TLS and does not require IBM CLI,
4
+ ODBC, JDBC, or any other Db2 client runtime.
5
+
6
+ Synchronous API (PEP 249 / DB-API 2.0)::
7
+
8
+ import justybase_db2
9
+
10
+ with justybase_db2.connect("db2://user:password@localhost:50000/TESTDB") as conn:
11
+ with conn.cursor() as cur:
12
+ cur.execute("SELECT ID, NAME FROM APP.CUSTOMERS WHERE ID = ?", (42,))
13
+ for row in cur:
14
+ print(row)
15
+
16
+ Asynchronous API::
17
+
18
+ from justybase_db2.async_ import connect
19
+
20
+ async with await connect(dsn) as conn:
21
+ async with conn.cursor() as cur:
22
+ await cur.execute("SELECT 1 FROM SYSIBM.SYSDUMMY1")
23
+ async for row in cur:
24
+ print(row)
25
+ """
26
+
27
+ from __future__ import annotations
28
+
29
+ import datetime
30
+
31
+ from .async_.pool import clear_all_async_pools
32
+ from .batch import BatchOptions
33
+ from .command_options import CommandOptions
34
+ from .config import (
35
+ Config,
36
+ ConfigError,
37
+ ProtocolLimits,
38
+ normalize_current_schema,
39
+ parse_dsn,
40
+ redact_dsn,
41
+ )
42
+ from .connection import (
43
+ Connection,
44
+ InsertResult,
45
+ ProcedureResult,
46
+ QueryResult,
47
+ RowReader,
48
+ Transaction,
49
+ connect,
50
+ )
51
+ from .cursor import Cursor
52
+ from .diagnostics import Db2Diagnostic, Db2Warning, observe_diagnostics
53
+ from .errors import (
54
+ BatchError,
55
+ DatabaseError,
56
+ DataError,
57
+ Error,
58
+ ErrorKind,
59
+ IntegrityError,
60
+ InterfaceError,
61
+ InternalError,
62
+ NotSupportedError,
63
+ OperationalError,
64
+ ProgrammingError,
65
+ ResourceLimitError,
66
+ Warning,
67
+ )
68
+ from .import_ import (
69
+ ImportOptions,
70
+ ImportPolicy,
71
+ ImportProgress,
72
+ ImportTarget,
73
+ async_import_rows,
74
+ import_rows,
75
+ )
76
+ from .lob import (
77
+ STREAM_LOB_THRESHOLD,
78
+ AsyncProgressiveLobReader,
79
+ ProgressiveLobReader,
80
+ blob,
81
+ clob,
82
+ xml,
83
+ )
84
+ from .pool import AsyncPoolOptions, Pool, PoolOptions, PoolStats, clear_all_pools
85
+ from .resources import (
86
+ CHUNK_BYTES,
87
+ MAX_MATERIALIZED_LOB_BYTES,
88
+ MAX_MATERIALIZED_RESULT_BYTES,
89
+ MAX_STREAMED_LOB_BYTES,
90
+ CancelToken,
91
+ ResourceBudget,
92
+ ResourceReservation,
93
+ default_spool_budget,
94
+ )
95
+ from .statement import PreparedStatement
96
+
97
+ # --------------------------------------------------------------- DB-API globals.
98
+ apilevel = "2.0"
99
+ threadsafety = 1
100
+ paramstyle = "qmark"
101
+
102
+
103
+ def Date(year: int, month: int, day: int) -> datetime.date:
104
+ return datetime.date(year, month, day)
105
+
106
+
107
+ def Time(hour: int, minute: int, second: int) -> datetime.time:
108
+ return datetime.time(hour, minute, second)
109
+
110
+
111
+ def Timestamp(
112
+ year: int, month: int, day: int, hour: int, minute: int, second: int
113
+ ) -> datetime.datetime:
114
+ return datetime.datetime(year, month, day, hour, minute, second)
115
+
116
+
117
+ def DateFromTicks(ticks: float) -> datetime.date:
118
+ return datetime.date.fromtimestamp(ticks)
119
+
120
+
121
+ def TimeFromTicks(ticks: float) -> datetime.time:
122
+ return datetime.datetime.fromtimestamp(ticks).time()
123
+
124
+
125
+ def TimestampFromTicks(ticks: float) -> datetime.datetime:
126
+ return datetime.datetime.fromtimestamp(ticks)
127
+
128
+
129
+ class _Binary:
130
+ def __init__(self, value: bytes) -> None:
131
+ self.value = value
132
+
133
+ def __bytes__(self) -> bytes:
134
+ return self.value
135
+
136
+
137
+ def Binary(value: bytes) -> _Binary:
138
+ return _Binary(value)
139
+
140
+
141
+ STRING = str
142
+ BINARY = bytes
143
+ NUMBER = float
144
+ DATETIME = datetime.datetime
145
+ ROWID = int
146
+
147
+ __version__ = "0.1.0a1"
148
+
149
+ __all__ = [
150
+ "BINARY",
151
+ "CHUNK_BYTES",
152
+ "DATETIME",
153
+ "MAX_MATERIALIZED_LOB_BYTES",
154
+ "MAX_MATERIALIZED_RESULT_BYTES",
155
+ "MAX_STREAMED_LOB_BYTES",
156
+ "NUMBER",
157
+ "ROWID",
158
+ "STREAM_LOB_THRESHOLD",
159
+ "STRING",
160
+ "AsyncPoolOptions",
161
+ "AsyncProgressiveLobReader",
162
+ "BatchError",
163
+ "BatchOptions",
164
+ "Binary",
165
+ "CancelToken",
166
+ "CommandOptions",
167
+ "Config",
168
+ "ConfigError",
169
+ "Connection",
170
+ "Cursor",
171
+ "DataError",
172
+ "DatabaseError",
173
+ "Date",
174
+ "DateFromTicks",
175
+ "Db2Diagnostic",
176
+ "Db2Warning",
177
+ "Error",
178
+ "ErrorKind",
179
+ "ImportOptions",
180
+ "ImportPolicy",
181
+ "ImportProgress",
182
+ "ImportTarget",
183
+ "InsertResult",
184
+ "IntegrityError",
185
+ "InterfaceError",
186
+ "InternalError",
187
+ "NotSupportedError",
188
+ "OperationalError",
189
+ "Pool",
190
+ "PoolOptions",
191
+ "PoolStats",
192
+ "PreparedStatement",
193
+ "ProcedureResult",
194
+ "ProgrammingError",
195
+ "ProgressiveLobReader",
196
+ "ProtocolLimits",
197
+ "QueryResult",
198
+ "ResourceBudget",
199
+ "ResourceLimitError",
200
+ "ResourceReservation",
201
+ "RowReader",
202
+ "Time",
203
+ "TimeFromTicks",
204
+ "Timestamp",
205
+ "TimestampFromTicks",
206
+ "Transaction",
207
+ "Warning",
208
+ "apilevel",
209
+ "async_import_rows",
210
+ "blob",
211
+ "clear_all_async_pools",
212
+ "clear_all_pools",
213
+ "clob",
214
+ "connect",
215
+ "default_spool_budget",
216
+ "import_rows",
217
+ "normalize_current_schema",
218
+ "observe_diagnostics",
219
+ "paramstyle",
220
+ "parse_dsn",
221
+ "redact_dsn",
222
+ "threadsafety",
223
+ "xml",
224
+ ]
@@ -0,0 +1,113 @@
1
+ """Internal cross-thread/task cancellation helpers for DB2 commands.
2
+
3
+ A :class:`~justybase_db2.resources.CancelToken` can be cancelled from any thread
4
+ or task. For synchronous commands a watcher thread sends the DRDA interrupt; for
5
+ asyncio commands a watcher task does the same. The driver never cancels the
6
+ socket coroutine itself: an interrupt is only an acknowledgment and the original
7
+ operation must still settle (JDC CAN-01/CAN-02).
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import asyncio
13
+ import contextlib
14
+ import contextvars
15
+ import threading
16
+ from typing import TYPE_CHECKING
17
+
18
+ from .resources import CancelToken
19
+
20
+ if TYPE_CHECKING: # pragma: no cover - typing only
21
+ from .async_.connection import AsyncConnection
22
+ from .connection import Connection
23
+
24
+ _POLL_SECONDS = 0.02
25
+ # A nested command (prepare inside execute, for example) reuses the outer
26
+ # cancellation scope; only the outermost one may start a watcher.
27
+ _active = contextvars.ContextVar("justybase_command_cancel_depth", default=0)
28
+
29
+
30
+ @contextlib.contextmanager
31
+ def command_cancellation(connection: Connection, token: CancelToken | None):
32
+ """Send a DRDA interrupt from a watcher thread if ``token`` is cancelled."""
33
+ if token is None or _active.get() > 0:
34
+ yield
35
+ return
36
+ token.raise_if_cancelled(outcome="not_started")
37
+ depth = _active.set(1)
38
+
39
+ done = threading.Event()
40
+ finished = False
41
+
42
+ def watch() -> None:
43
+ while not done.wait(_POLL_SECONDS):
44
+ if not token.cancelled:
45
+ continue
46
+ # The command may have returned between the token check and here;
47
+ # never taint a session whose exchange already completed.
48
+ if finished:
49
+ return
50
+ # The original command reports its own final outcome. A failed
51
+ # interrupt must never turn into a second exception here.
52
+ with contextlib.suppress(Exception):
53
+ connection.interrupt()
54
+ return
55
+
56
+ watcher = threading.Thread(target=watch, name="justybase-db2-cancel", daemon=True)
57
+ watcher.start()
58
+ try:
59
+ yield
60
+ finally:
61
+ finished = True
62
+ done.set()
63
+ watcher.join(timeout=1.0)
64
+ _active.reset(depth)
65
+
66
+
67
+ @contextlib.asynccontextmanager
68
+ async def async_command_cancellation(connection: AsyncConnection, token: CancelToken | None):
69
+ """Send a DRDA interrupt from a watcher task if ``token`` is cancelled."""
70
+ if token is None or _active.get() > 0:
71
+ yield
72
+ return
73
+ token.raise_if_cancelled(outcome="not_started")
74
+ depth = _active.set(1)
75
+
76
+ loop = asyncio.get_running_loop()
77
+ done = asyncio.Event()
78
+ fired = asyncio.Event()
79
+
80
+ def on_cancel() -> None:
81
+ loop.call_soon_threadsafe(fired.set)
82
+
83
+ unsubscribe = token.add_listener(on_cancel)
84
+ finished = False
85
+
86
+ async def watch() -> None:
87
+ waiter = asyncio.create_task(fired.wait())
88
+ finisher = asyncio.create_task(done.wait())
89
+ try:
90
+ await asyncio.wait({waiter, finisher}, return_when=asyncio.FIRST_COMPLETED)
91
+ if waiter.done() and not finished and token.cancelled:
92
+ with contextlib.suppress(Exception):
93
+ await connection.interrupt()
94
+ finally:
95
+ for task in (waiter, finisher):
96
+ if not task.done():
97
+ task.cancel()
98
+
99
+ watcher = asyncio.create_task(watch())
100
+ try:
101
+ yield
102
+ finally:
103
+ finished = True
104
+ done.set()
105
+ unsubscribe()
106
+ with contextlib.suppress(Exception):
107
+ await asyncio.wait_for(watcher, timeout=1.0)
108
+ if not watcher.done():
109
+ watcher.cancel()
110
+ _active.reset(depth)
111
+
112
+
113
+ __all__ = ["async_command_cancellation", "command_cancellation"]