Git Product home page Git Product logo

scalikejdbc's Introduction

ScalikeJDBC - SQL-Based DB Access Library for Scala

Just write SQL

ScalikeJDBC is a SQL-based DB access library for Scala developers.

This library naturally wraps JDBC APIs and provides you easy-to-use APIs.

Users do nothing other than writing SQL and mapping from java.sql.ResultSet objects to Scala values.

If you want to create mapper modules easily, also take a look at scalikejdbc-mapper-generator.

https://github.com/seratch/scalikejdbc/tree/master/scalikejdbc-mapper-generator

Supported RDBMS

We never release without passing all the unit tests with the following RDBMS.

  • PostgreSQL
  • MySQL
  • H2 Database Engine
  • HSQLDB
  • SQLite

Build Status

Scaladoc

Here is the scaladoc:

http://seratch.github.com/scalikejdbc/api/index.html#scalikejdbc.package

Setup

sbt

libraryDependencies += "com.github.seratch" %% "scalikejdbc" % "[1.4,)"

// slf4j binding you like
libraryDependencies += "org.slf4j" % "slf4j-simple" % "[1.7,)"

Maven

<dependency>
  <groupId>com.github.seratch</groupId>
  <artifactId>scalikejdbc_2.9.2</artifactId>
  <version>[1.4,)</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>[1.7,)</version>
</dependency>

Try it now

Try ScalikeJDBC right now!

git clone git://github.com/seratch/scalikejdbc.git
cd scalikejdbc/sandbox
sbt console

"users" table is already created. You can execute queries as follows:

case class User(id: Long, name: String)

val users = DB readOnly { implicit s => 
  SQL("select * from users")
    .map(rs => User(rs.long("id"), rs.string("name")))
    .list.apply()
}

DB localTx { implicit s => 
  SQL("insert into users (id, name) values ({id}, {name})")
    .bindByName('id -> 3, 'name -> "Charles")
    .update.apply() 
}

If you need more information(connection management, transaction, CRUD), please check the following wiki page or scaladoc in detail.

https://github.com/seratch/scalikejdbc/wiki/GettingStarted

http://seratch.github.com/scalikejdbc/api/index.html#scalikejdbc.package

Basic SQL template

The most basic way is just using prepared statement as follows.

SQL("""insert into users values (?, ?, ?, ?)""")
  .bind(132430, "[email protected]", "Bob", "xfewSZe2sd3w")
  .update.apply()

Anorm SQL template

Instead of embedding ?(place holder), you can specify named place holder that is similar to Anorm.

SQL("""insert into users values ({id}, {email}, {name}, {encryptedPassword})""")
  .bindByName(
    'id -> 132430,
    'emal -> "[email protected]",
    'name -> "Bob",
    'encryptedPassword -> "xfewSZe2sd3w")
  .update.apply()

Executable SQL template

Instead of embedding ?(place holder), you can specify executable SQL as template. Using this API, it's possible to validate SQL before building into application.

Usage is simple. Use Scala Symbol literal in comment with dummy value in SQL template, and pass named values by using not bind(Any*) but bindByName((Symbol, Any)*). When some of the passed names by #bindByName are not used, or #bind is used although the template seems to be executable SQL template, runtime exception will be thrown.

SQL("""
  insert into users values (
    /*'id*/123,
    /*'email*/'[email protected]',
    /*'name*/'Alice',
    /*'encryptedPassword*/'123456789012')
""")
  .bindByName(
    'id -> 132430,
    'emal -> "[email protected]",
    'name -> "Bob",
    'encryptedPassword -> "xfewSZe2sd3w")
  .update.apply()

SQLInterpolation for Scala 2.10

This feature is still experimental, but you can try it now.

https://github.com/seratch/scalikejdbc/tree/master/scalikejdbc-interpolation

def create(id: Long, email: String, name: String, encryptedPassword: Sting) {
  sql"insert into users values (${id}, ${email}, ${name}, ${encryptedPassword})"
    .update.apply()
}

Logging SQL And Timing

Using LogginSQLAndTime feature, you can check the actual SQL(not exactly) and time.

https://github.com/seratch/scalikejdbc/wiki/LoggingSQLAndTime

Mapper Generator

If you want to create mapper modules easily, also take a look at this sbt plugin.

sbt "scalikejdbc-gen [table-name (class-name)]"

https://github.com/seratch/scalikejdbc/tree/master/scalikejdbc-mapper-generator

Play framework 2.x support

You can use ScalikeJDBC with Play framework 2.x seamlessly.

https://github.com/seratch/scalikejdbc/tree/master/scalikejdbc-play-plugin

We promise you that it becomes more useful when using together with scalikejdbc-mapper-generator.

License

Apache License, Version 2.0

http://www.apache.org/licenses/LICENSE-2.0.html

scalikejdbc's People

Contributors

seratch avatar xuwei-k avatar tototoshi avatar

Watchers

Chris Birchall avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.