Skip to content

Make sqlc.arg and sqlc.narg compatible with pgFormatter #2130

Closed
@twpayne

Description

@twpayne

What do you want to change?

pgFormatter is a tool for formatting SQL for PostgreSQL, effectively go fmt for SQL.

sqlc.arg and sqlc.narg are sqlc-specific tweaks to SQL that is used to support named parameters and nullable parameters.

pgFormatter by default puts spaces before function calls, which converts sqlc.arg(name)::text to sqlc.arg (name)::text, which sqlc then does not parse:

$ sudo apt install -y pgformatter
...

$ git clone https://github.com/kyleconroy/sqlc
...

$ cd sqlc/examples/batch

$ pg_format --inplace postgresql/query.sql

$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
	modified:   postgresql/query.sql

no changes added to commit

$ git grep sqlc.arg  
postgresql/query.sql:WHERE book_id = sqlc.arg (book_id);

$ sqlc generate
# package batch
postgresql/query.sql:18:1: edited query syntax is invalid: syntax error at or near ")"

Removing the space between sqlc.arg and (book_id) allows sqlc to parse the SQL:

$ sed -i 's/sqlc\.arg /sqlc.arg/' postgresql/query.sql

$ git grep sqlc.arg                                   
postgresql/query.sql:WHERE book_id = sqlc.arg(book_id);

$ sqlc generate

$ echo $?
0

It would be great if sqlc supported spaces between sqlc.arg/sqlc.narg and the following opening parenthesis to make it compatible with pgFormatter in pgFormatter's default configuration.

Note that, as a work-around, users can use the --placeholder option to pg_format to prevent pgFormatter from adding the space:

$ pg_format --inplace --placeholder 'sqlc\.n?arg\(.*?\)' postgresql/query.sql

What database engines need to be changed?

PostgreSQL

What programming language backends need to be changed?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions