Showing 18 changed files with 1068 additions and 1007 deletions
+1 -1
t/common-oracle.t
... ...
@@ -16,7 +16,7 @@ use DBIx::Custom;
16 16
             Callbacks => {
17 17
               connected => sub {
18 18
                   shift->do(
19
-                      "ALTER SESSION SET NLS_DATE_FORMAT = 'RR-MM-DD HH24:MI:SS'"
19
+                      "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"
20 20
                   );
21 21
                   return;
22 22
               },
+994 -1006
t/common.t
... ...
@@ -14,6 +14,67 @@ plan skip_all => $ENV{DBIX_CUSTOM_SKIP_MESSAGE} || 'common.t is always skipped'
14 14
 
15 15
 plan 'no_plan';
16 16
 
17
+# Constant
18
+my $table1 = $dbi->table1;
19
+my $table2 = $dbi->table2;
20
+my $table2_alias = $dbi->table2_alias;
21
+my $table3 = $dbi->table3;
22
+my $key1 = $dbi->key1;
23
+my $key2 = $dbi->key2;
24
+my $key3 = $dbi->key3;
25
+my $key4 = $dbi->key4;
26
+my $key5 = $dbi->key5;
27
+my $key6 = $dbi->key6;
28
+my $key7 = $dbi->key7;
29
+my $key8 = $dbi->key8;
30
+my $key9 = $dbi->key9;
31
+my $key10 = $dbi->key10;
32
+my $create_table1 = $dbi->create_table1;
33
+my $create_table1_2 = $dbi->create_table1_2;
34
+my $create_table1_type = $dbi->create_table1_type;
35
+my $create_table1_highperformance = $dbi->create_table1_highperformance;
36
+my $create_table2 = $dbi->create_table2;
37
+my $create_table2_2 = $dbi->create_table2_2;
38
+my $create_table3 = $dbi->create_table3;
39
+my $create_table_reserved = $dbi->create_table_reserved;
40
+my $q = substr($dbi->quote, 0, 1);
41
+my $p = substr($dbi->quote, 1, 1) || $q;
42
+my $date_typename = $dbi->date_typename;
43
+my $datetime_typename = $dbi->datetime_typename;
44
+my $date_datatype = $dbi->date_datatype;
45
+my $datetime_datatype = $dbi->datetime_datatype;
46
+
47
+# Variables
48
+my $builder;
49
+my $datas;
50
+my $sth;
51
+my $source;
52
+my @sources;
53
+my $select_source;
54
+my $insert_source;
55
+my $update_source;
56
+my $param;
57
+my $params;
58
+my $sql;
59
+my $result;
60
+my $row;
61
+my @rows;
62
+my $rows;
63
+my $query;
64
+my @queries;
65
+my $select_query;
66
+my $insert_query;
67
+my $update_query;
68
+my $ret_val;
69
+my $infos;
70
+my $model;
71
+my $model2;
72
+my $where;
73
+my $update_param;
74
+my $insert_param;
75
+my $join;
76
+my $binary;
77
+
17 78
 use MyDBI1;
18 79
 {
19 80
     package MyDBI4;
... ...
@@ -28,8 +89,8 @@ use MyDBI1;
28 89
         
29 90
         $self->include_model(
30 91
             MyModel2 => [
31
-                'table1',
32
-                {class => 'table2', name => 'table2'}
92
+                $table1,
93
+                {class => $table2, name => $table2}
33 94
             ]
34 95
         );
35 96
     }
... ...
@@ -143,549 +204,56 @@ use MyDBI1;
143 204
 $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
144 205
 sub test { print "# $_[0]\n" }
145 206
 
146
-# Constant
147
-my $table1 = $dbi->table1;
148
-my $table2 = $dbi->table2;
149
-my $table2_alias = $dbi->table2_alias;
150
-my $table3 = $dbi->table3;
151
-my $key1 = $dbi->key1;
152
-my $key2 = $dbi->key2;
153
-my $key3 = $dbi->key3;
154
-my $key4 = $dbi->key4;
155
-my $key5 = $dbi->key5;
156
-my $key6 = $dbi->key6;
157
-my $key7 = $dbi->key7;
158
-my $key8 = $dbi->key8;
159
-my $key9 = $dbi->key9;
160
-my $key10 = $dbi->key10;
161
-my $create_table1 = $dbi->create_table1;
162
-my $create_table1_2 = $dbi->create_table1_2;
163
-my $create_table1_type = $dbi->create_table1_type;
164
-my $create_table1_highperformance = $dbi->create_table1_highperformance;
165
-my $create_table2 = $dbi->create_table2;
166
-my $create_table2_2 = $dbi->create_table2_2;
167
-my $create_table3 = $dbi->create_table3;
168
-my $create_table_reserved = $dbi->create_table_reserved;
169
-my $q = substr($dbi->quote, 0, 1);
170
-my $p = substr($dbi->quote, 1, 1) || $q;
171
-my $date_typename = $dbi->date_typename;
172
-my $datetime_typename = $dbi->datetime_typename;
173
-my $date_datatype = $dbi->date_datatype;
174
-my $datetime_datatype = $dbi->datetime_datatype;
175
-
176
-# Variables
177
-my $builder;
178
-my $datas;
179
-my $sth;
180
-my $source;
181
-my @sources;
182
-my $select_source;
183
-my $insert_source;
184
-my $update_source;
185
-my $param;
186
-my $params;
187
-my $sql;
188
-my $result;
189
-my $row;
190
-my @rows;
191
-my $rows;
192
-my $query;
193
-my @queries;
194
-my $select_query;
195
-my $insert_query;
196
-my $update_query;
197
-my $ret_val;
198
-my $infos;
199
-my $model;
200
-my $model2;
201
-my $where;
202
-my $update_param;
203
-my $insert_param;
204
-my $join;
205
-my $binary;
206
-
207
-
208
-test 'type_rule into';
209
-eval { $dbi->execute("drop table $table1") };
210
-$dbi->execute($create_table1_type);
207
+# Create table
211 208
 $dbi = DBIx::Custom->connect;
212 209
 eval { $dbi->execute("drop table $table1") };
213
-$dbi->execute($create_table1_type);
214
-$dbi->show_tables;
210
+$dbi->execute($create_table1);
211
+$model = $dbi->create_model(table => $table1);
212
+$model->insert({$key1 => 1, $key2 => 2});
213
+is_deeply($model->select->all, [{$key1 => 1, $key2 => 2}]);
215 214
 
216
-$DB::single = 1;
215
+test 'DBIx::Custom::Result test';
216
+$dbi->delete_all(table => $table1);
217
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
218
+$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
219
+$source = "select $key1, $key2 from $table1";
220
+$query = $dbi->create_query($source);
221
+$result = $dbi->execute($query);
217 222
 
218
-__END__
223
+@rows = ();
224
+while (my $row = $result->fetch) {
225
+    push @rows, [@$row];
226
+}
227
+is_deeply(\@rows, [[1, 2], [3, 4]], "fetch");
219 228
 
220
-$dbi->type_rule(
221
-    into1 => {
222
-        $date_typename => sub { '2010-' . $_[0] }
223
-    }
224
-);
225
-$dbi->insert({$key1 => '01-01'}, table => $table1);
226
-$result = $dbi->select(table => $table1);
227
-like($result->one->{$key1}, qr/^2010-01-01/);
229
+$result = $dbi->execute($query);
230
+@rows = ();
231
+while (my $row = $result->fetch_hash) {
232
+    push @rows, {%$row};
233
+}
234
+is_deeply(\@rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "fetch_hash");
228 235
 
229
-$dbi = DBIx::Custom->connect;
230
-eval { $dbi->execute("drop table $table1") };
231
-$dbi->execute($create_table1_type);
232
-$dbi->type_rule(
233
-    into1 => [
234
-         [$date_typename, $datetime_typename] => sub {
235
-            my $value = shift;
236
-            $value =~ s/02/03/g;
237
-            return $value;
238
-         }
239
-    ]
240
-);
241
-$dbi->insert({$key1 => '2010-01-02', $key2 => '2010-01-01 01:01:02'}, table => $table1);
242
-$result = $dbi->select(table => $table1);
243
-$row = $result->one;
244
-like($row->{$key1}, qr/^2010-01-03/);
245
-like($row->{$key2}, qr/^2010-01-01 01:01:03/);
236
+$result = $dbi->execute($query);
237
+$rows = $result->fetch_all;
238
+is_deeply($rows, [[1, 2], [3, 4]], "fetch_all");
246 239
 
247
-$dbi = DBIx::Custom->connect;
248
-eval { $dbi->execute("drop table $table1") };
249
-$dbi->execute($create_table1_type);
250
-$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
251
-$dbi->type_rule(
252
-    into1 => [
253
-        [$date_typename, $datetime_typename] => sub {
254
-            my $value = shift;
255
-            $value =~ s/02/03/g;
256
-            return $value;
257
-        }
258
-    ]
259
-);
260
-$result = $dbi->execute(
261
-    "select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2",
262
-    param => {$key1 => '2010-01-03', "$table1.$key2" => '2010-01-01 01:01:02'}
263
-);
264
-$row = $result->one;
265
-like($row->{$key1}, qr/^2010-01-03/);
266
-like($row->{$key2}, qr/^2010-01-01 01:01:03/);
240
+$result = $dbi->execute($query);
241
+$rows = $result->fetch_hash_all;
242
+is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "all");
267 243
 
268
-$dbi = DBIx::Custom->connect;
269
-eval { $dbi->execute("drop table $table1") };
270
-$dbi->execute($create_table1_type);
271
-$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
272
-$dbi->type_rule(
273
-    into1 => [
274
-        [$date_typename, $datetime_typename] => sub {
275
-            my $value = shift;
276
-            $value =~ s/02/03/g;
277
-            return $value;
278
-        }
279
-    ]
280
-);
281
-$result = $dbi->execute(
282
-    "select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2",
283
-    param => {$key1 => '2010-01-02', "$table1.$key2" => '2010-01-01 01:01:02'},
284
-    table => $table1
285
-);
286
-$row = $result->one;
287
-like($row->{$key1}, qr/^2010-01-03/);
288
-like($row->{$key2}, qr/2010-01-01 01:01:03/);
244
+test 'Insert query return value';
245
+$source = "insert into $table1 {insert_param $key1 $key2}";
246
+$query = $dbi->execute($source, {}, query => 1);
247
+$ret_val = $dbi->execute($query, param => {$key1 => 1, $key2 => 2});
248
+ok($ret_val);
289 249
 
290
-$dbi = DBIx::Custom->connect;
291
-eval { $dbi->execute("drop table $table1") };
292
-$dbi->execute($create_table1_type);
293
-$dbi->register_filter(convert => sub {
294
-    my $value = shift || '';
295
-    $value =~ s/02/03/;
296
-    return $value;
297
-});
298
-$dbi->type_rule(
299
-    from1 => {
300
-        $date_datatype => 'convert',
301
-    },
302
-    into1 => {
303
-        $date_typename => 'convert',
304
-    }
305
-);
306
-$dbi->insert({$key1 => '2010-02-02'}, table => $table1);
307
-$result = $dbi->select(table => $table1);
308
-like($result->fetch->[0], qr/^2010-03-03/);
309
-
310
-test 'type_rule and filter order';
311
-$dbi = DBIx::Custom->connect;
312
-eval { $dbi->execute("drop table $table1") };
313
-$dbi->execute($create_table1_type);
314
-$dbi->type_rule(
315
-    into1 => {
316
-        $date_typename => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
317
-    },
318
-    into2 => {
319
-        $date_typename => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
320
-    },
321
-    from1 => {
322
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
323
-    },
324
-    from2 => {
325
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/7/8/; return $v }
326
-    }
327
-);
328
-$dbi->insert({$key1 => '2010-01-03'}, 
329
-  table => $table1, filter => {$key1 => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }});
330
-$result = $dbi->select(table => $table1);
331
-$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
332
-like($result->fetch_first->[0], qr/^2010-01-09/);
333
-
334
-
335
-$dbi = DBIx::Custom->connect;
336
-eval { $dbi->execute("drop table $table1") };
337
-$dbi->execute($create_table1_type);
338
-$dbi->type_rule(
339
-    from1 => {
340
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
341
-    },
342
-    from2 => {
343
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
344
-    },
345
-);
346
-$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
347
-$result = $dbi->select(table => $table1);
348
-$result->type_rule(
349
-    from1 => {
350
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
351
-    },
352
-    from2 => {
353
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/8/; return $v }
354
-    }
355
-);
356
-$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
357
-like($result->fetch_first->[0], qr/^2010-01-09/);
358
-
359
-test 'type_rule_off';
360
-$dbi = DBIx::Custom->connect;
361
-eval { $dbi->execute("drop table $table1") };
362
-$dbi->execute($create_table1_type);
363
-$dbi->type_rule(
364
-    from1 => {
365
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
366
-    },
367
-    into1 => {
368
-        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
369
-    }
370
-);
371
-$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
372
-$result = $dbi->select(table => $table1, type_rule_off => 1);
373
-like($result->type_rule_off->fetch->[0], qr/^2010-01-03/);
374
-
375
-$dbi = DBIx::Custom->connect;
376
-eval { $dbi->execute("drop table $table1") };
377
-$dbi->execute($create_table1_type);
378
-$dbi->type_rule(
379
-    from1 => {
380
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
381
-    },
382
-    into1 => {
383
-        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
384
-    }
385
-);
386
-$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
387
-$result = $dbi->select(table => $table1, type_rule_off => 1);
388
-like($result->one->{$key1}, qr/^2010-01-04/);
389
-
390
-$dbi = DBIx::Custom->connect;
391
-eval { $dbi->execute("drop table $table1") };
392
-$dbi->execute($create_table1_type);
393
-$dbi->type_rule(
394
-    from1 => {
395
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
396
-    },
397
-    into1 => {
398
-        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
399
-    }
400
-);
401
-$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
402
-$result = $dbi->select(table => $table1);
403
-like($result->one->{$key1}, qr/^2010-01-05/);
404
-
405
-$dbi = DBIx::Custom->connect;
406
-eval { $dbi->execute("drop table $table1") };
407
-$dbi->execute($create_table1_type);
408
-$dbi->type_rule(
409
-    from1 => {
410
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
411
-    },
412
-    into1 => {
413
-        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
414
-    }
415
-);
416
-$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
417
-$result = $dbi->select(table => $table1);
418
-like($result->fetch->[0], qr/2010-01-05/);
419
-
420
-$dbi = DBIx::Custom->connect;
421
-eval { $dbi->execute("drop table $table1") };
422
-$dbi->execute($create_table1_type);
423
-$dbi->register_filter(ppp => sub { my $v = shift || ''; $v =~ s/3/4/; return $v });
424
-$dbi->type_rule(
425
-    into1 => {
426
-        $date_typename => 'ppp'
427
-    }
428
-);
429
-$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
430
-$result = $dbi->select(table => $table1);
431
-like($result->one->{$key1}, qr/^2010-01-04/);
432
-
433
-eval{$dbi->type_rule(
434
-    into1 => {
435
-        $date_typename => 'pp'
436
-    }
437
-)};
438
-like($@, qr/not registered/);
439
-
440
-$dbi = DBIx::Custom->connect;
441
-eval { $dbi->execute("drop table $table1") };
442
-$dbi->execute($create_table1_type);
443
-eval {
444
-    $dbi->type_rule(
445
-        from1 => {
446
-            Date => sub { $_[0] * 2 },
447
-        }
448
-    );
449
-};
450
-like($@, qr/lower/);
451
-
452
-eval {
453
-    $dbi->type_rule(
454
-        into1 => {
455
-            Date => sub { $_[0] * 2 },
456
-        }
457
-    );
458
-};
459
-like($@, qr/lower/);
460
-
461
-$dbi = DBIx::Custom->connect;
462
-eval { $dbi->execute("drop table $table1") };
463
-$dbi->execute($create_table1_type);
464
-$dbi->type_rule(
465
-    from1 => {
466
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
467
-    },
468
-    into1 => {
469
-        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
470
-    }
471
-);
472
-$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
473
-$result = $dbi->select(table => $table1);
474
-$result->type_rule_off;
475
-like($result->one->{$key1}, qr/^2010-01-04/);
476
-
477
-$dbi = DBIx::Custom->connect;
478
-eval { $dbi->execute("drop table $table1") };
479
-$dbi->execute($create_table1_type);
480
-$dbi->type_rule(
481
-    from1 => {
482
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v },
483
-        $datetime_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
484
-    },
485
-);
486
-$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
487
-$result = $dbi->select(table => $table1);
488
-$result->type_rule(
489
-    from1 => {
490
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
491
-    }
492
-);
493
-$row = $result->one;
494
-like($row->{$key1}, qr/^2010-01-05/);
495
-like($row->{$key2}, qr/^2010-01-01 01:01:03/);
496
-
497
-$result = $dbi->select(table => $table1);
498
-$result->type_rule(
499
-    from1 => {
500
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
501
-    }
502
-);
503
-$row = $result->one;
504
-like($row->{$key1}, qr/2010-01-05/);
505
-like($row->{$key2}, qr/2010-01-01 01:01:03/);
506
-
507
-$result = $dbi->select(table => $table1);
508
-$result->type_rule(
509
-    from1 => {
510
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
511
-    }
512
-);
513
-$row = $result->one;
514
-like($row->{$key1}, qr/2010-01-05/);
515
-like($row->{$key2}, qr/2010-01-01 01:01:03/);
516
-
517
-$result = $dbi->select(table => $table1);
518
-$result->type_rule(
519
-    from1 => [$date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }]
520
-);
521
-$row = $result->one;
522
-like($row->{$key1}, qr/2010-01-05/);
523
-like($row->{$key2}, qr/2010-01-01 01:01:03/);
524
-
525
-$dbi->register_filter(five => sub { my $v = shift || ''; $v =~ s/3/5/; return $v });
526
-$result = $dbi->select(table => $table1);
527
-$result->type_rule(
528
-    from1 => [$date_datatype => 'five']
529
-);
530
-$row = $result->one;
531
-like($row->{$key1}, qr/^2010-01-05/);
532
-like($row->{$key2}, qr/^2010-01-01 01:01:03/);
533
-
534
-$result = $dbi->select(table => $table1);
535
-$result->type_rule(
536
-    from1 => [$date_datatype => undef]
537
-);
538
-$row = $result->one;
539
-like($row->{$key1}, qr/^2010-01-03/);
540
-like($row->{$key2}, qr/^2010-01-01 01:01:03/);
541
-
542
-$dbi = DBIx::Custom->connect;
543
-eval { $dbi->execute("drop table $table1") };
544
-$dbi->execute($create_table1_type);
545
-$dbi->type_rule(
546
-    from1 => {
547
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v },
548
-    },
549
-);
550
-$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
551
-$result = $dbi->select(table => $table1);
552
-$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/4/5/; return $v });
553
-like($result->one->{$key1}, qr/^2010-01-05/);
554
-
555
-$dbi = DBIx::Custom->connect;
556
-eval { $dbi->execute("drop table $table1") };
557
-$dbi->execute($create_table1_type);
558
-$dbi->type_rule(
559
-    from1 => {
560
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
561
-    },
562
-);
563
-$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
564
-$result = $dbi->select(table => $table1);
565
-$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/4/5/; return $v });
566
-like($result->fetch->[0], qr/^2010-01-05/);
567
-
568
-$dbi = DBIx::Custom->connect;
569
-eval { $dbi->execute("drop table $table1") };
570
-$dbi->execute($create_table1_type);
571
-$dbi->type_rule(
572
-    into1 => {
573
-        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
574
-    },
575
-    into2 => {
576
-        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
577
-    },
578
-    from1 => {
579
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
580
-    },
581
-    from2 => {
582
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v }
583
-    }
584
-);
585
-$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
586
-$result = $dbi->select(table => $table1);
587
-like($result->type_rule_off->fetch_first->[0], qr/^2010-01-03/);
588
-$result = $dbi->select(table => $table1);
589
-like($result->type_rule_on->fetch_first->[0], qr/^2010-01-07/);
590
-
591
-$dbi = DBIx::Custom->connect;
592
-eval { $dbi->execute("drop table $table1") };
593
-$dbi->execute($create_table1_type);
594
-$dbi->type_rule(
595
-    into1 => {
596
-        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
597
-    },
598
-    into2 => {
599
-        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
600
-    },
601
-    from1 => {
602
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|5)/6/; return $v }
603
-    },
604
-    from2 => {
605
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
606
-    }
607
-);
608
-$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule1_off => 1);
609
-$result = $dbi->select(table => $table1);
610
-like($result->type_rule1_off->fetch_first->[0], qr/^2010-01-05/);
611
-$result = $dbi->select(table => $table1);
612
-like($result->type_rule1_on->fetch_first->[0], qr/^2010-01-07/);
613
-
614
-$dbi = DBIx::Custom->connect;
615
-eval { $dbi->execute("drop table $table1") };
616
-$dbi->execute($create_table1_type);
617
-$dbi->type_rule(
618
-    into1 => {
619
-        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
620
-    },
621
-    into2 => {
622
-        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
623
-    },
624
-    from1 => {
625
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
626
-    },
627
-    from2 => {
628
-        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v }
629
-    }
630
-);
631
-$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule2_off => 1);
632
-$result = $dbi->select(table => $table1);
633
-like($result->type_rule2_off->fetch_first->[0], qr/^2010-01-06/);
634
-$result = $dbi->select(table => $table1);
635
-like($result->type_rule2_on->fetch_first->[0], qr/^2010-01-07/);
636
-
637
-
638
-
639
-# Create table
640
-$dbi = DBIx::Custom->connect;
641
-eval { $dbi->execute("drop table $table1") };
642
-$dbi->execute($create_table1);
643
-$model = $dbi->create_model(table => $table1);
644
-$model->insert({$key1 => 1, $key2 => 2});
645
-is_deeply($model->select->all, [{$key1 => 1, $key2 => 2}]);
646
-
647
-test 'DBIx::Custom::Result test';
648
-$dbi->delete_all(table => $table1);
649
-$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
650
-$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
651
-$source = "select $key1, $key2 from $table1";
652
-$query = $dbi->create_query($source);
653
-$result = $dbi->execute($query);
654
-
655
-@rows = ();
656
-while (my $row = $result->fetch) {
657
-    push @rows, [@$row];
658
-}
659
-is_deeply(\@rows, [[1, 2], [3, 4]], "fetch");
660
-
661
-$result = $dbi->execute($query);
662
-@rows = ();
663
-while (my $row = $result->fetch_hash) {
664
-    push @rows, {%$row};
665
-}
666
-is_deeply(\@rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "fetch_hash");
667
-
668
-$result = $dbi->execute($query);
669
-$rows = $result->fetch_all;
670
-is_deeply($rows, [[1, 2], [3, 4]], "fetch_all");
671
-
672
-$result = $dbi->execute($query);
673
-$rows = $result->fetch_hash_all;
674
-is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "all");
675
-
676
-test 'Insert query return value';
677
-$source = "insert into $table1 {insert_param $key1 $key2}";
678
-$query = $dbi->execute($source, {}, query => 1);
679
-$ret_val = $dbi->execute($query, param => {$key1 => 1, $key2 => 2});
680
-ok($ret_val);
681
-
682
-test 'Direct query';
683
-$dbi->delete_all(table => $table1);
684
-$insert_source = "insert into $table1 {insert_param $key1 $key2}";
685
-$dbi->execute($insert_source, param => {$key1 => 1, $key2 => 2});
686
-$result = $dbi->execute("select * from $table1");
687
-$rows = $result->all;
688
-is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
250
+test 'Direct query';
251
+$dbi->delete_all(table => $table1);
252
+$insert_source = "insert into $table1 {insert_param $key1 $key2}";
253
+$dbi->execute($insert_source, param => {$key1 => 1, $key2 => 2});
254
+$result = $dbi->execute("select * from $table1");
255
+$rows = $result->all;
256
+is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
689 257
 
690 258
 test 'Filter basic';
691 259
 $dbi->delete_all(table => $table1);
... ...
@@ -1486,82 +1054,30 @@ $dbi->execute($create_table2);
1486 1054
 $dbi->register_filter(twice => sub { $_[0] * 2 });
1487 1055
 $dbi->register_filter(three_times => sub { $_[0] * 3 });
1488 1056
 $dbi->apply_filter(
1489
-    $table1, $key2 => {out => 'twice', in => 'twice'}
1490
-);
1491
-$dbi->apply_filter(
1492
-    $table2, $key3 => {out => 'three_times', in => 'three_times'}
1493
-);
1494
-$dbi->insert(table => $table1, param => {$key1 => 5, $key2 => 2}, filter => {$key2 => undef});
1495
-$dbi->insert(table => $table2, param => {$key1 => 5, $key3 => 6}, filter => {$key3 => undef});
1496
-$result = $dbi->select(
1497
-     table => [$table1, $table2],
1498
-     column => [$key2, $key3],
1499
-     where => {"$table1.$key2" => 1, "$table2.$key3" => 2}, relation => {"$table1.$key1" => "$table2.$key1"});
1500
-
1501
-$result->filter({$key2 => 'twice'});
1502
-$rows   = $result->all;
1503
-is_deeply($rows, [{$key2 => 4, $key3 => 18}], "select : join");
1504
-
1505
-$result = $dbi->select(
1506
-     table => [$table1, $table2],
1507
-     column => [$key2, $key3],
1508
-     where => {$key2 => 1, $key3 => 2}, relation => {"$table1.$key1" => "$table2.$key1"});
1509
-
1510
-$result->filter({$key2 => 'twice'});
1511
-$rows   = $result->all;
1512
-is_deeply($rows, [{$key2 => 4, $key3 => 18}], "select : join : omit");
1513
-
1514
-test 'each_column';
1515
-$dbi = DBIx::Custom->connect;
1516
-eval { $dbi->execute("drop table ${q}table$p") };
1517
-eval { $dbi->execute("drop table $table1") };
1518
-eval { $dbi->execute("drop table $table2") };
1519
-eval { $dbi->execute("drop table $table3") };
1520
-$dbi->execute($create_table1_type);
1521
-$dbi->execute($create_table2);
1522
-
1523
-$infos = [];
1524
-$dbi->each_column(sub {
1525
-    my ($self, $table, $column, $cinfo) = @_;
1526
-    
1527
-    if ($table =~ /^table\d/) {
1528
-         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
1529
-         push @$infos, $info;
1530
-    }
1531
-});
1532
-$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
1533
-is_deeply($infos, 
1534
-    [
1535
-        [$table1, $key1, $key1],
1536
-        [$table1, $key2, $key2],
1537
-        [$table2, $key1, $key1],
1538
-        [$table2, $key3, $key3]
1539
-    ]
1540
-    
1541
-);
1542
-test 'each_table';
1543
-$dbi = DBIx::Custom->connect;
1544
-eval { $dbi->execute("drop table $table1") };
1545
-eval { $dbi->execute("drop table $table2") };
1546
-$dbi->execute($create_table2);
1547
-$dbi->execute($create_table1_type);
1548
-
1549
-$infos = [];
1550
-$dbi->each_table(sub {
1551
-    my ($self, $table, $table_info) = @_;
1552
-    
1553
-    if ($table =~ /^table\d/) {
1554
-         my $info = [$table, $table_info->{TABLE_NAME}];
1555
-         push @$infos, $info;
1556
-    }
1557
-});
1558
-$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
1559
-is_deeply($infos, 
1560
-    [
1561
-        [$table1, $table1],
1562
-        [$table2, $table2],
1563
-    ]
1057
+    $table1, $key2 => {out => 'twice', in => 'twice'}
1058
+);
1059
+$dbi->apply_filter(
1060
+    $table2, $key3 => {out => 'three_times', in => 'three_times'}
1564 1061
 );
1062
+$dbi->insert(table => $table1, param => {$key1 => 5, $key2 => 2}, filter => {$key2 => undef});
1063
+$dbi->insert(table => $table2, param => {$key1 => 5, $key3 => 6}, filter => {$key3 => undef});
1064
+$result = $dbi->select(
1065
+     table => [$table1, $table2],
1066
+     column => [$key2, $key3],
1067
+     where => {"$table1.$key2" => 1, "$table2.$key3" => 2}, relation => {"$table1.$key1" => "$table2.$key1"});
1068
+
1069
+$result->filter({$key2 => 'twice'});
1070
+$rows   = $result->all;
1071
+is_deeply($rows, [{$key2 => 4, $key3 => 18}], "select : join");
1072
+
1073
+$result = $dbi->select(
1074
+     table => [$table1, $table2],
1075
+     column => [$key2, $key3],
1076
+     where => {$key2 => 1, $key3 => 2}, relation => {"$table1.$key1" => "$table2.$key1"});
1077
+
1078
+$result->filter({$key2 => 'twice'});
1079
+$rows   = $result->all;
1080
+is_deeply($rows, [{$key2 => 4, $key3 => 18}], "select : join : omit");
1565 1081
 
1566 1082
 test 'connect super';
1567 1083
 $dbi = DBIx::Custom->connect;
... ...
@@ -2114,10 +1630,6 @@ $where->map(
2114 1630
 is_deeply($where->param, {"$table1.id" => [$dbi->not_exists, $dbi->not_exists], "$table1.author" => '%Ken%',
2115 1631
   "$table1.price" => 1900});
2116 1632
 
2117
-test 'dbi_option default';
2118
-$dbi = DBIx::Custom->new;
2119
-is_deeply($dbi->dbi_option, {});
2120
-
2121 1633
 test 'register_tag_processor';
2122 1634
 $dbi = DBIx::Custom->connect;
2123 1635
 $dbi->register_tag_processor(
... ...
@@ -2428,7 +1940,7 @@ like($@, qr/must be/);
2428 1940
 test 'columns';
2429 1941
 use MyDBI1;
2430 1942
 $dbi = MyDBI1->connect;
2431
-$model = $dbi->model($table1);
1943
+$model = $dbi->model(lc $table1);
2432 1944
 
2433 1945
 
2434 1946
 test 'model delete_at';
... ...
@@ -3366,482 +2878,958 @@ eval{$builder->build_query("{a}")};
3366 2878
 like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
3367 2879
 
3368 2880
 
3369
-test 'Default tag Error case';
3370
-eval{$builder->build_query("{= }")};
3371
-like($@, qr/Column name must be specified in tag "{= }"/, "basic '=' : key not exist");
2881
+test 'Default tag Error case';
2882
+eval{$builder->build_query("{= }")};
2883
+like($@, qr/Column name must be specified in tag "{= }"/, "basic '=' : key not exist");
2884
+
2885
+eval{$builder->build_query("{in }")};
2886
+like($@, qr/Column name and count of values must be specified in tag "{in }"/, "in : key not exist");
2887
+
2888
+eval{$builder->build_query("{in a}")};
2889
+like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/,
2890
+     "in : key not exist");
2891
+
2892
+eval{$builder->build_query("{in a r}")};
2893
+like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/,
2894
+     "in : key not exist");
2895
+
2896
+test 'variouse source';
2897
+$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d";
2898
+$query = $builder->build_query($source);
2899
+is($query->sql, 'a b = ? c { } { = ? } = ? d', "basic : 1");
2900
+
2901
+$source = "abc";
2902
+$query = $builder->build_query($source);
2903
+is($query->sql, 'abc', "basic : 2");
2904
+
2905
+$source = "{= a}";
2906
+$query = $builder->build_query($source);
2907
+is($query->sql, 'a = ?', "only tag");
2908
+
2909
+$source = "000";
2910
+$query = $builder->build_query($source);
2911
+is($query->sql, '000', "contain 0 value");
2912
+
2913
+$source = "a {= b} }";
2914
+eval{$builder->build_query($source)};
2915
+like($@, qr/unexpected "}"/, "error : 1");
2916
+
2917
+$source = "a {= {}";
2918
+eval{$builder->build_query($source)};
2919
+like($@, qr/unexpected "{"/, "error : 2");
2920
+
2921
+test 'select() wrap option';
2922
+$dbi = DBIx::Custom->connect;
2923
+eval { $dbi->execute("drop table $table1") };
2924
+$dbi->execute($create_table1);
2925
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2926
+$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
2927
+$rows = $dbi->select(
2928
+    table => $table1,
2929
+    column => $key1,
2930
+    wrap => ["select * from (", ") as t where $key1 = 1"]
2931
+)->all;
2932
+is_deeply($rows, [{$key1 => 1}]);
2933
+
2934
+eval {
2935
+$dbi->select(
2936
+    table => $table1,
2937
+    column => $key1,
2938
+    wrap => 'select * from ('
2939
+)
2940
+};
2941
+like($@, qr/array/);
2942
+
2943
+test 'select() sqlfilter option';
2944
+$dbi = DBIx::Custom->connect;
2945
+eval { $dbi->execute("drop table $table1") };
2946
+$dbi->execute($create_table1);
2947
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2948
+$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
2949
+$rows = $dbi->select(
2950
+    table => $table1,
2951
+    column => $key1,
2952
+    sqlfilter => sub {
2953
+        my $sql = shift;
2954
+        $sql = "select * from ( $sql ) as t where $key1 = 1";
2955
+        return $sql;
2956
+    }
2957
+)->all;
2958
+is_deeply($rows, [{$key1 => 1}]);
2959
+
2960
+test 'dbi method from model';
2961
+$dbi = MyDBI9->connect;
2962
+eval { $dbi->execute("drop table $table1") };
2963
+$dbi->execute($create_table1);
2964
+$dbi->setup_model;
2965
+$model = $dbi->model($table1);
2966
+eval{$model->execute("select * from $table1")};
2967
+ok(!$@);
2968
+
2969
+test 'column table option';
2970
+$dbi = MyDBI9->connect;
2971
+eval { $dbi->execute("drop table $table1") };
2972
+$dbi->execute($create_table1);
2973
+eval { $dbi->execute("drop table $table2") };
2974
+$dbi->execute($create_table2);
2975
+$dbi->setup_model;
2976
+$dbi->execute("insert into $table1 ($key1, $key2) values (1, 2)");
2977
+$dbi->execute("insert into $table2 ($key1, $key3) values (1, 4)");
2978
+$model = $dbi->model($table1);
2979
+$result = $model->select(
2980
+    column => [
2981
+        $model->column($table2, {alias => $table2_alias})
2982
+    ],
2983
+    where => {"$table2_alias.$key3" => 4}
2984
+);
2985
+is_deeply($result->one, 
2986
+          {"$table2_alias.$key1" => 1, "$table2_alias.$key3" => 4});
2987
+
2988
+$dbi->separator('__');
2989
+$result = $model->select(
2990
+    column => [
2991
+        $model->column($table2, {alias => $table2_alias})
2992
+    ],
2993
+    where => {"$table2_alias.$key3" => 4}
2994
+);
2995
+is_deeply($result->one, 
2996
+          {"${table2_alias}__$key1" => 1, "${table2_alias}__$key3" => 4});
2997
+
2998
+$dbi->separator('-');
2999
+$result = $model->select(
3000
+    column => [
3001
+        $model->column($table2, {alias => $table2_alias})
3002
+    ],
3003
+    where => {"$table2_alias.$key3" => 4}
3004
+);
3005
+is_deeply($result->one, 
3006
+          {"$table2_alias-$key1" => 1, "$table2_alias-$key3" => 4});
3007
+
3008
+test 'create_model';
3009
+$dbi = DBIx::Custom->connect;
3010
+eval { $dbi->execute("drop table $table1") };
3011
+eval { $dbi->execute("drop table $table2") };
3012
+$dbi->execute($create_table1);
3013
+$dbi->execute($create_table2);
3014
+
3015
+$dbi->create_model(
3016
+    table => $table1,
3017
+    join => [
3018
+       "left outer join $table2 on $table1.$key1 = $table2.$key1"
3019
+    ],
3020
+    primary_key => [$key1]
3021
+);
3022
+$model2 = $dbi->create_model(
3023
+    table => $table2
3024
+);
3025
+$dbi->create_model(
3026
+    table => $table3,
3027
+    filter => [
3028
+        $key1 => {in => sub { uc $_[0] }}
3029
+    ]
3030
+);
3031
+$dbi->setup_model;
3032
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3033
+$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3034
+$model = $dbi->model($table1);
3035
+$result = $model->select(
3036
+    column => [$model->mycolumn, $model->column($table2)],
3037
+    where => {"$table1.$key1" => 1}
3038
+);
3039
+is_deeply($result->one,
3040
+          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
3041
+is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
3042
+
3043
+test 'model method';
3044
+$dbi = DBIx::Custom->connect;
3045
+eval { $dbi->execute("drop table $table2") };
3046
+$dbi->execute($create_table2);
3047
+$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3048
+$model = $dbi->create_model(
3049
+    table => $table2
3050
+);
3051
+$model->method(foo => sub { shift->select(@_) });
3052
+is_deeply($model->foo->one, {$key1 => 1, $key3 => 3});
3053
+
3054
+test 'update_param';
3055
+$dbi = DBIx::Custom->connect;
3056
+eval { $dbi->execute("drop table $table1") };
3057
+$dbi->execute($create_table1_2);
3058
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3059
+$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3060
+
3061
+$param = {$key2 => 11};
3062
+$update_param = $dbi->update_param($param);
3063
+$sql = <<"EOS";
3064
+update $table1 $update_param
3065
+where $key1 = 1
3066
+EOS
3067
+$dbi->execute($sql, param => $param);
3068
+$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3069
+$rows   = $result->all;
3070
+is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3071
+                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3072
+                  "basic");
3073
+
3074
+
3075
+$dbi = DBIx::Custom->connect;
3076
+eval { $dbi->execute("drop table $table1") };
3077
+$dbi->execute($create_table1_2);
3078
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3079
+$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3080
+
3081
+$param = {$key2 => 11, $key3 => 33};
3082
+$update_param = $dbi->update_param($param);
3083
+$sql = <<"EOS";
3084
+update $table1 $update_param
3085
+where $key1 = 1
3086
+EOS
3087
+$dbi->execute($sql, param => $param);
3088
+$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3089
+$rows   = $result->all;
3090
+is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5},
3091
+                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3092
+                  "basic");
3093
+
3094
+$dbi = DBIx::Custom->connect;
3095
+eval { $dbi->execute("drop table $table1") };
3096
+$dbi->execute($create_table1_2);
3097
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3098
+$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3099
+
3100
+$param = {$key2 => 11, $key3 => 33};
3101
+$update_param = $dbi->update_param($param, {no_set => 1});
3102
+$sql = <<"EOS";
3103
+update $table1 set $update_param
3104
+where $key1 = 1
3105
+EOS
3106
+$dbi->execute($sql, param => $param);
3107
+$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3108
+$rows   = $result->all;
3109
+is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5},
3110
+                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3111
+                  "update param no_set");
3112
+
3113
+            
3114
+eval { $dbi->update_param({";" => 1}) };
3115
+like($@, qr/not safety/);
3116
+
3372 3117
 
3373
-eval{$builder->build_query("{in }")};
3374
-like($@, qr/Column name and count of values must be specified in tag "{in }"/, "in : key not exist");
3118
+test 'update_param';
3119
+$dbi = DBIx::Custom->connect;
3120
+eval { $dbi->execute("drop table $table1") };
3121
+$dbi->execute($create_table1_2);
3122
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3123
+$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3375 3124
 
3376
-eval{$builder->build_query("{in a}")};
3377
-like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/,
3378
-     "in : key not exist");
3125
+$param = {$key2 => 11};
3126
+$update_param = $dbi->assign_param($param);
3127
+$sql = <<"EOS";
3128
+update $table1 set $update_param
3129
+where $key1 = 1
3130
+EOS
3131
+$dbi->execute($sql, param => $param, table => $table1);
3132
+$result = $dbi->execute("select * from $table1 order by $key1");
3133
+$rows   = $result->all;
3134
+is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3135
+                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3136
+                  "basic");
3379 3137
 
3380
-eval{$builder->build_query("{in a r}")};
3381
-like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/,
3382
-     "in : key not exist");
3138
+test 'join';
3139
+$dbi = DBIx::Custom->connect;
3140
+eval { $dbi->execute("drop table $table1") };
3141
+$dbi->execute($create_table1);
3142
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3143
+$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
3144
+eval { $dbi->execute("drop table $table2") };
3145
+$dbi->execute($create_table2);
3146
+$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3147
+eval { $dbi->execute("drop table $table3") };
3148
+$dbi->execute("create table $table3 ($key3 int, $key4 int)");
3149
+$dbi->insert(table => $table3, param => {$key3 => 5, $key4 => 4});
3150
+$rows = $dbi->select(
3151
+    table => $table1,
3152
+    column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
3153
+    where   => {"$table1.$key2" => 2},
3154
+    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
3155
+)->all;
3156
+is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}]);
3383 3157
 
3384
-test 'variouse source';
3385
-$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d";
3386
-$query = $builder->build_query($source);
3387
-is($query->sql, 'a b = ? c { } { = ? } = ? d', "basic : 1");
3158
+$rows = $dbi->select(
3159
+    table => $table1,
3160
+    where   => {$key1 => 1},
3161
+    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
3162
+)->all;
3163
+is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
3388 3164
 
3389
-$source = "abc";
3390
-$query = $builder->build_query($source);
3391
-is($query->sql, 'abc', "basic : 2");
3165
+eval {
3166
+    $rows = $dbi->select(
3167
+        table => $table1,
3168
+        column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
3169
+        where   => {"$table1.$key2" => 2},
3170
+        join  => {"$table1.$key1" => "$table2.$key1"}
3171
+    );
3172
+};
3173
+like ($@, qr/array/);
3392 3174
 
3393
-$source = "{= a}";
3394
-$query = $builder->build_query($source);
3395
-is($query->sql, 'a = ?', "only tag");
3175
+$rows = $dbi->select(
3176
+    table => $table1,
3177
+    where   => {$key1 => 1},
3178
+    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3179
+              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3180
+)->all;
3181
+is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
3396 3182
 
3397
-$source = "000";
3398
-$query = $builder->build_query($source);
3399
-is($query->sql, '000', "contain 0 value");
3183
+$rows = $dbi->select(
3184
+    column => "$table3.$key4 as ${table3}__$key4",
3185
+    table => $table1,
3186
+    where   => {"$table1.$key1" => 1},
3187
+    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3188
+              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3189
+)->all;
3190
+is_deeply($rows, [{"${table3}__$key4" => 4}]);
3400 3191
 
3401
-$source = "a {= b} }";
3402
-eval{$builder->build_query($source)};
3403
-like($@, qr/unexpected "}"/, "error : 1");
3192
+$rows = $dbi->select(
3193
+    column => "$table1.$key1 as ${table1}__$key1",
3194
+    table => $table1,
3195
+    where   => {"$table3.$key4" => 4},
3196
+    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3197
+              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3198
+)->all;
3199
+is_deeply($rows, [{"${table1}__$key1" => 1}]);
3200
+
3201
+$dbi = DBIx::Custom->connect;
3202
+eval { $dbi->execute("drop table $table1") };
3203
+$dbi->execute($create_table1);
3204
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3205
+eval { $dbi->execute("drop table $table2") };
3206
+$dbi->execute($create_table2);
3207
+$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3208
+$rows = $dbi->select(
3209
+    table => $table1,
3210
+    column => "${q}$table1$p.${q}$key1$p as ${q}${table1}_$key1$p, ${q}$table2$p.${q}$key1$p as ${q}${table2}_$key1$p, ${q}$key2$p, ${q}$key3$p",
3211
+    where   => {"$table1.$key2" => 2},
3212
+    join  => ["left outer join ${q}$table2$p on ${q}$table1$p.${q}$key1$p = ${q}$table2$p.${q}$key1$p"],
3213
+)->all;
3214
+is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}],
3215
+          'quote');
3404 3216
 
3405
-$source = "a {= {}";
3406
-eval{$builder->build_query($source)};
3407
-like($@, qr/unexpected "{"/, "error : 2");
3408 3217
 
3409
-test 'select() wrap option';
3410 3218
 $dbi = DBIx::Custom->connect;
3411 3219
 eval { $dbi->execute("drop table $table1") };
3412 3220
 $dbi->execute($create_table1);
3413 3221
 $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3414
-$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
3222
+$sql = <<"EOS";
3223
+left outer join (
3224
+  select * from $table1 as t1
3225
+  where t1.$key2 = (
3226
+    select max(t2.$key2) from $table1 as t2
3227
+    where t1.$key1 = t2.$key1
3228
+  )
3229
+) as latest_$table1 on $table1.$key1 = latest_$table1.$key1
3230
+EOS
3231
+$join = [$sql];
3415 3232
 $rows = $dbi->select(
3416 3233
     table => $table1,
3417
-    column => $key1,
3418
-    wrap => ["select * from (", ") as t where $key1 = 1"]
3234
+    column => "latest_$table1.$key1 as latest_${table1}__$key1",
3235
+    join  => $join
3419 3236
 )->all;
3420
-is_deeply($rows, [{$key1 => 1}]);
3237
+is_deeply($rows, [{"latest_${table1}__$key1" => 1}]);
3421 3238
 
3422
-eval {
3423
-$dbi->select(
3239
+$dbi = DBIx::Custom->connect;
3240
+eval { $dbi->execute("drop table $table1") };
3241
+eval { $dbi->execute("drop table $table2") };
3242
+$dbi->execute($create_table1);
3243
+$dbi->execute($create_table2);
3244
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3245
+$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
3246
+$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3247
+$result = $dbi->select(
3424 3248
     table => $table1,
3425
-    column => $key1,
3426
-    wrap => 'select * from ('
3427
-)
3428
-};
3429
-like($@, qr/array/);
3249
+    join => [
3250
+        "left outer join $table2 on $table2.$key2 = '4' and $table1.$key1 = $table2.$key1"
3251
+    ]
3252
+);
3253
+is_deeply($result->all, [{$key1 => 1, $key2 => 2}]);
3254
+$result = $dbi->select(
3255
+    table => $table1,
3256
+    column => [{$table2 => [$key3]}],
3257
+    join => [
3258
+        "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1"
3259
+    ]
3260
+);
3261
+is_deeply($result->all, [{"$table2.$key3" => 4}]);
3262
+$result = $dbi->select(
3263
+    table => $table1,
3264
+    column => [{$table2 => [$key3]}],
3265
+    join => [
3266
+        "left outer join $table2 on $table1.$key1 = $table2.$key1 and $table2.$key3 = '4'"
3267
+    ]
3268
+);
3269
+is_deeply($result->all, [{"$table2.$key3" => 4}]);
3270
+
3271
+$dbi = DBIx::Custom->connect;
3272
+eval { $dbi->execute("drop table $table1") };
3273
+eval { $dbi->execute("drop table $table2") };
3274
+$dbi->execute($create_table1);
3275
+$dbi->execute($create_table2);
3276
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3277
+$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
3278
+$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3279
+$result = $dbi->select(
3280
+    table => $table1,
3281
+    column => [{$table2 => [$key3]}],
3282
+    join => [
3283
+        {
3284
+            clause => "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1",
3285
+            table => [$table1, $table2]
3286
+        }
3287
+    ]
3288
+);
3289
+is_deeply($result->all, [{"$table2.$key3" => 4}]);
3290
+
3291
+test 'Model class';
3292
+use MyDBI1;
3293
+$dbi = MyDBI1->connect;
3294
+eval { $dbi->execute("drop table $table1") };
3295
+$dbi->execute($create_table1);
3296
+$model = $dbi->model($table1);
3297
+$model->insert({$key1 => 'a', $key2 => 'b'});
3298
+is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3299
+eval { $dbi->execute("drop table $table2") };
3300
+$dbi->execute($create_table2);
3301
+$model = $dbi->model($table2);
3302
+$model->insert({$key1 => 'a'});
3303
+is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3304
+is($dbi->models->{$table1}, $dbi->model($table1));
3305
+is($dbi->models->{$table2}, $dbi->model($table2));
3306
+
3307
+$dbi = MyDBI4->connect;
3308
+eval { $dbi->execute("drop table $table1") };
3309
+$dbi->execute($create_table1);
3310
+$model = $dbi->model($table1);
3311
+$model->insert({$key1 => 'a', $key2 => 'b'});
3312
+is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3313
+eval { $dbi->execute("drop table $table2") };
3314
+$dbi->execute($create_table2);
3315
+$model = $dbi->model($table2);
3316
+$model->insert({$key1 => 'a'});
3317
+is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3318
+
3319
+$dbi = MyDBI5->connect;
3320
+eval { $dbi->execute("drop table $table1") };
3321
+eval { $dbi->execute("drop table $table2") };
3322
+$dbi->execute($create_table1);
3323
+$dbi->execute($create_table2);
3324
+$model = $dbi->model($table2);
3325
+$model->insert({$key1 => 'a'});
3326
+is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'include all model');
3327
+$dbi->insert(table => $table1, param => {$key1 => 1});
3328
+$model = $dbi->model($table1);
3329
+is_deeply($model->list->all, [{$key1 => 1, $key2 => undef}], 'include all model');
3330
+
3331
+test 'primary_key';
3332
+use MyDBI1;
3333
+$dbi = MyDBI1->connect;
3334
+$model = $dbi->model($table1);
3335
+$model->primary_key([$key1, $key2]);
3336
+is_deeply($model->primary_key, [$key1, $key2]);
3337
+
3338
+test 'columns';
3339
+use MyDBI1;
3340
+$dbi = MyDBI1->connect;
3341
+$model = $dbi->model($table1);
3342
+$model->columns([$key1, $key2]);
3343
+is_deeply($model->columns, [$key1, $key2]);
3430 3344
 
3431
-test 'select() sqlfilter option';
3432
-$dbi = DBIx::Custom->connect;
3345
+test 'setup_model';
3346
+use MyDBI1;
3347
+$dbi = MyDBI1->connect;
3433 3348
 eval { $dbi->execute("drop table $table1") };
3434
-$dbi->execute($create_table1);
3435
-$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3436
-$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
3437
-$rows = $dbi->select(
3438
-    table => $table1,
3439
-    column => $key1,
3440
-    sqlfilter => sub {
3441
-        my $sql = shift;
3442
-        $sql = "select * from ( $sql ) as t where $key1 = 1";
3443
-        return $sql;
3444
-    }
3445
-)->all;
3446
-is_deeply($rows, [{$key1 => 1}]);
3349
+eval { $dbi->execute("drop table $table2") };
3447 3350
 
3448
-test 'dbi method from model';
3449
-$dbi = MyDBI9->connect;
3450
-eval { $dbi->execute("drop table $table1") };
3451 3351
 $dbi->execute($create_table1);
3352
+$dbi->execute($create_table2);
3452 3353
 $dbi->setup_model;
3453
-$model = $dbi->model($table1);
3454
-eval{$model->execute("select * from $table1")};
3455
-ok(!$@);
3354
+is_deeply([sort @{$dbi->model($table1)->columns}], [$key1, $key2]);
3355
+is_deeply([sort @{$dbi->model($table2)->columns}], [$key1, $key3]);
3456 3356
 
3457
-test 'column table option';
3458
-$dbi = MyDBI9->connect;
3357
+test 'each_column';
3358
+$dbi = DBIx::Custom->connect;
3359
+eval { $dbi->execute("drop table ${q}table$p") };
3459 3360
 eval { $dbi->execute("drop table $table1") };
3460
-$dbi->execute($create_table1);
3461 3361
 eval { $dbi->execute("drop table $table2") };
3362
+eval { $dbi->execute("drop table $table3") };
3363
+$dbi->execute($create_table1_type);
3462 3364
 $dbi->execute($create_table2);
3463
-$dbi->setup_model;
3464
-$dbi->execute("insert into $table1 ($key1, $key2) values (1, 2)");
3465
-$dbi->execute("insert into $table2 ($key1, $key3) values (1, 4)");
3466
-$model = $dbi->model($table1);
3467
-$result = $model->select(
3468
-    column => [
3469
-        $model->column($table2, {alias => $table2_alias})
3470
-    ],
3471
-    where => {"$table2_alias.$key3" => 4}
3365
+
3366
+$infos = [];
3367
+$dbi->each_column(sub {
3368
+    my ($self, $table, $column, $cinfo) = @_;
3369
+    
3370
+    if ($table =~ /^table\d/i) {
3371
+         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
3372
+         push @$infos, $info;
3373
+    }
3374
+});
3375
+$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3376
+$DB::single = 1;
3377
+is_deeply($infos, 
3378
+    [
3379
+        [$table1, $key1, $key1],
3380
+        [$table1, $key2, $key2],
3381
+        [$table2, $key1, $key1],
3382
+        [$table2, $key3, $key3]
3383
+    ]
3384
+    
3472 3385
 );
3473
-is_deeply($result->one, 
3474
-          {"$table2_alias.$key1" => 1, "$table2_alias.$key3" => 4});
3386
+test 'each_table';
3387
+$dbi = DBIx::Custom->connect;
3388
+eval { $dbi->execute("drop table $table1") };
3389
+eval { $dbi->execute("drop table $table2") };
3390
+$dbi->execute($create_table2);
3391
+$dbi->execute($create_table1_type);
3475 3392
 
3476
-$dbi->separator('__');
3477
-$result = $model->select(
3478
-    column => [
3479
-        $model->column($table2, {alias => $table2_alias})
3480
-    ],
3481
-    where => {"$table2_alias.$key3" => 4}
3393
+$infos = [];
3394
+$dbi->each_table(sub {
3395
+    my ($self, $table, $table_info) = @_;
3396
+    
3397
+    if ($table =~ /^table\d/i) {
3398
+         my $info = [$table, $table_info->{TABLE_NAME}];
3399
+         push @$infos, $info;
3400
+    }
3401
+});
3402
+$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3403
+is_deeply($infos, 
3404
+    [
3405
+        [$table1, $table1],
3406
+        [$table2, $table2],
3407
+    ]
3482 3408
 );
3483
-is_deeply($result->one, 
3484
-          {"${table2_alias}__$key1" => 1, "${table2_alias}__$key3" => 4});
3485 3409
 
3486
-$dbi->separator('-');
3487
-$result = $model->select(
3488
-    column => [
3489
-        $model->column($table2, {alias => $table2_alias})
3490
-    ],
3491
-    where => {"$table2_alias.$key3" => 4}
3410
+test 'type_rule into';
3411
+eval { $dbi->execute("drop table $table1") };
3412
+$dbi->execute($create_table1_type);
3413
+$dbi = DBIx::Custom->connect;
3414
+eval { $dbi->execute("drop table $table1") };
3415
+$dbi->execute($create_table1_type);
3416
+
3417
+$dbi->type_rule(
3418
+    into1 => {
3419
+        $date_typename => sub { '2010-' . $_[0] }
3420
+    }
3492 3421
 );
3493
-is_deeply($result->one, 
3494
-          {"$table2_alias-$key1" => 1, "$table2_alias-$key3" => 4});
3422
+$dbi->insert({$key1 => '01-01'}, table => $table1);
3423
+$result = $dbi->select(table => $table1);
3424
+like($result->one->{$key1}, qr/^2010-01-01/);
3495 3425
 
3496
-test 'create_model';
3497 3426
 $dbi = DBIx::Custom->connect;
3498 3427
 eval { $dbi->execute("drop table $table1") };
3499
-eval { $dbi->execute("drop table $table2") };
3500
-$dbi->execute($create_table1);
3501
-$dbi->execute($create_table2);
3428
+$dbi->execute($create_table1_type);
3429
+$dbi->type_rule(
3430
+    into1 => [
3431
+         [$date_typename, $datetime_typename] => sub {
3432
+            my $value = shift;
3433
+            $value =~ s/02/03/g;
3434
+            return $value;
3435
+         }
3436
+    ]
3437
+);
3438
+$dbi->insert({$key1 => '2010-01-02', $key2 => '2010-01-01 01:01:02'}, table => $table1);
3439
+$result = $dbi->select(table => $table1);
3440
+$row = $result->one;
3441
+like($row->{$key1}, qr/^2010-01-03/);
3442
+like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3502 3443
 
3503
-$dbi->create_model(
3504
-    table => $table1,
3505
-    join => [
3506
-       "left outer join $table2 on $table1.$key1 = $table2.$key1"
3507
-    ],
3508
-    primary_key => [$key1]
3444
+$dbi = DBIx::Custom->connect;
3445
+eval { $dbi->execute("drop table $table1") };
3446
+$dbi->execute($create_table1_type);
3447
+$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
3448
+$dbi->type_rule(
3449
+    into1 => [
3450
+        [$date_typename, $datetime_typename] => sub {
3451
+            my $value = shift;
3452
+            $value =~ s/02/03/g;
3453
+            return $value;
3454
+        }
3455
+    ]
3509 3456
 );
3510
-$model2 = $dbi->create_model(
3511
-    table => $table2
3457
+$result = $dbi->execute(
3458
+    "select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2",
3459
+    param => {$key1 => '2010-01-03', "$table1.$key2" => '2010-01-01 01:01:02'}
3512 3460
 );
3513
-$dbi->create_model(
3514
-    table => $table3,
3515
-    filter => [
3516
-        $key1 => {in => sub { uc $_[0] }}
3461
+$row = $result->one;
3462
+like($row->{$key1}, qr/^2010-01-03/);
3463
+like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3464
+
3465
+$dbi = DBIx::Custom->connect;
3466
+eval { $dbi->execute("drop table $table1") };
3467
+$dbi->execute($create_table1_type);
3468
+$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
3469
+$dbi->type_rule(
3470
+    into1 => [
3471
+        [$date_typename, $datetime_typename] => sub {
3472
+            my $value = shift;
3473
+            $value =~ s/02/03/g;
3474
+            return $value;
3475
+        }
3517 3476
     ]
3518 3477
 );
3519
-$dbi->setup_model;
3520
-$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3521
-$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3522
-$model = $dbi->model($table1);
3523
-$result = $model->select(
3524
-    column => [$model->mycolumn, $model->column($table2)],
3525
-    where => {"$table1.$key1" => 1}
3478
+$result = $dbi->execute(
3479
+    "select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2",
3480
+    param => {$key1 => '2010-01-02', "$table1.$key2" => '2010-01-01 01:01:02'},
3481
+    table => $table1
3482
+);
3483
+$row = $result->one;
3484
+like($row->{$key1}, qr/^2010-01-03/);
3485
+like($row->{$key2}, qr/2010-01-01 01:01:03/);
3486
+
3487
+$dbi = DBIx::Custom->connect;
3488
+eval { $dbi->execute("drop table $table1") };
3489
+$dbi->execute($create_table1_type);
3490
+$dbi->register_filter(convert => sub {
3491
+    my $value = shift || '';
3492
+    $value =~ s/02/03/;
3493
+    return $value;
3494
+});
3495
+$dbi->type_rule(
3496
+    from1 => {
3497
+        $date_datatype => 'convert',
3498
+    },
3499
+    into1 => {
3500
+        $date_typename => 'convert',
3501
+    }
3502
+);
3503
+$dbi->insert({$key1 => '2010-02-02'}, table => $table1);
3504
+$result = $dbi->select(table => $table1);
3505
+like($result->fetch->[0], qr/^2010-03-03/);
3506
+
3507
+test 'type_rule and filter order';
3508
+$dbi = DBIx::Custom->connect;
3509
+eval { $dbi->execute("drop table $table1") };
3510
+$dbi->execute($create_table1_type);
3511
+$dbi->type_rule(
3512
+    into1 => {
3513
+        $date_typename => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3514
+    },
3515
+    into2 => {
3516
+        $date_typename => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
3517
+    },
3518
+    from1 => {
3519
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
3520
+    },
3521
+    from2 => {
3522
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/7/8/; return $v }
3523
+    }
3524
+);
3525
+$dbi->insert({$key1 => '2010-01-03'}, 
3526
+  table => $table1, filter => {$key1 => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }});
3527
+$result = $dbi->select(table => $table1);
3528
+$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3529
+like($result->fetch_first->[0], qr/^2010-01-09/);
3530
+
3531
+
3532
+$dbi = DBIx::Custom->connect;
3533
+eval { $dbi->execute("drop table $table1") };
3534
+$dbi->execute($create_table1_type);
3535
+$dbi->type_rule(
3536
+    from1 => {
3537
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3538
+    },
3539
+    from2 => {
3540
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3541
+    },
3542
+);
3543
+$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3544
+$result = $dbi->select(table => $table1);
3545
+$result->type_rule(
3546
+    from1 => {
3547
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
3548
+    },
3549
+    from2 => {
3550
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/8/; return $v }
3551
+    }
3526 3552
 );
3527
-is_deeply($result->one,
3528
-          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
3529
-is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
3553
+$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3554
+like($result->fetch_first->[0], qr/^2010-01-09/);
3530 3555
 
3531
-test 'model method';
3556
+test 'type_rule_off';
3532 3557
 $dbi = DBIx::Custom->connect;
3533
-eval { $dbi->execute("drop table $table2") };
3534
-$dbi->execute($create_table2);
3535
-$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3536
-$model = $dbi->create_model(
3537
-    table => $table2
3558
+eval { $dbi->execute("drop table $table1") };
3559
+$dbi->execute($create_table1_type);
3560
+$dbi->type_rule(
3561
+    from1 => {
3562
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3563
+    },
3564
+    into1 => {
3565
+        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3566
+    }
3538 3567
 );
3539
-$model->method(foo => sub { shift->select(@_) });
3540
-is_deeply($model->foo->one, {$key1 => 1, $key3 => 3});
3568
+$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3569
+$result = $dbi->select(table => $table1, type_rule_off => 1);
3570
+like($result->type_rule_off->fetch->[0], qr/^2010-01-03/);
3541 3571
 
3542
-test 'update_param';
3543 3572
 $dbi = DBIx::Custom->connect;
3544 3573
 eval { $dbi->execute("drop table $table1") };
3545
-$dbi->execute($create_table1_2);
3546
-$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3547
-$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3548
-
3549
-$param = {$key2 => 11};
3550
-$update_param = $dbi->update_param($param);
3551
-$sql = <<"EOS";
3552
-update $table1 $update_param
3553
-where $key1 = 1
3554
-EOS
3555
-$dbi->execute($sql, param => $param);
3556
-$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3557
-$rows   = $result->all;
3558
-is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3559
-                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3560
-                  "basic");
3561
-
3574
+$dbi->execute($create_table1_type);
3575
+$dbi->type_rule(
3576
+    from1 => {
3577
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3578
+    },
3579
+    into1 => {
3580
+        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3581
+    }
3582
+);
3583
+$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3584
+$result = $dbi->select(table => $table1, type_rule_off => 1);
3585
+like($result->one->{$key1}, qr/^2010-01-04/);
3562 3586
 
3563 3587
 $dbi = DBIx::Custom->connect;
3564 3588
 eval { $dbi->execute("drop table $table1") };
3565
-$dbi->execute($create_table1_2);
3566
-$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3567
-$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3568
-
3569
-$param = {$key2 => 11, $key3 => 33};
3570
-$update_param = $dbi->update_param($param);
3571
-$sql = <<"EOS";
3572
-update $table1 $update_param
3573
-where $key1 = 1
3574
-EOS
3575
-$dbi->execute($sql, param => $param);
3576
-$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3577
-$rows   = $result->all;
3578
-is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5},
3579
-                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3580
-                  "basic");
3589
+$dbi->execute($create_table1_type);
3590
+$dbi->type_rule(
3591
+    from1 => {
3592
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3593
+    },
3594
+    into1 => {
3595
+        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3596
+    }
3597
+);
3598
+$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3599
+$result = $dbi->select(table => $table1);
3600
+like($result->one->{$key1}, qr/^2010-01-05/);
3581 3601
 
3582 3602
 $dbi = DBIx::Custom->connect;
3583 3603
 eval { $dbi->execute("drop table $table1") };
3584
-$dbi->execute($create_table1_2);
3585
-$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3586
-$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3587
-
3588
-$param = {$key2 => 11, $key3 => 33};
3589
-$update_param = $dbi->update_param($param, {no_set => 1});
3590
-$sql = <<"EOS";
3591
-update $table1 set $update_param
3592
-where $key1 = 1
3593
-EOS
3594
-$dbi->execute($sql, param => $param);
3595
-$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3596
-$rows   = $result->all;
3597
-is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5},
3598
-                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3599
-                  "update param no_set");
3600
-
3601
-            
3602
-eval { $dbi->update_param({";" => 1}) };
3603
-like($@, qr/not safety/);
3604
-
3604
+$dbi->execute($create_table1_type);
3605
+$dbi->type_rule(
3606
+    from1 => {
3607
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3608
+    },
3609
+    into1 => {
3610
+        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3611
+    }
3612
+);
3613
+$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3614
+$result = $dbi->select(table => $table1);
3615
+like($result->fetch->[0], qr/2010-01-05/);
3605 3616
 
3606
-test 'update_param';
3607 3617
 $dbi = DBIx::Custom->connect;
3608 3618
 eval { $dbi->execute("drop table $table1") };
3609
-$dbi->execute($create_table1_2);
3610
-$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3611
-$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3619
+$dbi->execute($create_table1_type);
3620
+$dbi->register_filter(ppp => sub { my $v = shift || ''; $v =~ s/3/4/; return $v });
3621
+$dbi->type_rule(
3622
+    into1 => {
3623
+        $date_typename => 'ppp'
3624
+    }
3625
+);
3626
+$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3627
+$result = $dbi->select(table => $table1);
3628
+like($result->one->{$key1}, qr/^2010-01-04/);
3612 3629
 
3613
-$param = {$key2 => 11};
3614
-$update_param = $dbi->assign_param($param);
3615
-$sql = <<"EOS";
3616
-update $table1 set $update_param
3617
-where $key1 = 1
3618
-EOS
3619
-$dbi->execute($sql, param => $param, table => $table1);
3620
-$result = $dbi->execute("select * from $table1 order by $key1");
3621
-$rows   = $result->all;
3622
-is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3623
-                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3624
-                  "basic");
3630
+eval{$dbi->type_rule(
3631
+    into1 => {
3632
+        $date_typename => 'pp'
3633
+    }
3634
+)};
3635
+like($@, qr/not registered/);
3625 3636
 
3626
-test 'join';
3627 3637
 $dbi = DBIx::Custom->connect;
3628 3638
 eval { $dbi->execute("drop table $table1") };
3629
-$dbi->execute($create_table1);
3630
-$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3631
-$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
3632
-eval { $dbi->execute("drop table $table2") };
3633
-$dbi->execute($create_table2);
3634
-$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3635
-eval { $dbi->execute("drop table $table3") };
3636
-$dbi->execute("create table $table3 ($key3 int, $key4 int)");
3637
-$dbi->insert(table => $table3, param => {$key3 => 5, $key4 => 4});
3638
-$rows = $dbi->select(
3639
-    table => $table1,
3640
-    column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
3641
-    where   => {"$table1.$key2" => 2},
3642
-    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
3643
-)->all;
3644
-is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}]);
3645
-
3646
-$rows = $dbi->select(
3647
-    table => $table1,
3648
-    where   => {$key1 => 1},
3649
-    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
3650
-)->all;
3651
-is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
3652
-
3639
+$dbi->execute($create_table1_type);
3653 3640
 eval {
3654
-    $rows = $dbi->select(
3655
-        table => $table1,
3656
-        column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
3657
-        where   => {"$table1.$key2" => 2},
3658
-        join  => {"$table1.$key1" => "$table2.$key1"}
3641
+    $dbi->type_rule(
3642
+        from1 => {
3643
+            Date => sub { $_[0] * 2 },
3644
+        }
3659 3645
     );
3660 3646
 };
3661
-like ($@, qr/array/);
3662
-
3663
-$rows = $dbi->select(
3664
-    table => $table1,
3665
-    where   => {$key1 => 1},
3666
-    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3667
-              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3668
-)->all;
3669
-is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
3670
-
3671
-$rows = $dbi->select(
3672
-    column => "$table3.$key4 as ${table3}__$key4",
3673
-    table => $table1,
3674
-    where   => {"$table1.$key1" => 1},
3675
-    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3676
-              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3677
-)->all;
3678
-is_deeply($rows, [{"${table3}__$key4" => 4}]);
3679
-
3680
-$rows = $dbi->select(
3681
-    column => "$table1.$key1 as ${table1}__$key1",
3682
-    table => $table1,
3683
-    where   => {"$table3.$key4" => 4},
3684
-    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3685
-              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3686
-)->all;
3687
-is_deeply($rows, [{"${table1}__$key1" => 1}]);
3688
-
3689
-$dbi = DBIx::Custom->connect;
3690
-eval { $dbi->execute("drop table $table1") };
3691
-$dbi->execute($create_table1);
3692
-$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3693
-eval { $dbi->execute("drop table $table2") };
3694
-$dbi->execute($create_table2);
3695
-$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3696
-$rows = $dbi->select(
3697
-    table => $table1,
3698
-    column => "${q}$table1$p.${q}$key1$p as ${q}${table1}_$key1$p, ${q}$table2$p.${q}$key1$p as ${q}${table2}_$key1$p, ${q}$key2$p, ${q}$key3$p",
3699
-    where   => {"$table1.$key2" => 2},
3700
-    join  => ["left outer join ${q}$table2$p on ${q}$table1$p.${q}$key1$p = ${q}$table2$p.${q}$key1$p"],
3701
-)->all;
3702
-is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}],
3703
-          'quote');
3647
+like($@, qr/lower/);
3704 3648
 
3649
+eval {
3650
+    $dbi->type_rule(
3651
+        into1 => {
3652
+            Date => sub { $_[0] * 2 },
3653
+        }
3654
+    );
3655
+};
3656
+like($@, qr/lower/);
3705 3657
 
3706 3658
 $dbi = DBIx::Custom->connect;
3707 3659
 eval { $dbi->execute("drop table $table1") };
3708
-$dbi->execute($create_table1);
3709
-$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3710
-$sql = <<"EOS";
3711
-left outer join (
3712
-  select * from $table1 as t1
3713
-  where t1.$key2 = (
3714
-    select max(t2.$key2) from $table1 as t2
3715
-    where t1.$key1 = t2.$key1
3716
-  )
3717
-) as latest_$table1 on $table1.$key1 = latest_$table1.$key1
3718
-EOS
3719
-$join = [$sql];
3720
-$rows = $dbi->select(
3721
-    table => $table1,
3722
-    column => "latest_$table1.$key1 as latest_${table1}__$key1",
3723
-    join  => $join
3724
-)->all;
3725
-is_deeply($rows, [{"latest_${table1}__$key1" => 1}]);
3660
+$dbi->execute($create_table1_type);
3661
+$dbi->type_rule(
3662
+    from1 => {
3663
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3664
+    },
3665
+    into1 => {
3666
+        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3667
+    }
3668
+);
3669
+$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3670
+$result = $dbi->select(table => $table1);
3671
+$result->type_rule_off;
3672
+like($result->one->{$key1}, qr/^2010-01-04/);
3726 3673
 
3727 3674
 $dbi = DBIx::Custom->connect;
3728 3675
 eval { $dbi->execute("drop table $table1") };
3729
-eval { $dbi->execute("drop table $table2") };
3730
-$dbi->execute($create_table1);
3731
-$dbi->execute($create_table2);
3732
-$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3733
-$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
3734
-$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3735
-$result = $dbi->select(
3736
-    table => $table1,
3737
-    join => [
3738
-        "left outer join $table2 on $table2.$key2 = '4' and $table1.$key1 = $table2.$key1"
3739
-    ]
3676
+$dbi->execute($create_table1_type);
3677
+$dbi->type_rule(
3678
+    from1 => {
3679
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v },
3680
+        $datetime_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3681
+    },
3740 3682
 );
3741
-is_deeply($result->all, [{$key1 => 1, $key2 => 2}]);
3742
-$result = $dbi->select(
3743
-    table => $table1,
3744
-    column => [{$table2 => [$key3]}],
3745
-    join => [
3746
-        "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1"
3747
-    ]
3683
+$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
3684
+$result = $dbi->select(table => $table1);
3685
+$result->type_rule(
3686
+    from1 => {
3687
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3688
+    }
3748 3689
 );
3749
-is_deeply($result->all, [{"$table2.$key3" => 4}]);
3750
-$result = $dbi->select(
3751
-    table => $table1,
3752
-    column => [{$table2 => [$key3]}],
3753
-    join => [
3754
-        "left outer join $table2 on $table1.$key1 = $table2.$key1 and $table2.$key3 = '4'"
3755
-    ]
3690
+$row = $result->one;
3691
+like($row->{$key1}, qr/^2010-01-05/);
3692
+like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3693
+
3694
+$result = $dbi->select(table => $table1);
3695
+$result->type_rule(
3696
+    from1 => {
3697
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3698
+    }
3756 3699
 );
3757
-is_deeply($result->all, [{"$table2.$key3" => 4}]);
3700
+$row = $result->one;
3701
+like($row->{$key1}, qr/2010-01-05/);
3702
+like($row->{$key2}, qr/2010-01-01 01:01:03/);
3703
+
3704
+$result = $dbi->select(table => $table1);
3705
+$result->type_rule(
3706
+    from1 => {
3707
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3708
+    }
3709
+);
3710
+$row = $result->one;
3711
+like($row->{$key1}, qr/2010-01-05/);
3712
+like($row->{$key2}, qr/2010-01-01 01:01:03/);
3713
+
3714
+$result = $dbi->select(table => $table1);
3715
+$result->type_rule(
3716
+    from1 => [$date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }]
3717
+);
3718
+$row = $result->one;
3719
+like($row->{$key1}, qr/2010-01-05/);
3720
+like($row->{$key2}, qr/2010-01-01 01:01:03/);
3721
+
3722
+$dbi->register_filter(five => sub { my $v = shift || ''; $v =~ s/3/5/; return $v });
3723
+$result = $dbi->select(table => $table1);
3724
+$result->type_rule(
3725
+    from1 => [$date_datatype => 'five']
3726
+);
3727
+$row = $result->one;
3728
+like($row->{$key1}, qr/^2010-01-05/);
3729
+like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3730
+
3731
+$result = $dbi->select(table => $table1);
3732
+$result->type_rule(
3733
+    from1 => [$date_datatype => undef]
3734
+);
3735
+$row = $result->one;
3736
+like($row->{$key1}, qr/^2010-01-03/);
3737
+like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3758 3738
 
3759 3739
 $dbi = DBIx::Custom->connect;
3760 3740
 eval { $dbi->execute("drop table $table1") };
3761
-eval { $dbi->execute("drop table $table2") };
3762
-$dbi->execute($create_table1);
3763
-$dbi->execute($create_table2);
3764
-$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3765
-$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
3766
-$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3767
-$result = $dbi->select(
3768
-    table => $table1,
3769
-    column => [{$table2 => [$key3]}],
3770
-    join => [
3771
-        {
3772
-            clause => "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1",
3773
-            table => [$table1, $table2]
3774
-        }
3775
-    ]
3741
+$dbi->execute($create_table1_type);
3742
+$dbi->type_rule(
3743
+    from1 => {
3744
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v },
3745
+    },
3776 3746
 );
3777
-is_deeply($result->all, [{"$table2.$key3" => 4}]);
3747
+$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3748
+$result = $dbi->select(table => $table1);
3749
+$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/4/5/; return $v });
3750
+like($result->one->{$key1}, qr/^2010-01-05/);
3778 3751
 
3779
-test 'Model class';
3780
-use MyDBI1;
3781
-$dbi = MyDBI1->connect;
3752
+$dbi = DBIx::Custom->connect;
3782 3753
 eval { $dbi->execute("drop table $table1") };
3783
-$dbi->execute($create_table1);
3784
-$model = $dbi->model($table1);
3785
-$model->insert({$key1 => 'a', $key2 => 'b'});
3786
-is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3787
-eval { $dbi->execute("drop table $table2") };
3788
-$dbi->execute($create_table2);
3789
-$model = $dbi->model($table2);
3790
-$model->insert({$key1 => 'a'});
3791
-is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3792
-is($dbi->models->{$table1}, $dbi->model($table1));
3793
-is($dbi->models->{$table2}, $dbi->model($table2));
3754
+$dbi->execute($create_table1_type);
3755
+$dbi->type_rule(
3756
+    from1 => {
3757
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3758
+    },
3759
+);
3760
+$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3761
+$result = $dbi->select(table => $table1);
3762
+$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/4/5/; return $v });
3763
+like($result->fetch->[0], qr/^2010-01-05/);
3794 3764
 
3795
-$dbi = MyDBI4->connect;
3765
+$dbi = DBIx::Custom->connect;
3796 3766
 eval { $dbi->execute("drop table $table1") };
3797
-$dbi->execute($create_table1);
3798
-$model = $dbi->model($table1);
3799
-$model->insert({$key1 => 'a', $key2 => 'b'});
3800
-is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3801
-eval { $dbi->execute("drop table $table2") };
3802
-$dbi->execute($create_table2);
3803
-$model = $dbi->model($table2);
3804
-$model->insert({$key1 => 'a'});
3805
-is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3767
+$dbi->execute($create_table1_type);
3768
+$dbi->type_rule(
3769
+    into1 => {
3770
+        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3771
+    },
3772
+    into2 => {
3773
+        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3774
+    },
3775
+    from1 => {
3776
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
3777
+    },
3778
+    from2 => {
3779
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v }
3780
+    }
3781
+);
3782
+$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3783
+$result = $dbi->select(table => $table1);
3784
+like($result->type_rule_off->fetch_first->[0], qr/^2010-01-03/);
3785
+$result = $dbi->select(table => $table1);
3786
+like($result->type_rule_on->fetch_first->[0], qr/^2010-01-07/);
3806 3787
 
3807
-$dbi = MyDBI5->connect;
3788
+$dbi = DBIx::Custom->connect;
3808 3789
 eval { $dbi->execute("drop table $table1") };
3809
-eval { $dbi->execute("drop table $table2") };
3810
-$dbi->execute($create_table1);
3811
-$dbi->execute($create_table2);
3812
-$model = $dbi->model($table2);
3813
-$model->insert({$key1 => 'a'});
3814
-is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'include all model');
3815
-$dbi->insert(table => $table1, param => {$key1 => 1});
3816
-$model = $dbi->model($table1);
3817
-is_deeply($model->list->all, [{$key1 => 1, $key2 => undef}], 'include all model');
3818
-
3819
-test 'primary_key';
3820
-use MyDBI1;
3821
-$dbi = MyDBI1->connect;
3822
-$model = $dbi->model($table1);
3823
-$model->primary_key([$key1, $key2]);
3824
-is_deeply($model->primary_key, [$key1, $key2]);
3825
-
3826
-test 'columns';
3827
-use MyDBI1;
3828
-$dbi = MyDBI1->connect;
3829
-$model = $dbi->model($table1);
3830
-$model->columns([$key1, $key2]);
3831
-is_deeply($model->columns, [$key1, $key2]);
3790
+$dbi->execute($create_table1_type);
3791
+$dbi->type_rule(
3792
+    into1 => {
3793
+        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3794
+    },
3795
+    into2 => {
3796
+        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3797
+    },
3798
+    from1 => {
3799
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|5)/6/; return $v }
3800
+    },
3801
+    from2 => {
3802
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
3803
+    }
3804
+);
3805
+$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule1_off => 1);
3806
+$result = $dbi->select(table => $table1);
3807
+like($result->type_rule1_off->fetch_first->[0], qr/^2010-01-05/);
3808
+$result = $dbi->select(table => $table1);
3809
+like($result->type_rule1_on->fetch_first->[0], qr/^2010-01-07/);
3832 3810
 
3833
-test 'setup_model';
3834
-use MyDBI1;
3835
-$dbi = MyDBI1->connect;
3811
+$dbi = DBIx::Custom->connect;
3836 3812
 eval { $dbi->execute("drop table $table1") };
3837
-eval { $dbi->execute("drop table $table2") };
3838
-
3839
-$dbi->execute($create_table1);
3840
-$dbi->execute($create_table2);
3841
-$dbi->setup_model;
3842
-is_deeply([sort @{$dbi->model($table1)->columns}], [$key1, $key2]);
3843
-is_deeply([sort @{$dbi->model($table2)->columns}], [$key1, $key3]);
3844
-
3813
+$dbi->execute($create_table1_type);
3814
+$dbi->type_rule(
3815
+    into1 => {
3816
+        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3817
+    },
3818
+    into2 => {
3819
+        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3820
+    },
3821
+    from1 => {
3822
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
3823
+    },
3824
+    from2 => {
3825
+        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v }
3826
+    }
3827
+);
3828
+$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule2_off => 1);
3829
+$result = $dbi->select(table => $table1);
3830
+like($result->type_rule2_off->fetch_first->[0], qr/^2010-01-06/);
3831
+$result = $dbi->select(table => $table1);
3832
+like($result->type_rule2_on->fetch_first->[0], qr/^2010-01-07/);
3845 3833
 
3846 3834
 
3847 3835
 1;
+13
t/common/MyModel1/TABLE1.pm
... ...
@@ -0,0 +1,13 @@
1
+package MyModel1::TABLE1;
2
+
3
+use DBIx::Custom::Model -base;
4
+
5
+sub insert {
6
+    my ($self, $param) = @_;
7
+    
8
+    return $self->SUPER::insert(param => $param);
9
+}
10
+
11
+sub list { shift->select; }
12
+
13
+1;
+17
t/common/MyModel1/TABLE2.pm
... ...
@@ -0,0 +1,17 @@
1
+package MyModel1::TABLE2;
2
+
3
+use strict;
4
+use warnings;
5
+
6
+use base 'DBIx::Custom::Model';
7
+
8
+
9
+sub insert {
10
+    my ($self, $param) = @_;
11
+    
12
+    return $self->SUPER::insert(param => $param);
13
+}
14
+
15
+sub list { shift->select; }
16
+
17
+1;
+9
t/common/MyModel4/TABLE1.pm
... ...
@@ -0,0 +1,9 @@
1
+package MyModel4::table1;
2
+
3
+use base 'MyModel4';
4
+
5
+sub table { 'table1' }
6
+
7
+sub list { shift->select }
8
+
9
+1;
t/common/MyModel4/TABLE2.pm
No changes.
t/common/MyModel5/TABLE1.pm
No changes.
t/common/MyModel5/TABLE2.pm
No changes.
t/common/MyModel5/TABLE3.pm
No changes.
t/common/MyModel6/TABLE1.pm
No changes.
t/common/MyModel6/TABLE2.pm
No changes.
t/common/MyModel6/TABLE3.pm
No changes.
t/common/MyModel7/TABLE1.pm
No changes.
t/common/MyModel7/TABLE2.pm
No changes.
t/common/MyModel8/TABLE1.pm
No changes.
t/common/MyModel8/TABLE2.pm
No changes.
+29
t/create_uppercase_module.pl
... ...
@@ -0,0 +1,29 @@
1
+use strict;
2
+use warnings;
3
+
4
+use FindBin;
5
+use File::Basename 'fileparse';
6
+
7
+my @dirs = grep { -d $_ } glob("$FindBin::Bin/common/*");
8
+for my $dir (@dirs) {
9
+    my @files = grep { /table\d\.pm/ } glob("$dir/*");
10
+    for my $file (@files) {
11
+    
12
+      my $content = do {
13
+        open my $fh, '<', $file;
14
+        local $/;
15
+        <$fh>;
16
+      };
17
+      
18
+      $content =~ s/table(\d)/TABLE$1/g;
19
+      
20
+      my $base_name = (fileparse($file, qr/\..+$/))[0];
21
+      $base_name = uc $base_name;
22
+      my $new_file = "$dir/$base_name.pm";
23
+      
24
+      open my $fh, '>', $new_file
25
+        or die "Can't write file: $!";
26
+      
27
+      print $fh $content;
28
+    }
29
+}
+5
t/sqlite.t
... ...
@@ -42,6 +42,11 @@ my $binary;
42 42
 $dbi = DBIx::Custom->connect;
43 43
 
44 44
 ### SQLite only test
45
+test 'dbi_option default';
46
+$dbi = DBIx::Custom->new;
47
+is_deeply($dbi->dbi_option, {});
48
+
49
+
45 50
 test 'prefix';
46 51
 $dbi = DBIx::Custom->connect;
47 52
 eval { $dbi->execute('drop table table1') };