python-datastore-sqlalchemy 0.0.1__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.
- python_datastore_sqlalchemy-0.0.1.dist-info/METADATA +65 -0
- python_datastore_sqlalchemy-0.0.1.dist-info/RECORD +12 -0
- python_datastore_sqlalchemy-0.0.1.dist-info/WHEEL +5 -0
- python_datastore_sqlalchemy-0.0.1.dist-info/entry_points.txt +2 -0
- python_datastore_sqlalchemy-0.0.1.dist-info/licenses/LICENSE +9 -0
- python_datastore_sqlalchemy-0.0.1.dist-info/top_level.txt +1 -0
- sqlalchemy_datastore/__init__.py +27 -0
- sqlalchemy_datastore/_helpers.py +135 -0
- sqlalchemy_datastore/_types.py +147 -0
- sqlalchemy_datastore/base.py +291 -0
- sqlalchemy_datastore/datastore_dbapi.py +2322 -0
- sqlalchemy_datastore/parse_url.py +287 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
# Copyright (c) 2025 hychang <hychang.1997.tw@gmail.com>
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
4
|
+
# this software and associated documentation files (the "Software"), to deal in
|
|
5
|
+
# the Software without restriction, including without limitation the rights to
|
|
6
|
+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
7
|
+
# the Software, and to permit persons to whom the Software is furnished to do so,
|
|
8
|
+
# subject to the following conditions:
|
|
9
|
+
#
|
|
10
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
# copies or substantial portions of the Software.
|
|
12
|
+
#
|
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
15
|
+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
16
|
+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
17
|
+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
18
|
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
19
|
+
import logging
|
|
20
|
+
from concurrent import futures
|
|
21
|
+
from typing import Any, List, Optional
|
|
22
|
+
|
|
23
|
+
from google.cloud import firestore_admin_v1
|
|
24
|
+
from google.cloud.firestore_admin_v1.types import Database
|
|
25
|
+
from google.oauth2 import service_account
|
|
26
|
+
from sqlalchemy import exc
|
|
27
|
+
from sqlalchemy.engine import Connection, default
|
|
28
|
+
from sqlalchemy.engine.interfaces import (
|
|
29
|
+
# DBAPICursor,
|
|
30
|
+
# _DBAPISingleExecuteParams,
|
|
31
|
+
ExecutionContext,
|
|
32
|
+
)
|
|
33
|
+
from sqlalchemy.sql import Select
|
|
34
|
+
|
|
35
|
+
from . import _types, datastore_dbapi
|
|
36
|
+
from ._helpers import create_datastore_client
|
|
37
|
+
from .parse_url import parse_url
|
|
38
|
+
|
|
39
|
+
logger = logging.getLogger("sqlalchemy.dialects.CloudDatastore")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class CloudDatastoreDialect(default.DefaultDialect):
|
|
43
|
+
"""SQLAlchemy dialect for Google Cloud Datastore."""
|
|
44
|
+
|
|
45
|
+
name = "datastore"
|
|
46
|
+
driver = "datastore"
|
|
47
|
+
|
|
48
|
+
# Datastore capabilities
|
|
49
|
+
supports_alter = False
|
|
50
|
+
supports_pk_autoincrement = True
|
|
51
|
+
supports_sequences = False
|
|
52
|
+
supports_comments = False
|
|
53
|
+
supports_sane_rowcount = False
|
|
54
|
+
supports_schemas = False
|
|
55
|
+
supports_foreign_keys = False
|
|
56
|
+
supports_check_constraints = False
|
|
57
|
+
supports_unique_constraint_initially_deferred = False
|
|
58
|
+
supports_unicode_statements = True
|
|
59
|
+
supports_unicode_binds = True
|
|
60
|
+
returns_unicode_strings = True
|
|
61
|
+
description_encoding = None
|
|
62
|
+
|
|
63
|
+
# JSON support - required for SQLAlchemy JSON type
|
|
64
|
+
_json_serializer = None
|
|
65
|
+
_json_deserializer = None
|
|
66
|
+
|
|
67
|
+
paramstyle = "named"
|
|
68
|
+
|
|
69
|
+
def __init__(
|
|
70
|
+
self,
|
|
71
|
+
arraysize=5000,
|
|
72
|
+
credentials_path=None,
|
|
73
|
+
billing_project_id=None,
|
|
74
|
+
location=None,
|
|
75
|
+
credentials_info=None,
|
|
76
|
+
credentials_base64=None,
|
|
77
|
+
list_tables_page_size=1000,
|
|
78
|
+
*args,
|
|
79
|
+
**kwargs,
|
|
80
|
+
):
|
|
81
|
+
super(CloudDatastoreDialect, self).__init__(*args, **kwargs)
|
|
82
|
+
self.arraysize = arraysize
|
|
83
|
+
self.credentials_path = credentials_path
|
|
84
|
+
self.credentials_info = credentials_info
|
|
85
|
+
self.credentials_base64 = credentials_base64
|
|
86
|
+
self.project_id = None
|
|
87
|
+
self.billing_project_id = billing_project_id
|
|
88
|
+
self.location = location
|
|
89
|
+
self.identifier_preparer = self.preparer(self)
|
|
90
|
+
self.dataset_id = None
|
|
91
|
+
self.list_tables_page_size = list_tables_page_size
|
|
92
|
+
self._client = None
|
|
93
|
+
|
|
94
|
+
@classmethod
|
|
95
|
+
def dbapi(cls):
|
|
96
|
+
"""Return the DBAPI 2.0 driver."""
|
|
97
|
+
return datastore_dbapi
|
|
98
|
+
|
|
99
|
+
def do_ping(self, dbapi_connection):
|
|
100
|
+
"""Performs a simple operation to check if the connection is still alive."""
|
|
101
|
+
try:
|
|
102
|
+
query = self._client.query(kind="__kind__")
|
|
103
|
+
query.fetch(limit=1, timeout=30)
|
|
104
|
+
return True
|
|
105
|
+
except Exception:
|
|
106
|
+
return False
|
|
107
|
+
|
|
108
|
+
def get_pk_constraint(self, connection, table_name, schema=None, **kw):
|
|
109
|
+
"""Datastore entities inherently have a primary key (the Key object)."""
|
|
110
|
+
return {"constrained_columns": ["id"], "name": "primary_key"}
|
|
111
|
+
|
|
112
|
+
def get_foreign_keys(self, connection, table_name, schema=None, **kw):
|
|
113
|
+
"""Datastore does not support foreign keys."""
|
|
114
|
+
return []
|
|
115
|
+
|
|
116
|
+
def get_indexes(self, connection, table_name, schema=None, **kw):
|
|
117
|
+
"""Datastore uses automatic and composite indexes."""
|
|
118
|
+
return []
|
|
119
|
+
|
|
120
|
+
def create_connect_args(self, url):
|
|
121
|
+
"""Parses the connection URL and returns args for the DBAPI connect function."""
|
|
122
|
+
(
|
|
123
|
+
self.project_id,
|
|
124
|
+
location,
|
|
125
|
+
dataset_id,
|
|
126
|
+
arraysize,
|
|
127
|
+
credentials_path,
|
|
128
|
+
credentials_base64,
|
|
129
|
+
provided_job_config,
|
|
130
|
+
list_tables_page_size,
|
|
131
|
+
user_supplied_client,
|
|
132
|
+
) = parse_url(url)
|
|
133
|
+
|
|
134
|
+
self.arraysize = arraysize or self.arraysize
|
|
135
|
+
self.list_tables_page_size = list_tables_page_size or self.list_tables_page_size
|
|
136
|
+
self.location = location or self.location
|
|
137
|
+
|
|
138
|
+
self.credentials_path = credentials_path
|
|
139
|
+
self.credentials_base64 = credentials_base64 or self.credentials_base64
|
|
140
|
+
self.dataset_id = dataset_id
|
|
141
|
+
self.billing_project_id = self.billing_project_id or self.project_id
|
|
142
|
+
|
|
143
|
+
if user_supplied_client:
|
|
144
|
+
return ([], {})
|
|
145
|
+
else:
|
|
146
|
+
client, credentials = create_datastore_client(
|
|
147
|
+
credentials_path=self.credentials_path,
|
|
148
|
+
credentials_info=self.credentials_info,
|
|
149
|
+
credentials_base64=self.credentials_base64,
|
|
150
|
+
project_id=self.billing_project_id,
|
|
151
|
+
database=None,
|
|
152
|
+
)
|
|
153
|
+
self.project_id = self.project_id if self.project_id else client.project
|
|
154
|
+
self.billing_project_id = (
|
|
155
|
+
self.billing_project_id if self.billing_project_id else client.project
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
if not self.project_id:
|
|
159
|
+
raise exc.ArgumentError(
|
|
160
|
+
"project_id is required for Datastore connection string."
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
self._client = client
|
|
164
|
+
self.credentials = credentials
|
|
165
|
+
setattr(self._client, "credentials_path", self.credentials_path)
|
|
166
|
+
setattr(self._client, "credentials_info", self.credentials_info)
|
|
167
|
+
setattr(self._client, "credentials_base64", self.credentials_base64)
|
|
168
|
+
setattr(self._client, "scoped_credentials", credentials)
|
|
169
|
+
return ([], {"client": client})
|
|
170
|
+
|
|
171
|
+
def get_schema_names(self, connection: Connection, **kw) -> List[str]:
|
|
172
|
+
if not isinstance(self.credentials, service_account.Credentials):
|
|
173
|
+
return []
|
|
174
|
+
return self._list_datastore_databases(self.credentials, self.project_id)
|
|
175
|
+
|
|
176
|
+
def _list_datastore_databases(
|
|
177
|
+
self, cred: service_account.Credentials, project_id: str
|
|
178
|
+
) -> List[str]:
|
|
179
|
+
"""Lists all Datastore databases for a given Google Cloud project."""
|
|
180
|
+
client = firestore_admin_v1.FirestoreAdminClient(credentials=cred)
|
|
181
|
+
parent = f"projects/{project_id}"
|
|
182
|
+
|
|
183
|
+
try:
|
|
184
|
+
list_database_resp = client.list_databases(parent=parent)
|
|
185
|
+
|
|
186
|
+
def get_database_short_name(database: Database) -> Optional[List[str]]:
|
|
187
|
+
full_name = database.name
|
|
188
|
+
if full_name:
|
|
189
|
+
return full_name.split("/")[-1]
|
|
190
|
+
return None
|
|
191
|
+
|
|
192
|
+
with futures.ThreadPoolExecutor() as executor:
|
|
193
|
+
schemas = list(
|
|
194
|
+
executor.map(get_database_short_name, list_database_resp.databases)
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
return schemas
|
|
198
|
+
except Exception as e:
|
|
199
|
+
logging.error(e)
|
|
200
|
+
return []
|
|
201
|
+
|
|
202
|
+
def get_table_names(
|
|
203
|
+
self, connection: Connection, schema: str | None = None, **kw
|
|
204
|
+
) -> List[str]:
|
|
205
|
+
client = self._client
|
|
206
|
+
query = client.query(kind="__kind__")
|
|
207
|
+
kinds = list(query.fetch())
|
|
208
|
+
|
|
209
|
+
def get_kind_name(kind):
|
|
210
|
+
return (
|
|
211
|
+
name
|
|
212
|
+
if (name := getattr(getattr(kind, "key", None), "name", None))
|
|
213
|
+
is not None
|
|
214
|
+
and isinstance(name, str)
|
|
215
|
+
and not name.startswith("__")
|
|
216
|
+
else None
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
with futures.ThreadPoolExecutor() as executor:
|
|
220
|
+
result = list(executor.map(get_kind_name, kinds))
|
|
221
|
+
|
|
222
|
+
return [t for t in result if t is not None]
|
|
223
|
+
|
|
224
|
+
def get_columns(
|
|
225
|
+
self, connection: Connection, table_name: str, schema: str | None = None, **kw
|
|
226
|
+
):
|
|
227
|
+
"""Retrieve column information from the database with optimized parallel processing."""
|
|
228
|
+
client = self._client
|
|
229
|
+
query = client.query(kind="__Stat_PropertyType_PropertyName_Kind__")
|
|
230
|
+
query.add_filter("kind_name", "=", table_name)
|
|
231
|
+
properties = list(query.fetch())
|
|
232
|
+
|
|
233
|
+
def process_property(property):
|
|
234
|
+
return {
|
|
235
|
+
"name": property["property_name"],
|
|
236
|
+
"type": _types._property_type[property["property_type"]],
|
|
237
|
+
"nullable": True,
|
|
238
|
+
"comment": "",
|
|
239
|
+
"default": None,
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
with futures.ThreadPoolExecutor() as executor:
|
|
243
|
+
columns = list(executor.map(process_property, properties))
|
|
244
|
+
return columns
|
|
245
|
+
|
|
246
|
+
def _contains_select_subquery(self, node) -> bool:
|
|
247
|
+
"""
|
|
248
|
+
Check the AST node contains select subquery
|
|
249
|
+
"""
|
|
250
|
+
if isinstance(node, Select):
|
|
251
|
+
for child in node.get_children():
|
|
252
|
+
if isinstance(child, Select) or self._contains_select_subquery(child):
|
|
253
|
+
return True
|
|
254
|
+
|
|
255
|
+
for child in node.get_children():
|
|
256
|
+
if isinstance(child, Select) or self._contains_select_subquery(child):
|
|
257
|
+
return True
|
|
258
|
+
|
|
259
|
+
return False
|
|
260
|
+
|
|
261
|
+
def do_execute(
|
|
262
|
+
self,
|
|
263
|
+
cursor,
|
|
264
|
+
# cursor: DBAPICursor, TODO: Uncomment when superset allow sqlalchemy version >= 2.0
|
|
265
|
+
statement: str,
|
|
266
|
+
# parameters: Optional[], TODO: Uncomment when superset allow sqlalchemy version >= 2.0
|
|
267
|
+
parameters,
|
|
268
|
+
context: Optional[ExecutionContext] = None,
|
|
269
|
+
) -> None:
|
|
270
|
+
cursor.execute(statement, parameters)
|
|
271
|
+
|
|
272
|
+
def get_view_names(
|
|
273
|
+
self, connection: Connection, schema: str | None = None, **kw: Any
|
|
274
|
+
) -> List[str]:
|
|
275
|
+
"""
|
|
276
|
+
Datastore doesn't have view, return empty list.
|
|
277
|
+
"""
|
|
278
|
+
return []
|
|
279
|
+
|
|
280
|
+
def has_table(
|
|
281
|
+
self,
|
|
282
|
+
connection: Connection,
|
|
283
|
+
table_name: str,
|
|
284
|
+
schema: str | None = None,
|
|
285
|
+
**kw: Any,
|
|
286
|
+
) -> bool:
|
|
287
|
+
try:
|
|
288
|
+
return table_name in self.get_table_names(connection, schema)
|
|
289
|
+
except Exception as e:
|
|
290
|
+
logging.debug(e)
|
|
291
|
+
return False
|