- 浏览: 2483501 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Slick(2)3 Working Types
1. Lifted Embedding
The name Lifted Embedding refers to the fact that you are not working with Standard Scala types, but with types that are lifted into a scala.sclick.liftted.Req type constructor.
var l: Lit[Coffee] *
val l2 = l.filter(_.price > 8.0).map(_.name)
=>
object Coffees extends Table[(String, Int, Double, Int, Int)]("COFFEES"){
…snip….
}
val q = Query(Coffees)
val q2 = q.filter(_.price > 8.0).map(_.name)
Rep[Double] Rep[Double] Rep[String]
Tables
Defined a table at first.
Mapped Tables
..snip…
Constraints
foreignKey
object Suppliers extends Table[(Int, String, String, String,String, String)]("SUPPLIERS"){
def id = column[Int]("SUP_ID", O.PrimaryKey)
…snip...
}
object Coffees extends Table[(String, Int, Double, Int, Int)]("COFFEES"){
def supID = column[Int]("SUP_ID")
…snip…
def supplier = foreignKey("SUP_FK", supID, Suppliers)(_.id)
}
A primary key constraint can be defined in a similar fashion by adding a method that calls primaryKey.
Object A extends Table[(Int, Int)]("a") {
def k1 = column[Int]("k1")
def k2 = column[Int]("k2")
def * = k1 ~ k2
def pk = primaryKey("pk_a",(k1, k2))
}
Object A extends Table[(Int, Int)]("a") {
def k1 = column[Int]("k1")
def k2 = column[Int]("k2")
def * = k1 ~ k2
def idx = index("idx_a", (k1, k2), unique=true)
}
Data Definition Language(DDL)
DDL statements for a table can be created with its ddl method. We can create and drop table with this function.
val ddl = Coffees.ddl ++ Suppliers.ddl
db withSession {
ddl.create
…snip…
ddl.drop
}
We can see the SQL with these statements>
ddl.createStatements.foreach(println)
ddl.dropStatements.foreach(println)
Output:
create table "SUPPLIERS" ("SUP_ID" INTEGER NOT NULL PRIMARY KEY,"SUP_NAME" VARCHAR NOT NULL,"STREET" VARCHAR NOT NULL,"CITY" VARCHAR NOT NULL,"STATE" VARCHAR NOT NULL,"ZIP" VARCHAR NOT NULL)
create table "COFFEES" ("COF_NAME" VARCHAR NOT NULL PRIMARY KEY,"SUP_ID" INTEGER NOT NULL,"PRICE" DOUBLE NOT NULL,"SALES" INTEGER NOT NULL,"TOTAL" INTEGER NOT NULL)
alter table "COFFEES" add constraint "SUP_FK" foreign key("SUP_ID") references "SUPPLIERS"("SUP_ID") on update NO ACTION on delete NO ACTION
alter table "COFFEES" drop constraint "SUP_FK"
drop table "SUPPLIERS"
drop table "COFFEES"
Expressions
…snip…
2. Direct Embedding
we did not use that in my project.
3. Plain SQL Queries
First of all the imports are different.
import scala.slick.session.Database
import Database.threadLocalSession
import scala.slick.jdbc.{GetResult, StaticQuery => Q}
Import scala.slick.jdbc.GetResult and import scala.sclick.jdbc.StaticQuery for Q.
The most important class for Plain SQL queries is scala.slick.jdbc.StaticQuery which gets imported as Q for more convenient use.
// Case classes for our data
case class Supplier(id: Int, name: String, street: String, city: String, state: String, zip: String)
case class Coffee(name: String, supID: Int, price: Double, sales: Int, total: Int)
DDL/DML Statements
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
…snip…
}
Q.u is StaticQuery.updateNA("")
// 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
We can use the special concatenation operator +? to add a bind variable to a query string.
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(
Coffee("Colombian", 101, 7.99, 0, 0),
Coffee("French_Roast", 49, 8.99, 0, 0),
Coffee("Espresso", 150, 9.99, 0, 0),
Coffee("Colombian_Decaf", 101, 8.99, 0, 0),
Coffee("French_Roast_Decaf", 49, 9.99, 0, 0)).foreach(insert)
Query Statements
// Result set getters
implicit val getSupplierResult = GetResult(r => Supplier(r.nextInt, r.nextString, r.nextString,
r.nextString, r.nextString, r.nextString))
implicit val getCoffeeResult = GetResult(r => Coffee(r.<<, r.<<, r.<<, r.<<, r.<<))
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)
}
There are predefined GetResult implicates for the standard JDBC types.
The first one uses the explicit PositionedResult methods nextInt and nextString Int or String.
The second one uses the shortcut method << which returns a value of whatever type is expected at this place.
query which takes two type parameters, one for the query parameters and one for the results set rows. Similarly for update and updateNA.
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)
valsupplierById = Q[Int, Supplier] + "select * from suppliers where id = ?"
println("Supplier #49: " + supplierById(49).first)
String Interpolation
import Q.interpolation
def coffeeByName(name: String) = sql"select * from coffees where name = $name".as[Coffee]
println("Coffee Colombian: " + coffeeByName("Colombian").first.name)
println("Coffee Colombian: " + coffeeByName("Colombian").firstOption)
sqlu seems to sql and update the table.
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)
References:
http://slick.typesafe.com/doc/0.11.2/index.html
http://slick.typesafe.com/doc/0.11.2/lifted-embedding.html
http://slick.typesafe.com/doc/0.11.2/direct-embedding.html
http://slick.typesafe.com/doc/0.11.2/sql.html
1. Lifted Embedding
The name Lifted Embedding refers to the fact that you are not working with Standard Scala types, but with types that are lifted into a scala.sclick.liftted.Req type constructor.
var l: Lit[Coffee] *
val l2 = l.filter(_.price > 8.0).map(_.name)
=>
object Coffees extends Table[(String, Int, Double, Int, Int)]("COFFEES"){
…snip….
}
val q = Query(Coffees)
val q2 = q.filter(_.price > 8.0).map(_.name)
Rep[Double] Rep[Double] Rep[String]
Tables
Defined a table at first.
Mapped Tables
..snip…
Constraints
foreignKey
object Suppliers extends Table[(Int, String, String, String,String, String)]("SUPPLIERS"){
def id = column[Int]("SUP_ID", O.PrimaryKey)
…snip...
}
object Coffees extends Table[(String, Int, Double, Int, Int)]("COFFEES"){
def supID = column[Int]("SUP_ID")
…snip…
def supplier = foreignKey("SUP_FK", supID, Suppliers)(_.id)
}
A primary key constraint can be defined in a similar fashion by adding a method that calls primaryKey.
Object A extends Table[(Int, Int)]("a") {
def k1 = column[Int]("k1")
def k2 = column[Int]("k2")
def * = k1 ~ k2
def pk = primaryKey("pk_a",(k1, k2))
}
Object A extends Table[(Int, Int)]("a") {
def k1 = column[Int]("k1")
def k2 = column[Int]("k2")
def * = k1 ~ k2
def idx = index("idx_a", (k1, k2), unique=true)
}
Data Definition Language(DDL)
DDL statements for a table can be created with its ddl method. We can create and drop table with this function.
val ddl = Coffees.ddl ++ Suppliers.ddl
db withSession {
ddl.create
…snip…
ddl.drop
}
We can see the SQL with these statements>
ddl.createStatements.foreach(println)
ddl.dropStatements.foreach(println)
Output:
create table "SUPPLIERS" ("SUP_ID" INTEGER NOT NULL PRIMARY KEY,"SUP_NAME" VARCHAR NOT NULL,"STREET" VARCHAR NOT NULL,"CITY" VARCHAR NOT NULL,"STATE" VARCHAR NOT NULL,"ZIP" VARCHAR NOT NULL)
create table "COFFEES" ("COF_NAME" VARCHAR NOT NULL PRIMARY KEY,"SUP_ID" INTEGER NOT NULL,"PRICE" DOUBLE NOT NULL,"SALES" INTEGER NOT NULL,"TOTAL" INTEGER NOT NULL)
alter table "COFFEES" add constraint "SUP_FK" foreign key("SUP_ID") references "SUPPLIERS"("SUP_ID") on update NO ACTION on delete NO ACTION
alter table "COFFEES" drop constraint "SUP_FK"
drop table "SUPPLIERS"
drop table "COFFEES"
Expressions
…snip…
2. Direct Embedding
we did not use that in my project.
3. Plain SQL Queries
First of all the imports are different.
import scala.slick.session.Database
import Database.threadLocalSession
import scala.slick.jdbc.{GetResult, StaticQuery => Q}
Import scala.slick.jdbc.GetResult and import scala.sclick.jdbc.StaticQuery for Q.
The most important class for Plain SQL queries is scala.slick.jdbc.StaticQuery which gets imported as Q for more convenient use.
// Case classes for our data
case class Supplier(id: Int, name: String, street: String, city: String, state: String, zip: String)
case class Coffee(name: String, supID: Int, price: Double, sales: Int, total: Int)
DDL/DML Statements
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
…snip…
}
Q.u is StaticQuery.updateNA("")
// 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
We can use the special concatenation operator +? to add a bind variable to a query string.
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(
Coffee("Colombian", 101, 7.99, 0, 0),
Coffee("French_Roast", 49, 8.99, 0, 0),
Coffee("Espresso", 150, 9.99, 0, 0),
Coffee("Colombian_Decaf", 101, 8.99, 0, 0),
Coffee("French_Roast_Decaf", 49, 9.99, 0, 0)).foreach(insert)
Query Statements
// Result set getters
implicit val getSupplierResult = GetResult(r => Supplier(r.nextInt, r.nextString, r.nextString,
r.nextString, r.nextString, r.nextString))
implicit val getCoffeeResult = GetResult(r => Coffee(r.<<, r.<<, r.<<, r.<<, r.<<))
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)
}
There are predefined GetResult implicates for the standard JDBC types.
The first one uses the explicit PositionedResult methods nextInt and nextString Int or String.
The second one uses the shortcut method << which returns a value of whatever type is expected at this place.
query which takes two type parameters, one for the query parameters and one for the results set rows. Similarly for update and updateNA.
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)
valsupplierById = Q[Int, Supplier] + "select * from suppliers where id = ?"
println("Supplier #49: " + supplierById(49).first)
String Interpolation
import Q.interpolation
def coffeeByName(name: String) = sql"select * from coffees where name = $name".as[Coffee]
println("Coffee Colombian: " + coffeeByName("Colombian").first.name)
println("Coffee Colombian: " + coffeeByName("Colombian").firstOption)
sqlu seems to sql and update the table.
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)
References:
http://slick.typesafe.com/doc/0.11.2/index.html
http://slick.typesafe.com/doc/0.11.2/lifted-embedding.html
http://slick.typesafe.com/doc/0.11.2/direct-embedding.html
http://slick.typesafe.com/doc/0.11.2/sql.html
发表评论
-
Update Site will come soon
2021-06-02 04:10 1609I am still keep notes my tech n ... -
Hadoop Docker 2019 Version 3.2.1
2019-12-10 07:39 255Hadoop Docker 2019 Version 3.2. ... -
Nginx and Proxy 2019(1)Nginx Enable Lua and Parse JSON
2019-12-03 04:17 374Nginx and Proxy 2019(1)Nginx En ... -
Data Solution 2019(13)Docker Zeppelin Notebook and Memory Configuration
2019-11-09 07:15 239Data Solution 2019(13)Docker Ze ... -
Data Solution 2019(10)Spark Cluster Solution with Zeppelin
2019-10-29 08:37 206Data Solution 2019(10)Spark Clu ... -
AMAZON Kinesis Firehose 2019(1)Firehose Buffer to S3
2019-10-01 10:15 286AMAZON Kinesis Firehose 2019(1) ... -
Rancher and k8s 2019(3)Clean Installation on CentOS7
2019-09-19 23:25 261Rancher and k8s 2019(3)Clean In ... -
Pacemaker 2019(1)Introduction and Installation on CentOS7
2019-09-11 05:48 298Pacemaker 2019(1)Introduction a ... -
Crontab-UI installation and Introduction
2019-08-30 05:54 408Crontab-UI installation and Int ... -
Spiderkeeper 2019(1)Installation and Introduction
2019-08-29 06:49 455Spiderkeeper 2019(1)Installatio ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 318Supervisor 2019(2)Ubuntu and Mu ... -
Supervisor 2019(1)CentOS 7
2019-08-19 09:33 285Supervisor 2019(1)CentOS 7 Ins ... -
Redis Cluster 2019(3)Redis Cluster on CentOS
2019-08-17 04:07 333Redis Cluster 2019(3)Redis Clus ... -
Amazon Lambda and Version Limit
2019-08-02 01:42 388Amazon Lambda and Version Limit ... -
MySQL HA Solution 2019(1)Master Slave on MySQL 5.7
2019-07-27 22:26 411MySQL HA Solution 2019(1)Master ... -
RabbitMQ Cluster 2019(2)Cluster HA and Proxy
2019-07-11 12:41 417RabbitMQ Cluster 2019(2)Cluster ... -
Running Zeppelin with Nginx Authentication
2019-05-25 21:35 288Running Zeppelin with Nginx Aut ... -
Running Zeppelin with Nginx Authentication
2019-05-25 21:34 294Running Zeppelin with Nginx Aut ... -
ElasticSearch(3)Version Upgrade and Cluster
2019-05-20 05:00 292ElasticSearch(3)Version Upgrade ... -
Jetty Server and Cookie Domain Name
2019-04-28 23:59 361Jetty Server and Cookie Domain ...
相关推荐
A simple guide, packed with tutorials that ease you into learning about the Slick game workflow and Slick game library. Who this book is written for If you are a game programmer who would like to ...
slick2d-maven.zip,slick2d库使用示例slick2d gava游戏开发库的七个发行版
essential-slick-3
解压方法step 1三个文件合成一个cat slickedit_linux_32bit.tar.gz.a* >slickedit.tar.gz step 2 tar xzvf slickedit.tar.gz slickedit 2014 v19 linux 32位版的 已经和谐了,step1:解压后执行vsinst安装,安装过程...
SlickEdit+Yagarto 搭建 Cortex M3 编译环境
解压方法step 1三个文件合成一个cat slickedit_linux_32bit.tar.gz.a* >slickedit.tar.gz step 2 tar xzvf slickedit.tar.gz slickedit 2014 v19 linux 32位版的 已经和谐了,step1:解压后执行vsinst安装,安装过程...
slickedit2010Windows版本安装文件和破解,验证可用
钓鱼女孩,使用 Slick 2d 的简单钓鱼游戏。 文件 Fishing-Girl-master.zip 包含以下条目。 .classpath/*w w w . j a va 2s . com*/ .gitignore .project .settings/org.eclipse.jdt.core.prefs OpenAL32.dll data/...
slickedit 17.0.2 linux vs_exe 覆蓋即可
block-slick:与Slick3兼容的Slick2阻塞API
slick.min.js
选择对应的版本 2.patch 文件公钥 3. gen license kg只编译了windows版本,其它平台的bin文件拷贝到windows底下patch。 含有公钥的bin文件, Windows平台是vs.exe。 其它平台类推,一般是vs。 已测试Win7 x64,linux...
Slickedit2014 Linux64 Part3 (带破解)
SlickEdit 是一个跨平台(在 9 种不同的工作平台上可以工作)的, 支持超过 40 种编程语言的代码编辑器,第二部分,破解版
开源的WEB表格控件 SlickGrid
slickedit 16.0.0.3 绿色 破解版,自己做的,非常方便,运行.\config\start_slickedit16.bat即可,生成的配置也在.\config下
Slickedit2012 Part2 (带破解,可破解全部版本)
1. 选择对应的版本 2.patch 文件公钥 3. gen license 其它平台的bin文件拷贝到windows底下patch。 含有公钥的bin文件, Windows平台是vs.exe。 其它平台类推,一般是vs。 已测试Win7 x64,其它如linux, Mac等...
1. 选择对应的版本 2.patch 文件公钥 3. gen license kg只编译了windows版本,其它平台的bin文件拷贝到windows底下patch。 含有公钥的bin文件, Windows平台是vs.exe。 其它平台类推,一般是vs。 已测试Win7 x64,...
slickedit 17.0.0.8安装包及其破解文件(linux 64位系统 part 2) slickedit 官网已更新到17.0.2, 目前没有破解文件。 网上的17.0.0.8安装包基本找不到, 我这里有备份,其中包含破解文件。 文件太大, 我分成两个...