| ... | ... |
@@ -7,6 +7,7 @@ use Carp 'croak'; |
| 7 | 7 |
use DBI; |
| 8 | 8 |
use DBI::Custom::SQL::Template; |
| 9 | 9 |
use DBI::Custom::Result; |
| 10 |
+use DBI::Custom::Query; |
|
| 10 | 11 |
|
| 11 | 12 |
### Class-Object Accessors |
| 12 | 13 |
sub user : ClassObjectAttr { initialize => {clone => 'scalar'} }
|
| ... | ... |
@@ -14,7 +15,7 @@ sub password : ClassObjectAttr { initialize => {clone => 'scalar'} }
|
| 14 | 15 |
sub data_source : ClassObjectAttr { initialize => {clone => 'scalar'} }
|
| 15 | 16 |
sub database : ClassObjectAttr { initialize => {clone => 'scalar'} }
|
| 16 | 17 |
|
| 17 |
-sub dbi_option : ClassObjectAttr { initialize => {clone => 'hash',
|
|
| 18 |
+sub dbi_options : ClassObjectAttr { initialize => {clone => 'hash',
|
|
| 18 | 19 |
default => sub { {} } } }
|
| 19 | 20 |
|
| 20 | 21 |
sub bind_filter : ClassObjectAttr { initialize => {clone => 'scalar'} }
|
| ... | ... |
@@ -81,7 +82,7 @@ sub connect {
|
| 81 | 82 |
my $data_source = $self->data_source; |
| 82 | 83 |
my $user = $self->user; |
| 83 | 84 |
my $password = $self->password; |
| 84 |
- my $dbi_option = $self->dbi_option; |
|
| 85 |
+ my $dbi_options = $self->dbi_options; |
|
| 85 | 86 |
|
| 86 | 87 |
my $dbh = DBI->connect( |
| 87 | 88 |
$data_source, |
| ... | ... |
@@ -91,7 +92,7 @@ sub connect {
|
| 91 | 92 |
RaiseError => 1, |
| 92 | 93 |
PrintError => 0, |
| 93 | 94 |
AutoCommit => 1, |
| 94 |
- %{$dbi_option || {} }
|
|
| 95 |
+ %{$dbi_options || {} }
|
|
| 95 | 96 |
} |
| 96 | 97 |
); |
| 97 | 98 |
|
| ... | ... |
@@ -154,7 +155,7 @@ sub create_query {
|
| 154 | 155 |
my $query = $self->sql_template->create_query($template); |
| 155 | 156 |
|
| 156 | 157 |
# Create Query object; |
| 157 |
- my $query = DBI::Custom::Query->new($query); |
|
| 158 |
+ $query = DBI::Custom::Query->new($query); |
|
| 158 | 159 |
|
| 159 | 160 |
# connect if not |
| 160 | 161 |
$self->connect unless $self->connected; |
| ... | ... |
@@ -173,7 +174,7 @@ sub execute {
|
| 173 | 174 |
# Create query if First argument is template |
| 174 | 175 |
if (!ref $query) {
|
| 175 | 176 |
my $template = $query; |
| 176 |
- $query = $sefl->create_query($tempalte); |
|
| 177 |
+ $query = $self->create_query($template); |
|
| 177 | 178 |
} |
| 178 | 179 |
|
| 179 | 180 |
# Set bind filter |
| ... | ... |
@@ -186,7 +187,8 @@ sub execute {
|
| 186 | 187 |
my $bind_values = $self->_build_bind_values($query, $params); |
| 187 | 188 |
|
| 188 | 189 |
# Execute |
| 189 |
- my $ret_val = $query->sth->execute(@$bind_values); |
|
| 190 |
+ my $sth = $query->sth; |
|
| 191 |
+ my $ret_val = $sth->execute(@$bind_values); |
|
| 190 | 192 |
|
| 191 | 193 |
# Return resultset if select statement is executed |
| 192 | 194 |
if ($sth->{NUM_OF_FIELDS}) {
|
| ... | ... |
@@ -202,20 +204,22 @@ sub execute {
|
| 202 | 204 |
|
| 203 | 205 |
sub _build_bind_values {
|
| 204 | 206 |
my ($self, $query, $params) = @_; |
| 205 |
- my $bind_filter = $query->bind_filter; |
|
| 206 |
- my $no_filters_map = $query->_no_filters_map || {};
|
|
| 207 |
+ |
|
| 208 |
+ my $key_infos = $query->key_infos; |
|
| 209 |
+ my $bind_filter = $query->bind_filter; |
|
| 210 |
+ my $no_filters_map = $query->_no_filters_map || {};
|
|
| 207 | 211 |
|
| 208 | 212 |
# binding values |
| 209 | 213 |
my @bind_values; |
| 210 | 214 |
|
| 211 | 215 |
# Filter and sdd bind values |
| 212 |
- foreach my $param_key_info (@$param_key_infos) {
|
|
| 213 |
- my $filtering_key = $param_key_info->{key};
|
|
| 214 |
- my $access_keys = $param_key_info->{access_keys};
|
|
| 216 |
+ foreach my $key_info (@$key_infos) {
|
|
| 217 |
+ my $filtering_key = $key_info->{key};
|
|
| 218 |
+ my $access_keys = $key_info->{access_keys};
|
|
| 215 | 219 |
|
| 216 |
- my $original_key = $param_key_info->{original_key} || '';
|
|
| 217 |
- my $table = $param_key_info->{table} || '';
|
|
| 218 |
- my $column = $param_key_info->{column} || '';
|
|
| 220 |
+ my $original_key = $key_info->{original_key} || '';
|
|
| 221 |
+ my $table = $key_info->{table} || '';
|
|
| 222 |
+ my $column = $key_info->{column} || '';
|
|
| 219 | 223 |
|
| 220 | 224 |
ACCESS_KEYS : |
| 221 | 225 |
foreach my $access_key (@$access_keys) {
|
| ... | ... |
@@ -314,17 +318,23 @@ Version 0.0101 |
| 314 | 318 |
|
| 315 | 319 |
# Sample(PostgreSQL) |
| 316 | 320 |
$dbi->data_source("dbi:Pg:dbname=$database");
|
| 321 |
+ |
|
| 322 |
+=head2 database |
|
| 323 |
+ |
|
| 324 |
+ # Set and get database name |
|
| 325 |
+ $self = $dbi->database($database); |
|
| 326 |
+ $database = $dbi->database; |
|
| 317 | 327 |
|
| 318 |
-=head2 dbi_option |
|
| 328 |
+=head2 dbi_options |
|
| 319 | 329 |
|
| 320 | 330 |
# Set and get DBI option |
| 321 |
- $self = $dbi->dbi_option({$options => $value, ...});
|
|
| 322 |
- $dbi_option = $dbi->dbi_option; |
|
| 331 |
+ $self = $dbi->dbi_options({$options => $value, ...});
|
|
| 332 |
+ $dbi_options = $dbi->dbi_options; |
|
| 323 | 333 |
|
| 324 | 334 |
# Sample |
| 325 |
- $dbi->dbi_option({PrintError => 0, RaiseError => 1});
|
|
| 335 |
+ $dbi->dbi_options({PrintError => 0, RaiseError => 1});
|
|
| 326 | 336 |
|
| 327 |
-dbi_option is used when you connect database by using connect. |
|
| 337 |
+dbi_options is used when you connect database by using connect. |
|
| 328 | 338 |
|
| 329 | 339 |
=head2 sql_template |
| 330 | 340 |
|
| ... | ... |
@@ -362,6 +372,12 @@ you can get DBI database handle if you need. |
| 362 | 372 |
# Sample |
| 363 | 373 |
$dbi->fetch_filter($self->filters->{default_fetch_filter});
|
| 364 | 374 |
|
| 375 |
+=head2 no_filters |
|
| 376 |
+ |
|
| 377 |
+ # Set and get no filter keys |
|
| 378 |
+ $self = $dbi->no_filters($no_filters); |
|
| 379 |
+ $no_filters = $dbi->no_filters; |
|
| 380 |
+ |
|
| 365 | 381 |
=head2 result_class |
| 366 | 382 |
|
| 367 | 383 |
# Set and get resultset class |
| ... | ... |
@@ -452,11 +468,16 @@ If database is already disconnected, this method do noting. |
| 452 | 468 |
|
| 453 | 469 |
add_filter add filter to filters |
| 454 | 470 |
|
| 455 |
-=head2 query |
|
| 471 |
+=head2 create_query |
|
| 472 |
+ |
|
| 473 |
+ # Create Query object from SQL template |
|
| 474 |
+ my $query = $dbi->create_query($template); |
|
| 475 |
+ |
|
| 476 |
+=head2 execute |
|
| 456 | 477 |
|
| 457 | 478 |
# Parse SQL template and execute SQL |
| 458 |
- $result = $dbi->query($sql_template, $param); |
|
| 459 |
- $result = $dbi->query($sql_template, $param, $bind_filter); |
|
| 479 |
+ $result = $dbi->query($query, $params); |
|
| 480 |
+ $result = $dbi->query($template, $params); # Shorcut |
|
| 460 | 481 |
|
| 461 | 482 |
# Sample |
| 462 | 483 |
$result = $dbi->query("select * from authors where {= name} && {= age}",
|
| ... | ... |
@@ -468,19 +489,6 @@ add_filter add filter to filters |
| 468 | 489 |
|
| 469 | 490 |
See also L<DBI::Custom::SQL::Template> |
| 470 | 491 |
|
| 471 |
-=head2 query_raw_sql |
|
| 472 |
- |
|
| 473 |
- # Execute SQL |
|
| 474 |
- $result = $dbi->query_raw_sql($sql, @bind_values); |
|
| 475 |
- |
|
| 476 |
- # Sample |
|
| 477 |
- $result = $dbi->query("select * from table where name = ?,
|
|
| 478 |
- title = ?;", 'taro', 'perl'); |
|
| 479 |
- |
|
| 480 |
- while (my @row = $result->fetch) {
|
|
| 481 |
- # do something |
|
| 482 |
- } |
|
| 483 |
- |
|
| 484 | 492 |
=head2 run_tranzaction |
| 485 | 493 |
|
| 486 | 494 |
# Run tranzaction |
| ... | ... |
@@ -491,6 +499,8 @@ See also L<DBI::Custom::SQL::Template> |
| 491 | 499 |
If tranzaction is success, commit is execute. |
| 492 | 500 |
If tranzation is died, rollback is execute. |
| 493 | 501 |
|
| 502 |
+ |
|
| 503 |
+ |
|
| 494 | 504 |
=head1 AUTHOR |
| 495 | 505 |
|
| 496 | 506 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
| ... | ... |
@@ -6,16 +6,16 @@ use DBI::Custom; |
| 6 | 6 |
use Scalar::Util qw/blessed/; |
| 7 | 7 |
use DBI::Custom::SQL::Template; |
| 8 | 8 |
|
| 9 |
-my $sql_tmpl1 = DBI::Custom::SQL::Template->new->upper_case(0); |
|
| 10 |
-my $sql_tmpl2 = DBI::Custom::SQL::Template->new->upper_case(1); |
|
| 11 |
-my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
|
| 9 |
+my $sql_tmpl1 = DBI::Custom::SQL::Template->new->tag_start(0); |
|
| 10 |
+my $sql_tmpl2 = DBI::Custom::SQL::Template->new->tag_start(1); |
|
| 11 |
+my $sql_tmpl3 = DBI::Custom::SQL::Template->new->tag_start(2); |
|
| 12 | 12 |
|
| 13 | 13 |
{
|
| 14 | 14 |
my $dbi = DBI::Custom->new( |
| 15 | 15 |
user => 'a', |
| 16 | 16 |
password => 'b', |
| 17 | 17 |
data_source => 'c', |
| 18 |
- dbi_option => {d => 1, e => 2},
|
|
| 18 |
+ dbi_options => {d => 1, e => 2},
|
|
| 19 | 19 |
filters => {
|
| 20 | 20 |
f => 3, |
| 21 | 21 |
}, |
| ... | ... |
@@ -25,7 +25,7 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 25 | 25 |
sql_template => $sql_tmpl1, |
| 26 | 26 |
); |
| 27 | 27 |
is_deeply($dbi,{user => 'a', password => 'b', data_source => 'c',
|
| 28 |
- dbi_option => {d => 1, e => 2}, filters => {f => 3}, bind_filter => 'f',
|
|
| 28 |
+ dbi_options => {d => 1, e => 2}, filters => {f => 3}, bind_filter => 'f',
|
|
| 29 | 29 |
fetch_filter => 'g', result_class => 'g', |
| 30 | 30 |
sql_template => $sql_tmpl1}, 'new'); |
| 31 | 31 |
|
| ... | ... |
@@ -43,7 +43,7 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 43 | 43 |
->user('a')
|
| 44 | 44 |
->password('b')
|
| 45 | 45 |
->data_source('c')
|
| 46 |
- ->dbi_option({d => 1, e => 2})
|
|
| 46 |
+ ->dbi_options({d => 1, e => 2})
|
|
| 47 | 47 |
->filters( |
| 48 | 48 |
f => 3 |
| 49 | 49 |
) |
| ... | ... |
@@ -58,7 +58,7 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 58 | 58 |
user => 'ao', |
| 59 | 59 |
password => 'bo', |
| 60 | 60 |
data_source => 'co', |
| 61 |
- dbi_option => {do => 10, eo => 20},
|
|
| 61 |
+ dbi_options => {do => 10, eo => 20},
|
|
| 62 | 62 |
filters => {
|
| 63 | 63 |
fo => 30, |
| 64 | 64 |
}, |
| ... | ... |
@@ -68,8 +68,8 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 68 | 68 |
sql_template => $sql_tmpl1, |
| 69 | 69 |
); |
| 70 | 70 |
my $sql_tmpl = delete $dbi->{sql_template};
|
| 71 |
- is($sql_tmpl->upper_case, 0); |
|
| 72 |
- is_deeply($dbi,{ user => 'ao', password => 'bo', data_source => 'co', dbi_option => {do => 10, eo => 20},
|
|
| 71 |
+ is($sql_tmpl->tag_start, 0); |
|
| 72 |
+ is_deeply($dbi,{ user => 'ao', password => 'bo', data_source => 'co', dbi_options => {do => 10, eo => 20},
|
|
| 73 | 73 |
,filters => {fo => 30}, bind_filter => 'fo', fetch_filter => 'go', result_class => 'ho',
|
| 74 | 74 |
}, 'new arguments'); |
| 75 | 75 |
|
| ... | ... |
@@ -82,12 +82,12 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 82 | 82 |
is($dbi->user, 'a'); |
| 83 | 83 |
is($dbi->password, 'b'); |
| 84 | 84 |
is($dbi->data_source, 'c'); |
| 85 |
- is_deeply($dbi->dbi_option, {d => 1, e => 2});
|
|
| 85 |
+ is_deeply($dbi->dbi_options, {d => 1, e => 2});
|
|
| 86 | 86 |
is_deeply({$dbi->filters}, {f => 3});
|
| 87 | 87 |
is($dbi->bind_filter, 'f'); |
| 88 | 88 |
is($dbi->fetch_filter, 'g'); |
| 89 | 89 |
is($dbi->result_class, 'DBI::Custom::Result'); |
| 90 |
- is($dbi->sql_template->upper_case, 0); |
|
| 90 |
+ is($dbi->sql_template->tag_start, 0); |
|
| 91 | 91 |
isa_ok($dbi, 'DBI::Custom::T1'); |
| 92 | 92 |
|
| 93 | 93 |
} |
| ... | ... |
@@ -103,12 +103,12 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 103 | 103 |
is($dbi->user, 'a'); |
| 104 | 104 |
is($dbi->password, 'b'); |
| 105 | 105 |
is($dbi->data_source, 'c'); |
| 106 |
- is_deeply($dbi->dbi_option, {d => 1, e => 2});
|
|
| 106 |
+ is_deeply($dbi->dbi_options, {d => 1, e => 2});
|
|
| 107 | 107 |
is_deeply(scalar $dbi->filters, {f => 3});
|
| 108 | 108 |
is($dbi->bind_filter, 'f'); |
| 109 | 109 |
is($dbi->fetch_filter, 'g'); |
| 110 | 110 |
is($dbi->result_class, 'DBI::Custom::Result'); |
| 111 |
- is($dbi->sql_template->upper_case, 0); |
|
| 111 |
+ is($dbi->sql_template->tag_start, 0); |
|
| 112 | 112 |
|
| 113 | 113 |
isa_ok($dbi, 'DBI::Custom::T1_2'); |
| 114 | 114 |
} |
| ... | ... |
@@ -123,7 +123,7 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 123 | 123 |
->user('ao')
|
| 124 | 124 |
->password('bo')
|
| 125 | 125 |
->data_source('co')
|
| 126 |
- ->dbi_option({do => 10, eo => 20})
|
|
| 126 |
+ ->dbi_options({do => 10, eo => 20})
|
|
| 127 | 127 |
->filters( |
| 128 | 128 |
fo => 30 |
| 129 | 129 |
) |
| ... | ... |
@@ -140,12 +140,12 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 140 | 140 |
is($dbi->user, 'ao'); |
| 141 | 141 |
is($dbi->password, 'bo'); |
| 142 | 142 |
is($dbi->data_source, 'co'); |
| 143 |
- is_deeply($dbi->dbi_option, {do => 10, eo => 20});
|
|
| 143 |
+ is_deeply($dbi->dbi_options, {do => 10, eo => 20});
|
|
| 144 | 144 |
is_deeply(scalar $dbi->filters, {fo => 30});
|
| 145 | 145 |
is($dbi->bind_filter, 'fo'); |
| 146 | 146 |
is($dbi->fetch_filter, 'go'); |
| 147 | 147 |
is($dbi->result_class, 'ho'); |
| 148 |
- is($dbi->sql_template->upper_case, 1); |
|
| 148 |
+ is($dbi->sql_template->tag_start, 1); |
|
| 149 | 149 |
|
| 150 | 150 |
isa_ok($dbi, 'DBI::Custom::T1_3'); |
| 151 | 151 |
} |
| ... | ... |
@@ -155,7 +155,7 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 155 | 155 |
user => 'a', |
| 156 | 156 |
password => 'b', |
| 157 | 157 |
data_source => 'c', |
| 158 |
- dbi_option => {d => 1, e => 2},
|
|
| 158 |
+ dbi_options => {d => 1, e => 2},
|
|
| 159 | 159 |
filters => {
|
| 160 | 160 |
f => 3, |
| 161 | 161 |
}, |
| ... | ... |
@@ -168,93 +168,13 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 168 | 168 |
is($dbi->user, 'a'); |
| 169 | 169 |
is($dbi->password, 'b'); |
| 170 | 170 |
is($dbi->data_source, 'c'); |
| 171 |
- is_deeply($dbi->dbi_option, {d => 1, e => 2});
|
|
| 171 |
+ is_deeply($dbi->dbi_options, {d => 1, e => 2});
|
|
| 172 | 172 |
is_deeply({$dbi->filters}, {f => 3});
|
| 173 | 173 |
is($dbi->bind_filter, 'f'); |
| 174 | 174 |
is($dbi->fetch_filter, 'g'); |
| 175 | 175 |
is($dbi->result_class, 'h'); |
| 176 |
- is($dbi->sql_template->upper_case, 2); |
|
| 176 |
+ is($dbi->sql_template->tag_start, 2); |
|
| 177 | 177 |
|
| 178 | 178 |
isa_ok($dbi, 'DBI::Custom'); |
| 179 | 179 |
} |
| 180 | 180 |
|
| 181 |
-{
|
|
| 182 |
- my $dbi = DBI::Custom->new; |
|
| 183 |
- my $tmpl = "select * from table where {= title};";
|
|
| 184 |
- my $values = {title => 'a'};
|
|
| 185 |
- my ($sql, @bind) = $dbi->_create_sql($tmpl, $values); |
|
| 186 |
- is($sql, "select * from table where title = ?;", 'sql template'); |
|
| 187 |
- is_deeply(\@bind, ['a'], 'sql template bind' ); |
|
| 188 |
-} |
|
| 189 |
- |
|
| 190 |
-{
|
|
| 191 |
- # Expand place holer |
|
| 192 |
- my $dbi = DBI::Custom->new; |
|
| 193 |
- my $tmpl = "select * from table where {= k1} && {<> k2} && {< k3} && {> k4} && {>= k5} && {<= k6} && {like k7}";
|
|
| 194 |
- my $values = {k1 => 'a', k2 => 'b', k3 => 'c', k4 => 'd', k5 => 'e', k6 => 'f', k7 => 'g'};
|
|
| 195 |
- |
|
| 196 |
- $dbi->filters(filter => sub {
|
|
| 197 |
- my ($key, $value) = @_; |
|
| 198 |
- if ($key eq 'k1' && $value eq 'a') {
|
|
| 199 |
- return uc $value; |
|
| 200 |
- } |
|
| 201 |
- return $value; |
|
| 202 |
- }); |
|
| 203 |
- |
|
| 204 |
- my ($sql, @bind) = $dbi->_create_sql($tmpl, $values, $dbi->filters->{filter});
|
|
| 205 |
- |
|
| 206 |
- is($sql, "select * from table where k1 = ? && k2 <> ? && k3 < ? && k4 > ? && k5 >= ? && k6 <= ? && k7 like ?;", 'sql template2'); |
|
| 207 |
- is_deeply(\@bind, ['A', 'b', 'c', 'd', 'e', 'f', 'g'], 'sql template bind2' ); |
|
| 208 |
-} |
|
| 209 |
- |
|
| 210 |
-{
|
|
| 211 |
- # Expand place holer upper case |
|
| 212 |
- my $dbi = DBI::Custom->new; |
|
| 213 |
- $dbi->sql_template->upper_case(1); |
|
| 214 |
- my $tmpl = "select * from table where {like k7}";
|
|
| 215 |
- my $values = {k7 => 'g'};
|
|
| 216 |
- |
|
| 217 |
- my ($sql, @bind) = $dbi->_create_sql($tmpl, $values); |
|
| 218 |
- is($sql, "select * from table where k7 LIKE ?;", 'sql template2'); |
|
| 219 |
- is_deeply(\@bind, ['g'], 'sql template bind2' ); |
|
| 220 |
-} |
|
| 221 |
- |
|
| 222 |
- |
|
| 223 |
-{
|
|
| 224 |
- # Insert values |
|
| 225 |
- my $dbi = DBI::Custom->new; |
|
| 226 |
- my $tmpl = "insert into table {insert_values}";
|
|
| 227 |
- my $values = {insert_values => {k1 => 'a', k2 => 'b'}};
|
|
| 228 |
- |
|
| 229 |
- $dbi->filters(filter => sub {
|
|
| 230 |
- my ($key, $value) = @_; |
|
| 231 |
- if ($key eq 'k1' && $value eq 'a') {
|
|
| 232 |
- return uc $value; |
|
| 233 |
- } |
|
| 234 |
- return $value; |
|
| 235 |
- }); |
|
| 236 |
- |
|
| 237 |
- my ($sql, @bind) = $dbi->_create_sql($tmpl, $values, $dbi->filters->{filter});
|
|
| 238 |
- is($sql, "insert into table (k1, k2) values (?, ?);"); |
|
| 239 |
- is_deeply(\@bind, ['A', 'b'], 'sql template bind' ); |
|
| 240 |
-} |
|
| 241 |
- |
|
| 242 |
-{
|
|
| 243 |
- # Update set |
|
| 244 |
- my $dbi = DBI::Custom->new; |
|
| 245 |
- my $tmpl = "update table {update_set}";
|
|
| 246 |
- my $values = {update_set => {k1 => 'a', k2 => 'b'}};
|
|
| 247 |
- |
|
| 248 |
- $dbi->filters(filter => sub {
|
|
| 249 |
- my ($key, $value) = @_; |
|
| 250 |
- if ($key eq 'k1' && $value eq 'a') {
|
|
| 251 |
- return uc $value; |
|
| 252 |
- } |
|
| 253 |
- return $value; |
|
| 254 |
- }); |
|
| 255 |
- |
|
| 256 |
- my ($sql, @bind) = $dbi->_create_sql($tmpl, $values, $dbi->filters->{filter});
|
|
| 257 |
- is($sql, "update table set k1 = ?, k2 = ?;"); |
|
| 258 |
- is_deeply(\@bind, ['A', 'b'], 'sql template bind' ); |
|
| 259 |
-} |
|
| 260 |
- |
| ... | ... |
@@ -13,142 +13,176 @@ BEGIN {
|
| 13 | 13 |
use_ok('DBI::Custom');
|
| 14 | 14 |
} |
| 15 | 15 |
|
| 16 |
-package Test::DBI::Custom; |
|
| 17 |
-use Object::Simple; |
|
| 16 |
+# Function for test name |
|
| 17 |
+my $test; |
|
| 18 |
+sub test {
|
|
| 19 |
+ $test = shift; |
|
| 20 |
+} |
|
| 18 | 21 |
|
| 19 |
-sub dbi : Attr {}
|
|
| 22 |
+# Varialbe for test |
|
| 23 |
+my $dbi; |
|
| 24 |
+my $sth; |
|
| 25 |
+my $tmpl; |
|
| 26 |
+my $params; |
|
| 27 |
+my $sql; |
|
| 28 |
+my $result; |
|
| 29 |
+my @rows; |
|
| 30 |
+my $rows; |
|
| 31 |
+ |
|
| 32 |
+ |
|
| 33 |
+# Prepare table |
|
| 34 |
+$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
| 35 |
+$dbi->connect; |
|
| 36 |
+$dbi->dbh->do("create table table1 (key1 char(255), key2 char(255))");
|
|
| 37 |
+$sth = $dbi->dbh->prepare("insert into table1 (key1, key2) values (?, ?);");
|
|
| 38 |
+$sth->execute(1, 2); |
|
| 39 |
+$sth->execute(3, 4); |
|
| 20 | 40 |
|
| 21 |
-sub new {
|
|
| 22 |
- my $self = shift->SUPER::new; |
|
| 23 |
- my $dbi = DBI::Custom->new->data_source('dbi:SQLite:dbname=:memory:');
|
|
| 24 |
- |
|
| 25 |
- $dbi->connect; |
|
| 26 |
- $self->dbi($dbi); |
|
| 27 |
- return $self; |
|
| 28 |
-} |
|
| 29 | 41 |
|
| 30 |
-sub create_table {
|
|
| 31 |
- my ($self, $create_table) = @_; |
|
| 32 |
- $self->dbi->query_raw_sql($create_table); |
|
| 33 |
- return $self; |
|
| 42 |
+__END__ |
|
| 43 |
+$result = $dbi->execute("select key1, key2 from table1");
|
|
| 44 |
+ |
|
| 45 |
+@rows = (); |
|
| 46 |
+while (my $row = $result->fetch) {
|
|
| 47 |
+ push @rows, [@$row]; |
|
| 34 | 48 |
} |
| 49 |
+is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch'); |
|
| 50 |
+ |
|
| 51 |
+ |
|
| 52 |
+$result = $dbi->execute("select key1, key2 from table1");
|
|
| 35 | 53 |
|
| 36 |
-sub create_table1 {
|
|
| 37 |
- my $self = shift; |
|
| 38 |
- $self->create_table("create table t1 (k1 char(255), k2 char(255), k3 char(255), k4 char(255), k5 char(255));");
|
|
| 39 |
- return $self; |
|
| 54 |
+@rows = (); |
|
| 55 |
+while (my @row = $result->fetch) {
|
|
| 56 |
+ push @rows, [@row]; |
|
| 40 | 57 |
} |
| 58 |
+is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch list context'); |
|
| 41 | 59 |
|
| 42 |
-sub insert {
|
|
| 43 |
- my ($self, @values_list) = @_; |
|
| 44 |
- my $table = ref $params_list[0] ? '' : shift; |
|
| 45 |
- $table ||= 't1'; |
|
| 46 |
- |
|
| 47 |
- foreach my $params (@values_list) {
|
|
| 48 |
- my $sql = $self->dbi->execute( |
|
| 49 |
- "insert into $table {insert_values}", {insert_values => $params}
|
|
| 50 |
- ); |
|
| 51 |
- } |
|
| 52 |
- return $self; |
|
| 60 |
+ |
|
| 61 |
+$result = $dbi->execute("select key1, key2 from table1;");
|
|
| 62 |
+ |
|
| 63 |
+@rows = (); |
|
| 64 |
+while (my $row = $result->fetch_hash) {
|
|
| 65 |
+ push @rows, {%$row};
|
|
| 53 | 66 |
} |
| 67 |
+is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'fetch_hash');
|
|
| 54 | 68 |
|
| 55 |
-sub test {
|
|
| 56 |
- my ($self, $code) = @_; |
|
| 57 |
- $code->($self->dbi); |
|
| 69 |
+ |
|
| 70 |
+$result = $dbi->execute("select key1, key2 from table1;");
|
|
| 71 |
+ |
|
| 72 |
+@rows = (); |
|
| 73 |
+while (my %row = $result->fetch_hash) {
|
|
| 74 |
+ push @rows, {%row};
|
|
| 58 | 75 |
} |
| 76 |
+is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'fetch hash list context');
|
|
| 59 | 77 |
|
| 60 |
-Object::Simple->build_class; |
|
| 61 | 78 |
|
| 62 |
-package main; |
|
| 63 |
-my $t = Test::DBI::Custom->new; |
|
| 79 |
+$result = $dbi->execute("select key1, key2 from table1");
|
|
| 64 | 80 |
|
| 65 |
-$t->new->create_table1->insert({k1 => 1, k2 => 2}, {k1 => 3, k2 => 4})->test(sub {
|
|
| 66 |
- my $dbi = shift; |
|
| 67 |
- |
|
| 68 |
- my $r; # resultset |
|
| 69 |
- my @rows; |
|
| 70 |
- my $rows; |
|
| 71 |
- |
|
| 72 |
- $r = $dbi->execute("select k1, k2 from t1");
|
|
| 73 |
- |
|
| 74 |
- @rows = (); |
|
| 75 |
- while (my $row = $r->fetch) {
|
|
| 76 |
- push @rows, [@$row]; |
|
| 81 |
+$rows = $result->fetch_all; |
|
| 82 |
+is_deeply($rows, [[1, 2], [3, 4]], 'fetch_all'); |
|
| 83 |
+ |
|
| 84 |
+ |
|
| 85 |
+$result = $dbi->execute("select key1, key2 from table1");
|
|
| 86 |
+ |
|
| 87 |
+@rows = $result->fetch_all; |
|
| 88 |
+is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch_all list context'); |
|
| 89 |
+ |
|
| 90 |
+ |
|
| 91 |
+$result = $dbi->execute("select key1, key2 from table1");
|
|
| 92 |
+ |
|
| 93 |
+@rows = $result->fetch_all_hash; |
|
| 94 |
+is_deeply($rows, [[1, 2], [3, 4]], 'fetch_all_hash'); |
|
| 95 |
+ |
|
| 96 |
+ |
|
| 97 |
+$result = $dbi->execute("select key1, key2 from table1");
|
|
| 98 |
+ |
|
| 99 |
+@rows = $result->fetch_all; |
|
| 100 |
+is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch_all_hash list context'); |
|
| 101 |
+ |
|
| 102 |
+ |
|
| 103 |
+$dbi->fetch_filter(sub {
|
|
| 104 |
+ my ($key, $value, $type, $sth, $i) = @_; |
|
| 105 |
+ if ($key eq 'key1' && $value == 1 && $type =~ /char/i && $i == 0 && $sth->{TYPE}->[$i] eq $type) {
|
|
| 106 |
+ return $value * 3; |
|
| 77 | 107 |
} |
| 78 |
- is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch'); |
|
| 79 |
- |
|
| 80 |
- |
|
| 81 |
- $r = $dbi->execute("select k1, k2 from t1");
|
|
| 82 |
- |
|
| 83 |
- @rows = (); |
|
| 84 |
- while (my @row = $r->fetch) {
|
|
| 85 |
- push @rows, [@row]; |
|
| 108 |
+ return $value; |
|
| 109 |
+}); |
|
| 110 |
+ |
|
| 111 |
+$result = $dbi->execute("select key1, key2 from table1");
|
|
| 112 |
+ |
|
| 113 |
+$rows = $result->fetch_all; |
|
| 114 |
+ |
|
| 115 |
+is_deeply($rows, [[3, 2], [3, 4]], 'fetch_filter array'); |
|
| 116 |
+ |
|
| 117 |
+ |
|
| 118 |
+$result = $dbi->execute("select key1, key2 from table1");
|
|
| 119 |
+ |
|
| 120 |
+$rows = $result->fetch_all_hash; |
|
| 121 |
+ |
|
| 122 |
+is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 3, key2 => 4}], 'fetch_filter hash');
|
|
| 123 |
+ |
|
| 124 |
+ |
|
| 125 |
+ |
|
| 126 |
+# Expand place holer |
|
| 127 |
+my $dbi = DBI::Custom->new; |
|
| 128 |
+my $tmpl = "select * from table where {= key1} && {<> key2} && {< k3} && {> k4} && {>= k5} && {<= k6} && {like k7}";
|
|
| 129 |
+my $params = {key1 => 'a', key2 => 'b', k3 => 'c', k4 => 'd', k5 => 'e', k6 => 'f', k7 => 'g'};
|
|
| 130 |
+ |
|
| 131 |
+$dbi->filters(filter => sub {
|
|
| 132 |
+ my ($key, $value) = @_; |
|
| 133 |
+ if ($key eq 'key1' && $value eq 'a') {
|
|
| 134 |
+ return uc $value; |
|
| 86 | 135 |
} |
| 87 |
- is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch list context'); |
|
| 88 |
- |
|
| 89 |
- |
|
| 90 |
- $r = $dbi->execute("select k1, k2 from t1;");
|
|
| 91 |
- |
|
| 92 |
- @rows = (); |
|
| 93 |
- while (my $row = $r->fetch_hash) {
|
|
| 94 |
- push @rows, {%$row};
|
|
| 136 |
+ return $value; |
|
| 137 |
+}); |
|
| 138 |
+ |
|
| 139 |
+my ($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter});
|
|
| 140 |
+ |
|
| 141 |
+is($sql, "select * from table where key1 = ? && key2 <> ? && k3 < ? && k4 > ? && k5 >= ? && k6 <= ? && k7 like ?;", 'sql template2'); |
|
| 142 |
+is_deeply(\@bind, ['A', 'b', 'c', 'd', 'e', 'f', 'g'], 'sql template bind2' ); |
|
| 143 |
+ |
|
| 144 |
+# Expand place holer upper case |
|
| 145 |
+my $dbi = DBI::Custom->new; |
|
| 146 |
+$dbi->sql_template->upper_case(1); |
|
| 147 |
+my $tmpl = "select * from table where {like k7}";
|
|
| 148 |
+my $params = {k7 => 'g'};
|
|
| 149 |
+ |
|
| 150 |
+($sql, @bind_values) = $dbi->_create_sql($tmpl, $params); |
|
| 151 |
+is($sql, "select * from table where k7 LIKE ?;", 'sql template2'); |
|
| 152 |
+is_deeply(\@bind, ['g'], 'sql template bind2' ); |
|
| 153 |
+ |
|
| 154 |
+# Insert values |
|
| 155 |
+$dbi = DBI::Custom->new; |
|
| 156 |
+$tmpl = "insert into table {insert_values}";
|
|
| 157 |
+$params = {insert_values => {key1 => 'a', key2 => 'b'}};
|
|
| 158 |
+ |
|
| 159 |
+$dbi->filters(filter => sub {
|
|
| 160 |
+ my ($key, $value) = @_; |
|
| 161 |
+ if ($key eq 'key1' && $value eq 'a') {
|
|
| 162 |
+ return uc $value; |
|
| 95 | 163 |
} |
| 96 |
- is_deeply(\@rows, [{k1 => 1, k2 => 2}, {k1 => 3, k2 => 4}], 'fetch_hash');
|
|
| 97 |
- |
|
| 98 |
- |
|
| 99 |
- $r = $dbi->execute("select k1, k2 from t1;");
|
|
| 164 |
+ return $value; |
|
| 165 |
+}); |
|
| 100 | 166 |
|
| 101 |
- @rows = (); |
|
| 102 |
- while (my %row = $r->fetch_hash) {
|
|
| 103 |
- push @rows, {%row};
|
|
| 167 |
+($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter});
|
|
| 168 |
+is($sql, "insert into table (key1, key2) values (?, ?);"); |
|
| 169 |
+is_deeply(\@bind, ['A', 'b'], 'sql template bind' ); |
|
| 170 |
+ |
|
| 171 |
+# Update set |
|
| 172 |
+$dbi = DBI::Custom->new; |
|
| 173 |
+$tmpl = "update table {update_set}";
|
|
| 174 |
+$params = {update_set => {key1 => 'a', key2 => 'b'}};
|
|
| 175 |
+ |
|
| 176 |
+$dbi->filters(filter => sub {
|
|
| 177 |
+ my ($key, $value) = @_; |
|
| 178 |
+ if ($key eq 'key1' && $value eq 'a') {
|
|
| 179 |
+ return uc $value; |
|
| 104 | 180 |
} |
| 105 |
- is_deeply(\@rows, [{k1 => 1, k2 => 2}, {k1 => 3, k2 => 4}], 'fetch hash list context');
|
|
| 106 |
- |
|
| 107 |
- |
|
| 108 |
- $r = $dbi->execute("select k1, k2 from t1");
|
|
| 109 |
- |
|
| 110 |
- $rows = $r->fetch_all; |
|
| 111 |
- is_deeply($rows, [[1, 2], [3, 4]], 'fetch_all'); |
|
| 112 |
- |
|
| 113 |
- |
|
| 114 |
- $r = $dbi->execute("select k1, k2 from t1");
|
|
| 115 |
- |
|
| 116 |
- @rows = $r->fetch_all; |
|
| 117 |
- is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch_all list context'); |
|
| 118 |
- |
|
| 119 |
- |
|
| 120 |
- $r = $dbi->execute("select k1, k2 from t1");
|
|
| 121 |
- |
|
| 122 |
- @rows = $r->fetch_all_hash; |
|
| 123 |
- is_deeply($rows, [[1, 2], [3, 4]], 'fetch_all_hash'); |
|
| 124 |
- |
|
| 125 |
- |
|
| 126 |
- $r = $dbi->execute("select k1, k2 from t1");
|
|
| 127 |
- |
|
| 128 |
- @rows = $r->fetch_all; |
|
| 129 |
- is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch_all_hash list context'); |
|
| 130 |
- |
|
| 131 |
- |
|
| 132 |
- $dbi->fetch_filter(sub {
|
|
| 133 |
- my ($key, $value, $type, $sth, $i) = @_; |
|
| 134 |
- if ($key eq 'k1' && $value == 1 && $type =~ /char/i && $i == 0 && $sth->{TYPE}->[$i] eq $type) {
|
|
| 135 |
- return $value * 3; |
|
| 136 |
- } |
|
| 137 |
- return $value; |
|
| 138 |
- }); |
|
| 139 |
- |
|
| 140 |
- $r = $dbi->execute("select k1, k2 from t1");
|
|
| 141 |
- |
|
| 142 |
- $rows = $r->fetch_all; |
|
| 143 |
- |
|
| 144 |
- is_deeply($rows, [[3, 2], [3, 4]], 'fetch_filter array'); |
|
| 145 |
- |
|
| 146 |
- |
|
| 147 |
- $r = $dbi->execute("select k1, k2 from t1");
|
|
| 148 |
- |
|
| 149 |
- $rows = $r->fetch_all_hash; |
|
| 181 |
+ return $value; |
|
| 182 |
+}); |
|
| 150 | 183 |
|
| 151 |
- is_deeply($rows, [{k1 => 3, k2 => 2}, {k1 => 3, k2 => 4}], 'fetch_filter hash');
|
|
| 184 |
+($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter});
|
|
| 185 |
+is($sql, "update table set key1 = ?, key2 = ?;"); |
|
| 186 |
+is_deeply(\@bind, ['A', 'b'], 'sql template bind' ); |
|
| 152 | 187 |
|
| 153 |
-}); |
|
| 154 | 188 |
|