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

Dollar sign in postgresql identifier causes code generate error #1357

Closed
Nlossae opened this issue Dec 23, 2021 · 1 comment
Closed

Dollar sign in postgresql identifier causes code generate error #1357

Nlossae opened this issue Dec 23, 2021 · 1 comment

Comments

@Nlossae
Copy link
Contributor

Nlossae commented Dec 23, 2021

Version

1.11.0

What happened?

When a function name is defined using a dollar sign ($) in the schema, sqlc reports an error:

error generating code: source error: 30:6: missing variable type or initialization (and 4 more errors)

Because the generator emits the identifier into the Go source:

func (q *Queries) Add(ctx context.Context, ) (int32, error) {
	row := q.db.QueryRowContext(ctx, add, )
	var f$n int32
	err := row.Scan(&f$n)
	return f$n, err
}

The PostgreSQL documentation states:

SQL identifiers and key words must begin with a letter (a-z, but also letters with diacritical marks and non-Latin letters) or an underscore (_). Subsequent characters in an identifier or key word can be letters, underscores, digits (0-9), or dollar signs ($).

4.1.1. Identifiers and Key Words

Additional points:

  • It seems that all identifiers are affected by this, not only function names. So even column names in table definitions are causing this error.
  • According to the PostgreSQL documentation, the dollar sign is not SQL standard compatible. So in the first place the question if identifiers with dollar signs should be reported as invalid or properly translated to valid go identifiers.
  • Interestingly the PostgresSQL identifier could also contain non-latin letters (e.g. áäâ...) but this matches the go language specification so these are fine. However I do not know if other language targets are affected by this.
  • Looking through the code the function names are not really relevant. So the naming of these could be quite simply changed. On the other side the column names are differently, as they are of course part of the public model. So possibly it would make sense to accept function names with dollar signs and reject column names with dollar signs.

Relevant log output

No response

Database schema

No response

SQL queries

CREATE FUNCTION f$n() RETURNS integer AS 'SELECT 1';

-- name: Fn :one
SELECT f$n();

Configuration

{
  "version": "1",
  "packages": [
    {
      "path": "db",
      "engine": "postgresql",
      "schema": "query.sql",
      "queries": "query.sql"
    }
  ]
}

Playground URL

https://play.sqlc.dev/p/5920ab6d1c170a37455d6c5bab505f22c5d660645d96a465feffb559571fd221

What operating system are you using?

Linux, Windows

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

@Nlossae Nlossae added bug Something isn't working triage New issues that hasn't been reviewed labels Dec 23, 2021
@kyleconroy kyleconroy added 📚 postgresql 💻 linux 💻 windows 🔧 golang and removed triage New issues that hasn't been reviewed labels Jan 26, 2022
@kyleconroy
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants