SQLPyHelper 0.1.8__py3-none-any.whl → 0.2.0__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.
- sqlpyhelper/__init__.py +1 -1
- sqlpyhelper/async_helper.py +0 -1
- sqlpyhelper/db_helper.py +10 -5
- {sqlpyhelper-0.1.8.dist-info → sqlpyhelper-0.2.0.dist-info}/METADATA +52 -18
- sqlpyhelper-0.2.0.dist-info/RECORD +13 -0
- sqlpyhelper-0.1.8.dist-info/RECORD +0 -13
- {sqlpyhelper-0.1.8.dist-info → sqlpyhelper-0.2.0.dist-info}/WHEEL +0 -0
- {sqlpyhelper-0.1.8.dist-info → sqlpyhelper-0.2.0.dist-info}/entry_points.txt +0 -0
- {sqlpyhelper-0.1.8.dist-info → sqlpyhelper-0.2.0.dist-info}/licenses/LICENSE +0 -0
- {sqlpyhelper-0.1.8.dist-info → sqlpyhelper-0.2.0.dist-info}/top_level.txt +0 -0
sqlpyhelper/__init__.py
CHANGED
sqlpyhelper/async_helper.py
CHANGED
|
@@ -105,7 +105,6 @@ class AsyncSQLPyHelper:
|
|
|
105
105
|
import aiosqlite
|
|
106
106
|
|
|
107
107
|
self._connection = await aiosqlite.connect(self.database or "") # type: ignore[arg-type]
|
|
108
|
-
self._connection.row_factory = aiosqlite.Row
|
|
109
108
|
logger.info("Connected to SQLite database: %s", self.database)
|
|
110
109
|
|
|
111
110
|
elif self.db_type == "postgres":
|
sqlpyhelper/db_helper.py
CHANGED
|
@@ -80,6 +80,7 @@ class SQLPyHelper:
|
|
|
80
80
|
self.port: Optional[str] = port or os.getenv("DB_PORT")
|
|
81
81
|
self.oracle_sid: Optional[str] = oracle_sid or os.getenv("ORACLE_SID")
|
|
82
82
|
self.pool: Any = None
|
|
83
|
+
self._in_transaction: bool = False
|
|
83
84
|
|
|
84
85
|
if not self.db_type or not self.database:
|
|
85
86
|
raise ValueError("Missing required database configuration.")
|
|
@@ -96,6 +97,7 @@ class SQLPyHelper:
|
|
|
96
97
|
user=self.user,
|
|
97
98
|
password=self.password,
|
|
98
99
|
dbname=self.database,
|
|
100
|
+
port=self.port,
|
|
99
101
|
)
|
|
100
102
|
elif self.db_type == "mysql":
|
|
101
103
|
import mysql.connector
|
|
@@ -142,14 +144,14 @@ class SQLPyHelper:
|
|
|
142
144
|
self.cursor.execute(query, params)
|
|
143
145
|
else:
|
|
144
146
|
self.cursor.execute(query)
|
|
145
|
-
self.
|
|
147
|
+
if not self._in_transaction:
|
|
148
|
+
self.connection.commit()
|
|
146
149
|
except Exception as e:
|
|
147
|
-
if "server has gone away" in str(
|
|
148
|
-
e
|
|
149
|
-
): # Example check for MySQL lost connection
|
|
150
|
+
if "server has gone away" in str(e):
|
|
150
151
|
self.reconnect()
|
|
151
152
|
self.cursor.execute(query, params) # type: ignore[arg-type]
|
|
152
|
-
self.
|
|
153
|
+
if not self._in_transaction:
|
|
154
|
+
self.connection.commit()
|
|
153
155
|
else:
|
|
154
156
|
raise QueryError(f"Query failed: {e}") from e
|
|
155
157
|
|
|
@@ -349,6 +351,7 @@ class SQLPyHelper:
|
|
|
349
351
|
self.execute_query("BEGIN TRANSACTION")
|
|
350
352
|
elif self.db_type == "oracle":
|
|
351
353
|
pass # Oracle starts transactions implicitly on first DML statement
|
|
354
|
+
self._in_transaction = True
|
|
352
355
|
logger.info("Transaction started on %s database", self.db_type)
|
|
353
356
|
except Exception as e:
|
|
354
357
|
raise QueryError(f"Failed to begin transaction: {e}") from e
|
|
@@ -357,6 +360,7 @@ class SQLPyHelper:
|
|
|
357
360
|
"""Commit the current transaction."""
|
|
358
361
|
try:
|
|
359
362
|
self.connection.commit()
|
|
363
|
+
self._in_transaction = False
|
|
360
364
|
logger.info("Transaction committed on %s database", self.db_type)
|
|
361
365
|
except Exception as e:
|
|
362
366
|
raise QueryError(f"Failed to commit transaction: {e}") from e
|
|
@@ -365,6 +369,7 @@ class SQLPyHelper:
|
|
|
365
369
|
"""Roll back the current transaction."""
|
|
366
370
|
try:
|
|
367
371
|
self.connection.rollback()
|
|
372
|
+
self._in_transaction = False
|
|
368
373
|
logger.info("Transaction rolled back on %s database", self.db_type)
|
|
369
374
|
except Exception as e:
|
|
370
375
|
raise QueryError(f"Failed to rollback transaction: {e}") from e
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: SQLPyHelper
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: A simple SQL database helper package for Python.
|
|
5
5
|
Home-page: https://github.com/adebayopeter/sqlpyhelper
|
|
6
6
|
Author: Adebayo Olaonipekun
|
|
@@ -101,6 +101,7 @@ with SQLPyHelper(db_type="postgres", host="localhost", user="user",
|
|
|
101
101
|
- [MySQL Example](#mysql-example)
|
|
102
102
|
- [SQL Server Example](#sql-server-example)
|
|
103
103
|
- [Oracle Example](#oracle-example)
|
|
104
|
+
- [Async Example (FastAPI / asyncio)](#async-example-fastapi--asyncio)
|
|
104
105
|
- [📂 Project Structure](#-project-structure)
|
|
105
106
|
- [📌 Available Methods in SQLPyHelper](#-available-methods-in-sqlpyhelper)
|
|
106
107
|
- [🌍 Contributing](#-contributing)
|
|
@@ -108,14 +109,16 @@ with SQLPyHelper(db_type="postgres", host="localhost", user="user",
|
|
|
108
109
|
|
|
109
110
|
---
|
|
110
111
|
|
|
111
|
-
## 🚀 Features in v0.1.
|
|
112
|
-
- Unified connection pooling for multiple databases.
|
|
113
|
-
- Automatic reconnection for lost connections.
|
|
114
|
-
- Transaction support (BEGIN, ROLLBACK, COMMIT).
|
|
115
|
-
- Secure parameterized queries to prevent SQL injection.
|
|
116
|
-
- Bulk insertion & dynamic table creation.
|
|
117
|
-
- Logging & error handling for better debugging.
|
|
112
|
+
## 🚀 Features in v0.1.8
|
|
113
|
+
- Unified connection pooling for multiple databases.
|
|
114
|
+
- Automatic reconnection for lost connections.
|
|
115
|
+
- Transaction support (BEGIN, ROLLBACK, COMMIT).
|
|
116
|
+
- Secure parameterized queries to prevent SQL injection.
|
|
117
|
+
- Bulk insertion & dynamic table creation.
|
|
118
|
+
- Logging & error handling for better debugging.
|
|
118
119
|
- CSV export & database backups.
|
|
120
|
+
- **Cross-database migration** — copy tables between any two supported databases.
|
|
121
|
+
- **Async support** — `AsyncSQLPyHelper` for FastAPI and asyncio applications.
|
|
119
122
|
|
|
120
123
|
---
|
|
121
124
|
## 📦 Installation
|
|
@@ -214,20 +217,50 @@ db.setup_connection_pool(min_conn=2, max_conn=10) # Enable pooling for better p
|
|
|
214
217
|
conn = db.get_connection_from_pool()
|
|
215
218
|
db.return_connection_to_pool(conn)
|
|
216
219
|
```
|
|
220
|
+
### Async Example (FastAPI / asyncio)
|
|
221
|
+
```python
|
|
222
|
+
import asyncio
|
|
223
|
+
from sqlpyhelper.async_helper import AsyncSQLPyHelper
|
|
224
|
+
|
|
225
|
+
async def main():
|
|
226
|
+
async with AsyncSQLPyHelper(db_type="sqlite", database="my.db") as db:
|
|
227
|
+
await db.execute(
|
|
228
|
+
"CREATE TABLE IF NOT EXISTS users (id INTEGER, name TEXT)"
|
|
229
|
+
)
|
|
230
|
+
await db.execute(
|
|
231
|
+
"INSERT INTO users VALUES ($1, $2)", 1, "Alice"
|
|
232
|
+
)
|
|
233
|
+
rows = await db.fetch_all("SELECT * FROM users")
|
|
234
|
+
print(rows)
|
|
235
|
+
|
|
236
|
+
asyncio.run(main())
|
|
237
|
+
```
|
|
217
238
|
|
|
218
239
|
## 📂 Project Structure
|
|
219
240
|
```
|
|
220
241
|
📦 SQLPyHelper/
|
|
221
|
-
├─
|
|
222
|
-
│
|
|
223
|
-
│
|
|
224
|
-
├─
|
|
225
|
-
│
|
|
226
|
-
├─
|
|
227
|
-
|
|
228
|
-
├─
|
|
229
|
-
├─
|
|
230
|
-
|
|
242
|
+
├─ sqlpyhelper/
|
|
243
|
+
│ ├─ __init__.py
|
|
244
|
+
│ ├─ db_helper.py
|
|
245
|
+
│ ├─ async_helper.py
|
|
246
|
+
│ ├─ automation_utils.py
|
|
247
|
+
│ ├─ cli.py
|
|
248
|
+
│ └─ migration.py
|
|
249
|
+
├─ test/
|
|
250
|
+
│ ├─ test_sqlpyhelper.py
|
|
251
|
+
│ ├─ test_async_helper.py
|
|
252
|
+
│ └─ test_migration.py
|
|
253
|
+
├─ docs/
|
|
254
|
+
├─ .env_example
|
|
255
|
+
├─ .gitignore
|
|
256
|
+
├─ setup.py
|
|
257
|
+
├─ setup.cfg
|
|
258
|
+
├─ pyproject.toml
|
|
259
|
+
├─ CHANGELOG.md
|
|
260
|
+
├─ CONTRIBUTING.md
|
|
261
|
+
├─ pre-commit.sh
|
|
262
|
+
├─ README.md
|
|
263
|
+
└─ requirements.txt
|
|
231
264
|
```
|
|
232
265
|
---
|
|
233
266
|
## 📌 Available Methods in SQLPyHelper
|
|
@@ -249,6 +282,7 @@ db.return_connection_to_pool(conn)
|
|
|
249
282
|
| `commit_transaction()` | Commits the current transaction. |
|
|
250
283
|
| `close()` | Closes the database connection safely. |
|
|
251
284
|
| `__enter__` / `__exit__()` | Use as a context manager — connection closes automatically. |
|
|
285
|
+
| `AsyncSQLPyHelper` | Async-native class for FastAPI/asyncio — see [Async docs](https://sqlpyhelper.readthedocs.io/en/latest/async.html). |
|
|
252
286
|
|
|
253
287
|
---
|
|
254
288
|
## 🌍 Contributing
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
sqlpyhelper/__init__.py,sha256=zxVPeEAfG2pTAxvjU6D_U6x4ef4YMg4NqNFr-6SoSK0,370
|
|
2
|
+
sqlpyhelper/async_helper.py,sha256=beq0wKxDl7Qv-CW_qA1-h_DPhDpC1CQHdstnKO-4FSI,21473
|
|
3
|
+
sqlpyhelper/automation_utils.py,sha256=pC6pH6bJ-k8iPVeHJ4gUiwEe822dasmKg53ya9bMxyE,5381
|
|
4
|
+
sqlpyhelper/cli.py,sha256=yj0kWJu3oh_JLnmi0L7a5ing2_0x4CQGOKSOhZLAtoY,5646
|
|
5
|
+
sqlpyhelper/db_helper.py,sha256=zRZGidneQspAhYekD7ACvn9nim3ylYav_ObbCLxRXaM,14409
|
|
6
|
+
sqlpyhelper/migration.py,sha256=byAn7ToVgIB8tl1N39DB0MbHigjH2l-qX7QSskgzzTg,11673
|
|
7
|
+
sqlpyhelper/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
sqlpyhelper-0.2.0.dist-info/licenses/LICENSE,sha256=9XzXxZ_8mWFM9-2TlqyE3L69zvRf4VPY_xIzSj5iU-g,1076
|
|
9
|
+
sqlpyhelper-0.2.0.dist-info/METADATA,sha256=mI3df5g2wPddtecAYNcq95ipzwoXyHA15b2UhAuj300,11607
|
|
10
|
+
sqlpyhelper-0.2.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
11
|
+
sqlpyhelper-0.2.0.dist-info/entry_points.txt,sha256=uAzSqwkAbbJqQUKHlPNwOebTJVA0FqkOvn2CRP6xSz8,52
|
|
12
|
+
sqlpyhelper-0.2.0.dist-info/top_level.txt,sha256=FrLqTmqTGDa8jHnnf2ZVkYO-gFvLXX9QonpUCE6wKGs,12
|
|
13
|
+
sqlpyhelper-0.2.0.dist-info/RECORD,,
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
sqlpyhelper/__init__.py,sha256=UN7k9UyQVsxtFLDH496vDYQdGOIa525T4lGvVXjRM8s,370
|
|
2
|
-
sqlpyhelper/async_helper.py,sha256=_R01cUZSj_q9QH4pGT30gh56GJGSfgi0hIhgDJqmyYw,21534
|
|
3
|
-
sqlpyhelper/automation_utils.py,sha256=pC6pH6bJ-k8iPVeHJ4gUiwEe822dasmKg53ya9bMxyE,5381
|
|
4
|
-
sqlpyhelper/cli.py,sha256=yj0kWJu3oh_JLnmi0L7a5ing2_0x4CQGOKSOhZLAtoY,5646
|
|
5
|
-
sqlpyhelper/db_helper.py,sha256=4DbdBVo86zz1d0hNHtSc4b3Tks7bJGTMTyabsydQyOE,14191
|
|
6
|
-
sqlpyhelper/migration.py,sha256=byAn7ToVgIB8tl1N39DB0MbHigjH2l-qX7QSskgzzTg,11673
|
|
7
|
-
sqlpyhelper/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
-
sqlpyhelper-0.1.8.dist-info/licenses/LICENSE,sha256=9XzXxZ_8mWFM9-2TlqyE3L69zvRf4VPY_xIzSj5iU-g,1076
|
|
9
|
-
sqlpyhelper-0.1.8.dist-info/METADATA,sha256=l-kKKsEjezQU1GjTsXHFeRpDAjiPfK-5AIlepQ396Wk,10450
|
|
10
|
-
sqlpyhelper-0.1.8.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
11
|
-
sqlpyhelper-0.1.8.dist-info/entry_points.txt,sha256=uAzSqwkAbbJqQUKHlPNwOebTJVA0FqkOvn2CRP6xSz8,52
|
|
12
|
-
sqlpyhelper-0.1.8.dist-info/top_level.txt,sha256=FrLqTmqTGDa8jHnnf2ZVkYO-gFvLXX9QonpUCE6wKGs,12
|
|
13
|
-
sqlpyhelper-0.1.8.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|