SQL helper for databases
Project description
๐ WaveSQL
WaveSQL is a lightweight yet powerful Python library for secure, synchronous and asynchronous interaction with MySQL and MariaDB.
๐ Features
- ๐ Easy database connection via config.ini or dictionary
- โ๏ธ Automatic database schema initialization on first run
- ๐ง Support for calling stored procedures (CALL)
- ๐ช Protected methods (via @protected) โ prevent direct calls to critical ftextions
- ๐ Asynchronous version with the same API
- ๐ชต Built-in logging to database + colored console output (colorama)
- ๐ง Automatic generation of Python code (Python Bridge) from SQL files
- ๐งฉ Flexible configuration: dictionary=True, colored output, pprint, backtrace control
- ๐ก๏ธ Error catching and logging with traceback
- ๐งช Protection against missing or incomplete SQL files
๐ฆ Installation
pip install wavesql
๐งฐ Usage
๐น Simple example
from wavesql.sync import WaveSQL
db = WaveSQL(
is_dictionary=True,
is_console_log=True,
is_log_backtrace=True,
is_auto_start=True
)
db.log(level=3, text="All is good!")
๐ Asynchronous version
from wavesql.aio import AsyncWaveSQL
adb = AsyncWaveSQL(
is_dictionary=True,
is_console_log=True,
is_log_backtrace=True,
is_auto_start=True
)
await adb.log(level=3, text="Async logging works!")
All methods and behavior are identical to the synchronous version.
Just use await and import AsyncWaveSQL from wavesql.aio.
๐ง Generating Python code from SQL
If your SQL directory contains a queries.sql file, WaveSQL can automatically generate Python code to call the SQL queries defined in it.
Example content of queries.sql:
create get_user with query SELECT * FROM users WHERE id = {% extend user_id : int %} LIMIT 1;
Explanation of syntax:
-
create get_userโ declares the function/method name to be generated. -
with queryโ keyword indicating the following is the SQL query. -
Inside the query,
{% extend user_id : int %}means the generated method will have a parameteruser_idof typeint. -
The SQL query safely substitutes this parameter (with
%sor equivalent) to prevent SQL injection.
Simply enable the flag is_create_python_bridge=True during initialization:
db = WaveSQL(
is_create_python_bridge=True,
...
)
The following files will be created:
-
database.pyโ synchronous interface -
asyncdatabase.pyโ asynchronous interface -
aio.pyโ entry point for async API -
__init__.pyโ entry point for sync API -
library SQL files that initialize the database and create minimal necessary tables for proper module operation
๐งฐ Usage with is_create_python_bridge=True
๐ Project structure (before running run.py):
database/
โโโ sql/
โ โโโ 0_init_users.sql
โ โโโ queries.sql
โโโ run.py
๐ run.py file:
from wavesql.sync import WaveSQL
db = WaveSQL(
config="db/config.yaml", path_to_sql="db/sql", is_console_log=True,
is_log_backtrace=True, is_auto_start=True, is_create_python_bridge=True
)
Files with names containing _init_ and a numeric prefix (e.g., 0_init_users.sql) are initialized in the database in ascending order of this number.
The prefix must be a non-negative integer โ negative values are reserved by the library.
The queries.sql file is used for automatic generation of query methods that create two bridges (sync and async).
Example queries.sql:
create get_user with query SELECT * FROM users WHERE id = {% extend user_id : int %} LIMIT 1;
Output:
# database.py
def get_user(self, user_id: int) -> dict | None:
return self._db_query("SELECT * FROM users WHERE id = %s LIMIT 1", (user_id, ), fetch=1)
# asyncdatabase.py
async def get_user(self, user_id: int) -> dict | None:
return await self._db_query("SELECT * FROM users WHERE id = %s LIMIT 1", (user_id, ), fetch=1)
๐ Project structure (after running run.py):
database/
โโโ sql/
โ โโโ -2_init_db.sql
โ โโโ -1_init_logs.sql
โ โโโ 0_init_users.sql
โ โโโ queries.sql
โโโ sync.py
โโโ aio.py
โโโ asyncdatabase.py
โโโ database.py
โโโ run.py
๐งพ Requirements
- Python 3.12.10+
- mysql-connector-python=9.3.0
- colorama=0.4.6
- PyYAML=6.0.2
๐ Project structure
WaveSQL/
โโโ wavesql/
โ โโโ sync.py
โ โโโ aio.py
โ โโโ sqlFileObject.py
โ โโโ constants.py
โ โโโ asyncdatabase.py
โ โโโ database.py
โ โโโ errors.py
โ โโโ config.yaml
โ โโโ sql/
โ โ โโโ -2_init_db.sql
โ โ โโโ -1_init_logs.sql
โ โโโ python/
โ โ โโโ sync.py
โ โ โโโ aio.py
โ โ โโโ asyncdatabase.py
โ โ โโโ database.py
โโโ README.md
โโโ NOTICE
โโโ LICENSE
โโโ pyproject.toml
โโโ requirements.txt
๐ Changelog
See CHANGELOG.md for the detailed version history.
๐ฎ Planned Features / Roadmap
- SQLite support
- Automatic SQL syntax validation
- Automatic type mapping from SQL tables to Python code
- Procedure output recognition
- PostgreSQL support
- Generation of APIs for other languages
๐ค Author
- Darov Alexander (eelus1ve)
- Email: darov-alexander@outlook.com
- GitHub:
@eelus1ve - Developed as part of
WaveTeam
๐ Links
- ๐ Repository:
github.com/WaveTeamDevs/WaveSQL - ๐ง Organization:
WaveTeamDevs
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wavesql-2.0.0.tar.gz.
File metadata
- Download URL: wavesql-2.0.0.tar.gz
- Upload date:
- Size: 33.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c3b8faf0b9f37c02b757c175f530d972c35fb6bc7175a2c3e5bd93ee4ee19b3
|
|
| MD5 |
d67b9c473b1915c13a873f1eaa7feb8d
|
|
| BLAKE2b-256 |
eac4e2a069d8e761bb58a463c0b74cc19015cc1972512b1ae58593507a2dcff6
|
File details
Details for the file wavesql-2.0.0-py3-none-any.whl.
File metadata
- Download URL: wavesql-2.0.0-py3-none-any.whl
- Upload date:
- Size: 35.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd7aaa5aa0b89456bbe4f0de1b599664bd85e89b6cf20339dc42ff0ed1e14580
|
|
| MD5 |
57fc745e6b1baedf4aead556512292bc
|
|
| BLAKE2b-256 |
751312810528a8c132deb18c48428b34563ece3e9aaf9c53ebe4390d0db1c40a
|