1+ import scala .slick .driver .H2Driver .simple ._
2+
3+ // The main application
4+ object HelloSlick extends App {
5+
6+ // The query interface for the Suppliers table
7+ val suppliers : TableQuery [Suppliers ] = TableQuery [Suppliers ]
8+
9+ // the query interface for the Coffees table
10+ val coffees : TableQuery [Coffees ] = TableQuery [Coffees ]
11+
12+ // Create a connection (called a "session") to an in-memory H2 database
13+ Database .forURL(" jdbc:h2:mem:hello" , driver = " org.h2.Driver" ).withSession { implicit session =>
14+
15+ // Create the schema by combining the DDLs for the Suppliers and Coffees tables using the query interfaces
16+ (suppliers.ddl ++ coffees.ddl).create
17+
18+
19+ /* Create / Insert */
20+
21+ // Insert some suppliers
22+ suppliers += (101 , " Acme, Inc." , " 99 Market Street" , " Groundsville" , " CA" , " 95199" )
23+ suppliers += (49 , " Superior Coffee" , " 1 Party Place" , " Mendocino" , " CA" , " 95460" )
24+ suppliers += (150 , " The High Ground" , " 100 Coffee Lane" , " Meadows" , " CA" , " 93966" )
25+
26+ // Insert some coffees (using JDBC's batch insert feature)
27+ val coffeesInsertResult : Option [Int ] = coffees ++= Seq (
28+ (" Colombian" , 101 , 7.99 , 0 , 0 ),
29+ (" French_Roast" , 49 , 8.99 , 0 , 0 ),
30+ (" Espresso" , 150 , 9.99 , 0 , 0 ),
31+ (" Colombian_Decaf" , 101 , 8.99 , 0 , 0 ),
32+ (" French_Roast_Decaf" , 49 , 9.99 , 0 , 0 )
33+ )
34+
35+ val allSuppliers : List [(Int , String , String , String , String , String )] = suppliers.list
36+
37+ // Print the number of rows inserted
38+ coffeesInsertResult foreach (numRows => println(s " Inserted $numRows rows into the Coffees table " ))
39+
40+
41+ /* Read / Query / Select */
42+
43+ // Print the SQL for the Coffees query
44+ println(" Generated SQL for base Coffees query:\n " + coffees.selectStatement)
45+
46+ // Query the Coffees table using a foreach and print each row
47+ coffees foreach { case (name, supID, price, sales, total) =>
48+ println(" " + name + " \t " + supID + " \t " + price + " \t " + sales + " \t " + total)
49+ }
50+
51+
52+ /* Filtering / Where */
53+
54+ // Construct a query where the price of Coffees is > 9.0
55+ val filterQuery : Query [Coffees , (String , Int , Double , Int , Int )] = coffees.filter(_.price > 9.0 )
56+
57+ println(" Generated SQL for filter query:\n " + filterQuery.selectStatement)
58+
59+ // Execute the query
60+ println(filterQuery.list)
61+
62+
63+ /* Update */
64+
65+ // Construct an update query with the sales column being the one to update
66+ val updateQuery : Query [Column [Int ], Int ] = coffees.map(_.sales)
67+
68+ // Print the SQL for the Coffees update query
69+ println(" Generated SQL for Coffees update:\n " + updateQuery.updateStatement)
70+
71+ // Perform the update
72+ val numUpdatedRows = updateQuery.update(1 )
73+
74+ println(s " Updated $numUpdatedRows rows " )
75+
76+
77+ /* Delete */
78+
79+ // Construct a delete query that deletes coffees with a price less than 8.0
80+ val deleteQuery : Query [Coffees ,(String , Int , Double , Int , Int )] = coffees.filter(_.price < 8.0 )
81+
82+ // Print the SQL for the Coffees delete query
83+ println(" Generated SQL for Coffees delete:\n " + deleteQuery.deleteStatement)
84+
85+ // Perform the delete
86+ val numDeletedRows = deleteQuery.delete
87+
88+ println(s " Deleted $numDeletedRows rows " )
89+
90+
91+ /* Selecting Specific Columns */
92+
93+ // Construct a new coffees query that just selects the name
94+ val justNameQuery : Query [Column [String ], String ] = coffees.map(_.name)
95+
96+ println(" Generated SQL for query returning just the name:\n " + justNameQuery.selectStatement)
97+
98+ // Execute the query
99+ println(justNameQuery.list)
100+
101+
102+ /* Sorting / Order By */
103+
104+ val sortByPriceQuery : Query [Coffees , (String , Int , Double , Int , Int )] = coffees.sortBy(_.price)
105+
106+ println(" Generated SQL for query sorted by price:\n " + sortByPriceQuery.selectStatement)
107+
108+ // Execute the query
109+ println(sortByPriceQuery.list)
110+
111+
112+ /* Query Composition */
113+
114+ val composedQuery : Query [Column [String ], String ] = coffees.sortBy(_.name).take(3 ).filter(_.price > 9.0 ).map(_.name)
115+
116+ println(" Generated SQL for composed query:\n " + composedQuery.selectStatement)
117+
118+ // Execute the composed query
119+ println(composedQuery.list)
120+
121+
122+ /* Joins */
123+
124+ // Join the tables using the relationship defined in the Coffees table
125+ val joinQuery : Query [(Column [String ], Column [String ]), (String , String )] = for {
126+ c <- coffees if c.price > 9.0
127+ s <- c.supplier
128+ } yield (c.name, s.name)
129+
130+ println(" Generated SQL for the join query:\n " + joinQuery.selectStatement)
131+
132+ // Print the rows which contain the coffee name and the supplier name
133+ println(joinQuery.list)
134+
135+
136+ /* Computed Values */
137+
138+ // Create a new computed column that calculates the max price
139+ val maxPriceColumn : Column [Option [Double ]] = coffees.map(_.price).max
140+
141+ println(" Generated SQL for max price column:\n " + maxPriceColumn.selectStatement)
142+
143+ // Execute the computed value query
144+ println(maxPriceColumn.run)
145+
146+
147+ /* Manual SQL / String Interpolation */
148+
149+ // Required import for the sql interpolator
150+ import scala .slick .jdbc .StaticQuery .interpolation
151+
152+ // A value to insert into the statement
153+ val state = " CA"
154+
155+ // Construct a SQL statement manually with an interpolated value
156+ val plainQuery = sql " select SUP_NAME from SUPPLIERS where STATE = $state" .as[String ]
157+
158+ println(" Generated SQL for plain query:\n " + plainQuery.getStatement)
159+
160+ // Execute the query
161+ println(plainQuery.list)
162+
163+ }
164+ }
0 commit comments