You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,7 +48,7 @@ repos:
48
48
49
49
To exemplify the formatting let's say you have a SQL query like this
50
50
51
-
```python
51
+
```
52
52
example_sql = """
53
53
create or replace table mytable as -- mytable example
54
54
seLecT a.asdf, b.qwer, -- some comment here
@@ -67,7 +67,7 @@ groUp by a.asdf
67
67
68
68
Then you can use this package to format it so that it is better readable
69
69
70
-
```python
70
+
```
71
71
from sql_formatter.core import format_sql
72
72
print(format_sql(example_sql))
73
73
```
@@ -91,7 +91,7 @@ print(format_sql(example_sql))
91
91
92
92
It can even deal with subqueries and it will correct my favourite simple careless mistake (comma at the end of SELECT statement before of FROM) for you on the flow :-)
93
93
94
-
```python
94
+
```
95
95
print(format_sql("""
96
96
select asdf, cast(qwer as numeric), -- some comment
97
97
qwer1
@@ -121,7 +121,7 @@ where qwer1 >= 0
121
121
122
122
The formatter is also robust against nested subqueries
123
123
124
-
```python
124
+
```
125
125
print(format_sql("""
126
126
select field1, field2 from (select field1,
127
127
field2 from (select field1, field2,
@@ -143,7 +143,7 @@ field3 from table1 where a=1 and b>=100))
143
143
144
144
If you do not want to get some query formatted in your SQL file then you can use the marker `/*skip-formatter*/` in your query to disable formatting for just the corresponding query
145
145
146
-
```python
146
+
```
147
147
from sql_formatter.format_file import format_sql_commands
<spanclass="n">format_select</span><spanclass="p">(</span><spanclass="s2">"select asdf, -- Some comment[C]asdforqwer -- Another comment[C]"</span><spanclass="p">),</span>
1765
+
<spanclass="sd">"""</span>
1766
+
<spanclass="sd">select asdf, -- Some comment</span>
1767
+
<spanclass="sd"> asdforqwer -- Another comment</span>
<spanclass="ansi-green-fg">~/Dokumente/Python_Stuff/Projekte/sql_formatter/sql_formatter/utils.py</span> in <spanclass="ansi-cyan-fg">assert_and_print</span><spanclass="ansi-blue-fg">(s_in, s_expected)</span>
<spanclass="ansi-green-fg">~/Dokumente/Python_Stuff/Projekte/sql_formatter/sql_formatter/utils.py</span> in <spanclass="ansi-cyan-fg">assert_and_print</span><spanclass="ansi-blue-fg">(s_in, s_expected)</span>
0 commit comments