MySQLX 2.2.9__tar.gz → 2.3.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,16 +1,16 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: MySQLX
3
- Version: 2.2.9
3
+ Version: 2.3.1
4
4
  Summary: A thread safe sql executor for MySQL like MyBatis with connection pool. It helps you automatically manage database connections and transactions.
5
5
  Home-page: https://gitee.com/summry/mysqlx
6
6
  Author: summy
7
7
  Author-email: xiazhongbiao@126.com
8
- License: UNKNOWN
9
8
  Keywords: sql,MySQL,MyBatis,python
10
- Platform: UNKNOWN
11
9
  Requires-Python: >=3.5
12
10
  Description-Content-Type: text/markdown
13
11
  License-File: LICENSE
12
+ Requires-Dist: Jinja2>=2.7.0
13
+ Requires-Dist: sqlexecx>=2.2.4
14
14
 
15
15
  Mapper file
16
16
  '''''''''''
@@ -177,5 +177,3 @@ If you want to operate PostgreSQL database, may be you need PgSQLX: https://pypi
177
177
  If you just wanted a simple sql executor, may be you need SQLExecX: https://pypi.org/project/sqlexecx
178
178
 
179
179
  If you wanted simultaneously support MySQL and PostgreSQL, may be you need BatisX: https://pypi.org/project/batisx
180
-
181
-
@@ -0,0 +1,2 @@
1
+ Jinja2>=2.7.0
2
+ sqlexecx>=2.2.4
@@ -1,16 +1,16 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: MySQLX
3
- Version: 2.2.9
3
+ Version: 2.3.1
4
4
  Summary: A thread safe sql executor for MySQL like MyBatis with connection pool. It helps you automatically manage database connections and transactions.
5
5
  Home-page: https://gitee.com/summry/mysqlx
6
6
  Author: summy
7
7
  Author-email: xiazhongbiao@126.com
8
- License: UNKNOWN
9
8
  Keywords: sql,MySQL,MyBatis,python
10
- Platform: UNKNOWN
11
9
  Requires-Python: >=3.5
12
10
  Description-Content-Type: text/markdown
13
11
  License-File: LICENSE
12
+ Requires-Dist: Jinja2>=2.7.0
13
+ Requires-Dist: sqlexecx>=2.2.4
14
14
 
15
15
  Mapper file
16
16
  '''''''''''
@@ -177,5 +177,3 @@ If you want to operate PostgreSQL database, may be you need PgSQLX: https://pypi
177
177
  If you just wanted a simple sql executor, may be you need SQLExecX: https://pypi.org/project/sqlexecx
178
178
 
179
179
  If you wanted simultaneously support MySQL and PostgreSQL, may be you need BatisX: https://pypi.org/project/batisx
180
-
181
-
@@ -0,0 +1,75 @@
1
+ """
2
+ 可以设置`SQL_CACHE_SIZE`环境变量来设置缓存大小,默认是128。
3
+ linux or macos:
4
+ export SQL_CACHE_SIZE=256
5
+ windows:
6
+ set SQL_CACHE_SIZE=256
7
+
8
+ 可以设置`LRU_CACHE_SIZE`环境变量来设置缓存大小,默认是64。
9
+ linux or macos:
10
+ export LRU_CACHE_SIZE=128
11
+ windows:
12
+ set LRU_CACHE_SIZE=128
13
+ """
14
+
15
+ from sqlexecx import (
16
+ conn,
17
+ trans,
18
+ get_connection,
19
+ close,
20
+ Driver,
21
+ Dialect,
22
+ Engine,
23
+ init as _init
24
+ )
25
+
26
+ from .support import InitArgs
27
+ from .log_support import logger
28
+ from .sql_mapper import sql, mapper
29
+
30
+
31
+ def init_db(*args, **kwargs) -> Engine:
32
+ """
33
+ Compliant with the Python DB API 2.0 (PEP-249).
34
+
35
+ Addition parameters:
36
+ - mapper_path: str, path of mapper files
37
+ - driver=None: str|Driver, 'psycopg2' or 'pymysql' or 'mysql.connector' or 'sqlite3' or 'turso'
38
+ - pool_size=0: int, default 0, size of connection pool
39
+ - show_sql=False: bool, if True, print sql
40
+ - debug=False: bool, if True, print debug context
41
+
42
+ Other parameters of connection pool refer to DBUtils: https://webwareforpython.github.io/DBUtils/main.html#pooleddb-pooled-db
43
+
44
+ Examples
45
+ --------
46
+ >>> from mysqlx import db, trans, init_db
47
+ >>> init_db('db.sqlite3', mapper_path='./mapper', driver='sqlite3', debug=True)
48
+ Engine.SQLITE
49
+ >>> init_db(user='root', password='xxx', host='127.0.0.1', port=3306, database='testdb', driver='pymysql', mapper_path='./mapper')
50
+ Engine.MYSQL
51
+ """
52
+ from .sql_holder import load_mapper
53
+
54
+ # Dialect.init(Engine.MYSQL)
55
+ mapper_path = kwargs.pop(InitArgs.MAPPER_PATH) if InitArgs.MAPPER_PATH in kwargs else None
56
+ engine = _init(*args, **kwargs)
57
+ assert engine in (Engine.MYSQL, Engine.SQLITE, Engine.TURSO)
58
+ if mapper_path:
59
+ load_mapper(mapper_path)
60
+ return engine
61
+
62
+
63
+ __all__ = [
64
+ 'conn',
65
+ 'trans',
66
+ 'get_connection',
67
+ 'close',
68
+ 'Driver',
69
+ 'Dialect',
70
+ 'Engine',
71
+ 'init_db',
72
+ 'sql',
73
+ 'mapper'
74
+ ]
75
+
@@ -1,12 +1,13 @@
1
- from typing import List, Tuple
1
+ from typing import List, Tuple, Any
2
2
  from . import sql_support
3
3
  # Don't remove. Import for not repetitive implementation
4
4
  from sqlexecx import insert, save, batch_insert, batch_execute, do_execute, do_save_sql, do_get, do_query,\
5
5
  do_query_one, do_select, do_select_one, do_select_page, do_query_page, do_select_page, do_query_page, \
6
- load, do_load, insert_from_csv, insert_from_df, insert_from_json, truncate_table, drop_table, table
6
+ load, do_load, insert_from_csv, insert_from_df, insert_from_json, truncate_table, drop_table, table, \
7
+ do_select_first, do_query_first
7
8
 
8
9
 
9
- def execute(sql: str, *args, **kwargs):
10
+ def execute(sql: str, *args, **kwargs) -> int:
10
11
  """
11
12
  Execute sql return effect rowcount
12
13
 
@@ -24,7 +25,7 @@ def execute(sql: str, *args, **kwargs):
24
25
  return do_execute(sql, *args)
25
26
 
26
27
 
27
- def get(sql: str, *args, **kwargs):
28
+ def get(sql: str, *args, **kwargs) -> Any:
28
29
  """
29
30
  Execute select SQL and expected one int and only one int result, SQL contain 'limit'.
30
31
 
@@ -80,6 +81,24 @@ def query_one(sql: str, *args, **kwargs) -> dict:
80
81
  return do_query_one(sql, *args)
81
82
 
82
83
 
84
+ def query_first(sql: str, *args, **kwargs) -> dict:
85
+ """
86
+ Execute select SQL and return first result(dict).
87
+
88
+ Examples
89
+ --------
90
+ >>> from mysqlx import db
91
+ >>> sql = 'SELECT id, name, age FROM person WHERE name=?'
92
+ >>> db.query_first(sql, '张三')
93
+ {'id': 3, 'name': '张三', 'age': 20}
94
+ >>> sql = 'SELECT id, name, age FROM person WHERE name=:name'
95
+ >>> db.query_first(sql, name='张三')
96
+ {'id': 3, 'name': '张三', 'age': 20}
97
+ """
98
+ sql, args = sql_support.try_dynamic_sql('MySQLX.db.query_first', sql, *args, **kwargs)
99
+ return do_query_first(sql, *args)
100
+
101
+
83
102
  def select(sql: str, *args, **kwargs) -> List[Tuple]:
84
103
  """
85
104
  Execute select SQL and return list results(tuple).
@@ -116,6 +135,24 @@ def select_one(sql: str, *args, **kwargs) -> Tuple:
116
135
  return do_select_one(sql, *args)
117
136
 
118
137
 
138
+ def select_first(sql: str, *args, **kwargs) -> Tuple:
139
+ """
140
+ Execute select SQL and return first result(tuple).
141
+
142
+ Examples
143
+ --------
144
+ >>> from mysqlx import db
145
+ >>> sql = 'SELECT id, name, age FROM person WHERE name=?'
146
+ >>> db.select_first(sql, '张三')
147
+ (3, '张三', 20)
148
+ >>> sql = 'SELECT id, name, age FROM person WHERE name=:name'
149
+ >>> db.select_first(sql, name='张三')
150
+ (3, '张三', 20)
151
+ """
152
+ sql, args = sql_support.try_dynamic_sql('MySQLX.db.select_first', sql, *args, **kwargs)
153
+ return do_select_first(sql, *args)
154
+
155
+
119
156
  def query_page(sql: str, page_num=1, page_size=10, *args, **kwargs) -> List[dict]:
120
157
  """
121
158
  Execute select SQL and return list or empty list if no result.
@@ -1,10 +1,11 @@
1
+ from typing import Any, List, Tuple
1
2
  from . import sql_holder as holder
2
3
  from .sql_support import get_batch_args
3
4
  from .log_support import logger, sql_id_log, page_sql_id_log
4
5
  from . import db
5
6
 
6
7
 
7
- def save(sql_id: str, *args, **kwargs):
8
+ def save(sql_id: str, *args, **kwargs) -> Any:
8
9
  """
9
10
  Execute insert SQL, return primary key.
10
11
  :return: Primary key
@@ -14,7 +15,7 @@ def save(sql_id: str, *args, **kwargs):
14
15
  return db.do_save_sql(sql, *args)
15
16
 
16
17
 
17
- def execute(sql_id: str, *args, **kwargs):
18
+ def execute(sql_id: str, *args, **kwargs) -> int:
18
19
  """
19
20
  Execute SQL.
20
21
  sql: INSERT INTO person(name, age) VALUES(?, ?) --> args: ('张三', 20)
@@ -24,7 +25,7 @@ def execute(sql_id: str, *args, **kwargs):
24
25
  return db.do_execute(sql, *args)
25
26
 
26
27
 
27
- def batch_execute(sql_id: str, *args):
28
+ def batch_execute(sql_id: str, *args) -> int:
28
29
  """
29
30
  Batch execute
30
31
  sql: INSERT INTO person(name, age) VALUES(?, ?) --> args: [('张三', 20), ('李四', 28)]
@@ -39,7 +40,7 @@ def batch_execute(sql_id: str, *args):
39
40
 
40
41
 
41
42
  # ----------------------------------------------------------Query function------------------------------------------------------------------
42
- def get(sql_id: str, *args, **kwargs):
43
+ def get(sql_id: str, *args, **kwargs) -> Any:
43
44
  """
44
45
  Execute select SQL and expected one int and only one int result. Automatically add 'limit ?' behind the sql statement if not.
45
46
  MultiColumnsError: Expect only one column.
@@ -50,7 +51,7 @@ def get(sql_id: str, *args, **kwargs):
50
51
  return db.do_get(sql, *args)
51
52
 
52
53
 
53
- def query(sql_id: str, *args, **kwargs):
54
+ def query(sql_id: str, *args, **kwargs) -> List[dict]:
54
55
  """
55
56
  Execute select SQL and return list or empty list if no result.
56
57
  sql: SELECT * FROM person WHERE name=? and age=? --> args: ('张三', 20)
@@ -60,7 +61,7 @@ def query(sql_id: str, *args, **kwargs):
60
61
  return db.do_query(sql, *args)
61
62
 
62
63
 
63
- def query_one(sql_id: str, *args, **kwargs):
64
+ def query_one(sql_id: str, *args, **kwargs) -> dict:
64
65
  """
65
66
  Execute select SQL and expected one row result(dict). Automatically add 'limit ?' behind the sql statement if not.
66
67
  If no result found, return None.
@@ -72,7 +73,18 @@ def query_one(sql_id: str, *args, **kwargs):
72
73
  return db.do_query_one(sql, *args)
73
74
 
74
75
 
75
- def select(sql_id: str, *args, **kwargs):
76
+ def query_first(sql_id: str, *args, **kwargs) -> dict:
77
+ """
78
+ Execute select SQL and return first row result(dict).
79
+ If no result found, return None.
80
+ sql: SELECT * FROM person WHERE name=? --> args: ('张三',)
81
+ SELECT * FROM person WHERE name=:name --> kwargs: {'name': '张三'}
82
+ """
83
+ sql, args = _get_sql_args_from_id('query_first', sql_id, *args, **kwargs)
84
+ return db.do_query_first(sql, *args)
85
+
86
+
87
+ def select(sql_id: str, *args, **kwargs) -> List[Tuple]:
76
88
  """
77
89
  Execute select SQL and return list(tuple) or empty list if no result.
78
90
  sql: SELECT * FROM person WHERE name=? and age=? --> args: ('张三', 20)
@@ -82,7 +94,7 @@ def select(sql_id: str, *args, **kwargs):
82
94
  return db.do_select(sql, *args)
83
95
 
84
96
 
85
- def select_one(sql_id: str, *args, **kwargs):
97
+ def select_one(sql_id: str, *args, **kwargs) -> Tuple:
86
98
  """
87
99
  Execute select SQL and expected one row result(tuple). Automatically add 'limit ?' behind the sql statement if not.
88
100
  If no result found, return None.
@@ -94,7 +106,18 @@ def select_one(sql_id: str, *args, **kwargs):
94
106
  return db.do_select_one(sql, *args)
95
107
 
96
108
 
97
- def query_page(sql_id: str, page_num=1, page_size=10, *args, **kwargs):
109
+ def select_first(sql_id: str, *args, **kwargs) -> Tuple:
110
+ """
111
+ Execute select SQL and return first row result(tuple).
112
+ If no result found, return None.
113
+ sql: SELECT * FROM person WHERE name=? --> args: ('张三',)
114
+ SELECT * FROM person WHERE name=:name --> kwargs: {'name': '张三'}
115
+ """
116
+ sql, args = _get_sql_args_from_id('select_first', sql_id, *args, **kwargs)
117
+ return db.do_select_first(sql, *args)
118
+
119
+
120
+ def query_page(sql_id: str, page_num=1, page_size=10, *args, **kwargs) -> List[dict]:
98
121
  """
99
122
  Execute select SQL and return list or empty list if no result. Automatically add 'limit ?,?' after sql statement if not.
100
123
  sql: SELECT * FROM person WHERE name=? and age=? --> args: ('张三', 20)
@@ -105,7 +128,7 @@ def query_page(sql_id: str, page_num=1, page_size=10, *args, **kwargs):
105
128
  return db.do_query_page(sql, page_num, page_size, *args)
106
129
 
107
130
 
108
- def select_page(sql_id: str, page_num=1, page_size=10, *args, **kwargs):
131
+ def select_page(sql_id: str, page_num=1, page_size=10, *args, **kwargs) -> List[Tuple]:
109
132
  """
110
133
  Execute select SQL and return list or empty list if no result. Automatically add 'limit ?,?' after sql statement if not.
111
134
  sql: SELECT * FROM person WHERE name=? and age=? --> args: ('张三', 20)
@@ -64,11 +64,11 @@ def do_get_sql(sql_model, batch, param_names, *args, **kwargs):
64
64
  return sql_model.sql, args
65
65
 
66
66
 
67
- def build_sql_id(namespace, _id):
67
+ def build_sql_id(namespace, _id) -> str:
68
68
  return f'{namespace}.{_id}'
69
69
 
70
70
 
71
- def get_sql_model(sql_id: str):
71
+ def get_sql_model(sql_id: str) -> SqlModel:
72
72
  global _SQL_CONTAINER
73
73
  return _SQL_CONTAINER[sql_id]
74
74
 
@@ -1,3 +1,4 @@
1
+ from typing import Any
1
2
  from . import dbx
2
3
  from sqlexecx.sql_exec import SqlExec
3
4
  from sqlexecx.page_exec import PageExec
@@ -5,7 +6,7 @@ from sqlexecx.page_exec import PageExec
5
6
 
6
7
  class SqlIdExec(SqlExec):
7
8
 
8
- def save(self, *args, **kwargs):
9
+ def save(self, *args, **kwargs) -> Any:
9
10
  """
10
11
  Insert data into table, return primary key.
11
12
 
@@ -15,7 +16,7 @@ class SqlIdExec(SqlExec):
15
16
  return self.exec.save(self.sql, *args, **kwargs)
16
17
 
17
18
 
18
- def sql(sql_id: str):
19
+ def sql(sql_id: str) -> SqlIdExec:
19
20
  """
20
21
  Get a SqlIdExec instance
21
22
 
@@ -29,7 +30,7 @@ def sql(sql_id: str):
29
30
  return SqlIdExec(dbx, sql_id)
30
31
 
31
32
 
32
- def page(page_num: int, page_size: int):
33
+ def page(page_num: int, page_size: int) -> PageExec:
33
34
  """
34
35
  Get a PageExec instance
35
36
 
@@ -105,7 +105,7 @@ def get_select_func(func):
105
105
  return sqlexecx.do_query
106
106
 
107
107
 
108
- def before(func, namespace, _id, *args, **kwargs):
108
+ def before(func, namespace, _id, *args, **kwargs) -> (str, str):
109
109
  file_name = os.path.basename(func.__code__.co_filename)[:-3]
110
110
  _namespace = namespace if namespace else file_name
111
111
  _id = _id if _id else func.__name__
@@ -3,7 +3,7 @@ from sqlexecx.sql_exec import SqlExec
3
3
  from sqlexecx.page_exec import PageExec
4
4
 
5
5
 
6
- def sql(sql: str):
6
+ def sql(sql: str) -> SqlExec:
7
7
  """
8
8
  Get a SqlExec instance
9
9
 
@@ -17,7 +17,7 @@ def sql(sql: str):
17
17
  return SqlExec(db, sql)
18
18
 
19
19
 
20
- def page(page_num: int, page_size: int):
20
+ def page(page_num: int, page_size: int) -> PageExec:
21
21
  """
22
22
  Get a PageExec instance
23
23
 
@@ -8,7 +8,6 @@ from .constant import DYNAMIC_REGEX, SQL_CACHE_SIZE
8
8
  from sqlexecx.sql_support import get_named_sql_args, is_mapping, get_mapping_sql_args, try_mapping, get_batch_args
9
9
 
10
10
  sql_cache_size = int(os.getenv('SQL_CACHE_SIZE', SQL_CACHE_SIZE))
11
- print(f'======== SQL_CACHE_SIZE: {sql_cache_size}')
12
11
 
13
12
 
14
13
  def simple_sql(sql: str, *args, **kwargs):
@@ -53,3 +52,4 @@ def try_page_mapping(function, sql, page_num, page_size, *args, **kwargs):
53
52
  page_log(function, sql, page_num, page_size, *args, **kwargs)
54
53
  sql = dynamic_sql(sql, **kwargs)
55
54
  return get_mapping_sql_args(sql, *args, **kwargs)
55
+
@@ -34,3 +34,4 @@ class InitArgs(_InitArgs):
34
34
  MAPPER_PATH = 'mapper_path'
35
35
  """
36
36
  MAPPER_PATH = 'mapper_path'
37
+
@@ -19,9 +19,9 @@ setup(
19
19
  long_description_content_type='text/markdown',
20
20
  install_requires=[
21
21
  'Jinja2>=2.7.0',
22
- 'sqlexecx>=2.1.9',
22
+ 'sqlexecx>=2.2.4',
23
23
  ],
24
- version='2.2.9',
24
+ version='2.3.1',
25
25
  url='https://gitee.com/summry/mysqlx',
26
26
  author='summy',
27
27
  author_email='xiazhongbiao@126.com',
@@ -1,2 +0,0 @@
1
- Jinja2>=2.7.0
2
- sqlexecx>=2.1.9
@@ -1,42 +0,0 @@
1
- from sqlexecx import (
2
- conn,
3
- trans,
4
- get_connection,
5
- close,
6
- Driver,
7
- Dialect,
8
- Engine,
9
- init as _init
10
- )
11
-
12
- from .support import InitArgs
13
- from .sql_mapper import sql, mapper
14
-
15
-
16
- def init_db(*args, **kwargs):
17
- """
18
- Compliant with the Python DB API 2.0 (PEP-249).
19
-
20
- from mysqlx import init_db
21
- init_db('test.db', driver='sqlite3', show_sql=True, debug=True)
22
- or
23
- init_db("postgres://user:password@127.0.0.1:5432/testdb", mapper_path='./mapper', driver='psycopg2', pool_size=5, show_sql=True, debug=True)
24
- or
25
- init_db(user='root', password='xxx', host='127.0.0.1', port=3306, database='testdb', mapper_path='./mapper', driver='pymysql', pool_size=5, show_sql=True, debug=True)
26
-
27
- Addition parameters:
28
- :param mapper_path: str, path of mapper files
29
- :param driver=None: str, import driver, 'import pymysql'
30
- :param pool_size=0: int, default 0, size of connection pool
31
- :param show_sql=False: bool, if True, print sql
32
- :param debug=False: bool, if True, print debug context
33
-
34
- Other parameters of connection pool refer to DBUtils: https://webwareforpython.github.io/DBUtils/main.html#pooleddb-pooled-db
35
- """
36
- from .sql_holder import load_mapper
37
-
38
- # Dialect.init(Engine.MYSQL)
39
- mapper_path = kwargs.pop(InitArgs.MAPPER_PATH) if InitArgs.MAPPER_PATH in kwargs else None
40
- _init(*args, **kwargs)
41
- if mapper_path:
42
- load_mapper(mapper_path)
File without changes
File without changes
File without changes
File without changes
File without changes