... | ... |
@@ -153,13 +153,14 @@ sub column { |
153 | 153 |
my $separator = $self->separator; |
154 | 154 |
|
155 | 155 |
# . is replaced |
156 |
- $table =~ s/\./$separator/g; |
|
156 |
+ my $t = $table; |
|
157 |
+ $t =~ s/\./$separator/g; |
|
157 | 158 |
|
158 | 159 |
# Column clause |
159 | 160 |
my @column; |
160 | 161 |
$columns ||= []; |
161 | 162 |
push @column, $self->_tq($table) . "." . $self->q($_) . |
162 |
- " as " . $self->q("${table}${separator}$_") |
|
163 |
+ " as " . $self->q("${t}${separator}$_") |
|
163 | 164 |
for @$columns; |
164 | 165 |
|
165 | 166 |
return join (', ', @column); |
... | ... |
@@ -290,7 +291,7 @@ sub create_model { |
290 | 291 |
|
291 | 292 |
sub each_column { |
292 | 293 |
my ($self, $cb, %options) = @_; |
293 |
- |
|
294 |
+ |
|
294 | 295 |
my $user_column_info = $self->user_column_info; |
295 | 296 |
|
296 | 297 |
if ($user_column_info) { |
... | ... |
@@ -790,7 +791,6 @@ sub include_model { |
790 | 791 |
if $@; |
791 | 792 |
|
792 | 793 |
# Search model modules |
793 |
- $DB::single = 1; |
|
794 | 794 |
my $path = $INC{"$name_space.pm"}; |
795 | 795 |
$path =~ s/\.pm$//; |
796 | 796 |
opendir my $dh, $path |
... | ... |
@@ -1113,12 +1113,16 @@ sub select { |
1113 | 1113 |
} |
1114 | 1114 |
|
1115 | 1115 |
sub setup_model { |
1116 |
- my $self = shift; |
|
1116 |
+ my ($self, %opt) = @_; |
|
1117 | 1117 |
|
1118 | 1118 |
# Setup model |
1119 | 1119 |
$self->each_column( |
1120 | 1120 |
sub { |
1121 | 1121 |
my ($self, $table, $column, $column_info) = @_; |
1122 |
+ my $database = $column_info->{TABLE_SCHEM}; |
|
1123 |
+ return if exists $opt{database} && $opt{database} ne $database; |
|
1124 |
+ |
|
1125 |
+ $table = "$database.$table" if exists $opt{prefix}; |
|
1122 | 1126 |
if (my $model = $self->models->{$table}) { |
1123 | 1127 |
push @{$model->columns}, $column; |
1124 | 1128 |
} |
... | ... |
@@ -1656,7 +1660,7 @@ sub _push_join { |
1656 | 1660 |
|
1657 | 1661 |
my @j_clauses = reverse split /\s(and|on)\s/, $j_clause; |
1658 | 1662 |
my $c = $self->{safety_character}; |
1659 |
- my $join_re = qr/([$c]+)\.[$c]+[^$c].*?([$c]+)\.[$c]+/sm; |
|
1663 |
+ my $join_re = qr/((?:[$c]+?\.[$c]+?)|(?:[$c]+?))\.[$c]+[^$c].*?((?:[$c]+?\.[$c]+?)|(?:[$c]+?))\.[$c]+/sm; |
|
1660 | 1664 |
for my $clause (@j_clauses) { |
1661 | 1665 |
if ($clause =~ $join_re) { |
1662 | 1666 |
$table1 = $1; |
... | ... |
@@ -3404,10 +3408,17 @@ See also L<DBIx::Custom::Where> to know how to create where clause. |
3404 | 3408 |
=head2 C<setup_model> |
3405 | 3409 |
|
3406 | 3410 |
$dbi->setup_model; |
3411 |
+ $dbi->setup_model(database => 'main'); |
|
3412 |
+ $dbi->setup_model(database => 'main', prefix => 1); |
|
3407 | 3413 |
|
3408 | 3414 |
Setup all model objects. |
3409 | 3415 |
C<columns> of model object is automatically set, parsing database information. |
3410 | 3416 |
|
3417 |
+If C<database> option is specified, only the database is searched. |
|
3418 |
+ |
|
3419 |
+If C<prefix> option is specified, Target model is qualified by dabtabase name |
|
3420 |
+like C<main.book>. |
|
3421 |
+ |
|
3411 | 3422 |
=head2 C<type_rule> |
3412 | 3423 |
|
3413 | 3424 |
$dbi->type_rule( |
... | ... |
@@ -39,6 +39,7 @@ use DBIx::Custom; |
39 | 39 |
has key8 => $key8; |
40 | 40 |
has key9 => $key9; |
41 | 41 |
has key10 => $key10; |
42 |
+ has setup_model_args => sub { [database => 'main', prefix => 1] }; |
|
42 | 43 |
|
43 | 44 |
my $date_typename = 'Date'; |
44 | 45 |
my $datetime_typename = 'Datetime'; |
... | ... |
@@ -23,6 +23,18 @@ sub u($) { |
23 | 23 |
return $value; |
24 | 24 |
} |
25 | 25 |
|
26 |
+sub u2($) { |
|
27 |
+ my $value = shift; |
|
28 |
+ $value =~ s/\./__/g; |
|
29 |
+ return $value; |
|
30 |
+} |
|
31 |
+ |
|
32 |
+sub hy($) { |
|
33 |
+ my $value = shift; |
|
34 |
+ $value =~ s/\./-/g; |
|
35 |
+ return $value; |
|
36 |
+} |
|
37 |
+ |
|
26 | 38 |
# Constant |
27 | 39 |
my $table1 = $dbi->table1; |
28 | 40 |
my $table2 = $dbi->table2; |
... | ... |
@@ -52,6 +64,7 @@ my $date_typename = $dbi->date_typename; |
52 | 64 |
my $datetime_typename = $dbi->datetime_typename; |
53 | 65 |
my $date_datatype = $dbi->date_datatype; |
54 | 66 |
my $datetime_datatype = $dbi->datetime_datatype; |
67 |
+my $setup_model_args = $dbi->can('setup_model_args') ? $dbi->setup_model_args : []; |
|
55 | 68 |
|
56 | 69 |
# Variables |
57 | 70 |
my $builder; |
... | ... |
@@ -2479,7 +2492,7 @@ eval { $dbi->execute("drop table $table2") }; |
2479 | 2492 |
$dbi->execute($create_table1); |
2480 | 2493 |
$dbi->execute($create_table2); |
2481 | 2494 |
$dbi->separator('__'); |
2482 |
-$dbi->setup_model; |
|
2495 |
+$dbi->setup_model(@$setup_model_args); |
|
2483 | 2496 |
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1); |
2484 | 2497 |
$dbi->insert({$key1 => 1, $key3 => 3}, table => $table2); |
2485 | 2498 |
$model = $dbi->model($table1); |
... | ... |
@@ -2488,7 +2501,7 @@ $result = $model->select( |
2488 | 2501 |
where => {"$table1.$key1" => 1} |
2489 | 2502 |
); |
2490 | 2503 |
is_deeply($result->one, |
2491 |
- {$key1 => 1, $key2 => 2, "${table2}__$key1" => 1, "${table2}__$key3" => 3}); |
|
2504 |
+ {$key1 => 1, $key2 => 2, u2"${table2}__$key1" => 1, u2"${table2}__$key3" => 3}); |
|
2492 | 2505 |
|
2493 | 2506 |
test 'values_clause'; |
2494 | 2507 |
$dbi = DBIx::Custom->connect; |
... | ... |
@@ -2522,7 +2535,7 @@ eval { $dbi->execute("drop table $table1") }; |
2522 | 2535 |
eval { $dbi->execute("drop table $table2") }; |
2523 | 2536 |
$dbi->execute($create_table1); |
2524 | 2537 |
$dbi->execute($create_table2); |
2525 |
-$dbi->setup_model; |
|
2538 |
+$dbi->setup_model(@$setup_model_args); |
|
2526 | 2539 |
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1); |
2527 | 2540 |
$dbi->insert({$key1 => 1, $key3 => 3}, table => $table2); |
2528 | 2541 |
$model = $dbi->model($table1); |
... | ... |
@@ -2599,23 +2612,23 @@ $dbi->insert({$key1 => 1, $key3 => 4}, table => $table2); |
2599 | 2612 |
$dbi->insert({$key1 => 2, $key3 => 5}, table => $table2); |
2600 | 2613 |
$rows = $dbi->select( |
2601 | 2614 |
table => $table1, |
2602 |
- column => "$table1.$key1 as ${table1}_$key1, $key2, $key3", |
|
2615 |
+ column => "$table1.$key1 as " . u("${table1}_$key1") . ", $key2, $key3", |
|
2603 | 2616 |
where => {"$table1.$key2" => 3}, |
2604 | 2617 |
join => ["inner join (select * from $table2 where {= $table2.$key3})" . |
2605 |
- " $table2 on $table1.$key1 = $table2.$key1"], |
|
2618 |
+ " \"$table2\" on $table1.$key1 = \"$table2\".$key1"], |
|
2606 | 2619 |
param => {"$table2.$key3" => 5} |
2607 | 2620 |
)->all; |
2608 |
-is_deeply($rows, [{"${table1}_$key1" => 2, $key2 => 3, $key3 => 5}]); |
|
2621 |
+is_deeply($rows, [{u"${table1}_$key1" => 2, $key2 => 3, $key3 => 5}]); |
|
2609 | 2622 |
|
2610 | 2623 |
$rows = $dbi->select( |
2611 | 2624 |
table => $table1, |
2612 |
- column => "$table1.$key1 as ${table1}_$key1, $key2, $key3", |
|
2625 |
+ column => "$table1.$key1 as " . u("${table1}_$key1") . ", $key2, $key3", |
|
2613 | 2626 |
where => {"$table1.$key2" => 3}, |
2614 | 2627 |
join => "inner join (select * from $table2 where {= $table2.$key3})" . |
2615 |
- " $table2 on $table1.$key1 = $table2.$key1", |
|
2628 |
+ " \"$table2\" on $table1.$key1 = \"$table2\".$key1", |
|
2616 | 2629 |
param => {"$table2.$key3" => 5} |
2617 | 2630 |
)->all; |
2618 |
-is_deeply($rows, [{"${table1}_$key1" => 2, $key2 => 3, $key3 => 5}]); |
|
2631 |
+is_deeply($rows, [{u"${table1}_$key1" => 2, $key2 => 3, $key3 => 5}]); |
|
2619 | 2632 |
|
2620 | 2633 |
test 'select() string where'; |
2621 | 2634 |
$dbi = DBIx::Custom->connect; |
... | ... |
@@ -2981,7 +2994,7 @@ eval { $dbi->execute("drop table $table1") }; |
2981 | 2994 |
eval { $dbi->execute("drop table $table2") }; |
2982 | 2995 |
$dbi->execute($create_table1); |
2983 | 2996 |
$dbi->execute($create_table2); |
2984 |
-$dbi->setup_model; |
|
2997 |
+$dbi->setup_model(@$setup_model_args); |
|
2985 | 2998 |
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1); |
2986 | 2999 |
$dbi->insert({$key1 => 1, $key3 => 3}, table => $table2); |
2987 | 3000 |
$model = $dbi->model($table1); |
... | ... |
@@ -3017,7 +3030,7 @@ $dbi->create_model( |
3017 | 3030 |
$model2 = $dbi->create_model( |
3018 | 3031 |
table => $table2, |
3019 | 3032 |
); |
3020 |
-$dbi->setup_model; |
|
3033 |
+$dbi->setup_model(@$setup_model_args); |
|
3021 | 3034 |
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1); |
3022 | 3035 |
$dbi->insert({$key1 => 1, $key3 => 3}, table => $table2); |
3023 | 3036 |
$model = $dbi->model($table1); |
... | ... |
@@ -3042,7 +3055,7 @@ $result = $model->select( |
3042 | 3055 |
where => {"$table1.$key1" => 1} |
3043 | 3056 |
); |
3044 | 3057 |
is_deeply($result->one, |
3045 |
- {$key1 => 1, $key2 => 2, "${table2}__$key1" => 1, "${table2}__$key3" => 3}); |
|
3058 |
+ {$key1 => 1, $key2 => 2, u2"${table2}__$key1" => 1, u2"${table2}__$key3" => 3}); |
|
3046 | 3059 |
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3}); |
3047 | 3060 |
|
3048 | 3061 |
$dbi->separator('-'); |
... | ... |
@@ -3055,7 +3068,7 @@ $result = $model->select( |
3055 | 3068 |
where => {"$table1.$key1" => 1} |
3056 | 3069 |
); |
3057 | 3070 |
is_deeply($result->one, |
3058 |
- {$key1 => 1, $key2 => 2, "$table2-$key1" => 1, "$table2-$key3" => 3}); |
|
3071 |
+ {$key1 => 1, $key2 => 2, hy"$table2-$key1" => 1, hy"$table2-$key3" => 3}); |
|
3059 | 3072 |
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3}); |
3060 | 3073 |
|
3061 | 3074 |
|
... | ... |
@@ -3074,7 +3087,7 @@ $dbi->create_model( |
3074 | 3087 |
], |
3075 | 3088 |
primary_key => [$key1], |
3076 | 3089 |
); |
3077 |
-$dbi->setup_model; |
|
3090 |
+$dbi->setup_model(@$setup_model_args); |
|
3078 | 3091 |
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1); |
3079 | 3092 |
$model = $dbi->model($table1); |
3080 | 3093 |
$result = $model->select(column => $key1); |
... | ... |
@@ -3859,7 +3872,7 @@ test 'dbi method from model'; |
3859 | 3872 |
$dbi = MyDBI9->connect; |
3860 | 3873 |
eval { $dbi->execute("drop table $table1") }; |
3861 | 3874 |
$dbi->execute($create_table1); |
3862 |
-$dbi->setup_model; |
|
3875 |
+$dbi->setup_model(@$setup_model_args); |
|
3863 | 3876 |
$model = $dbi->model($table1); |
3864 | 3877 |
eval{$model->execute("select * from $table1")}; |
3865 | 3878 |
ok(!$@); |
... | ... |
@@ -3871,7 +3884,7 @@ eval { $dbi->execute("drop table $table1") }; |
3871 | 3884 |
$dbi->execute($create_table1); |
3872 | 3885 |
eval { $dbi->execute("drop table $table2") }; |
3873 | 3886 |
$dbi->execute($create_table2); |
3874 |
-$dbi->setup_model; |
|
3887 |
+$dbi->setup_model(@$setup_model_args); |
|
3875 | 3888 |
$dbi->execute("insert into $table1 ($key1, $key2) values (1, 2)"); |
3876 | 3889 |
$dbi->execute("insert into $table2 ($key1, $key3) values (1, 4)"); |
3877 | 3890 |
$model = $dbi->model($table1); |
... | ... |
@@ -3928,7 +3941,7 @@ $dbi->create_model( |
3928 | 3941 |
$key1 => {in => sub { uc $_[0] }} |
3929 | 3942 |
] |
3930 | 3943 |
); |
3931 |
-$dbi->setup_model; |
|
3944 |
+$dbi->setup_model(@$setup_model_args); |
|
3932 | 3945 |
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1); |
3933 | 3946 |
$dbi->insert({$key1 => 1, $key3 => 3}, table => $table2); |
3934 | 3947 |
$model = $dbi->model($table1); |
... | ... |
@@ -4113,7 +4126,7 @@ eval { $dbi->execute("drop table $table2") }; |
4113 | 4126 |
|
4114 | 4127 |
$dbi->execute($create_table1); |
4115 | 4128 |
$dbi->execute($create_table2); |
4116 |
-$dbi->setup_model; |
|
4129 |
+$dbi->setup_model(@$setup_model_args); |
|
4117 | 4130 |
is_deeply([sort @{$dbi->model($table1)->columns}], [$key1, $key2]); |
4118 | 4131 |
is_deeply([sort @{$dbi->model($table2)->columns}], [$key1, $key3]); |
4119 | 4132 |
|