Name | MySQLX JSON |
Version |
2.2.8
JSON |
| download |
home_page | https://gitee.com/summry/mysqlx |
Summary | A thread safe sql executor for MySQL like MyBatis with connection pool. It helps you automatically manage database connections and transactions. |
upload_time | 2024-10-14 05:26:29 |
maintainer | None |
docs_url | None |
author | summy |
requires_python | >=3.5 |
license | None |
keywords |
sql
mysql
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
'user_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/mysqlx/blob/master/dtd/mapper.dtd">
<mapper namespace="user">
<select id="select_all">
select id, name, age from user
</select>
<select id="select_by_name">
select id, name, age from user where name = ?
</select>
<select id="select_by_name2">
select id, name, age from user 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 mysqlx.orm import Model
from typing import List, Tuple, Mapping
from mysqlx import mapper, sql, db, dbx, init_db
@mapper(namespace='user')
def select_all(): List
@mapper(namespace='user')
def select_by_name(name: str): List
@mapper(namespace='user')
def select_by_name2(name: str): List
@mapper(namespace='user')
def select_include(name: str): List
@sql('select id, name, age from user where name = ?')
def query_by_name(name: str): List(Mapping)
@sql('select id, name, age from user where name = :name')
def query_by_name2(name: str): List(Mapping)
if __name__ == '__main__':
init_db(host='127.0.0.1', port='3306', user='xxx', password='xxx', database='test', pool_size=5, show_sql=True, mapper_path='./mapper')
users = select_all()
# result:
# (3, 'zhangsan', 15)
# (4, 'lisi', 26)
# (5, 'wangwu', 38)
users = select_by_name('zhangsan')
# result:
# (3, 'zhangsan', 15)
users = select_by_name2(name='zhangsan')
# result:
# (3, 'zhangsan', 15)
users = select_include(name='zhangsan')
# result:
# (3, 'zhangsan', 15)
users = query_by_name('zhangsan')
# result:
# {'id': 3, 'name': 'zhangsan', 'age': 15}
users = query_by_name2(name='zhangsan')
# result:
# {'id': 3, 'name': 'zhangsan', 'age': 15}
# you can use dbx execte mapper sql with full sql id: namespace join sql id
users = dbx.select('user.select_all') # 'user' is namespace, 'select_all' is sql id
# result:
# (3, 'zhangsan', 15)
# (4, 'lisi', 26)
# (5, 'wangwu', 38)
users = dbx.select('user.select_by_name', name='zhangsan')
# result:
# (3, 'zhangsan', 15)
users = dbx.sql('user.select_by_name').select(name='zhangsan')
# result:
# (3, 'zhangsan', 15)
# you can direct execute sql with db
effected_rowcount = db.table('user').insert(name='zhangsan', age=15)
# 1
primary_key = db.table('user').save(name='lisi', age=26)
# 4
effected_rowcount = db.insert(table='user', name='wangwu', age=38)
# 1
users = db.table('user').columns('id, name, age').select()
# result:
# (3, 'zhangsan', 15)
# (4, 'lisi', 26)
# (5, 'wangwu', 38)
users = db.table('user').columns('id, name, age').where(name='zhangsan').query()
# result:
# [{'id': 3, 'name': 'zhangsan', 'age': 15}]
users = db.table('user').columns('id, name, age').where(name__eq='zhangsan').query()
# result:
# [{'id': 3, 'name': 'zhangsan', 'age': 15}]
users = db.select('select id, name, age from user')
# result:
# (3, 'zhangsan', 15)
# (4, 'lisi', 26)
# (5, 'wangwu', 38)
users = db.query('select id, name, age from user name = :name', name='zhangsan')
# result:
# [{'id': 3, 'name': 'zhangsan', 'age': 15}]
users = db.sql('select id, name, age from user name = :name').query(name='zhangsan')
# result:
# [{'id': 3, 'name': 'zhangsan', 'age': 15}]
Transaction
'''''''''''
.. code:: python
from mysqlx import trans
@trans
def test_transaction():
insert_func(....)
update_func(....)
def test_transaction2():
with trans():
insert_func(....)
update_func(....)
If you want to use ORM, may be you need SQLORMX: https://pypi.org/project/sqlormx
If you want to operate PostgreSQL database, may be you need PgSqlx: https://pypi.org/project/pgsqlx
If you just wanted a simple sql executor, may be you need SQLExecX: https://pypi.org/project/sqlexecx
If you wanted simultaneously support MySQL and PostgreSQL, may be you need BatisX: https://pypi.org/project/batisx
Raw data
{
"_id": null,
"home_page": "https://gitee.com/summry/mysqlx",
"name": "MySQLX",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.5",
"maintainer_email": null,
"keywords": "sql, MySQL, MyBatis, python",
"author": "summy",
"author_email": "xiazhongbiao@126.com",
"download_url": "https://files.pythonhosted.org/packages/bf/b6/16ed4cce30e7e917047bfba2828c9adb1278984b8af7a52dfa20b683901e/MySQLX-2.2.8.tar.gz",
"platform": null,
"description": "Mapper file\n'''''''''''\n\nCreate a mapper file in 'mapper' folder, you can named\n'user_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/mysqlx/blob/master/dtd/mapper.dtd\">\n <mapper namespace=\"user\">\n <select id=\"select_all\">\n select id, name, age from user\n </select>\n\n <select id=\"select_by_name\">\n select id, name, age from user where name = ?\n </select>\n\n <select id=\"select_by_name2\">\n select id, name, age from user 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 mysqlx.orm import Model\n from typing import List, Tuple, Mapping\n from mysqlx import mapper, sql, db, dbx, init_db\n\n @mapper(namespace='user')\n def select_all(): List\n\n @mapper(namespace='user')\n def select_by_name(name: str): List\n\n @mapper(namespace='user')\n def select_by_name2(name: str): List\n\n @mapper(namespace='user')\n def select_include(name: str): List\n\n @sql('select id, name, age from user where name = ?')\n def query_by_name(name: str): List(Mapping)\n\n @sql('select id, name, age from user where name = :name')\n def query_by_name2(name: str): List(Mapping)\n\n if __name__ == '__main__':\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 users = select_all()\n # result:\n # (3, 'zhangsan', 15)\n # (4, 'lisi', 26)\n # (5, 'wangwu', 38)\n\n users = select_by_name('zhangsan')\n # result:\n # (3, 'zhangsan', 15)\n\n users = select_by_name2(name='zhangsan')\n # result:\n # (3, 'zhangsan', 15)\n\n users = select_include(name='zhangsan')\n # result:\n # (3, 'zhangsan', 15)\n\n users = query_by_name('zhangsan')\n # result:\n # {'id': 3, 'name': 'zhangsan', 'age': 15}\n\n users = query_by_name2(name='zhangsan')\n # result:\n # {'id': 3, 'name': 'zhangsan', 'age': 15}\n \n # you can use dbx execte mapper sql with full sql id: namespace join sql id\n users = dbx.select('user.select_all') # 'user' is namespace, 'select_all' is sql id\n # result:\n # (3, 'zhangsan', 15)\n # (4, 'lisi', 26)\n # (5, 'wangwu', 38)\n\n users = dbx.select('user.select_by_name', name='zhangsan')\n # result:\n # (3, 'zhangsan', 15)\n\n users = dbx.sql('user.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('user').insert(name='zhangsan', age=15)\n # 1\n\n primary_key = db.table('user').save(name='lisi', age=26)\n # 4\n\n effected_rowcount = db.insert(table='user', name='wangwu', age=38)\n # 1\n\n users = db.table('user').columns('id, name, age').select()\n # result:\n # (3, 'zhangsan', 15)\n # (4, 'lisi', 26)\n # (5, 'wangwu', 38)\n\n users = db.table('user').columns('id, name, age').where(name='zhangsan').query()\n # result:\n # [{'id': 3, 'name': 'zhangsan', 'age': 15}]\n\n users = db.table('user').columns('id, name, age').where(name__eq='zhangsan').query()\n # result:\n # [{'id': 3, 'name': 'zhangsan', 'age': 15}]\n\n users = db.select('select id, name, age from user')\n # result:\n # (3, 'zhangsan', 15)\n # (4, 'lisi', 26)\n # (5, 'wangwu', 38)\n\n users = db.query('select id, name, age from user name = :name', name='zhangsan')\n # result:\n # [{'id': 3, 'name': 'zhangsan', 'age': 15}]\n\n users = db.sql('select id, name, age from user name = :name').query(name='zhangsan')\n # result:\n # [{'id': 3, 'name': 'zhangsan', 'age': 15}]\n\nTransaction\n'''''''''''\n\n.. code:: python\n\n from mysqlx 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\nIf you want to use ORM, may be you need SQLORMX: https://pypi.org/project/sqlormx\n\nIf you want to operate PostgreSQL database, may be you need PgSqlx: https://pypi.org/project/pgsqlx\n\nIf you just wanted a simple sql executor, may be you need SQLExecX: https://pypi.org/project/sqlexecx\n\nIf you wanted simultaneously support MySQL and PostgreSQL, may be you need BatisX: https://pypi.org/project/batisx\n\n\n",
"bugtrack_url": null,
"license": null,
"summary": "A thread safe sql executor for MySQL like MyBatis with connection pool. It helps you automatically manage database connections and transactions.",
"version": "2.2.8",
"project_urls": {
"Homepage": "https://gitee.com/summry/mysqlx"
},
"split_keywords": [
"sql",
" mysql",
" mybatis",
" python"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "bfb616ed4cce30e7e917047bfba2828c9adb1278984b8af7a52dfa20b683901e",
"md5": "110cd20088b6d0c514a654cf396d0220",
"sha256": "197c7b5d22c8becdc8d8a42aae25ed756182658fc604f472b645d6e445544510"
},
"downloads": -1,
"filename": "MySQLX-2.2.8.tar.gz",
"has_sig": false,
"md5_digest": "110cd20088b6d0c514a654cf396d0220",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.5",
"size": 13833,
"upload_time": "2024-10-14T05:26:29",
"upload_time_iso_8601": "2024-10-14T05:26:29.182747Z",
"url": "https://files.pythonhosted.org/packages/bf/b6/16ed4cce30e7e917047bfba2828c9adb1278984b8af7a52dfa20b683901e/MySQLX-2.2.8.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-14 05:26:29",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "mysqlx"
}