Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Added modfied version of PlainSQL example to show how it works with regular classes #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package scala.slick.examples.jdbc

import scala.slick.session.Database
import Database.threadLocalSession
import scala.slick.jdbc.{GetResult, StaticQuery => Q}
import Q.interpolation

/**
* This example is a modified version of PlainSQL that shows how to use Slick's plain SQL with regular classes,
* unfortunately the 22 property limit of case classes is not overcome.
*
* A simple example that uses plain SQL queries against an in-memory
* H2 database. The example data comes from Oracle's JDBC tutorial at
* http://download.oracle.com/javase/tutorial/jdbc/basics/tables.html.
*/
object PlainSqlRegularClasses extends App {

// Classes (not case classes) for our data
class Supplier(val id: Int, val name: String, val street: String, val city: String, val state: String, val zip: String)
class Coffee(val name: String, val supID: Int, val price: Double, val sales: Int, val total: Int)

// Result set getters
implicit val getSupplierResult =
GetResult(r => new Supplier(r.nextInt, r.nextString, r.nextString, r.nextString, r.nextString, r.nextString))
implicit val getCoffeeResult = GetResult(r => new Coffee(r.<<, r.<<, r.<<, r.<<, r.<<))

Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver") withSession {

// Create the tables, including primary and foreign keys
Q.updateNA("create table suppliers("+
"id int not null primary key, "+
"name varchar not null, "+
"street varchar not null, "+
"city varchar not null, "+
"state varchar not null, "+
"zip varchar not null)").execute
Q.updateNA("create table coffees("+
"name varchar not null, "+
"sup_id int not null, "+
"price double not null, "+
"sales int not null, "+
"total int not null, "+
"foreign key(sup_id) references suppliers(id))").execute

// Insert some suppliers
(Q.u + "insert into suppliers values(101, 'Acme, Inc.', '99 Market Street', 'Groundsville', 'CA', '95199')").execute
(Q.u + "insert into suppliers values(49, 'Superior Coffee', '1 Party Place', 'Mendocino', 'CA', '95460')").execute
(Q.u + "insert into suppliers values(150, 'The High Ground', '100 Coffee Lane', 'Meadows', 'CA', '93966')").execute

def insert(c: Coffee) = (Q.u + "insert into coffees values (" +? c.name +
"," +? c.supID + "," +? c.price + "," +? c.sales + "," +? c.total + ")").execute

// Insert some coffees
Seq(
new Coffee("Colombian", 101, 7.99, 0, 0),
new Coffee("French_Roast", 49, 8.99, 0, 0),
new Coffee("Espresso", 150, 9.99, 0, 0),
new Coffee("Colombian_Decaf", 101, 8.99, 0, 0),
new Coffee("French_Roast_Decaf", 49, 9.99, 0, 0)
).foreach(insert)

// Iterate through all coffees and output them
println("Coffees:")
Q.queryNA[Coffee]("select * from coffees") foreach { c =>
println(" " + c.name + "\t" + c.supID + "\t" + c.price + "\t" + c.sales + "\t" + c.total)
}

// Perform a join to retrieve coffee names and supplier names for
// all coffees costing less than $9.00
println("Manual join:")
val q2 = Q.query[Double, (String, String)]("""
select c.name, s.name
from coffees c, suppliers s
where c.price < ? and s.id = c.sup_id
""")
// This time we read the result set into a List
val l2 = q2.list(9.0)
for (t <- l2) println(" " + t._1 + " supplied by " + t._2)

// Append to a StaticQuery
val supplierById = Q[Int, Supplier] + "select * from suppliers where id = ?"
println("Supplier #49: " + supplierById(49).first)

def coffeeByName(name: String) = sql"select * from coffees where name = $name".as[Coffee]
println("Coffee Colombian: " + coffeeByName("Colombian").firstOption)

def deleteCoffee(name: String) = sqlu"delete from coffees where name = $name".first
val rows = deleteCoffee("Colombian")
println(s"Deleted $rows rows")
println("Coffee Colombian: " + coffeeByName("Colombian").firstOption)
}
}