You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorial/index.html
+27-7Lines changed: 27 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -54,7 +54,8 @@ <h2>Database Connection</h2>
54
54
55
55
<p>
56
56
Every query that runs against the database needs a database session to run with. The <ahref="#code/src/main/scala/HelloSlick.scala" class="shortcut">HelloSlick.scala</a> file sets up a database connection and gets a session:
Note: the <code>session</code> can be implicit to avoid specifying it explicitly with every query.<br/>
59
60
60
61
The <code>session</code> can now be used in the scope of the provided function to make queries to the database. The session is automatically closed after the function completes.
@@ -69,11 +70,12 @@ <h2>Database Connection</h2>
69
70
<h2>Creating the Schema</h2>
70
71
71
72
<p>
72
-
Once a session is available you can use it to perform operations on the database. To create corresponding tables from a mapping you can get the DDL via it's<code>TableQuery</code> and then call the <code>create</code> method, like:
73
+
Once a session is available you can use it to perform operations on the database. To create corresponding tables from a mapping you can get the DDL via its<code>TableQuery</code> and then call the <code>create</code> method, like:
73
74
<pre><code>suppliers.ddl.create</code></pre>
74
75
75
76
Multiple DDLs can also be combined together and created, like in <ahref="#code/src/main/scala/HelloSlick.scala" class="shortcut">HelloSlick.scala</a>:
This will create all database entities and links (like foreign key references) in the correct order, even in the presence of cyclic dependencies between tables.
77
79
</p>
78
80
</div>
79
81
@@ -170,10 +172,10 @@ <h2>Computed Values</h2>
170
172
</div>
171
173
172
174
<div>
173
-
<h2>Manual SQL / String Interpolation</h2>
175
+
<h2>Plain SQL / String Interpolation</h2>
174
176
175
177
<p>
176
-
Sometimes writing manual sql is the easiest and best way to go but we don't want to lose SQL injection protection that Slick includes. SQL String Interpolation provides a nice API for doing this. Start by importing the interpolation API:
178
+
Sometimes writing manual SQL is the easiest and best way to go but we don't want to lose SQL injection protection that Slick includes. SQL String Interpolation provides a nice API for doing this. Start by importing the interpolation API:
This produces a query that can be run using the normal functions like <code>list</code>.
184
186
</p>
187
+
188
+
<p>You can learn more about Slick's <em>Plain SQL</em> queries in the <ahref="https://typesafe.com/activator/template/slick-plainsql" target="_blank">Slick Plain SQL Queries</a> template for Activator.</p>
The <code>Users</code> table mapping in <ahref="#code/src/main/scala/CaseClassMapping.scala" class="shortcut">CaseClassMapping.scala</a> defines an id column which uses an auto-incrementing primary key:
202
-
<pre><code>def id = column[Int]("ID", O.PrimaryKey, O.AutoInc)</code></pre>
203
-
206
+
<pre><code>def id = column[Int]("ID", O.PrimaryKey, O.AutoInc)</code></pre>
207
+
208
+
</p>
209
+
</div>
210
+
211
+
<div>
212
+
<h2>Running Queries</h2>
213
+
214
+
<p>So far you have seen the <code>Invoker</code> methods <code>.list</code> and <code>.foreach</code> being used to run a collection-valued query. There are several other useful methods which are shown in <ahref="#code/src/main/scala/InvokerMethods.scala" class="shortcut">InvokerMethods.scala</a>. They are equally applicable to <em>Lifted Embedding</em> and <em>Plain SQL</em> queries.</p>
215
+
216
+
<p>Note the use of <code>Compiled</code> in this app. It is used to define a pre-compiled query that can be executed with different parameters without having to recompile the SQL statement each time. This is the prefered way of defining queries in real-world applications. It prevents the (possibly expensive) compilation each time and leads to the same SQL statement (or a small, fixed set of SQL statements) so that the database system can also reuse a previously computed execution plan. As a side-effect, all parameters are automatically turned into bind variables:
Check out the full <ahref="http://slick.typesafe.com/doc/2.0.0/" target="_blank">Slick docs</a>.
228
+
<p>Check out the full <ahref="http://slick.typesafe.com/doc/2.0.1/" target="_blank">Slick manual and API docs</a>.</p>
229
+
230
+
<p>You can also find more Slick templates, contributed by both, the Slick team and the community, <ahref="/home" class="shortcut">here in Activator</a>.</p>
0 commit comments