... | ... |
@@ -3,11 +3,10 @@ use 5.008001; |
3 | 3 |
package DBIx::Custom; |
4 | 4 |
use Object::Simple; |
5 | 5 |
|
6 |
-our $VERSION = '0.0601'; |
|
6 |
+our $VERSION = '0.0602'; |
|
7 | 7 |
|
8 | 8 |
use Carp 'croak'; |
9 | 9 |
use DBI; |
10 |
-use DBIx::Custom::Query; |
|
11 | 10 |
use DBIx::Custom::Result; |
12 | 11 |
use DBIx::Custom::SQL::Template; |
13 | 12 |
|
... | ... |
@@ -206,8 +205,6 @@ sub create_query { |
206 | 205 |
$query = eval{$sql_template->create_query($template)}; |
207 | 206 |
croak($@) if $@; |
208 | 207 |
|
209 |
- $query = DBIx::Custom::Query->new($query); |
|
210 |
- |
|
211 | 208 |
$class->_add_query_cache($template, $query); |
212 | 209 |
} |
213 | 210 |
|
... | ... |
@@ -256,7 +256,7 @@ Fetch a row |
256 | 256 |
Fetch row as hash |
257 | 257 |
|
258 | 258 |
$row = $self->fetch_hash; # hash reference |
259 |
- %row = $self->fecth_hash; # hash |
|
259 |
+ %row = $self->fetch_hash; # hash |
|
260 | 260 |
|
261 | 261 |
# Sample |
262 | 262 |
while (my $row = $result->fetch_hash) { |
... | ... |
@@ -282,7 +282,7 @@ This method fetch only first row and finish statement handle |
282 | 282 |
Fetch only first row as hash |
283 | 283 |
|
284 | 284 |
$row = $self->fetch_hash_first; # hash reference |
285 |
- @row = $self->fetch_hash_first; # hash |
|
285 |
+ %row = $self->fetch_hash_first; # hash |
|
286 | 286 |
|
287 | 287 |
# Sample |
288 | 288 |
$row = $result->fetch_hash_first; |
... | ... |
@@ -5,7 +5,9 @@ use strict; |
5 | 5 |
use warnings; |
6 | 6 |
use Carp 'croak'; |
7 | 7 |
|
8 |
-# Accessor is created by Object::Simple. Please read Object::Simple document |
|
8 |
+use DBIx::Custom::Query; |
|
9 |
+ |
|
10 |
+# Accessor is created by Object::Simple. |
|
9 | 11 |
|
10 | 12 |
### Class-Object accessors |
11 | 13 |
|
... | ... |
@@ -46,17 +48,17 @@ sub tag_processors : ClassObjectAttr { |
46 | 48 |
initialize => { |
47 | 49 |
clone => 'hash', |
48 | 50 |
default => sub {{ |
49 |
- '?' => \&DBIx::Custom::SQL::Template::TagProcessor::expand_basic_tag, |
|
50 |
- '=' => \&DBIx::Custom::SQL::Template::TagProcessor::expand_basic_tag, |
|
51 |
- '<>' => \&DBIx::Custom::SQL::Template::TagProcessor::expand_basic_tag, |
|
52 |
- '>' => \&DBIx::Custom::SQL::Template::TagProcessor::expand_basic_tag, |
|
53 |
- '<' => \&DBIx::Custom::SQL::Template::TagProcessor::expand_basic_tag, |
|
54 |
- '>=' => \&DBIx::Custom::SQL::Template::TagProcessor::expand_basic_tag, |
|
55 |
- '<=' => \&DBIx::Custom::SQL::Template::TagProcessor::expand_basic_tag, |
|
56 |
- 'like' => \&DBIx::Custom::SQL::Template::TagProcessor::expand_basic_tag, |
|
57 |
- 'in' => \&DBIx::Custom::SQL::Template::TagProcessor::expand_in_tag, |
|
58 |
- 'insert' => \&DBIx::Custom::SQL::Template::TagProcessor::expand_insert_tag, |
|
59 |
- 'update' => \&DBIx::Custom::SQL::Template::TagProcessor::expand_update_tag |
|
51 |
+ '?' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
52 |
+ '=' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
53 |
+ '<>' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
54 |
+ '>' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
55 |
+ '<' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
56 |
+ '>=' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
57 |
+ '<=' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
58 |
+ 'like' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
59 |
+ 'in' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_in_tag, |
|
60 |
+ 'insert' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_insert_tag, |
|
61 |
+ 'update' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_update_tag |
|
60 | 62 |
}} |
61 | 63 |
} |
62 | 64 |
} |
... | ... |
@@ -214,7 +216,7 @@ sub _build_query { |
214 | 216 |
$sql .= ';' unless $sql =~ /;$/; |
215 | 217 |
|
216 | 218 |
# Query |
217 |
- my $query = {sql => $sql, key_infos => $all_key_infos}; |
|
219 |
+ my $query = DBIx::Custom::Query->new(sql => $sql, key_infos => $all_key_infos); |
|
218 | 220 |
|
219 | 221 |
return $query; |
220 | 222 |
} |
... | ... |
@@ -235,7 +237,8 @@ sub _placeholder_count { |
235 | 237 |
Object::Simple->build_class; |
236 | 238 |
|
237 | 239 |
|
238 |
-package DBIx::Custom::SQL::Template::TagProcessor; |
|
240 |
+package DBIx::Custom::SQL::Template::TagProcessors; |
|
241 |
+ |
|
239 | 242 |
use strict; |
240 | 243 |
use warnings; |
241 | 244 |
use Carp 'croak'; |
... | ... |
@@ -475,7 +478,7 @@ sub expand_update_tag { |
475 | 478 |
|
476 | 479 |
DBIx::Custom::SQL::Template - DBIx::Custom SQL Template |
477 | 480 |
|
478 |
-=head1 SYNOPSIS |
|
481 |
+=head1 Synopsis |
|
479 | 482 |
|
480 | 483 |
my $sql_tmpl = DBIx::Custom::SQL::Template->new; |
481 | 484 |
|
... | ... |
@@ -497,32 +500,34 @@ DBIx::Custom::SQL::Template - DBIx::Custom SQL Template |
497 | 500 |
$query = $dbi->create_query($tmpl); # This is SQL::Template create_query |
498 | 501 |
$dbi->query($query, $param); |
499 | 502 |
|
500 |
-=head1 CLASS-OBJECT ACCESSORS |
|
503 |
+=head1 Accessors |
|
501 | 504 |
|
502 |
-Class-Object accessor is used from both object and class |
|
505 |
+=head2 tag_processors |
|
503 | 506 |
|
504 |
- $class->$accessor # call from class |
|
505 |
- $self->$accessor # call form object |
|
507 |
+Set and get tag processors |
|
506 | 508 |
|
507 |
-=head2 tag_processors |
|
509 |
+ # For object |
|
510 |
+ $self = $self->tag_processors($tag_processors); |
|
511 |
+ $tag_processors = $self->tag_processors; |
|
508 | 512 |
|
509 |
- # Set and get |
|
510 |
- $self = $sql_tmpl->tag_processors($tag_processors); |
|
511 |
- $tag_processors = $sql_tmpl->tag_processors; |
|
512 |
- |
|
513 |
- # Sample |
|
514 |
- $sql_tmpl->tag_processors( |
|
515 |
- '?' => \&expand_question, |
|
516 |
- '=' => \&expand_equel |
|
517 |
- ); |
|
513 |
+ # For class |
|
514 |
+ $class = $class->tag_processors($tag_processors); |
|
515 |
+ $tag_processors = $class->tag_processors; |
|
518 | 516 |
|
519 |
-You can use add_tag_processor to add tag processor |
|
517 |
+ # Sample |
|
518 |
+ $placeholder_tag_processor = $sql_tmpl->tag_processor->{'?'}; |
|
520 | 519 |
|
521 | 520 |
=head2 tag_start |
522 | 521 |
|
523 |
- # Set and get |
|
524 |
- $self = $sql_tmpl->tag_start($tag_start); |
|
525 |
- $tag_start = $sql_tmpl->tag_start; |
|
522 |
+Set and get start tag |
|
523 |
+ |
|
524 |
+ # For object |
|
525 |
+ $self = $self->tag_start($tag_start); |
|
526 |
+ $tag_start = $self->tag_start; |
|
527 |
+ |
|
528 |
+ # For class |
|
529 |
+ $class = $class->tag_start($tag_start); |
|
530 |
+ $tag_start = $class->tag_start; |
|
526 | 531 |
|
527 | 532 |
# Sample |
528 | 533 |
$sql_tmpl->tag_start('{'); |
... | ... |
@@ -531,9 +536,15 @@ Default is '{' |
531 | 536 |
|
532 | 537 |
=head2 tag_end |
533 | 538 |
|
534 |
- # Set and get |
|
535 |
- $self = $sql_tmpl->tag_start($tag_end); |
|
536 |
- $tag_end = $sql_tmpl->tag_start; |
|
539 |
+Set and get end tag |
|
540 |
+ |
|
541 |
+ # For object |
|
542 |
+ $self = $self->tag_start($tag_end); |
|
543 |
+ $tag_end = $self->tag_start; |
|
544 |
+ |
|
545 |
+ # For class |
|
546 |
+ $self = $self->tag_start($tag_end); |
|
547 |
+ $tag_end = $self->tag_start; |
|
537 | 548 |
|
538 | 549 |
# Sample |
539 | 550 |
$sql_tmpl->tag_start('}'); |
... | ... |
@@ -542,56 +553,61 @@ Default is '}' |
542 | 553 |
|
543 | 554 |
=head2 tag_syntax |
544 | 555 |
|
545 |
- # Set and get |
|
546 |
- $self = $sql_tmpl->tag_syntax($tag_syntax); |
|
547 |
- $tag_syntax = $sql_tmpl->tag_syntax; |
|
556 |
+Set and get tag syntax |
|
557 |
+ |
|
558 |
+ # For object |
|
559 |
+ $self = $self->tag_syntax($tag_syntax); |
|
560 |
+ $tag_syntax = $self->tag_syntax; |
|
561 |
+ |
|
562 |
+ # For class |
|
563 |
+ $class = $class->tag_syntax($tag_syntax); |
|
564 |
+ $tag_syntax = $class->tag_syntax; |
|
548 | 565 |
|
549 | 566 |
# Sample |
550 |
- $sql_tmpl->tag_syntax( |
|
551 |
- "[Tag] [Expand]\n" . |
|
552 |
- "{? name} ?\n" . |
|
553 |
- "{= name} name = ?\n" . |
|
554 |
- "{<> name} name <> ?\n" |
|
555 |
- ); |
|
567 |
+ $syntax = $sql_tmpl->tag_syntax; |
|
556 | 568 |
|
557 |
-=head1 METHODS |
|
569 |
+=head1 Methods |
|
558 | 570 |
|
559 | 571 |
=head2 create_query |
560 | 572 |
|
561 |
- # Create SQL form SQL template |
|
562 |
- $query = $sql_tmpl->create_query($tmpl); |
|
573 |
+Create L<DBIx::Custom::Query> object parsing SQL template |
|
574 |
+ |
|
575 |
+ $query = $self->create_query($tmpl); |
|
563 | 576 |
|
564 | 577 |
# Sample |
565 | 578 |
$query = $sql_tmpl->create_sql( |
566 | 579 |
"select * from table where {= title} && {like author} || {<= price}") |
567 | 580 |
|
568 |
- # Result |
|
569 |
- $qeury->{sql} : "select * from table where title = ? && author like ? price <= ?;" |
|
570 |
- $query->{key_infos} : [['title'], ['author'], ['price']] |
|
581 |
+ # Expanded |
|
582 |
+ $qeury->sql : "select * from table where title = ? && author like ? price <= ?;" |
|
583 |
+ $query->key_infos : [['title'], ['author'], ['price']] |
|
571 | 584 |
|
572 |
- # Sample2 (with table name) |
|
585 |
+ # Sample with table name |
|
573 | 586 |
($sql, @bind_values) = $sql_tmpl->create_sql( |
574 | 587 |
"select * from table where {= table.title} && {like table.author}", |
575 | 588 |
{table => {title => 'Perl', author => '%Taro%'}} |
576 | 589 |
) |
577 | 590 |
|
578 |
- # Result2 |
|
579 |
- $query->{sql} : "select * from table where table.title = ? && table.title like ?;" |
|
580 |
- $query->{key_infos} :[ [['table.title'],['table', 'title']], |
|
581 |
- [['table.author'],['table', 'author']] ] |
|
591 |
+ # Expanded |
|
592 |
+ $query->sql : "select * from table where table.title = ? && table.title like ?;" |
|
593 |
+ $query->key_infos :[ [['table.title'],['table', 'title']], |
|
594 |
+ [['table.author'],['table', 'author']] ] |
|
582 | 595 |
|
583 |
-This method create query using by DBIx::Custom. |
|
584 |
-query is two infomation |
|
596 |
+This method create query using by L<DBIx::Custom>. |
|
597 |
+query has two infomation |
|
585 | 598 |
|
586 |
- 1.sql : SQL |
|
587 |
- 2.key_infos : Parameter access key information |
|
599 |
+ 1. sql : SQL |
|
600 |
+ 2. key_infos : Parameter access key information |
|
588 | 601 |
|
589 | 602 |
=head2 add_tag_processor |
590 | 603 |
|
591 | 604 |
Add tag processor |
592 |
- |
|
593 |
- # Add |
|
594 |
- $self = $sql_tmpl->add_tag_processor($tag_processor); |
|
605 |
+ |
|
606 |
+ # For object |
|
607 |
+ $self = $self->add_tag_processor($tag_processor); |
|
608 |
+ |
|
609 |
+ # For class |
|
610 |
+ $class = $class->add_tag_processor($tag_processor); |
|
595 | 611 |
|
596 | 612 |
# Sample |
597 | 613 |
$sql_tmpl->add_tag_processor( |
... | ... |
@@ -628,12 +644,14 @@ If you want to know more, Please see DBIx::Custom::SQL::Template source code. |
628 | 644 |
|
629 | 645 |
=head2 clone |
630 | 646 |
|
631 |
- # Clone DBIx::Custom::SQL::Template object |
|
647 |
+Clone DBIx::Custom::SQL::Template object |
|
648 |
+ |
|
632 | 649 |
$clone = $self->clone; |
633 | 650 |
|
634 | 651 |
=head1 Available Tags |
635 | 652 |
|
636 |
- # Available Tags |
|
653 |
+Available Tags |
|
654 |
+ |
|
637 | 655 |
[tag] [expand] |
638 | 656 |
{? name} ? |
639 | 657 |
{= name} name = ? |
... | ... |
@@ -647,24 +665,24 @@ If you want to know more, Please see DBIx::Custom::SQL::Template source code. |
647 | 665 |
{like name} name like ? |
648 | 666 |
{in name} name in [?, ?, ..] |
649 | 667 |
|
650 |
- {insert} (key1, key2, key3) values (?, ?, ?) |
|
651 |
- {update} set key1 = ?, key2 = ?, key3 = ? |
|
668 |
+ {insert} (key1, key2, key3) values (?, ?, ?) |
|
669 |
+ {update} set key1 = ?, key2 = ?, key3 = ? |
|
652 | 670 |
|
653 |
- # Sample1 |
|
671 |
+ # Sample |
|
654 | 672 |
$query = $sql_tmpl->create_sql( |
655 | 673 |
"insert into table {insert key1 key2}" |
656 | 674 |
); |
657 |
- # Result1 |
|
658 |
- $sql : "insert into table (key1, key2) values (?, ?)" |
|
675 |
+ # Expanded |
|
676 |
+ $query->sql : "insert into table (key1, key2) values (?, ?)" |
|
659 | 677 |
|
660 | 678 |
|
661 |
- # Sample2 |
|
679 |
+ # Sample |
|
662 | 680 |
$query = $sql_tmpl->create_sql( |
663 | 681 |
"update table {update key1 key2} where {= key3}" |
664 | 682 |
); |
665 | 683 |
|
666 |
- # Result2 |
|
667 |
- $query->{sql} : "update table set key1 = ?, key2 = ? where key3 = ?;" |
|
684 |
+ # Expanded |
|
685 |
+ $query->sql : "update table set key1 = ?, key2 = ? where key3 = ?;" |
|
668 | 686 |
|
669 | 687 |
=head1 AUTHOR |
670 | 688 |
|