| ... | ... |
@@ -1,3 +1,10 @@ |
| 1 |
+0.1503 |
|
| 2 |
+ removed reconnect method |
|
| 3 |
+ removed connected method |
|
| 4 |
+ removed reconnect_memroy method |
|
| 5 |
+ renamed fetch_single to fetch_first |
|
| 6 |
+ renamed fetch_hash_single to fetch_hash_first |
|
| 7 |
+ updated document |
|
| 1 | 8 |
0.1502 |
| 2 | 9 |
added commit method |
| 3 | 10 |
added rollback method |
| ... | ... |
@@ -5,7 +12,6 @@ |
| 5 | 12 |
moved host attribute to DBIx::Custom::MySQL |
| 6 | 13 |
moved port attribute to DBIx::Custom::MySQL |
| 7 | 14 |
moved database attribute to DBIx::Custom::MySQL and DBIx::Custom::SQLite |
| 8 |
- |
|
| 9 | 15 |
0.1501 |
| 10 | 16 |
removed register_format() |
| 11 | 17 |
removed formats() |
| ... | ... |
@@ -26,32 +26,6 @@ __PACKAGE__->register_filter( |
| 26 | 26 |
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result'); |
| 27 | 27 |
__PACKAGE__->attr(sql_template => sub { DBIx::Custom::SQLTemplate->new });
|
| 28 | 28 |
|
| 29 |
-sub register_filter {
|
|
| 30 |
- my $invocant = shift; |
|
| 31 |
- |
|
| 32 |
- # Add filter |
|
| 33 |
- my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
|
|
| 34 |
- $invocant->filters({%{$invocant->filters}, %$filters});
|
|
| 35 |
- |
|
| 36 |
- return $invocant; |
|
| 37 |
-} |
|
| 38 |
- |
|
| 39 |
-sub auto_commit {
|
|
| 40 |
- my $self = shift; |
|
| 41 |
- |
|
| 42 |
- if (@_) {
|
|
| 43 |
- |
|
| 44 |
- # Set AutoCommit |
|
| 45 |
- $self->dbh->{AutoCommit} = $_[0];
|
|
| 46 |
- |
|
| 47 |
- return $self; |
|
| 48 |
- } |
|
| 49 |
- return $self->dbh->{AutoCommit};
|
|
| 50 |
-} |
|
| 51 |
- |
|
| 52 |
-sub commit { shift->dbh->commit }
|
|
| 53 |
-sub rollback { shift->dbh->rollback }
|
|
| 54 |
- |
|
| 55 | 29 |
sub connect {
|
| 56 | 30 |
my $proto = shift; |
| 57 | 31 |
|
| ... | ... |
@@ -84,186 +58,16 @@ sub connect {
|
| 84 | 58 |
return $self; |
| 85 | 59 |
} |
| 86 | 60 |
|
| 87 |
-sub DESTROY {
|
|
| 88 |
- my $self = shift; |
|
| 89 |
- |
|
| 90 |
- # Disconnect |
|
| 91 |
- $self->disconnect if $self->connected; |
|
| 92 |
-} |
|
| 93 |
- |
|
| 94 |
-sub connected { ref shift->{dbh} eq 'DBI::db' }
|
|
| 95 |
- |
|
| 96 | 61 |
sub disconnect {
|
| 97 | 62 |
my $self = shift; |
| 98 | 63 |
|
| 99 |
- if ($self->connected) {
|
|
| 100 |
- |
|
| 101 |
- # Disconnect |
|
| 102 |
- $self->dbh->disconnect; |
|
| 103 |
- delete $self->{dbh};
|
|
| 104 |
- } |
|
| 105 |
- |
|
| 106 |
- return $self; |
|
| 107 |
-} |
|
| 108 |
- |
|
| 109 |
-sub reconnect {
|
|
| 110 |
- my $self = shift; |
|
| 111 |
- |
|
| 112 |
- # Reconnect |
|
| 113 |
- $self->disconnect if $self->connected; |
|
| 114 |
- $self->connect; |
|
| 64 |
+ # Disconnect |
|
| 65 |
+ $self->dbh->disconnect; |
|
| 66 |
+ $self->dbh(undef); |
|
| 115 | 67 |
|
| 116 | 68 |
return $self; |
| 117 | 69 |
} |
| 118 | 70 |
|
| 119 |
-sub create_query {
|
|
| 120 |
- my ($self, $template) = @_; |
|
| 121 |
- |
|
| 122 |
- # Create query from SQL template |
|
| 123 |
- my $sql_template = $self->sql_template; |
|
| 124 |
- |
|
| 125 |
- # Get cached query |
|
| 126 |
- my $cache = $self->{_cache}->{$template};
|
|
| 127 |
- |
|
| 128 |
- # Create query |
|
| 129 |
- my $query; |
|
| 130 |
- if ($cache) {
|
|
| 131 |
- $query = DBIx::Custom::Query->new( |
|
| 132 |
- sql => $cache->sql, |
|
| 133 |
- columns => $cache->columns |
|
| 134 |
- ); |
|
| 135 |
- } |
|
| 136 |
- else {
|
|
| 137 |
- $query = eval{$sql_template->create_query($template)};
|
|
| 138 |
- croak($@) if $@; |
|
| 139 |
- |
|
| 140 |
- $self->{_cache}->{$template} = $query
|
|
| 141 |
- unless $self->{_cache}->{$template};
|
|
| 142 |
- } |
|
| 143 |
- |
|
| 144 |
- # Prepare statement handle |
|
| 145 |
- my $sth = $self->dbh->prepare($query->{sql});
|
|
| 146 |
- |
|
| 147 |
- # Set statement handle |
|
| 148 |
- $query->sth($sth); |
|
| 149 |
- |
|
| 150 |
- return $query; |
|
| 151 |
-} |
|
| 152 |
- |
|
| 153 |
-our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/;
|
|
| 154 |
- |
|
| 155 |
-sub execute{
|
|
| 156 |
- my $self = shift; |
|
| 157 |
- my $query = shift; |
|
| 158 |
- |
|
| 159 |
- # Arguments |
|
| 160 |
- my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
|
|
| 161 |
- |
|
| 162 |
- # Check arguments |
|
| 163 |
- foreach my $name (keys %$args) {
|
|
| 164 |
- croak "\"$name\" is invalid name" |
|
| 165 |
- unless $VALID_EXECUTE_ARGS{$name};
|
|
| 166 |
- } |
|
| 167 |
- |
|
| 168 |
- my $params = $args->{param} || {};
|
|
| 169 |
- |
|
| 170 |
- # First argument is SQL template |
|
| 171 |
- unless (ref $query eq 'DBIx::Custom::Query') {
|
|
| 172 |
- my $template; |
|
| 173 |
- |
|
| 174 |
- if (ref $query eq 'ARRAY') {
|
|
| 175 |
- $template = $query->[0]; |
|
| 176 |
- } |
|
| 177 |
- else { $template = $query }
|
|
| 178 |
- |
|
| 179 |
- $query = $self->create_query($template); |
|
| 180 |
- } |
|
| 181 |
- |
|
| 182 |
- my $filter = $args->{filter} || $query->filter || {};
|
|
| 183 |
- |
|
| 184 |
- # Create bind value |
|
| 185 |
- my $bind_values = $self->_build_bind_values($query, $params, $filter); |
|
| 186 |
- |
|
| 187 |
- # Execute |
|
| 188 |
- my $sth = $query->sth; |
|
| 189 |
- my $affected = eval{$sth->execute(@$bind_values)};
|
|
| 190 |
- |
|
| 191 |
- # Execute error |
|
| 192 |
- if (my $execute_error = $@) {
|
|
| 193 |
- require Data::Dumper; |
|
| 194 |
- my $sql = $query->{sql} || '';
|
|
| 195 |
- my $params_dump = Data::Dumper->Dump([$params], ['*params']); |
|
| 196 |
- |
|
| 197 |
- croak("$execute_error" .
|
|
| 198 |
- "<Your SQL>\n$sql\n" . |
|
| 199 |
- "<Your parameters>\n$params_dump"); |
|
| 200 |
- } |
|
| 201 |
- |
|
| 202 |
- # Return resultset if select statement is executed |
|
| 203 |
- if ($sth->{NUM_OF_FIELDS}) {
|
|
| 204 |
- |
|
| 205 |
- # Get result class |
|
| 206 |
- my $result_class = $self->result_class; |
|
| 207 |
- |
|
| 208 |
- # Create result |
|
| 209 |
- my $result = $result_class->new({
|
|
| 210 |
- sth => $sth, |
|
| 211 |
- default_filter => $self->default_fetch_filter, |
|
| 212 |
- filters => $self->filters |
|
| 213 |
- }); |
|
| 214 |
- return $result; |
|
| 215 |
- } |
|
| 216 |
- return $affected; |
|
| 217 |
-} |
|
| 218 |
- |
|
| 219 |
-sub _build_bind_values {
|
|
| 220 |
- my ($self, $query, $params, $filter) = @_; |
|
| 221 |
- |
|
| 222 |
- # binding values |
|
| 223 |
- my @bind_values; |
|
| 224 |
- |
|
| 225 |
- # Build bind values |
|
| 226 |
- my $count = {};
|
|
| 227 |
- foreach my $column (@{$query->columns}) {
|
|
| 228 |
- |
|
| 229 |
- croak "\"$column\" is not exists in params" |
|
| 230 |
- unless exists $params->{$column};
|
|
| 231 |
- |
|
| 232 |
- # Value |
|
| 233 |
- my $value = ref $params->{$column} eq 'ARRAY'
|
|
| 234 |
- ? $params->{$column}->[$count->{$column} || 0]
|
|
| 235 |
- : $params->{$column};
|
|
| 236 |
- |
|
| 237 |
- # Filter |
|
| 238 |
- $filter ||= {};
|
|
| 239 |
- |
|
| 240 |
- # Filter name |
|
| 241 |
- my $fname = $filter->{$column} || $self->default_query_filter || '';
|
|
| 242 |
- |
|
| 243 |
- my $filter_func; |
|
| 244 |
- if ($fname) {
|
|
| 245 |
- |
|
| 246 |
- if (ref $fname eq 'CODE') {
|
|
| 247 |
- $filter_func = $fname; |
|
| 248 |
- } |
|
| 249 |
- else {
|
|
| 250 |
- my $filters = $self->filters; |
|
| 251 |
- croak "Not exists filter \"$fname\"" unless exists $filters->{$fname};
|
|
| 252 |
- $filter_func = $filters->{$fname};
|
|
| 253 |
- } |
|
| 254 |
- } |
|
| 255 |
- |
|
| 256 |
- push @bind_values, $filter_func |
|
| 257 |
- ? $filter_func->($value) |
|
| 258 |
- : $value; |
|
| 259 |
- |
|
| 260 |
- # Count up |
|
| 261 |
- $count->{$column}++;
|
|
| 262 |
- } |
|
| 263 |
- |
|
| 264 |
- return \@bind_values; |
|
| 265 |
-} |
|
| 266 |
- |
|
| 267 | 71 |
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append filter/;
|
| 268 | 72 |
|
| 269 | 73 |
sub insert {
|
| ... | ... |
@@ -529,55 +333,239 @@ sub select {
|
| 529 | 333 |
return $result; |
| 530 | 334 |
} |
| 531 | 335 |
|
| 532 |
-=head1 NAME |
|
| 533 |
- |
|
| 534 |
-DBIx::Custom - DBI with hash parameter binding and filtering system |
|
| 535 |
- |
|
| 536 |
-=head1 VERSION |
|
| 537 |
- |
|
| 538 |
-Version 0.1502 |
|
| 539 |
- |
|
| 540 |
-=cut |
|
| 541 |
- |
|
| 542 |
-our $VERSION = '0.1502'; |
|
| 543 |
-$VERSION = eval $VERSION; |
|
| 544 |
- |
|
| 545 |
-=head1 STATE |
|
| 546 |
- |
|
| 547 |
-This module is not stable. Method name and functionality will be change. |
|
| 548 |
- |
|
| 549 |
-=head1 SYNOPSYS |
|
| 550 |
- |
|
| 551 |
- # Connect |
|
| 552 |
- my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books", |
|
| 553 |
- user => 'ken', password => '!LFKD%$&'); |
|
| 554 |
- |
|
| 555 |
- # Insert |
|
| 556 |
- $dbi->insert(table => 'books', |
|
| 557 |
- param => {title => 'perl', author => 'Ken'},
|
|
| 558 |
- filter => {title => 'encode_utf8'});
|
|
| 336 |
+sub create_query {
|
|
| 337 |
+ my ($self, $template) = @_; |
|
| 559 | 338 |
|
| 560 |
- # Update |
|
| 561 |
- $dbi->update(table => 'books', |
|
| 562 |
- param => {title => 'aaa', author => 'Ken'},
|
|
| 563 |
- where => {id => 5},
|
|
| 564 |
- filter => {title => 'encode_utf8'});
|
|
| 339 |
+ # Create query from SQL template |
|
| 340 |
+ my $sql_template = $self->sql_template; |
|
| 565 | 341 |
|
| 566 |
- # Update all |
|
| 567 |
- $dbi->update_all(table => 'books', |
|
| 568 |
- param => {title => 'aaa'},
|
|
| 569 |
- filter => {title => 'encode_utf8'});
|
|
| 342 |
+ # Get cached query |
|
| 343 |
+ my $cache = $self->{_cache}->{$template};
|
|
| 570 | 344 |
|
| 571 |
- # Delete |
|
| 572 |
- $dbi->delete(table => 'books', |
|
| 573 |
- where => {author => 'Ken'},
|
|
| 574 |
- filter => {title => 'encode_utf8'});
|
|
| 345 |
+ # Create query |
|
| 346 |
+ my $query; |
|
| 347 |
+ if ($cache) {
|
|
| 348 |
+ $query = DBIx::Custom::Query->new( |
|
| 349 |
+ sql => $cache->sql, |
|
| 350 |
+ columns => $cache->columns |
|
| 351 |
+ ); |
|
| 352 |
+ } |
|
| 353 |
+ else {
|
|
| 354 |
+ $query = eval{$sql_template->create_query($template)};
|
|
| 355 |
+ croak($@) if $@; |
|
| 356 |
+ |
|
| 357 |
+ $self->{_cache}->{$template} = $query
|
|
| 358 |
+ unless $self->{_cache}->{$template};
|
|
| 359 |
+ } |
|
| 575 | 360 |
|
| 576 |
- # Delete all |
|
| 577 |
- $dbi->delete_all(table => 'books'); |
|
| 361 |
+ # Prepare statement handle |
|
| 362 |
+ my $sth = $self->dbh->prepare($query->{sql});
|
|
| 578 | 363 |
|
| 579 |
- # Select |
|
| 580 |
- my $result = $dbi->select(table => 'books'); |
|
| 364 |
+ # Set statement handle |
|
| 365 |
+ $query->sth($sth); |
|
| 366 |
+ |
|
| 367 |
+ return $query; |
|
| 368 |
+} |
|
| 369 |
+ |
|
| 370 |
+our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/;
|
|
| 371 |
+ |
|
| 372 |
+sub execute{
|
|
| 373 |
+ my $self = shift; |
|
| 374 |
+ my $query = shift; |
|
| 375 |
+ |
|
| 376 |
+ # Arguments |
|
| 377 |
+ my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
|
|
| 378 |
+ |
|
| 379 |
+ # Check arguments |
|
| 380 |
+ foreach my $name (keys %$args) {
|
|
| 381 |
+ croak "\"$name\" is invalid name" |
|
| 382 |
+ unless $VALID_EXECUTE_ARGS{$name};
|
|
| 383 |
+ } |
|
| 384 |
+ |
|
| 385 |
+ my $params = $args->{param} || {};
|
|
| 386 |
+ |
|
| 387 |
+ # First argument is SQL template |
|
| 388 |
+ unless (ref $query eq 'DBIx::Custom::Query') {
|
|
| 389 |
+ my $template; |
|
| 390 |
+ |
|
| 391 |
+ if (ref $query eq 'ARRAY') {
|
|
| 392 |
+ $template = $query->[0]; |
|
| 393 |
+ } |
|
| 394 |
+ else { $template = $query }
|
|
| 395 |
+ |
|
| 396 |
+ $query = $self->create_query($template); |
|
| 397 |
+ } |
|
| 398 |
+ |
|
| 399 |
+ my $filter = $args->{filter} || $query->filter || {};
|
|
| 400 |
+ |
|
| 401 |
+ # Create bind value |
|
| 402 |
+ my $bind_values = $self->_build_bind_values($query, $params, $filter); |
|
| 403 |
+ |
|
| 404 |
+ # Execute |
|
| 405 |
+ my $sth = $query->sth; |
|
| 406 |
+ my $affected = eval{$sth->execute(@$bind_values)};
|
|
| 407 |
+ |
|
| 408 |
+ # Execute error |
|
| 409 |
+ if (my $execute_error = $@) {
|
|
| 410 |
+ require Data::Dumper; |
|
| 411 |
+ my $sql = $query->{sql} || '';
|
|
| 412 |
+ my $params_dump = Data::Dumper->Dump([$params], ['*params']); |
|
| 413 |
+ |
|
| 414 |
+ croak("$execute_error" .
|
|
| 415 |
+ "<Your SQL>\n$sql\n" . |
|
| 416 |
+ "<Your parameters>\n$params_dump"); |
|
| 417 |
+ } |
|
| 418 |
+ |
|
| 419 |
+ # Return resultset if select statement is executed |
|
| 420 |
+ if ($sth->{NUM_OF_FIELDS}) {
|
|
| 421 |
+ |
|
| 422 |
+ # Get result class |
|
| 423 |
+ my $result_class = $self->result_class; |
|
| 424 |
+ |
|
| 425 |
+ # Create result |
|
| 426 |
+ my $result = $result_class->new({
|
|
| 427 |
+ sth => $sth, |
|
| 428 |
+ default_filter => $self->default_fetch_filter, |
|
| 429 |
+ filters => $self->filters |
|
| 430 |
+ }); |
|
| 431 |
+ return $result; |
|
| 432 |
+ } |
|
| 433 |
+ return $affected; |
|
| 434 |
+} |
|
| 435 |
+ |
|
| 436 |
+sub _build_bind_values {
|
|
| 437 |
+ my ($self, $query, $params, $filter) = @_; |
|
| 438 |
+ |
|
| 439 |
+ # binding values |
|
| 440 |
+ my @bind_values; |
|
| 441 |
+ |
|
| 442 |
+ # Build bind values |
|
| 443 |
+ my $count = {};
|
|
| 444 |
+ foreach my $column (@{$query->columns}) {
|
|
| 445 |
+ |
|
| 446 |
+ croak "\"$column\" is not exists in params" |
|
| 447 |
+ unless exists $params->{$column};
|
|
| 448 |
+ |
|
| 449 |
+ # Value |
|
| 450 |
+ my $value = ref $params->{$column} eq 'ARRAY'
|
|
| 451 |
+ ? $params->{$column}->[$count->{$column} || 0]
|
|
| 452 |
+ : $params->{$column};
|
|
| 453 |
+ |
|
| 454 |
+ # Filter |
|
| 455 |
+ $filter ||= {};
|
|
| 456 |
+ |
|
| 457 |
+ # Filter name |
|
| 458 |
+ my $fname = $filter->{$column} || $self->default_query_filter || '';
|
|
| 459 |
+ |
|
| 460 |
+ my $filter_func; |
|
| 461 |
+ if ($fname) {
|
|
| 462 |
+ |
|
| 463 |
+ if (ref $fname eq 'CODE') {
|
|
| 464 |
+ $filter_func = $fname; |
|
| 465 |
+ } |
|
| 466 |
+ else {
|
|
| 467 |
+ my $filters = $self->filters; |
|
| 468 |
+ croak "Not exists filter \"$fname\"" unless exists $filters->{$fname};
|
|
| 469 |
+ $filter_func = $filters->{$fname};
|
|
| 470 |
+ } |
|
| 471 |
+ } |
|
| 472 |
+ |
|
| 473 |
+ push @bind_values, $filter_func |
|
| 474 |
+ ? $filter_func->($value) |
|
| 475 |
+ : $value; |
|
| 476 |
+ |
|
| 477 |
+ # Count up |
|
| 478 |
+ $count->{$column}++;
|
|
| 479 |
+ } |
|
| 480 |
+ |
|
| 481 |
+ return \@bind_values; |
|
| 482 |
+} |
|
| 483 |
+ |
|
| 484 |
+sub register_filter {
|
|
| 485 |
+ my $invocant = shift; |
|
| 486 |
+ |
|
| 487 |
+ # Add filter |
|
| 488 |
+ my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
|
|
| 489 |
+ $invocant->filters({%{$invocant->filters}, %$filters});
|
|
| 490 |
+ |
|
| 491 |
+ return $invocant; |
|
| 492 |
+} |
|
| 493 |
+ |
|
| 494 |
+sub auto_commit {
|
|
| 495 |
+ my $self = shift; |
|
| 496 |
+ |
|
| 497 |
+ if (@_) {
|
|
| 498 |
+ |
|
| 499 |
+ # Set AutoCommit |
|
| 500 |
+ $self->dbh->{AutoCommit} = $_[0];
|
|
| 501 |
+ |
|
| 502 |
+ return $self; |
|
| 503 |
+ } |
|
| 504 |
+ return $self->dbh->{AutoCommit};
|
|
| 505 |
+} |
|
| 506 |
+ |
|
| 507 |
+sub commit { shift->dbh->commit }
|
|
| 508 |
+sub rollback { shift->dbh->rollback }
|
|
| 509 |
+ |
|
| 510 |
+sub DESTROY {
|
|
| 511 |
+ my $self = shift; |
|
| 512 |
+ |
|
| 513 |
+ # Disconnect |
|
| 514 |
+ $self->disconnect if $self->dbh; |
|
| 515 |
+} |
|
| 516 |
+ |
|
| 517 |
+=head1 NAME |
|
| 518 |
+ |
|
| 519 |
+DBIx::Custom - DBI with hash parameter binding and filtering system |
|
| 520 |
+ |
|
| 521 |
+=head1 VERSION |
|
| 522 |
+ |
|
| 523 |
+Version 0.1503 |
|
| 524 |
+ |
|
| 525 |
+=cut |
|
| 526 |
+ |
|
| 527 |
+our $VERSION = '0.1503'; |
|
| 528 |
+$VERSION = eval $VERSION; |
|
| 529 |
+ |
|
| 530 |
+=head1 STABILITY |
|
| 531 |
+ |
|
| 532 |
+This module is not stable. Method name and functionality will be change. |
|
| 533 |
+ |
|
| 534 |
+=head1 SYNOPSYS |
|
| 535 |
+ |
|
| 536 |
+ # Connect |
|
| 537 |
+ my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books", |
|
| 538 |
+ user => 'ken', password => '!LFKD%$&'); |
|
| 539 |
+ |
|
| 540 |
+ # Disconnect |
|
| 541 |
+ $dbi->disconnect |
|
| 542 |
+ |
|
| 543 |
+ # Insert |
|
| 544 |
+ $dbi->insert(table => 'books', |
|
| 545 |
+ param => {title => 'perl', author => 'Ken'},
|
|
| 546 |
+ filter => {title => 'encode_utf8'});
|
|
| 547 |
+ |
|
| 548 |
+ # Update |
|
| 549 |
+ $dbi->update(table => 'books', |
|
| 550 |
+ param => {title => 'aaa', author => 'Ken'},
|
|
| 551 |
+ where => {id => 5},
|
|
| 552 |
+ filter => {title => 'encode_utf8'});
|
|
| 553 |
+ |
|
| 554 |
+ # Update all |
|
| 555 |
+ $dbi->update_all(table => 'books', |
|
| 556 |
+ param => {title => 'aaa'},
|
|
| 557 |
+ filter => {title => 'encode_utf8'});
|
|
| 558 |
+ |
|
| 559 |
+ # Delete |
|
| 560 |
+ $dbi->delete(table => 'books', |
|
| 561 |
+ where => {author => 'Ken'},
|
|
| 562 |
+ filter => {title => 'encode_utf8'});
|
|
| 563 |
+ |
|
| 564 |
+ # Delete all |
|
| 565 |
+ $dbi->delete_all(table => 'books'); |
|
| 566 |
+ |
|
| 567 |
+ # Select |
|
| 568 |
+ my $result = $dbi->select(table => 'books'); |
|
| 581 | 569 |
|
| 582 | 570 |
# Select(more complex) |
| 583 | 571 |
my $result = $dbi->select( |
| ... | ... |
@@ -594,7 +582,7 @@ This module is not stable. Method name and functionality will be change. |
| 594 | 582 |
column => ['books.name as book_name'] |
| 595 | 583 |
relation => {'books.id' => 'rental.book_id'}
|
| 596 | 584 |
); |
| 597 |
- |
|
| 585 |
+ |
|
| 598 | 586 |
# Execute SQL |
| 599 | 587 |
$dbi->execute("select title from books");
|
| 600 | 588 |
|
| ... | ... |
@@ -602,6 +590,12 @@ This module is not stable. Method name and functionality will be change. |
| 602 | 590 |
$dbi->execute("select id from books where {= author} && {like title}",
|
| 603 | 591 |
param => {author => 'ken', title => '%Perl%'},
|
| 604 | 592 |
filter => {tilte => 'encode_utf8'});
|
| 593 |
+ |
|
| 594 |
+ # Create query and execute it |
|
| 595 |
+ my $query = $dbi->create_query( |
|
| 596 |
+ "select id from books where {= author} && {like title}"
|
|
| 597 |
+ ); |
|
| 598 |
+ $dbi->execute($query, param => {author => 'ken', title => '%Perl%'})
|
|
| 605 | 599 |
|
| 606 | 600 |
# Default filter |
| 607 | 601 |
$dbi->default_query_filter('encode_utf8');
|
| ... | ... |
@@ -619,7 +613,33 @@ This module is not stable. Method name and functionality will be change. |
| 619 | 613 |
|
| 620 | 614 |
# DBI instance |
| 621 | 615 |
my $dbh = $dbi->dbh; |
| 622 |
- |
|
| 616 |
+ |
|
| 617 |
+=head1 DESCRIPTION |
|
| 618 |
+ |
|
| 619 |
+L<DBIx::Custom> is useful L<DBI> extention. |
|
| 620 |
+This module have hash parameter binding and filtering system. |
|
| 621 |
+ |
|
| 622 |
+Normally, binding parameter is array. |
|
| 623 |
+L<DBIx::Custom> enable you to pass binding parameter as hash. |
|
| 624 |
+ |
|
| 625 |
+This module also provide filtering system. |
|
| 626 |
+You can filter the binding parameter |
|
| 627 |
+or the value of fetching row. |
|
| 628 |
+ |
|
| 629 |
+And have useful method such as insert(), update(), delete(), and select(). |
|
| 630 |
+ |
|
| 631 |
+=head2 Features |
|
| 632 |
+ |
|
| 633 |
+=over 4 |
|
| 634 |
+ |
|
| 635 |
+=item 1. Hash parameter binding. |
|
| 636 |
+ |
|
| 637 |
+=item 2. Value filtering. |
|
| 638 |
+ |
|
| 639 |
+=item 3. Useful methos such as insert(), update(), delete(), and select(). |
|
| 640 |
+ |
|
| 641 |
+=back |
|
| 642 |
+ |
|
| 623 | 643 |
=head1 ATTRIBUTES |
| 624 | 644 |
|
| 625 | 645 |
=head2 user |
| ... | ... |
@@ -643,19 +663,18 @@ Database data source. |
| 643 | 663 |
$dbi = $dbi->data_source("dbi:mysql:dbname=$database");
|
| 644 | 664 |
$data_source = $dbi->data_source; |
| 645 | 665 |
|
| 646 |
-If you know data source more, See also L<DBI>. |
|
| 647 |
- |
|
| 648 |
-=head2 sql_template |
|
| 649 |
- |
|
| 650 |
-SQLTemplate instance. sql_template attribute must be |
|
| 651 |
-the instance of L<DBIx::Cutom::SQLTemplate> subclass. |
|
| 666 |
+=head2 dbh |
|
| 652 | 667 |
|
| 653 |
- $dbi = $dbi->sql_template(DBIx::Cutom::SQLTemplate->new); |
|
| 654 |
- $sql_template = $dbi->sql_template; |
|
| 668 |
+Database handle. This is the innstance of L<DBI> |
|
| 669 |
+ |
|
| 670 |
+ $dbi = $dbi->dbh($dbh); |
|
| 671 |
+ $dbh = $dbi->dbh; |
|
| 655 | 672 |
|
| 656 |
-the instance of DBIx::Cutom::SQLTemplate is set to |
|
| 657 |
-this attribute by default. |
|
| 673 |
+You can use all methods of L<DBI> |
|
| 658 | 674 |
|
| 675 |
+ my $sth = $dbi->dbh->prepare("...");
|
|
| 676 |
+ my $errstr = $dbi->dbh->errstr; |
|
| 677 |
+ |
|
| 659 | 678 |
=head2 filters |
| 660 | 679 |
|
| 661 | 680 |
Filters |
| ... | ... |
@@ -691,49 +710,21 @@ Result class. |
| 691 | 710 |
|
| 692 | 711 |
L<DBIx::Custom::Result> is set to this attribute by default. |
| 693 | 712 |
|
| 694 |
-=head2 dbh |
|
| 695 |
- |
|
| 696 |
-Database handle. |
|
| 697 |
- |
|
| 698 |
- $dbi = $dbi->dbh($dbh); |
|
| 699 |
- $dbh = $dbi->dbh; |
|
| 700 |
- |
|
| 701 |
-=head1 METHODS |
|
| 702 |
- |
|
| 703 |
-This class is L<Object::Simple> subclass. |
|
| 704 |
-You can use all methods of L<Object::Simple> |
|
| 705 |
- |
|
| 706 |
-=head2 auto_commit |
|
| 707 |
- |
|
| 708 |
-Auto commit. |
|
| 709 |
- |
|
| 710 |
- $self = $dbi->auto_commit(1); |
|
| 711 |
- $auto_commit = $dbi->auto_commit; |
|
| 712 |
- |
|
| 713 |
-This is equal to |
|
| 714 |
- |
|
| 715 |
- $dbi->dbh->{AutoCommit} = 1;
|
|
| 716 |
- $auto_commit = $dbi->dbh->{AutoCommit};
|
|
| 717 |
- |
|
| 718 |
-=head2 commit |
|
| 719 |
- |
|
| 720 |
-Commit. |
|
| 721 |
- |
|
| 722 |
- $dbi->commit; |
|
| 723 |
- |
|
| 724 |
-This is equal to |
|
| 725 |
- |
|
| 726 |
- $dbi->dbh->commit; |
|
| 713 |
+=head2 sql_template |
|
| 727 | 714 |
|
| 728 |
-=head2 rollback |
|
| 715 |
+SQLTemplate instance. sql_template attribute must be |
|
| 716 |
+the instance of L<DBIx::Cutom::SQLTemplate> subclass. |
|
| 729 | 717 |
|
| 730 |
-Rollback. |
|
| 718 |
+ $dbi = $dbi->sql_template(DBIx::Cutom::SQLTemplate->new); |
|
| 719 |
+ $sql_template = $dbi->sql_template; |
|
| 731 | 720 |
|
| 732 |
- $dbi->rollback |
|
| 721 |
+the instance of DBIx::Cutom::SQLTemplate is set to |
|
| 722 |
+this attribute by default. |
|
| 733 | 723 |
|
| 734 |
-This is equal to |
|
| 724 |
+=head1 METHODS |
|
| 735 | 725 |
|
| 736 |
- $dbi->dbh->rollback; |
|
| 726 |
+This class is L<Object::Simple> subclass. |
|
| 727 |
+You can use all methods of L<Object::Simple> |
|
| 737 | 728 |
|
| 738 | 729 |
=head2 connect |
| 739 | 730 |
|
| ... | ... |
@@ -753,70 +744,6 @@ Disconnect database. |
| 753 | 744 |
|
| 754 | 745 |
If database is already disconnected, this method do nothing. |
| 755 | 746 |
|
| 756 |
-=head2 reconnect |
|
| 757 |
- |
|
| 758 |
-Reconnect to database. |
|
| 759 |
- |
|
| 760 |
- $dbi->reconnect; |
|
| 761 |
- |
|
| 762 |
-=head2 connected |
|
| 763 |
- |
|
| 764 |
-Check if database is connected. |
|
| 765 |
- |
|
| 766 |
- $is_connected = $dbi->connected; |
|
| 767 |
- |
|
| 768 |
-=head2 register_filter |
|
| 769 |
- |
|
| 770 |
-Resister filter. |
|
| 771 |
- |
|
| 772 |
- $dbi->register_filter(%filters); |
|
| 773 |
- |
|
| 774 |
-Example. |
|
| 775 |
- |
|
| 776 |
- $dbi->register_filter( |
|
| 777 |
- encode_utf8 => sub {
|
|
| 778 |
- my $value = shift; |
|
| 779 |
- |
|
| 780 |
- require Encode; |
|
| 781 |
- |
|
| 782 |
- return Encode::encode('UTF-8', $value);
|
|
| 783 |
- }, |
|
| 784 |
- decode_utf8 => sub {
|
|
| 785 |
- my $value = shift; |
|
| 786 |
- |
|
| 787 |
- require Encode; |
|
| 788 |
- |
|
| 789 |
- return Encode::decode('UTF-8', $value)
|
|
| 790 |
- } |
|
| 791 |
- ); |
|
| 792 |
- |
|
| 793 |
-=head2 create_query |
|
| 794 |
- |
|
| 795 |
-Create the instance of L<DBIx::Custom::Query>. |
|
| 796 |
-This receive the string written by SQL template. |
|
| 797 |
- |
|
| 798 |
- my $query = $dbi->create_query("select * from authors where {= name} and {= age}");
|
|
| 799 |
- |
|
| 800 |
-=head2 execute |
|
| 801 |
- |
|
| 802 |
-Execute the query or the string written by SQL template. |
|
| 803 |
- |
|
| 804 |
- $result = $dbi->execute($query, param => $params, filter => {%filter});
|
|
| 805 |
- $result = $dbi->execute($template, param => $params, filter => {%filter});
|
|
| 806 |
- |
|
| 807 |
-Example. |
|
| 808 |
- |
|
| 809 |
- $result = $dbi->execute("select * from authors where {= name} and {= age}",
|
|
| 810 |
- {name => 'taro', age => 19});
|
|
| 811 |
- |
|
| 812 |
- while (my @row = $result->fetch) {
|
|
| 813 |
- # do something |
|
| 814 |
- } |
|
| 815 |
- |
|
| 816 |
-See also L<DBIx::Custom::SQLTemplate>. |
|
| 817 |
- |
|
| 818 |
-Returned value L<DBIx::Custom::Result> instance. |
|
| 819 |
- |
|
| 820 | 747 |
=head2 insert |
| 821 | 748 |
|
| 822 | 749 |
Insert row. |
| ... | ... |
@@ -828,7 +755,7 @@ Insert row. |
| 828 | 755 |
|
| 829 | 756 |
Retruned value is affected rows count. |
| 830 | 757 |
|
| 831 |
-Example. |
|
| 758 |
+Example: |
|
| 832 | 759 |
|
| 833 | 760 |
# insert |
| 834 | 761 |
$dbi->insert(table => 'books', |
| ... | ... |
@@ -848,7 +775,7 @@ Update rows. |
| 848 | 775 |
|
| 849 | 776 |
Retruned value is affected rows count |
| 850 | 777 |
|
| 851 |
-Example. |
|
| 778 |
+Example: |
|
| 852 | 779 |
|
| 853 | 780 |
#update |
| 854 | 781 |
$dbi->update(table => 'books', |
| ... | ... |
@@ -868,7 +795,7 @@ Update all rows. |
| 868 | 795 |
|
| 869 | 796 |
Retruned value is affected rows count. |
| 870 | 797 |
|
| 871 |
-Example. |
|
| 798 |
+Example: |
|
| 872 | 799 |
|
| 873 | 800 |
# update_all |
| 874 | 801 |
$dbi->update_all(table => 'books', |
| ... | ... |
@@ -886,13 +813,13 @@ Delete rows. |
| 886 | 813 |
|
| 887 | 814 |
Retrun value is affected rows count |
| 888 | 815 |
|
| 889 |
-Example. |
|
| 816 |
+Example: |
|
| 890 | 817 |
|
| 891 | 818 |
# delete |
| 892 | 819 |
$dbi->delete(table => 'books', |
| 893 | 820 |
where => {id => 5},
|
| 894 | 821 |
append => 'some statement', |
| 895 |
- filter => {id => 'encode_utf8');
|
|
| 822 |
+ filter => {id => 'encode_utf8'});
|
|
| 896 | 823 |
|
| 897 | 824 |
=head2 delete_all |
| 898 | 825 |
|
| ... | ... |
@@ -902,10 +829,10 @@ Delete all rows. |
| 902 | 829 |
|
| 903 | 830 |
Retruned value is affected rows count. |
| 904 | 831 |
|
| 905 |
-Example |
|
| 832 |
+Example: |
|
| 906 | 833 |
|
| 907 | 834 |
# delete_all |
| 908 |
- $dbi->delete_all('books');
|
|
| 835 |
+ $dbi->delete_all(table => 'books'); |
|
| 909 | 836 |
|
| 910 | 837 |
=head2 select |
| 911 | 838 |
|
| ... | ... |
@@ -915,23 +842,23 @@ Select rows. |
| 915 | 842 |
column => [@column], |
| 916 | 843 |
where => {%where},
|
| 917 | 844 |
append => $append, |
| 918 |
- relation => {%relation}
|
|
| 845 |
+ relation => {%relation},
|
|
| 919 | 846 |
filter => {%filter});
|
| 920 | 847 |
|
| 921 |
-Returned value is L<DBIx::Custom::Result> instance. |
|
| 848 |
+Return value is the instance of L<DBIx::Custom::Result>. |
|
| 922 | 849 |
|
| 923 |
-Example. |
|
| 850 |
+Example: |
|
| 924 | 851 |
|
| 925 | 852 |
# select * from books; |
| 926 |
- $result = $dbi->select('books');
|
|
| 853 |
+ $result = $dbi->select(table => 'books'); |
|
| 927 | 854 |
|
| 928 | 855 |
# select * from books where title = 'Perl'; |
| 929 |
- $result = $dbi->select('books', {title => 1});
|
|
| 856 |
+ $result = $dbi->select(table => 'books', where => {title => 1});
|
|
| 930 | 857 |
|
| 931 | 858 |
# select title, author from books where id = 1 for update; |
| 932 | 859 |
$result = $dbi->select( |
| 933 | 860 |
table => 'books', |
| 934 |
- column => ['title', 'author'], |
|
| 861 |
+ column => ['title', 'author'], |
|
| 935 | 862 |
where => {id => 1},
|
| 936 | 863 |
appned => 'for update' |
| 937 | 864 |
); |
| ... | ... |
@@ -939,18 +866,98 @@ Example. |
| 939 | 866 |
# select books.name as book_name from books, rental |
| 940 | 867 |
# where books.id = rental.book_id; |
| 941 | 868 |
my $result = $dbi->select( |
| 942 |
- table => ['books', 'rental'], |
|
| 943 |
- column => ['books.name as book_name'] |
|
| 869 |
+ table => ['books', 'rental'], |
|
| 870 |
+ column => ['books.name as book_name'] |
|
| 944 | 871 |
relation => {'books.id' => 'rental.book_id'}
|
| 945 | 872 |
); |
| 946 | 873 |
|
| 947 |
-=head1 AUTHOR |
|
| 874 |
+=head2 create_query |
|
| 875 |
+ |
|
| 876 |
+Create the instance of L<DBIx::Custom::Query>. |
|
| 877 |
+This receive the string written by SQL template. |
|
| 948 | 878 |
|
| 949 |
-Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
| 879 |
+ my $query = $dbi->create_query("select * from authors where {= name} and {= age}");
|
|
| 880 |
+ |
|
| 881 |
+=head2 execute |
|
| 882 |
+ |
|
| 883 |
+Execute the instace of L<DBIx::Custom::Query> or |
|
| 884 |
+the string written by SQL template. |
|
| 885 |
+Return value is the instance of L<DBIx::Custom::Result>. |
|
| 886 |
+ |
|
| 887 |
+ $result = $dbi->execute($query, param => $params, filter => {%filter});
|
|
| 888 |
+ $result = $dbi->execute($template, param => $params, filter => {%filter});
|
|
| 889 |
+ |
|
| 890 |
+Example: |
|
| 891 |
+ |
|
| 892 |
+ $result = $dbi->execute("select * from authors where {= name} and {= age}",
|
|
| 893 |
+ param => {name => 'taro', age => 19});
|
|
| 894 |
+ |
|
| 895 |
+ while (my $row = $result->fetch) {
|
|
| 896 |
+ # do something |
|
| 897 |
+ } |
|
| 898 |
+ |
|
| 899 |
+See also L<DBIx::Custom::SQLTemplate> to know how to write SQL template. |
|
| 900 |
+ |
|
| 901 |
+=head2 register_filter |
|
| 902 |
+ |
|
| 903 |
+Resister filter. |
|
| 904 |
+ |
|
| 905 |
+ $dbi->register_filter(%filters); |
|
| 906 |
+ |
|
| 907 |
+Example: |
|
| 950 | 908 |
|
| 951 |
-Github L<http://github.com/yuki-kimoto> |
|
| 909 |
+ $dbi->register_filter( |
|
| 910 |
+ encode_utf8 => sub {
|
|
| 911 |
+ my $value = shift; |
|
| 912 |
+ |
|
| 913 |
+ require Encode; |
|
| 914 |
+ |
|
| 915 |
+ return Encode::encode('UTF-8', $value);
|
|
| 916 |
+ }, |
|
| 917 |
+ decode_utf8 => sub {
|
|
| 918 |
+ my $value = shift; |
|
| 919 |
+ |
|
| 920 |
+ require Encode; |
|
| 921 |
+ |
|
| 922 |
+ return Encode::decode('UTF-8', $value)
|
|
| 923 |
+ } |
|
| 924 |
+ ); |
|
| 925 |
+ |
|
| 926 |
+=head2 auto_commit |
|
| 927 |
+ |
|
| 928 |
+Auto commit. |
|
| 952 | 929 |
|
| 953 |
-I develope this module on L<http://github.com/yuki-kimoto/DBIx-Custom> |
|
| 930 |
+ $self = $dbi->auto_commit(1); |
|
| 931 |
+ $auto_commit = $dbi->auto_commit; |
|
| 932 |
+ |
|
| 933 |
+This is equal to |
|
| 934 |
+ |
|
| 935 |
+ $dbi->dbh->{AutoCommit} = 1;
|
|
| 936 |
+ $auto_commit = $dbi->dbh->{AutoCommit};
|
|
| 937 |
+ |
|
| 938 |
+=head2 commit |
|
| 939 |
+ |
|
| 940 |
+Commit. |
|
| 941 |
+ |
|
| 942 |
+ $dbi->commit; |
|
| 943 |
+ |
|
| 944 |
+This is equal to |
|
| 945 |
+ |
|
| 946 |
+ $dbi->dbh->commit; |
|
| 947 |
+ |
|
| 948 |
+=head2 rollback |
|
| 949 |
+ |
|
| 950 |
+Rollback. |
|
| 951 |
+ |
|
| 952 |
+ $dbi->rollback |
|
| 953 |
+ |
|
| 954 |
+This is equal to |
|
| 955 |
+ |
|
| 956 |
+ $dbi->dbh->rollback; |
|
| 957 |
+ |
|
| 958 |
+=head1 AUTHOR |
|
| 959 |
+ |
|
| 960 |
+Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
| 954 | 961 |
|
| 955 | 962 |
=head1 COPYRIGHT & LICENSE |
| 956 | 963 |
|
| ... | ... |
@@ -30,17 +30,7 @@ sub connect {
|
| 30 | 30 |
return $self->SUPER::connect; |
| 31 | 31 |
} |
| 32 | 32 |
|
| 33 |
-sub last_insert_id {
|
|
| 34 |
- my $self = shift; |
|
| 35 |
- |
|
| 36 |
- # Not connected |
|
| 37 |
- croak "Not yet connected" unless $self->connected; |
|
| 38 |
- |
|
| 39 |
- # Get last insert id |
|
| 40 |
- my $last_insert_id = $self->dbh->{mysql_insertid};
|
|
| 41 |
- |
|
| 42 |
- return $last_insert_id; |
|
| 43 |
-} |
|
| 33 |
+sub last_insert_id { shift->dbh->{mysql_insertid} }
|
|
| 44 | 34 |
|
| 45 | 35 |
1; |
| 46 | 36 |
|
| ... | ... |
@@ -20,13 +20,6 @@ DBIx::Custom::Query - query used by DBIx::Custom |
| 20 | 20 |
|
| 21 | 21 |
=head1 ATTRIBUTES |
| 22 | 22 |
|
| 23 |
-=head2 sth |
|
| 24 |
- |
|
| 25 |
-Statement handle. |
|
| 26 |
- |
|
| 27 |
- $query = $query->sth($sth); |
|
| 28 |
- $sth = $query->sth; |
|
| 29 |
- |
|
| 30 | 23 |
=head2 sql |
| 31 | 24 |
|
| 32 | 25 |
SQL statement. |
| ... | ... |
@@ -34,6 +27,13 @@ SQL statement. |
| 34 | 27 |
$query = $query->sql($sql); |
| 35 | 28 |
$sql = $query->sql; |
| 36 | 29 |
|
| 30 |
+=head2 columns |
|
| 31 |
+ |
|
| 32 |
+Column names. |
|
| 33 |
+ |
|
| 34 |
+ $query = $query->columns([@columns]); |
|
| 35 |
+ $columns = $query->columns; |
|
| 36 |
+ |
|
| 37 | 37 |
=head2 default_filter |
| 38 | 38 |
|
| 39 | 39 |
Default filter. |
| ... | ... |
@@ -48,12 +48,12 @@ Filter. |
| 48 | 48 |
$query = $query->filter({%filter});
|
| 49 | 49 |
$filter = $query->filter; |
| 50 | 50 |
|
| 51 |
-=head2 columns |
|
| 51 |
+=head2 sth |
|
| 52 | 52 |
|
| 53 |
-Column names. |
|
| 53 |
+Statement handle. |
|
| 54 | 54 |
|
| 55 |
- $query = $query->columns([@columns]); |
|
| 56 |
- $columns = $query->columns; |
|
| 55 |
+ $query = $query->sth($sth); |
|
| 56 |
+ $sth = $query->sth; |
|
| 57 | 57 |
|
| 58 | 58 |
=head1 METHODS |
| 59 | 59 |
|
| ... | ... |
@@ -35,7 +35,54 @@ sub fetch {
|
| 35 | 35 |
: $_[0]->{filters}->{$fname}->($row[$i]);
|
| 36 | 36 |
} |
| 37 | 37 |
|
| 38 |
- return wantarray ? @row : \@row; |
|
| 38 |
+ return \@row; |
|
| 39 |
+} |
|
| 40 |
+ |
|
| 41 |
+sub fetch_first {
|
|
| 42 |
+ my $self = shift; |
|
| 43 |
+ |
|
| 44 |
+ # Fetch |
|
| 45 |
+ my $row = $self->fetch; |
|
| 46 |
+ |
|
| 47 |
+ # Not exist |
|
| 48 |
+ return unless $row; |
|
| 49 |
+ |
|
| 50 |
+ # Finish statement handle |
|
| 51 |
+ $self->sth->finish; |
|
| 52 |
+ |
|
| 53 |
+ return $row; |
|
| 54 |
+} |
|
| 55 |
+ |
|
| 56 |
+sub fetch_multi {
|
|
| 57 |
+ my ($self, $count) = @_; |
|
| 58 |
+ |
|
| 59 |
+ # Not specified Row count |
|
| 60 |
+ croak("Row count must be specified")
|
|
| 61 |
+ unless $count; |
|
| 62 |
+ |
|
| 63 |
+ # Fetch multi rows |
|
| 64 |
+ my $rows = []; |
|
| 65 |
+ for (my $i = 0; $i < $count; $i++) {
|
|
| 66 |
+ my $row = $self->fetch; |
|
| 67 |
+ |
|
| 68 |
+ last unless $row; |
|
| 69 |
+ |
|
| 70 |
+ push @$rows, $row; |
|
| 71 |
+ } |
|
| 72 |
+ |
|
| 73 |
+ return unless @$rows; |
|
| 74 |
+ return $rows; |
|
| 75 |
+} |
|
| 76 |
+ |
|
| 77 |
+sub fetch_all {
|
|
| 78 |
+ my $self = shift; |
|
| 79 |
+ |
|
| 80 |
+ # Fetch all rows |
|
| 81 |
+ my $rows = []; |
|
| 82 |
+ while(my $row = $self->fetch) {
|
|
| 83 |
+ push @$rows, $row; |
|
| 84 |
+ } |
|
| 85 |
+ return $rows; |
|
| 39 | 86 |
} |
| 40 | 87 |
|
| 41 | 88 |
sub fetch_hash {
|
| ... | ... |
@@ -65,25 +112,10 @@ sub fetch_hash {
|
| 65 | 112 |
$_[0]->{filters}->{$fname}->($row->[$i]);
|
| 66 | 113 |
} |
| 67 | 114 |
|
| 68 |
- return wantarray ? %$row_hash : $row_hash; |
|
| 69 |
-} |
|
| 70 |
- |
|
| 71 |
-sub fetch_single {
|
|
| 72 |
- my $self = shift; |
|
| 73 |
- |
|
| 74 |
- # Fetch |
|
| 75 |
- my $row = $self->fetch; |
|
| 76 |
- |
|
| 77 |
- # Not exist |
|
| 78 |
- return unless $row; |
|
| 79 |
- |
|
| 80 |
- # Finish statement handle |
|
| 81 |
- $self->sth->finish; |
|
| 82 |
- |
|
| 83 |
- return wantarray ? @$row : $row; |
|
| 115 |
+ return $row_hash; |
|
| 84 | 116 |
} |
| 85 | 117 |
|
| 86 |
-sub fetch_hash_single {
|
|
| 118 |
+sub fetch_hash_first {
|
|
| 87 | 119 |
my $self = shift; |
| 88 | 120 |
|
| 89 | 121 |
# Fetch hash |
| ... | ... |
@@ -95,28 +127,7 @@ sub fetch_hash_single {
|
| 95 | 127 |
# Finish statement handle |
| 96 | 128 |
$self->sth->finish; |
| 97 | 129 |
|
| 98 |
- return wantarray ? %$row : $row; |
|
| 99 |
-} |
|
| 100 |
- |
|
| 101 |
-sub fetch_multi {
|
|
| 102 |
- my ($self, $count) = @_; |
|
| 103 |
- |
|
| 104 |
- # Not specified Row count |
|
| 105 |
- croak("Row count must be specified")
|
|
| 106 |
- unless $count; |
|
| 107 |
- |
|
| 108 |
- # Fetch multi rows |
|
| 109 |
- my $rows = []; |
|
| 110 |
- for (my $i = 0; $i < $count; $i++) {
|
|
| 111 |
- my @row = $self->fetch; |
|
| 112 |
- |
|
| 113 |
- last unless @row; |
|
| 114 |
- |
|
| 115 |
- push @$rows, \@row; |
|
| 116 |
- } |
|
| 117 |
- |
|
| 118 |
- return unless @$rows; |
|
| 119 |
- return wantarray ? @$rows : $rows; |
|
| 130 |
+ return $row; |
|
| 120 | 131 |
} |
| 121 | 132 |
|
| 122 | 133 |
sub fetch_hash_multi {
|
| ... | ... |
@@ -129,26 +140,15 @@ sub fetch_hash_multi {
|
| 129 | 140 |
# Fetch multi rows |
| 130 | 141 |
my $rows = []; |
| 131 | 142 |
for (my $i = 0; $i < $count; $i++) {
|
| 132 |
- my %row = $self->fetch_hash; |
|
| 143 |
+ my $row = $self->fetch_hash; |
|
| 133 | 144 |
|
| 134 |
- last unless %row; |
|
| 145 |
+ last unless $row; |
|
| 135 | 146 |
|
| 136 |
- push @$rows, \%row; |
|
| 147 |
+ push @$rows, $row; |
|
| 137 | 148 |
} |
| 138 | 149 |
|
| 139 | 150 |
return unless @$rows; |
| 140 |
- return wantarray ? @$rows : $rows; |
|
| 141 |
-} |
|
| 142 |
- |
|
| 143 |
-sub fetch_all {
|
|
| 144 |
- my $self = shift; |
|
| 145 |
- |
|
| 146 |
- # Fetch all rows |
|
| 147 |
- my $rows = []; |
|
| 148 |
- while(my @row = $self->fetch) {
|
|
| 149 |
- push @$rows, [@row]; |
|
| 150 |
- } |
|
| 151 |
- return wantarray ? @$rows : $rows; |
|
| 151 |
+ return $rows; |
|
| 152 | 152 |
} |
| 153 | 153 |
|
| 154 | 154 |
sub fetch_hash_all {
|
| ... | ... |
@@ -156,10 +156,10 @@ sub fetch_hash_all {
|
| 156 | 156 |
|
| 157 | 157 |
# Fetch all rows as hash |
| 158 | 158 |
my $rows = []; |
| 159 |
- while(my %row = $self->fetch_hash) {
|
|
| 160 |
- push @$rows, {%row};
|
|
| 159 |
+ while(my $row = $self->fetch_hash) {
|
|
| 160 |
+ push @$rows, $row; |
|
| 161 | 161 |
} |
| 162 |
- return wantarray ? @$rows : $rows; |
|
| 162 |
+ return $rows; |
|
| 163 | 163 |
} |
| 164 | 164 |
|
| 165 | 165 |
1; |
| ... | ... |
@@ -169,177 +169,151 @@ sub fetch_hash_all {
|
| 169 | 169 |
DBIx::Custom::Result - DBIx::Custom Resultset |
| 170 | 170 |
|
| 171 | 171 |
=head1 SYNOPSIS |
| 172 |
- |
|
| 173 |
- my $result = $dbi->execute($query); |
|
| 174 | 172 |
|
| 175 |
- # Fetch |
|
| 176 |
- while (my @row = $result->fetch) {
|
|
| 177 |
- # Do something |
|
| 173 |
+ # Result |
|
| 174 |
+ my $result = $dbi->select(table => 'books'); |
|
| 175 |
+ |
|
| 176 |
+ # Fetch a row into array |
|
| 177 |
+ while (my $row = $result->fetch) {
|
|
| 178 |
+ my $value1 = $row->[0]; |
|
| 179 |
+ my $valuu2 = $row->[1]; |
|
| 180 |
+ |
|
| 181 |
+ # do something |
|
| 178 | 182 |
} |
| 179 | 183 |
|
| 180 |
- # Fetch hash |
|
| 181 |
- while (my %row = $result->fetch_hash) {
|
|
| 182 |
- # Do something |
|
| 184 |
+ # Fetch only first row into array |
|
| 185 |
+ my $row = $result->fetch_first; |
|
| 186 |
+ |
|
| 187 |
+ # Fetch multiple rows into array of array |
|
| 188 |
+ while (my $rows = $result->fetch_multi(5)) {
|
|
| 189 |
+ # do something |
|
| 190 |
+ } |
|
| 191 |
+ |
|
| 192 |
+ # Fetch all rows into array of array |
|
| 193 |
+ my $rows = $result->fetch_all; |
|
| 194 |
+ |
|
| 195 |
+ # Fetch hash into hash |
|
| 196 |
+ while (my $row = $result->fetch_hash) {
|
|
| 197 |
+ my $value1 = $row->{title};
|
|
| 198 |
+ my $value2 = $row->{author};
|
|
| 199 |
+ |
|
| 200 |
+ # do something |
|
| 183 | 201 |
} |
| 202 |
+ |
|
| 203 |
+ # Fetch only first row into hash |
|
| 204 |
+ my $row = $result->fetch_hash_first; |
|
| 205 |
+ |
|
| 206 |
+ # Fetch multiple rows into array of hash |
|
| 207 |
+ while (my $rows = $result->fetch_hash_multi) {
|
|
| 208 |
+ # do something |
|
| 209 |
+ } |
|
| 210 |
+ |
|
| 211 |
+ # Fetch all rows into array of hash |
|
| 212 |
+ my $rows = $result->fetch_hash_all; |
|
| 184 | 213 |
|
| 185 | 214 |
=head1 ATTRIBUTES |
| 186 | 215 |
|
| 187 | 216 |
=head2 sth |
| 188 | 217 |
|
| 189 |
-Statement handle |
|
| 218 |
+Statement handle. |
|
| 190 | 219 |
|
| 191 | 220 |
$result = $result->sth($sth); |
| 192 | 221 |
$sth = $reuslt->sth |
| 193 | 222 |
|
| 194 | 223 |
=head2 default_filter |
| 195 | 224 |
|
| 196 |
-Filter excuted when data is fetched |
|
| 225 |
+Default filter. |
|
| 197 | 226 |
|
| 198 |
- $result = $result->default_filter($default_filter); |
|
| 227 |
+ $result = $result->default_filter('decode_utf8');
|
|
| 199 | 228 |
$default_filter = $result->default_filter; |
| 200 | 229 |
|
| 201 | 230 |
=head2 filter |
| 202 | 231 |
|
| 203 |
-Filter excuted when data is fetched |
|
| 232 |
+Filter |
|
| 204 | 233 |
|
| 205 |
- $result = $result->filter($sth); |
|
| 206 |
- $filter = $result->filter; |
|
| 234 |
+ $result = $result->filter({title => 'decode_utf8'});
|
|
| 235 |
+ $filter = $result->filter; |
|
| 207 | 236 |
|
| 208 | 237 |
=head1 METHODS |
| 209 | 238 |
|
| 210 | 239 |
This class is L<Object::Simple> subclass. |
| 211 | 240 |
You can use all methods of L<Object::Simple> |
| 212 | 241 |
|
| 213 |
-=head2 new |
|
| 214 |
- |
|
| 215 |
- my $result = DBIx::Custom::Result->new; |
|
| 216 |
- |
|
| 217 | 242 |
=head2 fetch |
| 218 | 243 |
|
| 219 |
-Fetch a row |
|
| 244 |
+Fetch a row into array |
|
| 220 | 245 |
|
| 221 |
- $row = $result->fetch; # array reference |
|
| 222 |
- @row = $result->fecth; # array |
|
| 246 |
+ $row = $result->fetch; |
|
| 223 | 247 |
|
| 224 |
-The following is fetch sample |
|
| 248 |
+Example: |
|
| 225 | 249 |
|
| 226 | 250 |
while (my $row = $result->fetch) {
|
| 227 | 251 |
# do something |
| 228 |
- my $val1 = $row->[0]; |
|
| 229 |
- my $val2 = $row->[1]; |
|
| 230 |
- } |
|
| 231 |
- |
|
| 232 |
-=head2 fetch_hash |
|
| 233 |
- |
|
| 234 |
-Fetch row as hash |
|
| 235 |
- |
|
| 236 |
- $row = $result->fetch_hash; # hash reference |
|
| 237 |
- %row = $result->fetch_hash; # hash |
|
| 238 |
- |
|
| 239 |
-The following is fetch_hash sample |
|
| 240 |
- |
|
| 241 |
- while (my $row = $result->fetch_hash) {
|
|
| 242 |
- # do something |
|
| 243 |
- my $val1 = $row->{key1};
|
|
| 244 |
- my $val2 = $row->{key2};
|
|
| 252 |
+ my $value1 = $row->[0]; |
|
| 253 |
+ my $value2 = $row->[1]; |
|
| 245 | 254 |
} |
| 246 | 255 |
|
| 247 |
-=head2 fetch_single |
|
| 248 |
- |
|
| 249 |
-Fetch only first row(Scalar context) |
|
| 256 |
+=head2 fetch_first |
|
| 250 | 257 |
|
| 251 |
- $row = $result->fetch_single; # array reference |
|
| 252 |
- @row = $result->fetch_single; # array |
|
| 253 |
- |
|
| 254 |
-The following is fetch_single sample |
|
| 255 |
- |
|
| 256 |
- $row = $result->fetch_single; |
|
| 257 |
- |
|
| 258 |
-This method fetch only first row and finish statement handle |
|
| 259 |
- |
|
| 260 |
-=head2 fetch_hash_single |
|
| 261 |
- |
|
| 262 |
-Fetch only first row as hash |
|
| 263 |
- |
|
| 264 |
- $row = $result->fetch_hash_single; # hash reference |
|
| 265 |
- %row = $result->fetch_hash_single; # hash |
|
| 266 |
- |
|
| 267 |
-The following is fetch_hash_single sample |
|
| 258 |
+Fetch only first row into array and finish statment handle. |
|
| 268 | 259 |
|
| 269 |
- $row = $result->fetch_hash_single; |
|
| 270 |
- |
|
| 271 |
-This method fetch only single row and finish statement handle |
|
| 260 |
+ $row = $result->fetch_first; |
|
| 272 | 261 |
|
| 273 | 262 |
=head2 fetch_multi |
| 274 | 263 |
|
| 275 |
-Fetch rows |
|
| 264 |
+Fetch multiple rows into array of array. |
|
| 276 | 265 |
|
| 277 |
- $rows = $result->fetch_multi($row_count); # array ref of array ref |
|
| 278 |
- @rows = $result->fetch_multi($row_count); # array of array ref |
|
| 266 |
+ $rows = $result->fetch_multi($count); |
|
| 279 | 267 |
|
| 280 |
-The following is fetch_multi sample |
|
| 268 |
+Example: |
|
| 281 | 269 |
|
| 282 | 270 |
while(my $rows = $result->fetch_multi(10)) {
|
| 283 | 271 |
# do someting |
| 284 | 272 |
} |
| 285 | 273 |
|
| 286 |
-=head2 fetch_hash_multi |
|
| 287 |
- |
|
| 288 |
-Fetch rows as hash |
|
| 289 |
- |
|
| 290 |
- $rows = $result->fetch_hash_multi($row_count); # array ref of hash ref |
|
| 291 |
- @rows = $result->fetch_hash_multi($row_count); # array of hash ref |
|
| 292 |
- |
|
| 293 |
-The following is fetch_hash_multi sample |
|
| 294 |
- |
|
| 295 |
- while(my $rows = $result->fetch_hash_multi(10)) {
|
|
| 296 |
- # do someting |
|
| 297 |
- } |
|
| 298 |
- |
|
| 299 | 274 |
=head2 fetch_all |
| 300 | 275 |
|
| 301 |
-Fetch all rows |
|
| 276 |
+Fetch all rows into array of array. |
|
| 302 | 277 |
|
| 303 |
- $rows = $result->fetch_all; # array ref of array ref |
|
| 304 |
- @rows = $result->fecth_all; # array of array ref |
|
| 278 |
+ $rows = $result->fetch_all; |
|
| 305 | 279 |
|
| 306 |
-The following is fetch_all sample |
|
| 280 |
+=head2 fetch_hash |
|
| 307 | 281 |
|
| 308 |
- my $rows = $result->fetch_all; |
|
| 282 |
+Fetch a row into hash |
|
| 309 | 283 |
|
| 310 |
-=head2 fetch_hash_all |
|
| 284 |
+ $row = $result->fetch_hash; |
|
| 311 | 285 |
|
| 312 |
-Fetch all row as array ref of hash ref (Scalar context) |
|
| 286 |
+Example: |
|
| 313 | 287 |
|
| 314 |
- $rows = $result->fetch_hash_all; # array ref of hash ref |
|
| 315 |
- @rows = $result->fecth_all_hash; # array of hash ref |
|
| 288 |
+ while (my $row = $result->fetch_hash) {
|
|
| 289 |
+ my $val1 = $row->{title};
|
|
| 290 |
+ my $val2 = $row->{author};
|
|
| 291 |
+ |
|
| 292 |
+ # do something |
|
| 293 |
+ } |
|
| 316 | 294 |
|
| 317 |
-The following is fetch_hash_all sample |
|
| 295 |
+=head2 fetch_hash_first |
|
| 296 |
+ |
|
| 297 |
+Fetch only first row into hash and finish statment handle. |
|
| 318 | 298 |
|
| 319 |
- my $rows = $result->fetch_hash_all; |
|
| 299 |
+ $row = $result->fetch_hash_first; |
|
| 320 | 300 |
|
| 321 |
-=head2 error |
|
| 301 |
+=head2 fetch_hash_multi |
|
| 322 | 302 |
|
| 323 |
-Get error infomation |
|
| 303 |
+Fetch multiple rows into array of hash |
|
| 324 | 304 |
|
| 325 |
- $error_messege = $result->error; |
|
| 326 |
- ($error_message, $error_number, $error_state) = $result->error; |
|
| 305 |
+ $rows = $result->fetch_hash_multi($count); |
|
| 327 | 306 |
|
| 307 |
+Example: |
|
| 328 | 308 |
|
| 329 |
-You can get get information. This is same as the following. |
|
| 330 |
- |
|
| 331 |
- $error_message : $result->sth->errstr |
|
| 332 |
- $error_number : $result->sth->err |
|
| 333 |
- $error_state : $result->sth->state |
|
| 334 |
- |
|
| 335 |
-=head2 finish |
|
| 336 |
- |
|
| 337 |
-Finish statement handle |
|
| 309 |
+ while(my $rows = $result->fetch_hash_multi(10)) {
|
|
| 310 |
+ # do someting |
|
| 311 |
+ } |
|
| 338 | 312 |
|
| 339 |
- $result->finish |
|
| 313 |
+=head2 fetch_hash_all |
|
| 340 | 314 |
|
| 341 |
-This is equel to |
|
| 315 |
+Fetch all rows into array of hash. |
|
| 342 | 316 |
|
| 343 |
- $result->sth->finish; |
|
| 317 |
+ $rows = $result->fetch_hash_all; |
|
| 344 | 318 |
|
| 345 | 319 |
=cut |
| ... | ... |
@@ -59,18 +59,6 @@ sub register_tag_processor {
|
| 59 | 59 |
return $self; |
| 60 | 60 |
} |
| 61 | 61 |
|
| 62 |
-sub clone {
|
|
| 63 |
- my $self = shift; |
|
| 64 |
- my $new = $self->new; |
|
| 65 |
- |
|
| 66 |
- $new->tag_start($self->tag_start); |
|
| 67 |
- $new->tag_end($self->tag_end); |
|
| 68 |
- $new->tag_syntax($self->tag_syntax); |
|
| 69 |
- $new->tag_processors({%{$self->tag_processors || {}}});
|
|
| 70 |
- |
|
| 71 |
- return $new; |
|
| 72 |
-} |
|
| 73 |
- |
|
| 74 | 62 |
sub create_query {
|
| 75 | 63 |
my ($self, $template) = @_; |
| 76 | 64 |
|
| ... | ... |
@@ -29,38 +29,13 @@ sub connect_memory {
|
| 29 | 29 |
# Data source for memory database |
| 30 | 30 |
$self->data_source('dbi:SQLite:dbname=:memory:');
|
| 31 | 31 |
|
| 32 |
- # Already connected |
|
| 33 |
- croak("Already connected") if $self->connected;
|
|
| 34 |
- |
|
| 35 | 32 |
# Connect |
| 36 | 33 |
$self->connect; |
| 37 | 34 |
|
| 38 | 35 |
return $self; |
| 39 | 36 |
} |
| 40 | 37 |
|
| 41 |
-sub reconnect_memory {
|
|
| 42 |
- my $self = shift; |
|
| 43 |
- |
|
| 44 |
- # Data source for memory database |
|
| 45 |
- $self->data_source('dbi:SQLite:dbname=:memory:');
|
|
| 46 |
- |
|
| 47 |
- # Reconnect |
|
| 48 |
- $self->reconnect; |
|
| 49 |
- |
|
| 50 |
- return $self; |
|
| 51 |
-} |
|
| 52 |
- |
|
| 53 |
-sub last_insert_rowid {
|
|
| 54 |
- my $self = shift; |
|
| 55 |
- |
|
| 56 |
- # Not connected |
|
| 57 |
- croak "Not yet connected" unless $self->connected; |
|
| 58 |
- |
|
| 59 |
- # Get last insert row id |
|
| 60 |
- my $last_insert_rowid = $self->dbh->func('last_insert_rowid');
|
|
| 61 |
- |
|
| 62 |
- return $last_insert_rowid; |
|
| 63 |
-} |
|
| 38 |
+sub last_insert_rowid { shift->dbh->func('last_insert_rowid') }
|
|
| 64 | 39 |
|
| 65 | 40 |
1; |
| 66 | 41 |
|
| ... | ... |
@@ -80,9 +55,6 @@ DBIx::Custom::SQLite - a SQLite implementation of DBIx::Custom |
| 80 | 55 |
# Connect memory database |
| 81 | 56 |
my $dbi = DBIx::Custom::SQLite->connect_memory; |
| 82 | 57 |
|
| 83 |
- # Reconnect memory database |
|
| 84 |
- $dbi->reconnect_memory; |
|
| 85 |
- |
|
| 86 | 58 |
# Last insert row ID |
| 87 | 59 |
my $id = $dbi->last_insert_rowid; |
| 88 | 60 |
|
| ... | ... |
@@ -117,12 +89,6 @@ Connect memory database. |
| 117 | 89 |
|
| 118 | 90 |
$dbi->connect_memory; |
| 119 | 91 |
|
| 120 |
-=head2 reconnect_memory |
|
| 121 |
- |
|
| 122 |
-Reconnect to memory databsse. |
|
| 123 |
- |
|
| 124 |
- $dbi->reconnect_memory; |
|
| 125 |
- |
|
| 126 | 92 |
=head2 last_insert_rowid |
| 127 | 93 |
|
| 128 | 94 |
Last insert row ID. |
| ... | ... |
@@ -68,10 +68,6 @@ $dbi->disconnect; |
| 68 | 68 |
ok(!$dbi->dbh, $test); |
| 69 | 69 |
|
| 70 | 70 |
|
| 71 |
-test 'connected'; |
|
| 72 |
-$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
|
|
| 73 |
-ok($dbi->connected, "$test : connected"); |
|
| 74 |
- |
|
| 75 | 71 |
# Prepare table |
| 76 | 72 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
|
| 77 | 73 |
$dbi->execute($CREATE_TABLE->{0});
|
| ... | ... |
@@ -87,45 +83,22 @@ $result = $dbi->execute($query); |
| 87 | 83 |
while (my $row = $result->fetch) {
|
| 88 | 84 |
push @rows, [@$row]; |
| 89 | 85 |
} |
| 90 |
-is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context"); |
|
| 91 |
- |
|
| 92 |
-$result = $dbi->execute($query); |
|
| 93 |
-@rows = (); |
|
| 94 |
-while (my @row = $result->fetch) {
|
|
| 95 |
- push @rows, [@row]; |
|
| 96 |
-} |
|
| 97 |
-is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context"); |
|
| 86 |
+is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch"); |
|
| 98 | 87 |
|
| 99 | 88 |
$result = $dbi->execute($query); |
| 100 | 89 |
@rows = (); |
| 101 | 90 |
while (my $row = $result->fetch_hash) {
|
| 102 | 91 |
push @rows, {%$row};
|
| 103 | 92 |
} |
| 104 |
-is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context");
|
|
| 105 |
- |
|
| 106 |
-$result = $dbi->execute($query); |
|
| 107 |
-@rows = (); |
|
| 108 |
-while (my %row = $result->fetch_hash) {
|
|
| 109 |
- push @rows, {%row};
|
|
| 110 |
-} |
|
| 111 |
-is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch hash list context");
|
|
| 93 |
+is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash");
|
|
| 112 | 94 |
|
| 113 | 95 |
$result = $dbi->execute($query); |
| 114 | 96 |
$rows = $result->fetch_all; |
| 115 |
-is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context"); |
|
| 116 |
- |
|
| 117 |
-$result = $dbi->execute($query); |
|
| 118 |
-@rows = $result->fetch_all; |
|
| 119 |
-is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all list context"); |
|
| 97 |
+is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all"); |
|
| 120 | 98 |
|
| 121 | 99 |
$result = $dbi->execute($query); |
| 122 |
-@rows = $result->fetch_hash_all; |
|
| 123 |
-is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_hash_all scalar context"); |
|
| 124 |
- |
|
| 125 |
-$result = $dbi->execute($query); |
|
| 126 |
-@rows = $result->fetch_all; |
|
| 127 |
-is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_hash_all list context"); |
|
| 128 |
- |
|
| 100 |
+$rows = $result->fetch_hash_all; |
|
| 101 |
+is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash_all");
|
|
| 129 | 102 |
|
| 130 | 103 |
test 'Insert query return value'; |
| 131 | 104 |
$dbi->execute($DROP_TABLE->{0});
|
| ... | ... |
@@ -411,7 +384,7 @@ $dbi->execute($CREATE_TABLE->{0});
|
| 411 | 384 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
| 412 | 385 |
$result = $dbi->select(table => 'table1'); |
| 413 | 386 |
$result->filter({key1 => 'three_times'});
|
| 414 |
-$row = $result->fetch_hash_single; |
|
| 387 |
+$row = $result->fetch_hash_first; |
|
| 415 | 388 |
is_deeply($row, {key1 => 3, key2 => 4}, "$test: default_fetch_filter and filter");
|
| 416 | 389 |
|
| 417 | 390 |
test 'filters'; |
| ... | ... |
@@ -441,4 +414,4 @@ $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
| 441 | 414 |
$dbi->rollback; |
| 442 | 415 |
|
| 443 | 416 |
$result = $dbi->select(table => 'table1'); |
| 444 |
-ok(! $result->fetch_single, "$test: rollback"); |
|
| 417 |
+ok(! $result->fetch_first, "$test: rollback"); |
| ... | ... |
@@ -53,14 +53,6 @@ while (my $row = $result->fetch) {
|
| 53 | 53 |
is_deeply(\@rows, [[1, 2], [3, 4]], $test); |
| 54 | 54 |
|
| 55 | 55 |
|
| 56 |
-test 'fetch list context'; |
|
| 57 |
-$result = query($dbh, $sql); |
|
| 58 |
-@rows = (); |
|
| 59 |
-while (my @row = $result->fetch) {
|
|
| 60 |
- push @rows, [@row]; |
|
| 61 |
-} |
|
| 62 |
-is_deeply(\@rows, [[1, 2], [3, 4]], $test); |
|
| 63 |
- |
|
| 64 | 56 |
test 'fetch_hash scalar context'; |
| 65 | 57 |
$result = query($dbh, $sql); |
| 66 | 58 |
@rows = (); |
| ... | ... |
@@ -70,47 +62,22 @@ while (my $row = $result->fetch_hash) {
|
| 70 | 62 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], $test);
|
| 71 | 63 |
|
| 72 | 64 |
|
| 73 |
-test 'fetch hash list context'; |
|
| 65 |
+test 'fetch_first'; |
|
| 74 | 66 |
$result = query($dbh, $sql); |
| 75 |
-@rows = (); |
|
| 76 |
-while (my %row = $result->fetch_hash) {
|
|
| 77 |
- push @rows, {%row};
|
|
| 78 |
-} |
|
| 79 |
-is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], $test);
|
|
| 80 |
- |
|
| 81 |
- |
|
| 82 |
-test 'fetch_single'; |
|
| 83 |
-$result = query($dbh, $sql); |
|
| 84 |
-$row = $result->fetch_single; |
|
| 67 |
+$row = $result->fetch_first; |
|
| 85 | 68 |
is_deeply($row, [1, 2], "$test : row"); |
| 86 | 69 |
$row = $result->fetch; |
| 87 | 70 |
ok(!$row, "$test : finished"); |
| 88 | 71 |
|
| 89 | 72 |
|
| 90 |
-test 'fetch_single list context'; |
|
| 73 |
+test 'fetch_hash_first'; |
|
| 91 | 74 |
$result = query($dbh, $sql); |
| 92 |
-@row = $result->fetch_single; |
|
| 93 |
-is_deeply([@row], [1, 2], "$test : row"); |
|
| 94 |
-@row = $result->fetch; |
|
| 95 |
-ok(!@row, "$test : finished"); |
|
| 96 |
- |
|
| 97 |
- |
|
| 98 |
-test 'fetch_hash_single'; |
|
| 99 |
-$result = query($dbh, $sql); |
|
| 100 |
-$row = $result->fetch_hash_single; |
|
| 75 |
+$row = $result->fetch_hash_first; |
|
| 101 | 76 |
is_deeply($row, {key1 => 1, key2 => 2}, "$test : row");
|
| 102 | 77 |
$row = $result->fetch_hash; |
| 103 | 78 |
ok(!$row, "$test : finished"); |
| 104 | 79 |
|
| 105 | 80 |
|
| 106 |
-test 'fetch_hash_single list context'; |
|
| 107 |
-$result = query($dbh, $sql); |
|
| 108 |
-@row = $result->fetch_hash_single; |
|
| 109 |
-is_deeply({@row}, {key1 => 1, key2 => 2}, "$test : row");
|
|
| 110 |
-@row = $result->fetch_hash; |
|
| 111 |
-ok(!@row, "$test : finished"); |
|
| 112 |
- |
|
| 113 |
- |
|
| 114 | 81 |
test 'fetch_multi'; |
| 115 | 82 |
$dbh->do("insert into table1 (key1, key2) values ('5', '6');");
|
| 116 | 83 |
$dbh->do("insert into table1 (key1, key2) values ('7', '8');");
|
| ... | ... |
@@ -128,20 +95,6 @@ $rows = $result->fetch_multi(2); |
| 128 | 95 |
ok(!$rows); |
| 129 | 96 |
|
| 130 | 97 |
|
| 131 |
-test 'fetch_multi list context'; |
|
| 132 |
-$result = query($dbh, $sql); |
|
| 133 |
-@rows = $result->fetch_multi(2); |
|
| 134 |
-is_deeply([@rows], [[1, 2], |
|
| 135 |
- [3, 4]], "$test : fetch_multi first"); |
|
| 136 |
-@rows = $result->fetch_multi(2); |
|
| 137 |
-is_deeply([@rows], [[5, 6], |
|
| 138 |
- [7, 8]], "$test : fetch_multi secound"); |
|
| 139 |
-@rows = $result->fetch_multi(2); |
|
| 140 |
-is_deeply([@rows], [[9, 10]], "$test : fetch_multi third"); |
|
| 141 |
-@rows = $result->fetch_multi(2); |
|
| 142 |
-ok(!@rows); |
|
| 143 |
- |
|
| 144 |
- |
|
| 145 | 98 |
test 'fetch_multi error'; |
| 146 | 99 |
$result = query($dbh, $sql); |
| 147 | 100 |
eval {$result->fetch_multi};
|
| ... | ... |
@@ -162,48 +115,24 @@ $rows = $result->fetch_hash_multi(2); |
| 162 | 115 |
ok(!$rows); |
| 163 | 116 |
|
| 164 | 117 |
|
| 165 |
-test 'fetch_multi list context'; |
|
| 166 |
-$result = query($dbh, $sql); |
|
| 167 |
-@rows = $result->fetch_hash_multi(2); |
|
| 168 |
-is_deeply([@rows], [{key1 => 1, key2 => 2},
|
|
| 169 |
- {key1 => 3, key2 => 4}], "$test : fetch_multi first");
|
|
| 170 |
-@rows = $result->fetch_hash_multi(2); |
|
| 171 |
-is_deeply([@rows], [{key1 => 5, key2 => 6},
|
|
| 172 |
- {key1 => 7, key2 => 8}], "$test : fetch_multi secound");
|
|
| 173 |
-@rows = $result->fetch_hash_multi(2); |
|
| 174 |
-is_deeply([@rows], [{key1 => 9, key2 => 10}], "$test : fetch_multi third");
|
|
| 175 |
-@rows = $result->fetch_hash_multi(2); |
|
| 176 |
-ok(!@rows); |
|
| 177 |
-$dbh->do("delete from table1 where key1 = 5 or key1 = 7 or key1 = 9");
|
|
| 178 |
- |
|
| 179 |
- |
|
| 180 | 118 |
test 'fetch_multi error'; |
| 181 | 119 |
$result = query($dbh, $sql); |
| 182 | 120 |
eval {$result->fetch_hash_multi};
|
| 183 | 121 |
like($@, qr/Row count must be specified/, "$test : Not specified row count"); |
| 184 | 122 |
|
| 123 |
+$dbh->do('delete from table1');
|
|
| 124 |
+$dbh->do("insert into table1 (key1, key2) values ('1', '2');");
|
|
| 125 |
+$dbh->do("insert into table1 (key1, key2) values ('3', '4');");
|
|
| 185 | 126 |
|
| 186 | 127 |
test 'fetch_all'; |
| 187 | 128 |
$result = query($dbh, $sql); |
| 188 | 129 |
$rows = $result->fetch_all; |
| 189 | 130 |
is_deeply($rows, [[1, 2], [3, 4]], $test); |
| 190 | 131 |
|
| 191 |
-test 'fetch_all list context'; |
|
| 192 |
-$result = query($dbh, $sql); |
|
| 193 |
-@rows = $result->fetch_all; |
|
| 194 |
-is_deeply(\@rows, [[1, 2], [3, 4]], $test); |
|
| 195 |
- |
|
| 196 |
- |
|
| 197 | 132 |
test 'fetch_hash_all'; |
| 198 | 133 |
$result = query($dbh, $sql); |
| 199 |
-@rows = $result->fetch_hash_all; |
|
| 200 |
-is_deeply($rows, [[1, 2], [3, 4]], $test); |
|
| 201 |
- |
|
| 202 |
- |
|
| 203 |
-test 'fetch_hash_all list context'; |
|
| 204 |
-$result = query($dbh, $sql); |
|
| 205 |
-@rows = $result->fetch_all; |
|
| 206 |
-is_deeply(\@rows, [[1, 2], [3, 4]], $test); |
|
| 134 |
+$rows = $result->fetch_hash_all; |
|
| 135 |
+is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], $test);
|
|
| 207 | 136 |
|
| 208 | 137 |
|
| 209 | 138 |
test 'fetch filter'; |
| ... | ... |
@@ -16,7 +16,6 @@ my $datas; |
| 16 | 16 |
my $sql_tmpl; |
| 17 | 17 |
my $query; |
| 18 | 18 |
my $ret_val; |
| 19 |
-my $clone; |
|
| 20 | 19 |
|
| 21 | 20 |
test "Various template pattern"; |
| 22 | 21 |
$datas = [ |
| ... | ... |
@@ -153,25 +152,3 @@ $sql_tmpl |
| 153 | 152 |
->tag_syntax('syntax')
|
| 154 | 153 |
->tag_processors({a => 1, b => 2});
|
| 155 | 154 |
|
| 156 |
-$clone = $sql_tmpl->clone; |
|
| 157 |
-is($clone->tag_start, $sql_tmpl->tag_start, "$test : tag_start"); |
|
| 158 |
-is($clone->tag_end, $sql_tmpl->tag_end, "$test : tag_end"); |
|
| 159 |
-is($clone->tag_syntax, $sql_tmpl->tag_syntax, "$test : tag_syntax"); |
|
| 160 |
- |
|
| 161 |
-is_deeply( scalar $clone->tag_processors, scalar $sql_tmpl->tag_processors, |
|
| 162 |
- "$test : tag_processors deep clone"); |
|
| 163 |
- |
|
| 164 |
-isnt($clone->tag_processors, $sql_tmpl->tag_processors, |
|
| 165 |
- "$test : tag_processors reference not copy"); |
|
| 166 |
- |
|
| 167 |
-$sql_tmpl->tag_processors(undef); |
|
| 168 |
- |
|
| 169 |
-$clone = $sql_tmpl->clone; |
|
| 170 |
-is_deeply(scalar $clone->tag_processors, {}, "$test tag_processor undef copy");
|
|
| 171 |
- |
|
| 172 |
- |
|
| 173 |
- |
|
| 174 |
-__END__ |
|
| 175 |
- |
|
| 176 |
- |
|
| 177 |
- |
| ... | ... |
@@ -42,19 +42,6 @@ $dbi->insert(table => 'table1', param => {key1 => 'a', key2 => 2});
|
| 42 | 42 |
$rows = $dbi->select(table => 'table1', where => {key1 => 'a'})->fetch_hash_all;
|
| 43 | 43 |
is_deeply($rows, [{key1 => 'a', key2 => 2}], "$test : select rows");
|
| 44 | 44 |
|
| 45 |
-test 'connect_memory error'; |
|
| 46 |
-eval{$dbi->connect_memory};
|
|
| 47 |
-like($@, qr/Already connected/, "$test : already connected"); |
|
| 48 |
- |
|
| 49 |
-test 'reconnect_memory'; |
|
| 50 |
-$dbi = DBIx::Custom::SQLite->new; |
|
| 51 |
-$dbi->reconnect_memory; |
|
| 52 |
-$ret_val = $dbi->execute($CREATE_TABLE->{0});
|
|
| 53 |
-ok(defined $ret_val, "$test : connect first"); |
|
| 54 |
-$dbi->reconnect_memory; |
|
| 55 |
-$ret_val = $dbi->execute($CREATE_TABLE->{2});
|
|
| 56 |
-ok(defined $ret_val, "$test : connect first"); |
|
| 57 |
- |
|
| 58 | 45 |
test 'connect'; |
| 59 | 46 |
$db_file = 't/test.db'; |
| 60 | 47 |
unlink $db_file if -f $db_file; |