Skip to content

Commit

Permalink
[FEA] Separate each ON join by newline (#80)
Browse files Browse the repository at this point in the history
This closes #76
  • Loading branch information
PabloRMira authored Dec 20, 2020
1 parent 9534d91 commit 0ad891d
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 27 deletions.
101 changes: 91 additions & 10 deletions docs/core.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ <h2 id="General-formatting">General formatting<a class="anchor-link" href="#Gene
<span class="s2">table2 as b -- and here a comment</span>
<span class="s2"> on a.asdf = b.asdf /* joiN this way */</span>
<span class="s2"> inner join table3 as c</span>
<span class="s2">on a.asdf=c.asdf</span>
<span class="s2">on a.asdf=c.asdf and a.qwer= b.qwer</span>
<span class="s2">whEre a.asdf= 1 -- comment this</span>
<span class="s2">anD b.qwer =2 and a.asdf&lt;=1 --comment that</span>
<span class="s2">or b.qwer&gt;=5</span>
Expand Down Expand Up @@ -116,7 +116,8 @@ <h2 id="General-formatting">General formatting<a class="anchor-link" href="#Gene
<span class="s2"> LEFT JOIN table2 as b -- and here a comment</span>
<span class="s2"> ON a.asdf = b.asdf /* joiN this way */</span>
<span class="s2"> INNER JOIN table3 as c</span>
<span class="s2"> ON a.asdf = c.asdf</span>
<span class="s2"> ON a.asdf = c.asdf and</span>
<span class="s2"> a.qwer = b.qwer</span>
<span class="s2">WHERE a.asdf = 1 -- comment this</span>
<span class="s2"> and b.qwer = 2</span>
<span class="s2"> and a.asdf &lt;= 1 --comment that</span>
Expand Down Expand Up @@ -2474,6 +2475,84 @@ <h4 id="format_on" class="doc_header"><code>format_on</code><a href="https://git
</div>
</div>

</div>
{% endraw %}

{% raw %}

<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">assert_and_print</span><span class="p">(</span>
<span class="n">format_on</span><span class="p">(</span><span class="s2">&quot;on a.asdf =b.asdf and a.qwer=b.qwer&quot;</span><span class="p">),</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> on a.asdf = b.asdf and</span>
<span class="sd"> a.qwer = b.qwer</span>
<span class="sd">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">

<div class="output_area">

<div class="output_subarea output_stream output_stdout output_text">
<pre> on a.asdf = b.asdf and
a.qwer = b.qwer
</pre>
</div>
</div>

</div>
</div>

</div>
{% endraw %}

{% raw %}

<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">assert_and_print</span><span class="p">(</span>
<span class="n">format_on</span><span class="p">(</span><span class="s2">&quot;on a.asdf =b.asdf and a.qwer=b.qwer or a.qwer2= b.qwer2&quot;</span><span class="p">),</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> on a.asdf = b.asdf and</span>
<span class="sd"> a.qwer = b.qwer or</span>
<span class="sd"> a.qwer2 = b.qwer2</span>
<span class="sd">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">

<div class="output_area">

<div class="output_subarea output_stream output_stdout output_text">
<pre> on a.asdf = b.asdf and
a.qwer = b.qwer or
a.qwer2 = b.qwer2
</pre>
</div>
</div>

</div>
</div>

</div>
{% endraw %}

Expand All @@ -2494,7 +2573,7 @@ <h3 id="WHERE">WHERE<a class="anchor-link" href="#WHERE"> </a></h3>


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_where" class="doc_header"><code>format_where</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L320" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_where</code>(<strong><code>s</code></strong>)</p>
<h4 id="format_where" class="doc_header"><code>format_where</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L323" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_where</code>(<strong><code>s</code></strong>)</p>
</blockquote>
<p>Format WHERE statement line <code>s</code></p>

Expand Down Expand Up @@ -2569,7 +2648,7 @@ <h2 id="Format-all-statements">Format all statements<a class="anchor-link" href=


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_statement_line" class="doc_header"><code>format_statement_line</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L330" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_statement_line</code>(<strong><code>s</code></strong>)</p>
<h4 id="format_statement_line" class="doc_header"><code>format_statement_line</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L333" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_statement_line</code>(<strong><code>s</code></strong>)</p>
</blockquote>
<p>Format statement line <code>s</code></p>

Expand Down Expand Up @@ -2713,7 +2792,7 @@ <h4 id="format_statement_line" class="doc_header"><code>format_statement_line</c


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_statements" class="doc_header"><code>format_statements</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L348" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_statements</code>(<strong><code>s</code></strong>)</p>
<h4 id="format_statements" class="doc_header"><code>format_statements</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L351" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_statements</code>(<strong><code>s</code></strong>)</p>
</blockquote>
<p>Format statements lines <code>s</code></p>

Expand Down Expand Up @@ -2791,7 +2870,7 @@ <h3 id="Write-a-;-at-the-end-of-query">Write a ; at the end of query<a class="an


<div class="output_markdown rendered_html output_subarea ">
<h4 id="add_ending_semicolon" class="doc_header"><code>add_ending_semicolon</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L358" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>add_ending_semicolon</code>(<strong><code>s</code></strong>)</p>
<h4 id="add_ending_semicolon" class="doc_header"><code>add_ending_semicolon</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L361" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>add_ending_semicolon</code>(<strong><code>s</code></strong>)</p>
</blockquote>
<p>Add ending semicolon for SQL query <code>s</code></p>

Expand Down Expand Up @@ -2957,7 +3036,7 @@ <h2 id="Putting-everything-together">Putting everything together<a class="anchor


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_simple_sql" class="doc_header"><code>format_simple_sql</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L366" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_simple_sql</code>(<strong><code>s</code></strong>, <strong><code>add_semicolon</code></strong>=<em><code>True</code></em>)</p>
<h4 id="format_simple_sql" class="doc_header"><code>format_simple_sql</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L369" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_simple_sql</code>(<strong><code>s</code></strong>, <strong><code>add_semicolon</code></strong>=<em><code>True</code></em>)</p>
</blockquote>
<p>Format a simple SQL query without subqueries <code>s</code>.
If <code>add_semicolon</code> is True, then add a semicolon at the end</p>
Expand Down Expand Up @@ -3018,7 +3097,8 @@ <h4 id="format_simple_sql" class="doc_header"><code>format_simple_sql</code><a h
LEFT JOIN table2 as b -- and here a comment
ON a.asdf = b.asdf /* joiN this way */
INNER JOIN table3 as c
ON a.asdf = c.asdf
ON a.asdf = c.asdf and
a.qwer = b.qwer
WHERE a.asdf = 1 -- comment this
and b.qwer = 2
and a.asdf &lt;= 1 --comment that
Expand Down Expand Up @@ -3180,7 +3260,7 @@ <h3 id="Main-function-handling-queries-with-subqueries">Main function handling q


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_sql" class="doc_header"><code>format_sql</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L380" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_sql</code>(<strong><code>s</code></strong>, <strong><code>add_semicolon</code></strong>=<em><code>True</code></em>)</p>
<h4 id="format_sql" class="doc_header"><code>format_sql</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L384" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_sql</code>(<strong><code>s</code></strong>, <strong><code>add_semicolon</code></strong>=<em><code>True</code></em>)</p>
</blockquote>
<p>Format SQL query with subqueries. If <code>add_semicolon</code> is True then add a semicolon at the end</p>

Expand Down Expand Up @@ -3293,7 +3373,8 @@ <h4 id="format_sql" class="doc_header"><code>format_sql</code><a href="https://g
LEFT JOIN table2 as b -- and here a comment
ON a.asdf = b.asdf /* joiN this way */
INNER JOIN table3 as c
ON a.asdf = c.asdf
ON a.asdf = c.asdf and
a.qwer = b.qwer
WHERE a.asdf = 1 -- comment this
and b.qwer = 2
and a.asdf &lt;= 1 --comment that
Expand Down
80 changes: 64 additions & 16 deletions nbs/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The autoreload extension is already loaded. To reload it, use:\n",
" %reload_ext autoreload\n"
]
}
],
"outputs": [],
"source": [
"#hide\n",
"%load_ext autoreload\n",
Expand Down Expand Up @@ -96,7 +87,7 @@
"table2 as b -- and here a comment\n",
" on a.asdf = b.asdf /* joiN this way */\n",
" inner join table3 as c\n",
"on a.asdf=c.asdf\n",
"on a.asdf=c.asdf and a.qwer= b.qwer\n",
"whEre a.asdf= 1 -- comment this\n",
"anD b.qwer =2 and a.asdf<=1 --comment that\n",
"or b.qwer>=5\n",
Expand Down Expand Up @@ -133,7 +124,8 @@
" LEFT JOIN table2 as b -- and here a comment\n",
" ON a.asdf = b.asdf /* joiN this way */\n",
" INNER JOIN table3 as c\n",
" ON a.asdf = c.asdf\n",
" ON a.asdf = c.asdf and\n",
" a.qwer = b.qwer\n",
"WHERE a.asdf = 1 -- comment this\n",
" and b.qwer = 2\n",
" and a.asdf <= 1 --comment that\n",
Expand Down Expand Up @@ -1951,8 +1943,11 @@
"#export\n",
"def format_on(s):\n",
" \"Format ON statement line `s`\"\n",
" s = add_whitespaces_between_symbols(s) # add whitespaces between symbols in join \n",
" s = add_whitespaces_between_symbols(s) # add whitespaces between symbols in join\n",
" s = s.replace(\"[C]\", \" \")\n",
" s = \" \" + s # add indentation\n",
" s = re.sub(r\"and\\s\", r\"and\\n\" + \" \" * 11, s, flags=re.I) # add new line before every 'and' and indentation\n",
" s = re.sub(r\"or\\s\", r\"or\\n\" + \" \" * 11, s, flags=re.I) # add new line before every 'or' and indentation\n",
" return s"
]
},
Expand All @@ -1973,6 +1968,56 @@
"assert_and_print(format_on(\"on a.asdf =b.asdf\"), \" on a.asdf = b.asdf\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" on a.asdf = b.asdf and\n",
" a.qwer = b.qwer\n"
]
}
],
"source": [
"assert_and_print(\n",
" format_on(\"on a.asdf =b.asdf and a.qwer=b.qwer\"), \n",
"\"\"\"\n",
" on a.asdf = b.asdf and\n",
" a.qwer = b.qwer\n",
"\"\"\".strip(\"\\n\")\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" on a.asdf = b.asdf and\n",
" a.qwer = b.qwer or\n",
" a.qwer2 = b.qwer2\n"
]
}
],
"source": [
"assert_and_print(\n",
" format_on(\"on a.asdf =b.asdf and a.qwer=b.qwer or a.qwer2= b.qwer2\"), \n",
"\"\"\"\n",
" on a.asdf = b.asdf and\n",
" a.qwer = b.qwer or\n",
" a.qwer2 = b.qwer2\n",
"\"\"\".strip(\"\\n\")\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -2296,7 +2341,8 @@
" s = preformat_statements(s) # add breaklines for the main statements\n",
" s = format_statements(s) # format statements\n",
" s = re.sub(r\"\\[C\\]\", \"\", s) # replace remaining [C]\n",
" s = re.sub(r\"\\[CS\\]\", \"\\n\", s) # replace remainig [CS]\n",
" s = re.sub(r\"\\[CS\\]\", \"\\n\", s) # replace remaining [CS]\n",
" s = re.sub(r\"\\s+\\n\", \"\\n\", s) # replace redundant whitespaces before newline\n",
" if add_semicolon:\n",
" s = add_ending_semicolon(s) # add ending semicolon if not there yet\n",
" return s"
Expand Down Expand Up @@ -2327,7 +2373,8 @@
" LEFT JOIN table2 as b -- and here a comment\n",
" ON a.asdf = b.asdf /* joiN this way */\n",
" INNER JOIN table3 as c\n",
" ON a.asdf = c.asdf\n",
" ON a.asdf = c.asdf and\n",
" a.qwer = b.qwer\n",
"WHERE a.asdf = 1 -- comment this\n",
" and b.qwer = 2\n",
" and a.asdf <= 1 --comment that\n",
Expand Down Expand Up @@ -2545,7 +2592,8 @@
" LEFT JOIN table2 as b -- and here a comment\n",
" ON a.asdf = b.asdf /* joiN this way */\n",
" INNER JOIN table3 as c\n",
" ON a.asdf = c.asdf\n",
" ON a.asdf = c.asdf and\n",
" a.qwer = b.qwer\n",
"WHERE a.asdf = 1 -- comment this\n",
" and b.qwer = 2\n",
" and a.asdf <= 1 --comment that\n",
Expand Down
6 changes: 5 additions & 1 deletion sql_formatter/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,10 @@ def format_join(s):
def format_on(s):
"Format ON statement line `s`"
s = add_whitespaces_between_symbols(s) # add whitespaces between symbols in join
s = s.replace("[C]", " ")
s = " " + s # add indentation
s = re.sub(r"and\s", r"and\n" + " " * 11, s, flags=re.I) # add new line before every 'and' and indentation
s = re.sub(r"or\s", r"or\n" + " " * 11, s, flags=re.I) # add new line before every 'or' and indentation
return s

# Cell
Expand Down Expand Up @@ -371,7 +374,8 @@ def format_simple_sql(s, add_semicolon=True):
s = preformat_statements(s) # add breaklines for the main statements
s = format_statements(s) # format statements
s = re.sub(r"\[C\]", "", s) # replace remaining [C]
s = re.sub(r"\[CS\]", "\n", s) # replace remainig [CS]
s = re.sub(r"\[CS\]", "\n", s) # replace remaining [CS]
s = re.sub(r"\s+\n", "\n", s) # replace redundant whitespaces before newline
if add_semicolon:
s = add_ending_semicolon(s) # add ending semicolon if not there yet
return s
Expand Down

0 comments on commit 0ad891d

Please sign in to comment.