Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] SELECT adds newline for function arguments (#28) #35

Merged
merged 1 commit into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/_data/sidebars/home_sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ entries:
- output: web,pdf
title: format_file
url: format_file.html
- output: web,pdf
title: utils
url: utils.html
output: web
title: sql_formatter
output: web
Expand Down
966 changes: 425 additions & 541 deletions docs/core.html

Large diffs are not rendered by default.

242 changes: 25 additions & 217 deletions docs/format_file.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ <h2 id="Use-Case">Use-Case<a class="anchor-link" href="#Use-Case"> </a></h2><p>A
<span class="s2"> a.price,</span>
<span class="s2">from sales as a left join (select car_id, car_name, from cars) as b </span>
<span class="s2">on a.car_id = b.car_id</span>
<span class="s2">where car_id&gt;1 and car_id&lt;=100;</span>
<span class="s2">where car_id&gt;1 and car_id&lt;=100 order by b.car_name;</span>

<span class="s2">-- Table no. 1 --</span>
<span class="s2">create or replace table first_table as -- my first table</span>
<span class="s2">select car_id,</span>
<span class="s2"> avg(price) as avg_price,</span>
<span class="s2">from first_view</span>
<span class="s2">group by car_id;</span>
<span class="s2">group by car_id order by car_id;</span>

<span class="s2">--- End of file ---</span>
<span class="s2">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
Expand Down Expand Up @@ -112,15 +112,17 @@ <h2 id="Use-Case">Use-Case<a class="anchor-link" href="#Use-Case"> </a></h2><p>A
<span class="s2"> FROM cars) as b</span>
<span class="s2"> ON a.car_id = b.car_id</span>
<span class="s2">WHERE car_id &gt; 1</span>
<span class="s2"> and car_id &lt;= 100;</span>
<span class="s2"> and car_id &lt;= 100</span>
<span class="s2">ORDER BY b.car_name;</span>


<span class="s2">-- Table no. 1 --</span>
<span class="s2">CREATE OR REPLACE TABLE first_table AS -- my first table</span>
<span class="s2">SELECT car_id,</span>
<span class="s2"> avg(price) as avg_price</span>
<span class="s2">FROM first_view</span>
<span class="s2">GROUP BY car_id;</span>
<span class="s2">GROUP BY car_id</span>
<span class="s2">ORDER BY car_id;</span>

<span class="s2">--- End of file ---</span>
<span class="s2">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">lstrip</span><span class="p">()</span>
Expand Down Expand Up @@ -157,208 +159,6 @@ <h2 id="Formatting-Functions">Formatting Functions<a class="anchor-link" href="#
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="SQL-queries-identification">SQL-queries identification<a class="anchor-link" href="#SQL-queries-identification"> </a></h3><p>SQL queries always start either with <em>CREATE</em> or with <em>SELECT</em>.</p>
<p>Everything else is not a valid SQL statement and will therefore not be formatted</p>

</div>
</div>
</div>
{% raw %}

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

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

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="check_sql_query" class="doc_header"><code>check_sql_query</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/format_file.py#L12" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>check_sql_query</code>(<strong><code>s</code></strong>)</p>
</blockquote>
<p>Checks whether <code>s</code> is a SQL query</p>

</div>

</div>

</div>
</div>

</div>
{% endraw %}

{% raw %}

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

</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="k">assert</span> <span class="n">check_sql_query</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">--- Table 1---</span>
<span class="s2">creaTe or replace table my_table as</span>
<span class="s2">select asdf</span>
<span class="s2">from table</span>
<span class="s2">where asdf = 1</span>
<span class="s2">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
</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="k">assert</span> <span class="n">check_sql_query</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">SELECT qwer, asdf</span>
<span class="s2">&quot;&quot;&quot;</span><span class="p">)</span>
</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="k">assert</span> <span class="ow">not</span> <span class="n">check_sql_query</span><span class="p">(</span><span class="s2">&quot;use database my_database;&quot;</span><span class="p">)</span>
</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="k">assert</span> <span class="ow">not</span> <span class="n">check_sql_query</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>

<span class="s2">use schema my_schema;</span>
<span class="s2">&quot;&quot;&quot;</span><span class="p">)</span>
</pre></div>

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

</div>
{% endraw %}

<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Marker-to-not-format-queries-specified-by-user">Marker to not format queries specified by user<a class="anchor-link" href="#Marker-to-not-format-queries-specified-by-user"> </a></h3>
</div>
</div>
</div>
{% raw %}

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

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

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="check_skip_marker" class="doc_header"><code>check_skip_marker</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/format_file.py#L17" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>check_skip_marker</code>(<strong><code>s</code></strong>)</p>
</blockquote>
<p>Checks whether user set marker /<em>skip-formatter</em>/ to not format query</p>

</div>

</div>

</div>
</div>

</div>
{% endraw %}

{% raw %}

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

</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="k">assert</span> <span class="n">check_skip_marker</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">SELECT asdf,</span>
<span class="s2"> qwer,</span>
<span class="s2"> /*skip-formatter*/</span>
<span class="s2"> asdf2</span>
<span class="s2">FRoM table1</span>
<span class="s2">&quot;&quot;&quot;</span><span class="p">)</span>
</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="k">assert</span> <span class="ow">not</span> <span class="n">check_skip_marker</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">SELECT asdf,</span>
<span class="s2"> qwer,</span>
<span class="s2"> asdf2</span>
<span class="s2">FRoM table1</span>
<span class="s2">&quot;&quot;&quot;</span><span class="p">)</span>
</pre></div>

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

</div>
{% endraw %}

<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Main-function-formatting-SQL-commands">Main function formatting SQL commands<a class="anchor-link" href="#Main-function-formatting-SQL-commands"> </a></h3>
Expand All @@ -376,7 +176,7 @@ <h3 id="Main-function-formatting-SQL-commands">Main function formatting SQL comm


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_sql_commands" class="doc_header"><code>format_sql_commands</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/format_file.py#L22" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_sql_commands</code>(<strong><code>s</code></strong>)</p>
<h4 id="format_sql_commands" class="doc_header"><code>format_sql_commands</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/format_file.py#L13" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_sql_commands</code>(<strong><code>s</code></strong>)</p>
</blockquote>
<p>Format SQL commands in <code>s</code></p>

Expand Down Expand Up @@ -435,15 +235,17 @@ <h4 id="format_sql_commands" class="doc_header"><code>format_sql_commands</code>
FROM cars) as b
ON a.car_id = b.car_id
WHERE car_id &gt; 1
and car_id &lt;= 100;
and car_id &lt;= 100
ORDER BY b.car_name;


-- Table no. 1 --
CREATE OR REPLACE TABLE first_table AS -- my first table
SELECT car_id,
avg(price) as avg_price
FROM first_view
GROUP BY car_id;
GROUP BY car_id
ORDER BY car_id;

--- End of file ---

Expand Down Expand Up @@ -594,7 +396,7 @@ <h3 id="Function-to-format-1-SQL-file">Function to format 1 SQL file<a class="an


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_sql_file" class="doc_header"><code>format_sql_file</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/format_file.py#L42" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_sql_file</code>(<strong><code>f</code></strong>)</p>
<h4 id="format_sql_file" class="doc_header"><code>format_sql_file</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/format_file.py#L33" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_sql_file</code>(<strong><code>f</code></strong>)</p>
</blockquote>
<p>Format file <code>f</code> with SQL commands and overwrite the file.
Return 0 for no change and 1 for formatting adjustments</p>
Expand Down Expand Up @@ -660,15 +462,17 @@ <h4 id="format_sql_file" class="doc_header"><code>format_sql_file</code><a href=
FROM cars) as b
ON a.car_id = b.car_id
WHERE car_id &gt; 1
and car_id &lt;= 100;
and car_id &lt;= 100
ORDER BY b.car_name;


-- Table no. 1 --
CREATE OR REPLACE TABLE first_table AS -- my first table
SELECT car_id,
avg(price) as avg_price
FROM first_view
GROUP BY car_id;
GROUP BY car_id
ORDER BY car_id;

--- End of file ---

Expand Down Expand Up @@ -699,7 +503,7 @@ <h3 id="Function-to-format-many-SQL-files">Function to format many SQL files<a c


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_sql_files" class="doc_header"><code>format_sql_files</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/format_file.py#L57" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_sql_files</code>(<strong><code>files</code></strong>:"Path to SQL files")</p>
<h4 id="format_sql_files" class="doc_header"><code>format_sql_files</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/format_file.py#L48" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_sql_files</code>(<strong><code>files</code></strong>:"Path to SQL files")</p>
</blockquote>
<p>Format SQL <code>files</code></p>

Expand Down Expand Up @@ -774,15 +578,17 @@ <h4 id="format_sql_files" class="doc_header"><code>format_sql_files</code><a hre
FROM cars) as b
ON a.car_id = b.car_id
WHERE car_id &gt; 1
and car_id &lt;= 100;
and car_id &lt;= 100
ORDER BY b.car_name;


-- Table no. 1 --
CREATE OR REPLACE TABLE first_table AS -- my first table
SELECT car_id,
avg(price) as avg_price
FROM first_view
GROUP BY car_id;
GROUP BY car_id
ORDER BY car_id;

--- End of file ---

Expand All @@ -801,15 +607,17 @@ <h4 id="format_sql_files" class="doc_header"><code>format_sql_files</code><a hre
FROM cars) as b
ON a.car_id = b.car_id
WHERE car_id &gt; 1
and car_id &lt;= 100;
and car_id &lt;= 100
ORDER BY b.car_name;


-- Table no. 1 --
CREATE OR REPLACE TABLE first_table AS -- my first table
SELECT car_id,
avg(price) as avg_price
FROM first_view
GROUP BY car_id;
GROUP BY car_id
ORDER BY car_id;

--- End of file ---

Expand Down
3 changes: 2 additions & 1 deletion docs/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"sql_formatter": {
"Overview": "/",
"core": "core.html",
"format_file": "format_file.html"
"format_file": "format_file.html",
"utils": "utils.html"
}
}
Loading