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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.11Windows x86-64

mysqlengine-1.3.1-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.1-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.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.3.1-cp310-cp310-win_amd64.whl (6.0 MB view details)

Uploaded CPython 3.10Windows x86-64

mysqlengine-1.3.1-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.1-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.1-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.1.tar.gz.

File metadata

  • Download URL: mysqlengine-1.3.1.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.1.tar.gz
Algorithm Hash digest
SHA256 678318eeacc0d9598c2351ace45d400baa9200336cbba4a1aea491c19548a737
MD5 ebbf50c5bcd24a6cb1d712dfb994cb2d
BLAKE2b-256 d49b3ac8973ad6bf0ca611e96a9245bd82df6fe341edbd149e2d104aa7328fac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 3c27dadbec99bc67070fab7add85fd6a2265dd09b267e1cded2a2f95f94cca17
MD5 1ce5b880121a0a895b76eaab1bae0a74
BLAKE2b-256 92e25c23ca4ab441c0b8bcb9ee1464712f79b26da9f85844477af372a03af47b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.1-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 9483e6444969376fad35f335b7d7278f9e8f2cf9cf0cae101ae3ca90f7e2ccd7
MD5 3db0bde9cac90c2d6de3f9b2b8ce1e36
BLAKE2b-256 d2cde4f798312826f00f0d51f2f4d3eaf0f0f6126a74be19990af77c45889ac8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 184463e78ff75d1cc416a70ca66553f06e1b980aafb1b419a0e947ef9408b02f
MD5 212a93e3480dbf0500116f21f3d78d31
BLAKE2b-256 0b82ea4ada016bc197b814b7504a45c0f3172a0202de1d60e3c236a55d187d26

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.1-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 6cc1ae21eef859fda3b0f7bb6d9b1297cbccc91e9411b956880b15b551e25a0b
MD5 282d13a399664d65415ba9c9ed1fcdc8
BLAKE2b-256 49e423db270efab82fda544cede349ab11f17213f9bdc21d74223931b3eefc58

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 1733d479f486b1b289a85b7e89c7aa4599fb26b6b5fdb3d82738ea573c64564f
MD5 4ae9e3ab9d2ea88cf0f27eb4322fd06d
BLAKE2b-256 5c0ac0ad01f956c46a1b73f833e6d6d36c7f266982956c29c0d360d3a43bc090

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 fd25e77df7274540898808fbafe889e3a763e4865288468a77d1a219c4639054
MD5 0785f280664240ce2ac5c8dc917aadf1
BLAKE2b-256 0cdd9b6b82f44cfbdb4f1072462841ca40bb2926bc5b27eefeea696b83b437eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7886bca67ed45ab2eda9da004933f83bf25dcb385a621ce2b101a41b86cd82f3
MD5 37679740fd2801842719378999c60ea5
BLAKE2b-256 09110b9ff611196dedb1da642e0a240cc5179d30a4493e2677fa50b1b53308b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.1-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 7a4aeea32dbc98089669f7ff09ec19b8efc04850abfca872576a5fde32b5cf8d
MD5 47aa5b26f6af7473ca0fb3b9f396b349
BLAKE2b-256 b6929d6ad5b4417ceeda53e7e603bdc93dea5532abf4d0f1ec59ecf60a850432

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 8c9ffb60447616e253f2fc339756b1cd861f341612653050e727ac7b0974c7ad
MD5 2b154e066a6fe57fe94fd4d0fc7dd158
BLAKE2b-256 039d7ad61c58803155cacb4ae7f3a5ff20c29fd3f9d6bdaf76a7ea9887cc199a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 caa310941532eff2eb7a9ea2af591746a533610afcc5c8f1feeb4aa09ed3edb5
MD5 02025b7994a5e05b34ba2aab4024b36f
BLAKE2b-256 7acfa55dabcb9120ef00c6414be1e71cc2e3b92a3f432626893f8ac8f728d66e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 97cf1c8a2a399e090a661484f28dc74384f0c6b2308413b68aaba397025e88ec
MD5 063946d05d1c7a86c056028865f12f18
BLAKE2b-256 92dad8d17a86bd052dcf1c4fea30af0ff9553864fd551f318824572950b189f8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.1-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 17246e8926e23e817e1189c6cbfda14a33924b8e02dd8d400a1ec6cbfa6e18a8
MD5 e92e07c071e8f8548ebcb42a54f52978
BLAKE2b-256 c1f26a983573f35a2ed73852d63dab1f7f86fe6469316f8f3098e5fe0531167e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 0f921ec0598753cd4e4990b5fd53db284496a26ac904067819510a4d7673bf2f
MD5 090e3a8374322305586a38c2b709d34f
BLAKE2b-256 5579c838763a2aac02586074af4a2240122f27d3f27ec9601f058cdcd41dfd80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.1-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 3feba1292d385535f6ef74535d2c7f5aca37e31863ab2c4c7149e816390cf074
MD5 292b03b1cb5a65c4496ada53afb4dff3
BLAKE2b-256 5db5159aeee5f3dc7d151d1fb7dd25268228aa75e50486eac9cd7e7d2c84f7b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 01fcf5ff2b18d259e73ee317dac594566e789fdb4b7daa05c2593bd41a44d160
MD5 285f9f1b539f54b071190d0552f3a641
BLAKE2b-256 2e7b37afb0a103cf77cd3bd8bd0e79eb9fd747ebc9f2bd46ae2e7d23fdf23980

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mysqlengine-1.3.1-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 bfd7d43360737e5fa9a0d317e40446b4c711b9bb52fb822207e90995d294d9b1
MD5 81e4256b8c241d634bc63c0a19d93f42
BLAKE2b-256 9f76d88b14ab7b284ab4478e5fc1e966a8ad4f61211fbbc8966e588f6c847de1

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