Name | BatisX JSON |
Version |
2.2.4
JSON |
| download |
home_page | https://gitee.com/summry/batisx |
Summary | A easy thread safe sql executor for Python like MyBatis with connection pool. It helps you automatically manage database connections and transactions. Support MySQL, PostgreSQL, SQLite etc. |
upload_time | 2024-10-14 05:29:03 |
maintainer | None |
docs_url | None |
author | summy |
requires_python | >=3.5 |
license | None |
keywords |
sql
mysql
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/batisx/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 batisx 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.table('person').insert(name='zhangsan', age=15)
# 1
primary_key = db.table('person').save(name='lisi', age=26)
# 4
effected_rowcount = db.insert(table='person', name='wangwu', age=38)
# 1
users = db.table('person').columns('id, name, age').select()
# result:
# (3, 'zhangsan', 15)
# (4, 'lisi', 26)
# (5, 'wangwu', 38)
users = db.table('person').columns('id, name, age').where(name='zhangsan').query()
# result:
# [{'id': 3, 'name': 'zhangsan', 'age': 15}]
users = db.table('person').columns('id, name, age').where(name__eq='zhangsan').query()
# result:
# [{'id': 3, 'name': 'zhangsan', 'age': 15}]
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 batisx import trans
@trans
def test_transaction():
insert_func(....)
update_func(....)
def test_transaction2():
with trans():
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/sqlexecx
Raw data
{
"_id": null,
"home_page": "https://gitee.com/summry/batisx",
"name": "BatisX",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.5",
"maintainer_email": null,
"keywords": "sql, MySQL, PostgreSQL, MyBatis, python",
"author": "summy",
"author_email": "xiazhongbiao@126.com",
"download_url": "https://files.pythonhosted.org/packages/ed/40/3745213043ec04cf832afc4f711c552a2309a51282fc6cdf13eaffbe02f1/BatisX-2.2.4.tar.gz",
"platform": null,
"description": "Mapper file\n'''''''''''\n\nCreate a mapper file in 'mapper' folder, you can named\n'person_mapper.xml', like follow:\n\n.. code:: xml\n\n <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n <!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"https://gitee.com/summry/batisx/blob/master/dtd/mapper.dtd\">\n <mapper namespace=\"person\">\n <select id=\"select_all\">\n select id, name, age from person\n </select>\n\n <select id=\"select_by_name\">\n select id, name, age from person where name = ?\n </select>\n\n <select id=\"select_by_name2\">\n select id, name, age from person where name = :name\n </select>\n\n <select id=\"select_include\" include=\"select_all\">\n {{ select_all }}\n {% if name -%}\n where name = :name\n {%- endif -%}\n </select>\n </mapper>\n\nUsage Sample\n''''''''''''\n\n.. code:: python\n\n from typing import List, Tuple, Mapping\n from batisx import mapper, sql, dbx, init_db\n\n @mapper(namespace='person')\n def select_all(): List\n\n @mapper(namespace='person')\n def select_by_name(name: str): List\n\n @mapper(namespace='person')\n def select_by_name2(name: str): List\n\n @mapper(namespace='person')\n def select_include(name: str): List\n\n @sql('select id, name, age from person where name = ?')\n def query_by_name(name: str): List(Mapping)\n\n @sql('select id, name, age from person where name = :name')\n def query_by_name2(name: str): List(Mapping)\n\n if __name__ == '__main__':\n # init_db('test.db', driver='sqlite3', show_sql=True, debug=True, mapper_path='./mapper')\n # init_db(\"postgres://user:password@127.0.0.1:5432/testdb\", driver='psycopg2', pool_size=5, mapper_path='./mapper')\n init_db(host='127.0.0.1', port='3306', user='xxx', password='xxx', database='test', pool_size=5, show_sql=True, mapper_path='./mapper')\n\n persons = select_all()\n # result:\n # (3, 'zhangsan', 15)\n # (4, 'lisi', 26)\n # (5, 'wangwu', 38)\n\n persons = select_by_name('zhangsan')\n # result:\n # (3, 'zhangsan', 15)\n\n persons = select_by_name2(name='zhangsan')\n # result:\n # (3, 'zhangsan', 15)\n\n persons = select_include(name='zhangsan')\n # result:\n # (3, 'zhangsan', 15)\n\n persons = query_by_name('zhangsan')\n # result:\n # {'id': 3, 'name': 'zhangsan', 'age': 15}\n\n persons = query_by_name2(name='zhangsan')\n # result:\n # {'id': 3, 'name': 'zhangsan', 'age': 15}\n \n # you can use dbx execute mapper sql with full sql id: namespace join sql id\n persons = dbx.select('person.select_all') # 'person' is namespace, 'select_all' is sql id\n # result:\n # (3, 'zhangsan', 15)\n # (4, 'lisi', 26)\n # (5, 'wangwu', 38)\n\n persons = dbx.select('person.select_by_name', name='zhangsan')\n # result:\n # (3, 'zhangsan', 15)\n\n persons = dbx.sql('person.select_by_name').select(name='zhangsan')\n # result:\n # (3, 'zhangsan', 15)\n\n # you can direct execute sql with db\n effected_rowcount = db.table('person').insert(name='zhangsan', age=15)\n # 1\n\n primary_key = db.table('person').save(name='lisi', age=26)\n # 4\n\n effected_rowcount = db.insert(table='person', name='wangwu', age=38)\n # 1\n\n users = db.table('person').columns('id, name, age').select()\n # result:\n # (3, 'zhangsan', 15)\n # (4, 'lisi', 26)\n # (5, 'wangwu', 38)\n\n users = db.table('person').columns('id, name, age').where(name='zhangsan').query()\n # result:\n # [{'id': 3, 'name': 'zhangsan', 'age': 15}]\n\n users = db.table('person').columns('id, name, age').where(name__eq='zhangsan').query()\n # result:\n # [{'id': 3, 'name': 'zhangsan', 'age': 15}]\n\n persons = db.select('select id, name, age from person')\n # result:\n # (3, 'zhangsan', 15)\n # (4, 'lisi', 26)\n # (5, 'wangwu', 38)\n # (6, 'zhaoliu', 45)\n\n persons = db.query('select id, name, age from person name = :name', name='zhangsan')\n # result:\n # [{'id': 3, 'name': 'zhangsan', 'age': 15}]\n\n persons = db.sql('select id, name, age from person name = :name').query(name='zhangsan')\n # result:\n # [{'id': 3, 'name': 'zhangsan', 'age': 15}]\n\n persons = db.select('select id, name, age from person where name = ?', 'zhangsan')\n # result:\n # [(3, 'zhangsan', 15)]\n\n\nTransaction\n'''''''''''\n\n.. code:: python\n\n from batisx import trans\n\n @trans\n def test_transaction():\n insert_func(....)\n update_func(....)\n\n\n def test_transaction2():\n with trans():\n insert_func(....)\n update_func(....)\n\n\nIf you want to operate MySQL database, may be you need MySqlx: https://pypi.org/project/mysqlx\n\nIf you want to operate PostgreSQL database, may be you need MySqlx: https://pypi.org/project/pgsqlx\n\nIf you just wanted a simple sql executor, may be you need sqlx-exec: https://pypi.org/project/sqlexecx\n\n\n",
"bugtrack_url": null,
"license": null,
"summary": "A easy thread safe sql executor for Python like MyBatis with connection pool. It helps you automatically manage database connections and transactions. Support MySQL, PostgreSQL, SQLite etc.",
"version": "2.2.4",
"project_urls": {
"Homepage": "https://gitee.com/summry/batisx"
},
"split_keywords": [
"sql",
" mysql",
" postgresql",
" mybatis",
" python"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ed403745213043ec04cf832afc4f711c552a2309a51282fc6cdf13eaffbe02f1",
"md5": "d61101c054d3559c0d1fbd3752517c4a",
"sha256": "5fa337f5491db7a13eb107714b9e3d40f8d2f324876fabe7e71c5b8aca8db6df"
},
"downloads": -1,
"filename": "BatisX-2.2.4.tar.gz",
"has_sig": false,
"md5_digest": "d61101c054d3559c0d1fbd3752517c4a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.5",
"size": 9519,
"upload_time": "2024-10-14T05:29:03",
"upload_time_iso_8601": "2024-10-14T05:29:03.696770Z",
"url": "https://files.pythonhosted.org/packages/ed/40/3745213043ec04cf832afc4f711c552a2309a51282fc6cdf13eaffbe02f1/BatisX-2.2.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-14 05:29:03",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "batisx"
}