Skip to content

Instantly share code, notes, and snippets.

@seratch
Created October 3, 2012 15:28
Show Gist options
  • Save seratch/3827571 to your computer and use it in GitHub Desktop.
Save seratch/3827571 to your computer and use it in GitHub Desktop.
sbt dbconsole?
resolvers ++= Seq(
"Sonatype releases" at "http://oss.sonatype.org/content/repositories/releases",
"Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots"
)
libraryDependencies ++= Seq(
"com.github.seratch" %% "scalikejdbc" % "1.3.8-SNAPSHOT",
"org.slf4j" % "slf4j-simple" % "1.6.4",
"org.hsqldb" % "hsqldb" % "[2,)"
)
initialCommands := """import scalikejdbc._
Class.forName("org.hsqldb.jdbc.JDBCDriver")
ConnectionPool.singleton("jdbc:hsqldb:file:db/test", "", "")
DB autoCommit { implicit s =>
try {
SQL("create table users(id bigint primary key not null, name varchar(255))").execute.apply()
SQL("insert into users values ({id}, {name})").bindByName('id -> 1, 'name -> "Andy").update.apply()
SQL("insert into users values ({id}, {name})").bindByName('id -> 2, 'name -> "Brian").update.apply()
} catch { case e => println(e.getMessage) }
}
case class User(val id: Long, val name: String)
val * = (rs: WrappedResultSet) => new User(rs.long("id"), rs.string("name"))
implicit val session = DB.autoCommitSession
"""
// sbt console
Kazuhiros-MacBook-Air:sandbox seratch$ sbt console
[info] Loading project definition from /Users/seratch/tmp/scalikejdbc/sandbox/project
[info] Set current project to default-1dcd0f (in build file:/Users/seratch/tmp/scalikejdbc/sandbox/)
[info] Starting scala interpreter...
[info]
import scalikejdbc._
defined class User
*: scalikejdbc.WrappedResultSet => User = <function1>
session: scalikejdbc.DBSession = ActiveSession(jdbc:hsqldb:file:db/test, UserName=, HSQL Database Engine Driver,None,false)
Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_35).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :paste
// Entering paste mode (ctrl-D to finish)
case class SQLRunner(sql: String) {
def run(): Seq[Map[Symbol, Any]] = {
try {
SQL(sql).map(_.toSymbolMap()).list.apply()
} catch { case e: java.sql.SQLException =>
val result = SQL(sql).execute.apply()
Seq(Map('RESULT -> result))
}
}
}
implicit def stringToSQLRunner(sql: String) = SQLRunner(sql)
// Exiting paste mode, now interpreting.
defined class SQLRunner
stringToSQLRunner: (sql: String)SQLRunner
scala> "select * from users".run
res0: Seq[Map[Symbol,Any]] = List(Map('ID -> 1, 'NAME -> Andy), Map('ID -> 2, 'NAME -> Brian))
scala> "insert into users values (3, 'Charles')".run
res1: Seq[Map[Symbol,Any]] = List(Map('RESULT -> false))
scala> "select * from users".run
res2: Seq[Map[Symbol,Any]] = List(Map('ID -> 1, 'NAME -> Andy), Map('ID -> 2, 'NAME -> Brian), Map('ID -> 3, 'NAME -> Charles))
scala> :q
[success] Total time: 55 s, completed Oct 4, 2012 12:26:55 AM
case class SQLRunner(sql: String) {
def run(): Seq[Map[Symbol, Any]] = {
try {
SQL(sql).map(_.toSymbolMap()).list.apply()
} catch { case e: java.sql.SQLException =>
val result = SQL(sql).execute.apply()
Seq(Map('RESULT -> result))
}
}
}
implicit def stringToSQLRunner(sql: String) = SQLRunner(sql)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment