Skip to main content

Fast MySQL driver build in Cython (Sync and Async).

Project description

Fast MySQL driver build in Cython (Sync and Async).

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 sqlcycli

Install from github

pip install git+https://github.com/AresJef/SQLCyCli.git

Requirements

  • Python 3.10 or higher.
  • MySQL 5.5 or higher.

Features

  • Written in Cython for optimal performance (especially for SELECT/INSERT query).
  • All classes and methods are well documented and static typed.
  • Supports both Sync and Async connection to the server.
  • API Compatiable with PyMySQL and aiomysql.
  • Support conversion (escape) for most of the native python types, and objects from libaray numpy and pandas. Does NOT support custom conversion (escape).

Benchmark

The following result comes from benchmark:

  • Device: MacbookPro M1Pro(2E8P) 32GB
  • Python: 3.12.4
  • MySQL: 8.3.0
  • mysqlclient: 2.2.4
  • PyMySQL: 1.1.1
  • aiomysql: 0.2.0
  • asyncmy: 0.2.9
# Unit: second | Lower is better
name        type    rows    insert-per-row  insert-bulk select-per-row  select-all  update-per-row  update-all  delete-per-row  delete-all
mysqlclient sync    50000   1.734200        0.427027    1.756226        0.117552    1.901870        0.365056    1.651022        0.131151
SQLCyCli    sync    50000   2.203967        0.293410    2.331576        0.062152    2.279076        0.352106    2.115025        0.107902
PyMySQL     sync    50000   2.607349        0.412063    4.219286        0.321089    2.572826        0.345419    2.304015        0.104720
SQLCyCli    async   50000   3.342991        0.270254    4.297667        0.141201    3.466729        0.346168    3.343880        0.105293
aiomysql    async   50000   3.464662        0.384193    5.115068        0.322858    3.582689        0.345661    3.444377        0.104894
asyncmy     async   50000   3.851011        0.416715    5.604330        0.314939    3.824736        0.346903    3.580444        0.104748

Usage

Use connect() to create one connection (Sync or Async) to the server.

import asyncio
import sqlcycli

HOST = "localhost"
PORT = 3306
USER = "root"
PSWD = "password"

# Synchronous Connection
def test_sync_connection() -> None:
    with sqlcycli.connect(HOST, PORT, USER, PSWD) as conn:
        with conn.cursor() as cur:
            cur.execute("SELECT 1")
            res = cur.fetchone()
            assert res == (1,)
    # Connection closed
    assert conn.closed()

# Asynchronous Connection
async def test_async_connection() -> None:
    async with sqlcycli.connect(HOST, PORT, USER, PSWD) as conn:
        async with conn.cursor() as cur:
            await cur.execute("SELECT 1")
            res = await cur.fetchone()
            assert res == (1,)
    # Connection closed
    assert conn.closed()

if __name__ == "__main__":
    test_sync_connection()
    asyncio.run(test_async_connection())

Use create_pool() to create a Pool for managing and maintaining Async connections to the server.

import asyncio
import sqlcycli

HOST = "localhost"
PORT = 3306
USER = "root"
PSWD = "password"

# Pool (Context Manager: Connected)
async def test_pool_context_connected() -> None:
    async with sqlcycli.create_pool(HOST, PORT, USER, PSWD, min_size=1) as pool:
        # Pool is connected: 1 free connection (min_size=1)
        assert not pool.closed() and pool.free == 1
        async with pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute("SELECT 1")
                res = await cur.fetchone()
                assert res == (1,)
    # Pool closed
    assert pool.closed() and pool.total == 0

# Pool (Context Manager: Disconnected)
async def test_pool_context_disconnected() -> None:
    with sqlcycli.create_pool(HOST, PORT, USER, PSWD, min_size=1) as pool:
        # Pool is not connected: 0 free connection (min_size=1)
        assert pool.closed() and pool.free == 0
        # Connect automatically
        async with pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute("SELECT 1")
                res = await cur.fetchone()
                assert res == (1,)
        # 1 free connection
        assert pool.free == 1
    # Pool closed
    assert pool.closed() and pool.total == 0

# Pool (Create Directly: Connected)
async def test_pool_direct_connected() -> None:
    pool = await sqlcycli.create_pool(HOST, PORT, USER, PSWD, min_size=1)
    # Pool is connected: 1 free connection (min_size=1)
    assert not pool.closed() and pool.free == 1
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute("SELECT 1")
            res = await cur.fetchone()
            assert res == (1,)
    # Close pool manually
    await pool.close()
    assert pool.closed() and pool.total == 0

if __name__ == "__main__":
    asyncio.run(test_pool_context_connected())
    asyncio.run(test_pool_context_disconnected())
    asyncio.run(test_pool_direct_connected())

Acknowledgements

SQLCyCli is build on top of the following open-source repositories:

SQLCyCli is based on the following 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

sqlcycli-1.0.1.tar.gz (2.5 MB view hashes)

Uploaded Source

Built Distributions

sqlcycli-1.0.1-cp312-cp312-win_arm64.whl (3.6 MB view hashes)

Uploaded CPython 3.12 Windows ARM64

sqlcycli-1.0.1-cp312-cp312-win_amd64.whl (3.8 MB view hashes)

Uploaded CPython 3.12 Windows x86-64

sqlcycli-1.0.1-cp312-cp312-musllinux_1_1_x86_64.whl (11.1 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

sqlcycli-1.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.1 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

sqlcycli-1.0.1-cp312-cp312-macosx_11_0_arm64.whl (3.9 MB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

sqlcycli-1.0.1-cp312-cp312-macosx_10_9_x86_64.whl (4.0 MB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

sqlcycli-1.0.1-cp312-cp312-macosx_10_9_universal2.whl (5.3 MB view hashes)

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

sqlcycli-1.0.1-cp311-cp311-win_arm64.whl (3.6 MB view hashes)

Uploaded CPython 3.11 Windows ARM64

sqlcycli-1.0.1-cp311-cp311-win_amd64.whl (3.8 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

sqlcycli-1.0.1-cp311-cp311-musllinux_1_1_x86_64.whl (11.2 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

sqlcycli-1.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.1 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

sqlcycli-1.0.1-cp311-cp311-macosx_11_0_arm64.whl (3.9 MB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

sqlcycli-1.0.1-cp311-cp311-macosx_10_9_x86_64.whl (4.0 MB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

sqlcycli-1.0.1-cp311-cp311-macosx_10_9_universal2.whl (5.3 MB view hashes)

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

sqlcycli-1.0.1-cp310-cp310-win_arm64.whl (3.6 MB view hashes)

Uploaded CPython 3.10 Windows ARM64

sqlcycli-1.0.1-cp310-cp310-win_amd64.whl (3.8 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

sqlcycli-1.0.1-cp310-cp310-musllinux_1_1_x86_64.whl (10.5 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

sqlcycli-1.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.4 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

sqlcycli-1.0.1-cp310-cp310-macosx_11_0_arm64.whl (3.9 MB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

sqlcycli-1.0.1-cp310-cp310-macosx_10_9_x86_64.whl (4.0 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

sqlcycli-1.0.1-cp310-cp310-macosx_10_9_universal2.whl (5.3 MB view hashes)

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

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page