SQLPyHelper 0.1.1__py3-none-any.whl → 0.1.3__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 CHANGED
@@ -1,2 +1,2 @@
1
1
  # Match the version in setup.py
2
- __version__ = "0.1.1"
2
+ __version__ = "0.1.3"
sqlpyhelper/db_helper.py CHANGED
@@ -1,16 +1,16 @@
1
- import sqlite3
2
- import psycopg2
3
- import mysql.connector
4
- import pyodbc
5
- import cx_Oracle
6
1
  import csv
7
- from psycopg2 import pool
8
2
  from dotenv import load_dotenv
9
3
  import os
10
4
 
11
5
  load_dotenv() # Load environment variables from .env file
12
6
 
13
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
14
  class SQLPyHelper:
15
15
  def __init__(self):
16
16
  self.db_type = os.getenv("DB_TYPE").lower()
@@ -20,19 +20,25 @@ class SQLPyHelper:
20
20
  self.database = os.getenv("DB_NAME")
21
21
  self.driver = os.getenv("DB_DRIVER")
22
22
  self.oracle_sid = os.getenv("ORACLE_SID")
23
+ self.pool = None
23
24
 
24
25
  if self.db_type == "sqlite":
26
+ import sqlite3
25
27
  self.connection = sqlite3.connect(self.database)
26
28
  elif self.db_type == "postgres":
29
+ import psycopg2
27
30
  self.connection = psycopg2.connect(host=self.host, user=self.user,
28
31
  password=self.password, dbname=self.database)
29
32
  elif self.db_type == "mysql":
33
+ import mysql.connector
30
34
  self.connection = mysql.connector.connect(host=self.host, user=self.user,
31
35
  password=self.password, database=self.database)
32
36
  elif self.db_type == "sqlserver":
37
+ import pyodbc
33
38
  self.connection = pyodbc.connect(f"DRIVER={self.driver};SERVER={self.host};DATABASE={self.database};"
34
39
  f"UID={self.user};PWD={self.password}")
35
40
  elif self.db_type == "oracle":
41
+ import cx_Oracle
36
42
  oracle_port = os.getenv("ORACLE_DB_PORT", "1521") # Default to 1521 if not set
37
43
  dsn = cx_Oracle.makedsn(self.host, oracle_port, self.oracle_sid)
38
44
  self.connection = cx_Oracle.connect(self.user, self.password, dsn)
@@ -50,16 +56,46 @@ class SQLPyHelper:
50
56
  self.cursor.execute(query)
51
57
  self.connection.commit()
52
58
  except Exception as e:
53
- print(f"Error executing query: {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
54
73
 
55
74
  def fetch_all(self):
56
75
  """Fetches all rows from the last executed query"""
57
- return self.cursor.fetchall()
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
58
90
 
59
91
  def close(self):
60
92
  """Closes the connection"""
61
- self.cursor.close()
62
- self.connection.close()
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
63
99
 
64
100
  def create_table(self, table_name, columns):
65
101
  """
@@ -67,9 +103,13 @@ class SQLPyHelper:
67
103
  Example:
68
104
  columns = {'id': 'INTEGER PRIMARY KEY', 'name': 'TEXT', 'age': 'INTEGER'}
69
105
  """
70
- column_defs = ", ".join(f"{col} {dtype}" for col, dtype in columns.items())
71
- query = f"CREATE TABLE {table_name} ({column_defs})"
72
- self.execute_query(query)
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
73
113
 
74
114
  def insert_bulk(self, table_name, data):
75
115
  """
@@ -77,17 +117,17 @@ class SQLPyHelper:
77
117
  Example:
78
118
  data = [{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}]
79
119
  """
80
- columns = ", ".join(data[0].keys()) # Extract column names
81
- placeholders = ", ".join(["%s" for _ in data[0].keys()]) # Generate placeholders
82
- query = f"INSERT INTO {table_name} ({columns}) VALUES ({placeholders})"
83
- values = [tuple(row.values()) for row in data] # Convert dictionaries to tuples
84
- self.cursor.executemany(query, values)
85
- self.connection.commit()
86
-
87
- def log_query(self, query):
88
- """Logs queries for debugging purposes."""
89
- with open("query_log.txt", "a") as f:
90
- f.write(query + "\n")
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
91
131
 
92
132
  def backup_table(self, table_name, backup_file):
93
133
  """
@@ -95,34 +135,75 @@ class SQLPyHelper:
95
135
  Example:
96
136
  backup_table('users', 'users_backup.csv')
97
137
  """
98
- query = f"SELECT * FROM {table_name}"
99
- self.execute_query(query)
100
- rows = self.fetch_all()
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
101
150
 
102
- with open(backup_file, mode="w", newline="") as file:
103
- writer = csv.writer(file)
104
- writer.writerow([desc[0] for desc in self.cursor.description]) # Column headers
105
- writer.writerows(rows)
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)
106
181
 
107
- def setup_postgres_pool(self, min_conn=1, max_conn=5):
108
- """
109
- Creates a connection pool for PostgreSQL.
110
- Example:
111
- setup_postgres_pool(min_conn=2, max_conn=10)
112
- """
113
- self.pool = pool.SimpleConnectionPool(min_conn, max_conn,
114
- host=self.host,
115
- user=self.user,
116
- password=self.password,
117
- dbname=self.database)
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
118
187
 
119
188
  def get_connection_from_pool(self):
120
189
  """Fetches a connection from the pool."""
121
- return self.pool.getconn()
190
+ return self.pool.get_connection()
122
191
 
123
- def return_connection_to_pool(self, conn):
192
+ def return_connection_to_pool(self):
124
193
  """Returns a connection back to the pool."""
125
- self.pool.putconn(conn)
194
+ self.connection.close()
126
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}")
127
204
 
205
+ def begin_transaction(self):
206
+ self.execute_query("START TRANSACTION")
128
207
 
208
+ def rollback_transaction(self):
209
+ self.execute_query("ROLLBACK")
@@ -1,12 +1,15 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: SQLPyHelper
3
- Version: 0.1.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
7
7
  Classifier: Programming Language :: Python :: 3
8
- Classifier: License :: OSI Approved :: MIT License
8
+ Classifier: Development Status :: 5 - Production/Stable
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: Topic :: Database :: Database Engines/Servers
9
11
  Classifier: Operating System :: OS Independent
12
+ Classifier: License :: OSI Approved :: MIT License
10
13
  Requires-Python: >=3.8
11
14
  Description-Content-Type: text/markdown
12
15
  License-File: LICENSE
@@ -15,19 +18,29 @@ Requires-Dist: mysql-connector-python
15
18
  Requires-Dist: pyodbc
16
19
  Requires-Dist: cx_Oracle
17
20
  Requires-Dist: python-dotenv
21
+ Provides-Extra: mysql
22
+ Requires-Dist: mysql-connector-python; extra == "mysql"
23
+ Provides-Extra: postgres
24
+ Requires-Dist: psycopg2; extra == "postgres"
25
+ Provides-Extra: oracle
26
+ Requires-Dist: cx_Oracle; extra == "oracle"
27
+ Provides-Extra: sqlserver
28
+ Requires-Dist: pyodbc; extra == "sqlserver"
29
+ Provides-Extra: sqlite
18
30
  Dynamic: author
19
31
  Dynamic: author-email
20
32
  Dynamic: classifier
21
33
  Dynamic: description
22
34
  Dynamic: description-content-type
23
35
  Dynamic: license-file
36
+ Dynamic: provides-extra
24
37
  Dynamic: requires-dist
25
38
  Dynamic: requires-python
26
39
  Dynamic: summary
27
40
 
28
- # 📌 SQLPyHelper
41
+ # 📌 SQLPyHelper v.0.1.3 🚀
29
42
 
30
- A Python library for simplified database interactions across **SQLite, PostgreSQL, MySQL, SQL Server, and Oracle**. This open-source package provides an intuitive API for handling database operations efficiently.
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.
31
44
 
32
45
  ## 📖 Table of Contents
33
46
  - [🚀 Features](#-features)
@@ -40,24 +53,30 @@ A Python library for simplified database interactions across **SQLite, PostgreSQ
40
53
  - [SQL Server Example](#sql-server-example)
41
54
  - [Oracle Example](#oracle-example)
42
55
  - [📂 Project Structure](#-project-structure)
56
+ - [📌 Available Methods in SQLPyHelper](#-available-methods-in-sqlpyhelper)
43
57
  - [🌍 Contributing](#-contributing)
44
58
  - [☕ Support the Project](#-support-the-project)
45
59
 
46
60
  ---
47
61
 
48
- ## 🚀 Features
49
- - **Unified Interface** for multiple databases
50
- - **Connection pooling support** for PostgreSQL
51
- - **Bulk insertion & dynamic table creation**
52
- - **Automated logging & query execution**
53
- - **CSV export & backup functionality**
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.
54
70
 
55
71
  ---
56
72
  ## 📦 Installation
73
+ #### Install via PyPI:
57
74
  ```sh
58
75
  pip install sqlpyhelper
59
76
  ```
60
- Or, if working from source:
77
+ 📌 Package on PyPI: [SQLPyHelper on PyPI](https://pypi.org/project/SQLPyHelper/)
78
+
79
+ For local development:
61
80
  ```sh
62
81
  git clone https://github.com/adebayopeter/sqlpyhelper.git
63
82
  cd sqlpyhelper
@@ -75,11 +94,10 @@ DB_TYPE=postgres
75
94
  DB_HOST=localhost
76
95
  DB_USER=your_user
77
96
  DB_PASSWORD=your_secure_password
78
- DB_NAME=test_db
79
- DB_PORT=5432
97
+ DB_NAME=database_name
80
98
  DB_DRIVER={ODBC Driver 17 for SQL Server}
81
99
  ORACLE_SID=XE
82
- ORACLE_PORT=1521
100
+ ORACLE_DB_PORT=1521
83
101
  ```
84
102
  ### Loading `.env` in Code
85
103
  ```pycon
@@ -95,52 +113,46 @@ database = os.getenv("DB_NAME")
95
113
  ```
96
114
  ---
97
115
  ## 🛠 Usage Examples
98
- ### SQLite Example
116
+ ### Initialize SQLPyHelper
99
117
  ```pycon
100
118
  from sqlpyhelper.db_helper import SQLPyHelper
101
-
102
- db = SQLPyHelper()
119
+ db = SQLPyHelper() # Auto-detects database type based on `DB_TYPE`
120
+ ```
121
+ ### SQLite Example
122
+ ```pycon
103
123
  db.execute_query("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
104
124
  db.execute_query("INSERT INTO users (name) VALUES (?)", ("Alice",))
105
- db.execute_query("SELECT * FROM users")
106
- print(db.fetch_all())
125
+ print(db.fetch_all()) # Expected Output: [(1, 'Alice')]
107
126
  db.close()
108
127
  ```
109
128
  ### PostgreSQL Example
110
129
  ```pycon
111
- db = SQLPyHelper()
112
- db.execute_query("CREATE TABLE IF NOT EXISTS employees (id SERIAL PRIMARY KEY, name VARCHAR(100))")
113
- db.execute_query("INSERT INTO employees (name) VALUES (%s)", ("Charlie",))
114
- db.execute_query("SELECT * FROM employees")
115
- print(db.fetch_all())
116
- 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
117
135
  ```
118
136
  ### MySQL Example
119
- ```pycon
120
- db = SQLPyHelper()
121
- db.execute_query("CREATE TABLE IF NOT EXISTS customers (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100))")
122
- db.execute_query("INSERT INTO customers (name) VALUES (%s)", ("David",))
123
- db.execute_query("SELECT * FROM customers")
124
- print(db.fetch_all())
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')]
125
141
  db.close()
126
142
  ```
127
143
  ### SQL Server Example
128
144
  ```pycon
129
- db = SQLPyHelper()
130
- db.execute_query("CREATE TABLE IF NOT EXISTS orders (id INT PRIMARY KEY, product VARCHAR(100))")
131
- db.execute_query("INSERT INTO orders (id, product) VALUES (?, ?)", (1, "Laptop"))
132
- db.execute_query("SELECT * FROM orders")
133
- print(db.fetch_all())
134
- 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
135
148
  ```
136
149
  ### Oracle Example
137
150
  ```pycon
138
- db = SQLPyHelper()
139
151
  db.execute_query("CREATE TABLE employees (id NUMBER PRIMARY KEY, name VARCHAR2(100))")
140
- db.execute_query("INSERT INTO employees (id, name) VALUES (:1, :2)", (1, "Emily"))
141
- db.execute_query("SELECT * FROM employees")
142
- print(db.fetch_all())
143
- db.close()
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)
144
156
  ```
145
157
 
146
158
  ## 📂 Project Structure
@@ -157,6 +169,24 @@ db.close()
157
169
  ├─ README.md
158
170
  └─ requirements.txt
159
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. |
160
190
 
161
191
  ---
162
192
  ## 🌍 Contributing
@@ -0,0 +1,7 @@
1
+ sqlpyhelper/__init__.py,sha256=v4m1w8m6t3Ypvteu4Ukto47uZG_pAHHkBFcKjWrk__Q,54
2
+ sqlpyhelper/db_helper.py,sha256=hRgwzPM5Ze_xKwgphQLJovQmkVpVhKffmAa1C7Lje9g,8447
3
+ sqlpyhelper-0.1.3.dist-info/licenses/LICENSE,sha256=9XzXxZ_8mWFM9-2TlqyE3L69zvRf4VPY_xIzSj5iU-g,1076
4
+ sqlpyhelper-0.1.3.dist-info/METADATA,sha256=zYkK-eNm3BBm4T0bStWMSWdIR44pyWQgJOjUHh6lnQ8,7253
5
+ sqlpyhelper-0.1.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
6
+ sqlpyhelper-0.1.3.dist-info/top_level.txt,sha256=FrLqTmqTGDa8jHnnf2ZVkYO-gFvLXX9QonpUCE6wKGs,12
7
+ sqlpyhelper-0.1.3.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- sqlpyhelper/__init__.py,sha256=QB8qTXgxfZC3LzFVHrC0Ou__78Z80itF8ifXmsTG-7Y,54
2
- sqlpyhelper/db_helper.py,sha256=9z2WjA7tQaNZ1fd1TYZZAooJS2N8szescOtTMNDTCgE,4896
3
- sqlpyhelper-0.1.1.dist-info/licenses/LICENSE,sha256=9XzXxZ_8mWFM9-2TlqyE3L69zvRf4VPY_xIzSj5iU-g,1076
4
- sqlpyhelper-0.1.1.dist-info/METADATA,sha256=BjUg9nZ5PW-TjpA46g8DV8H9Q3NMWgKSqdWD5VK0_V0,5211
5
- sqlpyhelper-0.1.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
6
- sqlpyhelper-0.1.1.dist-info/top_level.txt,sha256=FrLqTmqTGDa8jHnnf2ZVkYO-gFvLXX9QonpUCE6wKGs,12
7
- sqlpyhelper-0.1.1.dist-info/RECORD,,