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.3.tar.gz (29.7 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.3-py3-none-any.whl (33.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: wavesql-1.0.3.tar.gz
  • Upload date:
  • Size: 29.7 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.3.tar.gz
Algorithm Hash digest
SHA256 794fc252d7bd6a4e79f20dd066f5c3d3d8bd218024a6a6bf2fcb837af824c181
MD5 84998f1a794b36a08690bf5c854e7561
BLAKE2b-256 8a4aedf9a38926f00c16573e46c0c9570007c7a503f5810cae6bd442bfc4e206

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wavesql-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 33.2 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 42bf0f63a7ab28ed252486a19ce268d754b37cab0dd93a3d24574d97671c45b6
MD5 73f48270b85fe3d1ab470fcfce8e5673
BLAKE2b-256 28dca912766d71e60f098c85ced6d632900bdee6b919106b77c0ad6389b9a321

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