From f285797ee9bd7dba861040a6943a3922d489caf4 Mon Sep 17 00:00:00 2001 From: longfengpili <398745129@qq.com> Date: Thu, 21 Nov 2024 15:59:10 +0800 Subject: [PATCH] =?UTF-8?q?[=E4=BC=98=E5=8C=96]repr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 ++++------ pydbapi/sql/parse.py | 12 +++++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e32a5f6..7da10b2 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,6 @@ result.to_csv(outfile) + SqlStatement ```python from pydbapi.sql import SqlStatement --- comment sql = 'select * from tablename where part_date >= $part_date;' sqlstmt = SqlStatement(sql) ``` @@ -136,17 +135,16 @@ sqlstmt = SqlStatement(sql) ``` + substitute_params ```python - sql = sqlstmt.substitute_params(part_date='2024-01-01') + sqlstmt = sqlstmt.substitute_params(part_date="'2024-01-01'") ``` - + get_with_testsql + + get_with_testsql(only support CETs) ```python - sql = sqlstmt.get_with_testsql(idx=1) + sqlstmt = sqlstmt.get_with_testsql(idx=1) ``` + SqlStatements ```python from pydbapi.sql import SqlStatements --- comment sql = ''' select * from tablename1 where part_date >= $part_date; select * from tablename2 where part_date >= $part_date; @@ -323,7 +321,7 @@ sqlstmts = SqlStatements(sql) 可以自动执行表名(表名包含即可) ## 调用日志格式 -1. 查看每步sql可以使用如下日志格式(如果还出错,同时加上上面的内容) +1. 查看每步sql可以使用如下日志格式(如果还出错,同时加上下面的内容) ```python import logging dblogger = logging.getLogger('pydbapi.db.base') diff --git a/pydbapi/sql/parse.py b/pydbapi/sql/parse.py index eda6bd0..c66d232 100644 --- a/pydbapi/sql/parse.py +++ b/pydbapi/sql/parse.py @@ -2,7 +2,7 @@ # @Author: longfengpili # @Date: 2024-10-09 16:33:05 # @Last Modified by: longfengpili -# @Last Modified time: 2024-11-21 15:19:31 +# @Last Modified time: 2024-11-21 15:57:35 # @github: https://github.com/longfengpili @@ -24,9 +24,9 @@ def __init__(self, sql: str): self._parsed = sqlparse.parse(sql)[0] def __repr__(self): - _repr = f"[{self.action}]{self.tablename}" + _repr = f"<{self.action}>{self.tablename}" _repr = f"{_repr}::{self.comment}" if self.comment else _repr - return _repr + return f"SqlStatement({_repr})" def __sub__(self, sqlstmt: str): self._sql = self._sql.strip(';') @@ -57,7 +57,7 @@ def sql(self): # use_space_around_operators=True:在运算符周围加空格。 formatted_sql = sqlparse.format(self._sql, keyword_case='lower', strip_comments=True, use_space_around_operators=True) # 手动移除空白行 - non_blank_lines = [line for line in formatted_sql.splitlines() if line.strip() != ''] + non_blank_lines = [line.strip() for line in formatted_sql.splitlines() if line.strip() != ''] sql = '\n'.join(non_blank_lines) return sql.strip(';') @@ -166,6 +166,8 @@ def subqueries(self): def get_with_testsql(self, idx: int = 1): subqueries = self.subqueries + if self.action != 'with': + raise ValueError('The function only support CTEs') if not subqueries: raise ValueError("No subqueries") last_subquery = subqueries[idx] @@ -187,7 +189,7 @@ def __init__(self, sql: str): self._statements = None def __str__(self): - return f"[Statement: {len(self)}]{self[0]}..." + return f"SqlStatements([stmt:{len(self)}]{self[0]}, ...)" def __repr__(self): return self.__str__()