Skip to main content

SQL helper for databases

Project description

๐ŸŒŠ WaveSQL

Read this in Russian

WaveSQL is a lightweight yet powerful Python library for secure, synchronous and asynchronous interaction with MySQL and MariaDB.

Developed by WaveTeam under the leadership of eelus1ve


๐Ÿš€ 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 parameter user_id of type int.

  • The SQL query safely substitutes this parameter (with %s or 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


๐Ÿ”— Links


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

wavesql-2.0.0.tar.gz (33.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wavesql-2.0.0-py3-none-any.whl (35.7 kB view details)

Uploaded Python 3

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

Hashes for wavesql-2.0.0.tar.gz
Algorithm Hash digest
SHA256 0c3b8faf0b9f37c02b757c175f530d972c35fb6bc7175a2c3e5bd93ee4ee19b3
MD5 d67b9c473b1915c13a873f1eaa7feb8d
BLAKE2b-256 eac4e2a069d8e761bb58a463c0b74cc19015cc1972512b1ae58593507a2dcff6

See more details on using hashes here.

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

Hashes for wavesql-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bd7aaa5aa0b89456bbe4f0de1b599664bd85e89b6cf20339dc42ff0ed1e14580
MD5 57fc745e6b1baedf4aead556512292bc
BLAKE2b-256 751312810528a8c132deb18c48428b34563ece3e9aaf9c53ebe4390d0db1c40a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page