atoti-client-jdbc 0.9.3__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.
- atoti_client_jdbc-0.9.3.dist-info/METADATA +18 -0
- atoti_client_jdbc-0.9.3.dist-info/RECORD +11 -0
- atoti_client_jdbc-0.9.3.dist-info/WHEEL +4 -0
- atoti_jdbc/__init__.py +6 -0
- atoti_jdbc/_connection_config.py +48 -0
- atoti_jdbc/_infer_driver_class_name.py +35 -0
- atoti_jdbc/_jdbc_ping_discovery_protocol.py +60 -0
- atoti_jdbc/driver.py +42 -0
- atoti_jdbc/jdbc_load.py +123 -0
- atoti_jdbc/py.typed +0 -0
- atoti_jdbc/user_content_storage_config.py +32 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: atoti-client-jdbc
|
|
3
|
+
Version: 0.9.3
|
|
4
|
+
Summary: Code to interact with databases through JDBC
|
|
5
|
+
Project-URL: Documentation, https://docs.atoti.io/0.9.3/api/atoti_jdbc.html
|
|
6
|
+
Project-URL: Homepage, https://www.atoti.io
|
|
7
|
+
Project-URL: Issues, https://github.com/atoti/atoti/issues
|
|
8
|
+
Author-email: ActiveViam <dev@atoti.io>
|
|
9
|
+
Keywords: atoti,database,jdbc,sql
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: License :: Free for non-commercial use
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Typing :: Typed
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Requires-Dist: atoti-client==0.9.3
|
|
17
|
+
Requires-Dist: pydantic>=2.10.1
|
|
18
|
+
Requires-Dist: typing-extensions>=4.12.2
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
atoti_jdbc/__init__.py,sha256=sTzKC3nV4cMceC5OfUT3z7sXUqrZCLq-M4hchxDEwNk,200
|
|
2
|
+
atoti_jdbc/_connection_config.py,sha256=45tby9HQRTFxkL8iAcPKJiGAOS24Znnvmw_TY-ZF1E0,1843
|
|
3
|
+
atoti_jdbc/_infer_driver_class_name.py,sha256=-791GIC1deg7_gp8rOJj6x23TdFGHcoAxyzd-iyu1aI,895
|
|
4
|
+
atoti_jdbc/_jdbc_ping_discovery_protocol.py,sha256=es2gYYg6CTgECT1hI_mkQ0jhbzi84cnfzVz20KPeQaA,1606
|
|
5
|
+
atoti_jdbc/driver.py,sha256=y48IVr0D6IDBUyDM2jlAIIRRkJgUiicit52Jdy6YXjo,1397
|
|
6
|
+
atoti_jdbc/jdbc_load.py,sha256=6Te9G0_1U3-G4_IkejK4UVIZk9cbKlq5Q9Ks_wwgZQE,3651
|
|
7
|
+
atoti_jdbc/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
atoti_jdbc/user_content_storage_config.py,sha256=tfVP9JngnjOlpXh5eVjND5cIGiFLUXAgS6VennxzHm4,979
|
|
9
|
+
atoti_client_jdbc-0.9.3.dist-info/METADATA,sha256=uVBNXp-iJzRUbOuu0OmwpSoIqjh3W1R5153vQjs-RSE,717
|
|
10
|
+
atoti_client_jdbc-0.9.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
11
|
+
atoti_client_jdbc-0.9.3.dist-info/RECORD,,
|
atoti_jdbc/__init__.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import KW_ONLY
|
|
4
|
+
from typing import Annotated
|
|
5
|
+
|
|
6
|
+
from atoti._jdbc import normalize_jdbc_url
|
|
7
|
+
from atoti._pydantic import PYDANTIC_CONFIG as _PYDANTIC_CONFIG
|
|
8
|
+
from pydantic import AfterValidator, ValidationInfo
|
|
9
|
+
from pydantic.dataclasses import dataclass
|
|
10
|
+
|
|
11
|
+
from ._infer_driver_class_name import infer_driver_class_name
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _validate_driver(driver: str | None, validation_info: ValidationInfo, /) -> str:
|
|
15
|
+
if driver:
|
|
16
|
+
return driver
|
|
17
|
+
|
|
18
|
+
url = validation_info.data.get("url")
|
|
19
|
+
assert isinstance(url, str)
|
|
20
|
+
return driver or infer_driver_class_name(url)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass(config=_PYDANTIC_CONFIG, frozen=True)
|
|
24
|
+
class ConnectionConfig: # pylint: disable=final-class
|
|
25
|
+
url: Annotated[str, AfterValidator(normalize_jdbc_url)]
|
|
26
|
+
"""The JDBC connection string of the database.
|
|
27
|
+
|
|
28
|
+
The ``"jdbc"`` scheme is optional but the database specific scheme (such as ``"h2"`` or ``"mysql"``) is mandatory.
|
|
29
|
+
For instance:
|
|
30
|
+
|
|
31
|
+
* ``"h2:/home/user/database/file/path;USER=username;PASSWORD=passwd"``
|
|
32
|
+
* ``"mysql://localhost:7777/example?user=username&password=passwd"``
|
|
33
|
+
* ``"postgresql://postgresql.db.server:5430/example?user=username&password=passwd"``
|
|
34
|
+
|
|
35
|
+
More examples can be found `here <https://www.baeldung.com/java-jdbc-url-format>`__.
|
|
36
|
+
|
|
37
|
+
This defines Hibernate's `URL <https://javadoc.io/static/org.hibernate/hibernate-core/5.6.15.Final/org/hibernate/cfg/AvailableSettings.html#URL>`__ option.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
_: KW_ONLY
|
|
41
|
+
|
|
42
|
+
driver: Annotated[str | None, AfterValidator(_validate_driver)] = None
|
|
43
|
+
"""The Java class name of the :mod:`~atoti_jdbc.driver` to use.
|
|
44
|
+
|
|
45
|
+
This defines Hibernate's `DRIVER <https://javadoc.io/static/org.hibernate/hibernate-core/5.6.15.Final/org/hibernate/cfg/AvailableSettings.html#DRIVER>`__ option.
|
|
46
|
+
|
|
47
|
+
Inferred from :attr:`url` if ``None``.
|
|
48
|
+
"""
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from collections.abc import Mapping
|
|
3
|
+
|
|
4
|
+
from .driver import (
|
|
5
|
+
H2_DRIVER,
|
|
6
|
+
IBM_DB2_DRIVER,
|
|
7
|
+
MARIADB_DRIVER,
|
|
8
|
+
MSSQL_DRIVER,
|
|
9
|
+
MYSQL_DRIVER,
|
|
10
|
+
ORACLE_DRIVER,
|
|
11
|
+
POSTGRESQL_DRIVER,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
_DRIVER_CLASS_NAME_FROM_SCHEME: Mapping[str, str] = {
|
|
15
|
+
"db2": IBM_DB2_DRIVER,
|
|
16
|
+
"h2": H2_DRIVER,
|
|
17
|
+
"mariadb": MARIADB_DRIVER,
|
|
18
|
+
"mysql": MYSQL_DRIVER,
|
|
19
|
+
"oracle": ORACLE_DRIVER,
|
|
20
|
+
"postgresql": POSTGRESQL_DRIVER,
|
|
21
|
+
"sqlserver": MSSQL_DRIVER,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def infer_driver_class_name(url: str, /) -> str:
|
|
26
|
+
match = re.match(r"jdbc:?(?P<scheme>[^:]+):", url)
|
|
27
|
+
scheme = match.group("scheme") if match else None
|
|
28
|
+
driver_class_name = _DRIVER_CLASS_NAME_FROM_SCHEME.get(scheme) if scheme else None
|
|
29
|
+
|
|
30
|
+
if not driver_class_name:
|
|
31
|
+
raise ValueError(
|
|
32
|
+
f"Cannot infer driver class name from URL: `{url}`, specify it manually.",
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
return driver_class_name
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import KW_ONLY
|
|
4
|
+
from typing import final
|
|
5
|
+
|
|
6
|
+
from atoti._distribution_protocols import DiscoveryProtocol
|
|
7
|
+
from atoti._pydantic import PYDANTIC_CONFIG as _PYDANTIC_CONFIG
|
|
8
|
+
from pydantic.dataclasses import dataclass
|
|
9
|
+
from typing_extensions import override
|
|
10
|
+
|
|
11
|
+
from ._connection_config import ConnectionConfig
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@final
|
|
15
|
+
@dataclass(config=_PYDANTIC_CONFIG, frozen=True)
|
|
16
|
+
class JdbcPingDiscoveryProtocol(ConnectionConfig, DiscoveryProtocol):
|
|
17
|
+
_: KW_ONLY
|
|
18
|
+
|
|
19
|
+
username: str
|
|
20
|
+
|
|
21
|
+
password: str
|
|
22
|
+
|
|
23
|
+
delete_single_sql: str | None = None
|
|
24
|
+
|
|
25
|
+
initialize_sql: str | None = None
|
|
26
|
+
|
|
27
|
+
insert_single_sql: str | None = None
|
|
28
|
+
|
|
29
|
+
remove_all_data_on_view_change: bool = True
|
|
30
|
+
""""Defined by the FILE_PING protocol.
|
|
31
|
+
|
|
32
|
+
See http://jgroups.org/manual4/index.html#_removal_of_zombie_files.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
remove_old_coords_on_view_change: bool = True
|
|
36
|
+
""""Defined by the FILE_PING protocol.
|
|
37
|
+
|
|
38
|
+
See http://jgroups.org/manual4/index.html#_removal_of_zombie_files.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
select_all_pingdata_sql: str | None = None
|
|
42
|
+
|
|
43
|
+
write_data_on_find: bool = True
|
|
44
|
+
""""Defined by the FILE_PING protocol.
|
|
45
|
+
|
|
46
|
+
See http://jgroups.org/manual4/index.html#_removal_of_zombie_files.
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
@property
|
|
50
|
+
@override
|
|
51
|
+
def _properties(self) -> dict[str, object]:
|
|
52
|
+
properties = super()._properties
|
|
53
|
+
for property_name in ["driver", "password", "url", "username"]:
|
|
54
|
+
properties[f"connection_{property_name}"] = properties.pop(property_name)
|
|
55
|
+
return properties
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
@override
|
|
59
|
+
def _protocol_name(self) -> str:
|
|
60
|
+
return "JDBC_PING"
|
atoti_jdbc/driver.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""Constants for the Java class names of the embedded JDBC drivers.
|
|
2
|
+
|
|
3
|
+
To use another JDBC driver, add it to :attr:`~atoti.SessionConfig.extra_jars`.
|
|
4
|
+
|
|
5
|
+
Example:
|
|
6
|
+
Storing user content in Google BigQuery:
|
|
7
|
+
|
|
8
|
+
>>> from pathlib import Path
|
|
9
|
+
>>> from atoti_jdbc import UserContentStorageConfig
|
|
10
|
+
>>> user_content_storage_config = UserContentStorageConfig(
|
|
11
|
+
... "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;ProjectId=PROJECT_ID;OAuthType=0;OAuthServiceAcctEmail=EMAIL_OF_SERVICEACCOUNT;OAuthPvtKeyPath=path/to/json/keys;",
|
|
12
|
+
... driver="com.simba.googlebigquery.jdbc42.Driver",
|
|
13
|
+
... )
|
|
14
|
+
>>> session_config = tt.SessionConfig(
|
|
15
|
+
... extra_jars=Path("odbc_jdbc_drivers").glob("*.jar"),
|
|
16
|
+
... user_content_storage=user_content_storage_config,
|
|
17
|
+
... )
|
|
18
|
+
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from atoti._jdbc import H2_DRIVER as _H2_DRIVER
|
|
22
|
+
|
|
23
|
+
H2_DRIVER = _H2_DRIVER
|
|
24
|
+
"""H2 driver class name."""
|
|
25
|
+
|
|
26
|
+
IBM_DB2_DRIVER = "com.ibm.db2.jcc.DB2Driver"
|
|
27
|
+
"""IBM Db2 driver class name."""
|
|
28
|
+
|
|
29
|
+
MARIADB_DRIVER = "org.mariadb.jdbc.Driver"
|
|
30
|
+
"""MariaDB driver class name."""
|
|
31
|
+
|
|
32
|
+
MSSQL_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
|
|
33
|
+
"""Microsoft SQL Server driver class name."""
|
|
34
|
+
|
|
35
|
+
MYSQL_DRIVER = "com.mysql.cj.jdbc.Driver"
|
|
36
|
+
"""MySQL driver class name."""
|
|
37
|
+
|
|
38
|
+
ORACLE_DRIVER = "oracle.jdbc.OracleDriver"
|
|
39
|
+
"""Oracle driver class name."""
|
|
40
|
+
|
|
41
|
+
POSTGRESQL_DRIVER = "org.postgresql.Driver"
|
|
42
|
+
"""PostgreSQL driver class name."""
|
atoti_jdbc/jdbc_load.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Sequence
|
|
4
|
+
from dataclasses import KW_ONLY
|
|
5
|
+
from typing import final
|
|
6
|
+
|
|
7
|
+
from atoti._constant import ConstantValue
|
|
8
|
+
from atoti._pydantic import PYDANTIC_CONFIG as _PYDANTIC_CONFIG
|
|
9
|
+
from atoti.data_load import DataLoad
|
|
10
|
+
from pydantic.dataclasses import dataclass
|
|
11
|
+
from typing_extensions import override
|
|
12
|
+
|
|
13
|
+
from ._connection_config import ConnectionConfig
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# This base class is only used so that `query` comes before `url` in `JdbcLoad`.
|
|
17
|
+
@dataclass(config=_PYDANTIC_CONFIG, frozen=True)
|
|
18
|
+
class _JdbcLoadBase(DataLoad): # pylint: disable=final-class
|
|
19
|
+
query: str
|
|
20
|
+
"""The query (usually SQL) to execute."""
|
|
21
|
+
|
|
22
|
+
_: KW_ONLY
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@final
|
|
26
|
+
@dataclass(config=_PYDANTIC_CONFIG, frozen=True)
|
|
27
|
+
# Unlike the other plugin symbols, this one repeats the plugin key in its name (i.e. it is not just `Load`).
|
|
28
|
+
# The reasons are:
|
|
29
|
+
# - Class names should be nouns but `Load` can read as a verb (`JdbcLoad` reads a noun, as opposed to `LoadJdbc`).
|
|
30
|
+
# - It avoids stuttering: `table.load(Load())`.
|
|
31
|
+
# - Consistency with the built-in `CsvLoad` and `ParquetLoad` classes.
|
|
32
|
+
class JdbcLoad(ConnectionConfig, _JdbcLoadBase):
|
|
33
|
+
"""The description of a JDBC query.
|
|
34
|
+
|
|
35
|
+
Example:
|
|
36
|
+
.. doctest::
|
|
37
|
+
:hide:
|
|
38
|
+
|
|
39
|
+
>>> from pathlib import Path
|
|
40
|
+
>>> from shutil import copy
|
|
41
|
+
>>> tmp_path = getfixture("tmp_path")
|
|
42
|
+
>>> database_path = Path(
|
|
43
|
+
... copy(TEST_RESOURCES_PATH / "jdbc" / "h2-database.mv.db", tmp_path)
|
|
44
|
+
... )
|
|
45
|
+
>>> # Remove `.mv.db` extension.
|
|
46
|
+
>>> database_path = database_path.parent / Path(database_path.stem).stem
|
|
47
|
+
>>> session = getfixture("session_with_jdbc_plugin")
|
|
48
|
+
|
|
49
|
+
>>> from atoti_jdbc import JdbcLoad
|
|
50
|
+
>>> url = f"h2:{database_path};USER=root;PASSWORD=pass"
|
|
51
|
+
>>> jdbc_load = JdbcLoad("SELECT * FROM MYTABLE", url=url)
|
|
52
|
+
|
|
53
|
+
Inferring data types:
|
|
54
|
+
|
|
55
|
+
>>> data_types = session.tables.infer_data_types(jdbc_load)
|
|
56
|
+
>>> data_types
|
|
57
|
+
{'ID': 'int', 'CITY': 'String', 'MY_VALUE': 'long'}
|
|
58
|
+
|
|
59
|
+
Creating table from inferred data types:
|
|
60
|
+
|
|
61
|
+
>>> table = session.create_table(
|
|
62
|
+
... "Cities",
|
|
63
|
+
... data_types=data_types,
|
|
64
|
+
... keys={"ID"},
|
|
65
|
+
... )
|
|
66
|
+
|
|
67
|
+
Loading query results into the table:
|
|
68
|
+
|
|
69
|
+
>>> table.load(jdbc_load)
|
|
70
|
+
>>> table.head().sort_index()
|
|
71
|
+
CITY MY_VALUE
|
|
72
|
+
ID
|
|
73
|
+
1 Paris 100
|
|
74
|
+
2 London 80
|
|
75
|
+
3 New York 90
|
|
76
|
+
4 Berlin 70
|
|
77
|
+
5 Jakarta 75
|
|
78
|
+
>>> table.drop()
|
|
79
|
+
|
|
80
|
+
Using a parametrized query:
|
|
81
|
+
|
|
82
|
+
>>> table.load(
|
|
83
|
+
... JdbcLoad(
|
|
84
|
+
... "SELECT * FROM MYTABLE WHERE City IN (?, ?)",
|
|
85
|
+
... parameters=["Paris", "New York"],
|
|
86
|
+
... url=url,
|
|
87
|
+
... )
|
|
88
|
+
... )
|
|
89
|
+
>>> table.head().sort_index()
|
|
90
|
+
CITY MY_VALUE
|
|
91
|
+
ID
|
|
92
|
+
1 Paris 100
|
|
93
|
+
3 New York 90
|
|
94
|
+
|
|
95
|
+
See Also:
|
|
96
|
+
The other :class:`~atoti.data_load.DataLoad` implementations.
|
|
97
|
+
|
|
98
|
+
"""
|
|
99
|
+
|
|
100
|
+
_: KW_ONLY
|
|
101
|
+
|
|
102
|
+
parameters: Sequence[ConstantValue] = ()
|
|
103
|
+
"""The query parameters, sometimes also called *bind variables*."""
|
|
104
|
+
|
|
105
|
+
@property
|
|
106
|
+
@override
|
|
107
|
+
def _options(
|
|
108
|
+
self,
|
|
109
|
+
) -> dict[str, object]:
|
|
110
|
+
assert self.driver is not None
|
|
111
|
+
return {
|
|
112
|
+
"driverClass": self.driver,
|
|
113
|
+
"parameters": self.parameters,
|
|
114
|
+
"query": self.query,
|
|
115
|
+
"url": self.url,
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
@property
|
|
119
|
+
@override
|
|
120
|
+
def _plugin_key(
|
|
121
|
+
self,
|
|
122
|
+
) -> str:
|
|
123
|
+
return "JDBC"
|
atoti_jdbc/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import KW_ONLY
|
|
4
|
+
from typing import final
|
|
5
|
+
|
|
6
|
+
from atoti._collections import FrozenMapping, frozendict
|
|
7
|
+
from atoti._pydantic import PYDANTIC_CONFIG as _PYDANTIC_CONFIG
|
|
8
|
+
from pydantic.dataclasses import dataclass
|
|
9
|
+
|
|
10
|
+
from ._connection_config import ConnectionConfig
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@final
|
|
14
|
+
@dataclass(config=_PYDANTIC_CONFIG, frozen=True)
|
|
15
|
+
class UserContentStorageConfig(ConnectionConfig):
|
|
16
|
+
"""The config for storing user content in a separate database.
|
|
17
|
+
|
|
18
|
+
Example:
|
|
19
|
+
>>> from atoti_jdbc import UserContentStorageConfig
|
|
20
|
+
>>> config = UserContentStorageConfig(
|
|
21
|
+
... "mysql://localhost:7777/example?user=username&password=passwd"
|
|
22
|
+
... )
|
|
23
|
+
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
_: KW_ONLY
|
|
27
|
+
|
|
28
|
+
hibernate_options: FrozenMapping[str, str] = frozendict()
|
|
29
|
+
"""Extra options to pass to Hibernate.
|
|
30
|
+
|
|
31
|
+
See `AvailableSettings <https://javadoc.io/static/org.hibernate/hibernate-core/5.6.15.Final/org/hibernate/cfg/AvailableSettings.html>`__.
|
|
32
|
+
"""
|