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

MySQL toString method produces invalid syntax #247

Open
joelsonoda opened this issue Feb 23, 2023 · 0 comments
Open

MySQL toString method produces invalid syntax #247

joelsonoda opened this issue Feb 23, 2023 · 0 comments

Comments

@joelsonoda
Copy link

Version: (e.g. 4.6.0)
Module: (e.g. quill-sql)
Database: (e.g. mysql)

Expected behavior

When toString is called on a selected value using the MySQL dialect, it should produce mysql compatible sql, for example cast(x as CHAR)

Actual behavior

When toString is called on a selected value using the MySQL dialect, it produces invalid sql, for example
cast(x as VARCHAR).

Steps to reproduce the behavior

https://scastie.scala-lang.org/vonCXkfhS2GhqWCU0k5cZw

import io.getquill.*

val ctx = new SqlMirrorContext(MySQLDialect, SnakeCase)
import ctx.*

run( quote { 1.toString } )

Workaround

Here are a few of the workarounds:

  1. Create a custom conversion function
import io.getquill.*

val ctx = new SqlMirrorContext(MySQLDialect, SnakeCase)
import ctx.*

extension[T] (i: T)
  inline def asStringCol = quote {
    sql"""cast($i as CHAR)""".pure.as[String]
  }

run( quote { 1.toString } )

The disadvantage here is that quill will still compile and generate invalid sql if we forget to do a asStringCol and do a toString. We may not discover the issue until runtime.

  1. Modify the mysql dialect
object CustomDialect extends MySQLDialect:
  override def astTokenizer(
    implicit astTokenizer: Tokenizer[Ast], 
    strategy: NamingStrategy, 
    idiomContext: IdiomContext
  ): Tokenizer[Ast] =
    Tokenizer[Ast] {
      case Infix(List("cast(", " as VARCHAR)") , params, pure, transparent, quat) =>
        super.astTokenizer.token(Infix(List("cast(", " as CHAR)") , params, pure, transparent, quat))
      case ast => super.astTokenizer.token(ast)
    }

@getquill/maintainers

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