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.3.2.tar.gz (4.0 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.3.2-cp313-cp313-win_amd64.whl (6.0 MB view details)

Uploaded CPython 3.13Windows x86-64

mysqlengine-1.3.2-cp313-cp313-musllinux_1_2_x86_64.whl (20.4 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

mysqlengine-1.3.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

mysqlengine-1.3.2-cp313-cp313-macosx_10_13_universal2.whl (8.7 MB view details)

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

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

Uploaded CPython 3.12Windows x86-64

mysqlengine-1.3.2-cp312-cp312-musllinux_1_2_x86_64.whl (20.5 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

mysqlengine-1.3.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

mysqlengine-1.3.2-cp312-cp312-macosx_10_13_universal2.whl (8.7 MB view details)

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

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

Uploaded CPython 3.11Windows x86-64

mysqlengine-1.3.2-cp311-cp311-musllinux_1_2_x86_64.whl (21.0 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

mysqlengine-1.3.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (21.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

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

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

mysqlengine-1.3.2-cp310-cp310-win_amd64.whl (6.0 MB view details)

Uploaded CPython 3.10Windows x86-64

mysqlengine-1.3.2-cp310-cp310-musllinux_1_2_x86_64.whl (19.8 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

mysqlengine-1.3.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.0 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

mysqlengine-1.3.2-cp310-cp310-macosx_10_9_universal2.whl (8.8 MB view details)

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

File details

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

File metadata

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

File hashes

Hashes for mysqlengine-1.3.2.tar.gz
Algorithm Hash digest
SHA256 3cb781b29d9859842be5cb0756b26aa53529dc900c81f1e0e47c0b0c1e765ade
MD5 e3c42ce42b6ba54bbace3280429b6dab
BLAKE2b-256 af2a74d29b0968ae7cbcb36a6f07fed3e12f4586a280e1defada2d7df1f31bae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 dded6122972fff482fe7dfbaa7635d3332aba56b8027c7a763f87a539f5c88b9
MD5 91ebfa9747e99143f90926f81c70daad
BLAKE2b-256 286d5a6b2b53d008e9388d7854d73bdb93eeaf8b96dc7621b1b479235450073f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.2-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 db2fb3b49098d4db49a84b406e96b5370fc9fd360625cd0af942615d4f7ae23d
MD5 f176616f32ac94664000860a31f5b265
BLAKE2b-256 3d223d22d88fa712a76469e544e516a80e8efcd4b13e239a226f696063c72db4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f9656c54edcd815409b115fd6fb738ed2f62d5ce9725ac57fd798f603401d4b5
MD5 83a3522d8d7d0f64bd10b0dbd0bd13e3
BLAKE2b-256 cf6ab0d87b3478eee5e946a47ba8393e90fb4ce03d3a2fd84831c33b7f817c89

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.2-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 2ccc2c0d9bbf77eb9d9c9787856a592c6d0e4f1ba050364aa34d0678702904d7
MD5 08c6b9a83a1b098c031159d3444522fa
BLAKE2b-256 9491f08927dc16dc839d8f03b2500b8bb94121e92e4c320d4591505ed4b271bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c99266566f84bd9d5e6dadc43dbc86fec216b840b7f8b7a17c0068ea3e268ae0
MD5 688658e2827636fb71b81aba2ae8c247
BLAKE2b-256 dca0dff1bb7295d39ca73f02f7061b650663307d8627bd08c25fd08879908026

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.2-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 81b65cc8f28d275015bf41a23780a31a5d58db9672884bbaceec74cdc954c704
MD5 e19f2cbc2ed161f189842df96af8a492
BLAKE2b-256 c70d28849ae6f1882da35d6f4cc57bccb431b55956f02c5de5f627663508b98f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 43741d5aee543e2badd9c21080a847f79bbd1046f1d9c09852acb00a9dc7c919
MD5 16e95313416d0a095274966e88467cd5
BLAKE2b-256 672bac5ff5e75f5f2c0856593f1f89a2cba68112c5ed70d349f37e67510b120a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.2-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 f2a19548a6a265c7e05b0277f94d8b14454d894b946c50a7a56cad67ced0d45b
MD5 56fbcfbe5cf2a52e7c3b713b0edc5041
BLAKE2b-256 92354219d1945a3b49671f17eb72261425ee105747812c35432ae64408a013f3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 fb12808d324b2b12b257ab7b1fb9be53bdb25d1179a489743397433d75258039
MD5 9db7d24caffecbfb628860ed9ff65bf3
BLAKE2b-256 286708de7ac32f64be235291e3fea1c8f73731dd5b186cb4412dc77d06c9bef8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.2-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 34fb03dd334b9f42b97af904ff317c2e99f941262524971ae85ca7d2bcf62eab
MD5 e164b840e50f4ae4bed5aad557d7e407
BLAKE2b-256 d01cf75fada5774528e0c3a2eefb4835540f052cafb96f37ade52b1010c9b6aa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8b23f1547b5d65a0f3c9b0e0294eba06bb58b3a914176792450b21b00955f30a
MD5 fea8763f81ab7a7cfaf0b71c1ef8743c
BLAKE2b-256 1c017e1ef47e4c5cb17a5ca912a13c1afda9f41787844adae4d9ed08f0ab3b24

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.2-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 6c4a1cb2de568e502ede56ccbbae2de83375ad5707f52f3665ca1ed65f63912e
MD5 d1a25fec6a5a265e683a86b2c49d360f
BLAKE2b-256 f76b57779718e28a74de72aa268598a34c1d759d9058e653d34fa2524029a028

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e04ff4ce47ed5b7be16cf7c1edc6ae0b2c059556a351dd4493686a60bf2dba29
MD5 4f3dfb4ef39323cf51c70ebc1f2faf82
BLAKE2b-256 e58def7f0682c7c7559fd8227f2e0a958fba0c39a36b8e274013e68b5540758a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.2-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5750dd810e0915a4d5c525c3aab8da7a53d6767394599077a650bee346c43f35
MD5 6a6e48b249cae501933692a72770b8a3
BLAKE2b-256 9f1b54d3678035a58dd188f46b1710ec119a0cd49c9e1d455b6722a47abc2b4a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e400364a690a51bce3fdb80ecc72d3bc3427937643d3699c5e5ca66e73e1420a
MD5 9638cdb6b5bd3a84aac05b388ad33c83
BLAKE2b-256 f699d74564e885c5aadc59085ef99f907837faf460a4508dbdc048d2fcfa1de1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.2-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 86805a158bd2d451fc7dce117da9266a48d931a124e431e0bf5e7a1738b0b9e2
MD5 5648657232bafd0c1ad9b49fbf127755
BLAKE2b-256 8563b64f3ed64c15b251a89a5147af4e7fc9b56f310ccc722b371429f15fd85c

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