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/
โ”‚   โ”œโ”€โ”€ 2_init_users.sql
โ”‚   โ””โ”€โ”€ queries.sql
โ”œโ”€โ”€ run.py

๐Ÿ run.py file:

from wavesql.sync import WaveSQL

db = WaveSQL(
    config="path_to_my_settings.ini", path_to_sql="database/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/
โ”‚   โ”œโ”€โ”€ 0_init_db.sql
โ”‚   โ”œโ”€โ”€ 1_init_logs.sql
โ”‚   โ”œโ”€โ”€ 2_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

๐Ÿ“ Project structure

WaveSQL/
โ”œโ”€โ”€ wavesql/
โ”‚   โ”œโ”€โ”€ sync.py
โ”‚   โ”œโ”€โ”€ aio.py
โ”‚   โ”œโ”€โ”€ sqlFileObject.py
โ”‚   โ”œโ”€โ”€ constants.py
โ”‚   โ”œโ”€โ”€ asyncdatabase.py
โ”‚   โ”œโ”€โ”€ database.py
โ”‚   โ”œโ”€โ”€ errors.py
โ”‚   โ”œโ”€โ”€ config.ini
โ”‚   โ”œโ”€โ”€ sql/
โ”‚   โ”‚   โ”œโ”€โ”€ 0_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-1.0.2.tar.gz (29.1 kB view details)

Uploaded Source

Built Distribution

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

wavesql-1.0.2-py3-none-any.whl (32.8 kB view details)

Uploaded Python 3

File details

Details for the file wavesql-1.0.2.tar.gz.

File metadata

  • Download URL: wavesql-1.0.2.tar.gz
  • Upload date:
  • Size: 29.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for wavesql-1.0.2.tar.gz
Algorithm Hash digest
SHA256 a35aab0be3af3cfe8ee127fcc96c41e2f4af1abad2ac6a3ae1421ff841ee2f51
MD5 e3749f13aeedfd79fd71b2c3cf5297a9
BLAKE2b-256 c050e0c3a5e836ece596399440e28d569230cdcdf03f1ad07c82a2d1678a24e7

See more details on using hashes here.

File details

Details for the file wavesql-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: wavesql-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 32.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for wavesql-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e992ea16d79cc364269be0c99f759b86da4ad99b7f19ca7b3effac6b3d421ef5
MD5 cc38fb4e5e4f1afdc91944af71cea9e1
BLAKE2b-256 436641e7648cf84120175123926677d286e3968ca418331197865e994783deda

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