- 浏览: 2566603 次
- 性别:
- 来自: 成都
-
文章分类
最新评论
-
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 1694I am still keep notes my tech n ... -
Hadoop Docker 2019 Version 3.2.1
2019-12-10 07:39 310Hadoop Docker 2019 Version 3.2. ... -
Nginx and Proxy 2019(1)Nginx Enable Lua and Parse JSON
2019-12-03 04:17 471Nginx and Proxy 2019(1)Nginx En ... -
Data Solution 2019(13)Docker Zeppelin Notebook and Memory Configuration
2019-11-09 07:15 316Data Solution 2019(13)Docker Ze ... -
Data Solution 2019(10)Spark Cluster Solution with Zeppelin
2019-10-29 08:37 265Data Solution 2019(10)Spark Clu ... -
AMAZON Kinesis Firehose 2019(1)Firehose Buffer to S3
2019-10-01 10:15 341AMAZON Kinesis Firehose 2019(1) ... -
Rancher and k8s 2019(3)Clean Installation on CentOS7
2019-09-19 23:25 336Rancher and k8s 2019(3)Clean In ... -
Pacemaker 2019(1)Introduction and Installation on CentOS7
2019-09-11 05:48 360Pacemaker 2019(1)Introduction a ... -
Crontab-UI installation and Introduction
2019-08-30 05:54 472Crontab-UI installation and Int ... -
Spiderkeeper 2019(1)Installation and Introduction
2019-08-29 06:49 534Spiderkeeper 2019(1)Installatio ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 387Supervisor 2019(2)Ubuntu and Mu ... -
Supervisor 2019(1)CentOS 7
2019-08-19 09:33 343Supervisor 2019(1)CentOS 7 Ins ... -
Redis Cluster 2019(3)Redis Cluster on CentOS
2019-08-17 04:07 384Redis Cluster 2019(3)Redis Clus ... -
Amazon Lambda and Version Limit
2019-08-02 01:42 450Amazon Lambda and Version Limit ... -
MySQL HA Solution 2019(1)Master Slave on MySQL 5.7
2019-07-27 22:26 550MySQL HA Solution 2019(1)Master ... -
RabbitMQ Cluster 2019(2)Cluster HA and Proxy
2019-07-11 12:41 474RabbitMQ Cluster 2019(2)Cluster ... -
Running Zeppelin with Nginx Authentication
2019-05-25 21:35 334Running Zeppelin with Nginx Aut ... -
Running Zeppelin with Nginx Authentication
2019-05-25 21:34 335Running Zeppelin with Nginx Aut ... -
ElasticSearch(3)Version Upgrade and Cluster
2019-05-20 05:00 340ElasticSearch(3)Version Upgrade ... -
Jetty Server and Cookie Domain Name
2019-04-28 23:59 420Jetty Server and Cookie Domain ...
相关推荐
《Slick2D游戏开发库详解与实例演示》 Slick2D,作为一个基于Java的2D游戏开发库,为开发者提供了丰富的功能和便捷的API,使得2D游戏的创建变得简单而高效。这个名为“slick2d-examples-1.0.1.zip”的压缩包,包含...
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 ...
根据提供的信息,我们可以总结出以下相关知识点,这些知识点主要聚焦于"Slick-3"这一数据库访问库的基础使用方法以及如何通过Scala语言进行高效的数据查询与操作。 ### Slick-3 简介 Slick-3 是一个用于 Scala 的...
钓鱼女孩,使用 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/...
<div><img src="image2.jpg" alt="Image 2"> <div><img src="image3.jpg" alt="Image 3"> ``` 接下来,为了使轮播图生效,需要在页面中引入SLICK插件的CSS和JavaScript文件。通常,这些文件会位于项目的`dist`...
《SlickEdit v27 Linux 64 专业版:高效编程的新篇章》 SlickEdit v27 是一款专为Linux 64位操作系统设计的专业级代码编辑器,它以其强大的功能和高效的编程体验而备受程序员的青睐。在这款软件中,开发者可以享受...
2. **编译并运行**:使用SlickEdit中的“Build”菜单来编译项目,编译成功后,将生成的目标文件下载到目标板上进行测试。 #### 五、总结 通过上述步骤,我们可以使用SlickEdit和Yagarto构建一个高效、稳定的Cortex...
《SlickEdit Gadgets 2 for VS2005:高效编程的实用工具》 在软件开发领域,Visual Studio 2005是一款备受推崇的集成开发环境(IDE),为程序员提供了丰富的功能和便利。然而,为了进一步提升开发效率,许多开发者...
解压方法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 13.0.0.0 绿色版与SlickEdit 12.0.3汉化包详解》 SlickEdit是一款备受程序员喜爱的文本编辑器,以其强大的功能和高效的代码编辑体验闻名。在这个压缩包中,我们得到了两个版本的SlickEdit:13.0.0.0的...
它使用高效的JavaScript和CSS3动画,保证了在不同设备上流畅运行,同时减少了对CPU的占用。对于大型项目或者高流量网站,这样的性能优化至关重要,可以提升用户体验,降低服务器压力。 再者,Slick提供了丰富的选项...
3. **动画效果**:Slick提供了多种过渡动画,如淡入淡出、左右滑动等,让切换过程更加流畅自然。 4. **导航箭头和分页**:可以自定义添加向前向后导航箭头和分页指示器,方便用户浏览幻灯片。 5. **无限循环**:...
解压方法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版本安装文件和破解,验证可用
3. **输入处理**:Slick2D允许开发者监听和响应用户的触摸或键盘输入。在Android上,这通常涉及`Input`接口,你可以通过它来检测点击、滑动等手势,实现游戏的交互逻辑。 4. **游戏逻辑与更新**:游戏的核心逻辑...
Block-Slick是一个专门为Scala开发的库,它提供了与Slick3兼容的Slick2阻塞API。Slick是一个强大的、类型安全的SQL库,用于在Scala应用程序中操作关系数据库。Slick3是其最新的版本,引入了许多改进和新特性,但同时...
slickedit 2012 17.0.2 windows 32位 和谐版,大家都懂的。 详细过程参见http:// zhiwei.li /text/2012/06/slickedit-2012/comment-page-1/
slick.min.js
2. 隐藏选中的某个区域 SlickEdit 也提供了隐藏选中的某个区域的功能,这可以帮助开发者快速隐藏不需要的代码。操作步骤为:先选取一块区域,然后选择菜单 View-> Hide Selection。 3. 隐藏注释内容 SlickEdit ...
3. **代码高亮**:Slick Edit支持多种语言的代码高亮,确保代码在视觉上清晰易懂。你可以根据自己的喜好调整高亮样式,例如增加或减少背景对比度。 4. **自动完成与提示**:开启自动完成功能,当输入代码时,Slick ...