-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathintroduction.html
319 lines (294 loc) · 23.1 KB
/
introduction.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Introduction — Slick 2.1.0-M2 documentation</title>
<link rel="stylesheet" href="_static/slick.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '2.1.0-M2',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="shortcut icon" href="_static/favicon.ico"/>
<link rel="top" title="Slick 2.1.0-M2 documentation" href="index.html" />
<link rel="next" title="Getting Started" href="gettingstarted.html" />
<link rel="prev" title="Slick - Scala Language Integrated Connection Kit" href="index.html" />
<script type="text/javascript">
if(window.location.host == 'slick.typesafe.com'){
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-23127719-3']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
}
</script>
</head>
<body>
<div class="header-wrapper">
<div class="header">
<div class="rel">
<span class="github"><a href="https://github.com/slick/slick/edit/master/src/sphinx/introduction.rst">edit this page on github</a><span>
|
<a href="index.html" title="Slick - Scala Language Integrated Connection Kit"
accesskey="P">previous</a> |
<a href="gettingstarted.html" title="Getting Started"
accesskey="N">next</a> |
<a href="genindex.html" title="General Index"
accesskey="I">index</a>
</div>
<div class="headertitle"><a href="index.html">
<span class="logo"><img class="logo" src="_static/slick-logo.png" alt="Slick"/></span>
2.1.0-M2 manual
</a></div>
</div>
</div>
<div class="content-wrapper">
<div class="content">
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="introduction">
<span id="index-0"></span><h1>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h1>
<div class="section" id="what-is-slick">
<h2>What is Slick?<a class="headerlink" href="#what-is-slick" title="Permalink to this headline">¶</a></h2>
<p>Slick is <a class="reference external" href="http://www.typesafe.com">Typesafe</a>‘s modern database query and access library for Scala. It allows you
to work with stored data almost as if you were using Scala collections while
at the same time giving you full control over when a database access happens
and which data is transferred. You can also use SQL directly.</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">val</span> <span class="n">limit</span> <span class="k">=</span> <span class="mf">10.0</span>
<span class="c1">// Your query could look like this:</span>
<span class="o">(</span> <span class="k">for</span><span class="o">(</span> <span class="n">c</span> <span class="k"><-</span> <span class="n">coffees</span><span class="o">;</span> <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">price</span> <span class="o"><</span> <span class="n">limit</span> <span class="o">)</span> <span class="k">yield</span> <span class="n">c</span><span class="o">.</span><span class="n">name</span> <span class="o">).</span><span class="n">list</span>
<span class="c1">// Or using more plain SQL String Interpolation:</span>
<span class="n">sql</span><span class="s">"select COF_NAME from COFFEES where PRICE < $limit"</span><span class="o">.</span><span class="n">as</span><span class="o">[</span><span class="kt">String</span><span class="o">].</span><span class="n">list</span>
<span class="c1">// Both queries result in SQL equivalent to:</span>
<span class="c1">// select COF_NAME from COFFEES where PRICE < 10.0</span>
</pre></div>
</div>
<p>When using Scala instead of raw SQL for your queries you benefit from compile-time safety
and compositionality. Slick can generate queries for different back-end databases including
your own, using its extensible query compiler.</p>
<p>Get started learning Slick in minutes using the <a class="reference external" href="https://typesafe.com/activator/template/hello-slick-2.1">Hello Slick template</a> in Typesafe <a class="reference external" href="https://typesafe.com/activator">Activator</a>.</p>
</div>
<div class="section" id="features">
<h2>Features<a class="headerlink" href="#features" title="Permalink to this headline">¶</a></h2>
<div class="section" id="scala">
<h3>Scala<a class="headerlink" href="#scala" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Queries, Table & Column Mappings, and types are plain Scala</li>
</ul>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Coffees</span><span class="o">(</span><span class="n">tag</span><span class="k">:</span> <span class="kt">Tag</span><span class="o">)</span> <span class="k">extends</span> <span class="nc">Table</span><span class="o">[(</span><span class="kt">String</span>, <span class="kt">Double</span><span class="o">)](</span><span class="n">tag</span><span class="o">,</span> <span class="s">"COFFEES"</span><span class="o">)</span> <span class="o">{</span>
<span class="k">def</span> <span class="n">name</span> <span class="k">=</span> <span class="n">column</span><span class="o">[</span><span class="kt">String</span><span class="o">](</span><span class="s">"COF_NAME"</span><span class="o">,</span> <span class="n">O</span><span class="o">.</span><span class="nc">PrimaryKey</span><span class="o">)</span>
<span class="k">def</span> <span class="n">price</span> <span class="k">=</span> <span class="n">column</span><span class="o">[</span><span class="kt">Double</span><span class="o">](</span><span class="s">"PRICE"</span><span class="o">)</span>
<span class="k">def</span> <span class="o">*</span> <span class="k">=</span> <span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">price</span><span class="o">)</span>
<span class="o">}</span>
<span class="k">val</span> <span class="n">coffees</span> <span class="k">=</span> <span class="nc">TableQuery</span><span class="o">[</span><span class="kt">Coffees</span><span class="o">]</span>
</pre></div>
</div>
<ul class="simple">
<li>Data access APIs similar to Scala collections</li>
</ul>
<div class="highlight-scala"><div class="highlight"><pre><span class="c1">// Query that only returns the "name" column</span>
<span class="n">coffees</span><span class="o">.</span><span class="n">map</span><span class="o">(</span><span class="k">_</span><span class="o">.</span><span class="n">name</span><span class="o">)</span>
<span class="c1">// Query that does a "where price < 10.0"</span>
<span class="n">coffees</span><span class="o">.</span><span class="n">filter</span><span class="o">(</span><span class="k">_</span><span class="o">.</span><span class="n">price</span> <span class="o"><</span> <span class="mf">10.0</span><span class="o">)</span>
</pre></div>
</div>
</div>
<div class="section" id="type-safe">
<h3>Type-Safe<a class="headerlink" href="#type-safe" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Let your IDE help you write your code</li>
<li>Find problems at compile-time instead of at runtime</li>
</ul>
<div class="highlight-scala"><div class="highlight"><pre><span class="c1">// The result of "select PRICE from COFFEES" is a Seq of Double</span>
<span class="c1">// because of the type safe column definitions</span>
<span class="k">val</span> <span class="n">coffeeNames</span><span class="k">:</span> <span class="kt">Seq</span><span class="o">[</span><span class="kt">Double</span><span class="o">]</span> <span class="k">=</span> <span class="n">coffees</span><span class="o">.</span><span class="n">map</span><span class="o">(</span><span class="k">_</span><span class="o">.</span><span class="n">price</span><span class="o">).</span><span class="n">list</span>
<span class="c1">// Query builders are type safe:</span>
<span class="n">coffees</span><span class="o">.</span><span class="n">filter</span><span class="o">(</span><span class="k">_</span><span class="o">.</span><span class="n">price</span> <span class="o"><</span> <span class="mf">10.0</span><span class="o">)</span>
<span class="c1">// Using a string in the filter would result in a compilation error</span>
</pre></div>
</div>
</div>
<div class="section" id="composable">
<h3>Composable<a class="headerlink" href="#composable" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Queries are functions that can be composed and reused</li>
</ul>
<div class="highlight-scala"><div class="highlight"><pre><span class="c1">// Create a query for coffee names with a price less than 10, sorted by name</span>
<span class="n">coffees</span><span class="o">.</span><span class="n">filter</span><span class="o">(</span><span class="k">_</span><span class="o">.</span><span class="n">price</span> <span class="o"><</span> <span class="mf">10.0</span><span class="o">).</span><span class="n">sortBy</span><span class="o">(</span><span class="k">_</span><span class="o">.</span><span class="n">name</span><span class="o">).</span><span class="n">map</span><span class="o">(</span><span class="k">_</span><span class="o">.</span><span class="n">name</span><span class="o">)</span>
<span class="c1">// The generated SQL is equivalent to:</span>
<span class="c1">// select name from COFFEES where PRICE < 10.0 order by NAME</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="compatibility">
<h2>Compatibility<a class="headerlink" href="#compatibility" title="Permalink to this headline">¶</a></h2>
<p>Slick requires Scala 2.10. (For Scala 2.9 please use <a class="reference external" href="http://scalaquery.org">ScalaQuery</a>, the predecessor of Slick).</p>
<span class="target" id="supported-dbs"></span><span class="target" id="index-1"></span></div>
<div class="section" id="supported-database-systems">
<span id="index-2"></span><h2>Supported database systems<a class="headerlink" href="#supported-database-systems" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li>DB2 (via <a class="reference internal" href="extensions.html"><em>slick-extensions</em></a>)</li>
<li>Derby/JavaDB</li>
<li>H2</li>
<li>HSQLDB/HyperSQL</li>
<li>Microsoft Access</li>
<li>Microsoft SQL Server (via <a class="reference internal" href="extensions.html"><em>slick-extensions</em></a>)</li>
<li>MySQL</li>
<li>Oracle (via <a class="reference internal" href="extensions.html"><em>slick-extensions</em></a>)</li>
<li>PostgreSQL</li>
<li>SQLite</li>
</ul>
<p>Other SQL databases can be accessed right away with a reduced feature set.
Writing a fully featured plugin for your own SQL-based backend can be achieved
with a reasonable amount of work. Support for other backends (like NoSQL) is
under development but not yet available.</p>
</div>
<div class="section" id="license">
<span id="index-3"></span><h2>License<a class="headerlink" href="#license" title="Permalink to this headline">¶</a></h2>
<p>Slick is released under a BSD-Style free and open source software <a class="reference external" href="https://github.com/slick/slick/blob/2.1.0-M2/LICENSE.txt">license</a>.
See the chapter on the commercial <a class="reference internal" href="extensions.html"><em>Slick Extensions</em></a> add-on
package for details on licensing the Slick drivers for the big commercial
database systems.</p>
</div>
<div class="section" id="compatibility-policy">
<span id="index-4"></span><h2>Compatibility Policy<a class="headerlink" href="#compatibility-policy" title="Permalink to this headline">¶</a></h2>
<p>Slick version numbers consist of an epoch, a major and minor version, and possibly a qualifier
(for milestone, RC and SNAPSHOT versions).</p>
<p>For release versions (i.e. versions without a qualifier), backward binary compatibility is
guaranteed between releases with the same epoch and major version (e.g. you could use 2.1.2 as a
drop-in relacement for 2.1.0 but not for 2.0.0). <a class="reference internal" href="extensions.html"><em>Slick Extensions</em></a> requires at
least the same minor version of Slick (e.g. Slick Extensions 2.1.2 can be used with Slick 2.1.2 but
not with Slick 2.1.1).</p>
<p>We do not guarantee source compatibility but we try to preserve it within the same major release.
Upgrading to a new major release may require some changes to your sources. We generally deprecate
old features and keep them around for a full major release cycle (i.e. features which become
deprecated in 2.1.0 will not be removed before 2.2.0) but this is not possible for all kinds of
changes.</p>
<p>Release candidates have the same compatibility guarantees as the final versions to which they
lead. There are <em>no compatibility guarantees</em> whatsoever for milestones and snapshots.</p>
</div>
<div class="section" id="query-apis">
<span id="index-5"></span><h2>Query APIs<a class="headerlink" href="#query-apis" title="Permalink to this headline">¶</a></h2>
<p>The <em>Lifted Embedding</em> is the standard API for type-safe queries and updates
in Slick. Please see <a class="reference internal" href="gettingstarted.html"><em>Getting Started</em></a> for an introduction. Most of this
user manual focuses on the <em>Lifted Embedding</em>.</p>
<p>For writing your own SQL statements you can use the <a class="reference internal" href="sql.html"><em>Plain SQL</em></a> API.</p>
<p>The experimental <a class="reference internal" href="direct-embedding.html"><em>Direct Embedding</em></a> is available as an
alternative to the <em>Lifted Embedding</em>.</p>
<span class="target" id="lifted-embedding"></span></div>
<div class="section" id="index-6">
<span id="id1"></span><h2>Lifted Embedding<a class="headerlink" href="#index-6" title="Permalink to this headline">¶</a></h2>
<p>The name <em>Lifted Embedding</em> refers to the fact that you are not working with
standard Scala types (as in the <a class="reference internal" href="direct-embedding.html"><em>direct embedding</em></a>)
but with types that are <em>lifted</em> into a <a class="reference external" href="http://slick.typesafe.com/doc/2.1.0-M2/api/#scala.slick.lifted.Rep">Rep</a> type
constructor. This becomes clear when you compare the types of a simple
Scala collections example</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">case</span> <span class="k">class</span> <span class="nc">Coffee</span><span class="o">(</span><span class="n">name</span><span class="k">:</span> <span class="kt">String</span><span class="o">,</span> <span class="n">price</span><span class="k">:</span> <span class="kt">Double</span><span class="o">)</span>
<span class="k">val</span> <span class="n">coffees</span><span class="k">:</span> <span class="kt">List</span><span class="o">[</span><span class="kt">Coffee</span><span class="o">]</span> <span class="k">=</span> <span class="c1">//...</span>
<span class="k">val</span> <span class="n">l</span> <span class="k">=</span> <span class="n">coffees</span><span class="o">.</span><span class="n">filter</span><span class="o">(</span><span class="k">_</span><span class="o">.</span><span class="n">price</span> <span class="o">></span> <span class="mf">8.0</span><span class="o">).</span><span class="n">map</span><span class="o">(</span><span class="k">_</span><span class="o">.</span><span class="n">name</span><span class="o">)</span>
<span class="c1">// ^ ^ ^</span>
<span class="c1">// Double Double String</span>
</pre></div>
</div>
<p>... with the types of similar code using the lifted embedding:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Coffees</span><span class="o">(</span><span class="n">tag</span><span class="k">:</span> <span class="kt">Tag</span><span class="o">)</span> <span class="k">extends</span> <span class="nc">Table</span><span class="o">[(</span><span class="kt">String</span>, <span class="kt">Double</span><span class="o">)](</span><span class="n">tag</span><span class="o">,</span> <span class="s">"COFFEES"</span><span class="o">)</span> <span class="o">{</span>
<span class="k">def</span> <span class="n">name</span> <span class="k">=</span> <span class="n">column</span><span class="o">[</span><span class="kt">String</span><span class="o">](</span><span class="s">"COF_NAME"</span><span class="o">)</span>
<span class="k">def</span> <span class="n">price</span> <span class="k">=</span> <span class="n">column</span><span class="o">[</span><span class="kt">Double</span><span class="o">](</span><span class="s">"PRICE"</span><span class="o">)</span>
<span class="k">def</span> <span class="o">*</span> <span class="k">=</span> <span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">price</span><span class="o">)</span>
<span class="o">}</span>
<span class="k">val</span> <span class="n">coffees</span> <span class="k">=</span> <span class="nc">TableQuery</span><span class="o">[</span><span class="kt">Coffees</span><span class="o">]</span>
<span class="k">val</span> <span class="n">q</span> <span class="k">=</span> <span class="n">coffees</span><span class="o">.</span><span class="n">filter</span><span class="o">(</span><span class="k">_</span><span class="o">.</span><span class="n">price</span> <span class="o">></span> <span class="mf">8.0</span><span class="o">).</span><span class="n">map</span><span class="o">(</span><span class="k">_</span><span class="o">.</span><span class="n">name</span><span class="o">)</span>
<span class="c1">// ^ ^ ^</span>
<span class="c1">// Rep[Double] Rep[Double] Rep[String]</span>
</pre></div>
</div>
<p>All plain types are lifted into <tt class="docutils literal"><span class="pre">Rep</span></tt>. The same is true for the table row
type <tt class="docutils literal"><span class="pre">Coffees</span></tt> which is a subtype of <tt class="docutils literal"><span class="pre">Rep[(String,</span> <span class="pre">Double)]</span></tt>.
Even the literal <tt class="docutils literal"><span class="pre">8.0</span></tt> is automatically lifted to a <tt class="docutils literal"><span class="pre">Rep[Double]</span></tt> by an
implicit conversion because that is what the <tt class="docutils literal"><span class="pre">></span></tt> operator on
<tt class="docutils literal"><span class="pre">Rep[Double]</span></tt> expects for the right-hand side. This lifting is necessary
because the lifted types allow us to generate a syntax tree that captures
the query computations. Getting plain Scala functions and values would not
give us enough information for translating those computations to SQL.</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sidebar">
<h3>Search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<h3 style="margin-top: 1.5em;">Table Of Contents</h3>
<ul class="current">
<li class="toctree-l1 current"><a class="current reference internal" href="">Introduction</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#what-is-slick">What is Slick?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#features">Features</a></li>
<li class="toctree-l2"><a class="reference internal" href="#compatibility">Compatibility</a></li>
<li class="toctree-l2"><a class="reference internal" href="#supported-database-systems">Supported database systems</a></li>
<li class="toctree-l2"><a class="reference internal" href="#license">License</a></li>
<li class="toctree-l2"><a class="reference internal" href="#compatibility-policy">Compatibility Policy</a></li>
<li class="toctree-l2"><a class="reference internal" href="#query-apis">Query APIs</a></li>
<li class="toctree-l2"><a class="reference internal" href="#index-6">Lifted Embedding</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="gettingstarted.html">Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="gettingstarted.html#adding-slick-to-your-project">Adding Slick to Your Project</a></li>
<li class="toctree-l1"><a class="reference internal" href="gettingstarted.html#quick-introduction">Quick Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="migration.html">Migration Guide from Slick 1.0 to 2.0</a></li>
<li class="toctree-l1"><a class="reference internal" href="from-sql-to-slick.html">Coming from SQL to Slick</a></li>
<li class="toctree-l1"><a class="reference internal" href="connection.html">Connections / Transactions</a></li>
<li class="toctree-l1"><a class="reference internal" href="code-generation.html">Schema code generation</a></li>
<li class="toctree-l1"><a class="reference internal" href="schemas.html">Schemas</a></li>
<li class="toctree-l1"><a class="reference internal" href="queries.html">Queries</a></li>
<li class="toctree-l1"><a class="reference internal" href="userdefined.html">User-Defined Features</a></li>
<li class="toctree-l1"><a class="reference internal" href="sql.html">Plain SQL Queries</a></li>
<li class="toctree-l1"><a class="reference internal" href="extensions.html">Slick Extensions</a></li>
<li class="toctree-l1"><a class="reference internal" href="direct-embedding.html">Direct Embedding (Experimental Feature)</a></li>
<li class="toctree-l1"><a class="reference internal" href="testkit.html">Slick TestKit</a></li>
</ul>
</div>
<div class="clearer"></div>
</div>
</div>
<div class="footer-wrapper">
<div class="footer">
<div class="left">
<a href="index.html" title="Slick - Scala Language Integrated Connection Kit"
>previous</a> |
<a href="gettingstarted.html" title="Getting Started"
>next</a> |
<a href="genindex.html" title="General Index"
>index</a>
</div>
<div class="right">
<div class="footer">
© Copyright 2011-2014 Typesafe, Inc.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b3.
</div>
</div>
<div class="clearer"></div>
</div>
</div>
</body>
</html>