-
Notifications
You must be signed in to change notification settings - Fork 49
/
index.html
1308 lines (1005 loc) · 70.3 KB
/
index.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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Android-Orma by gfx</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="stylesheets/normalize.css" media="screen">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="stylesheets/stylesheet.css" media="screen">
<link rel="stylesheet" type="text/css" href="stylesheets/github-light.css" media="screen">
</head>
<body>
<section class="page-header">
<h1 class="project-name">Android-Orma</h1>
<h2 class="project-tagline">A type-safe ORM for Android as a wrapper of SQLiteDatabase</h2>
<a href="https://github.com/gfx/Android-Orma" class="btn">View on GitHub</a>
<a href="https://github.com/gfx/Android-Orma/zipball/master" class="btn">Download .zip</a>
<a href="https://github.com/gfx/Android-Orma/tarball/master" class="btn">Download .tar.gz</a>
</section>
<section class="main-content">
<h1>
<a id="android-orma----" class="anchor" href="#android-orma----" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Android Orma <a href="https://circleci.com/gh/gfx/Android-Orma/tree/master"><img src="https://circleci.com/gh/gfx/Android-Orma/tree/master.svg?style=svg" alt="Circle CI"></a> <a href="https://bintray.com/gfx/maven/orma/"> <img src="https://api.bintray.com/packages/gfx/maven/orma/images/download.svg" alt="Download"> </a>
</h1>
<p align="center">
<img src="Orma.png" width="256" height="256" alt="Android Orma">
</p>
<p>Orma is an ORM (Object-Relation Mapper) for <a href="http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html">Android SQLiteDatabase</a>,
generating helper classes at compile time with <strong>annotation processing</strong>, inspired in ActiveAndroid, GreenDAO, and Realm.</p>
<p>The interface of Orma is very simple and easy to use,
as the author respects the Larry Wall's wisdom:</p>
<blockquote>
<p>Easy things should be easy, and hard things should be possible
-- <a href="http://www.amazon.com/gp/feature.html?ie=UTF8&docId=7137">Larry Wall</a></p>
</blockquote>
<h2>
<a id="table-of-contents" class="anchor" href="#table-of-contents" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Table of Contents</h2>
<ul>
<li><a href="#table-of-contents">Table of Contents</a></li>
<li><a href="#motivation">Motivation</a></li>
<li><a href="#requirements">Requirements</a></li>
<li><a href="#getting-started">Getting Started</a></li>
<li><a href="#synopsis">Synopsis</a></li>
<li>
<a href="#the-components">The Components</a>
<ul>
<li><a href="#database-handles">Database Handles</a></li>
<li><a href="#models">Models</a></li>
<li><a href="#schema-helpers">Schema Helpers</a></li>
<li><a href="#relation-helpers">Relation Helpers</a></li>
<li><a href="#selector-helpers">Selector Helpers</a></li>
<li><a href="#updater-helpers">Updater Helpers</a></li>
<li><a href="#deleter-helpers">Deleter Helpers</a></li>
<li>
<a href="#query-helper-methods">Query Helper Methods</a>
<ul>
<li><a href="#list-of-query-helper-methods">List of Query Helper Methods</a></li>
<li><a href="#how-to-control-generation-of-query-helpers">How to Control Generation of Query Helpers</a></li>
</ul>
</li>
<li><a href="#the-inserter-helpers">The Inserter Helpers</a></li>
</ul>
</li>
<li>
<a href="#details-of-database-handles">Details of Database Handles</a>
<ul>
<li><a href="#configuration-of-database-handles">Configuration of Database Handles</a></li>
<li><a href="#database-handle-builders">Database Handle Builders</a></li>
</ul>
</li>
<li>
<a href="#details-of-models">Details of Models</a>
<ul>
<li><a href="#setters-and-getters">Setters and Getters</a></li>
<li><a href="#immutable-models">Immutable Models</a></li>
</ul>
</li>
<li>
<a href="#associations">Associations</a>
<ul>
<li><a href="#has-one-associations-with-singleassociationt">Has-One Associations with <code>SingleAssociation<T></code></a></li>
<li><a href="#has-many-associations-with-singleassociationt">Has-Many Associations with <code>SingleAssociation<T></code></a></li>
<li><a href="#direct-associations">Direct Associations</a></li>
<li><a href="#limitations-in-associations">Limitations in Associations</a></li>
</ul>
</li>
<li>
<a href="#type-adapters">Type Adapters</a>
<ul>
<li><a href="#how-serialized-types-used">How Serialized Types Used</a></li>
<li><a href="#statictypeadapters-for-multiple-serializers-at-once"><code>@StaticTypeAdapters</code> for Multiple Serializers at Once</a></li>
<li><a href="#built-in-type-adapters">Built-In Type Adapters</a></li>
</ul>
</li>
<li><a href="#raw-queries">Raw Queries</a></li>
<li><a href="#migration">Migration</a></li>
<li><a href="#dataset-changed-events">DataSet Changed Events</a></li>
<li><a href="#cooperation-with-serialization-libraries">Cooperation with Serialization Libraries</a></li>
<li><a href="#example">Example</a></li>
<li><a href="#benchmark">Benchmark</a></li>
<li><a href="#method-count">Method Count</a></li>
<li>
<a href="#faq">FAQ</a>
<ul>
<li><a href="#cant-build-my-project">Can't build my project.</a></li>
<li><a href="#how-can-i-enable-debug-logging-on-release-build">How can I enable debug logging on release build?</a></li>
<li><a href="#how-can-see-the-generated-java-files">How can see the generated Java files?</a></li>
<li><a href="#does-orma-work-with-kotlin">Does Orma work with Kotlin?</a></li>
<li><a href="#does-orma-work-with-the-jack-compiler">Does Orma work with the Jack compiler?</a></li>
<li><a href="#when-the-database-handle-is-opened-and-closed">When the database handle is opened and closed?</a></li>
<li><a href="#who-uses-orma">Who uses Orma?</a></li>
</ul>
</li>
<li><a href="#support">Support</a></li>
<li><a href="#licenses-in-runtime-dependencies">Licenses in Runtime Dependencies</a></li>
<li><a href="#contribution">Contribution</a></li>
<li><a href="#release-engineering-for-maintainers">Release Engineering for Maintainers</a></li>
<li><a href="#see-also">See Also</a></li>
<li><a href="#authors-and-contributors">Authors and Contributors</a></li>
<li><a href="#license">License</a></li>
</ul>
<h2>
<a id="motivation" class="anchor" href="#motivation" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Motivation</h2>
<p>There are already <a href="https://android-arsenal.com/tag/69">a lot of ORMs</a>. Why I have to add another wheel?</p>
<p>The answer is that I need ORM that have <em>all</em> the following features:</p>
<ul>
<li>Fast as hand-written code</li>
<li>POJO models
<ul>
<li>Model classes should have no restriction</li>
<li>Might implement <code>Parcelable</code> and/or extend any classes</li>
<li>They should be passed to another thread</li>
</ul>
</li>
<li>A database handle must be an object instance
<ul>
<li>Not a singleton class</li>
<li>Not a static-method based class</li>
</ul>
</li>
<li>Easy migration
<ul>
<li>Some <code>ALTER TABLE</code>, e.g. <code>add column</code> and <code>drop column</code>, should be detectd and processed</li>
<li>There is a wheel in Perl: <a href="https://metacpan.org/pod/SQL::Translator::Diff">SQL::Translator::Diff</a>
</li>
</ul>
</li>
<li>Code completion friendly
<ul>
<li>
<code>db.selectFromModel()</code> is better than <code>new Select(Model.class)</code>
</li>
</ul>
</li>
<li>Custom raw queries are sometimes inevitable
<ul>
<li><code>GROUP BY ... HAVING ...</code></li>
<li><code>SELECT max(value), min(value), avg(value), count(value) FROM ...</code></li>
</ul>
</li>
</ul>
<p>And now they are what Orma has.</p>
<h2>
<a id="requirements" class="anchor" href="#requirements" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Requirements</h2>
<ul>
<li>JDK 8 (1.8.0_66 or later) to build</li>
<li>Android API level 15 to use</li>
</ul>
<h2>
<a id="getting-started" class="anchor" href="#getting-started" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Getting Started</h2>
<p>Declare dependencies to use Orma and its annotation processor.</p>
<pre lang="gradle:build.gradle"><code>dependencies {
annotationProcessor 'com.github.gfx.android.orma:orma-processor:4.0.0'
compile 'com.github.gfx.android.orma:orma:4.0.0'
}
</code></pre>
<p>NOTE: if you use Android Gradle Plugin before 2.2.0, you must use <a href="https://bitbucket.org/hvisser/android-apt">android-apt</a> plugin instead of <code>annotationProcessor</code> configuration.</p>
<h2>
<a id="synopsis" class="anchor" href="#synopsis" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Synopsis</h2>
<p>First, define model classes annotated with <code>@Table</code>, <code>@Column</code>, and <code>@PrimaryKey</code> and run the <strong>Build APK</strong> command to generate helper classes.</p>
<div class="highlight highlight-source-java"><pre><span class="pl-k">package</span> <span class="pl-smi">com.github.gfx.android.orma.example</span>;
<span class="pl-k">import</span> <span class="pl-smi">com.github.gfx.android.orma.annotation.Column</span>;
<span class="pl-k">import</span> <span class="pl-smi">com.github.gfx.android.orma.annotation.PrimaryKey</span>;
<span class="pl-k">import</span> <span class="pl-smi">com.github.gfx.android.orma.annotation.Table</span>;
<span class="pl-k">import</span> <span class="pl-smi">android.support.annotation.Nullable</span>;
<span class="pl-k">@Table</span>
<span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-en">Todo</span> {
<span class="pl-k">@PrimaryKey</span>
<span class="pl-k">public</span> <span class="pl-k">long</span> id;
<span class="pl-k">@Column</span>(<span class="pl-c1">indexed</span> <span class="pl-k">=</span> <span class="pl-c1">true</span>)
<span class="pl-k">public</span> <span class="pl-smi">String</span> title;
<span class="pl-k">@Column</span>
<span class="pl-k">@Nullable</span> <span class="pl-c">// allows NULL (default: NOT NULL)</span>
<span class="pl-k">public</span> <span class="pl-smi">String</span> content;
<span class="pl-k">@Column</span>
<span class="pl-k">public</span> <span class="pl-k">long</span> createdTimeMillis;
}</pre></div>
<p>Second, instantiate a database handle <code>OrmaDatabase</code>, which is generated by <code>orma-processor</code>.</p>
<p>Here is an example to configure <code>OrmaDatabase</code>:</p>
<div class="highlight highlight-source-java"><pre><span class="pl-c">// See OrmaDatabaseBuilderBase for other options.</span>
<span class="pl-smi">OrmaDatabase</span> orma <span class="pl-k">=</span> <span class="pl-smi">OrmaDatabase</span><span class="pl-k">.</span>builder(context)
.build();</pre></div>
<p>Then, you can create, read, update and delete models via <code>OrmaDatabase</code>:</p>
<div class="highlight highlight-source-java"><pre><span class="pl-smi">Todo</span> todo <span class="pl-k">=</span> <span class="pl-c1">...</span>;
<span class="pl-c">// create</span>
orma<span class="pl-k">.</span>insertIntoTodo(todo);
<span class="pl-c">// prepared statements with transaction</span>
orma<span class="pl-k">.</span>transactionSync( <span class="pl-k">-</span><span class="pl-k">></span> { <span class="pl-c">// or transactionAsync() to execute tasks in background</span>
<span class="pl-k">Inserter<<span class="pl-smi">Todo</span>></span> inserter <span class="pl-k">=</span> orma<span class="pl-k">.</span>prepareInsertIntoTodo();
inserter<span class="pl-k">.</span>execute(todo);
});
<span class="pl-c">// read</span>
orma<span class="pl-k">.</span>selectFromTodo()
.titleEq(<span class="pl-s"><span class="pl-pds">"</span>foo<span class="pl-pds">"</span></span>) <span class="pl-c">// equivalent to `where("title = ?", "foo")`</span>
.executeAsObservable() <span class="pl-c">// first-class RxJava interface</span>
.subscribe(<span class="pl-c1">...</span>);
<span class="pl-c">// update</span>
orma<span class="pl-k">.</span>updateTodo()
.titleEq(<span class="pl-s"><span class="pl-pds">"</span>foo<span class="pl-pds">"</span></span>)
.content(<span class="pl-s"><span class="pl-pds">"</span>a new content<span class="pl-pds">"</span></span>) <span class="pl-c">// to setup what are updated</span>
.execute();
<span class="pl-c">// delete</span>
orma<span class="pl-k">.</span>deleteFromTodo()
.titleEq(<span class="pl-s"><span class="pl-pds">"</span>foo<span class="pl-pds">"</span></span>)
.execute();</pre></div>
<p>Note that <strong>Orma aborts if writing occurs on main thread</strong> in debug build.</p>
<p>Use background threads for writing or RxJava interfaces with <code>Schedulers.io()</code>.</p>
<p>Otherwise you can disable this behavior:</p>
<div class="highlight highlight-source-java"><pre><span class="pl-smi">OrmaDatabase</span> orma <span class="pl-k">=</span> <span class="pl-smi">OrmaDatabase</span><span class="pl-k">.</span>builder(context)
.writeOnMainThread(<span class="pl-smi">BuildConfig</span><span class="pl-c1"><span class="pl-k">.</span>DEBUG</span> <span class="pl-k">?</span> <span class="pl-smi">AccessThreadConstraint</span><span class="pl-c1"><span class="pl-k">.</span>WARNING</span> <span class="pl-k">:</span> <span class="pl-smi">AccessThreadConstraint</span><span class="pl-c1"><span class="pl-k">.</span>NONE</span>)
.build();</pre></div>
<h2>
<a id="the-components" class="anchor" href="#the-components" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>The Components</h2>
<h3>
<a id="database-handles" class="anchor" href="#database-handles" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Database Handles</h3>
<p>A database handle, named <code>OrmaDatabase</code> by default, is generated by <code>orma-processor</code>, which is an entry point of all the high-level database operations.</p>
<p>This is typically used as a singleton instance and you don't need to manage its lifecycle. That is, you don't need to explicitly close it.</p>
<h3>
<a id="models" class="anchor" href="#models" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Models</h3>
<p>A <strong>model</strong> in Orma is a Java class that is annotated with <code>@Table</code>, which
has at least one column, a field annotated with <code>@Column</code> or <code>@PrimaryKey</code>.</p>
<p><code>orma-processor</code> generates helper classes for each model:
<code>Schema</code>, <code>Relation</code>, <code>Selector</code>, <code>Updater</code>, and <code>Deleter</code>.</p>
<p>Because these helper classes are generated at the compile time, you
can use Orma as a type-safe ORM.</p>
<h3>
<a id="schema-helpers" class="anchor" href="#schema-helpers" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Schema Helpers</h3>
<p>A Schema helper, e.g. <code>Todo_Schema</code>, has metadata for the corresponding model.</p>
<p>This is an internal helper class and not intended to be employed by users.</p>
<h3>
<a id="relation-helpers" class="anchor" href="#relation-helpers" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Relation Helpers</h3>
<p>A Relation helper, e.g. <code>Todo_Relation</code>, is an entry point of table operations, which has conditions and orderings.</p>
<p>This is created by a database handle:</p>
<div class="highlight highlight-source-java"><pre><span class="pl-k">public</span> <span class="pl-k">static</span> <span class="pl-smi">Todo_Relation</span> relation() {
<span class="pl-k">return</span> orma<span class="pl-k">.</span>relationOfTodo();
}</pre></div>
<p>And is able to create <code>Selector</code>, <code>Updater</code>, <code>Deleter</code>, and <code>Inserter</code> for the model.</p>
<div class="highlight highlight-source-java"><pre><span class="pl-smi">Todo_Relation</span> todos <span class="pl-k">=</span> orma<span class="pl-k">.</span>relationOfTodo();
todos<span class="pl-k">.</span>selector()<span class="pl-k">.</span>toList(); <span class="pl-c">// Todo_Selector</span>
todos<span class="pl-k">.</span>updater()<span class="pl-k">.</span>content(<span class="pl-s"><span class="pl-pds">"</span>foo<span class="pl-pds">"</span></span>)<span class="pl-k">.</span>execute(); <span class="pl-c">// Todo_Updater</span>
todos<span class="pl-k">.</span>inserter()<span class="pl-k">.</span>execute(todo); <span class="pl-c">// Inserter<Todo></span>
todos<span class="pl-k">.</span>deleter()<span class="pl-k">.</span>execute(); <span class="pl-c">// Todo_Deleter</span></pre></div>
<p>This can be a subset of a table which has <code>ORDER BY</code> clauses and <code>WHERE</code> clauses with some <code>List</code>-like methods:</p>
<div class="highlight highlight-source-java"><pre><span class="pl-smi">Todo_Relation</span> todos <span class="pl-k">=</span> orma<span class="pl-k">.</span>relationOfTodo()
.doneEq(<span class="pl-c1">false</span>) <span class="pl-c">// can have conditions</span>
.orderByCreatedTimeMillis(); <span class="pl-c">// can have orders</span>
<span class="pl-c">// List-like features:</span>
<span class="pl-k">int</span> count <span class="pl-k">=</span> todos<span class="pl-k">.</span>count();
<span class="pl-smi">Todo</span> todo <span class="pl-k">=</span> todos<span class="pl-k">.</span>get(<span class="pl-c1">0</span>);
<span class="pl-c">// Convenience utilities</span>
<span class="pl-k">int</span> position <span class="pl-k">=</span> todos<span class="pl-k">.</span>indexOf(todo);
todos<span class="pl-k">.</span>deleteWithTransactionAsObservable()
.subscribeOn(<span class="pl-smi">Schedulers</span><span class="pl-k">.</span>io())
.observeOn(<span class="pl-smi">AndroidSchedulers</span><span class="pl-k">.</span>mainThread())
.subscribe(position <span class="pl-k">-</span><span class="pl-k">></span> {
notifyItemRemoved(position); <span class="pl-c">// assumes Adapter#notifyItemRemoved()</span>
})
todos<span class="pl-k">.</span>truncateWithTransactionAsObservable()
.subscribeOn(<span class="pl-smi">Schedulers</span><span class="pl-k">.</span>io())
.subscribe();
<span class="pl-c">// Todo_Relation implements Iterable<Todo></span>
<span class="pl-k">for</span> (<span class="pl-smi">Todo</span> todo <span class="pl-k">:</span> todos) {
<span class="pl-c">// ...</span>
}</pre></div>
<h3>
<a id="selector-helpers" class="anchor" href="#selector-helpers" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Selector Helpers</h3>
<p>A <code>Selector</code> helper, e.g. <code>Todo_Selector</code>, is created by a <code>Relation</code>:</p>
<div class="highlight highlight-source-java"><pre><span class="pl-smi">Todo_Selector</span> selector <span class="pl-k">=</span> relation()<span class="pl-k">.</span>selector();
<span class="pl-c">// or orma.selectFromTodo();</span></pre></div>
<p>This is a query builder for <code>SELECT ... FROM *</code> statements.</p>
<h3>
<a id="updater-helpers" class="anchor" href="#updater-helpers" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Updater Helpers</h3>
<p>An <code>Updater</code> helper, e.g. <code>Todo_Updater</code>, is created by a <code>Relation</code>:</p>
<div class="highlight highlight-source-java"><pre><span class="pl-smi">Todo_Updater</span> updater <span class="pl-k">=</span> relation()<span class="pl-k">.</span>updater();
<span class="pl-c">// or orma.updateTodo();</span></pre></div>
<p>This is a query builder for <code>UPDATE *</code> statements.</p>
<h3>
<a id="deleter-helpers" class="anchor" href="#deleter-helpers" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Deleter Helpers</h3>
<p>A <code>Deleter</code> helper, e.g. <code>Todo_Deleter</code>, is created by a <code>Relation</code>:</p>
<div class="highlight highlight-source-java"><pre><span class="pl-smi">Todo_Deleter</span> deleter <span class="pl-k">=</span> relation()<span class="pl-k">.</span>deleter();
<span class="pl-c">// or orma.deleteFromTodo();</span></pre></div>
<p>This is a query builder for <code>DELETE FROM *</code> statements.</p>
<h3>
<a id="query-helper-methods" class="anchor" href="#query-helper-methods" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Query Helper Methods</h3>
<p>There are <strong>Query Helpers</strong> which are generated to query conditions and orders in a type-safe way.</p>
<p>For example, <code>titleEq()</code> shown in the synopsis section, are generated to help make <code>WHERE</code> and <code>ORDER BY</code> clauses,
for <code>Relation</code>, <code>Selecotr</code>, <code>Deleter</code>, and <code>Updater</code>.</p>
<p>Most of them are generated for columns with <code>indexed = true</code>, and some are for <code>@PrimaryKey</code> columns.</p>
<h4>
<a id="list-of-query-helper-methods" class="anchor" href="#list-of-query-helper-methods" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>List of Query Helper Methods</h4>
<p>Here is a list of Query Helpers that are generated for <strong>all</strong> the <code>indexed</code> columns, where <code>*</code> is a column name pladeholder:</p>
<table>
<thead>
<tr>
<th align="center">Method</th>
<th align="center">SQL</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center"><code>*Eq(value)</code></td>
<td align="center"><code>* = value</code></td>
</tr>
<tr>
<td align="center"><code>*NotEq(value)</code></td>
<td align="center"><code>* <> value</code></td>
</tr>
<tr>
<td align="center"><code>*In(values)</code></td>
<td align="center"><code>* IN (values)</code></td>
</tr>
<tr>
<td align="center"><code>*NotIn(values)</code></td>
<td align="center"><code>* NOT IN (values)</code></td>
</tr>
</tbody>
</table>
<p>The following are generated for <code>@Nullable</code> columns.</p>
<table>
<thead>
<tr>
<th align="center">Method</th>
<th align="center">SQL</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center"><code>*IsNull()</code></td>
<td align="center"><code>* IS NULL</code></td>
</tr>
<tr>
<td align="center"><code>*IsNotNull()</code></td>
<td align="center"><code>* IS NOT NULL</code></td>
</tr>
</tbody>
</table>
<p>The following are generated for numeric columns
(i.e. <code>byte</code>, <code>short</code>, <code>int</code>, <code>long</code>, <code>float</code>, <code>double</code>, and their corresponding box types)</p>
<table>
<thead>
<tr>
<th align="center">Method</th>
<th align="center">SQL</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center"><code>*Lt(value)</code></td>
<td align="center"><code>* < value</code></td>
</tr>
<tr>
<td align="center"><code>*Le(values)</code></td>
<td align="center"><code>* <= value</code></td>
</tr>
<tr>
<td align="center"><code>*Gt(value)</code></td>
<td align="center"><code>* > value</code></td>
</tr>
<tr>
<td align="center"><code>*Ge(value)</code></td>
<td align="center"><code>* >= value</code></td>
</tr>
<tr>
<td align="center"><code>*Between(a, b)</code></td>
<td align="center"><code>* BETWEEN a AND b</code></td>
</tr>
</tbody>
</table>
<p>And <code>ORDER BY</code> helpers:</p>
<table>
<thead>
<tr>
<th align="center">Method</th>
<th align="center">SQL</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center"><code>orderBy*Asc()</code></td>
<td align="center"><code>ORDER BY * ASC</code></td>
</tr>
<tr>
<td align="center"><code>orderBy*Desc()</code></td>
<td align="center"><code>ORDER BY * DESC</code></td>
</tr>
</tbody>
</table>
<h4>
<a id="how-to-control-generation-of-query-helpers" class="anchor" href="#how-to-control-generation-of-query-helpers" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>How to Control Generation of Query Helpers</h4>
<p><strong>This is an advanced setting for those who know what they do.</strong></p>
<p>You can control which Query Helpers are generater for a column by <code>@Column(helpers = ...)</code> attribute:</p>
<div class="highlight highlight-source-java"><pre><span class="pl-k">@Column</span>(
<span class="pl-c1">helpers</span> <span class="pl-k">=</span> <span class="pl-smi">Column</span><span class="pl-k">.</span><span class="pl-smi">Helpers</span><span class="pl-c1"><span class="pl-k">.</span>AUTO</span> <span class="pl-c">// default to AUTO</span>
)</pre></div>
<p>Here are the definition of options defined in <a href="annotations/src/main/java/com/github/gfx/android/orma/annotation/Column.java">Column.java</a>:</p>
<div class="highlight highlight-source-java"><pre><span class="pl-k">long</span> <span class="pl-c1">AUTO</span> <span class="pl-k">=</span> <span class="pl-k">-</span><span class="pl-c1">1</span>; <span class="pl-c">// the default, a smart way</span>
<span class="pl-k">long</span> <span class="pl-c1">NONE</span> <span class="pl-k">=</span> <span class="pl-c1">0</span>;
<span class="pl-k">long</span> <span class="pl-c1">CONDITION_EQ</span> <span class="pl-k">=</span> <span class="pl-c1">0b01</span>;
<span class="pl-k">long</span> <span class="pl-c1">CONDITION_NOT_EQ</span> <span class="pl-k">=</span> <span class="pl-c1">CONDITION_EQ</span> <span class="pl-k"><<</span> <span class="pl-c1">1</span>;
<span class="pl-k">long</span> <span class="pl-c1">CONDITION_IS_NULL</span> <span class="pl-k">=</span> <span class="pl-c1">CONDITION_NOT_EQ</span> <span class="pl-k"><<</span> <span class="pl-c1">1</span>;
<span class="pl-k">long</span> <span class="pl-c1">CONDITION_IS_NOT_NULL</span> <span class="pl-k">=</span> <span class="pl-c1">CONDITION_IS_NULL</span> <span class="pl-k"><<</span> <span class="pl-c1">1</span>;
<span class="pl-k">long</span> <span class="pl-c1">CONDITION_IN</span> <span class="pl-k">=</span> <span class="pl-c1">CONDITION_IS_NOT_NULL</span> <span class="pl-k"><<</span> <span class="pl-c1">1</span>;
<span class="pl-k">long</span> <span class="pl-c1">CONDITION_NOT_IN</span> <span class="pl-k">=</span> <span class="pl-c1">CONDITION_IN</span> <span class="pl-k"><<</span> <span class="pl-c1">1</span>;
<span class="pl-k">long</span> <span class="pl-c1">CONDITION_LT</span> <span class="pl-k">=</span> <span class="pl-c1">CONDITION_NOT_IN</span> <span class="pl-k"><<</span> <span class="pl-c1">1</span>;
<span class="pl-k">long</span> <span class="pl-c1">CONDITION_LE</span> <span class="pl-k">=</span> <span class="pl-c1">CONDITION_LT</span> <span class="pl-k"><<</span> <span class="pl-c1">1</span>;
<span class="pl-k">long</span> <span class="pl-c1">CONDITION_GT</span> <span class="pl-k">=</span> <span class="pl-c1">CONDITION_LE</span> <span class="pl-k"><<</span> <span class="pl-c1">1</span>;
<span class="pl-k">long</span> <span class="pl-c1">CONDITION_GE</span> <span class="pl-k">=</span> <span class="pl-c1">CONDITION_GT</span> <span class="pl-k"><<</span> <span class="pl-c1">1</span>;
<span class="pl-k">long</span> <span class="pl-c1">CONDITION_BETWEEN</span> <span class="pl-k">=</span> <span class="pl-c1">CONDITION_GE</span> <span class="pl-k"><<</span> <span class="pl-c1">1</span>;
<span class="pl-k">long</span> <span class="pl-c1">CONDITIONS</span> <span class="pl-k">=</span> <span class="pl-c1">CONDITION_EQ</span> <span class="pl-k">|</span> <span class="pl-c1">CONDITION_NOT_EQ</span> <span class="pl-k">|</span> <span class="pl-c1">CONDITION_IS_NULL</span> <span class="pl-k">|</span> <span class="pl-c1">CONDITION_IS_NOT_NULL</span>
<span class="pl-k">|</span> <span class="pl-c1">CONDITION_IN</span> <span class="pl-k">|</span> <span class="pl-c1">CONDITION_NOT_IN</span>
<span class="pl-k">|</span> <span class="pl-c1">CONDITION_LT</span> <span class="pl-k">|</span> <span class="pl-c1">CONDITION_LE</span> <span class="pl-k">|</span> <span class="pl-c1">CONDITION_GT</span> <span class="pl-k">|</span> <span class="pl-c1">CONDITION_GE</span> <span class="pl-k">|</span> <span class="pl-c1">CONDITION_BETWEEN</span>;
<span class="pl-k">long</span> <span class="pl-c1">ORDER_IN_ASC</span> <span class="pl-k">=</span> <span class="pl-c1">CONDITION_BETWEEN</span> <span class="pl-k"><<</span> <span class="pl-c1">1</span>;
<span class="pl-k">long</span> <span class="pl-c1">ORDER_IN_DESC</span> <span class="pl-k">=</span> <span class="pl-c1">ORDER_IN_ASC</span> <span class="pl-k"><<</span> <span class="pl-c1">1</span>;
<span class="pl-k">long</span> <span class="pl-c1">ORDERS</span> <span class="pl-k">=</span> <span class="pl-c1">ORDER_IN_ASC</span> <span class="pl-k">|</span> <span class="pl-c1">ORDER_IN_DESC</span>;
<span class="pl-k">long</span> <span class="pl-c1">ALL</span> <span class="pl-k">=</span> <span class="pl-c1">CONDITIONS</span> <span class="pl-k">|</span> <span class="pl-c1">ORDERS</span>;</pre></div>
<h3>
<a id="the-inserter-helpers" class="anchor" href="#the-inserter-helpers" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>The Inserter Helpers</h3>
<p>This is a prepared statement for <code>INSERT INTO ...</code> for bulk insertions.</p>
<div class="highlight highlight-source-java"><pre><span class="pl-k">Inserter<<span class="pl-smi">Todo</span>></span> inserter <span class="pl-k">=</span> relation()<span class="pl-k">.</span>inserter();
<span class="pl-c">// or orma.insertIntoTodo()</span>
inserter<span class="pl-k">.</span>execute(todo);
inserter<span class="pl-k">.</span>executeAll(todos);</pre></div>
<h2>
<a id="details-of-database-handles" class="anchor" href="#details-of-database-handles" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Details of Database Handles</h2>
<p>The section describes the details of database handles.</p>
<h3>
<a id="configuration-of-database-handles" class="anchor" href="#configuration-of-database-handles" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Configuration of Database Handles</h3>
<p>The database class is configured by the <a href="https://github.com/gfx/Android-Orma/blob/master/annotations/src/main/java/com/github/gfx/android/orma/annotation/Database.java"><code>@Database</code></a> annotation:</p>
<div class="highlight highlight-source-java"><pre><span class="pl-k">@Database</span>(
<span class="pl-c1">databaseClassName</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">"</span>OrmaDatabase<span class="pl-pds">"</span></span>, <span class="pl-c">// default to "OrmaDatabase"</span>
<span class="pl-c1">includes</span> <span class="pl-k">=</span> { <span class="pl-c">/* ... */</span> } <span class="pl-c">// Give model classes to handle</span>
<span class="pl-c1">excludes</span> <span class="pl-k">=</span> { <span class="pl-c">/* ... */</span> } <span class="pl-c">// Give model classes not to handle</span>
)
<span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-en">DatabaseConfiguration</span> { }</pre></div>
<p>The annotated class is not used for now, but the package is used to place the OrmaDatabase class.</p>
<h3>
<a id="database-handle-builders" class="anchor" href="#database-handle-builders" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Database Handle Builders</h3>
<p><code>OrmaDatabase.builder(Context)</code> returns a builder isntance, which
has configure the database handle instance:</p>
<table>
<thead>
<tr>
<th align="center">Method</th>
<th align="center">Description</th>
<th align="center">Default</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center"><code>name(String)</code></td>
<td align="center">The filename of SQLite DB</td>
<td align="center"><code>"${package}.orma.db"</code></td>
</tr>
<tr>
<td align="center"><code>migrationEngine(MigrationEngine)</code></td>
<td align="center">Custom migration engine</td>
<td align="center"><code>OrmaMigration</code></td>
</tr>
<tr>
<td align="center"><code>writeAheadLogging(boolean)</code></td>
<td align="center">SQLite WAL flag</td>
<td align="center"><code>true</code></td>
</tr>
<tr>
<td align="center"><code>foreignKeys(boolean)</code></td>
<td align="center">SQLite FOREIGN_KEYS flag</td>
<td align="center"><code>true</code></td>
</tr>
<tr>
<td align="center"><code>migrationStep(int, ManualStepMigration.Step)</code></td>
<td align="center">A migration step</td>
<td align="center">none</td>
</tr>
<tr>
<td align="center"><code>trace(boolean)</code></td>
<td align="center">Output executed queries to logcat if true</td>
<td align="center">dynamic (*1)</td>
</tr>
<tr>
<td align="center"><code>readOnMainThread(AccessThreadConstraint)</code></td>
<td align="center">Check read operation on main thread</td>
<td align="center">dynamic (*2)</td>
</tr>
<tr>
<td align="center"><code>writeOnMainThread(AccessThreadConstraint)</code></td>
<td align="center">Check write operation on main thread</td>
<td align="center">dynaimc (*3)</td>
</tr>
</tbody>
</table>
<ul>
<li>
<strong>*1</strong> <code>BuildConfig.DEBUG ? true : false</code>
</li>
<li>
<strong>*2</strong> <code>BuildConfig.DEBUG ? WARN : NONE</code>
</li>
<li>
<strong>*3</strong> <code>BuildConfig.DEBUG ? FATAL : NONE</code>
</li>
</ul>
<h2>
<a id="details-of-models" class="anchor" href="#details-of-models" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Details of Models</h2>
<p>The section describes the details of model definition.</p>
<h3>
<a id="setters-and-getters" class="anchor" href="#setters-and-getters" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Setters and Getters</h3>
<p>Orma can use getters and setters if columns have corresponding methods.</p>
<p>You can also connect getters and setters with <code>@Getter</code> and <code>@Setter</code>
respectively, which tells <code>orma-processor</code> to use accessors.</p>
<p>Each accessor name can have a column name in SQLite databases,
which is inferred from its method name if omitted.</p>
<div class="highlight highlight-source-java"><pre><span class="pl-k">@Table</span>
<span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-en">KeyValuePair</span> {
<span class="pl-k">static</span> <span class="pl-k">final</span> <span class="pl-smi">String</span> kKey <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">"</span>Key<span class="pl-pds">"</span></span>;
<span class="pl-k">@Column</span>(kKey) <span class="pl-c">// specifies the name</span>
<span class="pl-k">private</span> <span class="pl-smi">String</span> key;
<span class="pl-k">@Column</span> <span class="pl-c">// omits the name</span>
<span class="pl-k">private</span> <span class="pl-smi">String</span> value;
<span class="pl-k">@Getter</span>(kKey)
<span class="pl-k">public</span> <span class="pl-smi">String</span> <span class="pl-en">getKey</span>() {
<span class="pl-k">return</span> key;
}
<span class="pl-k">@Setter</span>(kKey)
<span class="pl-k">public</span> <span class="pl-k">void</span> <span class="pl-en">setKey</span>(<span class="pl-smi">String</span> <span class="pl-v">key</span>) {
<span class="pl-v">this</span><span class="pl-k">.</span>key <span class="pl-k">=</span> key;
}
<span class="pl-c">// used as a getter for the "value" column</span>
<span class="pl-c">// @Getter is optional in this case</span>
<span class="pl-k">public</span> <span class="pl-smi">String</span> <span class="pl-en">getValue</span>() {
<span class="pl-k">return</span> value;
}
<span class="pl-c">// used as a setter for the "value" column</span>
<span class="pl-c">// @Setter is optional in this case</span>
<span class="pl-k">public</span> <span class="pl-k">void</span> <span class="pl-en">setValue</span>(<span class="pl-smi">String</span> <span class="pl-v">value</span>) {
<span class="pl-v">this</span><span class="pl-k">.</span>value <span class="pl-k">=</span> value;
}
}</pre></div>
<h3>
<a id="immutable-models" class="anchor" href="#immutable-models" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Immutable Models</h3>
<p>Immutable models, where all the fields are declared with <code>final</code>, are supported
by annotating a constructor with <code>@Setter</code>.</p>
<div class="highlight highlight-source-java"><pre><span class="pl-k">@Table</span>
<span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-en">KeyValuePair</span> {
<span class="pl-k">@Column</span>
<span class="pl-k">public</span> <span class="pl-k">final</span> <span class="pl-smi">String</span> key;
<span class="pl-k">@Column</span>
<span class="pl-k">public</span> <span class="pl-k">final</span> <span class="pl-smi">String</span> value;
<span class="pl-k">@Setter</span>
<span class="pl-en">KeyValuePair</span>(<span class="pl-smi">String</span> <span class="pl-v">key</span>, <span class="pl-smi">String</span> <span class="pl-v">value</span>) {
<span class="pl-v">this</span><span class="pl-k">.</span>key <span class="pl-k">=</span> key;
<span class="pl-v">this</span><span class="pl-k">.</span>value <span class="pl-k">=</span> value;
}
}</pre></div>
<p>It can be declared with custom names:</p>
<div class="highlight highlight-source-java"><pre><span class="pl-k">@Table</span>
<span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-en">KeyValuePair</span> {
<span class="pl-k">static</span> <span class="pl-k">final</span> <span class="pl-smi">String</span> kKey <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">"</span>Key<span class="pl-pds">"</span></span>;
<span class="pl-k">static</span> <span class="pl-k">final</span> <span class="pl-smi">String</span> kValue <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">"</span>Value<span class="pl-pds">"</span></span>;
<span class="pl-k">@Column</span>(kKey)
<span class="pl-k">public</span> <span class="pl-k">final</span> <span class="pl-smi">String</span> key;
<span class="pl-k">@Column</span>(kValue)
<span class="pl-k">public</span> <span class="pl-k">final</span> <span class="pl-smi">String</span> value;
<span class="pl-en">KeyValuePair</span>(<span class="pl-k">@Setter</span>(kKey) <span class="pl-smi">String</span> <span class="pl-v">key</span>, <span class="pl-k">@Setter</span>(kValue) <span class="pl-smi">String</span> <span class="pl-v">value</span>) {
<span class="pl-v">this</span><span class="pl-k">.</span>key <span class="pl-k">=</span> key;
<span class="pl-v">this</span><span class="pl-k">.</span>value <span class="pl-k">=</span> value;
}
}</pre></div>
<h2>
<a id="associations" class="anchor" href="#associations" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Associations</h2>
<p>Two or more Orma models can be associated with <strong>association</strong> mechanism.</p>
<p>There are two type of associations: <strong>has-one</strong> and <strong>has-many</strong>.</p>
<p>In addition, there are another two kind of association supports: indirect associations with <code>SingleAssociation<T></code> and direct associations.</p>
<h3>
<a id="has-one-associations-with-singleassociationt" class="anchor" href="#has-one-associations-with-singleassociationt" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Has-One Associations with <code>SingleAssociation<T></code>
</h3>
<p>There is <code>SingleAssociation<T></code> to support has-one associations, which is
retrieved on demand, or loaded lazily.</p>
<p>For example, a book has a publisher:</p>
<div class="highlight highlight-source-java"><pre><span class="pl-k">@Table</span>
<span class="pl-k">class</span> <span class="pl-en">Publisher</span> {
<span class="pl-k">@PrimaryKey</span>
<span class="pl-k">public</span> <span class="pl-k">long</span> id;
}
<span class="pl-k">@Table</span>
<span class="pl-k">class</span> <span class="pl-en">Book</span> {
<span class="pl-k">@Column</span>
<span class="pl-k">public</span> <span class="pl-k">SingleAssociation<<span class="pl-smi">Publisher</span>></span> publisher;
}</pre></div>
<p>The entity of <code>Book#publisher</code> is <code>Publisher#id</code>.</p>
<h3>
<a id="has-many-associations-with-singleassociationt" class="anchor" href="#has-many-associations-with-singleassociationt" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Has-Many Associations with <code>SingleAssociation<T></code>
</h3>
<p>Has-many associations are not directly supported but you can define a method to get associated objects:</p>
<div class="highlight highlight-source-java"><pre><span class="pl-k">@Table</span>
<span class="pl-k">class</span> <span class="pl-en">Publisher</span> {
<span class="pl-k">@PrimaryKey</span>
<span class="pl-k">public</span> <span class="pl-k">long</span> id;
<span class="pl-k">public</span> <span class="pl-smi">Book_Relation</span> <span class="pl-en">getBooks</span>(<span class="pl-smi">OrmaDatabase</span> <span class="pl-v">orma</span>) {
<span class="pl-k">return</span> orma<span class="pl-k">.</span>relationOfBook()<span class="pl-k">.</span>publisherEq(<span class="pl-v">this</span>);
}
}
<span class="pl-k">@Table</span>
<span class="pl-k">class</span> <span class="pl-en">Book</span> {
<span class="pl-k">@Column</span>(<span class="pl-c1">indexed</span> <span class="pl-k">=</span> <span class="pl-c1">true</span>)
<span class="pl-k">public</span> <span class="pl-k">SingleAssociation<<span class="pl-smi">Publisher</span>></span> publisher;
}</pre></div>
<h3>
<a id="direct-associations" class="anchor" href="#direct-associations" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Direct Associations</h3>
<p>There are <em>direct associations</em>, where an Orma model has another Orma model directly.</p>
<p>Given a <code>has-one</code> association, <code>Book has-one Publisher</code>:</p>
<div class="highlight highlight-source-java"><pre><span class="pl-k">@Table</span>
<span class="pl-k">class</span> <span class="pl-en">Publisher</span> {
<span class="pl-k">@PrimaryKey</span>
<span class="pl-k">public</span> <span class="pl-k">long</span> id;
<span class="pl-k">@Column</span>
<span class="pl-k">public</span> <span class="pl-smi">String</span> name;
}
<span class="pl-k">@Table</span>
<span class="pl-k">class</span> <span class="pl-en">Book</span> {
<span class="pl-k">@PrimaryKey</span>
<span class="pl-k">public</span> <span class="pl-k">long</span> id;
<span class="pl-k">@column</span>
<span class="pl-k">public</span> <span class="pl-smi">String</span> title;
<span class="pl-k">@Column</span>
<span class="pl-k">public</span> <span class="pl-smi">Publisher</span> publisher;
}</pre></div>
<p>The corresponding table definition is something like this:</p>
<div class="highlight highlight-source-sql"><pre><span class="pl-k">CREATE</span> <span class="pl-k">TABLE</span> `<span class="pl-en">Publisher</span>` (
<span class="pl-s"><span class="pl-pds">`</span>id<span class="pl-pds">`</span></span> <span class="pl-k">INTEGER</span> <span class="pl-k">PRIMARY KEY</span>,
<span class="pl-s"><span class="pl-pds">`</span>name<span class="pl-pds">`</span></span> <span class="pl-k">TEXT</span> <span class="pl-k">NOT NULL</span>
)
<span class="pl-k">CREATE</span> <span class="pl-k">TABLE</span> `<span class="pl-en">Book</span>` (
<span class="pl-s"><span class="pl-pds">`</span>id<span class="pl-pds">`</span></span> <span class="pl-k">INTEGER</span> <span class="pl-k">PRIMARY KEY</span>,
<span class="pl-s"><span class="pl-pds">`</span>title<span class="pl-pds">`</span></span> <span class="pl-k">TEXT</span> <span class="pl-k">NOT NULL</span>,
<span class="pl-s"><span class="pl-pds">`</span>publisher<span class="pl-pds">`</span></span> <span class="pl-k">INTEGER</span> <span class="pl-k">NOT NULL</span>
<span class="pl-k">REFERENCES</span> <span class="pl-s"><span class="pl-pds">`</span>Publisher<span class="pl-pds">`</span></span>(<span class="pl-s"><span class="pl-pds">`</span>id<span class="pl-pds">`</span></span>) <span class="pl-k">ON</span> <span class="pl-k">UPDATE</span> CASCADE <span class="pl-k">ON DELETE CASCADE</span>
)</pre></div>
<p>In SQL, <code>Book#publisher</code> refers <code>Publisher#id</code>, indicating the two tables
should be joined in <code>SELECT</code> statements.</p>
<p>In Java, <code>Book#publisher</code> is a <code>Publisher</code> instance, which is retrieved in each
<code>SELECT</code> operations. There is no lazy loading in direct associations.</p>
<h3>
<a id="limitations-in-associations" class="anchor" href="#limitations-in-associations" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Limitations in Associations</h3>
<ul>
<li>There are no methods to query associated models</li>
</ul>
<p>These issues will be fixed in a future.</p>
<h2>
<a id="type-adapters" class="anchor" href="#type-adapters" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Type Adapters</h2>
<p>Orma models are able to have embedded objects with <strong>type adapters</strong>, a.k.a. static type adapters,
by defining classes with <code>@StaticTypeAdapter</code> annotation.</p>
<p>For example, if you want to embed <a href="https://developers.google.com/android/reference/com/google/android/gms/maps/model/LatLng">LatLng</a>
in your Orma model, you can define a type adapter like this:</p>
<div class="highlight highlight-source-java"><pre><span class="pl-k">@StaticTypeAdapter</span>(
<span class="pl-c1">targetType</span> <span class="pl-k">=</span> <span class="pl-smi">LatLng</span><span class="pl-k">.</span>class, <span class="pl-c">// required</span>
<span class="pl-c1">serializedType</span> <span class="pl-k">=</span> <span class="pl-smi">String</span><span class="pl-k">.</span>class <span class="pl-c">// required</span>
)
<span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-en">LatLngAdapter</span> {
<span class="pl-c">// SerializedType serialize(TargetType source)</span>
<span class="pl-k">@NonNull</span>
<span class="pl-k">public</span> <span class="pl-k">static</span> <span class="pl-smi">String</span> <span class="pl-en">serialize</span>(<span class="pl-k">@NonNull</span> <span class="pl-smi">LatLng</span> <span class="pl-v">source</span>) {
<span class="pl-k">return</span> source<span class="pl-k">.</span>latitude <span class="pl-k">+</span> <span class="pl-s"><span class="pl-pds">"</span>,<span class="pl-pds">"</span></span> <span class="pl-k">+</span> source<span class="pl-k">.</span>longitude
}
<span class="pl-c">// TargetType deserialize(SerializedType serialized)</span>
<span class="pl-k">@NonNull</span>
<span class="pl-k">public</span> <span class="pl-k">static</span> <span class="pl-smi">LatLng</span> <span class="pl-en">deserialize</span>(<span class="pl-k">@NonNull</span> <span class="pl-smi">String</span> <span class="pl-v">serialized</span>) {
<span class="pl-k">String</span>[] values <span class="pl-k">=</span> serialized<span class="pl-k">.</span>split(<span class="pl-s"><span class="pl-pds">"</span>,<span class="pl-pds">"</span></span>);
<span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-smi">LatLng</span>(
<span class="pl-smi">Double</span><span class="pl-k">.</span>parseDouble(values[<span class="pl-c1">0</span>]),
<span class="pl-smi">Double</span><span class="pl-k">.</span>parseDouble(values[<span class="pl-c1">1</span>]));
}
}</pre></div>
<p><code>@StaticTypeAdapter</code> requires <code>targetType</code> and <code>serializedType</code> options and two static methods <code>SerializedType serialize(TargetType)</code> and <code>TargetType deserialize(SerializedType)</code>.</p>
<h3>
<a id="how-serialized-types-used" class="anchor" href="#how-serialized-types-used" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>How Serialized Types Used</h3>
<p>A <code>@StaticTypeAdapter#serializedType</code> is bound to an SQLite storage type.
Thus it must be one of the "Java Type" listed the table below, where each "Java Type" has a corresponding "SQLite Type":</p>
<table>
<thead>
<tr>
<th align="center">Java Type</th>
<th align="center">SQLite Type</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">int</td>
<td align="center">INTEGER</td>
</tr>
<tr>
<td align="center">short</td>
<td align="center">INTEGER</td>
</tr>
<tr>
<td align="center">long</td>
<td align="center">INTEGER</td>
</tr>
<tr>
<td align="center">boolean</td>
<td align="center">INTEGER</td>
</tr>
<tr>
<td align="center">float</td>
<td align="center">REAL</td>
</tr>
<tr>
<td align="center">double</td>
<td align="center">REAL</td>
</tr>
<tr>
<td align="center">String</td>
<td align="center">TEXT</td>
</tr>
<tr>
<td align="center">byte[]</td>
<td align="center">BLOB</td>
</tr>
</tbody>
</table>
<h3>
<a id="statictypeadapters-for-multiple-serializers-at-once" class="anchor" href="#statictypeadapters-for-multiple-serializers-at-once" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><code>@StaticTypeAdapters</code> for Multiple Serializers at Once</h3>
<p>You can also define multiple type serializers to single class with <code>@StaticTypeAdapters</code> annotation containers:</p>
<div class="highlight highlight-source-java"><pre><span class="pl-k">@StaticTypeAdapters</span>({
<span class="pl-k">@StaticTypeAdapter</span>(
<span class="pl-c1">targetType</span> <span class="pl-k">=</span> <span class="pl-smi">MutableInt</span><span class="pl-k">.</span>class,
<span class="pl-c1">serializedType</span> <span class="pl-k">=</span> <span class="pl-k">int</span><span class="pl-k">.</span>class,
<span class="pl-c1">serializer</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">"</span>serializeMutableInt<span class="pl-pds">"</span></span>,
<span class="pl-c1">deserializer</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">"</span>deserializeMutableInt<span class="pl-pds">"</span></span>
),
<span class="pl-k">@StaticTypeAdapter</span>(
<span class="pl-c1">targetType</span> <span class="pl-k">=</span> <span class="pl-smi">MutableLong</span><span class="pl-k">.</span>class,
<span class="pl-c1">serializedType</span> <span class="pl-k">=</span> <span class="pl-k">long</span><span class="pl-k">.</span>class,
<span class="pl-c1">serializer</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">"</span>serializeMutableLong<span class="pl-pds">"</span></span>,
<span class="pl-c1">deserializer</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">"</span>deserializeMutableLong<span class="pl-pds">"</span></span>
)
})
<span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-en">TypeAdapters</span> {
<span class="pl-k">public</span> <span class="pl-k">static</span> <span class="pl-k">int</span> <span class="pl-en">serializeMutableInt</span>(<span class="pl-k">@NonNull</span> <span class="pl-smi">MutableInt</span> <span class="pl-v">target</span>) {
<span class="pl-k">return</span> target<span class="pl-k">.</span>value;
}
<span class="pl-k">@NonNull</span>
<span class="pl-k">public</span> <span class="pl-k">static</span> <span class="pl-smi">MutableInt</span> <span class="pl-en">deserializeMutableInt</span>(<span class="pl-k">int</span> <span class="pl-v">deserialized</span>) {
<span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-smi">MutableInt</span>(deserialized);
}
<span class="pl-k">public</span> <span class="pl-k">static</span> <span class="pl-k">long</span> <span class="pl-en">serializeMutableLong</span>(<span class="pl-k">@NonNull</span> <span class="pl-smi">MutableLong</span> <span class="pl-v">target</span>) {
<span class="pl-k">return</span> target<span class="pl-k">.</span>value;
}
<span class="pl-k">@NonNull</span>
<span class="pl-k">public</span> <span class="pl-k">static</span> <span class="pl-smi">MutableLong</span> <span class="pl-en">deserializeMutableLong</span>(<span class="pl-k">long</span> <span class="pl-v">deserialized</span>) {
<span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-smi">MutableLong</span>(deserialized);
}
}</pre></div>
<h3>
<a id="built-in-type-adapters" class="anchor" href="#built-in-type-adapters" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Built-In Type Adapters</h3>
<p>There are built-in type adapters for typically used value objects and collections:</p>
<ul>
<li><code>java.math.BigDecimal</code></li>
<li><code>java.math.BigInteger</code></li>
<li><code>java.nio.ByteBuffer</code></li>
<li><code>java.util.Currency</code></li>
<li><code>java.util.Date</code></li>
<li><code>java.sql.Date</code></li>
<li><code>java.sql.Time</code></li>
<li><code>java.sql.Timestamp</code></li>
<li><code>java.util.UUID</code></li>
<li><code>java.util.List<String></code></li>
<li><code>java.util.ArrayList<String></code></li>
<li><code>java.util.Set<String></code></li>
<li><code>java.util.HashSet<String></code></li>