miragesql-scala


License

License

Categories

Categories

Scala Languages
GroupId

GroupId

com.miragesql
ArtifactId

ArtifactId

miragesql-scala_2.12
Last Version

Last Version

2.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

miragesql-scala
miragesql-scala
Project URL

Project URL

https://github.com/mirage-sql/mirage-scala
Project Organization

Project Organization

com.miragesql
Source Code Management

Source Code Management

https://github.com/mirage-sql/mirage-scala

Download miragesql-scala_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/com.miragesql/miragesql-scala_2.12/ -->
<dependency>
    <groupId>com.miragesql</groupId>
    <artifactId>miragesql-scala_2.12</artifactId>
    <version>2.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.miragesql/miragesql-scala_2.12/
implementation 'com.miragesql:miragesql-scala_2.12:2.0.0'
// https://jarcasting.com/artifacts/com.miragesql/miragesql-scala_2.12/
implementation ("com.miragesql:miragesql-scala_2.12:2.0.0")
'com.miragesql:miragesql-scala_2.12:jar:2.0.0'
<dependency org="com.miragesql" name="miragesql-scala_2.12" rev="2.0.0">
  <artifact name="miragesql-scala_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.miragesql', module='miragesql-scala_2.12', version='2.0.0')
)
libraryDependencies += "com.miragesql" % "miragesql-scala_2.12" % "2.0.0"
[com.miragesql/miragesql-scala_2.12 "2.0.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.4
com.miragesql : miragesql jar 2.0.0
org.json4s : json4s-scalap_2.12 jar 3.5.3

test (3)

Group / Artifact Type Version
org.hsqldb : hsqldb jar 2.4.0
org.specs2 : specs2-core_2.12 jar 4.0.1
org.mockito : mockito-core jar 2.11.0

Project Modules

There are no modules declared in this project.

Mirage-SQL Scala Build Status Join the chat at https://gitter.im/mirage-sql/mirage-sql

The SQL-Centric Database Access Library for Scala.

Introduction

Mirage-SQL Scala is wrapper written in Scala for the Mirage SQL framework.

Among other features, it provides a dynamic SQL-Template language called: 2WaySQL.

Template directives are embedded as SQL comments, so that the 2WaySQL template is also executable as raw a SQL at the same time. This makes SQL files both dynamic and simply testable at the same time.

To use Mirage-SQL Scala with an SBT based project, just add following dependency to your build.sbt.

libraryDependencies += "com.miragesql" %% "miragesql-scala" % "2.0.0"

Links:

  • A more detailed documentation will provided in the central Wiki.
  • User Support in English is provided in the Gitter Chatroom.
  • If you find any bugs or issues, please report them in the GitHub Issue Tracker.

Usage

A. 2WaySQL Dynamic Template

This is a simple example to query using Mirage-SQL Scala:

First, define the DTO which is mapped to ResultList as case class. It's possible to specify Option[T] as property type for nullable properties.

// A class which mapped to ResultList
case class Book(
  bookId: IdentityPk[Int],
  bookName: String,
  author: String,
  price: Option[Int]
)

Execute the SQL using SqlManager. mirage-scala provides dynamic SQL template called 2waySQL in Sql().

import com.miragesql.miragesql.scala._

Session.withTransaction { session =>
  val sqlManager: SqlManager = session.sqlManager

  val books: List[Book] = sqlManager.getResultList[Book](
    Sql("""
      SELECT BOOK_ID, BOOK_NAME, AUTHOR, PRICE
      FROM BOOK
      /*IF author!=null*/
        WHERE AUTHOR = /*author*/
      /*END*/
    """), Map("author"->"Naoki Takezoe"))

  books.foreach { book =>
    println("bookId: " + book.bookId)
    println("bookName: " + book.bookName)
    println("author: " + book.author)
    println("price: " + book.price)
    println("--")
  }
}

You can also use an external SQL file using SqlFile():

val result: List[Book] = sqlManager.getResultList[Book](
  SqlFile("META-INF/selectBooks.sql"),
  Map("author"->"Naoki Takezoe"))

With Mirage-SQL Scala, it's also possible to use Map[String, _] as result class / parameter class instead of the case class.

See also the Mirage SQL Documentation to learn more about it's usage.

B. SQL-less Updates

Mirage-SQL Scala also supports SQL-less select / update using an entity class.

If the primary key is set at the server-side, for example, it's auto incremented, You have to specify Auto for the primary key property.

val book: Book = Book(
  Auto,
  "Mirage in Action",
  "Naoki Takezoe",
  Some(20)
)

sqlManager.insertEntity(book);

If the primary key must be set by the application, you can use Id(value) to set the value to the primary key property.

val book: Book = Book(
  Id(1),
  "Mirage in Action",
  "Naoki Takezoe",
  Some(20)
)

sqlManager.insertEntity(book);

Batch updating is also available.

// batch inserting
sqlManager.insertBatch(book1, book2, book3)
sqlManager.insertBatch(books: _*)

// batch updating
sqlManager.updateBatch(book1, book2, book3)
sqlManager.updateBatch(books: _*)

// batch deleting
sqlManager.deleteBatch(book1, book2, book3)
sqlManager.deleteBatch(books: _*)

C. ResultSet Streams

To handle large data, create streams by stream() method and process each records by foreach() method.

sqlManager
  .stream(Sql("SELECT BOOK_ID, BOOK_NAME, AUTHOR, PRICE FROM BOOK"))
  .foreach[Book] { book =>
    println(book)
  }

If you would like to aggregate streaming values, you can use foldLeft method instead:

val sum = sqlManager
  .stream(Sql("SELECT BOOK_ID, BOOK_NAME, AUTHOR, PRICE FROM BOOK"))
  .foldLeft[Book, Int](0){ case (book, i) =>
    i + book.price
  }
com.miragesql

Mirage SQL

Mirage SQL Framework

Versions

Version
2.0.0
1.3.0