An ode to sqlfmt
Published on .
WordPress database error: [<div style="clear:both"> </div><div class="queries" style="clear:both; margin_bottom:2px; border: red dotted thin;">Queries made or created this session were<br/>
<ol>
<li>Raw query:
SELECT * FROM wp_options WHERE </li>
<li>Rewritten:
SELECT * FROM wp_options WHERE </li>
<li>With Placeholders:
SELECT * FROM wp_options WHERE </li>
<li>Prepare:
SELECT * FROM wp_options WHERE </li>
</ol>
</div><div style="clear:both; margin_bottom:2px; border: red dotted thin;" class="error_message" style="border-bottom:dotted blue thin;">Error occurred at line 1644 in Function prepare_query. <br/> Error message was: Problem preparing the PDO SQL Statement. Error was: SQLSTATE[HY000]: General error: 1 incomplete input </div><pre>#0 /home/nattaylor/public_html/wordpress/wp-content/db.php(2746): WP_SQLite_DB\PDOEngine->get_error_message()
#1 /home/nattaylor/public_html/wordpress/wp-content/db.php(3484): WP_SQLite_DB\wpsqlitedb->query('...')
#2 /home/nattaylor/public_html/wordpress/wp-content/db.php(2952): WP_SQLite_DB\PDOSQLiteDriver->execute_duplicate_key_update()
#3 /home/nattaylor/public_html/wordpress/wp-content/db.php(1893): WP_SQLite_DB\PDOSQLiteDriver->rewrite_query('...', '...')
#4 /home/nattaylor/public_html/wordpress/wp-content/db.php(1357): WP_SQLite_DB\PDOEngine->execute_insert_query_new('...')
#5 /home/nattaylor/public_html/wordpress/wp-content/db.php(2739): WP_SQLite_DB\PDOEngine->query('...')
#6 /home/nattaylor/public_html/wordpress/wp-includes/option.php(1143): WP_SQLite_DB\wpsqlitedb->query('...')
#7 /home/nattaylor/public_html/wordpress/wp-includes/option.php(1552): add_option('...', 1758125757, '', '...')
#8 /home/nattaylor/public_html/wordpress/wp-content/plugins/syntax-highlighting-code-block/inc/functions.php(671): set_transient('...', Array, 2592000)
#9 /home/nattaylor/public_html/wordpress/wp-includes/class-wp-block.php(586): Syntax_Highlighting_Code_Block\render_block(Array, '...', Object(WP_Block))
#10 /home/nattaylor/public_html/wordpress/wp-includes/blocks.php(2359): WP_Block->render()
#11 /home/nattaylor/public_html/wordpress/wp-includes/blocks.php(2431): render_block(Array)
#12 /home/nattaylor/public_html/wordpress/wp-includes/class-wp-hook.php(324): do_blocks('...')
#13 /home/nattaylor/public_html/wordpress/wp-includes/plugin.php(205): WP_Hook->apply_filters('...', Array)
#14 /home/nattaylor/public_html/wordpress/wp-includes/post-template.php(256): apply_filters('...', '...')
#15 /home/nattaylor/public_html/wordpress/wp-content/themes/ntdc/index.php(70): the_content()
#16 /home/nattaylor/public_html/wordpress/wp-includes/template-loader.php(106): include('...')
#17 /home/nattaylor/public_html/wordpress/wp-blog-header.php(19): require_once('...')
#18 /home/nattaylor/public_html/wordpress/index.php(17): require('...')
</pre>]
SELECT * FROM wp_options WHERE
WordPress database error: [<div style="clear:both"> </div><div class="queries" style="clear:both; margin_bottom:2px; border: red dotted thin;">Queries made or created this session were<br/>
<ol>
<li>Raw query:
SELECT * FROM wp_options WHERE </li>
<li>Rewritten:
SELECT * FROM wp_options WHERE </li>
<li>With Placeholders:
SELECT * FROM wp_options WHERE </li>
<li>Prepare:
SELECT * FROM wp_options WHERE </li>
</ol>
</div><div style="clear:both; margin_bottom:2px; border: red dotted thin;" class="error_message" style="border-bottom:dotted blue thin;">Error occurred at line 1644 in Function prepare_query. <br/> Error message was: Problem preparing the PDO SQL Statement. Error was: SQLSTATE[HY000]: General error: 1 incomplete input </div><pre>#0 /home/nattaylor/public_html/wordpress/wp-content/db.php(2746): WP_SQLite_DB\PDOEngine->get_error_message()
#1 /home/nattaylor/public_html/wordpress/wp-content/db.php(3484): WP_SQLite_DB\wpsqlitedb->query('...')
#2 /home/nattaylor/public_html/wordpress/wp-content/db.php(2952): WP_SQLite_DB\PDOSQLiteDriver->execute_duplicate_key_update()
#3 /home/nattaylor/public_html/wordpress/wp-content/db.php(1893): WP_SQLite_DB\PDOSQLiteDriver->rewrite_query('...', '...')
#4 /home/nattaylor/public_html/wordpress/wp-content/db.php(1357): WP_SQLite_DB\PDOEngine->execute_insert_query_new('...')
#5 /home/nattaylor/public_html/wordpress/wp-content/db.php(2739): WP_SQLite_DB\PDOEngine->query('...')
#6 /home/nattaylor/public_html/wordpress/wp-includes/option.php(1143): WP_SQLite_DB\wpsqlitedb->query('...')
#7 /home/nattaylor/public_html/wordpress/wp-includes/option.php(1554): add_option('...', Array, '', '...')
#8 /home/nattaylor/public_html/wordpress/wp-content/plugins/syntax-highlighting-code-block/inc/functions.php(671): set_transient('...', Array, 2592000)
#9 /home/nattaylor/public_html/wordpress/wp-includes/class-wp-block.php(586): Syntax_Highlighting_Code_Block\render_block(Array, '...', Object(WP_Block))
#10 /home/nattaylor/public_html/wordpress/wp-includes/blocks.php(2359): WP_Block->render()
#11 /home/nattaylor/public_html/wordpress/wp-includes/blocks.php(2431): render_block(Array)
#12 /home/nattaylor/public_html/wordpress/wp-includes/class-wp-hook.php(324): do_blocks('...')
#13 /home/nattaylor/public_html/wordpress/wp-includes/plugin.php(205): WP_Hook->apply_filters('...', Array)
#14 /home/nattaylor/public_html/wordpress/wp-includes/post-template.php(256): apply_filters('...', '...')
#15 /home/nattaylor/public_html/wordpress/wp-content/themes/ntdc/index.php(70): the_content()
#16 /home/nattaylor/public_html/wordpress/wp-includes/template-loader.php(106): include('...')
#17 /home/nattaylor/public_html/wordpress/wp-blog-header.php(19): require_once('...')
#18 /home/nattaylor/public_html/wordpress/index.php(17): require('...')
</pre>]
SELECT * FROM wp_options WHERE
A colleague shared that I write the “cleanest and most understandable SQL queries he’s ever seen” and here’s my secret: https://sqlfmt.com/. It’s a SQL formatter that makes everything lowercase with pretty identation which I have adopted and am now advocate for. You might be wondering: “how much ad hoc SQL do you write?” …and that is an issue for another day, because the point of this story is that I’ve found tremendous value in consistently formatted SQL.
It only took a pip install
then about a half-day to fully commit, and now when I see YELLING KEYWORDS it makes me realize how much I like lowercase. The most important bit to my workflow is an Espanso shortcut :fmt
(below) so that I can use sqlfmt
anywhere I write SQL (be it an example, an ad hoc Looker query, a Slack message or within code).
I suppose it’s no different from any other formatter, but it’s very freeing to just freely write SQL without thinking about the formatting, then knowing it will turn out tidy and consistent. It is especially rewarding to know that colleagues also get value from this consistency.
One thing that tripped me is the potentially query-breaking handling of Snowflake dot notation where foo:Bar
becomes foo:bar
, which will break your query. Fix this by using quotes (eg foo:"Bar
“)
Give it a try!
Here’s my Espanso rule. I select the query, cut it onto my clipboard and type :fmt
- trigger: ":fmt"
replace: "{{output}}"
vars:
- name: output
type: shell
params:
cmd: "echo $(pbpaste) | sqlfmt -"