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.4.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.4-cp313-cp313-win_amd64.whl (6.0 MB view details)

Uploaded CPython 3.13Windows x86-64

mysqlengine-1.3.4-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.4-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.4-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.4-cp312-cp312-win_amd64.whl (6.0 MB view details)

Uploaded CPython 3.12Windows x86-64

mysqlengine-1.3.4-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.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

mysqlengine-1.3.4-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.4-cp311-cp311-win_amd64.whl (6.0 MB view details)

Uploaded CPython 3.11Windows x86-64

mysqlengine-1.3.4-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.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (21.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

mysqlengine-1.3.4-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.4-cp310-cp310-win_amd64.whl (5.9 MB view details)

Uploaded CPython 3.10Windows x86-64

mysqlengine-1.3.4-cp310-cp310-musllinux_1_2_x86_64.whl (19.9 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

mysqlengine-1.3.4-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.4-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.4.tar.gz.

File metadata

  • Download URL: mysqlengine-1.3.4.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.4.tar.gz
Algorithm Hash digest
SHA256 4a3d79618862a6807da72326ddff98fb30df7ddc9b1a8367329574f6d0ac4807
MD5 eeee7dd05f874b716954a8e1b024871a
BLAKE2b-256 dcee33c1b023df4bd8d8e126124692001ce417b79c35e3a6f40a8dec6b3ec5fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 f0d1c01bd250decc30cfab14d0a9aea94b50e252db86255f310f303fd38b728a
MD5 7b32dc448698622ef55a8b69bc6d508e
BLAKE2b-256 e0557997dd378dd3bc3769be6bdbb89d6abed3cb8fb58c936c38b4cde133165b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.4-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 6a547e388fa772c13f4f016488793d5940a536513d9dd8aad9bd5421022c02f6
MD5 c5ef5813c338305adc3d2197bbc0839e
BLAKE2b-256 e09a7b28c7891b9cd8d2b306b42c16dad457269d422c9a20587880b9ce516dba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b5295bd2e04c1793677e7f982ba32b9e7192cec0641f3f03cc6d3b30280e1574
MD5 47c79fe973109e7236b15b11926b4936
BLAKE2b-256 6d8eb56501dc97f27d31a5e869777c9787d6fccf0ae220beefa137c31b49f93f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.4-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 8d4ebe645966272e92d643d6bbb67124269605b1bddc82e503d378e277e259be
MD5 9dbd6f935a1e892b7b3bed7aeea1d575
BLAKE2b-256 7cae8ec6395c676afa625932bbafbd832f5477ecedaf8b8fbfe2872603468737

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ddcce5dbabe260734f76a0528b78f2240ad701a3a5a965dce243ae4e021a692c
MD5 eb23821a149862ac04e913690f88b681
BLAKE2b-256 5491a2a4f25ac33d28d43fffd6fdc8d68c1e637e4275d81846eeba68aff85822

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.4-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ac87a429974da4fa29be961883fab3b6e24085fdaeb3fbc6f6df47dd76999859
MD5 cc196f84e6a966e9ded94be80674700c
BLAKE2b-256 ff8ca3dae8ed901acf813907d444e876c27471c90854486af9c4a03542b43332

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b0d2c35ed73cc3cf6fde11dac761bc5d963d2b98cecfea41617e13d8f748b733
MD5 c8dd6e6ac52b7a61343331282cc1ed35
BLAKE2b-256 5796729a2394246da7843b5134a7dd2992b8f25a19f208294e268db45309ee83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.4-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 bd7cb22a423ae0f39134091da76edd49e852acaccf7231078d3bdf5e28137ad7
MD5 fe1b12c008b829878ce4e0eaced18a1d
BLAKE2b-256 79048b35ee3ffa924cbca9b561051c595b44c3d3be662c9838b975f7ae3f54c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 be4e5f667cd04c90b46b6f0772027412df9663a1f90ae4a7b503a7fcf93b0ac0
MD5 4f11173cb561e93b2a95cd5100630ebc
BLAKE2b-256 e98688dff412f1426b7e2664d018466d6f38782bb370533d6ec9aa8bac0676df

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.4-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f33b846fe9aa0be11f2b708ccd56085a747862ed03ac0ae96897fba7d397cf2e
MD5 03013b0d36b1cfccd7d8a9ba4f74de32
BLAKE2b-256 0a18be738a01196115e1970bc4ccfa6730b3233906865803328ecbe3558843cf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f6e0fcfce6a1eaf16a01dab44898c99b5dbee099c9aafd66200accbceab9f2c1
MD5 d3d5c6f2c1e3a3293ac56ef732fb3882
BLAKE2b-256 1ccf3f59631ee434e02323064dbd86496364da132541f838ea5ab4b2d855f10d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.4-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 3a5dab9e3c107593ca5a088e841e60d610267f6118ece8acf133799c4356ca84
MD5 dddf6223500eecab4eb34e44023ac036
BLAKE2b-256 35c3d33713134bd8357f2788b0998033622c6f98b3dbfc861b5355f2b30092ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 fdd58b391f4661f854ef226f21f0a669ac725a9475e6eda2c4bf8294648d11b5
MD5 f1f311a854d0c077e46c13f8a27e9977
BLAKE2b-256 1a2ff2ca2470fe851193bb555934292c6d0a5c2c6225aca7ef423d953f3fca84

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.4-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c87c1c2da717c97c08d902629a91471a5d2680c5d11790c25f5d8e254f3d8b05
MD5 c0b4298a2f468af4c9958e414f15c950
BLAKE2b-256 4a85b12dcac66a8cd5bd39400bc942523f512d4018450ebc1e6d62491b330c30

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e1fc3f9607edb6cce3676e2a04de2ecfc15280d261e4a6b6b8a73c4e3d539b91
MD5 24a78df53457991effccb4394b1583be
BLAKE2b-256 54ee6c3861416c034e452331f83b6c5a664d8ffaaf3457e6ab529f96e6f2ceb5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.4-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 cdc64eb4db06c2599fa75cd73807ed54b545d4345de137c885630b80e9e46bee
MD5 218c5ed340976ed80c84a43ecdd15a3a
BLAKE2b-256 44d718e639a4adc2f7cfa19a97316ea9a954e8bbacbc2ab074c9b8edf2dcdbd3

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