tconbeer / sqlfmt

sqlfmt formats your dbt SQL files so you don't have to
https://sqlfmt.com
Apache License 2.0
364 stars 15 forks source link

Support pg-promise Query Files with Named Parameters #556

Open cmcnicoll opened 5 months ago

cmcnicoll commented 5 months ago

I use sqlfmt for all my dbt projects. Thank you @tconbeer for creating this wonderful tool!

It would be great if sqlfmt could also support this:

select * from users where id = ${id}

Docs

tconbeer commented 3 months ago

From pg-promise docs:

Named Parameters

When a query method is parameterized with values as an object, the formatting engine expects the query to use the Named Parameter syntax $*propName*, with * being any of the following open-close pairs: {}, (), <>, [], //.

// We can use every supported variable syntax at the same time, if needed:
await db.none('INSERT INTO users(first_name, last_name, age) VALUES(${name.first}, $<name.last>, $/age/)', {
    name: {first: 'John', last: 'Dow'},
    age: 30
});
cmcnicoll commented 3 months ago

Here is the workaround I've been using:

  1. Run a script to quote named parameters in query file example.sql:
    select * from users where id = '${id}'
  2. Use sqlfmt via dbt Power User
  3. Test query manually
  4. Run another script to unquote named parameters
  5. Test query in app