select method can be called even if table name is not...
...specified
... | ... |
@@ -1,3 +1,5 @@ |
1 |
+0.2108 |
|
2 |
+ - select method can be called even if table name is not specified |
|
1 | 3 |
0.2108 |
2 | 4 |
- added async database access support using AnyEvent |
3 | 5 |
and added "async" option to execute method(EXPERIMENTAL) |
... | ... |
@@ -1,7 +1,7 @@ |
1 | 1 |
package DBIx::Custom; |
2 | 2 |
use Object::Simple -base; |
3 | 3 |
|
4 |
-our $VERSION = '0.2108'; |
|
4 |
+our $VERSION = '0.2109'; |
|
5 | 5 |
use 5.008001; |
6 | 6 |
|
7 | 7 |
use Carp 'croak'; |
... | ... |
@@ -616,8 +616,9 @@ sub execute { |
616 | 616 |
poll => 'r', |
617 | 617 |
cb => sub { |
618 | 618 |
$cb->($self, $result); |
619 |
- $watcher = undef; |
|
620 |
- $result =undef; |
|
619 |
+ undef $watcher; |
|
620 |
+ undef $result; |
|
621 |
+ undef $cb; |
|
621 | 622 |
}, |
622 | 623 |
); |
623 | 624 |
} |
... | ... |
@@ -959,13 +960,16 @@ sub select { |
959 | 960 |
my $self = shift; |
960 | 961 |
my $column = shift if @_ % 2; |
961 | 962 |
my %opt = @_; |
963 |
+ $opt{statement} = 'select'; |
|
962 | 964 |
$opt{column} = $column if defined $column; |
963 | 965 |
|
964 | 966 |
# Options |
967 |
+ my $table_is_empty; |
|
965 | 968 |
my $tables = ref $opt{table} eq 'ARRAY' ? $opt{table} |
966 | 969 |
: defined $opt{table} ? [$opt{table}] |
967 | 970 |
: []; |
968 | 971 |
$opt{table} = $tables; |
972 |
+ $table_is_empty = 1 unless @$tables; |
|
969 | 973 |
my $where_param = $opt{where_param} || delete $opt{param} || {}; |
970 | 974 |
warn "select method where_param option is DEPRECATED!" |
971 | 975 |
if $opt{where_param}; |
... | ... |
@@ -1000,13 +1004,17 @@ sub select { |
1000 | 1004 |
|
1001 | 1005 |
$column = join(' ', $column->[0], 'as', $self->q($column->[1])); |
1002 | 1006 |
} |
1003 |
- unshift @$tables, @{$self->_search_tables($column)}; |
|
1007 |
+ unshift @$tables, @{$self->_search_tables($column)} |
|
1008 |
+ unless $table_is_empty; |
|
1004 | 1009 |
$sql .= "$column, "; |
1005 | 1010 |
} |
1006 | 1011 |
$sql =~ s/, $/ /; |
1007 | 1012 |
} |
1008 | 1013 |
else { $sql .= '* ' } |
1009 |
- |
|
1014 |
+ |
|
1015 |
+ # Execute query without table |
|
1016 |
+ return $self->execute($sql, {}, %opt) if $table_is_empty; |
|
1017 |
+ |
|
1010 | 1018 |
# Table |
1011 | 1019 |
$sql .= 'from '; |
1012 | 1020 |
if ($opt{relation}) { |
... | ... |
@@ -1018,8 +1026,6 @@ sub select { |
1018 | 1026 |
} |
1019 | 1027 |
else { $sql .= $self->q($tables->[-1] || '') . ' ' } |
1020 | 1028 |
$sql =~ s/, $/ /; |
1021 |
- croak "select method table option must be specified " . _subname |
|
1022 |
- unless defined $tables->[-1]; |
|
1023 | 1029 |
|
1024 | 1030 |
# Add tables in parameter |
1025 | 1031 |
unshift @$tables, |
... | ... |
@@ -1027,7 +1033,7 @@ sub select { |
1027 | 1033 |
|
1028 | 1034 |
# Where |
1029 | 1035 |
my $w = $self->_where_clause_and_param($opt{where}, $where_param, |
1030 |
- delete $opt{id}, $opt{primary_key}, $tables->[-1]); |
|
1036 |
+ delete $opt{id}, $opt{primary_key}, @$tables ? $tables->[-1] : undef); |
|
1031 | 1037 |
|
1032 | 1038 |
# Add table names in where clause |
1033 | 1039 |
unshift @$tables, @{$self->_search_tables($w->{clause})}; |
... | ... |
@@ -1043,10 +1049,7 @@ sub select { |
1043 | 1049 |
if $opt{relation}; |
1044 | 1050 |
|
1045 | 1051 |
# Execute query |
1046 |
- $opt{statement} = 'select'; |
|
1047 |
- my $result = $self->execute($sql, $w->{param}, %opt); |
|
1048 |
- |
|
1049 |
- $result; |
|
1052 |
+ return $self->execute($sql, $w->{param}, %opt); |
|
1050 | 1053 |
} |
1051 | 1054 |
|
1052 | 1055 |
sub setup_model { |
... | ... |
@@ -1979,8 +1979,8 @@ is($dbi->{_tags}->{b}->(), 2); |
1979 | 1979 |
|
1980 | 1980 |
test 'table not specify exception'; |
1981 | 1981 |
$dbi = DBIx::Custom->connect; |
1982 |
-eval {$dbi->select}; |
|
1983 |
-like($@, qr/table/); |
|
1982 |
+eval {$dbi->select($key1)}; |
|
1983 |
+ok($@); |
|
1984 | 1984 |
|
1985 | 1985 |
test 'more tests'; |
1986 | 1986 |
$dbi = DBIx::Custom->connect; |
... | ... |
@@ -385,3 +385,10 @@ $result = $dbi->select( |
385 | 385 |
] |
386 | 386 |
); |
387 | 387 |
is_deeply($result->all, [{"table2.key3" => 4}]); |
388 |
+ |
|
389 |
+test 'select table nothing'; |
|
390 |
+eval { $dbi->execute('drop table table1') }; |
|
391 |
+eval { $dbi->select('key1') }; |
|
392 |
+ok($@); |
|
393 |
+$result = $dbi->select('3'); |
|
394 |
+is($result->fetch_one->[0], 3); |