Skip to content

Instantly share code, notes, and snippets.

@seratch
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seratch/28c07cbe788c467c9a48 to your computer and use it in GitHub Desktop.
Save seratch/28c07cbe788c467c9a48 to your computer and use it in GitHub Desktop.
SI-7420?
/* :paste
import scalikejdbc._
// initialize JDBC driver & connection pool
Class.forName("org.h2.Driver")
ConnectionPool.singleton("jdbc:h2:mem:hello", "user", "pass")
// ad-hoc session provider on the REPL
implicit val session = AutoSession
// table creation, you can run DDL by using #execute as same as JDBC
sql"""
create table members (
id serial not null primary key,
name varchar(64),
created_at timestamp not null
)
""".execute.apply()
// insert initial data
Seq("Alice", "Bob", "Chris") foreach { name =>
sql"insert into members (name, created_at) values (${name}, current_timestamp)".update.apply()
}
// for now, retrieves all data as Map value
val entities: List[Map[String, Any]] = sql"select * from members".map(_.toMap).list.apply()
// defines entity object and extractor
import org.joda.time._
case class Member(id: Long, name: Option[String], createdAt: DateTime)
object Member extends SQLSyntaxSupport[Member] {
override val tableName = "members"
def apply(rs: WrappedResultSet) = new Member(
rs.long("id"), rs.stringOpt("name"), rs.jodaDateTime("created_at"))
}
*/
scala> val c = Member.column
c: scalikejdbc.ColumnName[Member] = ColumnSQLSyntaxProvider(Member$@7d9798b)
scala> val ps = Seq(c.name -> "Alice", c.createdAt -> DateTime.now)
ps: Seq[(scalikejdbc.interpolation.SQLSyntax, Comparable[_ >: org.joda.time.ReadableInstant with String <: Object] with java.io.Serializable)] = List((SQLSyntax(value: name, parameters: List()),Alice), (SQLSyntax(value: created_at, parameters: List()),2014-05-27T12:51:49.370+09:00))
scala> update(Member).set(ps:_*).where.eq(c.name, "Alice").toSQL.update.apply()
<console>:21: error: applyDynamic does not support passing a vararg parameter
update(Member).set(ps:_*).where.eq(c.name, "Alice").toSQL.update.apply()
^
scala> val u = update(Member).set(ps:_*)
u: scalikejdbc.UpdateSQLBuilder = UpdateSQLBuilder(SQLSyntax(value: update members set name = ?, created_at = ?, parameters: List(Alice, 2014-05-27T12:51:49.370+09:00)))
scala> u.where.eq(c.name, "Alice").toSQL.update.apply()
12:52:40.791 [run-main-0] DEBUG s.StatementExecutor$$anon$1 - SQL execution completed
[SQL Execution]
update members set name = 'Alice', created_at = '2014-05-27 12:51:49.37' where name = 'Alice'; (2 ms)
[Stack Trace]
...
$line8.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$.<init>(<console>:22)
$line8.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$.<clinit>(<console>)
$line8.$eval$.$print$lzycompute(<console>:7)
$line8.$eval$.$print(<console>:6)
$line8.$eval.$print(<console>)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:736)
scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983)
scala.tools.nsc.interpreter.IMain$WrappedRequest$$anonfun$loadAndRunReq$1.apply(IMain.scala:590)
scala.tools.nsc.interpreter.IMain$WrappedRequest$$anonfun$loadAndRunReq$1.apply(IMain.scala:589)
scala.reflect.internal.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:31)
scala.reflect.internal.util.AbstractFileClassLoader.asContext(AbstractFileClassLoader.scala:19)
...
res1: Int = 1
@seratch
Copy link
Author

seratch commented May 27, 2014

  • ScalikeJDBC 2.0.0
  • Scala 2.11.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment