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.4.tar.gz (29.9 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.4-py3-none-any.whl (33.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: wavesql-1.0.4.tar.gz
  • Upload date:
  • Size: 29.9 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.4.tar.gz
Algorithm Hash digest
SHA256 8ecc5b07afaabcd8596bfcbcf0ab500b83920246de9bc64a50e8d45725b78832
MD5 98fd9d0519c55edd91c1d420892e2ca5
BLAKE2b-256 6beecc88171ff3ce3f95d2af35580a0e992c49e1a1238cd0479819a764771496

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wavesql-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 33.4 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e282f34fe8ecc8d2f17cfe3b7f25e96cd6c645616dda6bee8bc627f958bccd63
MD5 4a13c874e9bb823aa700312e8b8e45b5
BLAKE2b-256 12b159ea9f3f7ac5b334e382514d0bb100d321e9608696a9258afb0068580da4

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