Skip to content

Using scalameta to get update class and insert/update map from model case class

License

Notifications You must be signed in to change notification settings

nischalbasnet/norm-scalikejdbc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Macro annotations to add orm functionality to case class

Example

import com.nischal.macros.UpdateMap

@UpdateMap
case class UserUpdate(
  first_name: Option[String],
  middle_name: Option[String],
  last_name: Option[String],
  age: Option[Int]
)

Expands To:

case class UserUpdate(
first_name: Option[String],
middle_name: Option[String],
last_name: Option[String],
age: Option[Int]) {

  def updateSQL(
    nullables: Seq[String] = Seq.empty
  ): _root_.scala.collection.immutable.Map[String, scalikejdbc.SQLSyntax] = {
    val updateMap = _root_.scala.collection.mutable.Map[String, scalikejdbc.SQLSyntax]()
    if (first_name.isDefined) updateMap.put("first_name", scalikejdbc.nischalmod.SQLBinder.bind(first_name.get)) 
    else if (nullables.contains("first_name")) updateMap.put("first_name", scalikejdbc.nischalmod.SQLBinder.bind(null))
    if (middle_name.isDefined) updateMap.put("middle_name", scalikejdbc.nischalmod.SQLBinder.bind(middle_name.get)) 
    else if (nullables.contains("middle_name")) updateMap.put("middle_name", scalikejdbc.nischalmod.SQLBinder.bind(null))
    if (last_name.isDefined) updateMap.put("last_name", scalikejdbc.nischalmod.SQLBinder.bind(last_name.get)) 
    else if (nullables.contains("last_name")) updateMap.put("last_name", scalikejdbc.nischalmod.SQLBinder.bind(null))
    if (age.isDefined) updateMap.put("age", scalikejdbc.nischalmod.SQLBinder.bind(age.get)) 
    else if (nullables.contains("age")) updateMap.put("age", scalikejdbc.nischalmod.SQLBinder.bind(null))
    
    updateMap.toMap
  }
}
import com.nischal.macros.NormModel
import com.nischal.norm.INormModel

@NormModel
case class Person(
  id: String,
  @update first_name: String,
  @update middle_name: Option[String],
  @update last_name: String,
  @update email: String,
  @update age: Option[Int]
) extends INormModel

Expands To

case class Person(
id: String,
@update first_name: String,
@update middle_name: Option[String],
@update last_name: String,
@update email: String,
@update age: Option[Int]
) extends INormModel() {

  private val _updateForm = Person.Update()
  
  def insertSQL(exclude: Seq[String] = Seq.empty): _root_.scala.collection.immutable.Map[String, scalikejdbc.SQLSyntax] = {
    val insertMap = _root_.scala.collection.mutable.Map[String, scalikejdbc.SQLSyntax]()
    if (!exclude.contains("id")) insertMap.put("id", scalikejdbc.nischalmod.SQLBinder.bind(id))
    if (!exclude.contains("first_name")) insertMap.put("first_name", scalikejdbc.nischalmod.SQLBinder.bind(first_name))
    if (!exclude.contains("middle_name")) insertMap.put("middle_name", scalikejdbc.nischalmod.SQLBinder.bind(middle_name))
    if (!exclude.contains("last_name")) insertMap.put("last_name", scalikejdbc.nischalmod.SQLBinder.bind(last_name))
    if (!exclude.contains("email")) insertMap.put("email", scalikejdbc.nischalmod.SQLBinder.bind(email))
    if (!exclude.contains("age")) insertMap.put("age", scalikejdbc.nischalmod.SQLBinder.bind(age))
    insertMap.toMap
  }
  
  def updateSQL() = _updateForm.updateSQL()
  
  def setFirstName(first_name: String, setNull: Boolean = false) = {
    if (setNull) _updateForm.setNullValue("first_name") else _updateForm.first_name = Some(first_name)
    this
  }
  def setMiddleName(middle_name: String, setNull: Boolean = false) = {
    if (setNull) _updateForm.setNullValue("middle_name") else _updateForm.middle_name = Some(middle_name)
    this
  }
  def setLastName(last_name: String, setNull: Boolean = false) = {
    if (setNull) _updateForm.setNullValue("last_name") else _updateForm.last_name = Some(last_name)
    this
  }
  def setEmail(email: String, setNull: Boolean = false) = {
    if (setNull) _updateForm.setNullValue("email") else _updateForm.email = Some(email)
    this
  }
  def setAge(age: Int, setNull: Boolean = false) = {
    if (setNull) _updateForm.setNullValue("age") else _updateForm.age = Some(age)
    this
  }
}

object Person {
  case class Update(
  var first_name: Option[String] = None, 
  var middle_name: Option[String] = None, 
  var last_name: Option[String] = None, 
  var email: Option[String] = None, 
  var age: Option[Int] = None
  ) extends com.nischal.norm.INormModelUpdater {
    private val _nullValues: _root_.scala.collection.mutable.ListBuffer[String] = _root_.scala.collection.mutable.ListBuffer()
    
    def setNullValue(value: String) = _nullValues.append(value)
    def setNullValue(values: Seq[String]) = _nullValues.appendAll(values)
    
    def updateSQL(
      nullables: Seq[String] = _nullValues
    ): _root_.scala.collection.immutable.Map[String, scalikejdbc.SQLSyntax] = {
      val updateMap = _root_.scala.collection.mutable.Map[String, scalikejdbc.SQLSyntax]()
      if (first_name.isDefined) updateMap.put("first_name", scalikejdbc.nischalmod.SQLBinder.bind(first_name.get)) 
      else if (nullables.contains("first_name")) updateMap.put("first_name", scalikejdbc.nischalmod.SQLBinder.bind(null))
      if (middle_name.isDefined) updateMap.put("middle_name", scalikejdbc.nischalmod.SQLBinder.bind(middle_name.get)) 
      else if (nullables.contains("middle_name")) updateMap.put("middle_name", scalikejdbc.nischalmod.SQLBinder.bind(null))
      if (last_name.isDefined) updateMap.put("last_name", scalikejdbc.nischalmod.SQLBinder.bind(last_name.get)) 
      else if (nullables.contains("last_name")) updateMap.put("last_name", scalikejdbc.nischalmod.SQLBinder.bind(null))
      if (email.isDefined) updateMap.put("email", scalikejdbc.nischalmod.SQLBinder.bind(email.get)) 
      else if (nullables.contains("email")) updateMap.put("email", scalikejdbc.nischalmod.SQLBinder.bind(null))
      if (age.isDefined) updateMap.put("age", scalikejdbc.nischalmod.SQLBinder.bind(age.get)) 
      else if (nullables.contains("age")) updateMap.put("age", scalikejdbc.nischalmod.SQLBinder.bind(null))
      
      updateMap.toMap
    }
  }
}

Note: Intellij has issue expanding the NormModel annotation. And ouputs were formatted later and is not exactly how it looks in console.

About

Using scalameta to get update class and insert/update map from model case class

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages