AlchemyReflection 0.1.2__tar.gz → 0.2.0__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.
- {alchemyreflection-0.1.2 → alchemyreflection-0.2.0/AlchemyReflection.egg-info}/PKG-INFO +109 -15
- {alchemyreflection-0.1.2 → alchemyreflection-0.2.0}/AlchemyReflection.egg-info/SOURCES.txt +5 -1
- alchemyreflection-0.2.0/AlchemyReflection.egg-info/requires.txt +30 -0
- alchemyreflection-0.1.2/README.md → alchemyreflection-0.2.0/PKG-INFO +132 -13
- alchemyreflection-0.1.2/AlchemyReflection.egg-info/PKG-INFO → alchemyreflection-0.2.0/README.md +92 -38
- alchemyreflection-0.2.0/alchemy_reflection/_typing.py +21 -0
- alchemyreflection-0.2.0/alchemy_reflection/database.py +292 -0
- alchemyreflection-0.2.0/alchemy_reflection/execution.py +390 -0
- {alchemyreflection-0.1.2 → alchemyreflection-0.2.0}/alchemy_reflection/reflection.py +12 -6
- {alchemyreflection-0.1.2 → alchemyreflection-0.2.0}/pyproject.toml +31 -3
- alchemyreflection-0.2.0/tests/test_database.py +249 -0
- alchemyreflection-0.2.0/tests/test_execution.py +272 -0
- alchemyreflection-0.2.0/tests/test_reflection.py +110 -0
- alchemyreflection-0.1.2/AlchemyReflection.egg-info/requires.txt +0 -9
- alchemyreflection-0.1.2/alchemy_reflection/database.py +0 -123
- alchemyreflection-0.1.2/alchemy_reflection/execution.py +0 -130
- {alchemyreflection-0.1.2 → alchemyreflection-0.2.0}/AlchemyReflection.egg-info/dependency_links.txt +0 -0
- {alchemyreflection-0.1.2 → alchemyreflection-0.2.0}/AlchemyReflection.egg-info/top_level.txt +0 -0
- {alchemyreflection-0.1.2 → alchemyreflection-0.2.0}/LICENSE +0 -0
- {alchemyreflection-0.1.2 → alchemyreflection-0.2.0}/alchemy_reflection/__init__.py +0 -0
- {alchemyreflection-0.1.2 → alchemyreflection-0.2.0}/setup.cfg +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: AlchemyReflection
|
|
3
|
-
Version: 0.
|
|
4
|
-
Summary: An object-oriented interface for SQLAlchemy,
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: An object-oriented interface for SQLAlchemy, providing database reflection, statement execution, and query result retrieval with an emphasis on method chaining and native interoperability.
|
|
5
5
|
Author: MI77-LAB
|
|
6
6
|
License-Expression: MIT
|
|
7
7
|
Project-URL: Repository, https://github.com/mirabelleding/AlchemyReflection
|
|
@@ -15,16 +15,42 @@ Requires-Python: >=3.10
|
|
|
15
15
|
Description-Content-Type: text/markdown
|
|
16
16
|
License-File: LICENSE
|
|
17
17
|
Requires-Dist: sqlalchemy<3.0,>=2.0
|
|
18
|
+
Requires-Dist: numpy<3.0,>=1.26
|
|
18
19
|
Requires-Dist: pandas<4.0,>=2.0
|
|
19
20
|
Requires-Dist: polars<3.0,>=1.0
|
|
20
21
|
Provides-Extra: arrow
|
|
21
22
|
Requires-Dist: pyarrow<27.0,>=15.0; extra == "arrow"
|
|
23
|
+
Provides-Extra: psycopg
|
|
24
|
+
Requires-Dist: psycopg[binary]<4.0,>=3.2; extra == "psycopg"
|
|
22
25
|
Provides-Extra: pyodbc
|
|
23
26
|
Requires-Dist: pyodbc<6.0,>=5.0; extra == "pyodbc"
|
|
27
|
+
Provides-Extra: pymysql
|
|
28
|
+
Requires-Dist: pymysql[rsa]<2.0,>=1.1; extra == "pymysql"
|
|
29
|
+
Provides-Extra: duckdb
|
|
30
|
+
Requires-Dist: duckdb-sqlalchemy<3.0,>=2.0; extra == "duckdb"
|
|
31
|
+
Provides-Extra: oracledb
|
|
32
|
+
Requires-Dist: oracledb<27.0,>=3.0; extra == "oracledb"
|
|
33
|
+
Provides-Extra: bigquery
|
|
34
|
+
Requires-Dist: sqlalchemy-bigquery[bqstorage]<2.0,>=1.15; extra == "bigquery"
|
|
35
|
+
Provides-Extra: test
|
|
36
|
+
Requires-Dist: pytest<10.0,>=8.0; extra == "test"
|
|
37
|
+
Requires-Dist: pytest-cov<8.0,>=7.0; extra == "test"
|
|
38
|
+
Requires-Dist: testcontainers<5.0,>=4.0; extra == "test"
|
|
24
39
|
Dynamic: license-file
|
|
25
40
|
|
|
26
41
|
# AlchemyReflection
|
|
27
|
-
|
|
42
|
+
|
|
43
|
+
[](https://pypi.org/project/AlchemyReflection/)
|
|
44
|
+
|
|
45
|
+
[](https://pypi.org/project/AlchemyReflection/)
|
|
46
|
+
|
|
47
|
+
[](LICENSE)
|
|
48
|
+
</br>
|
|
49
|
+
[](https://github.com/mirabelleding/AlchemyReflection/actions/workflows/tests.yml)
|
|
50
|
+
|
|
51
|
+
[](https://github.com/mirabelleding/AlchemyReflection/actions/workflows/compatibility.yml)
|
|
52
|
+
|
|
53
|
+
A lightweight Python package built on SQLAlchemy for database connection management, table reflection, schema inspection, statement execution, and query result retrieval.
|
|
28
54
|
|
|
29
55
|
Designed with an emphasis on API ergonomics and method chaining, the object-oriented interface extends SQLAlchemy functionality while preserving native interoperability.
|
|
30
56
|
|
|
@@ -55,10 +81,7 @@ db = Database("postgresql+psycopg://user:password@localhost:5432/database_name")
|
|
|
55
81
|
```
|
|
56
82
|
|
|
57
83
|
**Raw SQL Server ODBC connection string:**<br>
|
|
58
|
-
|
|
59
|
-
```bash
|
|
60
|
-
pip install "AlchemyReflection[pyodbc]"
|
|
61
|
-
```
|
|
84
|
+
|
|
62
85
|
```text
|
|
63
86
|
DRIVER={ODBC Driver 18 for SQL Server};
|
|
64
87
|
SERVER=server_name;
|
|
@@ -72,8 +95,9 @@ from alchemy_reflection import Database
|
|
|
72
95
|
|
|
73
96
|
db = Database(os.environ["DB_ODBC"])
|
|
74
97
|
```
|
|
75
|
-
Each `Database` instance maintains its own SQLAlchemy engine and reflected metadata.
|
|
98
|
+
Each `Database` instance maintains its own SQLAlchemy engine and reflected metadata.
|
|
76
99
|
|
|
100
|
+
*Database connectivity and reflection capabilities depend on the underlying SQLAlchemy dialect and driver. Installation of the corresponding dependencies is required.*
|
|
77
101
|
## Basic Usage
|
|
78
102
|
```python
|
|
79
103
|
import os
|
|
@@ -85,7 +109,7 @@ from alchemy_reflection import Database
|
|
|
85
109
|
db = Database(os.environ["DB_ODBC"])
|
|
86
110
|
|
|
87
111
|
# Search and pretty-print matching tables and views
|
|
88
|
-
db.catalogue("some keywords", pprint=True)
|
|
112
|
+
db.catalogue(keyword="some keywords", schema="some_schema", pprint=True)
|
|
89
113
|
|
|
90
114
|
# Instantiate a ReflectedTable
|
|
91
115
|
table = db.reflect("schemaname.tablename")
|
|
@@ -108,9 +132,9 @@ reflected_table.alias("t")
|
|
|
108
132
|
reflected_table.select()
|
|
109
133
|
```
|
|
110
134
|
|
|
111
|
-
## Query
|
|
135
|
+
## Query and Result Retrieval
|
|
112
136
|
|
|
113
|
-
`QueryExecutor` supports query
|
|
137
|
+
`QueryExecutor` supports query and result retrieval for raw SQL statements, SQLAlchemy `Selectable`, and `ReflectedTable` instances. Its methods are accessible through `Database.query()` or directly through `ReflectedTable` interface.
|
|
114
138
|
|
|
115
139
|
| Method | Return Type |
|
|
116
140
|
| --- | --- |
|
|
@@ -122,23 +146,71 @@ reflected_table.select()
|
|
|
122
146
|
| `fetch(n_rows=None)` | List of SQLAlchemy `Row` objects; all rows are retrieved by default |
|
|
123
147
|
| `to_scalar()` | Scalar value from a single-row, single-column query |
|
|
124
148
|
|
|
149
|
+
*`to_pd()`, `to_pl()`, `to_numpy()`, and `to_arrow()` support SQL-level type conversions via `casts` for SQLAlchemy `Select` statements. Native pandas and Polars conversion parameters are also exposed, with `to_arrow()` inheriting those of `to_pl()`.*
|
|
125
150
|
|
|
126
151
|
```python
|
|
127
|
-
|
|
152
|
+
import polars as pl
|
|
153
|
+
from sqlalchemy import Float, func, select
|
|
128
154
|
|
|
129
155
|
stmt = select(table.c.some_column)
|
|
130
156
|
|
|
131
|
-
series = db.query(stmt).to_pd()
|
|
132
|
-
array = db.query(stmt).to_numpy()
|
|
157
|
+
series = db.query(stmt).to_pd(dtype="Float64")
|
|
158
|
+
array = db.query(stmt).to_numpy(casts={"some_column": Float})
|
|
133
159
|
rows = db.query(stmt).fetch(7)
|
|
134
160
|
|
|
161
|
+
df = db.query(table).to_pl(schema_overrides={"some_column": pl.Float64})
|
|
162
|
+
|
|
135
163
|
count_stmt = select(func.count()).select_from(table)
|
|
136
164
|
count = db.query(count_stmt).to_scalar()
|
|
137
165
|
```
|
|
138
166
|
|
|
167
|
+
## Statement Execution
|
|
168
|
+
|
|
169
|
+
`Database.execute()` and `Database.execution()` support raw SQL statements
|
|
170
|
+
and native SQLAlchemy executable constructs within managed transactions.
|
|
171
|
+
|
|
172
|
+
| Method | Description |
|
|
173
|
+
| --- | --- |
|
|
174
|
+
| `execute()` | Executes immediately and returns the driver-reported row count |
|
|
175
|
+
| `execution()` | Provides context-managed execution with access to the native SQLAlchemy `CursorResult` |
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
db.execute("CREATE SCHEMA some_schema")
|
|
179
|
+
|
|
180
|
+
db.execute(
|
|
181
|
+
table.insert(),
|
|
182
|
+
[
|
|
183
|
+
{"id": 1, "name": "Name_1"},
|
|
184
|
+
{"id": 2, "name": "Name_2"},
|
|
185
|
+
],
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
db.execute(
|
|
189
|
+
table.update()
|
|
190
|
+
.where(table.c.id == 1)
|
|
191
|
+
.values(name="Name_3")
|
|
192
|
+
)
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
```python
|
|
196
|
+
with db.execution(
|
|
197
|
+
table.insert()
|
|
198
|
+
.values(name="Name_4")
|
|
199
|
+
.returning(table.c.id)
|
|
200
|
+
) as result:
|
|
201
|
+
inserted_ids = result.scalars().all()
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Transactions are committed upon successful completion and rolled back on
|
|
205
|
+
failure, subject to the underlying database's transaction semantics.
|
|
206
|
+
|
|
207
|
+
*Row count reporting and support for `RETURNING` depend on the underlying
|
|
208
|
+
dialect and driver. Result rows must be consumed within the `execution()` context.*
|
|
209
|
+
|
|
139
210
|
## Advanced SQLAlchemy Queries
|
|
140
211
|
|
|
141
212
|
SQL construction remains entirely native to SQLAlchemy. Below are examples of advanced query expressions and with DataFrame operations.
|
|
213
|
+
|
|
142
214
|
```python
|
|
143
215
|
import hashlib
|
|
144
216
|
from sqlalchemy import select, func, exists, or_
|
|
@@ -224,7 +296,7 @@ test.dispose()
|
|
|
224
296
|
|
|
225
297
|
| Class | Attributes / Methods |
|
|
226
298
|
| --- | --- |
|
|
227
|
-
| `Database` | `engine`, `metadata`, `reflect()`, `catalogue()`, `query()` |
|
|
299
|
+
| `Database` | `engine`, `metadata`, `reflect()`, `catalogue()`, `query()`, `execute()`, `execution()`, `dispose()` |
|
|
228
300
|
| `ReflectedTable` | `sa`, `engine`, native SQLAlchemy `Table` attributes and methods, `inspect()`, delegated `QueryExecutor` methods |
|
|
229
301
|
| `QueryExecutor` | `statement`, `engine`, `to_pd()`, `to_pl()`, `to_numpy()`, `to_arrow()`, `to_records()`, `fetch()`, `to_scalar()` |
|
|
230
302
|
|
|
@@ -234,6 +306,28 @@ test.dispose()
|
|
|
234
306
|
|
|
235
307
|
`ReflectedTable.__clause_element__()` enables SQLAlchemy expression coercion. The explicit `.sa` attribute exposes the underlying SQLAlchemy `Table` when a native table object is required.
|
|
236
308
|
|
|
309
|
+
## Optional Dependencies
|
|
310
|
+
|
|
311
|
+
Optional dependency groups are provided for additional database dialects and result conversion capabilities.
|
|
312
|
+
|
|
313
|
+
| Extra | Database / Functionality | Dependency |
|
|
314
|
+
| --- | --- | --- |
|
|
315
|
+
| `arrow` | Apache Arrow | `pyarrow` |
|
|
316
|
+
| `psycopg` | PostgreSQL | `psycopg[binary]` |
|
|
317
|
+
| `pyodbc` | SQL Server | `pyodbc` |
|
|
318
|
+
| `pymysql` | MySQL / MariaDB | `pymysql[rsa]` |
|
|
319
|
+
| `duckdb` | DuckDB | `duckdb-sqlalchemy` |
|
|
320
|
+
| `oracledb` | Oracle | `oracledb` |
|
|
321
|
+
| `bigquery` | Google BigQuery | `sqlalchemy-bigquery[bqstorage]` |
|
|
322
|
+
|
|
323
|
+
Install one or multiple extras as required:
|
|
324
|
+
|
|
325
|
+
```bash
|
|
326
|
+
pip install "AlchemyReflection[psycopg,arrow]"
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
Database drivers may require additional system-level dependencies or configuration.
|
|
330
|
+
|
|
237
331
|
|
|
238
332
|
## Design Philosophy
|
|
239
333
|
|
|
@@ -7,6 +7,10 @@ AlchemyReflection.egg-info/dependency_links.txt
|
|
|
7
7
|
AlchemyReflection.egg-info/requires.txt
|
|
8
8
|
AlchemyReflection.egg-info/top_level.txt
|
|
9
9
|
alchemy_reflection/__init__.py
|
|
10
|
+
alchemy_reflection/_typing.py
|
|
10
11
|
alchemy_reflection/database.py
|
|
11
12
|
alchemy_reflection/execution.py
|
|
12
|
-
alchemy_reflection/reflection.py
|
|
13
|
+
alchemy_reflection/reflection.py
|
|
14
|
+
tests/test_database.py
|
|
15
|
+
tests/test_execution.py
|
|
16
|
+
tests/test_reflection.py
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
sqlalchemy<3.0,>=2.0
|
|
2
|
+
numpy<3.0,>=1.26
|
|
3
|
+
pandas<4.0,>=2.0
|
|
4
|
+
polars<3.0,>=1.0
|
|
5
|
+
|
|
6
|
+
[arrow]
|
|
7
|
+
pyarrow<27.0,>=15.0
|
|
8
|
+
|
|
9
|
+
[bigquery]
|
|
10
|
+
sqlalchemy-bigquery[bqstorage]<2.0,>=1.15
|
|
11
|
+
|
|
12
|
+
[duckdb]
|
|
13
|
+
duckdb-sqlalchemy<3.0,>=2.0
|
|
14
|
+
|
|
15
|
+
[oracledb]
|
|
16
|
+
oracledb<27.0,>=3.0
|
|
17
|
+
|
|
18
|
+
[psycopg]
|
|
19
|
+
psycopg[binary]<4.0,>=3.2
|
|
20
|
+
|
|
21
|
+
[pymysql]
|
|
22
|
+
pymysql[rsa]<2.0,>=1.1
|
|
23
|
+
|
|
24
|
+
[pyodbc]
|
|
25
|
+
pyodbc<6.0,>=5.0
|
|
26
|
+
|
|
27
|
+
[test]
|
|
28
|
+
pytest<10.0,>=8.0
|
|
29
|
+
pytest-cov<8.0,>=7.0
|
|
30
|
+
testcontainers<5.0,>=4.0
|
|
@@ -1,5 +1,56 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: AlchemyReflection
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: An object-oriented interface for SQLAlchemy, providing database reflection, statement execution, and query result retrieval with an emphasis on method chaining and native interoperability.
|
|
5
|
+
Author: MI77-LAB
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Repository, https://github.com/mirabelleding/AlchemyReflection
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Topic :: Database
|
|
13
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: sqlalchemy<3.0,>=2.0
|
|
18
|
+
Requires-Dist: numpy<3.0,>=1.26
|
|
19
|
+
Requires-Dist: pandas<4.0,>=2.0
|
|
20
|
+
Requires-Dist: polars<3.0,>=1.0
|
|
21
|
+
Provides-Extra: arrow
|
|
22
|
+
Requires-Dist: pyarrow<27.0,>=15.0; extra == "arrow"
|
|
23
|
+
Provides-Extra: psycopg
|
|
24
|
+
Requires-Dist: psycopg[binary]<4.0,>=3.2; extra == "psycopg"
|
|
25
|
+
Provides-Extra: pyodbc
|
|
26
|
+
Requires-Dist: pyodbc<6.0,>=5.0; extra == "pyodbc"
|
|
27
|
+
Provides-Extra: pymysql
|
|
28
|
+
Requires-Dist: pymysql[rsa]<2.0,>=1.1; extra == "pymysql"
|
|
29
|
+
Provides-Extra: duckdb
|
|
30
|
+
Requires-Dist: duckdb-sqlalchemy<3.0,>=2.0; extra == "duckdb"
|
|
31
|
+
Provides-Extra: oracledb
|
|
32
|
+
Requires-Dist: oracledb<27.0,>=3.0; extra == "oracledb"
|
|
33
|
+
Provides-Extra: bigquery
|
|
34
|
+
Requires-Dist: sqlalchemy-bigquery[bqstorage]<2.0,>=1.15; extra == "bigquery"
|
|
35
|
+
Provides-Extra: test
|
|
36
|
+
Requires-Dist: pytest<10.0,>=8.0; extra == "test"
|
|
37
|
+
Requires-Dist: pytest-cov<8.0,>=7.0; extra == "test"
|
|
38
|
+
Requires-Dist: testcontainers<5.0,>=4.0; extra == "test"
|
|
39
|
+
Dynamic: license-file
|
|
40
|
+
|
|
1
41
|
# AlchemyReflection
|
|
2
|
-
|
|
42
|
+
|
|
43
|
+
[](https://pypi.org/project/AlchemyReflection/)
|
|
44
|
+
|
|
45
|
+
[](https://pypi.org/project/AlchemyReflection/)
|
|
46
|
+
|
|
47
|
+
[](LICENSE)
|
|
48
|
+
</br>
|
|
49
|
+
[](https://github.com/mirabelleding/AlchemyReflection/actions/workflows/tests.yml)
|
|
50
|
+
|
|
51
|
+
[](https://github.com/mirabelleding/AlchemyReflection/actions/workflows/compatibility.yml)
|
|
52
|
+
|
|
53
|
+
A lightweight Python package built on SQLAlchemy for database connection management, table reflection, schema inspection, statement execution, and query result retrieval.
|
|
3
54
|
|
|
4
55
|
Designed with an emphasis on API ergonomics and method chaining, the object-oriented interface extends SQLAlchemy functionality while preserving native interoperability.
|
|
5
56
|
|
|
@@ -30,10 +81,7 @@ db = Database("postgresql+psycopg://user:password@localhost:5432/database_name")
|
|
|
30
81
|
```
|
|
31
82
|
|
|
32
83
|
**Raw SQL Server ODBC connection string:**<br>
|
|
33
|
-
|
|
34
|
-
```bash
|
|
35
|
-
pip install "AlchemyReflection[pyodbc]"
|
|
36
|
-
```
|
|
84
|
+
|
|
37
85
|
```text
|
|
38
86
|
DRIVER={ODBC Driver 18 for SQL Server};
|
|
39
87
|
SERVER=server_name;
|
|
@@ -47,8 +95,9 @@ from alchemy_reflection import Database
|
|
|
47
95
|
|
|
48
96
|
db = Database(os.environ["DB_ODBC"])
|
|
49
97
|
```
|
|
50
|
-
Each `Database` instance maintains its own SQLAlchemy engine and reflected metadata.
|
|
98
|
+
Each `Database` instance maintains its own SQLAlchemy engine and reflected metadata.
|
|
51
99
|
|
|
100
|
+
*Database connectivity and reflection capabilities depend on the underlying SQLAlchemy dialect and driver. Installation of the corresponding dependencies is required.*
|
|
52
101
|
## Basic Usage
|
|
53
102
|
```python
|
|
54
103
|
import os
|
|
@@ -60,7 +109,7 @@ from alchemy_reflection import Database
|
|
|
60
109
|
db = Database(os.environ["DB_ODBC"])
|
|
61
110
|
|
|
62
111
|
# Search and pretty-print matching tables and views
|
|
63
|
-
db.catalogue("some keywords", pprint=True)
|
|
112
|
+
db.catalogue(keyword="some keywords", schema="some_schema", pprint=True)
|
|
64
113
|
|
|
65
114
|
# Instantiate a ReflectedTable
|
|
66
115
|
table = db.reflect("schemaname.tablename")
|
|
@@ -83,9 +132,9 @@ reflected_table.alias("t")
|
|
|
83
132
|
reflected_table.select()
|
|
84
133
|
```
|
|
85
134
|
|
|
86
|
-
## Query
|
|
135
|
+
## Query and Result Retrieval
|
|
87
136
|
|
|
88
|
-
`QueryExecutor` supports query
|
|
137
|
+
`QueryExecutor` supports query and result retrieval for raw SQL statements, SQLAlchemy `Selectable`, and `ReflectedTable` instances. Its methods are accessible through `Database.query()` or directly through `ReflectedTable` interface.
|
|
89
138
|
|
|
90
139
|
| Method | Return Type |
|
|
91
140
|
| --- | --- |
|
|
@@ -97,23 +146,71 @@ reflected_table.select()
|
|
|
97
146
|
| `fetch(n_rows=None)` | List of SQLAlchemy `Row` objects; all rows are retrieved by default |
|
|
98
147
|
| `to_scalar()` | Scalar value from a single-row, single-column query |
|
|
99
148
|
|
|
149
|
+
*`to_pd()`, `to_pl()`, `to_numpy()`, and `to_arrow()` support SQL-level type conversions via `casts` for SQLAlchemy `Select` statements. Native pandas and Polars conversion parameters are also exposed, with `to_arrow()` inheriting those of `to_pl()`.*
|
|
100
150
|
|
|
101
151
|
```python
|
|
102
|
-
|
|
152
|
+
import polars as pl
|
|
153
|
+
from sqlalchemy import Float, func, select
|
|
103
154
|
|
|
104
155
|
stmt = select(table.c.some_column)
|
|
105
156
|
|
|
106
|
-
series = db.query(stmt).to_pd()
|
|
107
|
-
array = db.query(stmt).to_numpy()
|
|
157
|
+
series = db.query(stmt).to_pd(dtype="Float64")
|
|
158
|
+
array = db.query(stmt).to_numpy(casts={"some_column": Float})
|
|
108
159
|
rows = db.query(stmt).fetch(7)
|
|
109
160
|
|
|
161
|
+
df = db.query(table).to_pl(schema_overrides={"some_column": pl.Float64})
|
|
162
|
+
|
|
110
163
|
count_stmt = select(func.count()).select_from(table)
|
|
111
164
|
count = db.query(count_stmt).to_scalar()
|
|
112
165
|
```
|
|
113
166
|
|
|
167
|
+
## Statement Execution
|
|
168
|
+
|
|
169
|
+
`Database.execute()` and `Database.execution()` support raw SQL statements
|
|
170
|
+
and native SQLAlchemy executable constructs within managed transactions.
|
|
171
|
+
|
|
172
|
+
| Method | Description |
|
|
173
|
+
| --- | --- |
|
|
174
|
+
| `execute()` | Executes immediately and returns the driver-reported row count |
|
|
175
|
+
| `execution()` | Provides context-managed execution with access to the native SQLAlchemy `CursorResult` |
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
db.execute("CREATE SCHEMA some_schema")
|
|
179
|
+
|
|
180
|
+
db.execute(
|
|
181
|
+
table.insert(),
|
|
182
|
+
[
|
|
183
|
+
{"id": 1, "name": "Name_1"},
|
|
184
|
+
{"id": 2, "name": "Name_2"},
|
|
185
|
+
],
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
db.execute(
|
|
189
|
+
table.update()
|
|
190
|
+
.where(table.c.id == 1)
|
|
191
|
+
.values(name="Name_3")
|
|
192
|
+
)
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
```python
|
|
196
|
+
with db.execution(
|
|
197
|
+
table.insert()
|
|
198
|
+
.values(name="Name_4")
|
|
199
|
+
.returning(table.c.id)
|
|
200
|
+
) as result:
|
|
201
|
+
inserted_ids = result.scalars().all()
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Transactions are committed upon successful completion and rolled back on
|
|
205
|
+
failure, subject to the underlying database's transaction semantics.
|
|
206
|
+
|
|
207
|
+
*Row count reporting and support for `RETURNING` depend on the underlying
|
|
208
|
+
dialect and driver. Result rows must be consumed within the `execution()` context.*
|
|
209
|
+
|
|
114
210
|
## Advanced SQLAlchemy Queries
|
|
115
211
|
|
|
116
212
|
SQL construction remains entirely native to SQLAlchemy. Below are examples of advanced query expressions and with DataFrame operations.
|
|
213
|
+
|
|
117
214
|
```python
|
|
118
215
|
import hashlib
|
|
119
216
|
from sqlalchemy import select, func, exists, or_
|
|
@@ -199,7 +296,7 @@ test.dispose()
|
|
|
199
296
|
|
|
200
297
|
| Class | Attributes / Methods |
|
|
201
298
|
| --- | --- |
|
|
202
|
-
| `Database` | `engine`, `metadata`, `reflect()`, `catalogue()`, `query()` |
|
|
299
|
+
| `Database` | `engine`, `metadata`, `reflect()`, `catalogue()`, `query()`, `execute()`, `execution()`, `dispose()` |
|
|
203
300
|
| `ReflectedTable` | `sa`, `engine`, native SQLAlchemy `Table` attributes and methods, `inspect()`, delegated `QueryExecutor` methods |
|
|
204
301
|
| `QueryExecutor` | `statement`, `engine`, `to_pd()`, `to_pl()`, `to_numpy()`, `to_arrow()`, `to_records()`, `fetch()`, `to_scalar()` |
|
|
205
302
|
|
|
@@ -209,6 +306,28 @@ test.dispose()
|
|
|
209
306
|
|
|
210
307
|
`ReflectedTable.__clause_element__()` enables SQLAlchemy expression coercion. The explicit `.sa` attribute exposes the underlying SQLAlchemy `Table` when a native table object is required.
|
|
211
308
|
|
|
309
|
+
## Optional Dependencies
|
|
310
|
+
|
|
311
|
+
Optional dependency groups are provided for additional database dialects and result conversion capabilities.
|
|
312
|
+
|
|
313
|
+
| Extra | Database / Functionality | Dependency |
|
|
314
|
+
| --- | --- | --- |
|
|
315
|
+
| `arrow` | Apache Arrow | `pyarrow` |
|
|
316
|
+
| `psycopg` | PostgreSQL | `psycopg[binary]` |
|
|
317
|
+
| `pyodbc` | SQL Server | `pyodbc` |
|
|
318
|
+
| `pymysql` | MySQL / MariaDB | `pymysql[rsa]` |
|
|
319
|
+
| `duckdb` | DuckDB | `duckdb-sqlalchemy` |
|
|
320
|
+
| `oracledb` | Oracle | `oracledb` |
|
|
321
|
+
| `bigquery` | Google BigQuery | `sqlalchemy-bigquery[bqstorage]` |
|
|
322
|
+
|
|
323
|
+
Install one or multiple extras as required:
|
|
324
|
+
|
|
325
|
+
```bash
|
|
326
|
+
pip install "AlchemyReflection[psycopg,arrow]"
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
Database drivers may require additional system-level dependencies or configuration.
|
|
330
|
+
|
|
212
331
|
|
|
213
332
|
## Design Philosophy
|
|
214
333
|
|
alchemyreflection-0.1.2/AlchemyReflection.egg-info/PKG-INFO → alchemyreflection-0.2.0/README.md
RENAMED
|
@@ -1,30 +1,16 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: AlchemyReflection
|
|
3
|
-
Version: 0.1.2
|
|
4
|
-
Summary: An object-oriented interface for SQLAlchemy, designed for database reflection, query execution, and result retrieval, built around method chaining and native interoperability.
|
|
5
|
-
Author: MI77-LAB
|
|
6
|
-
License-Expression: MIT
|
|
7
|
-
Project-URL: Repository, https://github.com/mirabelleding/AlchemyReflection
|
|
8
|
-
Classifier: Development Status :: 3 - Alpha
|
|
9
|
-
Classifier: Intended Audience :: Developers
|
|
10
|
-
Classifier: Operating System :: OS Independent
|
|
11
|
-
Classifier: Programming Language :: Python :: 3
|
|
12
|
-
Classifier: Topic :: Database
|
|
13
|
-
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
14
|
-
Requires-Python: >=3.10
|
|
15
|
-
Description-Content-Type: text/markdown
|
|
16
|
-
License-File: LICENSE
|
|
17
|
-
Requires-Dist: sqlalchemy<3.0,>=2.0
|
|
18
|
-
Requires-Dist: pandas<4.0,>=2.0
|
|
19
|
-
Requires-Dist: polars<3.0,>=1.0
|
|
20
|
-
Provides-Extra: arrow
|
|
21
|
-
Requires-Dist: pyarrow<27.0,>=15.0; extra == "arrow"
|
|
22
|
-
Provides-Extra: pyodbc
|
|
23
|
-
Requires-Dist: pyodbc<6.0,>=5.0; extra == "pyodbc"
|
|
24
|
-
Dynamic: license-file
|
|
25
|
-
|
|
26
1
|
# AlchemyReflection
|
|
27
|
-
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/AlchemyReflection/)
|
|
4
|
+
|
|
5
|
+
[](https://pypi.org/project/AlchemyReflection/)
|
|
6
|
+
|
|
7
|
+
[](LICENSE)
|
|
8
|
+
</br>
|
|
9
|
+
[](https://github.com/mirabelleding/AlchemyReflection/actions/workflows/tests.yml)
|
|
10
|
+
|
|
11
|
+
[](https://github.com/mirabelleding/AlchemyReflection/actions/workflows/compatibility.yml)
|
|
12
|
+
|
|
13
|
+
A lightweight Python package built on SQLAlchemy for database connection management, table reflection, schema inspection, statement execution, and query result retrieval.
|
|
28
14
|
|
|
29
15
|
Designed with an emphasis on API ergonomics and method chaining, the object-oriented interface extends SQLAlchemy functionality while preserving native interoperability.
|
|
30
16
|
|
|
@@ -55,10 +41,7 @@ db = Database("postgresql+psycopg://user:password@localhost:5432/database_name")
|
|
|
55
41
|
```
|
|
56
42
|
|
|
57
43
|
**Raw SQL Server ODBC connection string:**<br>
|
|
58
|
-
|
|
59
|
-
```bash
|
|
60
|
-
pip install "AlchemyReflection[pyodbc]"
|
|
61
|
-
```
|
|
44
|
+
|
|
62
45
|
```text
|
|
63
46
|
DRIVER={ODBC Driver 18 for SQL Server};
|
|
64
47
|
SERVER=server_name;
|
|
@@ -72,8 +55,9 @@ from alchemy_reflection import Database
|
|
|
72
55
|
|
|
73
56
|
db = Database(os.environ["DB_ODBC"])
|
|
74
57
|
```
|
|
75
|
-
Each `Database` instance maintains its own SQLAlchemy engine and reflected metadata.
|
|
58
|
+
Each `Database` instance maintains its own SQLAlchemy engine and reflected metadata.
|
|
76
59
|
|
|
60
|
+
*Database connectivity and reflection capabilities depend on the underlying SQLAlchemy dialect and driver. Installation of the corresponding dependencies is required.*
|
|
77
61
|
## Basic Usage
|
|
78
62
|
```python
|
|
79
63
|
import os
|
|
@@ -85,7 +69,7 @@ from alchemy_reflection import Database
|
|
|
85
69
|
db = Database(os.environ["DB_ODBC"])
|
|
86
70
|
|
|
87
71
|
# Search and pretty-print matching tables and views
|
|
88
|
-
db.catalogue("some keywords", pprint=True)
|
|
72
|
+
db.catalogue(keyword="some keywords", schema="some_schema", pprint=True)
|
|
89
73
|
|
|
90
74
|
# Instantiate a ReflectedTable
|
|
91
75
|
table = db.reflect("schemaname.tablename")
|
|
@@ -108,9 +92,9 @@ reflected_table.alias("t")
|
|
|
108
92
|
reflected_table.select()
|
|
109
93
|
```
|
|
110
94
|
|
|
111
|
-
## Query
|
|
95
|
+
## Query and Result Retrieval
|
|
112
96
|
|
|
113
|
-
`QueryExecutor` supports query
|
|
97
|
+
`QueryExecutor` supports query and result retrieval for raw SQL statements, SQLAlchemy `Selectable`, and `ReflectedTable` instances. Its methods are accessible through `Database.query()` or directly through `ReflectedTable` interface.
|
|
114
98
|
|
|
115
99
|
| Method | Return Type |
|
|
116
100
|
| --- | --- |
|
|
@@ -122,23 +106,71 @@ reflected_table.select()
|
|
|
122
106
|
| `fetch(n_rows=None)` | List of SQLAlchemy `Row` objects; all rows are retrieved by default |
|
|
123
107
|
| `to_scalar()` | Scalar value from a single-row, single-column query |
|
|
124
108
|
|
|
109
|
+
*`to_pd()`, `to_pl()`, `to_numpy()`, and `to_arrow()` support SQL-level type conversions via `casts` for SQLAlchemy `Select` statements. Native pandas and Polars conversion parameters are also exposed, with `to_arrow()` inheriting those of `to_pl()`.*
|
|
125
110
|
|
|
126
111
|
```python
|
|
127
|
-
|
|
112
|
+
import polars as pl
|
|
113
|
+
from sqlalchemy import Float, func, select
|
|
128
114
|
|
|
129
115
|
stmt = select(table.c.some_column)
|
|
130
116
|
|
|
131
|
-
series = db.query(stmt).to_pd()
|
|
132
|
-
array = db.query(stmt).to_numpy()
|
|
117
|
+
series = db.query(stmt).to_pd(dtype="Float64")
|
|
118
|
+
array = db.query(stmt).to_numpy(casts={"some_column": Float})
|
|
133
119
|
rows = db.query(stmt).fetch(7)
|
|
134
120
|
|
|
121
|
+
df = db.query(table).to_pl(schema_overrides={"some_column": pl.Float64})
|
|
122
|
+
|
|
135
123
|
count_stmt = select(func.count()).select_from(table)
|
|
136
124
|
count = db.query(count_stmt).to_scalar()
|
|
137
125
|
```
|
|
138
126
|
|
|
127
|
+
## Statement Execution
|
|
128
|
+
|
|
129
|
+
`Database.execute()` and `Database.execution()` support raw SQL statements
|
|
130
|
+
and native SQLAlchemy executable constructs within managed transactions.
|
|
131
|
+
|
|
132
|
+
| Method | Description |
|
|
133
|
+
| --- | --- |
|
|
134
|
+
| `execute()` | Executes immediately and returns the driver-reported row count |
|
|
135
|
+
| `execution()` | Provides context-managed execution with access to the native SQLAlchemy `CursorResult` |
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
db.execute("CREATE SCHEMA some_schema")
|
|
139
|
+
|
|
140
|
+
db.execute(
|
|
141
|
+
table.insert(),
|
|
142
|
+
[
|
|
143
|
+
{"id": 1, "name": "Name_1"},
|
|
144
|
+
{"id": 2, "name": "Name_2"},
|
|
145
|
+
],
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
db.execute(
|
|
149
|
+
table.update()
|
|
150
|
+
.where(table.c.id == 1)
|
|
151
|
+
.values(name="Name_3")
|
|
152
|
+
)
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
with db.execution(
|
|
157
|
+
table.insert()
|
|
158
|
+
.values(name="Name_4")
|
|
159
|
+
.returning(table.c.id)
|
|
160
|
+
) as result:
|
|
161
|
+
inserted_ids = result.scalars().all()
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Transactions are committed upon successful completion and rolled back on
|
|
165
|
+
failure, subject to the underlying database's transaction semantics.
|
|
166
|
+
|
|
167
|
+
*Row count reporting and support for `RETURNING` depend on the underlying
|
|
168
|
+
dialect and driver. Result rows must be consumed within the `execution()` context.*
|
|
169
|
+
|
|
139
170
|
## Advanced SQLAlchemy Queries
|
|
140
171
|
|
|
141
172
|
SQL construction remains entirely native to SQLAlchemy. Below are examples of advanced query expressions and with DataFrame operations.
|
|
173
|
+
|
|
142
174
|
```python
|
|
143
175
|
import hashlib
|
|
144
176
|
from sqlalchemy import select, func, exists, or_
|
|
@@ -224,7 +256,7 @@ test.dispose()
|
|
|
224
256
|
|
|
225
257
|
| Class | Attributes / Methods |
|
|
226
258
|
| --- | --- |
|
|
227
|
-
| `Database` | `engine`, `metadata`, `reflect()`, `catalogue()`, `query()` |
|
|
259
|
+
| `Database` | `engine`, `metadata`, `reflect()`, `catalogue()`, `query()`, `execute()`, `execution()`, `dispose()` |
|
|
228
260
|
| `ReflectedTable` | `sa`, `engine`, native SQLAlchemy `Table` attributes and methods, `inspect()`, delegated `QueryExecutor` methods |
|
|
229
261
|
| `QueryExecutor` | `statement`, `engine`, `to_pd()`, `to_pl()`, `to_numpy()`, `to_arrow()`, `to_records()`, `fetch()`, `to_scalar()` |
|
|
230
262
|
|
|
@@ -234,6 +266,28 @@ test.dispose()
|
|
|
234
266
|
|
|
235
267
|
`ReflectedTable.__clause_element__()` enables SQLAlchemy expression coercion. The explicit `.sa` attribute exposes the underlying SQLAlchemy `Table` when a native table object is required.
|
|
236
268
|
|
|
269
|
+
## Optional Dependencies
|
|
270
|
+
|
|
271
|
+
Optional dependency groups are provided for additional database dialects and result conversion capabilities.
|
|
272
|
+
|
|
273
|
+
| Extra | Database / Functionality | Dependency |
|
|
274
|
+
| --- | --- | --- |
|
|
275
|
+
| `arrow` | Apache Arrow | `pyarrow` |
|
|
276
|
+
| `psycopg` | PostgreSQL | `psycopg[binary]` |
|
|
277
|
+
| `pyodbc` | SQL Server | `pyodbc` |
|
|
278
|
+
| `pymysql` | MySQL / MariaDB | `pymysql[rsa]` |
|
|
279
|
+
| `duckdb` | DuckDB | `duckdb-sqlalchemy` |
|
|
280
|
+
| `oracledb` | Oracle | `oracledb` |
|
|
281
|
+
| `bigquery` | Google BigQuery | `sqlalchemy-bigquery[bqstorage]` |
|
|
282
|
+
|
|
283
|
+
Install one or multiple extras as required:
|
|
284
|
+
|
|
285
|
+
```bash
|
|
286
|
+
pip install "AlchemyReflection[psycopg,arrow]"
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
Database drivers may require additional system-level dependencies or configuration.
|
|
290
|
+
|
|
237
291
|
|
|
238
292
|
## Design Philosophy
|
|
239
293
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Mapping, Sequence
|
|
4
|
+
from typing import Any, Literal
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
import pandas as pd
|
|
8
|
+
import polars as pl
|
|
9
|
+
from sqlalchemy.types import TypeEngine
|
|
10
|
+
|
|
11
|
+
SQLDtype = TypeEngine | type[TypeEngine]
|
|
12
|
+
SQLCasts = Mapping[str, SQLDtype]
|
|
13
|
+
SQLParameters = Mapping[str, Any] | Sequence[Mapping[str, Any]]
|
|
14
|
+
|
|
15
|
+
PandasDtype = str | type | np.dtype | pd.api.extensions.ExtensionDtype
|
|
16
|
+
PandasDtypeArg = PandasDtype | Mapping[str, PandasDtype]
|
|
17
|
+
PandasDtypeBackend = Literal["numpy_nullable", "pyarrow"]
|
|
18
|
+
PandasParseDates = list[str] | dict[str, str] | dict[str, dict[str, Any]]
|
|
19
|
+
|
|
20
|
+
PolarsDtype = pl.DataType | type[pl.DataType]
|
|
21
|
+
PolarsSchemaOverrides = Mapping[str, PolarsDtype]
|