Skip to main content

A thread safe sql executor for Python like MyBatis with connection pool. It helps you automatically manage database connections and transactions. It also provides ORM operations for single tables.

Project description

Mapper file '''''''''''

Create a mapper file in 'mapper' folder, you can named 'person_mapper.xml', like follow:

.. code:: xml

   <?xml version="1.0" encoding="UTF-8"?>
   <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://gitee.com/summry/sqlx-batis/blob/master/dtd/mapper.dtd">
   <mapper namespace="person">
       <select id="select_all">
           select id, name, age from person
        </select>

        <select id="select_by_name">
           select id, name, age from person where name = ?
        </select>

        <select id="select_by_name2">
           select id, name, age from person where name = :name
        </select>

        <select id="select_include" include="select_all">
           {{ select_all }}
             {% if name -%}
              where name = :name
             {%- endif -%}
        </select>
   </mapper>

Usage Sample ''''''''''''

.. code:: python

from typing import List, Tuple, Mapping
from sqlbatis import mapper, sql, dbx, init_db

@mapper(namespace='person')
def select_all(): List

@mapper(namespace='person')
def select_by_name(name: str): List

@mapper(namespace='person')
def select_by_name2(name: str): List

@mapper(namespace='person')
def select_include(name: str): List

@sql('select id, name, age from person where name = ?')
def query_by_name(name: str): List(Mapping)

@sql('select id, name, age from person where name = :name')
def query_by_name2(name: str): List(Mapping)

if __name__ == '__main__':
    # init_db('test.db', driver='sqlite3', show_sql=True, debug=True, mapper_path='./mapper')
    # init_db("postgres://user:password@127.0.0.1:5432/testdb", driver='psycopg2', pool_size=5, mapper_path='./mapper')
    init_db(host='127.0.0.1', port='3306', user='xxx', password='xxx', database='test', pool_size=5, show_sql=True, mapper_path='./mapper')

    persons = select_all()
    # result:
    # (3, 'zhangsan', 15)
    # (4, 'lisi', 26)
    # (5, 'wangwu', 38)

    persons = select_by_name('zhangsan')
    # result:
    # (3, 'zhangsan', 15)

    persons = select_by_name2(name='zhangsan')
    # result:
    # (3, 'zhangsan', 15)

    persons = select_include(name='zhangsan')
    # result:
    # (3, 'zhangsan', 15)

    persons = query_by_name('zhangsan')
    # result:
    # {'id': 3, 'name': 'zhangsan', 'age': 15}

    persons = query_by_name2(name='zhangsan')
    # result:
    # {'id': 3, 'name': 'zhangsan', 'age': 15}
   
    # you can use dbx execute mapper sql with full sql id: namespace join sql id
    persons = dbx.select('person.select_all')  # 'person' is namespace, 'select_all' is sql id
    # result:
    # (3, 'zhangsan', 15)
    # (4, 'lisi', 26)
    # (5, 'wangwu', 38)

    persons = dbx.select('person.select_by_name', name='zhangsan')
    # result:
    # (3, 'zhangsan', 15)

    persons = dbx.sql('person.select_by_name').select(name='zhangsan')
    # result:
    # (3, 'zhangsan', 15)

    # you can direct execute sql with db
    effected_rowcount = db.insert(table='person', name='zhaoliu', age=66)

    persons = db.select('select id, name, age from person')
    # result:
    # (3, 'zhangsan', 15)
    # (4, 'lisi', 26)
    # (5, 'wangwu', 38)
    # (6, 'zhaoliu', 45)

    persons = db.query('select id, name, age from person name = :name', name='zhangsan')
    # result:
    # [{'id': 3, 'name': 'zhangsan', 'age': 15}]

    persons = db.sql('select id, name, age from person name = :name').query(name='zhangsan')
    # result:
    # [{'id': 3, 'name': 'zhangsan', 'age': 15}]

    persons = db.select('select id, name, age from person where name = ?', 'zhangsan')
    # result:
    # [(3, 'zhangsan', 15)]

Transaction '''''''''''

.. code:: python

    from sqlbatis import with_transaction, transaction

    @with_transaction
    def test_transaction():
        insert_func(....)
        update_func(....)


    def test_transaction2():
        with transaction():
            insert_func(....)
            update_func(....)

If you want to operate MySQL database, may be you need MySqlx: https://pypi.org/project/mysqlx

If you want to operate PostgreSQL database, may be you need MySqlx: https://pypi.org/project/pgsqlx

If you just wanted a simple sql executor, may be you need sqlx-exec: https://pypi.org/project/sqlx-exec

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

sqlx-batis-2.1.4.tar.gz (5.0 kB view details)

Uploaded Source

File details

Details for the file sqlx-batis-2.1.4.tar.gz.

File metadata

  • Download URL: sqlx-batis-2.1.4.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.10.0 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.16 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.2

File hashes

Hashes for sqlx-batis-2.1.4.tar.gz
Algorithm Hash digest
SHA256 00bdc74c5ec4e67d98aa5eccfdebcb33f7ac336d954704e577efd71ab49e119e
MD5 e26ce266bbf61f57b76fe91d358c4d6a
BLAKE2b-256 aaccdaf722bc903e869dd60fa36f72efd1065ab7ea6a97b71002e965008a9d3c

See more details on using hashes here.

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