KirbyBase 2.6 → 2.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +65 -67
- data/bin/kbserver.rb +18 -18
- data/changes.txt +144 -137
- data/examples/aaa_try_this_first/kbtest.rb +237 -237
- data/examples/add_column_test/add_column_test.rb +27 -27
- data/examples/calculated_field_test/calculated_field_test.rb +51 -51
- data/examples/change_column_type_test/change_column_type_test.rb +25 -25
- data/examples/column_required_test/column_required_test.rb +44 -44
- data/examples/crosstab_test/crosstab_test.rb +100 -100
- data/examples/csv_import_test/csv_import_test.rb +31 -31
- data/examples/csv_import_test/plane.csv +11 -11
- data/examples/default_value_test/default_value_test.rb +54 -54
- data/examples/drop_column_test/drop_column_test.rb +24 -24
- data/examples/indexes_test/add_index_test.rb +46 -46
- data/examples/indexes_test/drop_index_test.rb +65 -65
- data/examples/indexes_test/index_test.rb +94 -94
- data/examples/kbserver_as_win32_service/kbserver_daemon.rb +47 -47
- data/examples/kbserver_as_win32_service/kbserverctl.rb +75 -75
- data/examples/link_many_test/link_many_test.rb +70 -70
- data/examples/lookup_field_test/lookup_field_test.rb +55 -55
- data/examples/lookup_field_test/lookup_field_test_2.rb +62 -62
- data/examples/lookup_field_test/the_hal_fulton_feature_test.rb +69 -69
- data/examples/many_to_many_test/many_to_many_test.rb +65 -65
- data/examples/memo_test/memo_test.rb +74 -74
- data/examples/record_class_test/record_class_test.rb +77 -77
- data/examples/record_class_test/record_class_test2.rb +31 -31
- data/examples/rename_column_test/rename_column_test.rb +45 -45
- data/examples/rename_table_test/rename_table_test.rb +38 -38
- data/examples/yaml_field_test/yaml_field_test.rb +47 -47
- data/kirbybaserubymanual.html +2324 -2324
- data/lib/kirbybase.rb +3907 -3880
- data/test/tc_local_table.rb +108 -108
- metadata +56 -54
@@ -1,237 +1,237 @@
|
|
1
|
-
#Simple test of KirbyBase.
|
2
|
-
|
3
|
-
require 'kirbybase'
|
4
|
-
require 'date'
|
5
|
-
|
6
|
-
def print_divider(text)
|
7
|
-
puts
|
8
|
-
puts text.center(75, '-')
|
9
|
-
puts
|
10
|
-
end
|
11
|
-
|
12
|
-
#-------------------- Initialize KirbyBase Instance ------------------------
|
13
|
-
# To run local, single-user, uncomment next line.
|
14
|
-
db = KirbyBase.new
|
15
|
-
|
16
|
-
# To run as a client in a multi-user environment, uncomment next line.
|
17
|
-
# Also, make sure kbserver.rb is running.
|
18
|
-
#db = KirbyBase.new do |d|
|
19
|
-
# d.connect_type = :client
|
20
|
-
# d.host = 'localhost'
|
21
|
-
# d.port = 44444
|
22
|
-
#end
|
23
|
-
|
24
|
-
#----------------------- Drop Table Example --------------------------------
|
25
|
-
# If table exists, delete it.
|
26
|
-
db.drop_table(:plane) if db.table_exists?(:plane)
|
27
|
-
|
28
|
-
#----------------------- Create Table Example ------------------------------
|
29
|
-
# Create a table.
|
30
|
-
plane_tbl = db.create_table(:plane, :name, :String, :country, :String,
|
31
|
-
:role, :String, :speed, :Integer, :range, :Integer, :began_service, :Date,
|
32
|
-
:still_flying, :Boolean) { |obj| obj.encrypt = false }
|
33
|
-
|
34
|
-
#----------------------- Insert Record Examples ----------------------------
|
35
|
-
# Four different ways to insert records in KirbyBase.
|
36
|
-
|
37
|
-
# 1) Insert a record using an array for the input values.
|
38
|
-
plane_tbl.insert('FW-190', 'Germany', 'Fighter', 399, 499,
|
39
|
-
Date.new(1942,12,1), false)
|
40
|
-
|
41
|
-
# 2) Insert a record using a hash for the input values.
|
42
|
-
plane_tbl.insert(:name => 'P-51', :country => 'USA',
|
43
|
-
:role => 'Fighter', :speed => 403, :range => 1201,
|
44
|
-
:began_service => Date.new(1943,6,24), :still_flying => true)
|
45
|
-
|
46
|
-
# 3) Insert a record using a Struct for the input values.
|
47
|
-
InputRec = Struct.new(:name, :country, :role, :speed, :range,
|
48
|
-
:began_service, :still_flying)
|
49
|
-
rec = InputRec.new('P-47', 'USA', 'Fighter', 365, 888, Date.new(1943,3,12),
|
50
|
-
false)
|
51
|
-
plane_tbl.insert(rec)
|
52
|
-
|
53
|
-
# 4) Insert a record using a code block for the input values.
|
54
|
-
plane_tbl.insert { |r|
|
55
|
-
r.name = 'B-17'
|
56
|
-
r.country = 'USA'
|
57
|
-
r.role = 'Bomber'
|
58
|
-
r.speed = 315
|
59
|
-
r.range = r.speed * 3
|
60
|
-
r.began_service = Date.new(1937, 5, 1)
|
61
|
-
r.still_flying = true
|
62
|
-
}
|
63
|
-
|
64
|
-
# If a table is already existing and you need to get a reference to it so
|
65
|
-
# that you can insert, select, etc., just do a get_table.
|
66
|
-
plane_tbl_another_reference = db.get_table(:plane)
|
67
|
-
|
68
|
-
# Then, you can use it just like you have been using the reference you got
|
69
|
-
# when you created the table.
|
70
|
-
plane_tbl_another_reference.insert('Typhoon', 'Great Britain',
|
71
|
-
'Fighter-Bomber', 389, 690, Date.new(1944,11,20), false)
|
72
|
-
|
73
|
-
# Insert a bunch more records so we can have some "select" fun below.
|
74
|
-
plane_tbl.insert('Spitfire', 'Great Britain', 'Fighter', 345, 540,
|
75
|
-
Date.new(1939,2,18), true)
|
76
|
-
plane_tbl.insert('Oscar', 'Japan', 'Fighter', 361, 777,
|
77
|
-
Date.new(1943,12,31), false)
|
78
|
-
plane_tbl.insert('ME-109', 'Germany', 'Fighter', 366, 514,
|
79
|
-
Date.new(1936,7,7),true)
|
80
|
-
plane_tbl.insert('JU-88', 'Germany', 'Bomber', 289, 999,
|
81
|
-
Date.new(1937,1,19), false)
|
82
|
-
plane_tbl.insert('P-39', 'USA', 'Fighter', nil, nil,
|
83
|
-
nil, false)
|
84
|
-
plane_tbl.insert('Zero', 'Japan', 'Fighter', 377, 912,
|
85
|
-
Date.new(1937,5,15), true)
|
86
|
-
plane_tbl.insert('B-25', 'USA', '', 320, 1340, Date.new(1940,4,4), true)
|
87
|
-
|
88
|
-
#--------------------- Update Examples -------------------------------------
|
89
|
-
# Four different ways to update existing data in KirbyBase. In all three
|
90
|
-
# instances, you still need a code block attached to the update method in
|
91
|
-
# order to select records that will be updated.
|
92
|
-
|
93
|
-
# 1) Update record using a Hash, Struct, or an Array.
|
94
|
-
plane_tbl.update(:speed => 405, :range => 1210) { |r| r.name == 'P-51' }
|
95
|
-
|
96
|
-
# 2) Update record using a Hash, Struct, or an Array, via the set
|
97
|
-
# command.
|
98
|
-
plane_tbl.update {|r| r.name == 'P-51'}.set(:speed => 405, :range => 1210)
|
99
|
-
|
100
|
-
# 3) Update record by treating table as if it were a Hash and the keys were
|
101
|
-
# recno's.
|
102
|
-
plane_tbl[2] = {:speed => 405, :range => 1210}
|
103
|
-
|
104
|
-
# 4) Update record using a code block, via the set command. Notice how you
|
105
|
-
# have access to the current record's values within the block.
|
106
|
-
plane_tbl.update {|r| r.name == 'P-51'}.set {|r|
|
107
|
-
r.speed = r.speed+7
|
108
|
-
r.range = r.range-2
|
109
|
-
}
|
110
|
-
|
111
|
-
#--------------------- Delete Examples -------------------------------------
|
112
|
-
# Delete 'FW-190' record.
|
113
|
-
plane_tbl.delete { |r| r.name == 'FW-190' }
|
114
|
-
|
115
|
-
#---------------------- Select Example 0 -----------------------------------
|
116
|
-
print_divider('Select Example 0')
|
117
|
-
# Select all records, including all fields in result set.
|
118
|
-
plane_tbl.select.each { |r|
|
119
|
-
puts(('%s ' * r.members.size) % r.to_a)
|
120
|
-
}
|
121
|
-
|
122
|
-
#-------------------------- Select Example 1 -------------------------------
|
123
|
-
print_divider('Select Example 1')
|
124
|
-
# Select all Japanese planes. Include just name and speed in the result.
|
125
|
-
plane_tbl.select(:name, :speed) { |r| r.country == 'Japan' }.each { |r|
|
126
|
-
puts '%s %s' % [r.name, r.speed]
|
127
|
-
}
|
128
|
-
|
129
|
-
#-------------------------- Select Example 2 -------------------------------
|
130
|
-
print_divider('Select Example 2')
|
131
|
-
# Select all US planes with a speed greater than 350mph. Include just name
|
132
|
-
# and speed in result set.
|
133
|
-
plane_tbl.select(:name, :speed) { |r|
|
134
|
-
r.country == 'USA' and r.speed > 350
|
135
|
-
}.each { |r| puts '%s %s' % [r.name, r.speed] }
|
136
|
-
|
137
|
-
#-------------------------- Select Example 3 -------------------------------
|
138
|
-
print_divider('Select Example 3')
|
139
|
-
# Select all Axis fighters.
|
140
|
-
plane_tbl.select { |r|
|
141
|
-
(r.country == 'Germany' or r.country == 'Japan') and r.role == 'Fighter'
|
142
|
-
}.each { |r| puts r }
|
143
|
-
|
144
|
-
#-------------------------- Select Example 4 -------------------------------
|
145
|
-
print_divider('Select Example 4')
|
146
|
-
# Same query as above, but let's use regular expressions instead of an 'or'.
|
147
|
-
plane_tbl.select { |r|
|
148
|
-
r.country =~ /Germany|Japan/ and r.role == 'Fighter'
|
149
|
-
}.each { |r| puts r }
|
150
|
-
|
151
|
-
#-------------------------- Select Example 5 -------------------------------
|
152
|
-
print_divider('Select Example 5')
|
153
|
-
# Select all Bombers (but not Fighter-Bombers) and return only their name
|
154
|
-
# and country. This is also an example of how to get a reference to an
|
155
|
-
# existing table as opposed to already having a reference to one via the
|
156
|
-
# table_create method.
|
157
|
-
match_role = /^Bomber/
|
158
|
-
plane_tbl2 = db.get_table(:plane)
|
159
|
-
plane_tbl2.select(:name, :country) { |r| r.role =~ match_role }.each { |r|
|
160
|
-
puts '%s %s' % r.to_a
|
161
|
-
}
|
162
|
-
|
163
|
-
#-------------------------- Select Example 6 -------------------------------
|
164
|
-
print_divider('Select Example 6')
|
165
|
-
# Select all planes. Include just name, country, and speed in result set.
|
166
|
-
# Sort result set by country (ascending) then name (ascending).
|
167
|
-
plane_tbl.select(:name, :country, :speed).sort(:country,
|
168
|
-
:name).each { |r| puts "%s %s %d" % r.to_a }
|
169
|
-
|
170
|
-
#-------------------------- Select Example 7 -------------------------------
|
171
|
-
print_divider('Select Example 7')
|
172
|
-
# Select all planes. Include just name, country, and speed in result set.
|
173
|
-
# Return result set as a nicely formatted report, sorted by
|
174
|
-
# country (ascending) then speed (descending).
|
175
|
-
puts plane_tbl.select(:name, :country, :speed).sort(+:country,
|
176
|
-
-:speed).to_report
|
177
|
-
|
178
|
-
#-------------------------- Select Example 8 -------------------------------
|
179
|
-
print_divider('Select Example 8')
|
180
|
-
# Select planes that are included in nameArray.
|
181
|
-
nameArray = ['P-51', 'Spitfire', 'Zero']
|
182
|
-
plane_tbl.select { |r| nameArray.include?(r.name) }.each { |r| puts r }
|
183
|
-
|
184
|
-
#-------------------------- Select Example 9 -------------------------------
|
185
|
-
print_divider('Select Example 9')
|
186
|
-
# You can select a record as if the table is a hash and it's keys are the
|
187
|
-
# recno's.
|
188
|
-
# Select the record that has a recno of 5.
|
189
|
-
puts plane_tbl[5].name
|
190
|
-
|
191
|
-
#-------------------------- Select Example 10 -------------------------------
|
192
|
-
print_divider('Select Example 10')
|
193
|
-
# You can even have a select within the code block of another select. Here
|
194
|
-
# we are selecting all records that are from the same country as the Zero.
|
195
|
-
puts plane_tbl.select { |r|
|
196
|
-
r.country == plane_tbl.select { |x| x.name == 'Zero' }.first.country
|
197
|
-
}
|
198
|
-
|
199
|
-
#-------------------------- Select Example 11 -------------------------------
|
200
|
-
print_divider('Select Example 11')
|
201
|
-
# Select all planes.
|
202
|
-
plane_tbl.select.each { |r| puts r }
|
203
|
-
|
204
|
-
#-------------------------- Select Example 12 -------------------------------
|
205
|
-
print_divider('Select Example 12')
|
206
|
-
# Select all planes with a speed of nil.
|
207
|
-
|
208
|
-
#**************** Note: This example also demonstrates the change from
|
209
|
-
# nil to kb_nil for KirbyBase's internal representation of a nil value. You
|
210
|
-
# should only encounter this different if you have to check for nil in your
|
211
|
-
# query, as this example does. Other than that, everything else should
|
212
|
-
# be transparent, since KirbyBase converts a kb_nil back into a nil when it
|
213
|
-
# returns a query's result set.
|
214
|
-
#***************
|
215
|
-
plane_tbl.select { |r| r.speed.kb_nil? }.each { |r| puts r }
|
216
|
-
|
217
|
-
#-------------------------- Select Example 13 -------------------------------
|
218
|
-
print_divider('Select Example 13')
|
219
|
-
# Same thing, but in a slightly different way.
|
220
|
-
|
221
|
-
#**************** Note: This example also demonstrates the change from
|
222
|
-
# nil to kb_nil for KirbyBase's internal representation of a nil value. You
|
223
|
-
# should only encounter this different if you have to check for nil in your
|
224
|
-
# query, as this example does. Other than that, everything else should
|
225
|
-
# be transparent, since KirbyBase converts a kb_nil back into a nil when it
|
226
|
-
# returns a query's result set.
|
227
|
-
#***************
|
228
|
-
plane_tbl.select { |r| r.speed == kb_nil }.each { |r| puts r }
|
229
|
-
|
230
|
-
#-------------------------- Misc. Methods Examples -------------------------
|
231
|
-
print_divider('Misc. Methods Examples')
|
232
|
-
puts 'Total # of records in table: %d' % plane_tbl.total_recs
|
233
|
-
puts
|
234
|
-
puts 'Fields for plane.tbl:'
|
235
|
-
plane_tbl.field_names.zip(plane_tbl.field_types).each { |r|
|
236
|
-
print r[0].to_s.ljust(15), r[1].to_s, "\n"
|
237
|
-
}
|
1
|
+
#Simple test of KirbyBase.
|
2
|
+
|
3
|
+
require 'kirbybase'
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
def print_divider(text)
|
7
|
+
puts
|
8
|
+
puts text.center(75, '-')
|
9
|
+
puts
|
10
|
+
end
|
11
|
+
|
12
|
+
#-------------------- Initialize KirbyBase Instance ------------------------
|
13
|
+
# To run local, single-user, uncomment next line.
|
14
|
+
db = KirbyBase.new
|
15
|
+
|
16
|
+
# To run as a client in a multi-user environment, uncomment next line.
|
17
|
+
# Also, make sure kbserver.rb is running.
|
18
|
+
#db = KirbyBase.new do |d|
|
19
|
+
# d.connect_type = :client
|
20
|
+
# d.host = 'localhost'
|
21
|
+
# d.port = 44444
|
22
|
+
#end
|
23
|
+
|
24
|
+
#----------------------- Drop Table Example --------------------------------
|
25
|
+
# If table exists, delete it.
|
26
|
+
db.drop_table(:plane) if db.table_exists?(:plane)
|
27
|
+
|
28
|
+
#----------------------- Create Table Example ------------------------------
|
29
|
+
# Create a table.
|
30
|
+
plane_tbl = db.create_table(:plane, :name, :String, :country, :String,
|
31
|
+
:role, :String, :speed, :Integer, :range, :Integer, :began_service, :Date,
|
32
|
+
:still_flying, :Boolean) { |obj| obj.encrypt = false }
|
33
|
+
|
34
|
+
#----------------------- Insert Record Examples ----------------------------
|
35
|
+
# Four different ways to insert records in KirbyBase.
|
36
|
+
|
37
|
+
# 1) Insert a record using an array for the input values.
|
38
|
+
plane_tbl.insert('FW-190', 'Germany', 'Fighter', 399, 499,
|
39
|
+
Date.new(1942,12,1), false)
|
40
|
+
|
41
|
+
# 2) Insert a record using a hash for the input values.
|
42
|
+
plane_tbl.insert(:name => 'P-51', :country => 'USA',
|
43
|
+
:role => 'Fighter', :speed => 403, :range => 1201,
|
44
|
+
:began_service => Date.new(1943,6,24), :still_flying => true)
|
45
|
+
|
46
|
+
# 3) Insert a record using a Struct for the input values.
|
47
|
+
InputRec = Struct.new(:name, :country, :role, :speed, :range,
|
48
|
+
:began_service, :still_flying)
|
49
|
+
rec = InputRec.new('P-47', 'USA', 'Fighter', 365, 888, Date.new(1943,3,12),
|
50
|
+
false)
|
51
|
+
plane_tbl.insert(rec)
|
52
|
+
|
53
|
+
# 4) Insert a record using a code block for the input values.
|
54
|
+
plane_tbl.insert { |r|
|
55
|
+
r.name = 'B-17'
|
56
|
+
r.country = 'USA'
|
57
|
+
r.role = 'Bomber'
|
58
|
+
r.speed = 315
|
59
|
+
r.range = r.speed * 3
|
60
|
+
r.began_service = Date.new(1937, 5, 1)
|
61
|
+
r.still_flying = true
|
62
|
+
}
|
63
|
+
|
64
|
+
# If a table is already existing and you need to get a reference to it so
|
65
|
+
# that you can insert, select, etc., just do a get_table.
|
66
|
+
plane_tbl_another_reference = db.get_table(:plane)
|
67
|
+
|
68
|
+
# Then, you can use it just like you have been using the reference you got
|
69
|
+
# when you created the table.
|
70
|
+
plane_tbl_another_reference.insert('Typhoon', 'Great Britain',
|
71
|
+
'Fighter-Bomber', 389, 690, Date.new(1944,11,20), false)
|
72
|
+
|
73
|
+
# Insert a bunch more records so we can have some "select" fun below.
|
74
|
+
plane_tbl.insert('Spitfire', 'Great Britain', 'Fighter', 345, 540,
|
75
|
+
Date.new(1939,2,18), true)
|
76
|
+
plane_tbl.insert('Oscar', 'Japan', 'Fighter', 361, 777,
|
77
|
+
Date.new(1943,12,31), false)
|
78
|
+
plane_tbl.insert('ME-109', 'Germany', 'Fighter', 366, 514,
|
79
|
+
Date.new(1936,7,7),true)
|
80
|
+
plane_tbl.insert('JU-88', 'Germany', 'Bomber', 289, 999,
|
81
|
+
Date.new(1937,1,19), false)
|
82
|
+
plane_tbl.insert('P-39', 'USA', 'Fighter', nil, nil,
|
83
|
+
nil, false)
|
84
|
+
plane_tbl.insert('Zero', 'Japan', 'Fighter', 377, 912,
|
85
|
+
Date.new(1937,5,15), true)
|
86
|
+
plane_tbl.insert('B-25', 'USA', '', 320, 1340, Date.new(1940,4,4), true)
|
87
|
+
|
88
|
+
#--------------------- Update Examples -------------------------------------
|
89
|
+
# Four different ways to update existing data in KirbyBase. In all three
|
90
|
+
# instances, you still need a code block attached to the update method in
|
91
|
+
# order to select records that will be updated.
|
92
|
+
|
93
|
+
# 1) Update record using a Hash, Struct, or an Array.
|
94
|
+
plane_tbl.update(:speed => 405, :range => 1210) { |r| r.name == 'P-51' }
|
95
|
+
|
96
|
+
# 2) Update record using a Hash, Struct, or an Array, via the set
|
97
|
+
# command.
|
98
|
+
plane_tbl.update {|r| r.name == 'P-51'}.set(:speed => 405, :range => 1210)
|
99
|
+
|
100
|
+
# 3) Update record by treating table as if it were a Hash and the keys were
|
101
|
+
# recno's.
|
102
|
+
plane_tbl[2] = {:speed => 405, :range => 1210}
|
103
|
+
|
104
|
+
# 4) Update record using a code block, via the set command. Notice how you
|
105
|
+
# have access to the current record's values within the block.
|
106
|
+
plane_tbl.update {|r| r.name == 'P-51'}.set {|r|
|
107
|
+
r.speed = r.speed+7
|
108
|
+
r.range = r.range-2
|
109
|
+
}
|
110
|
+
|
111
|
+
#--------------------- Delete Examples -------------------------------------
|
112
|
+
# Delete 'FW-190' record.
|
113
|
+
plane_tbl.delete { |r| r.name == 'FW-190' }
|
114
|
+
|
115
|
+
#---------------------- Select Example 0 -----------------------------------
|
116
|
+
print_divider('Select Example 0')
|
117
|
+
# Select all records, including all fields in result set.
|
118
|
+
plane_tbl.select.each { |r|
|
119
|
+
puts(('%s ' * r.members.size) % r.to_a)
|
120
|
+
}
|
121
|
+
|
122
|
+
#-------------------------- Select Example 1 -------------------------------
|
123
|
+
print_divider('Select Example 1')
|
124
|
+
# Select all Japanese planes. Include just name and speed in the result.
|
125
|
+
plane_tbl.select(:name, :speed) { |r| r.country == 'Japan' }.each { |r|
|
126
|
+
puts '%s %s' % [r.name, r.speed]
|
127
|
+
}
|
128
|
+
|
129
|
+
#-------------------------- Select Example 2 -------------------------------
|
130
|
+
print_divider('Select Example 2')
|
131
|
+
# Select all US planes with a speed greater than 350mph. Include just name
|
132
|
+
# and speed in result set.
|
133
|
+
plane_tbl.select(:name, :speed) { |r|
|
134
|
+
r.country == 'USA' and r.speed > 350
|
135
|
+
}.each { |r| puts '%s %s' % [r.name, r.speed] }
|
136
|
+
|
137
|
+
#-------------------------- Select Example 3 -------------------------------
|
138
|
+
print_divider('Select Example 3')
|
139
|
+
# Select all Axis fighters.
|
140
|
+
plane_tbl.select { |r|
|
141
|
+
(r.country == 'Germany' or r.country == 'Japan') and r.role == 'Fighter'
|
142
|
+
}.each { |r| puts r }
|
143
|
+
|
144
|
+
#-------------------------- Select Example 4 -------------------------------
|
145
|
+
print_divider('Select Example 4')
|
146
|
+
# Same query as above, but let's use regular expressions instead of an 'or'.
|
147
|
+
plane_tbl.select { |r|
|
148
|
+
r.country =~ /Germany|Japan/ and r.role == 'Fighter'
|
149
|
+
}.each { |r| puts r }
|
150
|
+
|
151
|
+
#-------------------------- Select Example 5 -------------------------------
|
152
|
+
print_divider('Select Example 5')
|
153
|
+
# Select all Bombers (but not Fighter-Bombers) and return only their name
|
154
|
+
# and country. This is also an example of how to get a reference to an
|
155
|
+
# existing table as opposed to already having a reference to one via the
|
156
|
+
# table_create method.
|
157
|
+
match_role = /^Bomber/
|
158
|
+
plane_tbl2 = db.get_table(:plane)
|
159
|
+
plane_tbl2.select(:name, :country) { |r| r.role =~ match_role }.each { |r|
|
160
|
+
puts '%s %s' % r.to_a
|
161
|
+
}
|
162
|
+
|
163
|
+
#-------------------------- Select Example 6 -------------------------------
|
164
|
+
print_divider('Select Example 6')
|
165
|
+
# Select all planes. Include just name, country, and speed in result set.
|
166
|
+
# Sort result set by country (ascending) then name (ascending).
|
167
|
+
plane_tbl.select(:name, :country, :speed).sort(:country,
|
168
|
+
:name).each { |r| puts "%s %s %d" % r.to_a }
|
169
|
+
|
170
|
+
#-------------------------- Select Example 7 -------------------------------
|
171
|
+
print_divider('Select Example 7')
|
172
|
+
# Select all planes. Include just name, country, and speed in result set.
|
173
|
+
# Return result set as a nicely formatted report, sorted by
|
174
|
+
# country (ascending) then speed (descending).
|
175
|
+
puts plane_tbl.select(:name, :country, :speed).sort(+:country,
|
176
|
+
-:speed).to_report
|
177
|
+
|
178
|
+
#-------------------------- Select Example 8 -------------------------------
|
179
|
+
print_divider('Select Example 8')
|
180
|
+
# Select planes that are included in nameArray.
|
181
|
+
nameArray = ['P-51', 'Spitfire', 'Zero']
|
182
|
+
plane_tbl.select { |r| nameArray.include?(r.name) }.each { |r| puts r }
|
183
|
+
|
184
|
+
#-------------------------- Select Example 9 -------------------------------
|
185
|
+
print_divider('Select Example 9')
|
186
|
+
# You can select a record as if the table is a hash and it's keys are the
|
187
|
+
# recno's.
|
188
|
+
# Select the record that has a recno of 5.
|
189
|
+
puts plane_tbl[5].name
|
190
|
+
|
191
|
+
#-------------------------- Select Example 10 -------------------------------
|
192
|
+
print_divider('Select Example 10')
|
193
|
+
# You can even have a select within the code block of another select. Here
|
194
|
+
# we are selecting all records that are from the same country as the Zero.
|
195
|
+
puts plane_tbl.select { |r|
|
196
|
+
r.country == plane_tbl.select { |x| x.name == 'Zero' }.first.country
|
197
|
+
}
|
198
|
+
|
199
|
+
#-------------------------- Select Example 11 -------------------------------
|
200
|
+
print_divider('Select Example 11')
|
201
|
+
# Select all planes.
|
202
|
+
plane_tbl.select.each { |r| puts r }
|
203
|
+
|
204
|
+
#-------------------------- Select Example 12 -------------------------------
|
205
|
+
print_divider('Select Example 12')
|
206
|
+
# Select all planes with a speed of nil.
|
207
|
+
|
208
|
+
#**************** Note: This example also demonstrates the change from
|
209
|
+
# nil to kb_nil for KirbyBase's internal representation of a nil value. You
|
210
|
+
# should only encounter this different if you have to check for nil in your
|
211
|
+
# query, as this example does. Other than that, everything else should
|
212
|
+
# be transparent, since KirbyBase converts a kb_nil back into a nil when it
|
213
|
+
# returns a query's result set.
|
214
|
+
#***************
|
215
|
+
plane_tbl.select { |r| r.speed.kb_nil? }.each { |r| puts r }
|
216
|
+
|
217
|
+
#-------------------------- Select Example 13 -------------------------------
|
218
|
+
print_divider('Select Example 13')
|
219
|
+
# Same thing, but in a slightly different way.
|
220
|
+
|
221
|
+
#**************** Note: This example also demonstrates the change from
|
222
|
+
# nil to kb_nil for KirbyBase's internal representation of a nil value. You
|
223
|
+
# should only encounter this different if you have to check for nil in your
|
224
|
+
# query, as this example does. Other than that, everything else should
|
225
|
+
# be transparent, since KirbyBase converts a kb_nil back into a nil when it
|
226
|
+
# returns a query's result set.
|
227
|
+
#***************
|
228
|
+
plane_tbl.select { |r| r.speed == kb_nil }.each { |r| puts r }
|
229
|
+
|
230
|
+
#-------------------------- Misc. Methods Examples -------------------------
|
231
|
+
print_divider('Misc. Methods Examples')
|
232
|
+
puts 'Total # of records in table: %d' % plane_tbl.total_recs
|
233
|
+
puts
|
234
|
+
puts 'Fields for plane.tbl:'
|
235
|
+
plane_tbl.field_names.zip(plane_tbl.field_types).each { |r|
|
236
|
+
print r[0].to_s.ljust(15), r[1].to_s, "\n"
|
237
|
+
}
|