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

Support for type casts #893

Open
perotom opened this issue Jun 3, 2018 · 3 comments
Open

Support for type casts #893

perotom opened this issue Jun 3, 2018 · 3 comments

Comments

@perotom
Copy link

perotom commented Jun 3, 2018

It would be great if it is possible to support a custom binding symbol format. For example to generate the following statements with QueryDSL.

insert into message (type) values (cast(? as message_type))
or more postgresql specific
insert into message (type) values (?::message_type)

The would add great support for postgresql enums and other data types

@seratch
Copy link
Member

seratch commented Aug 18, 2018

How about doing like this? blog.seratch.net/post/76689657956/how-to-extend-scalikejdbc-querydsl (this URL is no longer available)

@sjurco
Copy link

sjurco commented Nov 27, 2018

How about doing like this:

package scalikejdbc

object SQLSyntaxParameterBinderFactory {
  def apply[T](syntax: T  SQLSyntax): ParameterBinderFactory[T] =
    (v: T) => SQLSyntaxParameterBinder(syntax(v))
}

must be in package scalikejdbc because SQLSyntaxParameterBinder is package private

object CustomType {
  implicit val customTypeParameterBinderFactory: ParameterBinderFactory[CustomType] =
    SQLSyntaxParameterBinderFactory(v  sqls"$v::custom_type")
}

just change custom_type and $v to make it work.

FYI: To read it back you need to define TypeBinder

  implicit val customTypeTypeBinder: TypeBinder[CustomType] = TypeBinder.string.map(CustomType.fromString)

Using this you can write just

insertInto(ClassWithCustomType).namedValues(autoNamedValues(c, ClassWithCustomType.column)

It could be done without custom Factory:

ParameterBinderFactory.sqlSyntaxParameterBinderFactory.contramap[CustomType](v  sqls"$v::custom_type")

but in this case contramap returns ContramappedParameterBinder which is not matched as SQLSyntaxParameterBinder in

private def addPlaceholders(sb: StringBuilder, param: Any): StringBuilder = param match {
case _: String => sb += '?'
case traversable: Traversable[_] => {
// e.g. in clause
traversable.map {
case SQLSyntax(s, _) => s
case SQLSyntaxParameterBinder(SQLSyntax(s, _)) => s
case _ => "?"
}.addString(sb, ", ")
}
case LastParameter => sb
case SQLSyntax(s, _) => sb ++= s
case SQLSyntaxParameterBinder(SQLSyntax(s, _)) => sb ++= s
case _ => sb += '?'
}

therefore simple question mark is used instead of custom sqls

@seratch could that be changed somehow so that anyone can define custom placeholder?

@seratch
Copy link
Member

seratch commented Dec 18, 2018

@sjurco Sorry for my belated reply. The solution you suggested looks fine. I welcome your pull requests to enable the new way.

@seratch seratch added this to the version 4.1.x milestone Jun 12, 2021
Saisse added a commit to Saisse/scalikejdbc that referenced this issue Mar 2, 2022
Saisse added a commit to Saisse/scalikejdbc that referenced this issue Mar 2, 2022
@seratch seratch modified the milestones: version 4.1.x, version 4.2.x Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants