Skip to main content

Functions and types to facilitate mysql typed queries

Project description

qusql-py-mysql-type

Facilitate mysql typed queries that can be checked using mypy.

A schema definition must be placed in "mysql-type-schema.sql" in the root of the project:

DROP TABLE IF EXISTS `t1`;
CREATE TABLE `t1` (
    `id` int(11) NOT NULL,
    `cbool` tinyint(1) NOT NULL,
    `cu8` tinyint UNSIGNED NOT NULL,
    `cu16` smallint UNSIGNED NOT NULL,
    `cu32` int UNSIGNED NOT NULL,
    `cu64` bigint UNSIGNED NOT NULL,
    `ci8` tinyint,
    `ci16` smallint,
    `ci32` int,
    `ci64` bigint,
    `ctext` varchar(100) NOT NULL,
    `cbytes` blob,
    `cf32` float,
    `cf64` double
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `t1`
    MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

See sql_type::schema for a detailed description.

This schema can then be used to type queries:

import MySQLdb as mdb
from qusql_mysql_type import execute

connection = mdb.connect(
    host="127.0.0.1",
    user="test",
    passwd="test",
    db="test",
    port=3306,
    use_unicode=True,
    autocommit=True,
)

id = execute(
    connection,
    "INSERT INTO `t1` (`cbool`, `cu8`, `cu16`, `cu32`, `cu64`, `ctext`)"
    "VALUES (%s, %s, %s, %s, %s, %s)",
    True, 8, 1243, 42, 42, "Hello world"
).lastrowid


(cu16, ctext, ci31) = execute(
    connection,
    "SELECT `cu16`, `ctext`, `ci32` FROM `t1` WHERE `id`=%s",
    id
).fetchone()

assert row.cu16 == 1234
assert cu16 == 1234
assert ctext == "Hello would"

These queries can then be checked by mypy assuming the "qusql_mysql_type_plugin" is enabled in tho pyproject.toml

[mypy]
plugins = qusql_mysql_type_plugin

List hack

Support for lists as arguments to queries is added this is done done by adding a sufficient number of %s's to the query.

rows = execute(
    connection,
    "SELECT `v`, `t` FROM `table` WHERE `v` IN (_LIST_)",
    [42, 43, 45],
).fetchall()

Here the _LIST_ is replaced with %s,%s,..,%s where the number of %s's depend on the length of the list. If the list is empty _LIST_ is replaced by NULL

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

qusql_mysql_type-0.1.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qusql_mysql_type-0.1.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file qusql_mysql_type-0.1.0.tar.gz.

File metadata

  • Download URL: qusql_mysql_type-0.1.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.6

File hashes

Hashes for qusql_mysql_type-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dc5c38880495532a2cb2b2de75fe1c8f39782fe6d05763db32a8152be0c452d9
MD5 70204dd8fbcc40f32ae69f2dc28169be
BLAKE2b-256 a8040b4dc40f8203b4ed2269df71202bd3b15015228e5579047e19f82cd6b0fe

See more details on using hashes here.

File details

Details for the file qusql_mysql_type-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for qusql_mysql_type-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2f20452cd7be5ba7ed85d466407dafdb2b51588a75e0667aa05aa0a91618e62
MD5 2d3d6c109166e80f657c6d553c63fa31
BLAKE2b-256 6f0d1602ec50598ffd5463ddcb5df544b5948ce66250c759593ca9d1acffe3c3

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