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

Code generation fails for query with a conditional parameter #144

Open
akshaydewan opened this issue Dec 11, 2020 · 0 comments
Open

Code generation fails for query with a conditional parameter #144

akshaydewan opened this issue Dec 11, 2020 · 0 comments

Comments

@akshaydewan
Copy link

Consider the query

SELECT
  COUNT(*)
FROM some_table
  WHERE
    CASE
        WHEN :username IS NULL THEN 1 = 1 ELSE username = :username
      END

The following exception is thrown during code generation:

org.postgresql.util.PSQLException: ERROR: could not determine data type of parameter $1
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2532)
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2267)
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:312)
	at org.postgresql.jdbc.PgPreparedStatement.getParameterMetaData(PgPreparedStatement.java:1585)
	at norm.analyzer.SqlAnalyzer.sqlModel(SqlAnalyzer.kt:23)
	at norm.api.NormApi.generate(NormApi.kt:22)
	at norm.cli.NormCli$run$1$2$1.invoke(NormCli.kt:80)
	at norm.cli.NormCli$run$1$2$1.invoke(NormCli.kt:28)
	at norm.fs.IO.process(IO.kt:22)
	at norm.cli.NormCli$run$1.invoke(NormCli.kt:80)
	at norm.cli.NormCli$run$1.invoke(NormCli.kt:28)
	at norm.util.PGKt.withPGConnection(PG.kt:14)
	at norm.cli.NormCli.run(NormCli.kt:68)
	at com.github.ajalt.clikt.parsers.Parser.parse(Parser.kt:168)
	at com.github.ajalt.clikt.parsers.Parser.parse(Parser.kt:16)
	at com.github.ajalt.clikt.core.CliktCommand.parse(CliktCommand.kt:258)
	at com.github.ajalt.clikt.core.CliktCommand.parse$default(CliktCommand.kt:255)
	at com.github.ajalt.clikt.core.CliktCommand.main(CliktCommand.kt:273)
	at com.github.ajalt.clikt.core.CliktCommand.main(CliktCommand.kt:298)
	at norm.cli.NormCliKt.main(NormCli.kt:20)

Possible workaround (for postgres) - cast the parameter in the query

SELECT
  COUNT(*)
FROM some_table
  WHERE
    CASE
        WHEN CAST(:username AS VARCHAR) IS NULL THEN 1 = 1 ELSE username = CAST(:username AS VARCHAR)
      END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant