修訂 | 57 (tree) |
---|---|
時間 | 2008-03-10 22:28:28 |
作者 | junkikuchi |
updated.
@@ -7,6 +7,7 @@ | ||
7 | 7 | |
8 | 8 | class DBTableTest < Test::Unit::TestCase |
9 | 9 | class A < Tenarai::DB::Row |
10 | + column Tenarai::DB::Boolean.new('b1', :default => false) | |
10 | 11 | column Tenarai::DB::Integer.new('i1', :default => 100) |
11 | 12 | column Tenarai::DB::Integer.new('i2', :unsigned => true) |
12 | 13 | column Tenarai::DB::String.new('s1', :default => 'DEFAULT') |
@@ -487,6 +488,7 @@ | ||
487 | 488 | assert_equal(100, a.i1) |
488 | 489 | assert_equal('DEFAULT', a.s1) |
489 | 490 | |
491 | + a.b1 = true | |
490 | 492 | a.i1 = 1 |
491 | 493 | a.i2 = 2 |
492 | 494 | a.s1 = 'a' |
@@ -498,6 +500,7 @@ | ||
498 | 500 | a.save |
499 | 501 | |
500 | 502 | assert_equal(1, a.id) |
503 | + assert_equal(true, a.b1) | |
501 | 504 | assert_equal(1, a.i1) |
502 | 505 | assert_equal(2, a.i2) |
503 | 506 | assert_equal('a', a.s1) |
@@ -508,6 +511,7 @@ | ||
508 | 511 | |
509 | 512 | time = Time.now |
510 | 513 | |
514 | + a.b1 = false | |
511 | 515 | a.i1 = 10 |
512 | 516 | a.i2 = 20 |
513 | 517 | a.s1 = 'A' |
@@ -519,6 +523,7 @@ | ||
519 | 523 | a.save |
520 | 524 | |
521 | 525 | assert_equal(1, a.id) |
526 | + assert_equal(false, a.b1) | |
522 | 527 | assert_equal(10, a.i1) |
523 | 528 | assert_equal(20, a.i2) |
524 | 529 | assert_equal('A', a.s1) |
@@ -534,6 +539,7 @@ | ||
534 | 539 | a = table.fetch(1) |
535 | 540 | |
536 | 541 | assert_equal(1, a.id) |
542 | + assert_equal(false, a.b1) | |
537 | 543 | assert_equal(10, a.i1) |
538 | 544 | assert_equal(20, a.i2) |
539 | 545 | assert_equal('A', a.s1) |
@@ -553,6 +559,7 @@ | ||
553 | 559 | a = table.fetch(1) |
554 | 560 | |
555 | 561 | assert_equal(1, a.id) |
562 | + assert_equal(false, a.b1) | |
556 | 563 | assert_equal(10, a.i1) |
557 | 564 | assert_equal(20, a.i2) |
558 | 565 | assert_equal('A', a.s1) |
@@ -564,6 +571,7 @@ | ||
564 | 571 | a.delete |
565 | 572 | |
566 | 573 | assert_nil(a.id) |
574 | + assert_equal(false, a.b1) | |
567 | 575 | assert_equal(10, a.i1) |
568 | 576 | assert_equal(20, a.i2) |
569 | 577 | assert_equal('A', a.s1) |
@@ -34,7 +34,7 @@ | ||
34 | 34 | :class => 'User', |
35 | 35 | :row => { |
36 | 36 | 'name' => 'admin', |
37 | - '_password' => Tenarai::ClassLoader['User'].digest('admin') | |
37 | + '_password' => Tenarai::ClassLoader['User'].digest('admin'), | |
38 | 38 | }, |
39 | 39 | :relation => { |
40 | 40 | 'content' => 'content:admin', |
@@ -9,7 +9,7 @@ | ||
9 | 9 | attr_reader :param |
10 | 10 | |
11 | 11 | def initialize(name, param={}) |
12 | - @name = name | |
12 | + @name = name | |
13 | 13 | @param = param |
14 | 14 | end |
15 | 15 |
@@ -37,6 +37,12 @@ | ||
37 | 37 | end |
38 | 38 | end |
39 | 39 | |
40 | + class Boolean < Column | |
41 | + def execute(command) | |
42 | + command.boolean(self) | |
43 | + end | |
44 | + end | |
45 | + | |
40 | 46 | class Integer < Column |
41 | 47 | def execute(command) |
42 | 48 | if @param[:unsigned] |
@@ -16,7 +16,7 @@ | ||
16 | 16 | class Engine |
17 | 17 | module Load |
18 | 18 | def preference(col) |
19 | - @handlers[col.name] = lambda do |row, val| | |
19 | + @handler[col.name] = lambda do |row, val| | |
20 | 20 | row.class.meta[:preference].load_param(val) |
21 | 21 | end |
22 | 22 | end |
@@ -25,7 +25,7 @@ | ||
25 | 25 | module Dump |
26 | 26 | def preference(col) |
27 | 27 | super |
28 | - @handlers[col.name] = lambda do |row, val| | |
28 | + @handler[col.name] = lambda do |row, val| | |
29 | 29 | val.dump_param(row.class.meta[:preference]) |
30 | 30 | end |
31 | 31 | end |
@@ -8,7 +8,7 @@ | ||
8 | 8 | module Load |
9 | 9 | def initialize(engine,table) |
10 | 10 | super |
11 | - @handlers = {} | |
11 | + @handler = {} | |
12 | 12 | end |
13 | 13 | |
14 | 14 | def build |
@@ -17,14 +17,18 @@ | ||
17 | 17 | |
18 | 18 | def handler(col) |
19 | 19 | if default = col.param[:default] |
20 | - @handlers[col.name] = lambda do |row, val| | |
21 | - val || default | |
20 | + @handler[col.name] = lambda do |row, val| | |
21 | + if val.nil? | |
22 | + default | |
23 | + else | |
24 | + val | |
25 | + end | |
22 | 26 | end |
23 | 27 | end |
24 | 28 | end |
25 | 29 | |
26 | 30 | def date(col) |
27 | - @handlers[col.name] = lambda do |row, val| | |
31 | + @handler[col.name] = lambda do |row, val| | |
28 | 32 | if val |
29 | 33 | Time.local( |
30 | 34 | val.year, val.month, val.day, val.hour, val.minute, val.second |
@@ -36,7 +40,7 @@ | ||
36 | 40 | end |
37 | 41 | |
38 | 42 | def serialize(col) |
39 | - @handlers[col.name] = lambda do |row, val| | |
43 | + @handler[col.name] = lambda do |row, val| | |
40 | 44 | if val |
41 | 45 | Marshal.load(val) |
42 | 46 | else |
@@ -46,13 +50,13 @@ | ||
46 | 50 | end |
47 | 51 | |
48 | 52 | def multiple(ref) |
49 | - @handlers[ref.name] = lambda do |row, val| | |
53 | + @handler[ref.name] = lambda do |row, val| | |
50 | 54 | @table.relation[ref.name].create_multiple(ref, row, val) |
51 | 55 | end |
52 | 56 | end |
53 | 57 | |
54 | 58 | def single(ref) |
55 | - @handlers[ref.name] = lambda do |row, val| | |
59 | + @handler[ref.name] = lambda do |row, val| | |
56 | 60 | @table.relation[ref.name].create_single(ref, row, val) |
57 | 61 | end |
58 | 62 | end |
@@ -60,7 +64,7 @@ | ||
60 | 64 | def execute(row) |
61 | 65 | row.primary_key = row.row.delete(row.class.meta[:primary_key].name) |
62 | 66 | row.row[row.class.meta[:class_name].name] = row.class.name |
63 | - @handlers.each do |key, val| | |
67 | + @handler.each do |key, val| | |
64 | 68 | row.row[key] = val.call(row, row.row[key]) |
65 | 69 | end |
66 | 70 | end |
@@ -69,42 +73,42 @@ | ||
69 | 73 | module Dump |
70 | 74 | def initialize(engine, table) |
71 | 75 | super |
72 | - @cols = [] | |
73 | - @handlers = {} | |
74 | - @handlers.default = lambda do |row, val| val end | |
76 | + @col = [] | |
77 | + @handler = {} | |
78 | + @handler.default = lambda do |row, val| val end | |
75 | 79 | end |
76 | 80 | |
77 | 81 | def build |
78 | 82 | each_class_name do |val| dispatch(val) end |
79 | - each_column do |val| dispatch(val) end | |
83 | + each_column do |val| dispatch(val) end | |
80 | 84 | end |
81 | 85 | |
82 | 86 | def handler(col) |
83 | - @cols << col.name | |
87 | + @col << col.name | |
84 | 88 | end |
85 | 89 | |
86 | 90 | def serialize(col) |
87 | 91 | super |
88 | - @handlers[col.name] = lambda do |row, val| | |
92 | + @handler[col.name] = lambda do |row, val| | |
89 | 93 | Marshal.dump(val) |
90 | 94 | end |
91 | 95 | end |
92 | 96 | |
93 | 97 | def multiple(col) |
94 | - @handlers[col.name] = lambda do |row, val| | |
98 | + @handler[col.name] = lambda do |row, val| | |
95 | 99 | end |
96 | 100 | end |
97 | 101 | |
98 | 102 | def single(col) |
99 | 103 | super |
100 | - @handlers[col.name] = lambda do |row, val| | |
104 | + @handler[col.name] = lambda do |row, val| | |
101 | 105 | val && val.primary_key |
102 | 106 | end |
103 | 107 | end |
104 | 108 | |
105 | 109 | def build_bindvars(row) |
106 | - @cols.map do |key| | |
107 | - @handlers[key].call(row, row.row[key]) | |
110 | + @col.map do |key| | |
111 | + @handler[key].call(row, row.row[key]) | |
108 | 112 | end |
109 | 113 | end |
110 | 114 | end |
@@ -111,10 +115,10 @@ | ||
111 | 115 | |
112 | 116 | class Command |
113 | 117 | def initialize(engine, table) |
114 | - @engine = engine | |
115 | - @table = table | |
116 | - @stmt = '' | |
117 | - @bindvars = [] | |
118 | + @engine = engine | |
119 | + @table = table | |
120 | + @stmt = '' | |
121 | + @bindvar = [] | |
118 | 122 | end |
119 | 123 | |
120 | 124 | def each_primary_key(&block) |
@@ -150,7 +154,7 @@ | ||
150 | 154 | end |
151 | 155 | |
152 | 156 | def execute(*param) |
153 | - @table.db.execute(@stmt, *@bindvars) | |
157 | + @table.db.execute(@stmt, *@bindvar) | |
154 | 158 | end |
155 | 159 | |
156 | 160 | def handler(col) |
@@ -157,6 +161,7 @@ | ||
157 | 161 | end |
158 | 162 | |
159 | 163 | def primary_key(col) handler(col); end |
164 | + def boolean(col) handler(col); end | |
160 | 165 | def integer(col) handler(col); end |
161 | 166 | def integer_unsigned(col) handler(col); end |
162 | 167 | def varchar(col) handler(col); end |
@@ -174,7 +179,7 @@ | ||
174 | 179 | class CreateTable < Command |
175 | 180 | def initialize(engine, table) |
176 | 181 | super |
177 | - @cols = [] | |
182 | + @col = [] | |
178 | 183 | @idxs = [] |
179 | 184 | end |
180 | 185 |
@@ -181,43 +186,47 @@ | ||
181 | 186 | def build |
182 | 187 | each_primary_key do |val| dispatch(val) end |
183 | 188 | each_class_name do |val| dispatch(val) end |
184 | - each_column do |val| dispatch(val) end | |
185 | - each_index do |val| dispatch(val) end | |
186 | - @stmt = self.class::SQL % [@table.name, (@cols + @idxs).join(",\n")] | |
189 | + each_column do |val| dispatch(val) end | |
190 | + each_index do |val| dispatch(val) end | |
191 | + @stmt = self.class::SQL % [@table.name, (@col + @idxs).join(",\n")] | |
187 | 192 | end |
188 | 193 | |
189 | 194 | def primary_key(col) |
190 | - @cols << self.class::SQL_PRIMARY_KEY % col.name | |
195 | + @col << self.class::SQL_PRIMARY_KEY % col.name | |
191 | 196 | end |
192 | 197 | |
198 | + def boolean(col) | |
199 | + @col << self.class::SQL_BOOLEAN % col.name | |
200 | + end | |
201 | + | |
193 | 202 | def integer(col) |
194 | - @cols << self.class::SQL_INT % col.name | |
203 | + @col << self.class::SQL_INT % col.name | |
195 | 204 | end |
196 | 205 | |
197 | 206 | def integer_unsigned(col) |
198 | - @cols << self.class::SQL_INT_UNSIG % col.name | |
207 | + @col << self.class::SQL_INT_UNSIG % col.name | |
199 | 208 | end |
200 | 209 | |
201 | 210 | def varchar(col) |
202 | - @cols << self.class::SQL_VARCHAR % [col.name, col.param[:length]] | |
211 | + @col << self.class::SQL_VARCHAR % [col.name, col.param[:length]] | |
203 | 212 | end |
204 | 213 | |
205 | 214 | def char(col) |
206 | - @cols << self.class::SQL_CHAR % [col.name, col.param[:length]] | |
215 | + @col << self.class::SQL_CHAR % [col.name, col.param[:length]] | |
207 | 216 | end |
208 | 217 | |
209 | 218 | def text(col) |
210 | - @cols << self.class::SQL_TEXT % col.name | |
219 | + @col << self.class::SQL_TEXT % col.name | |
211 | 220 | end |
212 | 221 | |
213 | 222 | def date(col) |
214 | - @cols << self.class::SQL_DATE % col.name | |
223 | + @col << self.class::SQL_DATE % col.name | |
215 | 224 | end |
216 | 225 | |
217 | 226 | alias :serialize :text |
218 | 227 | |
219 | 228 | def class_name(col) |
220 | - @cols << self.class::SQL_VARCHAR % [col.name, 255] | |
229 | + @col << self.class::SQL_VARCHAR % [col.name, 255] | |
221 | 230 | end |
222 | 231 | |
223 | 232 | def multiple(col) |
@@ -224,7 +233,7 @@ | ||
224 | 233 | end |
225 | 234 | |
226 | 235 | def single(col) |
227 | - @cols << self.class::SQL_INT_UNSIG % col.name | |
236 | + @col << self.class::SQL_INT_UNSIG % col.name | |
228 | 237 | @idxs << self.class::SQL_INDEX % [col.name, col.name] |
229 | 238 | end |
230 | 239 |
@@ -264,7 +273,7 @@ | ||
264 | 273 | |
265 | 274 | def execute(col, row) |
266 | 275 | @stmt = self.class::SQL % [@table.name, col, col] |
267 | - @bindvars = row.primary_key | |
276 | + @bindvar = row.primary_key | |
268 | 277 | super |
269 | 278 | @engine.rows.each do |key, val| |
270 | 279 | val.row[col] == row && val.row[col] = nil |
@@ -280,7 +289,7 @@ | ||
280 | 289 | def build |
281 | 290 | super |
282 | 291 | @stmt = self.class::SQL % [ |
283 | - @table.name, @cols.join(', '), (['?'] * @cols.length).join(', ') | |
292 | + @table.name, @col.join(', '), (['?'] * @col.length).join(', ') | |
284 | 293 | ] |
285 | 294 | end |
286 | 295 |
@@ -288,7 +297,7 @@ | ||
288 | 297 | end |
289 | 298 | |
290 | 299 | def execute(row) |
291 | - @bindvars = build_bindvars(row) | |
300 | + @bindvar = build_bindvars(row) | |
292 | 301 | super |
293 | 302 | row.primary_key = primary_key |
294 | 303 | @engine.rows[row.primary_key] = row |
@@ -304,14 +313,14 @@ | ||
304 | 313 | super |
305 | 314 | @stmt = self.class::SQL % [ |
306 | 315 | @table.name, |
307 | - @cols.map do |key| '%s = ?' % key end.join(', '), | |
316 | + @col.map do |key| '%s = ?' % key end.join(', '), | |
308 | 317 | @table.row_class.meta[:primary_key].name |
309 | 318 | ] |
310 | 319 | end |
311 | 320 | |
312 | 321 | def execute(row) |
313 | - @bindvars = build_bindvars(row) | |
314 | - @bindvars.push(row.primary_key) | |
322 | + @bindvar = build_bindvars(row) | |
323 | + @bindvar.push(row.primary_key) | |
315 | 324 | super |
316 | 325 | end |
317 | 326 | end |
@@ -347,9 +356,9 @@ | ||
347 | 356 | |
348 | 357 | def execute(key) |
349 | 358 | key = key.to_i |
350 | - @bindvars = [key] | |
359 | + @bindvar = [key] | |
351 | 360 | unless @engine.rows.key?(key) |
352 | - @table.db.query(@stmt, *@bindvars) do |val| | |
361 | + @table.db.query(@stmt, *@bindvar) do |val| | |
353 | 362 | @engine.rows[key] ||= @table.create_row(val) |
354 | 363 | end |
355 | 364 | end |
@@ -378,7 +387,7 @@ | ||
378 | 387 | |
379 | 388 | def initialize(engine, table) |
380 | 389 | super |
381 | - @handlers = {} | |
390 | + @handler = {} | |
382 | 391 | end |
383 | 392 | |
384 | 393 | def build |
@@ -389,8 +398,8 @@ | ||
389 | 398 | end |
390 | 399 | |
391 | 400 | def execute(row) |
392 | - @handlers.each do |key, val| val.call(row, key) end | |
393 | - @bindvars = [row.primary_key] | |
401 | + @handler.each do |key, val| val.call(row, key) end | |
402 | + @bindvar = [row.primary_key] | |
394 | 403 | super |
395 | 404 | @engine.rows.delete(row.primary_key) |
396 | 405 | row.primary_key = nil |
@@ -397,7 +406,7 @@ | ||
397 | 406 | end |
398 | 407 | |
399 | 408 | def multiple(ref) |
400 | - @handlers[ref.name] = lambda do |row, key| | |
409 | + @handler[ref.name] = lambda do |row, key| | |
401 | 410 | @table.relation[key].unlink(row) |
402 | 411 | end |
403 | 412 | end |
@@ -421,8 +430,8 @@ | ||
421 | 430 | SQL = "DELETE FROM %s " |
422 | 431 | |
423 | 432 | def execute(condition, *bindvars) |
424 | - @stmt = self.class::SQL % @table.name + condition | |
425 | - @bindvars = *bindvars | |
433 | + @stmt = self.class::SQL % @table.name + condition | |
434 | + @bindvar = *bindvars | |
426 | 435 | super |
427 | 436 | @engine.reload |
428 | 437 | end |
@@ -433,7 +442,7 @@ | ||
433 | 442 | def initialize(table) |
434 | 443 | @rows = {} |
435 | 444 | @table = table |
436 | - @commands = {} | |
445 | + @command = {} | |
437 | 446 | @command_map = { |
438 | 447 | :create_table => self.class::CreateTable, |
439 | 448 | :drop_table => self.class::DropTable, |
@@ -456,16 +465,16 @@ | ||
456 | 465 | end |
457 | 466 | |
458 | 467 | def build |
459 | - @commands = @command_map.inject({}) do |ret, (key, val)| | |
468 | + @command = @command_map.inject({}) do |ret, (key, val)| | |
460 | 469 | instance_eval <<-END |
461 | 470 | def #{key}(*param, &block) |
462 | - @commands[:#{key}].execute(*param, &block) | |
471 | + @command[:#{key}].execute(*param, &block) | |
463 | 472 | end |
464 | 473 | END |
465 | 474 | ret[key] = val.new(self, @table) |
466 | 475 | ret |
467 | 476 | end |
468 | - @commands.each do |key, val| val.build end | |
477 | + @command.each do |key, val| val.build end | |
469 | 478 | end |
470 | 479 | |
471 | 480 | def refresh |
@@ -10,6 +10,7 @@ | ||
10 | 10 | class CreateTable < CreateTable |
11 | 11 | SQL = "CREATE TABLE %s (\n%s\n) TYPE=INNODB" |
12 | 12 | SQL_PRIMARY_KEY = ' %s INT UNSIGNED PRIMARY KEY AUTO_INCREMENT' |
13 | + SQL_BOOLEAN = ' %s BOOLEAN' | |
13 | 14 | SQL_INT = ' %s INT' |
14 | 15 | SQL_INT_UNSIG = ' %s INT UNSIGNED' |
15 | 16 | SQL_VARCHAR = ' %s VARCHAR(%s)' |
@@ -143,4 +143,4 @@ | ||
143 | 143 | Main.new(CONFIG).mongrel |
144 | 144 | when 'webrick' |
145 | 145 | Main.new(CONFIG).webrick |
146 | -end | |
146 | +end if __FILE__ == $0 |
@@ -20,6 +20,7 @@ | ||
20 | 20 | end |
21 | 21 | end |
22 | 22 | |
23 | + column Tenarai::DB::Boolean.new('active', :default => true) | |
23 | 24 | column Tenarai::DB::String.new('name', :length => 255) |
24 | 25 | column Tenarai::DB::String.new('_password', :length => 255) |
25 | 26 | column Tenarai::DB::Reference.new('content') |
@@ -30,6 +31,7 @@ | ||
30 | 31 | end |
31 | 32 | |
32 | 33 | def auth(password) |
33 | - self._password == self.class.digest(password) | |
34 | + p [name, self.active] | |
35 | + active && self._password == self.class.digest(password) | |
34 | 36 | end |
35 | 37 | end |