Skip to content

Commit

Permalink
[优化]repr
Browse files Browse the repository at this point in the history
  • Loading branch information
longfengpili committed Nov 21, 2024
1 parent 4c94107 commit f285797
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -323,7 +321,7 @@ sqlstmts = SqlStatements(sql)
可以自动执行表名(表名包含即可)

## 调用日志格式
1. 查看每步sql可以使用如下日志格式(如果还出错,同时加上上面的内容
1. 查看每步sql可以使用如下日志格式(如果还出错,同时加上下面的内容
```python
import logging
dblogger = logging.getLogger('pydbapi.db.base')
Expand Down
12 changes: 7 additions & 5 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 15:19:31
# @Last Modified time: 2024-11-21 15:57:35
# @github: https://github.com/longfengpili


Expand All @@ -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(';')
Expand Down Expand Up @@ -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(';')

Expand Down Expand Up @@ -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]
Expand All @@ -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__()
Expand Down

0 comments on commit f285797

Please sign in to comment.