-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcode-generation.html
279 lines (258 loc) · 19.5 KB
/
code-generation.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
<!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>Schema Code Generation — Slick 3.1.0-M1 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: '3.1.0-M1',
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 3.1.0-M1 documentation" href="index.html" />
<link rel="next" title="User-Defined Features" href="userdefined.html" />
<link rel="prev" title="Queries" href="queries.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/slick/src/sphinx/code-generation.rst">edit this page on github</a><span>
|
<a href="queries.html" title="Queries"
accesskey="P">previous</a> |
<a href="userdefined.html" title="User-Defined Features"
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>
3.1.0-M1 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="schema-code-generation">
<h1>Schema Code Generation<a class="headerlink" href="#schema-code-generation" title="Permalink to this headline">¶</a></h1>
<p>The Slick code generator is a convenient tool for working
with an existing or evolving database schema. It can be run
stand-alone or integrated into you sbt build for creating all
code Slick needs to work.</p>
<div class="section" id="overview">
<h2>Overview<a class="headerlink" href="#overview" title="Permalink to this headline">¶</a></h2>
<p>By default, the code generator generates <tt class="docutils literal"><span class="pre">Table</span></tt> classes, corresponding <tt class="docutils literal"><span class="pre">TableQuery</span></tt> values, which
can be used in a collection-like manner, as well as case classes for holding complete
rows of values. For tables with more than 22 columns the generator automatically switches
to Slick’s experimental <tt class="docutils literal"><span class="pre">HList</span></tt> implementation for overcoming Scala’s tuple size limit. (In Scala
<= 2.10.3 use <tt class="docutils literal"><span class="pre">HCons</span></tt> instead of <tt class="docutils literal"><span class="pre">::</span></tt> as a type contructor due to performance issues during compilation, which are fixed in 2.10.4 and later.)</p>
<p>Parts of the generator are also explained in our <a class="reference external" href="http://slick.typesafe.com/docs/#20131203_patterns_for_slick_database_applications_at_scala_exchange_2013">talk at Scala eXchange 2013</a>.</p>
</div>
<div class="section" id="standalone-use">
<h2>Standalone use<a class="headerlink" href="#standalone-use" title="Permalink to this headline">¶</a></h2>
<p>To include Slick’s code generator use the published library. For sbt projects add following to your build definition -
<tt class="docutils literal"><span class="pre">build.sbt</span></tt> or <tt class="docutils literal"><span class="pre">project/Build.scala</span></tt>:</p>
<pre class="literal-block">
libraryDependencies += "com.typesafe.slick" %% "slick-codegen" % "3.1.0-M1"
</pre>
<p>For Maven projects add the following to your <tt class="docutils literal"><span class="pre"><dependencies></span></tt>:</p>
<pre class="literal-block">
<dependency>
<groupId>com.typesafe.slick</groupId>
<artifactId>slick-codegen_2.10</artifactId>
<version>3.1.0-M1</version>
</dependency>
</pre>
<p>Slick’s code generator comes with a default runner that can be used from the command line or from Java/Scala. You can simply execute</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">slick</span><span class="o">.</span><span class="n">codegen</span><span class="o">.</span><span class="nc">SourceCodeGenerator</span><span class="o">.</span><span class="n">main</span><span class="o">(</span>
<span class="nc">Array</span><span class="o">(</span><span class="n">slickDriver</span><span class="o">,</span> <span class="n">jdbcDriver</span><span class="o">,</span> <span class="n">url</span><span class="o">,</span> <span class="n">outputFolder</span><span class="o">,</span> <span class="n">pkg</span><span class="o">)</span>
<span class="o">)</span>
</pre></div>
</div>
<p>or</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">slick</span><span class="o">.</span><span class="n">codegen</span><span class="o">.</span><span class="nc">SourceCodeGenerator</span><span class="o">.</span><span class="n">main</span><span class="o">(</span>
<span class="nc">Array</span><span class="o">(</span><span class="n">slickDriver</span><span class="o">,</span> <span class="n">jdbcDriver</span><span class="o">,</span> <span class="n">url</span><span class="o">,</span> <span class="n">outputFolder</span><span class="o">,</span> <span class="n">pkg</span><span class="o">,</span> <span class="n">user</span><span class="o">,</span> <span class="n">password</span><span class="o">)</span>
<span class="o">)</span>
</pre></div>
</div>
<p>and provide the following values</p>
<ul class="simple">
<li><strong>slickDriver</strong> Fully qualified name of Slick driver class, e.g. <em>“slick.driver.H2Driver”</em></li>
<li><strong>jdbcDriver</strong> Fully qualified name of jdbc driver class, e.g. <em>“org.h2.Driver”</em></li>
<li><strong>url</strong> jdbc url, e.g. <em>“jdbc:postgresql://localhost/test”</em></li>
<li><strong>outputFolder</strong> Place where the package folder structure should be put</li>
<li><strong>pkg</strong> Scala package the generated code should be places in</li>
<li><strong>user</strong> database connection user name</li>
<li><strong>password</strong> database connection password</li>
</ul>
</div>
<div class="section" id="integrated-into-sbt">
<h2>Integrated into sbt<a class="headerlink" href="#integrated-into-sbt" title="Permalink to this headline">¶</a></h2>
<p>The code generator can be run before every compilation or manually in <a class="reference external" href="http://www.scala-sbt.org/">sbt</a>.
An example project showing both can be <a class="reference external" href="https://github.com/slick/slick-codegen-example">found here</a>.</p>
</div>
<div class="section" id="generated-code">
<h2>Generated Code<a class="headerlink" href="#generated-code" title="Permalink to this headline">¶</a></h2>
<p>By default, the code generator places a file <tt class="docutils literal"><span class="pre">Tables.scala</span></tt> in the given folder in a subfolder corresponding
to the package. The file contains an <tt class="docutils literal"><span class="pre">object</span> <span class="pre">Tables</span></tt> from which the code
can be imported for use right away. Make sure you use the same Slick driver.
The file also contains a <tt class="docutils literal"><span class="pre">trait</span> <span class="pre">Tables</span></tt> which can be used in the cake pattern.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">When using the generated code, be careful <strong>not</strong> to mix different database drivers accidentally. The
default <tt class="docutils literal"><span class="pre">object</span> <span class="pre">Tables</span></tt> uses the driver used during code generation. Using it together with a different
driver for queries will lead to runtime errors. The generated <tt class="docutils literal"><span class="pre">trait</span> <span class="pre">Tables</span></tt> can be used with a
different driver, but be aware, that this is currently untested and not officially supported. It may or
may not work in your case. We will officially support this at some point in the future.</p>
</div>
</div>
<div class="section" id="customization">
<h2>Customization<a class="headerlink" href="#customization" title="Permalink to this headline">¶</a></h2>
<p>The generator can be flexibly customized by overriding methods to programmatically
generate any code based on the data model. This can be used for minor customizations
as well as heavy, model driven code generation, e.g. for framework bindings in <a class="reference external" href="https://playframework.com/">Play</a>,
other data-related, repetitive sections of applications, etc.</p>
<p><a class="reference external" href="https://github.com/slick/slick-codegen-customization-example">This example</a>
shows a customized code-generator and how to setup
up a multi-project sbt build, which compiles and runs it
before compiling the main sources.</p>
<p>The implementation of the code generator is structured into a small hierarchy of sub-generators responsible
for different fragments of the complete output. The implementation of each sub-generator can be swapped out
for a customized one by overriding the corresponding factory method.
<a class="reference external" href="codegen-api/index.html#slick.codegen.SourceCodeGenerator">SourceCodeGenerator</a> contains a factory method Table,
which it uses to generate a sub-generator for each table. The sub-generator Table in turn contains
sub-generators for Table classes, entity case classes, columns, key, indices, etc.
Custom sub-generators can easily be added as well.</p>
<p>Within the sub-generators the relevant part of the Slick data model can
be accessed to drive the code generation.</p>
<p>Please see the <a class="reference external" href="codegen-api/index.html#slick.codegen.SourceCodeGenerator">api documentation</a> for info
on all of the methods that can be overridden for customization.</p>
<p>Here is an example for customizing the generator:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">import</span> <span class="nn">slick.codegen.SourceCodeGenerator</span>
<span class="c1">// fetch data model</span>
<span class="k">val</span> <span class="n">modelAction</span> <span class="k">=</span> <span class="n">H2Driver</span><span class="o">.</span><span class="n">createModel</span><span class="o">(</span><span class="nc">Some</span><span class="o">(</span><span class="n">H2Driver</span><span class="o">.</span><span class="n">defaultTables</span><span class="o">))</span> <span class="c1">// you can filter specific tables here</span>
<span class="k">val</span> <span class="n">modelFuture</span> <span class="k">=</span> <span class="n">db</span><span class="o">.</span><span class="n">run</span><span class="o">(</span><span class="n">modelAction</span><span class="o">)</span>
<span class="c1">// customize code generator</span>
<span class="k">val</span> <span class="n">codegenFuture</span> <span class="k">=</span> <span class="n">modelFuture</span><span class="o">.</span><span class="n">map</span><span class="o">(</span><span class="n">model</span> <span class="k">=></span> <span class="k">new</span> <span class="nc">SourceCodeGenerator</span><span class="o">(</span><span class="n">model</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// override mapped table and class name</span>
<span class="k">override</span> <span class="k">def</span> <span class="n">entityName</span> <span class="k">=</span>
<span class="n">dbTableName</span> <span class="k">=></span> <span class="n">dbTableName</span><span class="o">.</span><span class="n">dropRight</span><span class="o">(</span><span class="mi">1</span><span class="o">).</span><span class="n">toLowerCase</span><span class="o">.</span><span class="n">toCamelCase</span>
<span class="k">override</span> <span class="k">def</span> <span class="n">tableName</span> <span class="k">=</span>
<span class="n">dbTableName</span> <span class="k">=></span> <span class="n">dbTableName</span><span class="o">.</span><span class="n">toLowerCase</span><span class="o">.</span><span class="n">toCamelCase</span>
<span class="c1">// add some custom import</span>
<span class="k">override</span> <span class="k">def</span> <span class="n">code</span> <span class="k">=</span> <span class="s">"import foo.{MyCustomType,MyCustomTypeMapper}"</span> <span class="o">+</span> <span class="s">"\n"</span> <span class="o">+</span> <span class="k">super</span><span class="o">.</span><span class="n">code</span>
<span class="c1">// override table generator</span>
<span class="k">override</span> <span class="k">def</span> <span class="nc">Table</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">Table</span><span class="o">(</span><span class="k">_</span><span class="o">){</span>
<span class="c1">// disable entity class generation and mapping</span>
<span class="k">override</span> <span class="k">def</span> <span class="nc">EntityType</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">EntityType</span><span class="o">{</span>
<span class="k">override</span> <span class="k">def</span> <span class="n">classEnabled</span> <span class="k">=</span> <span class="kc">false</span>
<span class="o">}</span>
<span class="c1">// override contained column generator</span>
<span class="k">override</span> <span class="k">def</span> <span class="nc">Column</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">Column</span><span class="o">(</span><span class="k">_</span><span class="o">){</span>
<span class="c1">// use the data model member of this column to change the Scala type,</span>
<span class="c1">// e.g. to a custom enum or anything else</span>
<span class="k">override</span> <span class="k">def</span> <span class="n">rawType</span> <span class="k">=</span>
<span class="k">if</span><span class="o">(</span><span class="n">model</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s">"SOME_SPECIAL_COLUMN_NAME"</span><span class="o">)</span> <span class="s">"MyCustomType"</span> <span class="k">else</span> <span class="k">super</span><span class="o">.</span><span class="n">rawType</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">})</span>
<span class="n">codegenFuture</span><span class="o">.</span><span class="n">onSuccess</span> <span class="o">{</span> <span class="k">case</span> <span class="n">codegen</span> <span class="k">=></span>
<span class="n">codegen</span><span class="o">.</span><span class="n">writeToFile</span><span class="o">(</span>
<span class="s">"slick.driver.H2Driver"</span><span class="o">,</span><span class="s">"some/folder/"</span><span class="o">,</span><span class="s">"some.packag"</span><span class="o">,</span><span class="s">"Tables"</span><span class="o">,</span><span class="s">"Tables.scala"</span>
<span class="o">)</span>
<span class="o">}</span>
</pre></div>
</div>
</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"><a class="reference internal" href="introduction.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="supported-databases.html">Supported Databases</a></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="database.html">Database Configuration</a></li>
<li class="toctree-l1"><a class="reference internal" href="dbio.html">Database I/O Actions</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 current"><a class="current reference internal" href="">Schema Code Generation</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#overview">Overview</a></li>
<li class="toctree-l2"><a class="reference internal" href="#standalone-use">Standalone use</a></li>
<li class="toctree-l2"><a class="reference internal" href="#integrated-into-sbt">Integrated into sbt</a></li>
<li class="toctree-l2"><a class="reference internal" href="#generated-code">Generated Code</a></li>
<li class="toctree-l2"><a class="reference internal" href="#customization">Customization</a></li>
</ul>
</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="orm-to-slick.html">Coming from ORM to Slick</a></li>
<li class="toctree-l1"><a class="reference internal" href="sql-to-slick.html">Coming from SQL to Slick</a></li>
<li class="toctree-l1"><a class="reference internal" href="upgrade.html">Upgrade Guides</a></li>
<li class="toctree-l1"><a class="reference internal" href="extensions.html">Slick Extensions</a></li>
</ul>
<ul>
<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="queries.html" title="Queries"
>previous</a> |
<a href="userdefined.html" title="User-Defined Features"
>next</a> |
<a href="genindex.html" title="General Index"
>index</a>
</div>
<div class="right">
<div class="footer">
© Copyright 2011-2015 Typesafe, Inc.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b3.
</div>
</div>
<div class="clearer"></div>
</div>
</div>
</body>
</html>