scalikejdbc.autoNamedValues.scala Maven / Gradle / Ivy
package scalikejdbc
import scala.language.experimental.macros
import scalikejdbc.MacroCompatible.Context
object autoNamedValues {
def apply_impl[E: c.WeakTypeTag](c: Context)(entity: c.Expr[E], column: c.Expr[ColumnName[E]], excludes: c.Expr[String]*): c.Expr[Map[SQLSyntax, ParameterBinder]] = {
import c.universe._
val toMapParams: List[c.universe.Tree] = EntityUtil.constructorParams[E](c)("autoNamedValues", excludes: _*).map { field =>
val fieldName = field.name.toTermName
q"$column.$fieldName -> $entity.$fieldName"
}
c.Expr[Map[SQLSyntax, ParameterBinder]](q"_root_.scala.collection.immutable.Map(..$toMapParams)")
}
def debug_impl[E: c.WeakTypeTag](c: Context)(entity: c.Expr[E], column: c.Expr[ColumnName[E]], excludes: c.Expr[String]*): c.Expr[Map[SQLSyntax, ParameterBinder]] = {
val expr = apply_impl[E](c)(entity, column, excludes: _*)
println(expr.tree)
expr
}
def debug[E](entity: E, column: ColumnName[E], excludes: String*): Map[SQLSyntax, ParameterBinder] = macro debug_impl[E]
def apply[E](entity: E, column: ColumnName[E], excludes: String*): Map[SQLSyntax, ParameterBinder] = macro apply_impl[E]
}