Created
April 3, 2014 14:04
-
-
Save seratch/9954969 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scalaVersion := "2.10.4" | |
libraryDependencies ++= Seq( | |
"org.scalikejdbc" %% "scalikejdbc" % "1.7.4", | |
"org.scalikejdbc" %% "scalikejdbc-interpolation" % "1.7.4", | |
"com.h2database" % "h2" % "[1.3,)", | |
"ch.qos.logback" % "logback-classic" % "[1.1,)" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> import scalikejdbc._, SQLInterpolation._ | |
import scalikejdbc._ | |
import SQLInterpolation._ | |
scala> | |
scala> Class.forName("org.h2.Driver") | |
res0: Class[_] = class org.h2.Driver | |
scala> ConnectionPool.singleton("jdbc:h2:mem:hello", "user", "pass") | |
23:04:02.568 [run-main] DEBUG scalikejdbc.ConnectionPool$ - Registered connection pool : ConnectionPool(url:jdbc:h2:mem:hello, user:user) | |
23:04:02.572 [run-main] DEBUG scalikejdbc.ConnectionPool$ - Registered singleton connection pool : ConnectionPool(url:jdbc:h2:mem:hello, user:user) | |
scala> implicit val session = AutoSession | |
session: scalikejdbc.AutoSession.type = AutoSession | |
scala> sql""" | |
| create table members ( | |
| id serial not null primary key, | |
| name varchar(64), | |
| created_at timestamp not null | |
| ) | |
| """.execute.apply() | |
23:04:04.289 [run-main] DEBUG s.StatementExecutor$$anon$1 - SQL execution completed | |
[SQL Execution] | |
create table members ( id serial not null primary key, name varchar(64), created_at timestamp not null ); (7 ms) | |
[Stack Trace] | |
... | |
$line10.$read$$iw$$iw$$iw$$iw$$iw$$iw$.<init>(<console>:21) | |
$line10.$read$$iw$$iw$$iw$$iw$$iw$$iw$.<clinit>(<console>) | |
$line10.$eval$.<init>(<console>:7) | |
$line10.$eval$.<clinit>(<console>) | |
$line10.$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:734) | |
scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983) | |
scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573) | |
scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604) | |
scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568) | |
scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:760) | |
... | |
res2: Boolean = false | |
scala> | |
scala> Seq("Alice", "Bob", "Chris", "Alice") foreach { name => | |
| sql"insert into members (name, created_at) values (${name}, current_timestamp)".update.apply() | |
| } | |
23:04:04.700 [run-main] DEBUG s.StatementExecutor$$anon$1 - SQL execution completed | |
[SQL Execution] | |
insert into members (name, created_at) values ('Alice', current_timestamp); (4 ms) | |
[Stack Trace] | |
... | |
$line11.$read$$iw$$iw$$iw$$iw$$iw$$iw$$anonfun$1.apply(<console>:16) | |
$line11.$read$$iw$$iw$$iw$$iw$$iw$$iw$$anonfun$1.apply(<console>:15) | |
scala.collection.immutable.List.foreach(List.scala:318) | |
$line11.$read$$iw$$iw$$iw$$iw$$iw$$iw$.<init>(<console>:15) | |
$line11.$read$$iw$$iw$$iw$$iw$$iw$$iw$.<clinit>(<console>) | |
$line11.$eval$.<init>(<console>:7) | |
$line11.$eval$.<clinit>(<console>) | |
$line11.$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:734) | |
scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983) | |
scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573) | |
... | |
23:04:04.702 [run-main] DEBUG s.StatementExecutor$$anon$1 - SQL execution completed | |
[SQL Execution] | |
insert into members (name, created_at) values ('Bob', current_timestamp); (0 ms) | |
[Stack Trace] | |
... | |
$line11.$read$$iw$$iw$$iw$$iw$$iw$$iw$$anonfun$1.apply(<console>:16) | |
$line11.$read$$iw$$iw$$iw$$iw$$iw$$iw$$anonfun$1.apply(<console>:15) | |
scala.collection.immutable.List.foreach(List.scala:318) | |
$line11.$read$$iw$$iw$$iw$$iw$$iw$$iw$.<init>(<console>:15) | |
$line11.$read$$iw$$iw$$iw$$iw$$iw$$iw$.<clinit>(<console>) | |
$line11.$eval$.<init>(<console>:7) | |
$line11.$eval$.<clinit>(<console>) | |
$line11.$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:734) | |
scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983) | |
scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573) | |
... | |
23:04:04.704 [run-main] DEBUG s.StatementExecutor$$anon$1 - SQL execution completed | |
[SQL Execution] | |
insert into members (name, created_at) values ('Chris', current_timestamp); (0 ms) | |
[Stack Trace] | |
... | |
$line11.$read$$iw$$iw$$iw$$iw$$iw$$iw$$anonfun$1.apply(<console>:16) | |
$line11.$read$$iw$$iw$$iw$$iw$$iw$$iw$$anonfun$1.apply(<console>:15) | |
scala.collection.immutable.List.foreach(List.scala:318) | |
$line11.$read$$iw$$iw$$iw$$iw$$iw$$iw$.<init>(<console>:15) | |
$line11.$read$$iw$$iw$$iw$$iw$$iw$$iw$.<clinit>(<console>) | |
$line11.$eval$.<init>(<console>:7) | |
$line11.$eval$.<clinit>(<console>) | |
$line11.$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:734) | |
scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983) | |
scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573) | |
... | |
23:04:04.706 [run-main] DEBUG s.StatementExecutor$$anon$1 - SQL execution completed | |
[SQL Execution] | |
insert into members (name, created_at) values ('Alice', current_timestamp); (1 ms) | |
[Stack Trace] | |
... | |
$line11.$read$$iw$$iw$$iw$$iw$$iw$$iw$$anonfun$1.apply(<console>:16) | |
$line11.$read$$iw$$iw$$iw$$iw$$iw$$iw$$anonfun$1.apply(<console>:15) | |
scala.collection.immutable.List.foreach(List.scala:318) | |
$line11.$read$$iw$$iw$$iw$$iw$$iw$$iw$.<init>(<console>:15) | |
$line11.$read$$iw$$iw$$iw$$iw$$iw$$iw$.<clinit>(<console>) | |
$line11.$eval$.<init>(<console>:7) | |
$line11.$eval$.<clinit>(<console>) | |
$line11.$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:734) | |
scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983) | |
scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573) | |
... | |
scala> | |
scala> import org.joda.time._ | |
import org.joda.time._ | |
scala> case class Member(id: Long, name: Option[String], createdAt: DateTime) | |
defined class Member | |
scala> object Member extends SQLSyntaxSupport[Member] { | |
| override val tableName = "members" | |
| def apply(rs: WrappedResultSet) = new Member( | |
| rs.long("id"), rs.stringOpt("name"), rs.dateTime("created_at")) | |
| } | |
defined module Member | |
warning: previously defined class Member is not a companion to object Member. | |
Companions must be defined together; you may wish to use :paste mode for this. | |
scala> | |
scala> import sqls.distinct | |
import sqls.distinct | |
scala> val m = Member.syntax("m") | |
m: scalikejdbc.SQLInterpolation.QuerySQLSyntaxProvider[scalikejdbc.SQLInterpolation.SQLSyntaxSupport[Member],Member] = QuerySQLSyntaxProvider(Member$@cf794d4,m) | |
scala> val names = withSQL { select(distinct(m.name)).from(Member as m).orderBy(m.name) }.map(_.string(1)).list.apply() | |
23:04:07.751 [run-main] DEBUG s.StatementExecutor$$anon$1 - SQL execution completed | |
[SQL Execution] | |
select distinct m.name from members m order by m.name; (2 ms) | |
[Stack Trace] | |
... | |
$line26.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$.<init>(<console>:23) | |
$line26.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$.<clinit>(<console>) | |
$line26.$eval$.<init>(<console>:7) | |
$line26.$eval$.<clinit>(<console>) | |
$line26.$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:734) | |
scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983) | |
scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573) | |
scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604) | |
scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568) | |
scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:760) | |
... | |
names: List[String] = List(Alice, Bob, Chris) | |
scala> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scalikejdbc._, SQLInterpolation._ | |
Class.forName("org.h2.Driver") | |
ConnectionPool.singleton("jdbc:h2:mem:hello", "user", "pass") | |
implicit val session = AutoSession | |
sql""" | |
create table members ( | |
id serial not null primary key, | |
name varchar(64), | |
created_at timestamp not null | |
) | |
""".execute.apply() | |
Seq("Alice", "Bob", "Chris", "Alice") foreach { name => | |
sql"insert into members (name, created_at) values (${name}, current_timestamp)".update.apply() | |
} | |
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.dateTime("created_at")) | |
} | |
import sqls.distinct | |
val m = Member.syntax("m") | |
val names = withSQL { select(distinct(m.name)).from(Member as m).orderBy(m.name) }.map(_.string(1)).list.apply() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment