diff --git a/pydbapi/db/base.py b/pydbapi/db/base.py index dc54c60..eab56ab 100644 --- a/pydbapi/db/base.py +++ b/pydbapi/db/base.py @@ -2,7 +2,7 @@ # @Author: longfengpili # @Date: 2023-06-02 15:27:41 # @Last Modified by: longfengpili -# @Last Modified time: 2024-08-12 14:00:32 +# @Last Modified time: 2024-10-12 11:05:06 # @github: https://github.com/longfengpili @@ -40,9 +40,8 @@ def prepare_sql_statements(self, sql, verbose): else: from tqdm import tqdm - sqls = SqlParse.split_sqls(sql) - # print(sqls) - sqls = [sql.strip() for sql in sqls if sql] + sqlparse = SqlParse(sql) + sqls = [stmt.value.strip(' ;') for stmt in sqlparse.statements] bar_format = '{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}] {postfix[0]}' sqls = sqls if verbose <= 1 else tqdm(sqls, postfix=['START'], bar_format=bar_format) # 如果verbose>=2则显示进度条 return sqls diff --git a/pydbapi/sql/__init__.py b/pydbapi/sql/__init__.py index 45bd7d0..7f3eede 100644 --- a/pydbapi/sql/__init__.py +++ b/pydbapi/sql/__init__.py @@ -2,11 +2,12 @@ # @Author: longfengpili # @Date: 2023-06-02 15:27:41 # @Last Modified by: longfengpili -# @Last Modified time: 2023-07-27 15:35:17 +# @Last Modified time: 2024-10-11 10:51:22 # @github: https://github.com/longfengpili -from .parse import SqlParse, SqlFileParse +from .parse import SqlParse +from .fileparse import SqlFileParse from .compile import SqlCompile __all__ = ['SqlParse', 'SqlCompile', 'SqlFileParse'] diff --git a/pydbapi/sql/fileparse.py b/pydbapi/sql/fileparse.py new file mode 100644 index 0000000..a16bff9 --- /dev/null +++ b/pydbapi/sql/fileparse.py @@ -0,0 +1,140 @@ +# -*- coding: utf-8 -*- +# @Author: longfengpili +# @Date: 2023-06-02 15:27:41 +# @Last Modified by: longfengpili +# @Last Modified time: 2024-10-11 14:52:55 +# @github: https://github.com/longfengpili + + +import re +import os +from datetime import datetime, date, timedelta +from typing import Any, Dict, List, Tuple + +from .parse import SqlParse + +import logging +sqllogger = logging.getLogger(__name__) + + +class SqlFileParse(object): + + def __init__(self, filepath): + self.filepath = filepath + + def get_content(self): + if not os.path.isfile(self.filepath): + raise Exception(f'File 【{self.filepath}】 not exists !') + + with open(self.filepath, 'r', encoding='utf-8') as f: + content = f.read() + return content + + def parse_argument(self, argument: str, arguments: Dict[str, Any]) -> Tuple[str, Any]: + key, value = argument.split('=', 1) + key, value = key.strip(), value.strip() + try: + globals_value = {'timedelta': timedelta} + value = eval(value, globals_value, arguments) + except NameError as e: + raise NameError(f"{e}, please set it before '{key}' !!!") + return key, value + + def get_arguments_infile(self, content: str): + '''[summary] + + [description] + 获取文件中配置的arguments + Returns: + [dict] -- [返回文件中的参数设置] + ''' + arguments = { + 'today': date.today(), + 'now': datetime.now(), + } + arguments_infile = re.findall(r'(?