... | ... |
@@ -1,3 +1,5 @@ |
1 |
+0.1730 |
|
2 |
+ - fixed id option bug when column name is anbiguous |
|
1 | 3 |
0.1729 |
2 | 4 |
- dbi_option attribute is renamed to option, dbi_option is DEPRECATED! |
3 | 5 |
- default_dbi_option is renamed to default_option, default_dbi_option |
... | ... |
@@ -1,7 +1,7 @@ |
1 | 1 |
package DBIx::Custom; |
2 | 2 |
use Object::Simple -base; |
3 | 3 |
|
4 |
-our $VERSION = '0.1728'; |
|
4 |
+our $VERSION = '0.1730'; |
|
5 | 5 |
use 5.008001; |
6 | 6 |
|
7 | 7 |
use Carp 'croak'; |
... | ... |
@@ -249,7 +249,8 @@ sub delete { |
249 | 249 |
my $prefix = delete $args{prefix}; |
250 | 250 |
|
251 | 251 |
# Where |
252 |
- $where = $self->_create_param_from_id($id, $primary_key) if defined $id; |
|
252 |
+ $where = $self->_create_param_from_id($id, $primary_key, $table) |
|
253 |
+ if defined $id; |
|
253 | 254 |
my $where_clause = ''; |
254 | 255 |
if (ref $where eq 'ARRAY' && !ref $where->[0]) { |
255 | 256 |
$where_clause = "where " . $where->[0]; |
... | ... |
@@ -403,16 +404,11 @@ sub execute { |
403 | 404 |
warn "sqlfilter option is DEPRECATED" if $args{sqlfilter}; |
404 | 405 |
my $id = delete $args{id}; |
405 | 406 |
my $primary_key = delete $args{primary_key}; |
406 |
- croak "insert method primary_key option " . |
|
407 |
+ croak "execute method primary_key option " . |
|
407 | 408 |
"must be specified when id is specified " . _subname |
408 | 409 |
if defined $id && !defined $primary_key; |
409 | 410 |
$primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY'; |
410 | 411 |
|
411 |
- if (defined $id) { |
|
412 |
- my $id_param = $self->_create_param_from_id($id, $primary_key); |
|
413 |
- $param = $self->merge_param($id_param, $param); |
|
414 |
- } |
|
415 |
- |
|
416 | 412 |
# Check argument names |
417 | 413 |
foreach my $name (keys %args) { |
418 | 414 |
croak qq{"$name" is wrong option } . _subname |
... | ... |
@@ -422,6 +418,7 @@ sub execute { |
422 | 418 |
$query = $self->_create_query($query, $after_build_sql) unless ref $query; |
423 | 419 |
|
424 | 420 |
# Save query |
421 |
+ if (ref $query eq 'DBIx::Custom::Result') { $DB::single = 1 } |
|
425 | 422 |
$self->last_sql($query->sql); |
426 | 423 |
|
427 | 424 |
return $query if $query_return; |
... | ... |
@@ -432,6 +429,11 @@ sub execute { |
432 | 429 |
# Tables |
433 | 430 |
unshift @$tables, @{$query->{tables} || []}; |
434 | 431 |
my $main_table = @{$tables}[-1]; |
432 |
+ |
|
433 |
+ if (defined $id) { |
|
434 |
+ my $id_param = $self->_create_param_from_id($id, $primary_key, $main_table); |
|
435 |
+ $param = $self->merge_param($id_param, $param); |
|
436 |
+ } |
|
435 | 437 |
|
436 | 438 |
# DEPRECATED! Cleanup tables |
437 | 439 |
$tables = $self->_remove_duplicate_table($tables, $main_table) |
... | ... |
@@ -932,7 +934,7 @@ sub select { |
932 | 934 |
} |
933 | 935 |
else { |
934 | 936 |
my $main_table = $tables->[-1] || ''; |
935 |
- $sql .= $self->_q($main_table); |
|
937 |
+ $sql .= $self->_q($main_table) . ' '; |
|
936 | 938 |
} |
937 | 939 |
$sql =~ s/, $/ /; |
938 | 940 |
croak "Not found table name " . _subname |
... | ... |
@@ -944,7 +946,8 @@ sub select { |
944 | 946 |
|
945 | 947 |
# Where |
946 | 948 |
my $where_clause = ''; |
947 |
- $where = $self->_create_param_from_id($id, $primary_key) if defined $id; |
|
949 |
+ $where = $self->_create_param_from_id($id, $primary_key, $tables->[-1]) |
|
950 |
+ if defined $id; |
|
948 | 951 |
if (ref $where eq 'ARRAY' && !ref $where->[0]) { |
949 | 952 |
$where_clause = "where " . $where->[0]; |
950 | 953 |
$where_param = $where->[1]; |
... | ... |
@@ -1141,7 +1144,8 @@ sub update { |
1141 | 1144 |
my $assign_clause = $self->assign_clause($param, {wrap => $wrap}); |
1142 | 1145 |
|
1143 | 1146 |
# Where |
1144 |
- $where = $self->_create_param_from_id($id, $primary_key) if defined $id; |
|
1147 |
+ $where = $self->_create_param_from_id($id, $primary_key, $table) |
|
1148 |
+ if defined $id; |
|
1145 | 1149 |
my $where_clause = ''; |
1146 | 1150 |
if (ref $where eq 'ARRAY' && !ref $where->[0]) { |
1147 | 1151 |
$where_clause = "where " . $where->[0]; |
... | ... |
@@ -1315,7 +1319,7 @@ sub _create_bind_values { |
1315 | 1319 |
} |
1316 | 1320 |
|
1317 | 1321 |
sub _create_param_from_id { |
1318 |
- my ($self, $id, $primary_keys) = @_; |
|
1322 |
+ my ($self, $id, $primary_keys, $table) = @_; |
|
1319 | 1323 |
|
1320 | 1324 |
# Create parameter |
1321 | 1325 |
my $param = {}; |
... | ... |
@@ -1328,7 +1332,9 @@ sub _create_param_from_id { |
1328 | 1332 |
. " (" . (caller 1)[3] . ")" |
1329 | 1333 |
unless @$primary_keys eq @$id; |
1330 | 1334 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
1331 |
- $param->{$primary_keys->[$i]} = $id->[$i]; |
|
1335 |
+ my $key = $primary_keys->[$i]; |
|
1336 |
+ $key = "$table." . $key if $table; |
|
1337 |
+ $param->{$key} = $id->[$i]; |
|
1332 | 1338 |
} |
1333 | 1339 |
} |
1334 | 1340 |
|
... | ... |
@@ -2329,7 +2335,7 @@ and C<PrintError> option is false by default. |
2329 | 2335 |
|
2330 | 2336 |
=head2 C<count> |
2331 | 2337 |
|
2332 |
- my $count = $model->count(table => 'book'); |
|
2338 |
+ my $count = $dbi->count(table => 'book'); |
|
2333 | 2339 |
|
2334 | 2340 |
Get rows count. |
2335 | 2341 |
|
... | ... |
@@ -2806,7 +2806,7 @@ $result = $dbi->execute("select $key1 as h1, $key2 as h2 from $table1"); |
2806 | 2806 |
is_deeply([map { lc } @{$result->header}], [qw/h1 h2/]); |
2807 | 2807 |
|
2808 | 2808 |
test 'Named placeholder :name(operater) syntax'; |
2809 |
-$dbi->execute("drop table $table1"); |
|
2809 |
+eval { $dbi->execute("drop table $table1") }; |
|
2810 | 2810 |
$dbi->execute($create_table1_2); |
2811 | 2811 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
2812 | 2812 |
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
... | ... |
@@ -2836,7 +2836,7 @@ $rows = $result->all; |
2836 | 2836 |
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]); |
2837 | 2837 |
|
2838 | 2838 |
test 'high perfomance way'; |
2839 |
-$dbi->execute("drop table $table1"); |
|
2839 |
+eval { $dbi->execute("drop table $table1") }; |
|
2840 | 2840 |
$dbi->execute($create_table1_highperformance); |
2841 | 2841 |
$rows = [ |
2842 | 2842 |
{$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7}, |
... | ... |
@@ -2856,7 +2856,7 @@ $rows = [ |
2856 | 2856 |
); |
2857 | 2857 |
} |
2858 | 2858 |
|
2859 |
-$dbi->execute("drop table $table1"); |
|
2859 |
+eval { $dbi->execute("drop table $table1") }; |
|
2860 | 2860 |
$dbi->execute($create_table1_highperformance); |
2861 | 2861 |
$rows = [ |
2862 | 2862 |
{$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7}, |
... | ... |
@@ -2878,7 +2878,7 @@ $rows = [ |
2878 | 2878 |
); |
2879 | 2879 |
} |
2880 | 2880 |
|
2881 |
-$dbi->execute("drop table $table1"); |
|
2881 |
+eval { $dbi->execute("drop table $table1") }; |
|
2882 | 2882 |
$dbi->execute($create_table1_highperformance); |
2883 | 2883 |
$rows = [ |
2884 | 2884 |
{$key7 => 10, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5}, |
... | ... |
@@ -2888,17 +2888,71 @@ $rows = [ |
2888 | 2888 |
$model = $dbi->create_model(table => $table1, primary_key => $key1); |
2889 | 2889 |
my $query; |
2890 | 2890 |
foreach my $row (@$rows) { |
2891 |
- $query ||= $model->insert($row, id => 1, query => 1); |
|
2892 |
- $model->execute($query, $row, id => 1, filter => {$key7 => sub { $_[0] * 2 }}); |
|
2891 |
+ $query ||= $model->insert($row, query => 1); |
|
2892 |
+ $model->execute($query, $row, filter => {$key7 => sub { $_[0] * 2 }}); |
|
2893 | 2893 |
} |
2894 | 2894 |
is_deeply($dbi->select(table => $table1, append => 'order by key2')->all, |
2895 | 2895 |
[ |
2896 |
- {$key7 => 20, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5, $key1 => 1}, |
|
2897 |
- {$key7 => 22, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 1}, |
|
2896 |
+ {$key7 => 20, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5, $key1 => undef}, |
|
2897 |
+ {$key7 => 22, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => undef}, |
|
2898 | 2898 |
] |
2899 | 2899 |
); |
2900 | 2900 |
} |
2901 | 2901 |
|
2902 |
+test 'id option more'; |
|
2903 |
+eval { $dbi->execute("drop table $table1") }; |
|
2904 |
+$dbi->execute($create_table1_highperformance); |
|
2905 |
+$row = { |
|
2906 |
+ $key7 => 10, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5, $key1 => 2 |
|
2907 |
+}; |
|
2908 |
+$model = $dbi->create_model(table => $table1, primary_key => $key1); |
|
2909 |
+$model->insert($row); |
|
2910 |
+$query = $model->update({$key7 => 11}, id => 1, query => 1); |
|
2911 |
+$model->execute($query, {$key7 => 11}, id => 1, filter => {"$table1.$key1" => sub { $_[0] * 2 }}); |
|
2912 |
+is_deeply($dbi->select(table => $table1)->one, |
|
2913 |
+ {$key7 => 11, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5, $key1 => 2}, |
|
2914 |
+); |
|
2915 |
+ |
|
2916 |
+eval { $dbi->execute("drop table $table1") }; |
|
2917 |
+eval { $dbi->execute("drop table $table2") }; |
|
2918 |
+$dbi->execute($create_table1); |
|
2919 |
+$dbi->execute($create_table2); |
|
2920 |
+$model = $dbi->create_model(table => $table1, primary_key => $key1); |
|
2921 |
+$model->insert({$key1 => 1, $key2 => 2}); |
|
2922 |
+$model = $dbi->create_model(table => $table2, primary_key => $key1, |
|
2923 |
+ join => ["left outer join $table1 on $table2.$key1 = $table1.$key1"]); |
|
2924 |
+$model->insert({$key1 => 1, $key3 => 3}); |
|
2925 |
+$result = $model->select( |
|
2926 |
+ column => {$table1 => ["$key2"]}, |
|
2927 |
+ id => 1 |
|
2928 |
+); |
|
2929 |
+$DB::single = 1; |
|
2930 |
+is_deeply($result->all, [{"$table1.$key2" => 2}]); |
|
2931 |
+ |
|
2932 |
+eval { $dbi->execute("drop table $table1") }; |
|
2933 |
+$dbi->execute($create_table1_highperformance); |
|
2934 |
+$row = { |
|
2935 |
+ $key7 => 10, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5, $key1 => 2 |
|
2936 |
+}; |
|
2937 |
+$model = $dbi->create_model(table => $table1, primary_key => $key1); |
|
2938 |
+$model->insert($row); |
|
2939 |
+$query = $model->delete(id => 1, query => 1); |
|
2940 |
+$model->execute($query, {}, id => 1, , filter => {"$table1.$key1" => sub { $_[0] * 2 }}); |
|
2941 |
+is_deeply($dbi->select(table => $table1)->all, []); |
|
2942 |
+ |
|
2943 |
+eval { $dbi->execute("drop table $table1") }; |
|
2944 |
+eval { $dbi->execute($create_table1_highperformance) }; |
|
2945 |
+$row = { |
|
2946 |
+ $key7 => 10, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5, $key1 => 2 |
|
2947 |
+}; |
|
2948 |
+$model = $dbi->create_model(table => $table1, primary_key => $key1); |
|
2949 |
+$model->insert($row); |
|
2950 |
+$query = $model->select(id => 1, query => 1); |
|
2951 |
+$model->execute($query, {$key7 => 11}, id => 1, filter => {"$table1.$key1" => sub { $_[0] * 2 }}); |
|
2952 |
+is_deeply($dbi->select(table => $table1)->one, |
|
2953 |
+ {$key7 => 10, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5, $key1 => 2}, |
|
2954 |
+); |
|
2955 |
+ |
|
2902 | 2956 |
test 'result'; |
2903 | 2957 |
$dbi = DBIx::Custom->connect; |
2904 | 2958 |
eval { $dbi->execute("drop table $table1") }; |