This repository has been archived by the owner on Apr 28, 2021. It is now read-only.
forked from manveru/KirbyBase
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kirbybaserubymanual.html
2324 lines (2278 loc) · 78.3 KB
/
kirbybaserubymanual.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 PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="generator" content="AsciiDoc 7.0.2" />
<style type="text/css">
/* Debug borders */
p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 {
/*
border: 1px solid red;
*/
}
body {
margin: 1em 5% 1em 5%;
}
a { color: blue; }
a:visited { color: fuchsia; }
em {
font-style: italic;
}
strong {
font-weight: bold;
}
tt {
color: navy;
}
h1, h2, h3, h4, h5, h6 {
color: #527bbd;
font-family: sans-serif;
margin-top: 1.2em;
margin-bottom: 0.5em;
line-height: 1.3;
}
h1 {
border-bottom: 2px solid silver;
}
h2 {
border-bottom: 2px solid silver;
padding-top: 0.5em;
}
div.sectionbody {
font-family: serif;
margin-left: 0;
}
hr {
border: 1px solid silver;
}
p {
margin-top: 0.5em;
margin-bottom: 0.5em;
}
pre {
padding: 0;
margin: 0;
}
span#author {
color: #527bbd;
font-family: sans-serif;
font-weight: bold;
font-size: 1.2em;
}
span#email {
}
span#revision {
font-family: sans-serif;
}
div#footer {
font-family: sans-serif;
font-size: small;
border-top: 2px solid silver;
padding-top: 0.5em;
margin-top: 4.0em;
}
div#footer-text {
float: left;
padding-bottom: 0.5em;
}
div#footer-badges {
float: right;
padding-bottom: 0.5em;
}
div#preamble,
div.tableblock, div.imageblock, div.exampleblock, div.verseblock,
div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
div.admonitionblock {
margin-right: 10%;
margin-top: 1.5em;
margin-bottom: 1.5em;
}
div.admonitionblock {
margin-top: 2.5em;
margin-bottom: 2.5em;
}
div.content { /* Block element content. */
padding: 0;
}
/* Block element titles. */
div.title, caption.title {
font-family: sans-serif;
font-weight: bold;
text-align: left;
margin-top: 1.0em;
margin-bottom: 0.5em;
}
div.title + * {
margin-top: 0;
}
td div.title:first-child {
margin-top: 0.0em;
}
div.content div.title:first-child {
margin-top: 0.0em;
}
div.content + div.title {
margin-top: 0.0em;
}
div.sidebarblock > div.content {
background: #ffffee;
border: 1px solid silver;
padding: 0.5em;
}
div.listingblock > div.content {
border: 1px solid silver;
background: #f4f4f4;
padding: 0.5em;
}
div.quoteblock > div.content {
padding-left: 2.0em;
}
div.quoteblock .attribution {
text-align: right;
}
div.admonitionblock .icon {
vertical-align: top;
font-size: 1.1em;
font-weight: bold;
text-decoration: underline;
color: #527bbd;
padding-right: 0.5em;
}
div.admonitionblock td.content {
padding-left: 0.5em;
border-left: 2px solid silver;
}
div.exampleblock > div.content {
border-left: 2px solid silver;
padding: 0.5em;
}
div.verseblock div.content {
white-space: pre;
}
div.imageblock div.content { padding-left: 0; }
div.imageblock img { border: 1px solid silver; }
span.image img { border-style: none; }
dl {
margin-top: 0.8em;
margin-bottom: 0.8em;
}
dt {
margin-top: 0.5em;
margin-bottom: 0;
font-style: italic;
}
dd > *:first-child {
margin-top: 0;
}
ul, ol {
list-style-position: outside;
}
ol.olist2 {
list-style-type: lower-alpha;
}
div.tableblock > table {
border-color: #527bbd;
border-width: 3px;
}
thead {
font-family: sans-serif;
font-weight: bold;
}
tfoot {
font-weight: bold;
}
div.hlist {
margin-top: 0.8em;
margin-bottom: 0.8em;
}
td.hlist1 {
vertical-align: top;
font-style: italic;
padding-right: 0.8em;
}
td.hlist2 {
vertical-align: top;
}
@media print {
div#footer-badges { display: none; }
}
/* Workarounds for IE6's broken and incomplete CSS2. */
div.sidebar-content {
background: #ffffee;
border: 1px solid silver;
padding: 0.5em;
}
div.sidebar-title, div.image-title {
font-family: sans-serif;
font-weight: bold;
margin-top: 0.0em;
margin-bottom: 0.5em;
}
div.listingblock div.content {
border: 1px solid silver;
background: #f4f4f4;
padding: 0.5em;
}
div.quoteblock-content {
padding-left: 2.0em;
}
div.exampleblock-content {
border-left: 2px solid silver;
padding-left: 0.5em;
}
</style>
<title>KirbyBase Manual (Ruby Version)</title>
</head>
<body>
<div id="header">
<h1>KirbyBase Manual (Ruby Version)</h1>
<span id="author">Jamey Cribbs</span><br />
<span id="email"><tt><<a href="mailto:jcribbs@netpromi.com">jcribbs@netpromi.com</a>></tt></span><br />
v2.6 June 2006
</div>
<div id="preamble">
<div class="sectionbody">
<div class="imageblock">
<div class="content">
<img src="images/kirby1.jpg" alt="images/kirby1.jpg"/>
</div>
<div class="image-title">Figure: Kirby, pictured here in attack mode.</div>
</div>
</div>
</div>
<h2>Table of Contents</h2>
<div class="sectionbody">
<div class="sidebarblock">
<div class="sidebar-content">
<ol>
<li>
<p>
<a href="#introduction">Introduction</a>
</p>
</li>
<li>
<p>
<a href="#connecting-to-a-database">Connecting to a database</a>
</p>
</li>
<li>
<p>
<a href="#creating-a-new-table">Creating a new table</a>
</p>
<ol class="olist2">
<li>
<p>
<a href="#field-types">Database field types</a>
</p>
</li>
<li>
<p>
<a href="#recno">The recno field</a>
</p>
</li>
<li>
<p>
<a href="#encrypt">Turning on encryption</a>
</p>
</li>
<li>
<p>
<a href="#record-class">Specifying a custom record class</a>
</p>
</li>
<li>
<p>
<a href="#defaults">Specifying default values</a>
</p>
</li>
<li>
<p>
<a href="#requireds">Specifying required fields</a>
</p>
</li>
<li>
<p>
<a href="#creating-indexes">Indexes</a>
</p>
</li>
<li>
<p>
<a href="#a-note-about-indexes">A note about indexes</a>
</p>
</li>
<li>
<p>
<a href="#creating-lookup-fields">Lookup Fields</a>
</p>
</li>
<li>
<p>
<a href="#creating-link-many-fields">Link_many Fields</a>
</p>
</li>
<li>
<p>
<a href="#creating-calculated-fields">Calculated Fields</a>
</p>
</li>
</ol>
</li>
<li>
<p>
<a href="#obtaining-a-reference-to-an-existing-table">Obtaining a reference to an existing table</a>
</p>
</li>
<li>
<p>
<a href="#insert-method">The insert method</a>
</p>
</li>
<li>
<p>
<a href="#how-to-select-records">How to select records</a>
</p>
<ol class="olist2">
<li>
<p>
<a href="#select-method">The select method</a>
</p>
</li>
<li>
<p>
<a href="#selecting-by-index">Selecting by index</a>
</p>
</li>
<li>
<p>
<a href="#selecting-by-recno-index">Selecting by :recno index</a>
</p>
</li>
<li>
<p>
<a href="#selects-involving-lookups-or-link-manys">Selects involving Lookups or Link_manys</a>
</p>
</li>
<li>
<p>
<a href="#a-note-about-nil-values">A note about nil values</a>
</p>
</li>
</ol>
</li>
<li>
<p>
<a href="#kbresultset">KBResultSet</a>
</p>
<ol class="olist2">
<li>
<p>
<a href="#sorting-a-result-set">Sorting a result set</a>
</p>
</li>
<li>
<p>
<a href="#to-report">Producing a report from a result set</a>
</p>
</li>
<li>
<p>
<a href="#crosstabs">CrossTabs or Pivot Tables or Column Arrays (i.e. I don't know what to call them!)</a>
</p>
</li>
</ol>
</li>
<li>
<p>
<a href="#how-to-update-records">How to update records</a>
</p>
<ol class="olist2">
<li>
<p>
<a href="#update-method">The update method</a>
</p>
</li>
<li>
<p>
<a href="#set-method">The set method</a>
</p>
</li>
<li>
<p>
<a href="#update-all-method">The update_all method</a>
</p>
</li>
</ol>
</li>
<li>
<p>
<a href="#how-to-delete-records">How to delete records</a>
</p>
<ol class="olist2">
<li>
<p>
<a href="#delete-method">The delete method</a>
</p>
</li>
<li>
<p>
<a href="#clear-method">The clear method</a>
</p>
</li>
</ol>
</li>
<li>
<p>
<a href="#pack-method">The pack method</a>
</p>
</li>
<li>
<p>
<a href="#memo-and-blob-fields">Memo and Blob Fields</a>
</p>
</li>
<li>
<p>
<a href="#misc-kirbybase-methods">Miscellaneous KirbyBase methods</a>
</p>
<ol class="olist2">
<li>
<p>
<a href="#drop-table">KirbyBase#drop_table</a>
</p>
</li>
<li>
<p>
<a href="#tables">KirbyBase#tables</a>
</p>
</li>
<li>
<p>
<a href="#table-exists">KirbyBase#table_exists?</a>
</p>
</li>
<li>
<p>
<a href="#rename-table">KirbyBase#rename_table</a>
</p>
</li>
</ol>
</li>
<li>
<p>
<a href="#misc-kbtable-methods">Miscellaneous KBTable methods</a>
</p>
<ol class="olist2">
<li>
<p>
<a href="#update-by-recno">KBTable#[]=</a>
</p>
</li>
<li>
<p>
<a href="#get-by-recno">KBTable#[]</a>
</p>
</li>
<li>
<p>
<a href="#field-names">KBTable#field_names</a>
</p>
</li>
<li>
<p>
<a href="#field-types">KBTable#field_types</a>
</p>
</li>
<li>
<p>
<a href="#total-recs">KBTable#total_recs</a>
</p>
</li>
<li>
<p>
<a href="#import-csv">KBTable#import_csv</a>
</p>
</li>
<li>
<p>
<a href="#add-column">KBTable#add_column</a>
</p>
</li>
<li>
<p>
<a href="#drop-column">KBTable#drop_column</a>
</p>
</li>
<li>
<p>
<a href="#rename-column">KBTable#rename_column</a>
</p>
</li>
<li>
<p>
<a href="#change-column-type">KBTable#change_column_type</a>
</p>
</li>
<li>
<p>
<a href="#change-column-default-value">KBTable#change_column_defaul_value</a>
</p>
</li>
<li>
<p>
<a href="#change-column-required">KBTable#change_column_required</a>
</p>
</li>
<li>
<p>
<a href="#add-index">KBTable#add_index</a>
</p>
</li>
<li>
<p>
<a href="#drop-index">KBTable#drop_index</a>
</p>
</li>
</ol>
</li>
<li>
<p>
<a href="#special-characters-in-data">Special characters in data</a>
</p>
</li>
<li>
<p>
<a href="#table-structure">Table Structure</a>
</p>
</li>
<li>
<p>
<a href="#server-notes">Server Notes</a>
</p>
</li>
<li>
<p>
<a href="#tips-on-improving-performance">Tips on improving performance</a>
</p>
</li>
<li>
<p>
<a href="#single-user-diagram">Single-user memory space diagram</a>
</p>
</li>
<li>
<p>
<a href="#client-server-diagram">Client/Server memory space diagram</a>
</p>
</li>
<li>
<p>
<a href="#license">License</a>
</p>
</li>
<li>
<p>
<a href="#credits">Credits</a>
</p>
</li>
</ol>
</div></div>
</div>
<h2><a id="introduction"></a>Introduction</h2>
<div class="sectionbody">
<p>KirbyBase is a simple, pure-Ruby, flat-file database management system.
Some of its features include:</p>
<ul>
<li>
<p>
Since KirbyBase is written in Ruby, it runs anywhere that Ruby runs. It
is easy to distribute, since the entire DBMS is in one (approx. 100k) code
file.
</p>
</li>
<li>
<p>
All data is kept in plain-text, delimited files that can be edited by
hand. This gives you the ability to make changes by just opening the file
up in a text editor, or you can use another programming language to read the
file in and do things with it.
</p>
</li>
<li>
<p>
It can be used as an embedded database or in a client/server, multi-user
mode. To switch from one mode to the other, you only have to change one
line in your program. Included in the distribution is a sample database
server script using DRb.
</p>
</li>
<li>
<p>
Tables are kept on disk during use and accessed from disk when selecting,
updating, inserting, and deleting records. Changes to a table are written
immediately to disk. KirbyBase is not an "in-memory" database. Once you
update the database in your program, you can be assured that the change has
been saved to disk. The chance of lost data due to power interruptions, or
disk crashes is much reduced. Also, since the entire database does not have
to reside in memory at once, KirbyBase should run adequately on a
memory-constrained system.
</p>
</li>
<li>
<p>
You can specify the type of data that each field will hold. The available
data types are: String, Integer, Float, Boolean, Time, Date, DateTime, Memo,
Blob, and YAML.
</p>
</li>
<li>
<p>
The query syntax is very "rubyish". Instead of having to use another
language like SQL, you can express your query using Ruby code blocks.
</p>
</li>
<li>
<p>
All inserted records have an auto-incrementing primary key that is
guaranteed to uniquely identify the record throughout its lifetime.
</p>
</li>
<li>
<p>
You can specify that the result set be sorted on multiple fields, each
one either ascending or descending.
</p>
</li>
<li>
<p>
You can specify that certain fields be indexed. Using an index in a select
query can greatly improve performance on large tables (I've seen 10x speed
improvements). Index maintenance is completely handled by KirbyBase.
</p>
</li>
<li>
<p>
You can specify that a field has a "lookup table". Whenever that field is
accessed, the corresponding record from the lookup table is automatically
available.
</p>
</li>
<li>
<p>
You can specify one-to-many links between tables, somewhat analogous to a
"join" in SQL.
</p>
</li>
<li>
<p>
You can create calculated fields that are computed at runtime.
</p>
</li>
<li>
<p>
It is fairly fast, comparing favorably to SQLite.
</p>
</li>
</ul>
<p>In meeting your DBMS needs, KirbyBase fits in somewhere between plain
text files and small SQL database management systems like SQLite and
MySQL.</p>
<div class="sidebarblock">
<div class="sidebar-content">
<div class="sidebar-title">Drop me a line!</div>
<p>If you find a use for KirbyBase, please send me an email telling how you
use it, whether it is ok for me to mention your application on the
"KirbyBase Applications" webpage, and possibly a link to your application's
webpage (if you have one).</p>
</div></div>
</div>
<h2><a id="connecting-to-a-database"></a>Connecting to a database</h2>
<div class="sectionbody">
<p>To use Kirbybase, you first need to require the module:</p>
<div class="listingblock">
<div class="content">
<pre><tt>require 'kirbybase'</tt></pre>
</div></div>
<p>Then create an instance:</p>
<div class="listingblock">
<div class="content">
<pre><tt>db = KirbyBase.new</tt></pre>
</div></div>
<p>By default, the instance is a local connection using the same memory space
as your application. To specify a client/server connection, it would look
like this:</p>
<div class="listingblock">
<div class="content">
<pre><tt>db = KirbyBase.new(:client, 'localhost', 44444)</tt></pre>
</div></div>
<p>Of course, you would substitute your server's ip address and port number.</p>
<p>To specify a different location (other than the current directory) for the
database files, you need to pass the location as an argument, like so:</p>
<div class="listingblock">
<div class="content">
<pre><tt>db = KirbyBase.new(:local, nil, nil, './data')</tt></pre>
</div></div>
<p>KirbyBase treats every file in the specified directory that has the proper
extension as a database table. The default extension is ".tbl". To specify
a different extension, pass this as an argument, like so:</p>
<div class="listingblock">
<div class="content">
<pre><tt>db = KirbyBase.new(:local, nil, nil, './', '.dat')</tt></pre>
</div></div>
<p>To specify a different location other than the current directory for any
memo/blob files, you need to pass the location as an argument, like so:</p>
<div class="listingblock">
<div class="content">
<pre><tt>db = KirbyBase.new(:local, nil, nil, './', '.tbl', './memos')</tt></pre>
</div></div>
<p>If you don't want KirbyBase to spend time initially creating all of the
indexes for the tables in the database, you can pass true as the
delay_index_creation argument:</p>
<div class="listingblock">
<div class="content">
<pre><tt>db = KirbyBase.new(:local, nil, nil, './', '.tbl', './', true)</tt></pre>
</div></div>
<p>KirbyBase will skip initial index creation and will create a table's
indexes when the table is first referenced.</p>
<p>You can also specify the arguments via a code block. So, if you don't want
to have to specify a bunch of arguments just to get to the one you want,
put it in a code block attached to the method call. You could re-code the
previous example like so:</p>
<div class="listingblock">
<div class="content">
<pre><tt>db = KirbyBase.new { |d| d.delay_index_creation = true }</tt></pre>
</div></div>
</div>
<h2><a id="creating-a-new-table"></a>Creating a new table</h2>
<div class="sectionbody">
<p>To create a new table, you specify the table name, and a name and type for
each column. For example, to create a table containing information on World
War II planes:</p>
<div class="listingblock">
<div class="content">
<pre><tt>plane_tbl = db.create_table(:plane, :name, :String, :country, :String,
:role, :String, :speed, :Integer, :range, :Integer, :began_service, :Date,
:still_flying, :Boolean)</tt></pre>
</div></div>
<div class="sidebarblock">
<a id="field-types"></a>
<div class="sidebar-content">
<div class="sidebar-title">KirbyBase Field Types</div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/tip.png" alt="Tip" />
</td>
<td class="content">:String, :Integer, :Float, :Boolean(true/false), :Time, :Date,
:DateTime, :Memo, :Blob, and :YAML.</td>
</tr></table>
</div>
</div></div>
<div class="sidebarblock">
<a id="recno"></a>
<div class="sidebar-content">
<div class="sidebar-title">The recno field</div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/important.png" alt="Important" />
</td>
<td class="content">KirbyBase will automatically create a primary key field, called
recno, with a type of :Integer, for each table. This field will be
auto-incremented each time a record is inserted. You can use this field in
select, update, and delete queries, but you can't modify this field.</td>
</tr></table>
</div>
</div></div>
<h3><a id="encrypt"></a>Turning on encryption</h3>
<p>You can also specify whether the table should be encrypted. This will save
the table to disk encrypted (more like obfuscated) using a Vignere Cipher.
This is similar to rot13, but it uses a key to determine character
substitution. Still, this encryption will only stymie the most casual
of hackers. Do not rely on it to keep sensitive data safe! You specify
encryption by using a code block attached to #create_table:</p>
<div class="listingblock">
<div class="content">
<pre><tt>plane_tbl = db.create_table(:plane, :name, :String...) do |t|
t.encrypt = true
end</tt></pre>
</div></div>
<h3><a id="record-class"></a>Specifying a custom record class</h3>
<p>You can also specify that you want records of the table to be returned to
you as instances of a class. To do this, simply define a class before you
call #create_table. This class needs to have an accessor for each fieldname
in the table.</p>
<p>If this class has a class method, called #kb_create, KirbyBase, when
creating each record of the result set, will call that method and pass it
the field values of the result record. #kb_create will need to handle
creating an instance of the record class itself.</p>
<p>Here is an example of #kb_create in action:</p>
<div class="listingblock">
<div class="content">
<pre><tt>class Foobar
attr_accessor(:recno, :name, :country, :role, :speed, :range,
:began_service, :still_flying, :alpha, :beta)
def Foobar.kb_create(recno, name, country, role, speed, range,
began_service, still_flying)
name ||= 'No Name!'
speed ||= 0
began_service ||= Date.today
Foobar.new do |x|
x.recno = recno
x.name = name
x.country = country
x.role = role
x.speed = speed
x.range = range
x.began_service = began_service
x.still_flying = still_flying
end
end
def initialize(&block)
instance_eval(&block)
end
end</tt></pre>
</div></div>
<p>Pass this class to #create_table in an attached code block, like so:</p>
<div class="listingblock">
<div class="content">
<pre><tt>plane_tbl = db.create_table(:plane, :name, :String...) do |t|
t.record_class = Foobar
end</tt></pre>
</div></div>
<p>Now, when you call #select, the result set will be made up of instances of
Foobar, instead of the default, which is instances of Struct. This also
works the other way. You can now specify instances of Foobar as input to
the #insert, #update and #set methods. More on those methods below.</p>
<p>If the custom record class does not respond to #kb_create, KirbyBase will
call the class's #new method instead, passing it all of the field values.</p>
<div class="sidebarblock">
<div class="sidebar-content">
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/important.png" alt="Important" />
</td>
<td class="content">The #create_table method will return a reference to a KBTable
instance. This is the only way, besides calling KirbyBase#get_table, that
you can obtain a handle to a KBTable instance. You can not call KBTable#new
directly.</td>
</tr></table>
</div>
</div></div>
<h3>Specifying Advanced Field Type Information</h3>
<p>There are four types of advanced field type information that you can
specify:
Defaults, Requireds, Indexes and Extras (i.e. Lookup, Link_many,
Calculated).</p>
<h4><a id="defaults"></a>Default Field Values</h4>
<p>Normally, when you insert a record, if you don't specify a value for a field
or specify nil as the value, KirbyBase stores this as an empty string
(i.e. "") in the table's physical file, and returns it as a nil value when
you do a #select.</p>
<p>However, you can tell KirbyBase that you want a column to have a default
value. Only KBTable#insert is affected by default values. Default values
do not apply to updating a record. So, for inserts, there are two cases
where a default value, if one is specified, will be applied: (1) if nil
is specified for a field's value, and (2) if no value is specified for a
field.</p>
<p>For example, to specify that the category column has a default value, you
would code:</p>
<div class="listingblock">
<div class="content">
<pre><tt>db.create_table(:addressbook, :firstname, :String,
:lastname, :String, :phone_no, :String,
:category, {:DataType=>:String, :Default=>'Business'})</tt></pre>
</div></div>
<p>Notice that, since we are specifying advanced field type information, we
cannot just simply say :String for the second half of the field definition.
Instead, we have to pass a hash, with a :DataType item set to :String.
Next, because we are specifying a default value, we have to include a hash
item called :Default with its value set to whatever we want the default to
be. The default value must be of a type that is valid for the column.</p>
<h4><a id="requireds"></a>Required Fields</h4>
<p>Normally, when you insert or update a record, if you don't specify a value
for a field or specify nil as the value, KirbyBase stores this as an empty
string (i.e. "") in the table's physical file, and returns it as a nil
value when you do a #select.</p>
<p>However, you can tell KirbyBase that you want a column to be required
(i.e. you must supply a value and it can't be nil). When a record is
inserted or updated, an exception will be raised for any required field
that has not been given a value or been given a nil value.</p>
<p>For example, to specify that the category column is required, you would
code:</p>
<div class="listingblock">
<div class="content">
<pre><tt>db.create_table(:addressbook, :firstname, :String,
:lastname, :String, :phone_no, :String,
:category, {:DataType=>:String, :Required=>true})</tt></pre>
</div></div>
<p>Notice that, since we are specifying advanced field type information, we
cannot just simply say :String for the second half of the field definition.
Instead, we have to pass a hash, with a :DataType item set to :String.
Next, because we are specifying that a column is required, we have to
include a hash item called :Required with its value set to true.</p>
<h4><a id="creating-indexes"></a>Indexes</h4>
<p>Indexes are in-memory arrays that have an entry that corresponds to each
table record, but just holds the field values specified when the index was
created, plus the :recno of the actual table record. Because index arrays
are smaller than the actual table and because they are in-memory instead of
on-disk, using an index in a select query is usually much faster than
selecting against the table itself, especially when the table is quite
large.</p>
<p>To specify that an index is to be created, you need to tell KirbyBase which
fields are to be included in a particular index. You can have up to 5
indexes per table. Indexes can either contain single or multiple fields.
For example, to create an index on firstname and lastname for a table called
:addressbook, you would code:</p>
<div class="listingblock">
<div class="content">
<pre><tt>db.create_table(:addressbook, :firstname, {:DataType=>:String, :Index=>1},
:lastname, {:DataType=>:String, :Index=>1},
:phone_no, :String)</tt></pre>
</div></div>
<p>Notice that, since we are specifying advanced field type information, we
cannot just simply say :String for the second half of the field definition.
Instead, we have to pass a hash, with a :DataType item set to :String.
Next, because we are creating an index, we have to include a hash item
called :Index with its value set from 1 to 5. For compound indexes, like
the one in the above example, we need to make sure that all fields in the
index have the same number. To have another index for a table, just make
sure you increment the index number. So, for example, if we wanted to have
another index for the :addressbook table on the field phone number, we would
code:</p>
<div class="listingblock">
<div class="content">
<pre><tt>db.create_table(:addressbook, :firstname, {:DataType=>:String, :Index=>1},
:lastname, {:DataType=>:String, :Index=>1},
:phone_no, {:DataType=>:String, :Index=>2})</tt></pre>
</div></div>
<p>Notice how we just increment the index number to 2 for the :phone_no index.
Since there are no other fields with the same index number, this will create
an index with just the :phone_no field in it. You will see how to use
indexes in your select queries later.</p>
<div class="sidebarblock">
<a id="a-note-about-indexes"></a>
<div class="sidebar-content">
<div class="sidebar-title">A note about indexes</div>
<p>When KirbyBase initializes, it creates an instance for each table in
the database. It also creates each index array for each indexed field in
each table. So, if there are several large tables that have indexed fields,
this database initialization process could take several seconds. I decided
to have it operate this way, because I thought that it made more sense to
have a user application wait once upon initialization, rather than have it
wait the first time a table is referenced. A user is more used to waiting