senax 0.3.21

Senax Web Server Code Generator
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<!DOCTYPE html>
<html lang="ja">

<head>
    <meta charset="utf-8">
    <title>{{config | get(key="title", default="") }} テーブル定義書</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/7.0.0/normalize.min.css">
    <style>
        /* https://qiita.com/cognitom/items/d39d5f19054c8c8fd592 */
        /* https://github.com/cognitom/paper-css */
        @page {
            margin: 10mm 0;
            size: A4
        }

        body {
            margin: 0;
            font-size: 3.5mm;
        }

        .sheet {
            margin: 0;
            padding: 0 10mm;
            overflow: hidden;
            position: relative;
            box-sizing: border-box;
            page-break-after: always;
        }

        body.A4 .sheet {
            width: 210mm;
            min-height: 276mm
        }

        body.A4.landscape .sheet {
            width: 297mm;
            min-height: 189mm
        }

        body.letter .sheet {
            width: 216mm;
            min-height: 259mm
        }

        body.letter.landscape .sheet {
            width: 280mm;
            min-height: 195mm
        }

        /** For screen preview **/
        @media screen {
            body {
                background: #e0e0e0
            }

            .sheet {
                background: white;
                box-shadow: 0 .5mm 2mm rgba(0, 0, 0, .3);
                margin: 5mm auto;
                padding-bottom: 5mm;
            }
        }

        .break-avoid {
            break-inside: avoid;
        }

        .frame table {
            margin-left: auto;
            margin-right: auto;
            border-collapse: collapse;
            width: 100%;
        }

        .frame th,
        td {
            border: 1px #000000 solid;
            padding: 0.2em;
        }

        th {
            text-align: center
        }

        .frame caption {
            caption-side: top;
        }

        a {
            color: #1a0dab;
        }

        @media print {
            a {
                color: #000000;
                text-decoration: none;
            }
        }

        pre,
        p {
            margin: 0.2mm;
            font-family: unset;
        }

        .title {
            text-align: center
        }

        .no_frame {
            margin-bottom: 4mm;
        }

        .narrow {
            width: 8mm
        }

        .middle {
            width: 24mm
        }

        .center {
            text-align: center
        }

        .comment {
            margin: 0 0 2mm;
        }

        .supplementary {
            font-size: 80%;
            text-align: right;
        }

        .er {
            break-inside: avoid;
            margin-top: 5mm;
        }

        .er svg {
            width: 100%;
        }
    </style>
</head>

<body class="A4">
    {% if group_list | length > 1 -%}
    <section class="sheet cover1">
        <div class="title">
            <h1>{{config | get(key="title", default="") }} テーブル定義書</h1>
        </div>
        <div class="frame info1">
            <table>
                <tr>
                    {% if config.author -%}
                    <th class="author">作成者</th>
                    <td class="author">{{config.author}}</td>
                    {% endif -%}
                    <th class="created">作成日</th>
                    <td class="created">{{date | date(format="%x", locale=locale)}}</td>
                </tr>
            </table>
        </div>
        <div class="frame model-group-list">
            <table>
                <caption>
                    <h3>モデルグループ一覧</h3>
                </caption>
                <thead>
                    <tr>
                        <th class="no narrow">No.</th>
                        <th class="group-name">モデルグループ</th>
                    </tr>
                </thead>
                {% for group in group_list -%}
                <tr>
                    <td class="no center">{{loop.index}}</td>
                    <td class="group-name"><a href="#sub_{{group.group_name}}">{{group.group_def.label | default(value=group.group_name) }}</a></td>
                </tr>
                {% endfor -%}
            </table>
        </div>
        {% if history | length > 0 -%}
        <div class="frame history">
            <table>
                <caption>
                    <h3>更新履歴</h3>
                </caption>
                <thead>
                    <tr>
                        <th class="updated">更新日</th>
                        <th class="description">変更内容</th>
                    </tr>
                </thead>
                {% set_global history_idx = 0 %}
                {% for row in history -%}
                {% set description = row.description | history_description(
                    AddTable='テーブル追加 <a href="#table_{T}">{T}</a><br>',
                    DropTable='テーブル削除 {T}<br>',
                    RenameTable='テーブル名変更 <a href="#table_{T}">{T}</a> ({C})<br>',
                    AddColumn='カラム追加 <a href="#table_{T}">{T}</a> ({C})<br>',
                    ChangeColumn='カラム変更 <a href="#table_{T}">{T}</a> ({C})<br>',
                    DropColumn='カラム削除 <a href="#table_{T}">{T}</a> ({C})<br>',
                    RenameColumn='カラム名変更 <a href="#table_{T}">{T}</a> ({C})<br>',
                    ChangePrimary='主キー変更 <a href="#table_{T}">{T}</a><br>',
                    DropPrimary='主キー削除 <a href="#table_{T}">{T}</a><br>',
                    AddIndex='インデックス追加 <a href="#table_{T}">{T}</a> ({C})<br>',
                    ChangeIndex='インデックス変更 <a href="#table_{T}">{T}</a> ({C})<br>',
                    DropIndex='インデックス削除 <a href="#table_{T}">{T}</a> ({C})<br>',
                    AddForeign='外部キー追加 <a href="#table_{T}">{T}</a> ({C})<br>',
                    ChangeForeign='外部キー変更 <a href="#table_{T}">{T}</a> ({C})<br>',
                    DropForeign='外部キー削除 <a href="#table_{T}">{T}</a> ({C})<br>',
                ) -%}
                {% if description | length == 0 %}{% continue %}{% endif -%}
                <tr>
                    <td class="updated">{{row.date | date(format="%x", locale=locale)}}</td>
                    <td class="description">
                        {{- description | safe -}}
                    </td>
                </tr>
                {% set_global history_idx = history_idx + 1 %}
                {% if history_idx == history_max %}{% break %}{% endif -%}
                {% endfor -%}
            </table>
        </div>
        {% endif -%}
    </section>
    {%- endif -%}

    {% for group in group_list -%}
    <section class="sheet cover2">
        <a id="sub_{{group.group_name}}"></a>
        {% if group_list | length == 1 -%}
        <div class="title">
            <h1>{{config | get(key="title", default="") }} テーブル定義書</h1>
        </div>
        <div class="frame info2">
            <table>
                <tr>
                    {% if config.groups | length > 1 -%}
                    <th class="group-name">モデルグループ</th>
                    <td class="group-name">{{group.group_def.label | default(value=group.group_name) }}</td>
                    {% endif -%}
                    {% if config.author -%}
                    <th class="author">作成者</th>
                    <td class="author">{{config.author}}</td>
                    {% endif -%}
                    <th class="created">作成日</th>
                    <td class="created">{{date | date(format="%x", locale=locale)}}</td>
                </tr>
            </table>
        </div>
        {% else -%}
        <div class="title group-name">
            <h2>{{group.group_def.label | default(value=group.group_name) }}</h2>
        </div>
        {% endif -%}
        {% if group.models -%}
        <div class="frame break-avoid model-list">
            <table>
                <caption>
                    <h3>テーブル一覧</h3>
                </caption>
                <thead>
                    <tr>
                        <th class="no narrow">No.</th>
                        <th class="model-name">テーブル名</th>
                    </tr>
                </thead>
                {% for model_name, model in group.models -%}
                <tr>
                    <td class="no center">{{loop.index}}</td>
                    <td class="model-name"><a href="#model_{{group.group_name}}::{{model_name}}">{{model_name}}
                            {%- if model.label %} ({{model.label}}){% endif %}</a></td>
                </tr>
                {% endfor -%}
            </table>
            {% if group.er -%}
            <div class="er">
                <h3 class="center">ER図</h3>
                {{group.er | safe}}
            </div>
            {% endif -%}
        </div>
        {% endif -%}
    </section>
    {% if group.models -%}
    {% for model_name, model in group.models -%}
    <section class="sheet table-info">
        <a id="model_{{group.group_name}}::{{model_name}}"></a>
        <a id="table_{{model.table_name}}"></a>
        <div class="title">
            <h3>{{loop.index}}. {{model_name}}{% if model.label %} ({{model.label}}){% endif %}</h3>
        </div>
        <p class="no_frame table-name">
            <span>物理テーブル名: </span>
            <span>{{model.table_name}}</span>
        </p>
        {% if model.comment -%}
        <div class="comment">
            <pre>{{model.comment}}</pre>
        </div>
        {% endif -%}
        <div class="frame table-columns">
            <table>
                <thead>
                    <tr>
                        <th class="no narrow">No.</th>
                        <th class="name">論理名</th>
                        <th class="name">物理名</th>
                        <th class="type">データ型</th>
                        <th class="null narrow">Null</th>
                        <th class="length">長さ</th>
                        <th class="primary narrow"></th>
                        <th class="comment">備考</th>
                    </tr>
                </thead>
                {% for name, column in model.columns -%}
                <tr>
                    <td class="no center">{{loop.index}}</td>
                    <td class="name">{{column | get(key="label", default="")}}</td>
                    <td class="name">{{column | get(key="column_name", default=name)}}</td>
                    <td class="type">{% if column.signed %}signed {% endif %}{{column.type}}</td>
                    <td class="null center">{% if not column.not_null %}Y{% endif %}</td>
                    <td class="length center">
                        {%- if column.length %}{{column.length}}{% endif %}
                        {%- if column.precision %}{{column.precision}}{% endif %}
                        {%- if column.scale %},{{column.scale}}{% endif %}</td>
                    <td class="primary center">{% if column.primary %}Y{% endif %}</td>
                    <td class="comment">
                        {%- if column.enum_values -%}
                        <p>{% for enum in column.enum_values -%}
                            {%- if enum.value is defined %}
                            {{enum.value}}:{{enum | get(key="label", default=enum.name) }}
                            {%- else %}
                            {{enum.name}}{% if enum.label %}:{{enum.label}}{% endif %}
                            {%- endif %}
                            {%- if enum.comment %} ({{enum.comment}}){% endif %}
                            {%- if not loop.last %}<br />{% endif %}
                            {%- endfor %}</p>
                        {%- endif -%}
                        {%- if column.comment %}
                        <pre>{{column.comment}}</pre>{% endif -%}
                    </td>
                </tr>
                {% endfor -%}
            </table>
            <p class="supplementary">データ型に signed の指定がない場合、 double, decimal 等も含めすべての数値型は unsigned。</p>
        </div>
        {% if model.indexes -%}
        <div class="frame break-avoid table-indexes">
            <table>
                <caption>
                    <h3>インデックス</h3>
                </caption>
                <thead>
                    <tr>
                        <th class="no narrow">No.</th>
                        <th class="type middle">タイプ</th>
                        <th class="fields">カラム</th>
                    </tr>
                </thead>
                {% for name, index in model.indexes -%}
                <tr>
                    <td class="no center">{{loop.index}}</td>
                    <td class="type">
                        {%- if index %}{{index | get(key="type", default="INDEX") | upper}}
                        {%- else %}INDEX{% endif %}
                    </td>
                    <td class="fields">{%- if index and index.fields %}{% for name, field in index.fields -%}
                        {%- if field and field.query %}({{field.query}}){% else %}{{name}}{%- if field and field.length %}({{field.length}}){% endif %}{% endif %}
                        {%- if not loop.last %}, {% endif %}
                        {%- endfor %}{% else %}{{name}}{% endif %}</td>
                </tr>
                {% endfor -%}
            </table>
        </div>
        {% endif -%}
        {% if model.relations -%}
        <div class="frame break-avoid table-relations">
            <table>
                <caption>
                    <h3>リレーション</h3>
                </caption>
                <thead>
                    <tr>
                        <th class="no narrow">No.</th>
                        <th class="name">名前</th>
                        <th class="local">カラム</th>
                        <th class="type">タイプ</th>
                        <th class="model">結合先</th>
                        <th class="foreign">結合先カラム</th>
                        <th class="on_delete">削除時</th>
                    </tr>
                </thead>
                {% for name, relation in model.relations -%}
                <tr>{% set type = relation | get(key="type") %}
                    <td class="no center">{{loop.index}}</td>
                    <td class="name">{{relation | get(key="label", default=name)}}</td>
                    <td class="local">{%- if relation.local %}{{relation.local | join(sep=", ")}}{% else %}
                        {%- if type == "has_many" or type == "has_one" %}<div style="text-align: center">-</div>{% else %}{{name}}_id{% endif %}
                        {%- endif %}</td>
                    <td class="type">{{type | upper}}</td>
                    <td class="model"><a href="#model_{{relation.model}}">
                        {{- relation.model | split(pat="::") | last}}</a></td>
                    <td class="foreign">{%- if relation.foreign %}{{relation.foreign | join(sep=", ")}}{% else %}
                        {%- if type == "has_many" %}{{model_name}}_id{% else %}<div style="text-align: center">-</div>{% endif %}
                        {%- endif %}</td>
                    <td class="on_delete">{{relation | get(key="on_delete", default="")}}</td>
                </tr>
                {% endfor -%}
            </table>
            <p class="supplementary">カラムの - は主キー。{%- if config.ignore_foreign_key %}実際にはデータベースに外部キー制約は設定しない。{% endif -%}
            </p>
        </div>
        {% endif -%}
    </section>
    {% endfor -%}
    {% endif -%}
    {% endfor -%}
</body>