SQLPyHelper 0.1.2__tar.gz → 0.1.3__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.
- {sqlpyhelper-0.1.2 → sqlpyhelper-0.1.3}/PKG-INFO +53 -48
- {sqlpyhelper-0.1.2 → sqlpyhelper-0.1.3}/README.md +52 -47
- {sqlpyhelper-0.1.2 → sqlpyhelper-0.1.3}/SQLPyHelper.egg-info/PKG-INFO +53 -48
- {sqlpyhelper-0.1.2 → sqlpyhelper-0.1.3}/setup.py +1 -1
- {sqlpyhelper-0.1.2 → sqlpyhelper-0.1.3}/sqlpyhelper/__init__.py +1 -1
- sqlpyhelper-0.1.3/sqlpyhelper/db_helper.py +209 -0
- sqlpyhelper-0.1.3/test/test_sqlpyhelper.py +86 -0
- sqlpyhelper-0.1.2/sqlpyhelper/db_helper.py +0 -135
- sqlpyhelper-0.1.2/test/test_sqlpyhelper.py +0 -71
- {sqlpyhelper-0.1.2 → sqlpyhelper-0.1.3}/LICENSE +0 -0
- {sqlpyhelper-0.1.2 → sqlpyhelper-0.1.3}/SQLPyHelper.egg-info/SOURCES.txt +0 -0
- {sqlpyhelper-0.1.2 → sqlpyhelper-0.1.3}/SQLPyHelper.egg-info/dependency_links.txt +0 -0
- {sqlpyhelper-0.1.2 → sqlpyhelper-0.1.3}/SQLPyHelper.egg-info/requires.txt +0 -0
- {sqlpyhelper-0.1.2 → sqlpyhelper-0.1.3}/SQLPyHelper.egg-info/top_level.txt +0 -0
- {sqlpyhelper-0.1.2 → sqlpyhelper-0.1.3}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: SQLPyHelper
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.3
|
|
4
4
|
Summary: A simple SQL database helper package for Python.
|
|
5
5
|
Author: Adebayo Olaonipekun
|
|
6
6
|
Author-email: pekunmi@live.com
|
|
@@ -38,9 +38,9 @@ Dynamic: requires-dist
|
|
|
38
38
|
Dynamic: requires-python
|
|
39
39
|
Dynamic: summary
|
|
40
40
|
|
|
41
|
-
# 📌 SQLPyHelper
|
|
41
|
+
# 📌 SQLPyHelper v.0.1.3 🚀
|
|
42
42
|
|
|
43
|
-
A Python library for simplified database interactions across **SQLite, PostgreSQL, MySQL, SQL Server, and Oracle**.
|
|
43
|
+
A Python library for simplified database interactions across **SQLite, PostgreSQL, MySQL, SQL Server, and Oracle**. SQLPyHelper provides an intuitive API for handling queries, connection pooling, transactions, logging, and backups efficiently.
|
|
44
44
|
|
|
45
45
|
## 📖 Table of Contents
|
|
46
46
|
- [🚀 Features](#-features)
|
|
@@ -53,17 +53,20 @@ A Python library for simplified database interactions across **SQLite, PostgreSQ
|
|
|
53
53
|
- [SQL Server Example](#sql-server-example)
|
|
54
54
|
- [Oracle Example](#oracle-example)
|
|
55
55
|
- [📂 Project Structure](#-project-structure)
|
|
56
|
+
- [📌 Available Methods in SQLPyHelper](#-available-methods-in-sqlpyhelper)
|
|
56
57
|
- [🌍 Contributing](#-contributing)
|
|
57
58
|
- [☕ Support the Project](#-support-the-project)
|
|
58
59
|
|
|
59
60
|
---
|
|
60
61
|
|
|
61
|
-
## 🚀 Features
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
62
|
+
## 🚀 Features in v0.1.3
|
|
63
|
+
✅ Unified connection pooling for multiple databases.
|
|
64
|
+
✅ Automatic reconnection for lost connections.
|
|
65
|
+
✅ Transaction support (BEGIN, ROLLBACK, COMMIT).
|
|
66
|
+
✅ Secure parameterized queries to prevent SQL injection.
|
|
67
|
+
✅ Bulk insertion & dynamic table creation.
|
|
68
|
+
✅ Logging & error handling for better debugging.
|
|
69
|
+
✅ CSV export & database backups.
|
|
67
70
|
|
|
68
71
|
---
|
|
69
72
|
## 📦 Installation
|
|
@@ -73,7 +76,7 @@ pip install sqlpyhelper
|
|
|
73
76
|
```
|
|
74
77
|
📌 Package on PyPI: [SQLPyHelper on PyPI](https://pypi.org/project/SQLPyHelper/)
|
|
75
78
|
|
|
76
|
-
|
|
79
|
+
For local development:
|
|
77
80
|
```sh
|
|
78
81
|
git clone https://github.com/adebayopeter/sqlpyhelper.git
|
|
79
82
|
cd sqlpyhelper
|
|
@@ -110,62 +113,46 @@ database = os.getenv("DB_NAME")
|
|
|
110
113
|
```
|
|
111
114
|
---
|
|
112
115
|
## 🛠 Usage Examples
|
|
113
|
-
###
|
|
116
|
+
### Initialize SQLPyHelper
|
|
114
117
|
```pycon
|
|
115
118
|
from sqlpyhelper.db_helper import SQLPyHelper
|
|
116
|
-
|
|
117
|
-
|
|
119
|
+
db = SQLPyHelper() # Auto-detects database type based on `DB_TYPE`
|
|
120
|
+
```
|
|
121
|
+
### SQLite Example
|
|
122
|
+
```pycon
|
|
118
123
|
db.execute_query("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
|
|
119
124
|
db.execute_query("INSERT INTO users (name) VALUES (?)", ("Alice",))
|
|
120
|
-
db.
|
|
121
|
-
print(db.fetch_all())
|
|
125
|
+
print(db.fetch_all()) # Expected Output: [(1, 'Alice')]
|
|
122
126
|
db.close()
|
|
123
127
|
```
|
|
124
128
|
### PostgreSQL Example
|
|
125
129
|
```pycon
|
|
126
|
-
db
|
|
127
|
-
db.execute_query("
|
|
128
|
-
db.
|
|
129
|
-
db.execute_query("
|
|
130
|
-
|
|
131
|
-
db.close()
|
|
130
|
+
db.execute_query("CREATE TABLE customers (id SERIAL PRIMARY KEY, name TEXT)")
|
|
131
|
+
db.execute_query("INSERT INTO customers (name) VALUES (%s)", ("Bob",))
|
|
132
|
+
db.begin_transaction()
|
|
133
|
+
db.execute_query("DELETE FROM customers WHERE name=%s", ("Bob",))
|
|
134
|
+
db.rollback_transaction() # Undo delete
|
|
132
135
|
```
|
|
133
136
|
### MySQL Example
|
|
134
|
-
```pycon
|
|
135
|
-
db
|
|
136
|
-
db.execute_query("
|
|
137
|
-
db.
|
|
138
|
-
db.execute_query("SELECT * FROM customers")
|
|
139
|
-
print(db.fetch_all())
|
|
140
|
-
db.close()
|
|
141
|
-
```
|
|
142
|
-
```pycon
|
|
143
|
-
db = SQLPyHelper()
|
|
144
|
-
|
|
145
|
-
# Fetch rows where customer_id = 3
|
|
146
|
-
customers = db.fetch_by_param("customers", "id", 3)
|
|
147
|
-
print(customers)
|
|
148
|
-
|
|
137
|
+
```pycon
|
|
138
|
+
db.execute_query("CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(100))")
|
|
139
|
+
db.execute_query("INSERT INTO users (id, name) VALUES (%s, %s)", (1, "Alice"))
|
|
140
|
+
print(db.fetch_by_param("users", "id", 1)) # Expected Output: [(1, 'Alice')]
|
|
149
141
|
db.close()
|
|
150
142
|
```
|
|
151
|
-
|
|
152
143
|
### SQL Server Example
|
|
153
144
|
```pycon
|
|
154
|
-
db
|
|
155
|
-
db.
|
|
156
|
-
db.
|
|
157
|
-
db.execute_query("SELECT * FROM orders")
|
|
158
|
-
print(db.fetch_all())
|
|
159
|
-
db.close()
|
|
145
|
+
db.execute_query("CREATE TABLE orders (order_id INT PRIMARY KEY, item NVARCHAR(100))")
|
|
146
|
+
db.insert_bulk("orders", [{"order_id": 1, "item": "Laptop"}, {"order_id": 2, "item": "Mouse"}])
|
|
147
|
+
db.backup_table("orders", "orders_backup.csv") # Export data to CSV
|
|
160
148
|
```
|
|
161
149
|
### Oracle Example
|
|
162
150
|
```pycon
|
|
163
|
-
db = SQLPyHelper()
|
|
164
151
|
db.execute_query("CREATE TABLE employees (id NUMBER PRIMARY KEY, name VARCHAR2(100))")
|
|
165
|
-
db.execute_query("INSERT INTO employees (id, name) VALUES (:1, :2)", (1, "
|
|
166
|
-
db.
|
|
167
|
-
|
|
168
|
-
db.
|
|
152
|
+
db.execute_query("INSERT INTO employees (id, name) VALUES (:1, :2)", (1, "Charlie"))
|
|
153
|
+
db.setup_connection_pool(min_conn=2, max_conn=10) # Enable pooling for better performance
|
|
154
|
+
conn = db.get_connection_from_pool()
|
|
155
|
+
db.return_connection_to_pool(conn)
|
|
169
156
|
```
|
|
170
157
|
|
|
171
158
|
## 📂 Project Structure
|
|
@@ -182,6 +169,24 @@ db.close()
|
|
|
182
169
|
├─ README.md
|
|
183
170
|
└─ requirements.txt
|
|
184
171
|
```
|
|
172
|
+
---
|
|
173
|
+
## 📌 Available Methods in SQLPyHelper
|
|
174
|
+
|
|
175
|
+
| Method | Description |
|
|
176
|
+
|--------|-------------|
|
|
177
|
+
| `execute_query(query, params=None)` | Executes a SQL query with optional parameters. |
|
|
178
|
+
| `fetch_one()` | Retrieves a **single row** from query results. |
|
|
179
|
+
| `fetch_all()` | Retrieves **all rows** from query results. |
|
|
180
|
+
| `fetch_by_param(table, column, value)` | Fetches **rows dynamically** based on a given parameter. |
|
|
181
|
+
| `create_table(table_name, columns_dict)` | Creates a table dynamically with a dictionary format. |
|
|
182
|
+
| `insert_bulk(table, data_list)` | Inserts **multiple rows at once** efficiently. |
|
|
183
|
+
| `backup_table(table, backup_file.csv)` | Exports table data to **CSV format**. |
|
|
184
|
+
| `setup_connection_pool()` | Initializes **database connection pooling**. |
|
|
185
|
+
| `get_connection_from_pool()` | Fetches a connection from the pool. |
|
|
186
|
+
| `return_connection_to_pool(conn)` | Returns connection back to pool. |
|
|
187
|
+
| `begin_transaction()` | Begins an **explicit transaction**. |
|
|
188
|
+
| `rollback_transaction()` | Rolls back **uncommitted transactions**. |
|
|
189
|
+
| `close()` | Closes the database connection safely. |
|
|
185
190
|
|
|
186
191
|
---
|
|
187
192
|
## 🌍 Contributing
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
# 📌 SQLPyHelper
|
|
1
|
+
# 📌 SQLPyHelper v.0.1.3 🚀
|
|
2
2
|
|
|
3
|
-
A Python library for simplified database interactions across **SQLite, PostgreSQL, MySQL, SQL Server, and Oracle**.
|
|
3
|
+
A Python library for simplified database interactions across **SQLite, PostgreSQL, MySQL, SQL Server, and Oracle**. SQLPyHelper provides an intuitive API for handling queries, connection pooling, transactions, logging, and backups efficiently.
|
|
4
4
|
|
|
5
5
|
## 📖 Table of Contents
|
|
6
6
|
- [🚀 Features](#-features)
|
|
@@ -13,17 +13,20 @@ A Python library for simplified database interactions across **SQLite, PostgreSQ
|
|
|
13
13
|
- [SQL Server Example](#sql-server-example)
|
|
14
14
|
- [Oracle Example](#oracle-example)
|
|
15
15
|
- [📂 Project Structure](#-project-structure)
|
|
16
|
+
- [📌 Available Methods in SQLPyHelper](#-available-methods-in-sqlpyhelper)
|
|
16
17
|
- [🌍 Contributing](#-contributing)
|
|
17
18
|
- [☕ Support the Project](#-support-the-project)
|
|
18
19
|
|
|
19
20
|
---
|
|
20
21
|
|
|
21
|
-
## 🚀 Features
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
## 🚀 Features in v0.1.3
|
|
23
|
+
✅ Unified connection pooling for multiple databases.
|
|
24
|
+
✅ Automatic reconnection for lost connections.
|
|
25
|
+
✅ Transaction support (BEGIN, ROLLBACK, COMMIT).
|
|
26
|
+
✅ Secure parameterized queries to prevent SQL injection.
|
|
27
|
+
✅ Bulk insertion & dynamic table creation.
|
|
28
|
+
✅ Logging & error handling for better debugging.
|
|
29
|
+
✅ CSV export & database backups.
|
|
27
30
|
|
|
28
31
|
---
|
|
29
32
|
## 📦 Installation
|
|
@@ -33,7 +36,7 @@ pip install sqlpyhelper
|
|
|
33
36
|
```
|
|
34
37
|
📌 Package on PyPI: [SQLPyHelper on PyPI](https://pypi.org/project/SQLPyHelper/)
|
|
35
38
|
|
|
36
|
-
|
|
39
|
+
For local development:
|
|
37
40
|
```sh
|
|
38
41
|
git clone https://github.com/adebayopeter/sqlpyhelper.git
|
|
39
42
|
cd sqlpyhelper
|
|
@@ -70,62 +73,46 @@ database = os.getenv("DB_NAME")
|
|
|
70
73
|
```
|
|
71
74
|
---
|
|
72
75
|
## 🛠 Usage Examples
|
|
73
|
-
###
|
|
76
|
+
### Initialize SQLPyHelper
|
|
74
77
|
```pycon
|
|
75
78
|
from sqlpyhelper.db_helper import SQLPyHelper
|
|
76
|
-
|
|
77
|
-
|
|
79
|
+
db = SQLPyHelper() # Auto-detects database type based on `DB_TYPE`
|
|
80
|
+
```
|
|
81
|
+
### SQLite Example
|
|
82
|
+
```pycon
|
|
78
83
|
db.execute_query("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
|
|
79
84
|
db.execute_query("INSERT INTO users (name) VALUES (?)", ("Alice",))
|
|
80
|
-
db.
|
|
81
|
-
print(db.fetch_all())
|
|
85
|
+
print(db.fetch_all()) # Expected Output: [(1, 'Alice')]
|
|
82
86
|
db.close()
|
|
83
87
|
```
|
|
84
88
|
### PostgreSQL Example
|
|
85
89
|
```pycon
|
|
86
|
-
db
|
|
87
|
-
db.execute_query("
|
|
88
|
-
db.
|
|
89
|
-
db.execute_query("
|
|
90
|
-
|
|
91
|
-
db.close()
|
|
90
|
+
db.execute_query("CREATE TABLE customers (id SERIAL PRIMARY KEY, name TEXT)")
|
|
91
|
+
db.execute_query("INSERT INTO customers (name) VALUES (%s)", ("Bob",))
|
|
92
|
+
db.begin_transaction()
|
|
93
|
+
db.execute_query("DELETE FROM customers WHERE name=%s", ("Bob",))
|
|
94
|
+
db.rollback_transaction() # Undo delete
|
|
92
95
|
```
|
|
93
96
|
### MySQL Example
|
|
94
|
-
```pycon
|
|
95
|
-
db
|
|
96
|
-
db.execute_query("
|
|
97
|
-
db.
|
|
98
|
-
db.execute_query("SELECT * FROM customers")
|
|
99
|
-
print(db.fetch_all())
|
|
100
|
-
db.close()
|
|
101
|
-
```
|
|
102
|
-
```pycon
|
|
103
|
-
db = SQLPyHelper()
|
|
104
|
-
|
|
105
|
-
# Fetch rows where customer_id = 3
|
|
106
|
-
customers = db.fetch_by_param("customers", "id", 3)
|
|
107
|
-
print(customers)
|
|
108
|
-
|
|
97
|
+
```pycon
|
|
98
|
+
db.execute_query("CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(100))")
|
|
99
|
+
db.execute_query("INSERT INTO users (id, name) VALUES (%s, %s)", (1, "Alice"))
|
|
100
|
+
print(db.fetch_by_param("users", "id", 1)) # Expected Output: [(1, 'Alice')]
|
|
109
101
|
db.close()
|
|
110
102
|
```
|
|
111
|
-
|
|
112
103
|
### SQL Server Example
|
|
113
104
|
```pycon
|
|
114
|
-
db
|
|
115
|
-
db.
|
|
116
|
-
db.
|
|
117
|
-
db.execute_query("SELECT * FROM orders")
|
|
118
|
-
print(db.fetch_all())
|
|
119
|
-
db.close()
|
|
105
|
+
db.execute_query("CREATE TABLE orders (order_id INT PRIMARY KEY, item NVARCHAR(100))")
|
|
106
|
+
db.insert_bulk("orders", [{"order_id": 1, "item": "Laptop"}, {"order_id": 2, "item": "Mouse"}])
|
|
107
|
+
db.backup_table("orders", "orders_backup.csv") # Export data to CSV
|
|
120
108
|
```
|
|
121
109
|
### Oracle Example
|
|
122
110
|
```pycon
|
|
123
|
-
db = SQLPyHelper()
|
|
124
111
|
db.execute_query("CREATE TABLE employees (id NUMBER PRIMARY KEY, name VARCHAR2(100))")
|
|
125
|
-
db.execute_query("INSERT INTO employees (id, name) VALUES (:1, :2)", (1, "
|
|
126
|
-
db.
|
|
127
|
-
|
|
128
|
-
db.
|
|
112
|
+
db.execute_query("INSERT INTO employees (id, name) VALUES (:1, :2)", (1, "Charlie"))
|
|
113
|
+
db.setup_connection_pool(min_conn=2, max_conn=10) # Enable pooling for better performance
|
|
114
|
+
conn = db.get_connection_from_pool()
|
|
115
|
+
db.return_connection_to_pool(conn)
|
|
129
116
|
```
|
|
130
117
|
|
|
131
118
|
## 📂 Project Structure
|
|
@@ -142,6 +129,24 @@ db.close()
|
|
|
142
129
|
├─ README.md
|
|
143
130
|
└─ requirements.txt
|
|
144
131
|
```
|
|
132
|
+
---
|
|
133
|
+
## 📌 Available Methods in SQLPyHelper
|
|
134
|
+
|
|
135
|
+
| Method | Description |
|
|
136
|
+
|--------|-------------|
|
|
137
|
+
| `execute_query(query, params=None)` | Executes a SQL query with optional parameters. |
|
|
138
|
+
| `fetch_one()` | Retrieves a **single row** from query results. |
|
|
139
|
+
| `fetch_all()` | Retrieves **all rows** from query results. |
|
|
140
|
+
| `fetch_by_param(table, column, value)` | Fetches **rows dynamically** based on a given parameter. |
|
|
141
|
+
| `create_table(table_name, columns_dict)` | Creates a table dynamically with a dictionary format. |
|
|
142
|
+
| `insert_bulk(table, data_list)` | Inserts **multiple rows at once** efficiently. |
|
|
143
|
+
| `backup_table(table, backup_file.csv)` | Exports table data to **CSV format**. |
|
|
144
|
+
| `setup_connection_pool()` | Initializes **database connection pooling**. |
|
|
145
|
+
| `get_connection_from_pool()` | Fetches a connection from the pool. |
|
|
146
|
+
| `return_connection_to_pool(conn)` | Returns connection back to pool. |
|
|
147
|
+
| `begin_transaction()` | Begins an **explicit transaction**. |
|
|
148
|
+
| `rollback_transaction()` | Rolls back **uncommitted transactions**. |
|
|
149
|
+
| `close()` | Closes the database connection safely. |
|
|
145
150
|
|
|
146
151
|
---
|
|
147
152
|
## 🌍 Contributing
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: SQLPyHelper
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.3
|
|
4
4
|
Summary: A simple SQL database helper package for Python.
|
|
5
5
|
Author: Adebayo Olaonipekun
|
|
6
6
|
Author-email: pekunmi@live.com
|
|
@@ -38,9 +38,9 @@ Dynamic: requires-dist
|
|
|
38
38
|
Dynamic: requires-python
|
|
39
39
|
Dynamic: summary
|
|
40
40
|
|
|
41
|
-
# 📌 SQLPyHelper
|
|
41
|
+
# 📌 SQLPyHelper v.0.1.3 🚀
|
|
42
42
|
|
|
43
|
-
A Python library for simplified database interactions across **SQLite, PostgreSQL, MySQL, SQL Server, and Oracle**.
|
|
43
|
+
A Python library for simplified database interactions across **SQLite, PostgreSQL, MySQL, SQL Server, and Oracle**. SQLPyHelper provides an intuitive API for handling queries, connection pooling, transactions, logging, and backups efficiently.
|
|
44
44
|
|
|
45
45
|
## 📖 Table of Contents
|
|
46
46
|
- [🚀 Features](#-features)
|
|
@@ -53,17 +53,20 @@ A Python library for simplified database interactions across **SQLite, PostgreSQ
|
|
|
53
53
|
- [SQL Server Example](#sql-server-example)
|
|
54
54
|
- [Oracle Example](#oracle-example)
|
|
55
55
|
- [📂 Project Structure](#-project-structure)
|
|
56
|
+
- [📌 Available Methods in SQLPyHelper](#-available-methods-in-sqlpyhelper)
|
|
56
57
|
- [🌍 Contributing](#-contributing)
|
|
57
58
|
- [☕ Support the Project](#-support-the-project)
|
|
58
59
|
|
|
59
60
|
---
|
|
60
61
|
|
|
61
|
-
## 🚀 Features
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
62
|
+
## 🚀 Features in v0.1.3
|
|
63
|
+
✅ Unified connection pooling for multiple databases.
|
|
64
|
+
✅ Automatic reconnection for lost connections.
|
|
65
|
+
✅ Transaction support (BEGIN, ROLLBACK, COMMIT).
|
|
66
|
+
✅ Secure parameterized queries to prevent SQL injection.
|
|
67
|
+
✅ Bulk insertion & dynamic table creation.
|
|
68
|
+
✅ Logging & error handling for better debugging.
|
|
69
|
+
✅ CSV export & database backups.
|
|
67
70
|
|
|
68
71
|
---
|
|
69
72
|
## 📦 Installation
|
|
@@ -73,7 +76,7 @@ pip install sqlpyhelper
|
|
|
73
76
|
```
|
|
74
77
|
📌 Package on PyPI: [SQLPyHelper on PyPI](https://pypi.org/project/SQLPyHelper/)
|
|
75
78
|
|
|
76
|
-
|
|
79
|
+
For local development:
|
|
77
80
|
```sh
|
|
78
81
|
git clone https://github.com/adebayopeter/sqlpyhelper.git
|
|
79
82
|
cd sqlpyhelper
|
|
@@ -110,62 +113,46 @@ database = os.getenv("DB_NAME")
|
|
|
110
113
|
```
|
|
111
114
|
---
|
|
112
115
|
## 🛠 Usage Examples
|
|
113
|
-
###
|
|
116
|
+
### Initialize SQLPyHelper
|
|
114
117
|
```pycon
|
|
115
118
|
from sqlpyhelper.db_helper import SQLPyHelper
|
|
116
|
-
|
|
117
|
-
|
|
119
|
+
db = SQLPyHelper() # Auto-detects database type based on `DB_TYPE`
|
|
120
|
+
```
|
|
121
|
+
### SQLite Example
|
|
122
|
+
```pycon
|
|
118
123
|
db.execute_query("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
|
|
119
124
|
db.execute_query("INSERT INTO users (name) VALUES (?)", ("Alice",))
|
|
120
|
-
db.
|
|
121
|
-
print(db.fetch_all())
|
|
125
|
+
print(db.fetch_all()) # Expected Output: [(1, 'Alice')]
|
|
122
126
|
db.close()
|
|
123
127
|
```
|
|
124
128
|
### PostgreSQL Example
|
|
125
129
|
```pycon
|
|
126
|
-
db
|
|
127
|
-
db.execute_query("
|
|
128
|
-
db.
|
|
129
|
-
db.execute_query("
|
|
130
|
-
|
|
131
|
-
db.close()
|
|
130
|
+
db.execute_query("CREATE TABLE customers (id SERIAL PRIMARY KEY, name TEXT)")
|
|
131
|
+
db.execute_query("INSERT INTO customers (name) VALUES (%s)", ("Bob",))
|
|
132
|
+
db.begin_transaction()
|
|
133
|
+
db.execute_query("DELETE FROM customers WHERE name=%s", ("Bob",))
|
|
134
|
+
db.rollback_transaction() # Undo delete
|
|
132
135
|
```
|
|
133
136
|
### MySQL Example
|
|
134
|
-
```pycon
|
|
135
|
-
db
|
|
136
|
-
db.execute_query("
|
|
137
|
-
db.
|
|
138
|
-
db.execute_query("SELECT * FROM customers")
|
|
139
|
-
print(db.fetch_all())
|
|
140
|
-
db.close()
|
|
141
|
-
```
|
|
142
|
-
```pycon
|
|
143
|
-
db = SQLPyHelper()
|
|
144
|
-
|
|
145
|
-
# Fetch rows where customer_id = 3
|
|
146
|
-
customers = db.fetch_by_param("customers", "id", 3)
|
|
147
|
-
print(customers)
|
|
148
|
-
|
|
137
|
+
```pycon
|
|
138
|
+
db.execute_query("CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(100))")
|
|
139
|
+
db.execute_query("INSERT INTO users (id, name) VALUES (%s, %s)", (1, "Alice"))
|
|
140
|
+
print(db.fetch_by_param("users", "id", 1)) # Expected Output: [(1, 'Alice')]
|
|
149
141
|
db.close()
|
|
150
142
|
```
|
|
151
|
-
|
|
152
143
|
### SQL Server Example
|
|
153
144
|
```pycon
|
|
154
|
-
db
|
|
155
|
-
db.
|
|
156
|
-
db.
|
|
157
|
-
db.execute_query("SELECT * FROM orders")
|
|
158
|
-
print(db.fetch_all())
|
|
159
|
-
db.close()
|
|
145
|
+
db.execute_query("CREATE TABLE orders (order_id INT PRIMARY KEY, item NVARCHAR(100))")
|
|
146
|
+
db.insert_bulk("orders", [{"order_id": 1, "item": "Laptop"}, {"order_id": 2, "item": "Mouse"}])
|
|
147
|
+
db.backup_table("orders", "orders_backup.csv") # Export data to CSV
|
|
160
148
|
```
|
|
161
149
|
### Oracle Example
|
|
162
150
|
```pycon
|
|
163
|
-
db = SQLPyHelper()
|
|
164
151
|
db.execute_query("CREATE TABLE employees (id NUMBER PRIMARY KEY, name VARCHAR2(100))")
|
|
165
|
-
db.execute_query("INSERT INTO employees (id, name) VALUES (:1, :2)", (1, "
|
|
166
|
-
db.
|
|
167
|
-
|
|
168
|
-
db.
|
|
152
|
+
db.execute_query("INSERT INTO employees (id, name) VALUES (:1, :2)", (1, "Charlie"))
|
|
153
|
+
db.setup_connection_pool(min_conn=2, max_conn=10) # Enable pooling for better performance
|
|
154
|
+
conn = db.get_connection_from_pool()
|
|
155
|
+
db.return_connection_to_pool(conn)
|
|
169
156
|
```
|
|
170
157
|
|
|
171
158
|
## 📂 Project Structure
|
|
@@ -182,6 +169,24 @@ db.close()
|
|
|
182
169
|
├─ README.md
|
|
183
170
|
└─ requirements.txt
|
|
184
171
|
```
|
|
172
|
+
---
|
|
173
|
+
## 📌 Available Methods in SQLPyHelper
|
|
174
|
+
|
|
175
|
+
| Method | Description |
|
|
176
|
+
|--------|-------------|
|
|
177
|
+
| `execute_query(query, params=None)` | Executes a SQL query with optional parameters. |
|
|
178
|
+
| `fetch_one()` | Retrieves a **single row** from query results. |
|
|
179
|
+
| `fetch_all()` | Retrieves **all rows** from query results. |
|
|
180
|
+
| `fetch_by_param(table, column, value)` | Fetches **rows dynamically** based on a given parameter. |
|
|
181
|
+
| `create_table(table_name, columns_dict)` | Creates a table dynamically with a dictionary format. |
|
|
182
|
+
| `insert_bulk(table, data_list)` | Inserts **multiple rows at once** efficiently. |
|
|
183
|
+
| `backup_table(table, backup_file.csv)` | Exports table data to **CSV format**. |
|
|
184
|
+
| `setup_connection_pool()` | Initializes **database connection pooling**. |
|
|
185
|
+
| `get_connection_from_pool()` | Fetches a connection from the pool. |
|
|
186
|
+
| `return_connection_to_pool(conn)` | Returns connection back to pool. |
|
|
187
|
+
| `begin_transaction()` | Begins an **explicit transaction**. |
|
|
188
|
+
| `rollback_transaction()` | Rolls back **uncommitted transactions**. |
|
|
189
|
+
| `close()` | Closes the database connection safely. |
|
|
185
190
|
|
|
186
191
|
---
|
|
187
192
|
## 🌍 Contributing
|
|
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as f:
|
|
|
5
5
|
|
|
6
6
|
setup(
|
|
7
7
|
name='SQLPyHelper',
|
|
8
|
-
version='0.1.
|
|
8
|
+
version='0.1.3',
|
|
9
9
|
description='A simple SQL database helper package for Python.',
|
|
10
10
|
long_description=long_description,
|
|
11
11
|
long_description_content_type="text/markdown",
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
# Match the version in setup.py
|
|
2
|
-
__version__ = "0.1.
|
|
2
|
+
__version__ = "0.1.3"
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import csv
|
|
2
|
+
from dotenv import load_dotenv
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
load_dotenv() # Load environment variables from .env file
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def log_query(query):
|
|
9
|
+
"""Logs queries for debugging purposes."""
|
|
10
|
+
with open("query_log.txt", "a") as f:
|
|
11
|
+
f.write(query + "\n")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class SQLPyHelper:
|
|
15
|
+
def __init__(self):
|
|
16
|
+
self.db_type = os.getenv("DB_TYPE").lower()
|
|
17
|
+
self.host = os.getenv("DB_HOST")
|
|
18
|
+
self.user = os.getenv("DB_USER")
|
|
19
|
+
self.password = os.getenv("DB_PASSWORD")
|
|
20
|
+
self.database = os.getenv("DB_NAME")
|
|
21
|
+
self.driver = os.getenv("DB_DRIVER")
|
|
22
|
+
self.oracle_sid = os.getenv("ORACLE_SID")
|
|
23
|
+
self.pool = None
|
|
24
|
+
|
|
25
|
+
if self.db_type == "sqlite":
|
|
26
|
+
import sqlite3
|
|
27
|
+
self.connection = sqlite3.connect(self.database)
|
|
28
|
+
elif self.db_type == "postgres":
|
|
29
|
+
import psycopg2
|
|
30
|
+
self.connection = psycopg2.connect(host=self.host, user=self.user,
|
|
31
|
+
password=self.password, dbname=self.database)
|
|
32
|
+
elif self.db_type == "mysql":
|
|
33
|
+
import mysql.connector
|
|
34
|
+
self.connection = mysql.connector.connect(host=self.host, user=self.user,
|
|
35
|
+
password=self.password, database=self.database)
|
|
36
|
+
elif self.db_type == "sqlserver":
|
|
37
|
+
import pyodbc
|
|
38
|
+
self.connection = pyodbc.connect(f"DRIVER={self.driver};SERVER={self.host};DATABASE={self.database};"
|
|
39
|
+
f"UID={self.user};PWD={self.password}")
|
|
40
|
+
elif self.db_type == "oracle":
|
|
41
|
+
import cx_Oracle
|
|
42
|
+
oracle_port = os.getenv("ORACLE_DB_PORT", "1521") # Default to 1521 if not set
|
|
43
|
+
dsn = cx_Oracle.makedsn(self.host, oracle_port, self.oracle_sid)
|
|
44
|
+
self.connection = cx_Oracle.connect(self.user, self.password, dsn)
|
|
45
|
+
else:
|
|
46
|
+
raise ValueError("Unsupported database type")
|
|
47
|
+
|
|
48
|
+
self.cursor = self.connection.cursor()
|
|
49
|
+
|
|
50
|
+
def execute_query(self, query, params=None):
|
|
51
|
+
"""Executes a query with optional parameters"""
|
|
52
|
+
try:
|
|
53
|
+
if params:
|
|
54
|
+
self.cursor.execute(query, params)
|
|
55
|
+
else:
|
|
56
|
+
self.cursor.execute(query)
|
|
57
|
+
self.connection.commit()
|
|
58
|
+
except Exception as e:
|
|
59
|
+
if "server has gone away" in str(e): # Example check for MySQL lost connection
|
|
60
|
+
self.reconnect()
|
|
61
|
+
self.cursor.execute(query, params)
|
|
62
|
+
self.connection.commit()
|
|
63
|
+
else:
|
|
64
|
+
print(f"Error executing query: {e}")
|
|
65
|
+
|
|
66
|
+
def fetch_one(self):
|
|
67
|
+
"""Fetches a single row"""
|
|
68
|
+
try:
|
|
69
|
+
return self.cursor.fetchone()
|
|
70
|
+
except Exception as e:
|
|
71
|
+
print(f"Error fetching row: {e}")
|
|
72
|
+
return None
|
|
73
|
+
|
|
74
|
+
def fetch_all(self):
|
|
75
|
+
"""Fetches all rows from the last executed query"""
|
|
76
|
+
try:
|
|
77
|
+
return self.cursor.fetchall()
|
|
78
|
+
except Exception as e:
|
|
79
|
+
print(f"Error fetching rows: {e}")
|
|
80
|
+
return None
|
|
81
|
+
|
|
82
|
+
def fetch_by_param(self, table_name, column_name, value):
|
|
83
|
+
try:
|
|
84
|
+
query = f"SELECT * FROM {table_name} WHERE {column_name} = %s"
|
|
85
|
+
self.cursor.execute(query, (value,))
|
|
86
|
+
return self.cursor.fetchall()
|
|
87
|
+
except Exception as e:
|
|
88
|
+
print(f"Error fetching row(s): {e}")
|
|
89
|
+
return None
|
|
90
|
+
|
|
91
|
+
def close(self):
|
|
92
|
+
"""Closes the connection"""
|
|
93
|
+
try:
|
|
94
|
+
self.cursor.close()
|
|
95
|
+
self.connection.close()
|
|
96
|
+
except Exception as e:
|
|
97
|
+
print(f"Error closing connection: {e}")
|
|
98
|
+
return None
|
|
99
|
+
|
|
100
|
+
def create_table(self, table_name, columns):
|
|
101
|
+
"""
|
|
102
|
+
Creates a table dynamically using a dictionary format.
|
|
103
|
+
Example:
|
|
104
|
+
columns = {'id': 'INTEGER PRIMARY KEY', 'name': 'TEXT', 'age': 'INTEGER'}
|
|
105
|
+
"""
|
|
106
|
+
try:
|
|
107
|
+
column_defs = ", ".join(f"{col} {dtype}" for col, dtype in columns.items())
|
|
108
|
+
query = f"CREATE TABLE {table_name} ({column_defs})"
|
|
109
|
+
self.execute_query(query)
|
|
110
|
+
except Exception as e:
|
|
111
|
+
print(f"Error creating table: {e}")
|
|
112
|
+
return None
|
|
113
|
+
|
|
114
|
+
def insert_bulk(self, table_name, data):
|
|
115
|
+
"""
|
|
116
|
+
Inserts multiple rows at once.
|
|
117
|
+
Example:
|
|
118
|
+
data = [{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}]
|
|
119
|
+
"""
|
|
120
|
+
try:
|
|
121
|
+
columns = ", ".join(data[0].keys()) # Extract column names
|
|
122
|
+
placeholders = ", ".join(["%s" for _ in data[0].keys()]) # Generate placeholders
|
|
123
|
+
query = f"INSERT INTO {table_name} ({columns}) VALUES ({placeholders})"
|
|
124
|
+
values = [tuple(row.values()) for row in data]
|
|
125
|
+
self.cursor.executemany(query, values)
|
|
126
|
+
self.connection.commit()
|
|
127
|
+
|
|
128
|
+
except Exception as e:
|
|
129
|
+
print(f"Error inserting bulk rows: {e}")
|
|
130
|
+
return None
|
|
131
|
+
|
|
132
|
+
def backup_table(self, table_name, backup_file):
|
|
133
|
+
"""
|
|
134
|
+
Exports table data into a CSV file.
|
|
135
|
+
Example:
|
|
136
|
+
backup_table('users', 'users_backup.csv')
|
|
137
|
+
"""
|
|
138
|
+
try:
|
|
139
|
+
query = f"SELECT * FROM {table_name}"
|
|
140
|
+
self.execute_query(query)
|
|
141
|
+
rows = self.fetch_all()
|
|
142
|
+
|
|
143
|
+
with open(backup_file, mode="w", newline="") as file:
|
|
144
|
+
writer = csv.writer(file)
|
|
145
|
+
writer.writerow([desc[0] for desc in self.cursor.description]) # Column headers
|
|
146
|
+
writer.writerows(rows)
|
|
147
|
+
except Exception as e:
|
|
148
|
+
print(f"Error backing up table: {e}")
|
|
149
|
+
return None
|
|
150
|
+
|
|
151
|
+
def setup_connection_pool(self, min_conn=1, max_conn=5, pool_size=5):
|
|
152
|
+
"""Sets up connection pooling based on the database type"""
|
|
153
|
+
try:
|
|
154
|
+
if self.db_type == "postgres":
|
|
155
|
+
from psycopg2 import pool
|
|
156
|
+
self.pool = pool.SimpleConnectionPool(min_conn, max_conn,
|
|
157
|
+
host=self.host, user=self.user,
|
|
158
|
+
password=self.password, dbname=self.database)
|
|
159
|
+
|
|
160
|
+
elif self.db_type == "mysql":
|
|
161
|
+
import mysql.connector.pooling
|
|
162
|
+
self.pool = mysql.connector.pooling.MySQLConnectionPool(pool_name="mypool",
|
|
163
|
+
pool_size=pool_size, host=self.host,
|
|
164
|
+
user=self.user, password=self.password,
|
|
165
|
+
database=self.database)
|
|
166
|
+
|
|
167
|
+
elif self.db_type == "sqlserver":
|
|
168
|
+
import pyodbc
|
|
169
|
+
self.pool = [
|
|
170
|
+
pyodbc.connect(f"DRIVER={self.driver};SERVER={self.host};DATABASE={self.database};"
|
|
171
|
+
f"UID={self.user};PWD={self.password};ConnectionPooling=Yes")
|
|
172
|
+
for _ in range(pool_size)
|
|
173
|
+
]
|
|
174
|
+
|
|
175
|
+
elif self.db_type == "oracle":
|
|
176
|
+
import cx_Oracle
|
|
177
|
+
oracle_port = os.getenv("ORACLE_DB_PORT", "1521") # Default Oracle port
|
|
178
|
+
dsn = cx_Oracle.makedsn(self.host, oracle_port, self.oracle_sid)
|
|
179
|
+
self.pool = cx_Oracle.SessionPool(user=self.user, password=self.password, dsn=dsn,
|
|
180
|
+
min=min_conn, max=max_conn, increment=1, threaded=True)
|
|
181
|
+
|
|
182
|
+
else:
|
|
183
|
+
raise ValueError(f"Connection pooling not supported for {self.db_type}")
|
|
184
|
+
except Exception as e:
|
|
185
|
+
print(f"⚠️ Error setting up connection pool: {e}")
|
|
186
|
+
self.pool = None # Prevent broken pool usage
|
|
187
|
+
|
|
188
|
+
def get_connection_from_pool(self):
|
|
189
|
+
"""Fetches a connection from the pool."""
|
|
190
|
+
return self.pool.get_connection()
|
|
191
|
+
|
|
192
|
+
def return_connection_to_pool(self):
|
|
193
|
+
"""Returns a connection back to the pool."""
|
|
194
|
+
self.connection.close()
|
|
195
|
+
|
|
196
|
+
def reconnect(self):
|
|
197
|
+
"""Reconnects to the database if connection is lost"""
|
|
198
|
+
try:
|
|
199
|
+
self.connection.close() # Close existing connection
|
|
200
|
+
self.__init__() # Reinitialize the connection
|
|
201
|
+
print("Database reconnected successfully.")
|
|
202
|
+
except Exception as e:
|
|
203
|
+
print(f"Error during reconnection: {e}")
|
|
204
|
+
|
|
205
|
+
def begin_transaction(self):
|
|
206
|
+
self.execute_query("START TRANSACTION")
|
|
207
|
+
|
|
208
|
+
def rollback_transaction(self):
|
|
209
|
+
self.execute_query("ROLLBACK")
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from sqlpyhelper.db_helper import SQLPyHelper
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# Connection test
|
|
7
|
+
@pytest.fixture
|
|
8
|
+
def db():
|
|
9
|
+
"""Fixture to initialize and return a database helper instance based on DB_TYPE."""
|
|
10
|
+
db_type = os.getenv("DB_TYPE", "mysql") # Default to MySQL if not set
|
|
11
|
+
os.environ["DB_TYPE"] = db_type # Ensure correct DB type is used
|
|
12
|
+
db_instance = SQLPyHelper()
|
|
13
|
+
yield db_instance
|
|
14
|
+
db_instance.close() # Cleanup after tests
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@pytest.mark.skipif(os.getenv("DB_TYPE") != "mysql", reason="Skipping non-MySQL tests")
|
|
18
|
+
def test_connection(db):
|
|
19
|
+
"""Ensure MySQL connects successfully."""
|
|
20
|
+
assert db.connection is not None, "MySQL connection failed!"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@pytest.mark.skipif(os.getenv("DB_TYPE") != "mysql", reason="Skipping non-MySQL tests")
|
|
24
|
+
def test_fetch_all(db):
|
|
25
|
+
"""Ensure fetch_all retrieves multiple rows in MySQL."""
|
|
26
|
+
db.execute_query("INSERT INTO customers (name) VALUES ('Sade'), ('Rita')")
|
|
27
|
+
db.execute_query("SELECT * FROM customers")
|
|
28
|
+
result = db.fetch_all()
|
|
29
|
+
assert len(result) >= 2, "fetch_all() failed to return expected results!"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
# Query Execution & Fetching
|
|
33
|
+
@pytest.mark.parametrize("db_type,query", [
|
|
34
|
+
("mysql", "CREATE TABLE test_user_tbl (id INT PRIMARY KEY, name VARCHAR(100))"),
|
|
35
|
+
# ("postgres", "CREATE TABLE test_user_tbl (id SERIAL PRIMARY KEY, name TEXT)"),
|
|
36
|
+
# ("sqlserver", "CREATE TABLE test_user_tbl (id INT PRIMARY KEY, name NVARCHAR(100))"),
|
|
37
|
+
# ("oracle", "CREATE TABLE test_user_tbl (id NUMBER PRIMARY KEY, name VARCHAR2(100))")
|
|
38
|
+
])
|
|
39
|
+
def test_query_execution(db_type, query):
|
|
40
|
+
"""Test table creation syntax across database variants."""
|
|
41
|
+
os.environ["DB_TYPE"] = db_type
|
|
42
|
+
db = SQLPyHelper()
|
|
43
|
+
db.execute_query(query)
|
|
44
|
+
assert True # If no errors, test passes
|
|
45
|
+
db.close()
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
# Parameterized Query Tests
|
|
49
|
+
# @pytest.mark.parametrize("db_type", ["mysql", "postgres", "sqlserver", "oracle"])
|
|
50
|
+
@pytest.mark.parametrize("db_type", ["mysql"])
|
|
51
|
+
def test_fetch_by_param(db_type):
|
|
52
|
+
"""Test parameterized queries for different databases."""
|
|
53
|
+
os.environ["DB_TYPE"] = db_type
|
|
54
|
+
db = SQLPyHelper()
|
|
55
|
+
|
|
56
|
+
result = db.fetch_by_param("customers", "name", "David")
|
|
57
|
+
assert len(result) >= 1, f"Failed to fetch record for {db_type}"
|
|
58
|
+
db.close()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
# Connection Pooling Validation
|
|
62
|
+
# @pytest.mark.parametrize("db_type", ["mysql", "postgres", "sqlserver", "oracle"])
|
|
63
|
+
@pytest.mark.parametrize("db_type", ["mysql"])
|
|
64
|
+
def test_connection_pooling(db_type):
|
|
65
|
+
"""Test pooling setup for different databases."""
|
|
66
|
+
os.environ["DB_TYPE"] = db_type
|
|
67
|
+
db = SQLPyHelper()
|
|
68
|
+
db.setup_connection_pool()
|
|
69
|
+
conn = db.get_connection_from_pool()
|
|
70
|
+
assert conn is not None, f"Pooling failed for {db_type}"
|
|
71
|
+
db.return_connection_to_pool()
|
|
72
|
+
db.close()
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
# Transaction Management
|
|
76
|
+
def test_transaction_rollback(db):
|
|
77
|
+
"""Verify rollback restores previous state."""
|
|
78
|
+
db.begin_transaction()
|
|
79
|
+
db.execute_query("INSERT INTO customers (name) VALUES ('Eve')")
|
|
80
|
+
db.rollback_transaction()
|
|
81
|
+
result = db.fetch_by_param("customers", "name", "Eve")
|
|
82
|
+
assert len(result) == 0, "Rollback did not revert changes!"
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
# pytest test_sqlpyhelper.py
|
|
86
|
+
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import csv
|
|
2
|
-
from dotenv import load_dotenv
|
|
3
|
-
import os
|
|
4
|
-
|
|
5
|
-
load_dotenv() # Load environment variables from .env file
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
def log_query(query):
|
|
9
|
-
"""Logs queries for debugging purposes."""
|
|
10
|
-
with open("query_log.txt", "a") as f:
|
|
11
|
-
f.write(query + "\n")
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
class SQLPyHelper:
|
|
15
|
-
def __init__(self):
|
|
16
|
-
self.db_type = os.getenv("DB_TYPE").lower()
|
|
17
|
-
self.host = os.getenv("DB_HOST")
|
|
18
|
-
self.user = os.getenv("DB_USER")
|
|
19
|
-
self.password = os.getenv("DB_PASSWORD")
|
|
20
|
-
self.database = os.getenv("DB_NAME")
|
|
21
|
-
self.driver = os.getenv("DB_DRIVER")
|
|
22
|
-
self.oracle_sid = os.getenv("ORACLE_SID")
|
|
23
|
-
self.pool = None
|
|
24
|
-
|
|
25
|
-
if self.db_type == "sqlite":
|
|
26
|
-
import sqlite3
|
|
27
|
-
self.connection = sqlite3.connect(self.database)
|
|
28
|
-
elif self.db_type == "postgres":
|
|
29
|
-
import psycopg2
|
|
30
|
-
self.connection = psycopg2.connect(host=self.host, user=self.user,
|
|
31
|
-
password=self.password, dbname=self.database)
|
|
32
|
-
elif self.db_type == "mysql":
|
|
33
|
-
import mysql.connector
|
|
34
|
-
self.connection = mysql.connector.connect(host=self.host, user=self.user,
|
|
35
|
-
password=self.password, database=self.database)
|
|
36
|
-
elif self.db_type == "sqlserver":
|
|
37
|
-
import pyodbc
|
|
38
|
-
self.connection = pyodbc.connect(f"DRIVER={self.driver};SERVER={self.host};DATABASE={self.database};"
|
|
39
|
-
f"UID={self.user};PWD={self.password}")
|
|
40
|
-
elif self.db_type == "oracle":
|
|
41
|
-
import cx_Oracle
|
|
42
|
-
oracle_port = os.getenv("ORACLE_DB_PORT", "1521") # Default to 1521 if not set
|
|
43
|
-
dsn = cx_Oracle.makedsn(self.host, oracle_port, self.oracle_sid)
|
|
44
|
-
self.connection = cx_Oracle.connect(self.user, self.password, dsn)
|
|
45
|
-
else:
|
|
46
|
-
raise ValueError("Unsupported database type")
|
|
47
|
-
|
|
48
|
-
self.cursor = self.connection.cursor()
|
|
49
|
-
|
|
50
|
-
def execute_query(self, query, params=None):
|
|
51
|
-
"""Executes a query with optional parameters"""
|
|
52
|
-
try:
|
|
53
|
-
if params:
|
|
54
|
-
self.cursor.execute(query, params)
|
|
55
|
-
else:
|
|
56
|
-
self.cursor.execute(query)
|
|
57
|
-
self.connection.commit()
|
|
58
|
-
except Exception as e:
|
|
59
|
-
print(f"Error executing query: {e}")
|
|
60
|
-
|
|
61
|
-
def fetch_one(self):
|
|
62
|
-
return self.cursor.fetchone()
|
|
63
|
-
|
|
64
|
-
def fetch_all(self):
|
|
65
|
-
"""Fetches all rows from the last executed query"""
|
|
66
|
-
return self.cursor.fetchall()
|
|
67
|
-
|
|
68
|
-
def fetch_by_param(self, table_name, column_name, value):
|
|
69
|
-
query = f"SELECT * FROM {table_name} WHERE {column_name} = %s"
|
|
70
|
-
self.cursor.execute(query, (value,))
|
|
71
|
-
return self.cursor.fetchall() # Fetch matching rows
|
|
72
|
-
|
|
73
|
-
def close(self):
|
|
74
|
-
"""Closes the connection"""
|
|
75
|
-
self.cursor.close()
|
|
76
|
-
self.connection.close()
|
|
77
|
-
|
|
78
|
-
def create_table(self, table_name, columns):
|
|
79
|
-
"""
|
|
80
|
-
Creates a table dynamically using a dictionary format.
|
|
81
|
-
Example:
|
|
82
|
-
columns = {'id': 'INTEGER PRIMARY KEY', 'name': 'TEXT', 'age': 'INTEGER'}
|
|
83
|
-
"""
|
|
84
|
-
column_defs = ", ".join(f"{col} {dtype}" for col, dtype in columns.items())
|
|
85
|
-
query = f"CREATE TABLE {table_name} ({column_defs})"
|
|
86
|
-
self.execute_query(query)
|
|
87
|
-
|
|
88
|
-
def insert_bulk(self, table_name, data):
|
|
89
|
-
"""
|
|
90
|
-
Inserts multiple rows at once.
|
|
91
|
-
Example:
|
|
92
|
-
data = [{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}]
|
|
93
|
-
"""
|
|
94
|
-
columns = ", ".join(data[0].keys()) # Extract column names
|
|
95
|
-
placeholders = ", ".join(["%s" for _ in data[0].keys()]) # Generate placeholders
|
|
96
|
-
query = f"INSERT INTO {table_name} ({columns}) VALUES ({placeholders})"
|
|
97
|
-
values = [tuple(row.values()) for row in data] # Convert dictionaries to tuples
|
|
98
|
-
self.cursor.executemany(query, values)
|
|
99
|
-
self.connection.commit()
|
|
100
|
-
|
|
101
|
-
def backup_table(self, table_name, backup_file):
|
|
102
|
-
"""
|
|
103
|
-
Exports table data into a CSV file.
|
|
104
|
-
Example:
|
|
105
|
-
backup_table('users', 'users_backup.csv')
|
|
106
|
-
"""
|
|
107
|
-
query = f"SELECT * FROM {table_name}"
|
|
108
|
-
self.execute_query(query)
|
|
109
|
-
rows = self.fetch_all()
|
|
110
|
-
|
|
111
|
-
with open(backup_file, mode="w", newline="") as file:
|
|
112
|
-
writer = csv.writer(file)
|
|
113
|
-
writer.writerow([desc[0] for desc in self.cursor.description]) # Column headers
|
|
114
|
-
writer.writerows(rows)
|
|
115
|
-
|
|
116
|
-
def setup_postgres_pool(self, min_conn=1, max_conn=5):
|
|
117
|
-
"""
|
|
118
|
-
Creates a connection pool for PostgreSQL.
|
|
119
|
-
Example:
|
|
120
|
-
setup_postgres_pool(min_conn=2, max_conn=10)
|
|
121
|
-
"""
|
|
122
|
-
from psycopg2 import pool
|
|
123
|
-
self.pool = pool.SimpleConnectionPool(min_conn, max_conn,
|
|
124
|
-
host=self.host,
|
|
125
|
-
user=self.user,
|
|
126
|
-
password=self.password,
|
|
127
|
-
dbname=self.database)
|
|
128
|
-
|
|
129
|
-
def get_connection_from_pool(self):
|
|
130
|
-
"""Fetches a connection from the pool."""
|
|
131
|
-
return self.pool.getconn()
|
|
132
|
-
|
|
133
|
-
def return_connection_to_pool(self, conn):
|
|
134
|
-
"""Returns a connection back to the pool."""
|
|
135
|
-
self.pool.putconn(conn)
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
from sqlpyhelper.db_helper import SQLPyHelper
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
# SQLite Test
|
|
5
|
-
def test_sqlite():
|
|
6
|
-
print("Testing SQLite...")
|
|
7
|
-
db = SQLPyHelper()
|
|
8
|
-
db.execute_query("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)")
|
|
9
|
-
db.execute_query("INSERT INTO users (name) VALUES (?)", ("Alice",))
|
|
10
|
-
db.execute_query("INSERT INTO users (name) VALUES (?)", ("Bob",))
|
|
11
|
-
db.execute_query("SELECT * FROM users")
|
|
12
|
-
results = db.fetch_all()
|
|
13
|
-
print("Results:", results)
|
|
14
|
-
db.close()
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
# PostgreSQL Test (Ensure you have PostgreSQL running locally)
|
|
18
|
-
def test_postgres():
|
|
19
|
-
print("Testing PostgreSQL...")
|
|
20
|
-
db = SQLPyHelper()
|
|
21
|
-
db.execute_query("CREATE TABLE IF NOT EXISTS employees (id SERIAL PRIMARY KEY, name VARCHAR(100))")
|
|
22
|
-
db.execute_query("INSERT INTO employees (name) VALUES (%s)", ("Charlie",))
|
|
23
|
-
db.execute_query("SELECT * FROM employees")
|
|
24
|
-
results = db.fetch_all()
|
|
25
|
-
print("Results:", results)
|
|
26
|
-
db.close()
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
# MySQL Test (Ensure MySQL is running locally)
|
|
30
|
-
def test_mysql():
|
|
31
|
-
print("Testing MySQL...")
|
|
32
|
-
db = SQLPyHelper()
|
|
33
|
-
db.execute_query("CREATE TABLE IF NOT EXISTS customers (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100))")
|
|
34
|
-
db.execute_query("INSERT INTO customers (name) VALUES (%s)", ("Joe",))
|
|
35
|
-
db.execute_query("SELECT * FROM customers")
|
|
36
|
-
results = db.fetch_all()
|
|
37
|
-
print("Results:", results)
|
|
38
|
-
db.close()
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
# SQL Server Test (Requires ODBC Driver for SQL Server)
|
|
42
|
-
def test_sqlserver():
|
|
43
|
-
print("Testing SQL Server...")
|
|
44
|
-
db = SQLPyHelper()
|
|
45
|
-
db.execute_query("CREATE TABLE IF NOT EXISTS orders (id INT PRIMARY KEY, product VARCHAR(100))")
|
|
46
|
-
db.execute_query("INSERT INTO orders (id, product) VALUES (?, ?)", (1, "Laptop"))
|
|
47
|
-
db.execute_query("SELECT * FROM orders")
|
|
48
|
-
results = db.fetch_all()
|
|
49
|
-
print("Results:", results)
|
|
50
|
-
db.close()
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
# Oracle Test (Ensure Oracle is installed & running)
|
|
54
|
-
def test_oracle():
|
|
55
|
-
print("Testing Oracle...")
|
|
56
|
-
db = SQLPyHelper()
|
|
57
|
-
db.execute_query("CREATE TABLE employees (id NUMBER PRIMARY KEY, name VARCHAR2(100))")
|
|
58
|
-
db.execute_query("INSERT INTO employees (id, name) VALUES (:1, :2)", (1, "Emily"))
|
|
59
|
-
db.execute_query("SELECT * FROM employees")
|
|
60
|
-
results = db.fetch_all()
|
|
61
|
-
print("Results:", results)
|
|
62
|
-
db.close()
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
# Run Tests
|
|
66
|
-
if __name__ == "__main__":
|
|
67
|
-
# test_sqlite()
|
|
68
|
-
# test_postgres()
|
|
69
|
-
test_mysql()
|
|
70
|
-
# test_sqlserver()
|
|
71
|
-
# test_oracle()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|