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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.11Windows x86-64

mysqlengine-1.3.3-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.3-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.3-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.3-cp310-cp310-win_amd64.whl (6.0 MB view details)

Uploaded CPython 3.10Windows x86-64

mysqlengine-1.3.3-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.3-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.3-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.3.tar.gz.

File metadata

  • Download URL: mysqlengine-1.3.3.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.3.tar.gz
Algorithm Hash digest
SHA256 118b21c5f9cdd884cd6f97ff4fa5e68123ba69e926311a99788be5f527140837
MD5 0449e12de877535b850737f0222e46f0
BLAKE2b-256 fc6b058f5d5079af181b6165cbfe4bb5eefd2063c1b7b7f38652c15720b9fd1e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 4f6be51c560f5a81c0784bd198d450735153f169124aea67993c9eb1eba4ac8b
MD5 e2f67210e05272d4d21a8e83231f4069
BLAKE2b-256 c422c959ca4f19b23906c46c450bda3cee7cccaed6c779e5ff68e46617f56e0f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.3-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 9a46fc5f352b44a2b54c7d300d3db8521f77e18c77fea8e10d234c61d1ea9e26
MD5 ef039328946270bebe6a6c3f4552233d
BLAKE2b-256 5360b9139f316f253d80ddc566860d42de87fb1f28a0f6a36dd7e029f33b6eb9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1ce12a10d187cc773c63607d695f8c5faa6bee7d0d5bbcafa74fdc21bfe6fa32
MD5 be6f99d345c8de54b511d0a59648ad04
BLAKE2b-256 4534d66b658bdcd376703f391aa0b406fec9ddb490e2d318bdacea969544fd69

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.3-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 fd7f3ccb5ec12b086d4148c493b9f2a75930fc100d7a9154b5c610301b45df3e
MD5 b727c76913df7d1318a8738c8d53625c
BLAKE2b-256 5e68afe7cdf067862f7ee332aed00bae60cbed1af3dca8b5d8b6839760f1dfb3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ed3defcd1e52b8faae48bce792e1b27978da933eb8a88a144ffde6645606c083
MD5 2c1ce9402f0544eca327aa91bb9bd42f
BLAKE2b-256 5de97fa5b2d5ea66068ce2e5c7682a14df070926ddf98dedee42ecdeeacf1928

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.3-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 a51b8a678c0f12852dd5ed3492215977dc711415f86c6cb5b3fe2fcb3c3ee4c4
MD5 d0e2c4d44c0d796e6097c38c89d5c647
BLAKE2b-256 bd1050f752276242ce408d3c5bc6a5aff5fba536f760069d45931c0dc667247d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ce3e84f6a885613a97b6ac253279dda5a9c8d0554cfcaaa4adf39f9aad5e05eb
MD5 31a56381eea4a93a382bb5ef45bf6103
BLAKE2b-256 eb604f6a287c24d05cab9d84ea58bf16bf8951ff2c8571ba8061252ad47bbda6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.3-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 f8997c2d998a211ffe5f3a8bd2c538ec86daae89bbadd0abe4a0055be2bf3088
MD5 171b71ffd886c008381f172cd68b885b
BLAKE2b-256 0a77e7c0646d87a02a89fa0ce3b42ac3229e83f3604c1e9ab3f27c66c389b91d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 2573b697d786e8948774f78bc8dfbe8d9276ec709625b69a10d830efb813b10b
MD5 f2c1d975b18f2bffa042bd3a5575d75a
BLAKE2b-256 5c14904dd62bafe74d2137308d28f5ec67d6acfcceccb392ab4210005df26d7e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.3-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 283e814fc5893648b3596c95fc5227bfb3256c52df70da69bd3c8a7221eaedbf
MD5 726b57923e2427f8a0d9c0970e252b25
BLAKE2b-256 900d8e221b1738466363d6d08e5fd51d7d3a710abaeb2e2517367976862b41ce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7c88121a30af37f9a8a15a23e7d02f47621f7641f9d5f7edf0afa96cacfeef2f
MD5 c8c957d16e8aca5d991997c3045e910f
BLAKE2b-256 d87b77d238772360f9c0e790208c768023c9e7ae31601f75b18a0904e1a64cf7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.3-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 c06bd46060f5319c409f5e9316b0c33750da14a90c42adc5e9a9ddcfde060c3a
MD5 0e878fc672c76449d8c30f1cebd1b6a4
BLAKE2b-256 bdb39c38c4452ffc704af42e457296b9bb063556a91a1bb86ac89ffad9868315

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e707ab73145b84bfb6b7f43fea019ce18ad88863abaf5970fdaf17695605b583
MD5 5db0ebecd044210db1db167c628ab447
BLAKE2b-256 1eaed84eb81f3acdef35347dd914a0bef9f297add3cf4621994b3a5631e0616e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.3-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 39ca0aa96ff7f9c7111a31f84daadce169284d68759d871bcb0994a6833f445c
MD5 019720be05093c833d78c9f2b85fea0f
BLAKE2b-256 8483a522f43ff6095f7b36edd07718917d668c623b37dc32cd588789a7aef08c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 080d4a440b672b3ec1b130a42f9be3a303f248031c792e8f6ea1fc1613ab3571
MD5 edb24a6fb54956a464179a158151bfb6
BLAKE2b-256 706f6a337e47fc01f00453969feafe7e9d617c315a17a745af8c53a4a2f148a0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.3-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 60c7c3fea743f2f7bff9f76a8d26f3a1dd743614b165cb37c59170cd2ea826ca
MD5 dd42c25ef018ad442eb9536fc8334b12
BLAKE2b-256 e245dc885593ca59a7506272e7103b9ff297677de81e1756f58c0c6d2089565b

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