Martin Odersky created the Scala programming language in 2001 at EPFL. Some key events include the initial Java release in 2004, the Scala 2.0 release in 2006, and the founding of Typesafe by Odersky and Jonas Bonér in 2011 to support Scala. Scala can be used with build tools like Maven, Gradle, and SBT, and supports databases through libraries like Slick, ScalikeJDBC, and Squeryl. Frameworks like Play and Lift allow for web development in Scala. Testing libraries include ScalaCheck, Specs2, and Gatling.
10. DB - SCALIKEJDBC
10
PostgreSQL、MySQL、H2、HSQLDB、Oracle、SQL Server… …
val orders: List[Order] = withSQL {
select
.from(Order as o)
.innerJoin(Product as p).on(o.productId, p.id)
.leftJoin(Account as a).on(o.accountId, a.id)
.where.eq(o.productId, 123)
.orderBy(o.id).desc
.limit(4)
.offset(0)
}.map(Order(o, p, a)).list.apply()
SELECT
o.*, p.*, a.*
FROM
orders o
INNER JOIN products p ON o.product_id = p.id
LEFT JOIN accounts a ON o.account_id = a.id
WHERE o.product_id = 123
ORDER BY o.id DESC
LIMIT 4
OFFSET 0
11. DB - SQUERYL
11
Postgres、Oracle、MySQL、H2、DB2、SQL Server、Derby
def songCountByArtistId: Query[GroupWithMeasures[Long, Long]] =
from(artists, songs)((a, s) =>
where(a.id === s.artistId)
groupBy (a.id)
compute (countDistinct(s.id))
)
SELECT
Artist1.id AS g0, count(DISTINCT Song2.id) AS c0
FROM
Artist Artist1, Song Song2
WHERE
(Artist1.id = Song2.artistId)
GROUP BY
Artist1.id
12. DB - SLICK
12
Derby、H2、HSQLDB、Access、MySQL、PostgreSQL、SQLite、
Oracle、DB2 、SQL Server
val q = coffees.filter(_.price > 20)
.sortBy(_.name.desc)
.drop(10)
.take(5)
SELECT
*
FROM
coffees
WHERE
price > 20
ORDER BY
name DESC
LIMIT 5
OFFSET 10
13. WEB - PLAY
13
route
GET /hello/:username HelloController.say(username: String)
controller
import play.api.mvc.{Action, Controller}
object HelloController extends Controller {
def say(username: String) = Action {
Ok(s"hello, $username")
}
}
16. TEST - SCALACHECK
16
Test
object StringSpec extends Properties("String") {
property("startWith") = forAll { (a: String, b: String) =>
(a + b).startsWith(a)
}
property("concatenate") = forAll { (a: String, b: String) =>
(a + b).length > a.length && (a + b).length > b.length
}
}
Result
+ String.startWith: OK, passed 100 tests.
! String.concat: Falsified after 0 passed tests.
> ARG_0: ""
> ARG_1: ""
!
17. TEST - SPEC2
17
Test
import org.specs2.mutable._
class HelloWorldSpec extends Specification {
"The 'Hello world' string" should {
"contain 11 characters" in {
"Hello world" must have size(11)
}
"start with 'Hello'" in {
"Hello world" must startWith("Hello")
}
"end with 'world'" in {
"Hello world" must endWith("world")
}
}
}
18. TEST - GATLING
18
class BasicSimulation extends Simulation {
val httpConf = http.baseURL("http://tax.myhpsnet.com:8800")
val scn = scenario("BasicSimulation")
.exec(http("get an invoice")
.get("/api/invoices/d361bb03-e762-3842-b6ae-a3062d1e5cf7"))
.pause(1)
setUp(scn.inject(atOnceUsers(100)).protocols(httpConf))
}