From 419ac701f9618165d352e30b4dae565dace7cf65 Mon Sep 17 00:00:00 2001 From: Mike Slinn Date: Fri, 4 Jan 2013 14:31:46 -0800 Subject: [PATCH 1/2] Added modfied version of PlainSQL example to show how it works with regular classes, so the 22 property limit can be overcome --- .../jdbc/PlainSqlRegularClasses.scala | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/main/scala/scala/slick/examples/jdbc/PlainSqlRegularClasses.scala diff --git a/src/main/scala/scala/slick/examples/jdbc/PlainSqlRegularClasses.scala b/src/main/scala/scala/slick/examples/jdbc/PlainSqlRegularClasses.scala new file mode 100644 index 0000000..86f2f45 --- /dev/null +++ b/src/main/scala/scala/slick/examples/jdbc/PlainSqlRegularClasses.scala @@ -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, + * so the 22 property limit of case classes could be 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) + } +} From e4019cf539b40238f17d511609730d96f2b0ff9d Mon Sep 17 00:00:00 2001 From: Mike Slinn Date: Sat, 5 Jan 2013 09:05:07 -0800 Subject: [PATCH 2/2] Update src/main/scala/scala/slick/examples/jdbc/PlainSqlRegularClasses.scala --- .../scala/slick/examples/jdbc/PlainSqlRegularClasses.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/scala/slick/examples/jdbc/PlainSqlRegularClasses.scala b/src/main/scala/scala/slick/examples/jdbc/PlainSqlRegularClasses.scala index 86f2f45..b8224a2 100644 --- a/src/main/scala/scala/slick/examples/jdbc/PlainSqlRegularClasses.scala +++ b/src/main/scala/scala/slick/examples/jdbc/PlainSqlRegularClasses.scala @@ -7,7 +7,7 @@ import Q.interpolation /** * This example is a modified version of PlainSQL that shows how to use Slick's plain SQL with regular classes, - * so the 22 property limit of case classes could be overcome. + * 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