SQLExecX 2.2.4__tar.gz → 2.2.6__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.
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/PKG-INFO +2 -2
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/SQLExecX.egg-info/PKG-INFO +2 -2
- SQLExecX-2.2.6/SQLExecX.egg-info/requires.txt +1 -0
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/setup.py +2 -2
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/sqlexecx/__init__.py +5 -2
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/sqlexecx/dialect.py +46 -26
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/sqlexecx/exec.py +17 -15
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/sqlexecx/loader.py +1 -0
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/sqlexecx/sql_exec.py +3 -3
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/sqlexecx/sql_support.py +2 -2
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/sqlexecx/table_exec.py +21 -20
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/sqlexecx/table_limit_exec.py +35 -35
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/sqlexecx/table_order_by.py +61 -46
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/sqlexecx/table_page_exec.py +56 -54
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/sqlexecx/table_support.py +17 -16
- SQLExecX-2.2.4/SQLExecX.egg-info/requires.txt +0 -1
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/LICENSE +0 -0
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/README.rst +0 -0
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/SQLExecX.egg-info/SOURCES.txt +0 -0
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/SQLExecX.egg-info/dependency_links.txt +0 -0
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/SQLExecX.egg-info/not-zip-safe +0 -0
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/SQLExecX.egg-info/top_level.txt +0 -0
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/setup.cfg +0 -0
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/sqlexecx/constant.py +0 -0
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/sqlexecx/log_support.py +0 -0
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/sqlexecx/page_exec.py +0 -0
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/sqlexecx/utils.py +0 -0
- {SQLExecX-2.2.4 → SQLExecX-2.2.6}/test/test.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: SQLExecX
|
|
3
|
-
Version: 2.2.
|
|
3
|
+
Version: 2.2.6
|
|
4
4
|
Summary: A easy, fast sql executor for Python. Supported MySQL, PostgreSQL, SQLite etc.
|
|
5
5
|
Home-page: https://gitee.com/summry/sqlexecx
|
|
6
6
|
Author: summy
|
|
@@ -9,7 +9,7 @@ Keywords: SQL,MySQL,PostgreSQL,SQLite,Database,Python,RDB
|
|
|
9
9
|
Requires-Python: >=3.6
|
|
10
10
|
Description-Content-Type: text/markdown
|
|
11
11
|
License-File: LICENSE
|
|
12
|
-
Requires-Dist: sqlexecutorx>=1.
|
|
12
|
+
Requires-Dist: sqlexecutorx>=1.9.1
|
|
13
13
|
|
|
14
14
|
Usage Sample
|
|
15
15
|
''''''''''''
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: SQLExecX
|
|
3
|
-
Version: 2.2.
|
|
3
|
+
Version: 2.2.6
|
|
4
4
|
Summary: A easy, fast sql executor for Python. Supported MySQL, PostgreSQL, SQLite etc.
|
|
5
5
|
Home-page: https://gitee.com/summry/sqlexecx
|
|
6
6
|
Author: summy
|
|
@@ -9,7 +9,7 @@ Keywords: SQL,MySQL,PostgreSQL,SQLite,Database,Python,RDB
|
|
|
9
9
|
Requires-Python: >=3.6
|
|
10
10
|
Description-Content-Type: text/markdown
|
|
11
11
|
License-File: LICENSE
|
|
12
|
-
Requires-Dist: sqlexecutorx>=1.
|
|
12
|
+
Requires-Dist: sqlexecutorx>=1.9.1
|
|
13
13
|
|
|
14
14
|
Usage Sample
|
|
15
15
|
''''''''''''
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
sqlexecutorx>=1.9.1
|
|
@@ -17,9 +17,9 @@ setup(
|
|
|
17
17
|
description="A easy, fast sql executor for Python. Supported MySQL, PostgreSQL, SQLite etc.",
|
|
18
18
|
long_description=long_description,
|
|
19
19
|
long_description_content_type='text/markdown',
|
|
20
|
-
version='2.2.
|
|
20
|
+
version='2.2.6',
|
|
21
21
|
install_requires=[
|
|
22
|
-
'sqlexecutorx>=1.
|
|
22
|
+
'sqlexecutorx>=1.9.1',
|
|
23
23
|
],
|
|
24
24
|
url='https://gitee.com/summry/sqlexecx',
|
|
25
25
|
author='summy',
|
|
@@ -26,7 +26,7 @@ export LRU_CACHE_SIZE=128
|
|
|
26
26
|
windows:
|
|
27
27
|
set LRU_CACHE_SIZE=128
|
|
28
28
|
"""
|
|
29
|
-
|
|
29
|
+
from typing import Optional
|
|
30
30
|
from sqlexecutorx import (
|
|
31
31
|
conn,
|
|
32
32
|
trans,
|
|
@@ -86,7 +86,7 @@ from .dialect import Dialect
|
|
|
86
86
|
from .table_exec import table
|
|
87
87
|
|
|
88
88
|
|
|
89
|
-
def init(*args, **kwargs) -> Engine:
|
|
89
|
+
def init(*args, **kwargs) -> Optional[Engine]:
|
|
90
90
|
"""
|
|
91
91
|
Compliant with the Python DB API 2.0 (PEP-249).
|
|
92
92
|
|
|
@@ -109,5 +109,8 @@ def init(*args, **kwargs) -> Engine:
|
|
|
109
109
|
"""
|
|
110
110
|
|
|
111
111
|
engine = _init(*args, **kwargs)
|
|
112
|
+
if engine is None:
|
|
113
|
+
return None
|
|
114
|
+
|
|
112
115
|
Dialect.init(engine)
|
|
113
116
|
return engine
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
|
|
4
4
|
import re
|
|
5
5
|
from functools import lru_cache
|
|
6
|
-
from
|
|
6
|
+
from reprlib import aRepr
|
|
7
|
+
from typing import Sequence, Collection, Union, Optional, List
|
|
7
8
|
from sqlexecutorx.sql_support import require_limit
|
|
8
9
|
from sqlexecutorx import execute, get, query, select, DBError, Engine
|
|
9
10
|
from sqlexecutorx.conf import lru_cache_size
|
|
@@ -30,7 +31,7 @@ class BaseDialect:
|
|
|
30
31
|
self.engine = engine
|
|
31
32
|
|
|
32
33
|
@staticmethod
|
|
33
|
-
def create_insert_sql(table_name: str, cols:
|
|
34
|
+
def create_insert_sql(table_name: str, cols: Sequence[str], placeholder: str = '%s') -> str:
|
|
34
35
|
columns, placeholders = zip(*[('{}'.format(col), placeholder) for col in cols])
|
|
35
36
|
return f"INSERT INTO {table_name}({', '.join(columns)}) VALUES({','.join(placeholders)})"
|
|
36
37
|
|
|
@@ -53,6 +54,7 @@ class BaseDialect:
|
|
|
53
54
|
return f'{sql} LIMIT ? OFFSET ?'
|
|
54
55
|
|
|
55
56
|
@staticmethod
|
|
57
|
+
@lru_cache(maxsize=lru_cache_size)
|
|
56
58
|
def get_table_columns(table_name: str) -> str:
|
|
57
59
|
return '*'
|
|
58
60
|
|
|
@@ -63,16 +65,20 @@ class BaseDialect:
|
|
|
63
65
|
@staticmethod
|
|
64
66
|
@lru_cache(maxsize=lru_cache_size)
|
|
65
67
|
def get_dialect_str(src: Union[str, int, Collection[str]]) -> str:
|
|
66
|
-
assert
|
|
67
|
-
|
|
68
|
+
assert src, 'src is required.'
|
|
69
|
+
if isinstance(src, str):
|
|
70
|
+
return src.strip()
|
|
71
|
+
elif isinstance(src, int):
|
|
72
|
+
return str(src)
|
|
73
|
+
return ','.join([s.strip() for s in src])
|
|
68
74
|
|
|
69
75
|
@staticmethod
|
|
70
76
|
def get_select_key(*args, **kwargs):
|
|
71
77
|
raise NotImplementedError("Not implement method 'get_select_key', you can use sqlormx snowflake for primary key.")
|
|
72
78
|
|
|
73
79
|
@staticmethod
|
|
74
|
-
def show_tables(schema: Optional[str]):
|
|
75
|
-
|
|
80
|
+
def show_tables(schema: Optional[str] = None) -> List[str]:
|
|
81
|
+
raise NotImplementedError
|
|
76
82
|
|
|
77
83
|
|
|
78
84
|
class MySQLDialect(BaseDialect):
|
|
@@ -107,12 +113,12 @@ class MySQLDialect(BaseDialect):
|
|
|
107
113
|
@lru_cache(maxsize=lru_cache_size)
|
|
108
114
|
def get_dialect_str(src: Union[str, int, Collection[str]]) -> str:
|
|
109
115
|
assert src, 'src is required.'
|
|
110
|
-
if isinstance(src, str):
|
|
116
|
+
if isinstance(src, (str, int)):
|
|
111
117
|
return _dialect_str(src)
|
|
112
118
|
return ','.join([_dialect_str(arg) for arg in src])
|
|
113
119
|
|
|
114
120
|
@staticmethod
|
|
115
|
-
def show_tables(schema: Optional[str]):
|
|
121
|
+
def show_tables(schema: Optional[str] = None) -> List[str]:
|
|
116
122
|
if schema:
|
|
117
123
|
execute(f'use {schema}')
|
|
118
124
|
return [table[0] for table in select('show tables')]
|
|
@@ -126,36 +132,40 @@ class PostgresDialect(BaseDialect):
|
|
|
126
132
|
return get(POSTGRES_COLUMN_SQL, table_name, LIMIT_1)
|
|
127
133
|
|
|
128
134
|
@staticmethod
|
|
129
|
-
def get_select_key(key_seq: str = None, table_name: str = None, key: str =None, sql: str = None) -> str:
|
|
135
|
+
def get_select_key(key_seq: Optional[str] = None, table_name: Optional[str] = None, key: Optional[str] = None, sql: Optional[str] = None) -> str:
|
|
130
136
|
if not key_seq:
|
|
131
137
|
if table_name:
|
|
132
138
|
key_seq = PostgresDialect.build_key_seq(table_name, key)
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
139
|
+
elif sql:
|
|
140
|
+
key_seq = PostgresDialect._get_key_seq_from_sql(sql)
|
|
141
|
+
|
|
142
|
+
if not key_seq:
|
|
143
|
+
raise DBError("Get PostgreSQL select key fail, all of 'key_seq', 'table', 'sql' are None")
|
|
144
|
+
|
|
138
145
|
return f"SELECT currval('{key_seq}')"
|
|
139
146
|
|
|
140
147
|
@staticmethod
|
|
141
|
-
def build_key_seq(table_name: str, key: str = None) -> str:
|
|
148
|
+
def build_key_seq(table_name: str, key: Optional[str] = None) -> str:
|
|
142
149
|
if not key:
|
|
143
150
|
key = DEFAULT_KEY_FIELD
|
|
144
151
|
return f'{table_name}_{key}_seq'
|
|
145
152
|
|
|
146
153
|
@staticmethod
|
|
147
154
|
@lru_cache(maxsize=lru_cache_size)
|
|
148
|
-
def _get_key_seq_from_sql(sql: str) -> str:
|
|
155
|
+
def _get_key_seq_from_sql(sql: str) -> Optional[str]:
|
|
149
156
|
table_name = re.search('(?<=into )\w+', sql, re.I)
|
|
157
|
+
if not table_name:
|
|
158
|
+
return None
|
|
159
|
+
|
|
150
160
|
key_seq = PostgresDialect.build_key_seq(table_name.group())
|
|
151
161
|
logger.warning("'key_seq' is None, will use default '{}' from sql.".format(key_seq))
|
|
152
162
|
return key_seq
|
|
153
163
|
|
|
154
164
|
@staticmethod
|
|
155
|
-
def show_tables(schema: Optional[str]):
|
|
165
|
+
def show_tables(schema: Optional[str] = None) -> List[str]:
|
|
156
166
|
sql = '''
|
|
157
|
-
SELECT tablename FROM pg_catalog.pg_tables
|
|
158
|
-
WHERE schemaname != 'pg_catalog'
|
|
167
|
+
SELECT tablename FROM pg_catalog.pg_tables
|
|
168
|
+
WHERE schemaname != 'pg_catalog'
|
|
159
169
|
AND schemaname != 'information_schema'
|
|
160
170
|
'''
|
|
161
171
|
if schema:
|
|
@@ -182,7 +192,7 @@ class OracleDialect(BaseDialect):
|
|
|
182
192
|
|
|
183
193
|
|
|
184
194
|
class SQLiteDialect(MySQLDialect):
|
|
185
|
-
|
|
195
|
+
|
|
186
196
|
@staticmethod
|
|
187
197
|
def create_insert_sql(table_name: str, cols: Sequence[str], placeholder: str = '?') -> str:
|
|
188
198
|
return MySQLDialect.create_insert_sql(table_name, cols, placeholder)
|
|
@@ -208,7 +218,7 @@ class SQLiteDialect(MySQLDialect):
|
|
|
208
218
|
return execute(f'DELETE FROM `{table_name}`')
|
|
209
219
|
|
|
210
220
|
@staticmethod
|
|
211
|
-
def show_tables(schema: Optional[str]):
|
|
221
|
+
def show_tables(schema: Optional[str] = None) -> List[str]:
|
|
212
222
|
return [table[0] for table in select("SELECT name FROM sqlite_master WHERE type='table'")]
|
|
213
223
|
|
|
214
224
|
|
|
@@ -234,49 +244,59 @@ class Dialect:
|
|
|
234
244
|
|
|
235
245
|
@staticmethod
|
|
236
246
|
@lru_cache(maxsize=lru_cache_size)
|
|
237
|
-
def create_insert_sql(table_name: str, cols:
|
|
247
|
+
def create_insert_sql(table_name: str, cols: Sequence[str]) -> str:
|
|
248
|
+
assert _DIALECT, 'Dialect not initialized.'
|
|
238
249
|
return _DIALECT.create_insert_sql(table_name, cols)
|
|
239
250
|
|
|
240
251
|
@staticmethod
|
|
241
252
|
@lru_cache(maxsize=lru_cache_size)
|
|
242
253
|
def before_execute(sql: str) -> str:
|
|
254
|
+
assert _DIALECT, 'Dialect not initialized.'
|
|
243
255
|
return _DIALECT.before_execute(sql)
|
|
244
256
|
|
|
245
257
|
@staticmethod
|
|
246
258
|
def get_page_sql_args(sql: str, page_num: int, page_size: int, *args):
|
|
259
|
+
assert _DIALECT, 'Dialect not initialized.'
|
|
247
260
|
return _DIALECT.get_page_sql_args(sql, page_num, page_size, *args)
|
|
248
|
-
|
|
261
|
+
|
|
249
262
|
@staticmethod
|
|
250
263
|
def limit_offset_sql(sql: str):
|
|
264
|
+
assert _DIALECT, 'Dialect not initialized.'
|
|
251
265
|
return _DIALECT.limit_offset_sql(sql)
|
|
252
266
|
|
|
253
267
|
@staticmethod
|
|
254
268
|
def get_table_columns(table_name: str) -> str:
|
|
269
|
+
assert _DIALECT, 'Dialect not initialized.'
|
|
255
270
|
return _DIALECT.get_table_columns(table_name)
|
|
256
271
|
|
|
257
272
|
@staticmethod
|
|
258
273
|
def truncate_table(table_name: str) -> int:
|
|
274
|
+
assert _DIALECT, 'Dialect not initialized.'
|
|
259
275
|
return _DIALECT.truncate_table(table_name)
|
|
260
276
|
|
|
261
277
|
@staticmethod
|
|
262
278
|
def get_select_key(*args, **kwargs) -> str:
|
|
279
|
+
assert _DIALECT, 'Dialect not initialized.'
|
|
263
280
|
return _DIALECT.get_select_key(*args, **kwargs)
|
|
264
281
|
|
|
265
282
|
@staticmethod
|
|
266
283
|
def get_dialect_str(src: Union[str, int, Collection[str]]) -> str:
|
|
284
|
+
assert _DIALECT, 'Dialect not initialized.'
|
|
267
285
|
return _DIALECT.get_dialect_str(src)
|
|
268
286
|
|
|
269
287
|
@staticmethod
|
|
270
288
|
def curr_engine() -> Engine:
|
|
289
|
+
assert _DIALECT, 'Dialect not initialized.'
|
|
271
290
|
return _DIALECT.engine
|
|
272
291
|
|
|
273
292
|
@staticmethod
|
|
274
|
-
def show_tables(schema: Optional[str]):
|
|
293
|
+
def show_tables(schema: Optional[str] = None) -> List[str]:
|
|
294
|
+
assert _DIALECT, 'Dialect not initialized.'
|
|
275
295
|
return _DIALECT.show_tables(schema)
|
|
276
296
|
|
|
277
297
|
|
|
278
|
-
def _dialect_str(src: Union[str, int]):
|
|
279
|
-
assert isinstance(src, str
|
|
298
|
+
def _dialect_str(src: Union[str, int]) -> str:
|
|
299
|
+
assert isinstance(src, (str, int)), 'source must be a string or int.'
|
|
280
300
|
if isinstance(src, int):
|
|
281
301
|
return str(src)
|
|
282
302
|
elif '(' in src or ' as ' in src.lower():
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from typing import Collection, Iterable, List, Tuple, Mapping, Any
|
|
1
|
+
from typing import Collection, Iterable, List, Tuple, Mapping, Any, Optional
|
|
2
2
|
from .loader import Loader
|
|
3
3
|
from .dialect import Dialect
|
|
4
4
|
from . import sql_support
|
|
@@ -62,7 +62,7 @@ def save(table_name: str, **kwargs) -> Any:
|
|
|
62
62
|
try:
|
|
63
63
|
select_key = Dialect.get_select_key(table_name=table_name)
|
|
64
64
|
except NotImplementedError:
|
|
65
|
-
raise DBError(
|
|
65
|
+
raise DBError("Expect 'select_key' but not. you may should use 'save_select_key' func with 'select_key'.")
|
|
66
66
|
return save_select_key(select_key, table_name, **kwargs)
|
|
67
67
|
|
|
68
68
|
|
|
@@ -84,7 +84,7 @@ def save_sql(sql: str, *args, **kwargs) -> Any:
|
|
|
84
84
|
try:
|
|
85
85
|
select_key = Dialect.get_select_key(sql=sql)
|
|
86
86
|
except NotImplementedError:
|
|
87
|
-
raise DBError(
|
|
87
|
+
raise DBError("Expect 'select_key' but not. you may should use 'save_sql_select_key' func with 'select_key'.")
|
|
88
88
|
return save_sql_select_key(select_key, sql, *args, **kwargs)
|
|
89
89
|
|
|
90
90
|
|
|
@@ -174,7 +174,7 @@ def select(sql: str, *args, **kwargs) -> List[Tuple]:
|
|
|
174
174
|
return do_select(sql, *args)
|
|
175
175
|
|
|
176
176
|
|
|
177
|
-
def select_one(sql: str, *args, **kwargs) -> Tuple:
|
|
177
|
+
def select_one(sql: str, *args, **kwargs) -> Optional[Tuple[Any, ...]]:
|
|
178
178
|
"""
|
|
179
179
|
Execute select SQL and return unique result(tuple), SQL contain 'limit'.
|
|
180
180
|
|
|
@@ -212,7 +212,7 @@ def query(sql: str, *args, **kwargs) -> List[dict]:
|
|
|
212
212
|
return do_query(sql, *args)
|
|
213
213
|
|
|
214
214
|
|
|
215
|
-
def query_one(sql: str, *args, **kwargs) -> dict:
|
|
215
|
+
def query_one(sql: str, *args, **kwargs) -> Optional[dict]:
|
|
216
216
|
"""
|
|
217
217
|
Execute select SQL and return unique result(dict), SQL contain 'limit'.
|
|
218
218
|
|
|
@@ -266,7 +266,7 @@ def query_first(sql: str, *args, **kwargs) -> dict:
|
|
|
266
266
|
return do_query_first(sql, *args)
|
|
267
267
|
|
|
268
268
|
|
|
269
|
-
def ravel_list(sql: str, *args, position: int = 0, **kwargs) -> List:
|
|
269
|
+
def ravel_list(sql: str, *args, position: int = 0, **kwargs) -> List[Any]:
|
|
270
270
|
"""
|
|
271
271
|
Execute select SQL and return list.
|
|
272
272
|
|
|
@@ -322,7 +322,7 @@ def do_save_sql(sql: str, *args) -> Any:
|
|
|
322
322
|
try:
|
|
323
323
|
select_key = Dialect.get_select_key(sql=sql)
|
|
324
324
|
except NotImplementedError:
|
|
325
|
-
raise DBError(
|
|
325
|
+
raise DBError("Expect 'select_key' but not. you may should use 'save_sql_select_key' func with 'select_key'.")
|
|
326
326
|
return do_save_sql_select_key(select_key, sql, *args)
|
|
327
327
|
|
|
328
328
|
|
|
@@ -385,7 +385,7 @@ def do_select(sql: str, *args) -> List[Tuple[Any, ...]]:
|
|
|
385
385
|
return _select(sql)
|
|
386
386
|
|
|
387
387
|
|
|
388
|
-
def do_select_one(sql: str, *args) -> Tuple[Any, ...]:
|
|
388
|
+
def do_select_one(sql: str, *args) -> Optional[Tuple[Any, ...]]:
|
|
389
389
|
"""
|
|
390
390
|
Execute select SQL and return unique result(tuple), SQL contain 'limit'.
|
|
391
391
|
|
|
@@ -419,7 +419,7 @@ def do_query(sql: str, *args) -> List[dict[str, Any]]:
|
|
|
419
419
|
return _query(sql)
|
|
420
420
|
|
|
421
421
|
|
|
422
|
-
def do_query_one(sql: str, *args) -> dict[str, Any]:
|
|
422
|
+
def do_query_one(sql: str, *args) -> Optional[dict[str, Any]]:
|
|
423
423
|
"""
|
|
424
424
|
Execute select SQL and return unique result(dict), SQL contain 'limit'.
|
|
425
425
|
|
|
@@ -470,7 +470,7 @@ def do_query_first(sql: str, *args) -> dict:
|
|
|
470
470
|
return _query_first(sql, *args)
|
|
471
471
|
|
|
472
472
|
|
|
473
|
-
def do_ravel_list(sql: str, *args, position: int = 0) -> List:
|
|
473
|
+
def do_ravel_list(sql: str, *args, position: int = 0) -> List[Any]:
|
|
474
474
|
"""
|
|
475
475
|
Execute select SQL and return list results(tuple).
|
|
476
476
|
|
|
@@ -608,7 +608,7 @@ def batch_insert(table_name: str, *args) -> int:
|
|
|
608
608
|
>>> db.batch_execute('person', *args)
|
|
609
609
|
2
|
|
610
610
|
"""
|
|
611
|
-
|
|
611
|
+
|
|
612
612
|
logger.debug("Exec func 'sqlexecx.%s' \n\t Table: '%s', args: %s" % ('batch_insert', table_name, args))
|
|
613
613
|
assert args, "*args must not be empty."
|
|
614
614
|
args = sql_support.get_batch_args(*args)
|
|
@@ -658,7 +658,7 @@ def do_load(sql: str, *args) -> Loader:
|
|
|
658
658
|
return Loader(*_do_select(sql))
|
|
659
659
|
|
|
660
660
|
|
|
661
|
-
def insert_from_df(df, table_name: str, columns: Collection[str] = None) -> int:
|
|
661
|
+
def insert_from_df(df, table_name: str, columns: Optional[Collection[str]] = None) -> int:
|
|
662
662
|
"""
|
|
663
663
|
Insert data into table from pandas DataFrame and return effected rowcount.
|
|
664
664
|
|
|
@@ -673,7 +673,7 @@ def insert_from_df(df, table_name: str, columns: Collection[str] = None) -> int:
|
|
|
673
673
|
return batch_execute(sql, data)
|
|
674
674
|
|
|
675
675
|
|
|
676
|
-
def insert_from_pl(df, table_name: str, columns: Collection[str] = None) -> int:
|
|
676
|
+
def insert_from_pl(df, table_name: str, columns: Optional[Collection[str]] = None) -> int:
|
|
677
677
|
"""
|
|
678
678
|
Insert data into table from polars DataFrame and return effected rowcount.
|
|
679
679
|
|
|
@@ -688,7 +688,8 @@ def insert_from_pl(df, table_name: str, columns: Collection[str] = None) -> int:
|
|
|
688
688
|
return batch_execute(sql, data)
|
|
689
689
|
|
|
690
690
|
|
|
691
|
-
def insert_from_csv(file_name: str, table_name: str, delimiter=',', header=True,
|
|
691
|
+
def insert_from_csv(file_name: str, table_name: str, delimiter=',', header=True,
|
|
692
|
+
columns: Optional[Collection[str]] = None, encoding='utf-8') -> int:
|
|
692
693
|
"""
|
|
693
694
|
Insert data into table from a csv file and return effected rowcount.
|
|
694
695
|
|
|
@@ -721,6 +722,7 @@ def insert_from_csv(file_name: str, table_name: str, delimiter=',', header=True,
|
|
|
721
722
|
sql = sql_support.insert_sql(table_name.strip(), lines[0])
|
|
722
723
|
lines = lines[1:]
|
|
723
724
|
|
|
725
|
+
assert sql, "No insert SQL generated"
|
|
724
726
|
return batch_execute(sql, lines)
|
|
725
727
|
|
|
726
728
|
|
|
@@ -779,7 +781,7 @@ def drop_table(table_name: str) -> int:
|
|
|
779
781
|
return _execute('DROP TABLE IF EXISTS %s' % Dialect.get_dialect_str(table_name))
|
|
780
782
|
|
|
781
783
|
|
|
782
|
-
def show_tables(schema: str = None) -> List[str]:
|
|
784
|
+
def show_tables(schema: Optional[str] = None) -> List[str]:
|
|
783
785
|
"""
|
|
784
786
|
Show tables
|
|
785
787
|
|
|
@@ -262,7 +262,7 @@ class Param:
|
|
|
262
262
|
"""
|
|
263
263
|
return self.sql_exec.query_first(*self.args, **self.kwargs)
|
|
264
264
|
|
|
265
|
-
def ravel_list(self, position: int = 0) -> List:
|
|
265
|
+
def ravel_list(self, position: int = 0) -> List[Any]:
|
|
266
266
|
"""
|
|
267
267
|
Execute select SQL and return list.
|
|
268
268
|
|
|
@@ -274,7 +274,7 @@ class Param:
|
|
|
274
274
|
>>> db.sql('SELECT name FROM person WHERE age=:age').param(age=20).ravel_list()
|
|
275
275
|
['张三', '李四', '王五']
|
|
276
276
|
"""
|
|
277
|
-
return self.sql_exec.ravel_list(*self.args, position=position **self.kwargs)
|
|
277
|
+
return self.sql_exec.ravel_list(*self.args, position=position, **self.kwargs)
|
|
278
278
|
|
|
279
279
|
def to_df(self):
|
|
280
280
|
"""
|
|
@@ -490,7 +490,7 @@ class SqlExec:
|
|
|
490
490
|
"""
|
|
491
491
|
return self.exec.query_first(self.sql, *args, **kwargs)
|
|
492
492
|
|
|
493
|
-
def ravel_list(self, *args, position: int = 0, **kwargs) -> List:
|
|
493
|
+
def ravel_list(self, *args, position: int = 0, **kwargs) -> List[Any]:
|
|
494
494
|
"""
|
|
495
495
|
Execute select SQL and return list.
|
|
496
496
|
|
|
@@ -7,7 +7,7 @@ from sqlexecutorx.conf import lru_cache_size
|
|
|
7
7
|
|
|
8
8
|
from .dialect import Dialect
|
|
9
9
|
from .log_support import sql_log
|
|
10
|
-
from .constant import NAMED_REGEX
|
|
10
|
+
from .constant import NAMED_REGEX
|
|
11
11
|
|
|
12
12
|
PATTERN = re.compile(NAMED_REGEX)
|
|
13
13
|
|
|
@@ -84,7 +84,7 @@ def get_mapping_sql_args(sql: str, *args, **kwargs):
|
|
|
84
84
|
if args:
|
|
85
85
|
return sql, args
|
|
86
86
|
raise DBError("Placeholder sql expected '*args' but empty.")
|
|
87
|
-
|
|
87
|
+
|
|
88
88
|
if is_mapping(sql):
|
|
89
89
|
assert kwargs, "Named mapping SQL expected '**kwargs' empty."
|
|
90
90
|
return get_named_sql_args(sql, **kwargs)
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
# -*- coding:utf-8 -*-
|
|
3
3
|
|
|
4
4
|
from functools import lru_cache
|
|
5
|
-
from typing import Tuple, List, Union, Any
|
|
5
|
+
from typing import Tuple, List, Union, Any, Optional
|
|
6
6
|
from sqlexecutorx import batch_execute as _batch_execute
|
|
7
7
|
from .loader import Loader
|
|
8
8
|
from . import exec, sql_support
|
|
@@ -101,7 +101,7 @@ class ColumnWhereExec:
|
|
|
101
101
|
"""
|
|
102
102
|
return self._where_exec.query_first(*self.columns)
|
|
103
103
|
|
|
104
|
-
def ravel_list(self) -> List:
|
|
104
|
+
def ravel_list(self) -> List[Any]:
|
|
105
105
|
"""
|
|
106
106
|
Select data from table and return list.
|
|
107
107
|
|
|
@@ -179,7 +179,7 @@ class ColumnWhereExec:
|
|
|
179
179
|
ColumnWhereLimitExec()
|
|
180
180
|
"""
|
|
181
181
|
return ColumnWhereLimitExec(self._where_exec.limit(limit), *self.columns)
|
|
182
|
-
|
|
182
|
+
|
|
183
183
|
def order_by(self, order_by: str) -> ColumnWhereOrderByExec:
|
|
184
184
|
"""
|
|
185
185
|
Get a ColumnWhereOrderByExec instance
|
|
@@ -195,10 +195,10 @@ class ColumnWhereExec:
|
|
|
195
195
|
|
|
196
196
|
class WhereExec(WhereBase):
|
|
197
197
|
|
|
198
|
-
def __init__(self, _exec, table_name: str, order_by: str, **kwargs):
|
|
198
|
+
def __init__(self, _exec, table_name: str, order_by: Optional[str], **kwargs):
|
|
199
199
|
super().__init__(_exec, table_name, order_by, **kwargs)
|
|
200
200
|
|
|
201
|
-
def get(self, column: str) -> Any:
|
|
201
|
+
def get(self, column: Union[str, int]) -> Any:
|
|
202
202
|
"""
|
|
203
203
|
Select data from table and expected one int and only one int result, SQL contain 'limit'.
|
|
204
204
|
|
|
@@ -346,7 +346,7 @@ class WhereExec(WhereBase):
|
|
|
346
346
|
"""
|
|
347
347
|
return WherePageExec(self, page_num, page_size, return_total)
|
|
348
348
|
|
|
349
|
-
def limit(self, limit: Union[int, Tuple[int], List[int]] = 10) -> WhereLimitExec:
|
|
349
|
+
def limit(self, limit: Union[int, Tuple[int, int], List[int]] = 10) -> WhereLimitExec:
|
|
350
350
|
"""
|
|
351
351
|
Get a WhereLimitExec instance
|
|
352
352
|
|
|
@@ -357,7 +357,7 @@ class WhereExec(WhereBase):
|
|
|
357
357
|
WhereLimitExec()
|
|
358
358
|
"""
|
|
359
359
|
return WhereLimitExec(self.exec, self.table, None, limit, **self.where_condition)
|
|
360
|
-
|
|
360
|
+
|
|
361
361
|
def order_by(self, order_by: str) -> WhereOrderByExec:
|
|
362
362
|
"""
|
|
363
363
|
Get a WhereOrderByExec instance
|
|
@@ -369,8 +369,8 @@ class WhereExec(WhereBase):
|
|
|
369
369
|
WhereOrderByExec()
|
|
370
370
|
"""
|
|
371
371
|
return WhereOrderByExec(self.exec, self.table, order_by, **self.where_condition)
|
|
372
|
-
|
|
373
|
-
|
|
372
|
+
|
|
373
|
+
|
|
374
374
|
class ColumnExec:
|
|
375
375
|
|
|
376
376
|
def __init__(self, table_exec, *columns):
|
|
@@ -493,7 +493,7 @@ class ColumnExec:
|
|
|
493
493
|
"""
|
|
494
494
|
return self.table_exec.query_first(*self.columns)
|
|
495
495
|
|
|
496
|
-
def ravel_list(self) -> List:
|
|
496
|
+
def ravel_list(self) -> List[Any]:
|
|
497
497
|
"""
|
|
498
498
|
Select data from table and return list.
|
|
499
499
|
|
|
@@ -515,7 +515,7 @@ class ColumnExec:
|
|
|
515
515
|
>>> db.table('person').columns('id', 'name', 'age').to_df()
|
|
516
516
|
"""
|
|
517
517
|
return self.table_exec.load(*self.columns).to_df()
|
|
518
|
-
|
|
518
|
+
|
|
519
519
|
def to_pl(self):
|
|
520
520
|
"""
|
|
521
521
|
Select from table and return polars DataFrame instance.
|
|
@@ -584,7 +584,7 @@ class ColumnExec:
|
|
|
584
584
|
ColumnLimitExec()
|
|
585
585
|
"""
|
|
586
586
|
return ColumnLimitExec(self.table_exec.limit(limit), *self.columns)
|
|
587
|
-
|
|
587
|
+
|
|
588
588
|
def order_by(self, order_by: str) -> ColumnOrderByExec:
|
|
589
589
|
"""
|
|
590
590
|
Get a ColumnOrderByExec instance
|
|
@@ -659,7 +659,7 @@ class Table:
|
|
|
659
659
|
"""
|
|
660
660
|
return self.exec.batch_insert(self.table, *args)
|
|
661
661
|
|
|
662
|
-
def get(self, column: str) -> Any:
|
|
662
|
+
def get(self, column: Union[str, int]) -> Any:
|
|
663
663
|
"""
|
|
664
664
|
Select data from table and expected one int and only one int result, SQL contain 'limit'.
|
|
665
665
|
|
|
@@ -762,7 +762,7 @@ class Table:
|
|
|
762
762
|
sql = get_table_select_sql(self.table, None, None, 0, *columns)
|
|
763
763
|
return self.exec.do_query_first(sql)
|
|
764
764
|
|
|
765
|
-
def ravel_list(self, column: str) -> List:
|
|
765
|
+
def ravel_list(self, column: str) -> List[Any]:
|
|
766
766
|
"""
|
|
767
767
|
Select data from table and return list.
|
|
768
768
|
|
|
@@ -790,7 +790,7 @@ class Table:
|
|
|
790
790
|
sql = get_table_select_sql(self.table, None, None, 0, *columns)
|
|
791
791
|
return self.exec.do_load(sql)
|
|
792
792
|
|
|
793
|
-
def insert_from_df(self, df, columns: Tuple[str] = None):
|
|
793
|
+
def insert_from_df(self, df, columns: Optional[Tuple[str]] = None):
|
|
794
794
|
"""
|
|
795
795
|
Insert data into table from pandas DataFrame and return effected rowcount.
|
|
796
796
|
|
|
@@ -801,8 +801,8 @@ class Table:
|
|
|
801
801
|
20
|
|
802
802
|
"""
|
|
803
803
|
return self.exec.insert_from_df(df, self.table, columns)
|
|
804
|
-
|
|
805
|
-
def insert_from_pl(self, df, columns: Tuple[str] = None):
|
|
804
|
+
|
|
805
|
+
def insert_from_pl(self, df, columns: Optional[Tuple[str]] = None):
|
|
806
806
|
"""
|
|
807
807
|
从polars DataFrame插入数据到表中并返回影响的行数。
|
|
808
808
|
|
|
@@ -814,7 +814,8 @@ class Table:
|
|
|
814
814
|
"""
|
|
815
815
|
return self.exec.insert_from_pl(df, self.table, columns)
|
|
816
816
|
|
|
817
|
-
def insert_from_csv(self, file_name: str, delimiter=',', header=True,
|
|
817
|
+
def insert_from_csv(self, file_name: str, delimiter=',', header=True,
|
|
818
|
+
columns: Optional[Tuple[str]] = None, encoding='utf-8'):
|
|
818
819
|
"""
|
|
819
820
|
Insert data into table from a csv file and return effected rowcount.
|
|
820
821
|
|
|
@@ -898,7 +899,7 @@ class Table:
|
|
|
898
899
|
"""
|
|
899
900
|
return TablePageExec(self, page_num, page_size, return_total)
|
|
900
901
|
|
|
901
|
-
def limit(self, limit: Union[int, Tuple[int], List[int]] = 10) -> LimitExec:
|
|
902
|
+
def limit(self, limit: Union[int, Tuple[int, int], List[int]] = 10) -> LimitExec:
|
|
902
903
|
"""
|
|
903
904
|
Get a LimitExec instance
|
|
904
905
|
|
|
@@ -909,7 +910,7 @@ class Table:
|
|
|
909
910
|
LimitExec()
|
|
910
911
|
"""
|
|
911
912
|
return LimitExec(self.exec, self.table, None, limit)
|
|
912
|
-
|
|
913
|
+
|
|
913
914
|
def order_by(self, order_by: str) -> OrderByExec:
|
|
914
915
|
"""
|
|
915
916
|
Get a OrderByExec instance
|