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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.10Windows x86-64

mysqlengine-1.3.0-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.0-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.0-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.0.tar.gz.

File metadata

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

File hashes

Hashes for mysqlengine-1.3.0.tar.gz
Algorithm Hash digest
SHA256 f690b4f96489ef5b317b29ffc433949081017d9537a2c99236d0eb5a2e7c974e
MD5 dee89cb974acc9f85d6bd0168f642102
BLAKE2b-256 ea4161e77623b4b28c3b7f89fe680be70065b16329998be9bfd4a3055aa337eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 1f86563d6be0698c7ed64bd5cdf54c976dba60a85a5b37e5130b5c0d015d4a4c
MD5 dae4a74b53de5d22c00d07473d8b042d
BLAKE2b-256 17a87e6460c99307a1e27d50e9e94460e7a86ffc69f3c55cf8f1b775a7814cf0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.0-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e020fed8bd4b1d972ad1d4759448b235b74f09a08bd73de72eed50492f853e9e
MD5 38ec73b51bd674ad957cc59da5e4b17a
BLAKE2b-256 1b767705345558f06e0f405fd9ca7822977c262010d4499daeeaa3e4fc1b2086

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 974949e1e2e3ea72acc4981377207e9d6cdf4e22727d023b974e8e5755d0dce3
MD5 d07268ca64cf2ffeef933e4b67e68dd5
BLAKE2b-256 8f83219ca8b944a70ece3b96a604c1f43ced6ce596351b5f817bd47917d2c1f3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.0-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 31e5d90b55dbf27cbdb89ed7770149eb3a1bc49659909814d150db1cbb1f31e4
MD5 bbca54ba44237b6eed72a6279afaa5aa
BLAKE2b-256 9832adfc7395d78ce9e75ba64845941e14a07b0b5bc9432dae047a3d63738612

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 5d3d0dc75a02556ab81ce6ad90df3bde897d27ea191cf78e45cbd07938419aa6
MD5 64442711a32b5364d2459a46bd425a5f
BLAKE2b-256 bb2ff99025541931d2d744a304ddf63f2b07cedac6135edcf79b7fe8022671cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 8b101aa53228cd04c86186a21ef59a2709286b7bc54c39ec1396d08691f7181c
MD5 d0007bc3e8ec3e47d3eb9291da6557f5
BLAKE2b-256 621fd2b917bca91c2e046bfe42c6e24db8d69f2ecb000dcc4e326d1c9d3aedb4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8123b1004fbc55f05ec165a8e5f5642cbc2004a3df1cc02d0bc11331e813d50b
MD5 f242508bcff0ed5c01128b743854751f
BLAKE2b-256 3b3395981cd555aa19c9b0509243b79567f8ea6f958fc6e23fb3d16d0129b48d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.0-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 9a469dbb9eb94d8f3119c0998a0779f9647276d24630a41285c5aababc33011f
MD5 5f56aa6e82661542114e808c2c67e0d4
BLAKE2b-256 e4533fa6484948a87553f14fc89c0578f14ce344584284fb2ba045cec8c2c585

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 debdfca22a87c6548c8bb5743bf7b2142ecc02556faec83b34a71efa99a06860
MD5 2b9f6a4d65400db4d274b1013e5a1748
BLAKE2b-256 87e60da40f28fc917655ac84051085757e057dcdf6acc58df1a4eda02131d4b6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.0-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 291e74de1b9ea2f94e5a39b7d365ab5a51491447ff3ab9431377a9e711809392
MD5 5881fb4a821787212c676455d7fc865a
BLAKE2b-256 77c9fb58afefe235921601fba0f1472b59d904892e8f514cb67a1428c7e94e30

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4a83594b80d5b769fb0cffba1c24d89bb66c043a24239879126533507dbdc167
MD5 5c4895e7c3500c4a8b40b4b4c9b09efe
BLAKE2b-256 0174a16a02d8b2459623c01befcc5a1c944a8848e50e04c041920fb1c2eb7ef4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.0-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 f6e1dd0fd2e7334fcc2915e679b81800d5959c6938517b36136c0fce02391877
MD5 f47891a4a8388f54a3ffd13e656b9dde
BLAKE2b-256 3f6aece3719c1ffcf5d15fbc05bba220837fb2d201f63358ea883ed8d25b2a4c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f77a59b1b0f826915a4170931f939daa1475a3ca0dba8e5cbaff553372bc2477
MD5 5554a5256cab8d6d1e873a05552abde6
BLAKE2b-256 b7c0e58264bc6d8d0b96ed5f532c9b8b79c920c4db27152bb80c6773749b7d61

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.0-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d886cf2b29af77fbfcb9d73877358f5ec6dee8d4492d96dd4af9d1b41fbe8d38
MD5 96fa50fbe16c3347131df90008e1da48
BLAKE2b-256 3419ccd025a3b421d1f5d483c45a1638cc745bc5872bcf822376a62b5a2bff4a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 89e17567ef49a9de0ad1f55727f72219b68e6e0d48e655ecee1d9b39b4614804
MD5 82bec8a600d8f3145ad611ac4cdbaba3
BLAKE2b-256 4d87c5a1c3a3e2c0b18c014345ea70eb4d10181c88e7661e46db9608da31c530

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.0-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 75a2d22e96d7012dbe37732e6e2545ae7f049f54bd399192669a5492b23d7bca
MD5 af459c8d28221438ae635080142cfa6a
BLAKE2b-256 2211a5292d69927582ab1cfe672b7fde39543ee55ff2f7d9f881b36826cd4a33

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