zio / zio-quill

Compile-time Language Integrated Queries for Scala
https://zio.dev/zio-quill
Apache License 2.0
2.15k stars 348 forks source link

Using an SQL keyword like "order" in a query results in an error #2614

Open adamsar opened 2 years ago

adamsar commented 2 years ago

Version: 4.4.1 Module: quill-jdbc-zio Database: postgres

Expected behavior

I have a table called orders in the database. I would assume this would be a fairly common thing for people developing commerce related applications. If I make a query like this:

run(orderSchema.filter { order => order.id > lift(id) })

I expect it to return orders great then the provided id

Actual behavior

It compiles correctly, but gives abstruse errors on execution (syntax errors)

Workaround

Just use any other name in functions, such as

run(orderSchema.filter { orders => orders.id > lift(id) })

@getquill/maintainers

guizmaii commented 1 year ago

Would it be valid in SQL to use the word "order"?

joelsonoda commented 7 months ago

I always quote my table names, column names, table aliases, column aliases, fixed column values to make sure that I don't have to think about database keywords in my class names or properties.

We do the following in mysql, but I suspect it would work similarly for the postgres dialect

trait NamingMysqlDialect extends MySQLDialect:
  override def tokenizeColumnAlias(strategy: NamingStrategy, column: String): String =
    strategy.column(column)

  override def tokenizeFixedColumn(strategy: NamingStrategy, column: String): String =
    strategy.column(column)

  override def tokenizeTableAlias(strategy: NamingStrategy, table: String): String =
    strategy.table(table)

  override def tokenizeIdentName(strategy: NamingStrategy, name: String): String =
    strategy.default(name)