pgsqlx


Namepgsqlx JSON
Version 2.2.0 PyPI version JSON
download
home_pagehttps://gitee.com/summry/pgsqlx
SummaryA thread safe sql executor for PostgreSQL like MyBatis with connection pool. It helps you automatically manage database connections and transactions. It also provides ORM operations for single tables.
upload_time2024-05-04 10:37:18
maintainerNone
docs_urlNone
authorsummy
requires_python>=3.5
licenseNone
keywords sql postgresql mybatis python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            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/pgsqlx/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 pgsqlx import mapper, sql, db, 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(host='127.0.0.1', port='3306', person='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)

        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}]

        # you can use orm to operate a single table
        class person(Model):
            __pk__ = 'id'
            __table__ = 'person'
            __pk_seq__ = 'person_id_seq'

            def __init__(self, id: int = None, name: str = None, age: int = None):
                self.id = id
                self.name = name
                self.age = age


        effected_rowcount = Person.insert(name='tianqi', age=77)

        persons = Person.query(name='tianqi')
        # select id, name, age from person where name = :name
        # result:
        # {'id': 7, 'name': 'tianqi', 'age': 77}

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

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

.. code:: python

       from pgsqlx import with_transaction, transaction

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


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


You can generate model class with pgsqlx-generator: https://pypi.org/project/pgsqlx-generator

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

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

If you wanted simultaneously support MySQL and PostgreSQL, may be you need sqlx-batis: https://pypi.org/project/sqlx-batis



            

Raw data

            {
    "_id": null,
    "home_page": "https://gitee.com/summry/pgsqlx",
    "name": "pgsqlx",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.5",
    "maintainer_email": null,
    "keywords": "sql, PostgreSQL, MyBatis, python",
    "author": "summy",
    "author_email": "xiazhongbiao@126.com",
    "download_url": "https://files.pythonhosted.org/packages/8b/22/4868a6c5a593db542493c7cbf907437b36a097b0cbd9d038dca84b0dfa52/pgsqlx-2.2.0.tar.gz",
    "platform": null,
    "description": "Mapper file\r\n'''''''''''\r\n\r\nCreate a mapper file in 'mapper' folder, you can named\r\n'person_mapper.xml', like follow:\r\n\r\n.. code:: xml\r\n\r\n       <?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n       <!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"https://gitee.com/summry/pgsqlx/blob/master/dtd/mapper.dtd\">\r\n       <mapper namespace=\"person\">\r\n           <select id=\"select_all\">\r\n               select id, name, age from person\r\n            </select>\r\n\r\n            <select id=\"select_by_name\">\r\n               select id, name, age from person where name = ?\r\n            </select>\r\n\r\n            <select id=\"select_by_name2\">\r\n               select id, name, age from person where name = :name\r\n            </select>\r\n\r\n            <select id=\"select_include\" include=\"select_all\">\r\n               {{ select_all }}\r\n                 {% if name -%}\r\n                  where name = :name\r\n                 {%- endif -%}\r\n            </select>\r\n       </mapper>\r\n\r\nUsage Sample\r\n''''''''''''\r\n\r\n.. code:: python\r\n\r\n    from typing import List, Tuple, Mapping\r\n    from pgsqlx import mapper, sql, db, dbx, init_db\r\n\r\n    @mapper(namespace='person')\r\n    def select_all(): List\r\n\r\n    @mapper(namespace='person')\r\n    def select_by_name(name: str): List\r\n\r\n    @mapper(namespace='person')\r\n    def select_by_name2(name: str): List\r\n\r\n    @mapper(namespace='person')\r\n    def select_include(name: str): List\r\n\r\n    @sql('select id, name, age from person where name = ?')\r\n    def query_by_name(name: str): List(Mapping)\r\n\r\n    @sql('select id, name, age from person where name = :name')\r\n    def query_by_name2(name: str): List(Mapping)\r\n\r\n    if __name__ == '__main__':\r\n        init_db(host='127.0.0.1', port='3306', person='xxx', password='xxx', database='test', pool_size=5, show_sql=True, mapper_path='./mapper')\r\n\r\n        persons = select_all()\r\n        # result:\r\n        # (3, 'zhangsan', 15)\r\n        # (4, 'lisi', 26)\r\n        # (5, 'wangwu', 38)\r\n\r\n        persons = select_by_name('zhangsan')\r\n        # result:\r\n        # (3, 'zhangsan', 15)\r\n\r\n        persons = select_by_name2(name='zhangsan')\r\n        # result:\r\n        # (3, 'zhangsan', 15)\r\n\r\n        persons = select_include(name='zhangsan')\r\n        # result:\r\n        # (3, 'zhangsan', 15)\r\n\r\n        persons = query_by_name('zhangsan')\r\n        # result:\r\n        # {'id': 3, 'name': 'zhangsan', 'age': 15}\r\n\r\n        persons = query_by_name2(name='zhangsan')\r\n        # result:\r\n        # {'id': 3, 'name': 'zhangsan', 'age': 15}\r\n\r\n        # you can use dbx execute mapper sql with full sql id: namespace join sql id\r\n        persons = dbx.select('person.select_all')  # 'person' is namespace, 'select_all' is sql id\r\n        # result:\r\n        # (3, 'zhangsan', 15)\r\n        # (4, 'lisi', 26)\r\n        # (5, 'wangwu', 38)\r\n\r\n        persons = dbx.select('person.select_by_name', name='zhangsan')\r\n        # result:\r\n        # (3, 'zhangsan', 15)\r\n\r\n        persons = dbx.sql('person.select_by_name').select(name='zhangsan')\r\n        # result:\r\n        # (3, 'zhangsan', 15)\r\n\r\n        # you can direct execute sql with db\r\n        effected_rowcount = db.insert(table='person', name='zhaoliu', age=66)\r\n\r\n        persons = db.select('select id, name, age from person')\r\n        # result:\r\n        # (3, 'zhangsan', 15)\r\n        # (4, 'lisi', 26)\r\n        # (5, 'wangwu', 38)\r\n\r\n        persons = db.query('select id, name, age from person name = :name', name='zhangsan')\r\n        # result:\r\n        # [{'id': 3, 'name': 'zhangsan', 'age': 15}]\r\n\r\n        persons = db.sql('select id, name, age from person name = :name').query(name='zhangsan')\r\n        # result:\r\n        # [{'id': 3, 'name': 'zhangsan', 'age': 15}]\r\n\r\n        # you can use orm to operate a single table\r\n        class person(Model):\r\n            __pk__ = 'id'\r\n            __table__ = 'person'\r\n            __pk_seq__ = 'person_id_seq'\r\n\r\n            def __init__(self, id: int = None, name: str = None, age: int = None):\r\n                self.id = id\r\n                self.name = name\r\n                self.age = age\r\n\r\n\r\n        effected_rowcount = Person.insert(name='tianqi', age=77)\r\n\r\n        persons = Person.query(name='tianqi')\r\n        # select id, name, age from person where name = :name\r\n        # result:\r\n        # {'id': 7, 'name': 'tianqi', 'age': 77}\r\n\r\n        persons = Person.query(name__eq='zhangsan')\r\n        # select id, name, age from person where name = :name\r\n        # result:\r\n        # [{'id': 3, 'name': 'zhangsan', 'age': 15}]\r\n\r\nTransaction\r\n'''''''''''\r\n\r\n.. code:: python\r\n\r\n       from pgsqlx import with_transaction, transaction\r\n\r\n       @with_transaction\r\n       def test_transaction():\r\n           insert_func(....)\r\n           update_func(....)\r\n\r\n\r\n       def test_transaction2():\r\n           with transaction():\r\n               insert_func(....)\r\n               update_func(....)\r\n\r\n\r\nYou can generate model class with pgsqlx-generator: https://pypi.org/project/pgsqlx-generator\r\n\r\nIf you want to operate MySQL database, may be you need pgsqlx: https://pypi.org/project/mysqlx\r\n\r\nIf you just wanted a simple sql executor, may be you need sqlx-exec: https://pypi.org/project/sqlx-exec\r\n\r\nIf you wanted simultaneously support MySQL and PostgreSQL, may be you need sqlx-batis: https://pypi.org/project/sqlx-batis\r\n\r\n\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A thread safe sql executor for PostgreSQL like MyBatis with connection pool. It helps you automatically manage database connections and transactions. It also provides ORM operations for single tables.",
    "version": "2.2.0",
    "project_urls": {
        "Homepage": "https://gitee.com/summry/pgsqlx"
    },
    "split_keywords": [
        "sql",
        " postgresql",
        " mybatis",
        " python"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8b224868a6c5a593db542493c7cbf907437b36a097b0cbd9d038dca84b0dfa52",
                "md5": "e59deb67a4ed0fc47b1e3dc6788343e1",
                "sha256": "1883180639f55fb83a4943f0997af2fbc62076c6aeb8fd597ff283c7cf73d19b"
            },
            "downloads": -1,
            "filename": "pgsqlx-2.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "e59deb67a4ed0fc47b1e3dc6788343e1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.5",
            "size": 5186,
            "upload_time": "2024-05-04T10:37:18",
            "upload_time_iso_8601": "2024-05-04T10:37:18.447607Z",
            "url": "https://files.pythonhosted.org/packages/8b/22/4868a6c5a593db542493c7cbf907437b36a097b0cbd9d038dca84b0dfa52/pgsqlx-2.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-04 10:37:18",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "pgsqlx"
}
        
Elapsed time: 0.38544s