sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
13.37k stars 803 forks source link

Error generating code when using parameters whose inferred names are go keywords #1412

Closed natasha-jarus closed 1 year ago

natasha-jarus commented 2 years ago

Version

1.10.0

What happened?

If you create a query with a single positional parameter whose inferred arg name is a go keyword (e.g., WHERE type = $1), sqlc reports a generation error:

error generating code: source error: 21:54: expected 'IDENT', found 'type'

Adding a second query parameter allows the code generation to succeed. Currently the workaround is to use an explicitly named parameter (e.g., WHERE type = @my_type).

Relevant log output

No response

Database schema

No response

SQL queries

-- error generating code:
SELECT *
FROM my_table
WHERE type = $1;

-- this works fine:
SELECT *
FROM my_table
WHERE type = $1 AND TRUE = $2;

-- this is also ok:
SELECT *
FROM my_table
WHERE type = @my_type;

Configuration

No response

Playground URL

No response

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

mohammadVatandoost commented 2 years ago

I like to work on this bug, please assign me. My Solution: In this if, we check Params.Column.name to see if it is a golang specific keywords or not. If it is, we should capitalize it. if len(query.Params) == 1 { p := query.Params[0] gq.Arg = QueryValue{ Name: paramName(p), Typ: goType(r, p.Column, settings), SQLPackage: sqlpkg, } }

Golang Specific Keywords are defined here.

ddway2 commented 1 year ago

Hi do you have already a fix for this problem ?

mohammadVatandoost commented 1 year ago

Hi, I checked the code base, My suggestion was applied to the code. It should be fixed now. I don't know why issue still is open.