Skip to main content

A Cython-Accelerated, Pythonic MySQL ORM & Query Builder

Project description

A Cython-Accelerated, Pythonic MySQL ORM & Query Builder

Created to be used in a project, this package is published to github for ease of management and installation across different modules.

Installation

Install from PyPi

pip install mysqlengine

Install from github

pip install git+https://github.com/AresJef/MysqlEngine.git

Requirements

  • Python 3.10 or higher.
  • MySQL 5.5 or higher.

Features

MysqlEngine is a Python/Cython hybrid library that provides a high-performance, programmatic interface to MySQL. It provides:

  • An ORM-style schema definition via <'Database'>, <'Table'>, <'Column'>, etc.
  • A fluent query-builder (less hand-written SQL strings).
  • Built-in support for both sync and async workflows.
  • Custom <'TimeTable'> class to create and manage time-series partitions.
  • Critical parts are implemented in Cython, minimizing Python overhead and maximizing throughput.

MysqlEngine is built on top of SQLCyCli. Because SQLCyCli already delivers solid and high-performance connectivity, MysqlEngine can concentrate on its higher-level features.

  • Delegates raw socket I/O, packet parsing, authentication, pooling, and cursor management to SQLCyCli.
  • All exeption is inherited from the SQLCyCli.errors.MySQLError.

Example (Normal Table)

import asyncio
from mysqlengine import Database, Table, Pool
from mysqlengine import Column, Index, Define, PrimaryKey


# Difine Table
class User(Table):
    id: Column = Column(Define.BIGINT(unsigned=True, auto_increment=True))
    name: Column = Column(Define.VARCHAR(255))
    pk: PrimaryKey = PrimaryKey("id")
    idx: Index = Index("name")


class Product(Table):
    id: Column = Column(Define.BIGINT(unsigned=True, auto_increment=True))
    product_name: Column = Column(Define.VARCHAR(255))
    product_price: Column = Column(Define.DECIMAL(12, 2))
    pk: PrimaryKey = PrimaryKey("id")
    idx: Index = Index("product_name")

# Define Database
class MyDatabase(Database):
    user: User = User()
    product: Product = Product()

# Instanciate Database
pool = Pool(host="localhost", user="root", password="Password_123456")
db = MyDatabase("db", pool)

# Synchronize Demo
def sync_demo(db: MyDatabase) -> None:
    db.Initialize()  # Initialize 'db'
    db.ShowDatabases()
    db.user.ShowCreateTable()
    db.user.ShowMetadata()
    db.user.Insert().Columns(db.user.name).Values(1).Execute(
        ["John", "Sarah"], many=True
    )
    db.Select("*").From(db.user).Execute()  # ((1, 'John'), (2, 'Sarah'))
    db.Drop()  # Drop 'db'

sync_demo(db)

# Asynchronize Demo
async def async_demo(db: MyDatabase) -> None:
    await db.aioInitialize()  # Initialize 'db'
    await db.aioShowDatabases()
    await db.user.aioShowCreateTable()
    await db.user.aioShowMetadata()
    await db.user.Insert().Columns(db.user.name).Values(1).aioExecute(
        ["John", "Sarah"], many=True
    )
    await db.Select("*").From(db.user).aioExecute()  # ((1, 'John'), (2, 'Sarah'))
    await db.aioDrop()  # Drop 'db'

asyncio.run(async_demo(db))

Example (Temporary Table)

import asyncio
from mysqlengine import Pool, Database, Table, TempTable
from mysqlengine import Column, Index, Define, PrimaryKey


class MyTable(Table):
    id: Column = Column(Define.BIGINT(unsigned=True, auto_increment=True))
    name: Column = Column(Define.VARCHAR(255))
    pk: PrimaryKey = PrimaryKey("id")


class MyTempTable(TempTable):
    id: Column = Column(Define.BIGINT(unsigned=True, auto_increment=True))
    name: Column = Column(Define.VARCHAR(255))
    pk: PrimaryKey = PrimaryKey("id")


class MyDatabase(Database):
    tb: MyTable = MyTable()


pool = Pool(host="localhost", user="root", password="Password_123456")
db = MyDatabase("db", pool)
db.Drop(True)

# Synchronize Demo
def sync_demo(db: MyDatabase) -> None:
    db.Initialize()  # Initialize 'db'
    db.tb.Insert().Columns("name").Values(1).Execute(["John", "Sarah"], many=True)
    with db.transaction() as conn:
        with db.CreateTempTable(conn, "temp_tb", MyTempTable()) as tmp:
            tmp.Insert().Columns("name").Select("name").From(db.tb).Execute()
            tmp.Select("*").Execute()  # ((1, 'John'), (2, 'Sarah'))
    # temporary table is automatically dropped
    db.Drop()  # Drop 'db'

sync_demo(db)

# Asynchronize Demo
async def async_demo(db: MyDatabase) -> None:
    await db.aioInitialize()  # Initialize 'db'
    await db.tb.Insert().Columns("name").Values(1).aioExecute(
        ["John", "Sarah"], many=True
    )
    async with db.transaction() as conn:
        async with db.CreateTempTable(conn, "temp_tb", MyTempTable()) as tmp:
            await tmp.Insert().Columns("name").Select("name").From(db.tb).aioExecute()
            await tmp.Select("*").aioExecute()  # ((1, 'John'), (2, 'Sarah'))
    # temporary table is automatically dropped
    await db.aioDrop()  # Drop 'db'

asyncio.run(async_demo(db))

Example (Time Table)

import asyncio
from mysqlengine import Pool, Database, TimeTable
from mysqlengine import Column, Index, Define, PrimaryKey


class MyTimeTable(TimeTable):
    id: Column = Column(Define.BIGINT(unsigned=True, auto_increment=True))
    name: Column = Column(Define.VARCHAR(255))
    dt: Column = Column(Define.DATETIME())
    pk: PrimaryKey = PrimaryKey("id", "dt")


class MyDatabase(Database):
    tb: MyTimeTable = MyTimeTable("dt", "YEAR", "2024-01-01", "2025-01-01")


pool = Pool(host="localhost", user="root", password="Password_123456")
db = MyDatabase("db", pool)
db.Drop(True)

# Synchronize Demo
def sync_demo(db: MyDatabase) -> None:
    db.Initialize()  # Initialize 'db'
    db.tb.Insert().Columns("name", "dt").Values(2).Execute(
        [("John", "2024-02-01"), ("Sarah", "2025-02-01")], many=True
    )
    db.tb.ShowPartitionRows()  # {'past': 0, 'y2024': 1, 'y2025': 1, 'future': 0}
    db.tb.ExtendToTime(end_with="2026-02-01")
    db.tb.ShowPartitionRows()  # {'past': 0, 'y2024': 1, 'y2025': 1, 'y2026': 0, 'future': 0}
    db.tb.DropToTime(start_from="2025-01-01")
    db.tb.ShowPartitionRows()  # {'past': 0, 'y2025': 1, 'y2026': 0, 'future': 0}
    db.Drop()  # Drop 'db'

sync_demo(db)

# Asynchronize Demo
async def async_demo(db: MyDatabase) -> None:
    await db.aioInitialize()  # Initialize 'db'
    await db.tb.Insert().Columns("name", "dt").Values(2).aioExecute(
        [("John", "2024-02-01"), ("Sarah", "2025-02-01")], many=True
    )
    await db.tb.aioShowPartitionRows()  # {'past': 0, 'y2024': 1, 'y2025': 1, 'future': 0}
    await db.tb.aioExtendToTime(end_with="2026-02-01")
    await db.tb.aioShowPartitionRows()  # {'past': 0, 'y2024': 1, 'y2025': 1, 'y2026': 0, 'future': 0}
    await db.tb.aioDropToTime(start_from="2025-01-01")
    await db.tb.aioShowPartitionRows()  # {'past': 0, 'y2025': 1, 'y2026': 0, 'future': 0}
    await db.aioDrop()  # Drop 'db'

asyncio.run(async_demo(db))

Acknowledgements

MysqlEngine is based on several open-source repositories.

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

mysqlengine-1.2.1.tar.gz (3.9 MB view details)

Uploaded Source

Built Distributions

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

mysqlengine-1.2.1-cp313-cp313-win_amd64.whl (6.0 MB view details)

Uploaded CPython 3.13Windows x86-64

mysqlengine-1.2.1-cp313-cp313-musllinux_1_2_x86_64.whl (20.3 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

mysqlengine-1.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.3 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

mysqlengine-1.2.1-cp313-cp313-macosx_10_13_universal2.whl (8.6 MB view details)

Uploaded CPython 3.13macOS 10.13+ universal2 (ARM64, x86-64)

mysqlengine-1.2.1-cp312-cp312-win_amd64.whl (6.0 MB view details)

Uploaded CPython 3.12Windows x86-64

mysqlengine-1.2.1-cp312-cp312-musllinux_1_2_x86_64.whl (20.3 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

mysqlengine-1.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

mysqlengine-1.2.1-cp312-cp312-macosx_10_13_universal2.whl (8.6 MB view details)

Uploaded CPython 3.12macOS 10.13+ universal2 (ARM64, x86-64)

mysqlengine-1.2.1-cp311-cp311-win_amd64.whl (6.0 MB view details)

Uploaded CPython 3.11Windows x86-64

mysqlengine-1.2.1-cp311-cp311-musllinux_1_2_x86_64.whl (20.8 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

mysqlengine-1.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (21.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

mysqlengine-1.2.1-cp311-cp311-macosx_10_9_universal2.whl (8.7 MB view details)

Uploaded CPython 3.11macOS 10.9+ universal2 (ARM64, x86-64)

mysqlengine-1.2.1-cp310-cp310-win_amd64.whl (5.9 MB view details)

Uploaded CPython 3.10Windows x86-64

mysqlengine-1.2.1-cp310-cp310-musllinux_1_2_x86_64.whl (19.7 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

mysqlengine-1.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (19.8 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

mysqlengine-1.2.1-cp310-cp310-macosx_10_9_universal2.whl (8.7 MB view details)

Uploaded CPython 3.10macOS 10.9+ universal2 (ARM64, x86-64)

File details

Details for the file mysqlengine-1.2.1.tar.gz.

File metadata

  • Download URL: mysqlengine-1.2.1.tar.gz
  • Upload date:
  • Size: 3.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mysqlengine-1.2.1.tar.gz
Algorithm Hash digest
SHA256 050e3cafeab38fa7147737a4eb817b080fd043ec1d0613f3c67875babd8b0060
MD5 69d2e03d277a357a46c1b8bede1d47df
BLAKE2b-256 0ecb8819e6667780a492466355cc7ac60e41caafa6c4a8407ab7f49009ca838c

See more details on using hashes here.

File details

Details for the file mysqlengine-1.2.1-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for mysqlengine-1.2.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 16a9a2f3e471120eea845beb8d128b3db1c5b2c3537efa2ccd92f5d9a371e3aa
MD5 6e99b93484ac2065dbcc29f1e871a9f3
BLAKE2b-256 9f3ffc5d5c53efe0403ae34daa04557fe3f1caf7a2db4a428ca484316781dd92

See more details on using hashes here.

File details

Details for the file mysqlengine-1.2.1-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for mysqlengine-1.2.1-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 274027d580046707eac6ec7f90ff6b15a5c36a6f8343a78daf8ebf31fe95cf09
MD5 a75e5cfdb5eaf37c8f73361c3dfe4bc6
BLAKE2b-256 f806023e3abc8263ec1bcaccad782227dd67b38f8e4b0e0177f438a56f2aa887

See more details on using hashes here.

File details

Details for the file mysqlengine-1.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mysqlengine-1.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 537cc4ec41f9371f1179a3ca5950208f4e44874f5a7c87717c6d7e838a228009
MD5 660e6b55f279d2d1aae99c9e93e36535
BLAKE2b-256 495928383db50841bb0eead0d97bc0891244c944c82c28e8c7aa0e8e0b0a6356

See more details on using hashes here.

File details

Details for the file mysqlengine-1.2.1-cp313-cp313-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for mysqlengine-1.2.1-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 2d16e92486824d5c6b3536031c983d4d9d86c22d73a1d11febb673487d8b6ca2
MD5 42340e144241e05ad5f2f166d48d8d47
BLAKE2b-256 8e7d5d93fff62ecfcf0aa4343e219819cfe5cebea53978325f3ba2337c73f9ee

See more details on using hashes here.

File details

Details for the file mysqlengine-1.2.1-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for mysqlengine-1.2.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 1230bf1212a4c0214d8c7868407f6ec893118a6f59f33f78ae40f55e4fff0e5c
MD5 71d60894d403fb80023a4081054d8f11
BLAKE2b-256 5530d7048f120127ba04dca7fe1b9630df3dbf80c82a1595ba85bbba41e1e217

See more details on using hashes here.

File details

Details for the file mysqlengine-1.2.1-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for mysqlengine-1.2.1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b6ecdaca05b2d97f8d9a501b23f92aedf131d6eba4bef35dd362ad40fbd74619
MD5 1aefa30ad974708c7fba83c3c8d34489
BLAKE2b-256 416d2a76c51a4746f8f802e78631ddc49ec1f07b5d077d9e714cdaa07b496a77

See more details on using hashes here.

File details

Details for the file mysqlengine-1.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mysqlengine-1.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5e6816c7fbaa71b212240c8cbf9e3263954a35b6af0e3fc2dd942e55795d4efb
MD5 88eee10fac5837cb80a2077d2728a15d
BLAKE2b-256 884275e6e32bcf91249ee1969d241ee7dadb5c9cc6d574d023dd7774229e6c94

See more details on using hashes here.

File details

Details for the file mysqlengine-1.2.1-cp312-cp312-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for mysqlengine-1.2.1-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 70e176acbf604c1a6267870577b3adaa2a4416872d1738a33a7d2ea479f328b5
MD5 f9b7b252a27e6fe0b6b20b1389a1f5b3
BLAKE2b-256 06cea753bc0f099ded674193f6e2c3d1347aea358a28f0a8b09081e3fa75cb29

See more details on using hashes here.

File details

Details for the file mysqlengine-1.2.1-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for mysqlengine-1.2.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 2db27b18b5321bd996f71451d90d3fb8a852bf7a42d06d32ccd0b4fd7a7ca7ca
MD5 77d6a66ad9b4cc0033097ec0112e63e7
BLAKE2b-256 9c5d3af5bf70ce5e902b149d9f5fcdd00889a750c7d828079ceec508977fd04f

See more details on using hashes here.

File details

Details for the file mysqlengine-1.2.1-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for mysqlengine-1.2.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 0d5acbf14e413205f43f58e054c8fc871673b940bb927cb552bf15d861cbea03
MD5 72ca7d30b8c4ed918d3442e3e0f78c89
BLAKE2b-256 f133ed2d41430215d66a567838849a48ea4135abb9d0de5500306b13333fdddd

See more details on using hashes here.

File details

Details for the file mysqlengine-1.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mysqlengine-1.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1f8d127ebc69ceb7ca20ca22f6663f52427c643f38110132e90d72a971c7d522
MD5 c6251d8d598e7240d8adca07fbc93d37
BLAKE2b-256 b3da8f39d963d93016d435f559eb0af8536711bdca86bc945a7abe4d2631515e

See more details on using hashes here.

File details

Details for the file mysqlengine-1.2.1-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for mysqlengine-1.2.1-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 951f8cd4baaaaf9e0a56e9427418eeddca486fcaa1d5dcd8097b608a52ad9533
MD5 064326694f7879cd0becd760a767ced4
BLAKE2b-256 21ee47e68c6efc1e0f296364319cd5303d264717d272498633cf59b23c7a0c67

See more details on using hashes here.

File details

Details for the file mysqlengine-1.2.1-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for mysqlengine-1.2.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 9b3cd74597aa36ea3e86f62107c12d35740c1c7648dbf56478eeb5e6c99ffc2e
MD5 981cf4e11173a46be71166d848597da7
BLAKE2b-256 297cf78c425cd3f4cea82b031f82387f8af68c8f6517de022db815c770855bd9

See more details on using hashes here.

File details

Details for the file mysqlengine-1.2.1-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for mysqlengine-1.2.1-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 20bdf0b02d50843074a77d42e3cf6a2975359d6a9e3be94e5cd924f38209eece
MD5 926b9cce1e59f23931d5ec2160f0aa85
BLAKE2b-256 075a11b0b6f7c675655951cb4bdbefaf4c25ced318fcfde5993c6480a8413512

See more details on using hashes here.

File details

Details for the file mysqlengine-1.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mysqlengine-1.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 31defbd9bd6479700f2bf7b81e15654ef68d9162a6ef64fbd877102169fbccd6
MD5 f301d2ce2b56d0ddd64f4d78fe9fb187
BLAKE2b-256 6be666dd172326541ee0804de89d2d2e87a51ca1766a98f7f4ff3bef3003d029

See more details on using hashes here.

File details

Details for the file mysqlengine-1.2.1-cp310-cp310-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for mysqlengine-1.2.1-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 40ba369a1300f3a7a52535974b1b88a734a048d43f208f976f9302f50c20852a
MD5 b6ed8e23ad1659c3335bf6af7cc9ae96
BLAKE2b-256 3d89a027d82702d2ff7852c8ea46e955179ce984cd762a3827cff67f36cb47ce

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