thoth-dbmanager 0.4.13__py3-none-any.whl → 0.5.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.
- thoth_dbmanager/ThothDbManager.py +19 -227
- thoth_dbmanager/__init__.py +8 -63
- thoth_dbmanager/adapters/__init__.py +0 -6
- thoth_dbmanager/core/factory.py +0 -16
- thoth_dbmanager/dynamic_imports.py +0 -9
- thoth_dbmanager/helpers/multi_db_generator.py +1 -1
- thoth_dbmanager/plugins/__init__.py +0 -6
- thoth_dbmanager/plugins/sqlite.py +7 -0
- {thoth_dbmanager-0.4.13.dist-info → thoth_dbmanager-0.5.1.dist-info}/METADATA +228 -27
- {thoth_dbmanager-0.4.13.dist-info → thoth_dbmanager-0.5.1.dist-info}/RECORD +13 -19
- thoth_dbmanager/adapters/mysql.py +0 -165
- thoth_dbmanager/adapters/oracle.py +0 -554
- thoth_dbmanager/adapters/supabase.py +0 -249
- thoth_dbmanager/plugins/mysql.py +0 -408
- thoth_dbmanager/plugins/oracle.py +0 -150
- thoth_dbmanager/plugins/supabase.py +0 -224
- {thoth_dbmanager-0.4.13.dist-info → thoth_dbmanager-0.5.1.dist-info}/WHEEL +0 -0
- {thoth_dbmanager-0.4.13.dist-info → thoth_dbmanager-0.5.1.dist-info}/licenses/LICENSE +0 -0
- {thoth_dbmanager-0.4.13.dist-info → thoth_dbmanager-0.5.1.dist-info}/top_level.txt +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: thoth_dbmanager
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.5.1
|
4
4
|
Summary: A Python library for managing SQL databases with support for multiple database types, LSH-based similarity search, and a modern plugin architecture.
|
5
5
|
Author-email: Marco Pancotti <mp@tylconsulting.it>
|
6
6
|
Project-URL: Homepage, https://github.com/mptyl/thoth_dbmanager
|
@@ -9,7 +9,6 @@ Project-URL: Documentation, https://github.com/mptyl/thoth_dbmanager#readme
|
|
9
9
|
Project-URL: Source Code, https://github.com/mptyl/thoth_dbmanager
|
10
10
|
Keywords: database,sql,lsh,similarity-search,orm
|
11
11
|
Classifier: Programming Language :: Python :: 3
|
12
|
-
Classifier: Programming Language :: Python :: 3.8
|
13
12
|
Classifier: Programming Language :: Python :: 3.9
|
14
13
|
Classifier: Programming Language :: Python :: 3.10
|
15
14
|
Classifier: Programming Language :: Python :: 3.11
|
@@ -20,7 +19,7 @@ Classifier: Intended Audience :: Developers
|
|
20
19
|
Classifier: Topic :: Database
|
21
20
|
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
22
21
|
Classifier: Development Status :: 4 - Beta
|
23
|
-
Requires-Python: >=3.
|
22
|
+
Requires-Python: >=3.9
|
24
23
|
Description-Content-Type: text/markdown
|
25
24
|
License-File: LICENSE
|
26
25
|
Requires-Dist: datasketch>=1.5.0
|
@@ -31,30 +30,15 @@ Requires-Dist: pandas>=1.3.0
|
|
31
30
|
Requires-Dist: requests>=2.25.0
|
32
31
|
Provides-Extra: postgresql
|
33
32
|
Requires-Dist: psycopg2-binary>=2.9.0; extra == "postgresql"
|
34
|
-
Provides-Extra: mysql
|
35
|
-
Requires-Dist: mysql-connector-python>=8.0.0; extra == "mysql"
|
36
33
|
Provides-Extra: mariadb
|
37
34
|
Requires-Dist: mariadb>=1.1.0; extra == "mariadb"
|
38
35
|
Provides-Extra: sqlserver
|
39
36
|
Requires-Dist: pyodbc>=4.0.0; extra == "sqlserver"
|
40
|
-
Requires-Dist: pymssql>=2.3.0; extra == "sqlserver"
|
41
|
-
Provides-Extra: oracle
|
42
|
-
Requires-Dist: cx_Oracle>=8.3.0; extra == "oracle"
|
43
|
-
Requires-Dist: oracledb>=3.0.0; extra == "oracle"
|
44
|
-
Provides-Extra: supabase
|
45
|
-
Requires-Dist: supabase>=2.0.0; extra == "supabase"
|
46
|
-
Requires-Dist: postgrest-py>=0.10.0; extra == "supabase"
|
47
|
-
Requires-Dist: gotrue>=1.0.0; extra == "supabase"
|
48
37
|
Provides-Extra: sqlite
|
49
38
|
Provides-Extra: all
|
50
39
|
Requires-Dist: psycopg2-binary>=2.9.0; extra == "all"
|
51
|
-
Requires-Dist: mysql-connector-python>=8.0.0; extra == "all"
|
52
40
|
Requires-Dist: mariadb>=1.1.0; extra == "all"
|
53
41
|
Requires-Dist: pyodbc>=4.0.0; extra == "all"
|
54
|
-
Requires-Dist: pymssql>=2.3.0; extra == "all"
|
55
|
-
Requires-Dist: cx_Oracle>=8.3.0; extra == "all"
|
56
|
-
Requires-Dist: oracledb>=3.0.0; extra == "all"
|
57
|
-
Requires-Dist: supabase>=2.0.0; extra == "all"
|
58
42
|
Provides-Extra: dev
|
59
43
|
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
60
44
|
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
@@ -69,9 +53,12 @@ Requires-Dist: docker>=6.0.0; extra == "dev"
|
|
69
53
|
Provides-Extra: test-postgresql
|
70
54
|
Requires-Dist: pytest>=7.0.0; extra == "test-postgresql"
|
71
55
|
Requires-Dist: psycopg2-binary>=2.9.0; extra == "test-postgresql"
|
72
|
-
Provides-Extra: test-
|
73
|
-
Requires-Dist: pytest>=7.0.0; extra == "test-
|
74
|
-
Requires-Dist:
|
56
|
+
Provides-Extra: test-mariadb
|
57
|
+
Requires-Dist: pytest>=7.0.0; extra == "test-mariadb"
|
58
|
+
Requires-Dist: mariadb>=1.1.0; extra == "test-mariadb"
|
59
|
+
Provides-Extra: test-sqlserver
|
60
|
+
Requires-Dist: pytest>=7.0.0; extra == "test-sqlserver"
|
61
|
+
Requires-Dist: pyodbc>=4.0.0; extra == "test-sqlserver"
|
75
62
|
Dynamic: license-file
|
76
63
|
|
77
64
|
# Thoth Database Manager
|
@@ -84,12 +71,83 @@ A Python library for managing SQL databases with support for multiple database t
|
|
84
71
|
- **Plugin Architecture**: Extensible design for adding new database types
|
85
72
|
- **LSH Search**: Locality-Sensitive Hashing for finding similar values across database columns
|
86
73
|
- **Type Safety**: Pydantic-based document models for structured data
|
87
|
-
- **
|
74
|
+
- **Modern API**: Clean, plugin-based interface for all database operations
|
88
75
|
|
89
76
|
## Installation
|
90
77
|
|
78
|
+
### Basic Installation
|
79
|
+
|
80
|
+
```bash
|
81
|
+
uv add thoth-dbmanager
|
82
|
+
```
|
83
|
+
|
84
|
+
### Installation with Database Support
|
85
|
+
|
86
|
+
To use specific databases, you'll need to install the corresponding database drivers. You can install them individually or use our convenience extras:
|
87
|
+
|
88
|
+
#### Install with All Database Support
|
89
|
+
|
90
|
+
```bash
|
91
|
+
uv add thoth-dbmanager[all]
|
92
|
+
```
|
93
|
+
|
94
|
+
#### Install with Specific Database Support
|
95
|
+
|
96
|
+
**PostgreSQL:**
|
97
|
+
|
98
|
+
```bash
|
99
|
+
uv add thoth-dbmanager[postgresql]
|
100
|
+
# or manually: uv add psycopg2-binary
|
101
|
+
```
|
102
|
+
|
103
|
+
**MySQL:**
|
104
|
+
|
105
|
+
```bash
|
106
|
+
uv add thoth-dbmanager[mysql]
|
107
|
+
# or manually: uv add mysql-connector-python
|
108
|
+
```
|
109
|
+
|
110
|
+
**MariaDB:**
|
111
|
+
|
112
|
+
```bash
|
113
|
+
uv add thoth-dbmanager[mariadb]
|
114
|
+
# or manually: uv add mariadb
|
115
|
+
```
|
116
|
+
|
117
|
+
**SQL Server:**
|
118
|
+
|
119
|
+
```bash
|
120
|
+
uv add thoth-dbmanager[sqlserver]
|
121
|
+
# or manually: uv add pyodbc
|
122
|
+
```
|
123
|
+
|
124
|
+
**Oracle:**
|
125
|
+
|
126
|
+
```bash
|
127
|
+
uv add thoth-dbmanager[oracle]
|
128
|
+
# or manually: uv add cx_Oracle
|
129
|
+
```
|
130
|
+
|
131
|
+
**Informix:**
|
132
|
+
|
133
|
+
```bash
|
134
|
+
uv add thoth-dbmanager[informix]
|
135
|
+
# or manually: uv add IfxPy
|
136
|
+
```
|
137
|
+
|
138
|
+
**Supabase:**
|
139
|
+
|
91
140
|
```bash
|
92
|
-
|
141
|
+
uv add thoth-dbmanager[supabase]
|
142
|
+
# or manually: uv add supabase gotrue
|
143
|
+
```
|
144
|
+
|
145
|
+
**SQLite** is supported out of the box (no additional drivers needed).
|
146
|
+
|
147
|
+
#### Development Installation
|
148
|
+
|
149
|
+
```bash
|
150
|
+
uv add thoth-dbmanager[dev]
|
93
151
|
```
|
94
152
|
|
95
153
|
## Quick Start
|
@@ -278,9 +336,17 @@ if hasattr(manager, 'get_columns_as_documents'):
|
|
278
336
|
print(f"Primary Key: {doc.is_pk}")
|
279
337
|
```
|
280
338
|
|
281
|
-
##
|
339
|
+
## Database Connection Examples
|
282
340
|
|
283
341
|
### PostgreSQL
|
342
|
+
|
343
|
+
**Installation:**
|
344
|
+
|
345
|
+
```bash
|
346
|
+
uv add thoth-dbmanager[postgresql]
|
347
|
+
```
|
348
|
+
|
349
|
+
**Usage:**
|
284
350
|
```python
|
285
351
|
manager = ThothDbManager.get_instance(
|
286
352
|
db_type="postgresql",
|
@@ -294,7 +360,63 @@ manager = ThothDbManager.get_instance(
|
|
294
360
|
)
|
295
361
|
```
|
296
362
|
|
363
|
+
### MySQL
|
364
|
+
|
365
|
+
**Installation:**
|
366
|
+
|
367
|
+
```bash
|
368
|
+
uv add thoth-dbmanager[mysql]
|
369
|
+
```
|
370
|
+
|
371
|
+
**Usage:**
|
372
|
+
|
373
|
+
```python
|
374
|
+
manager = ThothDbManager.get_instance(
|
375
|
+
db_type="mysql",
|
376
|
+
db_root_path="./data",
|
377
|
+
db_mode="production",
|
378
|
+
host="localhost",
|
379
|
+
port=3306,
|
380
|
+
database="myapp",
|
381
|
+
user="dbuser",
|
382
|
+
password="dbpass"
|
383
|
+
)
|
384
|
+
```
|
385
|
+
|
386
|
+
### MariaDB
|
387
|
+
|
388
|
+
**Installation:**
|
389
|
+
|
390
|
+
```bash
|
391
|
+
uv add thoth-dbmanager[mariadb]
|
392
|
+
```
|
393
|
+
|
394
|
+
**Usage:**
|
395
|
+
|
396
|
+
```python
|
397
|
+
manager = ThothDbManager.get_instance(
|
398
|
+
db_type="mariadb",
|
399
|
+
db_root_path="./data",
|
400
|
+
db_mode="production",
|
401
|
+
host="localhost",
|
402
|
+
port=3306,
|
403
|
+
database="myapp",
|
404
|
+
user="dbuser",
|
405
|
+
password="dbpass"
|
406
|
+
)
|
407
|
+
```
|
408
|
+
|
297
409
|
### SQLite
|
410
|
+
|
411
|
+
**Installation:**
|
412
|
+
|
413
|
+
```bash
|
414
|
+
# No additional drivers needed - SQLite is included with Python
|
415
|
+
uv add thoth-dbmanager
|
416
|
+
```
|
417
|
+
|
418
|
+
**Usage:**
|
419
|
+
|
298
420
|
```python
|
299
421
|
manager = ThothDbManager.get_instance(
|
300
422
|
db_type="sqlite",
|
@@ -304,20 +426,99 @@ manager = ThothDbManager.get_instance(
|
|
304
426
|
)
|
305
427
|
```
|
306
428
|
|
307
|
-
###
|
429
|
+
### SQL Server
|
430
|
+
|
431
|
+
**Installation:**
|
432
|
+
|
433
|
+
```bash
|
434
|
+
uv add thoth-dbmanager[sqlserver]
|
435
|
+
```
|
436
|
+
|
437
|
+
**Usage:**
|
438
|
+
|
439
|
+
```python
|
440
|
+
manager = ThothDbManager.get_instance(
|
441
|
+
db_type="sqlserver",
|
442
|
+
db_root_path="./data",
|
443
|
+
db_mode="production",
|
444
|
+
server="localhost",
|
445
|
+
database="myapp",
|
446
|
+
user="dbuser",
|
447
|
+
password="dbpass"
|
448
|
+
)
|
449
|
+
```
|
450
|
+
|
451
|
+
### Oracle
|
452
|
+
|
453
|
+
**Installation:**
|
454
|
+
|
455
|
+
```bash
|
456
|
+
uv add thoth-dbmanager[oracle]
|
457
|
+
```
|
458
|
+
|
459
|
+
**Usage:**
|
460
|
+
|
308
461
|
```python
|
309
462
|
manager = ThothDbManager.get_instance(
|
310
|
-
db_type="
|
463
|
+
db_type="oracle",
|
311
464
|
db_root_path="./data",
|
312
465
|
db_mode="production",
|
313
466
|
host="localhost",
|
314
|
-
port=
|
467
|
+
port=1521,
|
468
|
+
service_name="ORCL",
|
469
|
+
user="dbuser",
|
470
|
+
password="dbpass"
|
471
|
+
)
|
472
|
+
```
|
473
|
+
|
474
|
+
### Informix
|
475
|
+
|
476
|
+
**Installation:**
|
477
|
+
|
478
|
+
```bash
|
479
|
+
uv add thoth-dbmanager[informix]
|
480
|
+
```
|
481
|
+
|
482
|
+
**Usage:**
|
483
|
+
|
484
|
+
```python
|
485
|
+
manager = ThothDbManager.get_instance(
|
486
|
+
db_type="informix",
|
487
|
+
db_root_path="./data",
|
488
|
+
db_mode="production",
|
489
|
+
server="informix_server",
|
315
490
|
database="myapp",
|
491
|
+
host="localhost",
|
316
492
|
user="dbuser",
|
317
493
|
password="dbpass"
|
318
494
|
)
|
319
495
|
```
|
320
496
|
|
497
|
+
### Supabase
|
498
|
+
|
499
|
+
**Installation:**
|
500
|
+
|
501
|
+
```bash
|
502
|
+
uv add thoth-dbmanager[supabase]
|
503
|
+
```
|
504
|
+
|
505
|
+
**Usage:**
|
506
|
+
|
507
|
+
```python
|
508
|
+
manager = ThothDbManager.get_instance(
|
509
|
+
db_type="supabase",
|
510
|
+
db_root_path="./data",
|
511
|
+
db_mode="production",
|
512
|
+
host="db.example.supabase.co",
|
513
|
+
port=5432,
|
514
|
+
database="postgres",
|
515
|
+
user="postgres",
|
516
|
+
password="your_password",
|
517
|
+
project_url="https://your-project.supabase.co",
|
518
|
+
api_key="your_supabase_api_key"
|
519
|
+
)
|
520
|
+
```
|
521
|
+
|
321
522
|
## Error Handling
|
322
523
|
|
323
524
|
The library provides clear error messages for common issues:
|
@@ -1,21 +1,18 @@
|
|
1
|
-
thoth_dbmanager/ThothDbManager.py,sha256=
|
2
|
-
thoth_dbmanager/__init__.py,sha256=
|
1
|
+
thoth_dbmanager/ThothDbManager.py,sha256=q-jctgt3MJCDFzq6icQdP1oLeVy1ypg402F4ybxhG8c,8943
|
2
|
+
thoth_dbmanager/__init__.py,sha256=Qrd9n7YYMkNKCFiIkzWbjKf5LNOkfp4TkebEpsKqt7E,1655
|
3
3
|
thoth_dbmanager/documents.py,sha256=z-f7zo_CZHqoGM0qHT8-lSUx4NhnMNZTSajpoFtRxn4,5051
|
4
|
-
thoth_dbmanager/dynamic_imports.py,sha256=
|
5
|
-
thoth_dbmanager/adapters/__init__.py,sha256=
|
4
|
+
thoth_dbmanager/dynamic_imports.py,sha256=xDahgiqKvwSYqjPgHiQqD1XPhAbM_JqnU3OhBp2N-fc,7013
|
5
|
+
thoth_dbmanager/adapters/__init__.py,sha256=Ua3ZjSOFlP9kVMFTiC6fyrpcv327b_mBimQ_4fnUsGY,318
|
6
6
|
thoth_dbmanager/adapters/mariadb.py,sha256=LTsf0gORiwqZkd6WtKcOsYLHyDgysxdqNesBscbJwNs,5709
|
7
|
-
thoth_dbmanager/adapters/mysql.py,sha256=TrFbxoMMNWbmUcgkKQYOIfsstmMUmuLlGB7R4ZFEIYI,5698
|
8
|
-
thoth_dbmanager/adapters/oracle.py,sha256=JSrsgohjz5PbVc8nI188MZ4QGBQls4ieNmwWfAKA7II,21468
|
9
7
|
thoth_dbmanager/adapters/postgresql.py,sha256=qxdlxOV7Nvn8U4Lhat50w87Z2S8AzBfmLfEwKfz7dis,17299
|
10
8
|
thoth_dbmanager/adapters/sqlite.py,sha256=RTDszgnAtkE14LKFeoe9lBHgsqXqkmDk6jDCTmVpnoM,14659
|
11
9
|
thoth_dbmanager/adapters/sqlserver.py,sha256=V555kUH54Fb1Atow0BfvbSHmoSwGnrB_RJGn718VQSI,23880
|
12
|
-
thoth_dbmanager/adapters/supabase.py,sha256=bl2C6LpOpykPF3vIbdNRDk43aXLADzSk0wQuwTcEHZA,10348
|
13
10
|
thoth_dbmanager/core/__init__.py,sha256=FlqNW0GZNv1rnwNgyXGzveLqaw0Z90y5AKhR_1DvHBE,269
|
14
|
-
thoth_dbmanager/core/factory.py,sha256=
|
11
|
+
thoth_dbmanager/core/factory.py,sha256=84EeZYRoH7y7b19EFHqN4X0CSA6dv-0yxUmlX2zHETk,8840
|
15
12
|
thoth_dbmanager/core/interfaces.py,sha256=wZpKVQJdwMlAsHTQMB7yVviD2-N_dlOe19F-GhgEoGE,9576
|
16
13
|
thoth_dbmanager/core/registry.py,sha256=url4qpQMoMw4rDrdAAvV6L7-NdO4z86xSJPSwTH_l5g,8624
|
17
14
|
thoth_dbmanager/helpers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
18
|
-
thoth_dbmanager/helpers/multi_db_generator.py,sha256=
|
15
|
+
thoth_dbmanager/helpers/multi_db_generator.py,sha256=frN0SZtWAfeojoJFLs4XLR3ri6h9pHYc-2O4aLAOlbo,23238
|
19
16
|
thoth_dbmanager/helpers/preprocess_values.py,sha256=sKMjD50UL2CZG7-g7KGe3TJQeXmXC7n28LGge8CaP5c,6183
|
20
17
|
thoth_dbmanager/helpers/schema.py,sha256=PhHgUxfFe7qUPxdpWQ3YfS593VQjyKtaSKMlChCyLNo,13978
|
21
18
|
thoth_dbmanager/helpers/search.py,sha256=k04L7clSPfeQOlq_ifsH3PZ21ZK-rujh_Qy4hWrvSso,3970
|
@@ -24,16 +21,13 @@ thoth_dbmanager/lsh/core.py,sha256=171FqHW7ItAqAPk6g_AoayKTE3Bs1rRZxnt55MJVzjY,6
|
|
24
21
|
thoth_dbmanager/lsh/factory.py,sha256=2Bpkk-OygjaptZAw1yysxO1cxG3QTxmJ1yFGcXHqX3w,2411
|
25
22
|
thoth_dbmanager/lsh/manager.py,sha256=LGrKbGKiBuISlNXaU4Yxfc_BqJfN27MaXapJbzEAjJQ,6513
|
26
23
|
thoth_dbmanager/lsh/storage.py,sha256=qei6fwpmRCBSS8CRtDlnZCuWEmyuOK9gVSTkEJdX0eI,4543
|
27
|
-
thoth_dbmanager/plugins/__init__.py,sha256=
|
24
|
+
thoth_dbmanager/plugins/__init__.py,sha256=KiamB8UgZujwLUE8Q5suYgqC2i5VEAdmC1m0KF9GgvM,430
|
28
25
|
thoth_dbmanager/plugins/mariadb.py,sha256=ElYa4Rexwrofcjcs0UQKan8fZpbU6-n9zghYR9SgRb4,17975
|
29
|
-
thoth_dbmanager/plugins/mysql.py,sha256=mbDsIDV2H_BWYANU4JHMsUkxLQICuGtjKTTPbig2Ngs,16546
|
30
|
-
thoth_dbmanager/plugins/oracle.py,sha256=k4Yxvz5MdsH3Sfty9lxbhr8igSnHvGbGujz3bLpNcHo,5230
|
31
26
|
thoth_dbmanager/plugins/postgresql.py,sha256=pI1W9oHpQty8tHMoEDcsOT-Msv6S4aoFcArOGFxLR7Q,5518
|
32
|
-
thoth_dbmanager/plugins/sqlite.py,sha256=
|
27
|
+
thoth_dbmanager/plugins/sqlite.py,sha256=B-9ATDQacaBHbQTexWNeJo1_F2k1z6JrsApiYI_3FgM,8853
|
33
28
|
thoth_dbmanager/plugins/sqlserver.py,sha256=mMb3F5FmSWV02FZwj-Ult-2TjuyeVA4Fl1iME1dbgLU,5289
|
34
|
-
thoth_dbmanager/
|
35
|
-
thoth_dbmanager-0.
|
36
|
-
thoth_dbmanager-0.
|
37
|
-
thoth_dbmanager-0.
|
38
|
-
thoth_dbmanager-0.
|
39
|
-
thoth_dbmanager-0.4.13.dist-info/RECORD,,
|
29
|
+
thoth_dbmanager-0.5.1.dist-info/licenses/LICENSE,sha256=81-BOzGgwtY1XdYfkwMQB87AkOGXI9OMq0kjNcZA4UE,1071
|
30
|
+
thoth_dbmanager-0.5.1.dist-info/METADATA,sha256=WwtN_HnMRIqWZBT4L9yjHLY56q6B9pRDk1PoJ24QNoQ,14781
|
31
|
+
thoth_dbmanager-0.5.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
32
|
+
thoth_dbmanager-0.5.1.dist-info/top_level.txt,sha256=b9ttxm9RUc0KUCASEKRx6FqoREYJ1-KZWSpNuaM0uQ4,16
|
33
|
+
thoth_dbmanager-0.5.1.dist-info/RECORD,,
|
@@ -1,165 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
MySQL adapter for Thoth SQL Database Manager.
|
3
|
-
"""
|
4
|
-
|
5
|
-
from typing import Any, Dict, List, Optional, Union
|
6
|
-
from sqlalchemy import create_engine, text
|
7
|
-
from sqlalchemy.engine import Engine
|
8
|
-
from sqlalchemy.exc import SQLAlchemyError
|
9
|
-
|
10
|
-
from ..core.interfaces import DbAdapter
|
11
|
-
|
12
|
-
|
13
|
-
class MySQLAdapter(DbAdapter):
|
14
|
-
"""MySQL database adapter."""
|
15
|
-
|
16
|
-
def __init__(self, connection_string: str, **kwargs: Any) -> None:
|
17
|
-
"""
|
18
|
-
Initialize MySQL adapter.
|
19
|
-
|
20
|
-
Args:
|
21
|
-
connection_string: MySQL connection string
|
22
|
-
**kwargs: Additional connection parameters
|
23
|
-
"""
|
24
|
-
self.connection_string = connection_string
|
25
|
-
self.engine = None
|
26
|
-
self.connection_params = kwargs
|
27
|
-
|
28
|
-
def connect(self) -> None:
|
29
|
-
"""Establish database connection."""
|
30
|
-
try:
|
31
|
-
self.engine = create_engine(
|
32
|
-
self.connection_string,
|
33
|
-
pool_pre_ping=True,
|
34
|
-
**self.connection_params
|
35
|
-
)
|
36
|
-
except Exception as e:
|
37
|
-
raise ConnectionError(f"Failed to connect to MySQL: {e}")
|
38
|
-
|
39
|
-
def disconnect(self) -> None:
|
40
|
-
"""Close database connection."""
|
41
|
-
if self.engine:
|
42
|
-
self.engine.dispose()
|
43
|
-
self.engine = None
|
44
|
-
|
45
|
-
def execute_query(self, query: str, params: Optional[Dict[str, Any]] = None) -> List[Dict[str, Any]]:
|
46
|
-
"""Execute a query and return results."""
|
47
|
-
if not self.engine:
|
48
|
-
self.connect()
|
49
|
-
|
50
|
-
try:
|
51
|
-
with self.engine.connect() as conn:
|
52
|
-
result = conn.execute(text(query), params or {})
|
53
|
-
return [dict(row._mapping) for row in result]
|
54
|
-
except SQLAlchemyError as e:
|
55
|
-
raise RuntimeError(f"MySQL query failed: {e}")
|
56
|
-
|
57
|
-
def execute_update(self, query: str, params: Optional[Dict[str, Any]] = None) -> int:
|
58
|
-
"""Execute an update query and return affected row count."""
|
59
|
-
if not self.engine:
|
60
|
-
self.connect()
|
61
|
-
|
62
|
-
try:
|
63
|
-
with self.engine.connect() as conn:
|
64
|
-
result = conn.execute(text(query), params or {})
|
65
|
-
conn.commit()
|
66
|
-
return result.rowcount
|
67
|
-
except SQLAlchemyError as e:
|
68
|
-
raise RuntimeError(f"MySQL update failed: {e}")
|
69
|
-
|
70
|
-
def get_tables(self) -> List[str]:
|
71
|
-
"""Get list of tables in the database."""
|
72
|
-
query = "SHOW TABLES"
|
73
|
-
result = self.execute_query(query)
|
74
|
-
return [list(row.values())[0] for row in result]
|
75
|
-
|
76
|
-
def get_table_schema(self, table_name: str) -> Dict[str, Any]:
|
77
|
-
"""Get schema information for a specific table."""
|
78
|
-
query = f"DESCRIBE {table_name}"
|
79
|
-
columns = self.execute_query(query)
|
80
|
-
|
81
|
-
schema = {
|
82
|
-
'table_name': table_name,
|
83
|
-
'columns': []
|
84
|
-
}
|
85
|
-
|
86
|
-
for col in columns:
|
87
|
-
schema['columns'].append({
|
88
|
-
'name': col['Field'],
|
89
|
-
'type': col['Type'],
|
90
|
-
'nullable': col['Null'] == 'YES',
|
91
|
-
'default': col['Default'],
|
92
|
-
'primary_key': col['Key'] == 'PRI'
|
93
|
-
})
|
94
|
-
|
95
|
-
return schema
|
96
|
-
|
97
|
-
def get_indexes(self, table_name: str) -> List[Dict[str, Any]]:
|
98
|
-
"""Get index information for a table."""
|
99
|
-
query = f"SHOW INDEX FROM {table_name}"
|
100
|
-
indexes = self.execute_query(query)
|
101
|
-
|
102
|
-
result = []
|
103
|
-
for idx in indexes:
|
104
|
-
result.append({
|
105
|
-
'name': idx['Key_name'],
|
106
|
-
'column': idx['Column_name'],
|
107
|
-
'unique': not idx['Non_unique'],
|
108
|
-
'type': idx['Index_type']
|
109
|
-
})
|
110
|
-
|
111
|
-
return result
|
112
|
-
|
113
|
-
def get_foreign_keys(self, table_name: str) -> List[Dict[str, Any]]:
|
114
|
-
"""Get foreign key information for a table."""
|
115
|
-
query = f"""
|
116
|
-
SELECT
|
117
|
-
CONSTRAINT_NAME as name,
|
118
|
-
COLUMN_NAME as column_name,
|
119
|
-
REFERENCED_TABLE_NAME as referenced_table,
|
120
|
-
REFERENCED_COLUMN_NAME as referenced_column
|
121
|
-
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
122
|
-
WHERE TABLE_NAME = '{table_name}'
|
123
|
-
AND REFERENCED_TABLE_NAME IS NOT NULL
|
124
|
-
"""
|
125
|
-
|
126
|
-
return self.execute_query(query)
|
127
|
-
|
128
|
-
def create_table(self, table_name: str, schema: Dict[str, Any]) -> None:
|
129
|
-
"""Create a new table with the given schema."""
|
130
|
-
columns = []
|
131
|
-
for col in schema.get('columns', []):
|
132
|
-
col_def = f"{col['name']} {col['type']}"
|
133
|
-
if not col.get('nullable', True):
|
134
|
-
col_def += " NOT NULL"
|
135
|
-
if col.get('default') is not None:
|
136
|
-
col_def += f" DEFAULT {col['default']}"
|
137
|
-
if col.get('primary_key'):
|
138
|
-
col_def += " PRIMARY KEY"
|
139
|
-
columns.append(col_def)
|
140
|
-
|
141
|
-
query = f"CREATE TABLE {table_name} ({', '.join(columns)})"
|
142
|
-
self.execute_update(query)
|
143
|
-
|
144
|
-
def drop_table(self, table_name: str) -> None:
|
145
|
-
"""Drop a table."""
|
146
|
-
query = f"DROP TABLE IF EXISTS {table_name}"
|
147
|
-
self.execute_update(query)
|
148
|
-
|
149
|
-
def table_exists(self, table_name: str) -> bool:
|
150
|
-
"""Check if a table exists."""
|
151
|
-
query = f"""
|
152
|
-
SELECT COUNT(*) as count
|
153
|
-
FROM INFORMATION_SCHEMA.TABLES
|
154
|
-
WHERE TABLE_NAME = '{table_name}'
|
155
|
-
"""
|
156
|
-
result = self.execute_query(query)
|
157
|
-
return result[0]['count'] > 0
|
158
|
-
|
159
|
-
def get_connection_info(self) -> Dict[str, Any]:
|
160
|
-
"""Get connection information."""
|
161
|
-
return {
|
162
|
-
'type': 'mysql',
|
163
|
-
'connection_string': self.connection_string,
|
164
|
-
'connected': self.engine is not None
|
165
|
-
}
|