scala jdbc/quill

Quill 기본 사용

wefree 2023. 6. 13. 13:22

https://zio.dev/zio-quill/ 의 기본 사용법

build.sbt

libraryDependencies += "io.getquill" %% "quill-jdbc" % "4.6.1"
libraryDependencies += "mysql" % "mysql-connector-java" % "8.0.33"

 

코드

package com.github.windbird123

import com.mysql.cj.jdbc.MysqlDataSource
import com.zaxxer.hikari.{HikariConfig, HikariDataSource}
import io.getquill._

case class Person(name: String, age: Int)
case class Info(name: String, address: String)

object QuillMain {
  def main(args: Array[String]): Unit = {
    val mysqlDataSource = new MysqlDataSource()
    mysqlDataSource.setUrl("jdbc:mysql://localhost:3306/test")
    mysqlDataSource.setUser("admin")
    mysqlDataSource.setPassword("mypasswd")
    mysqlDataSource.setAutoReconnect(true)

    val hikariConfig = new HikariConfig()
    hikariConfig.setDataSource(mysqlDataSource)
    hikariConfig.setMaximumPoolSize(10)

    // LowerCase: Person case class --> person table
    // UpperCase: Person case class --> PERSON table
    // SnakeCase
    // CamelCase
    val ctx = new MysqlJdbcContext(LowerCase, new HikariDataSource(hikariConfig))
    import ctx._

    ///////////////////////////////////////////////////////
    // map
    ///////////////////////////////////////////////////////
    val query1 = quote {
      query[Person].map(_.name)
    }

    val out1: List[String] = ctx.run(query1)
    println(out1)

    ///////////////////////////////////////////////////////
    // filter
    ///////////////////////////////////////////////////////
    val query2 = quote {
      query[Person].filter(_.age >= 10)
    }

    val out2: List[Person] = ctx.run(query2)
    println(out2)

    ///////////////////////////////////////////////////////
    // lifting
    ///////////////////////////////////////////////////////
    // select name, age from person where person.name = ?
    def find(name: String): List[Person] = {
      val query3 = quote {
        query[Person].filter(_.name == lift(name))
      }

      ctx.run(query3)
    }
    println(find("KJM"))

    ///////////////////////////////////////////////////////
    // Joins
    ///////////////////////////////////////////////////////
    val query4 = quote {
      query[Person]
        .join(query[Info])
        .on(_.name == _.name)
        .filter { case (person, info) =>
          person.age >= 10
        }
    }
    val out4: List[(Person, Info)] = ctx.run(query4)
    println(out4)

    ///////////////////////////////////////////////////////
    // Inserts
    ///////////////////////////////////////////////////////
    val query5 = quote {
      query[Person].insertValue(Person("AAA", 50))
    }
    ctx.run(query5)

    ///////////////////////////////////////////////////////
    // Batch Inserts
    ///////////////////////////////////////////////////////
    val persons = List(Person("A", 1), Person("B", 2))
    val query6 = quote {
      liftQuery(persons).foreach(p => query[Person].insertValue(p))
    }
    ctx.run(query6)

    ///////////////////////////////////////////////////////
    // Updates
    ///////////////////////////////////////////////////////
    val query7 = quote {
      query[Person].filter(_.name == "A").update(_.age -> 33)
    }
    ctx.run(query7)

    ///////////////////////////////////////////////////////
    // transactions
    ///////////////////////////////////////////////////////
    ctx.transaction {
      ctx.run(query4)
      ctx.run(query7)
      throw new Exception()
    }
  }
}