Skip to content

Commit

Permalink
[重构]parse
Browse files Browse the repository at this point in the history
  • Loading branch information
longfengpili committed Nov 21, 2024
1 parent 2d8e4a3 commit d12043a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
24 changes: 12 additions & 12 deletions pydbapi/db/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Author: longfengpili
# @Date: 2023-06-02 15:27:41
# @Last Modified by: longfengpili
# @Last Modified time: 2024-11-20 18:45:24
# @Last Modified time: 2024-11-21 11:35:50
# @github: https://github.com/longfengpili


Expand Down Expand Up @@ -33,18 +33,18 @@ def __init__(self, *args, **kwargs):
def get_conn(self):
pass

def prepare_sql_statements(self, sql, verbose):
def prepare_sql_statements(self, sqlstmts, verbose):
if any("jupyter" in arg for arg in sys.argv):
from tqdm.notebook import tqdm
else:
from tqdm import tqdm

if isinstance(sql, str):
sqlstmts = SqlStatements(sql)
elif isinstance(sql, SqlStatement):
sqlstmts = SqlStatements.from_sqlstatements(sql)
if isinstance(sqlstmts, str):
sqlstmts = SqlStatements(sqlstmts)
elif isinstance(sqlstmts, SqlStatements):
sqlstmts = sqlstmts
else:
sqlstmts = sql
raise TypeError("sqlstmts must be a string or an instance of SqlStatements")

bar_format = '{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}] {postfix[0]}'
sqlstmts = sqlstmts if verbose <= 1 else tqdm(sqlstmts, postfix=['START'], bar_format=bar_format) # 如果verbose>=2则显示进度条
Expand Down Expand Up @@ -93,21 +93,21 @@ def fetch_query_results(self, action, cursor, count, verbose):

return results

def handle_progress_logging(self, step, verbose, sqls):
def handle_progress_logging(self, step, verbose, sqlstmts):
if verbose == 1:
dblogger.info(step)
elif verbose >= 2:
sqls.postfix[0] = step
sqlstmts.postfix[0] = step
if verbose >= 3:
dblogger.info(step)

def execute(self, sql, count=None, ehandling='raise', verbose=0):
def execute(self, sqlstmts: any([str, SqlStatements]), count: int = None, ehandling: str = 'raise', verbose: int = 0):
'''[summary]
[description]
执行sql
Arguments:
sql {[str]} -- [sql]
sqlstmt {[str, SqlStatements]} -- [sql]
Keyword Arguments:
count {[int]} -- [返回的结果数量] (default: {None})
Expand All @@ -123,7 +123,7 @@ def execute(self, sql, count=None, ehandling='raise', verbose=0):
results = None
conn = self.get_conn()
cursor = conn.cursor()
sqlstmts = self.prepare_sql_statements(sql, verbose)
sqlstmts = self.prepare_sql_statements(sqlstmts, verbose)
try:
with logging_redirect_tqdm():
for idx, stmt in enumerate(sqlstmts):
Expand Down
4 changes: 2 additions & 2 deletions pydbapi/sql/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Author: longfengpili
# @Date: 2024-10-09 16:33:05
# @Last Modified by: longfengpili
# @Last Modified time: 2024-11-21 11:31:51
# @Last Modified time: 2024-11-21 11:39:19
# @github: https://github.com/longfengpili


Expand Down Expand Up @@ -53,7 +53,7 @@ def tokens(self):
@property
def sql(self):
sql = sqlparse.format(self._sql, keyword_case='lower', strip_comments=True, use_space_around_operators=True)
return sql.strip()
return sql.strip(';')

@property
def comment(self):
Expand Down
11 changes: 9 additions & 2 deletions tests/sql/sqlstmt_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# @Author: longfengpili
# @Date: 2024-11-19 14:22:01
# @Last Modified by: longfengpili
# @Last Modified time: 2024-11-20 17:33:59
# @Last Modified time: 2024-11-21 11:27:18
# @github: https://github.com/longfengpili


import pytest

from pydbapi.sql.parse import SqlStatement
from pydbapi.sql.parse import SqlStatement, SqlStatements


class TestSqlStatement:
Expand Down Expand Up @@ -116,3 +116,10 @@ def test_combination_sqls(self):
print(combination_sql)
print(combination_sql.sql)

@pytest.mark.parametrize("idx", [0, 1, 2, 3, 4])
def test_stmts_attributes(self, idx: int):
sql = self.sqls[idx]
print(f"{idx}".center(50, '='))
sqlstmts = SqlStatements(sql)
print(sqlstmts)

0 comments on commit d12043a

Please sign in to comment.