Showing 1 changed files with 119 additions and 0 deletions
+119
lib/DBI/Custom.pm
... ...
@@ -580,6 +580,92 @@ sub delete_all {
580 580
     return $self->delete($table, {}, undef, {allow_delete_all => 1});
581 581
 }
582 582
 
583
+sub _select_usage { return << 'EOS' }
584
+Usage select :
585
+$dbi->select(
586
+    $table,                # must be string
587
+    [@$columns],           # must be array reference. this is optional
588
+    {%$where_params},      # must be hash reference.  this is optional
589
+    $append_statement,     # must be string.          this is optional
590
+    $query_edit_callback   # must be code reference.  this is optional
591
+);
592
+EOS
593
+
594
+sub select {
595
+    my $self = shift;
596
+    
597
+    # Check argument
598
+    croak($self->_select_usage) unless @_;
599
+    
600
+    # Arguments
601
+    my $tables = shift || '';
602
+    $tables    = [$tables] unless ref $tables;
603
+    
604
+    my $columns          = ref $_[0] eq 'ARRAY' ? shift : [];
605
+    my $where_params     = ref $_[0] eq 'HASH'  ? shift : {};
606
+    my $append_statement = shift unless ref $_[0] || '';
607
+    my $query_edit_cb    = shift if ref $_[0] eq 'CODE';
608
+    
609
+    # Check rest argument
610
+    croak($self->_select_usage) unless @_;
611
+    
612
+    # SQL template for select statement
613
+    my $template = 'select ';
614
+    
615
+    # Join column clause
616
+    if (@$columns) {
617
+        foreach my $column (@$columns) {
618
+            $template .= "$column, ";
619
+        }
620
+        $template .= s/, $/ /;
621
+    }
622
+    else {
623
+        $template .= '* ';
624
+    }
625
+    
626
+    # Join table
627
+    foreach my $table (@$tables) {
628
+        $template .= "$table, ";
629
+    }
630
+    $template =~ s/, / /;
631
+    
632
+    # Where clause keys
633
+    my @where_keys = keys %$where_params;
634
+    
635
+    # Join where clause
636
+    if (@where_keys) {
637
+        $template .= 'where ';
638
+        foreach my $where_key (@where_keys) {
639
+            $template .= "{= $where_key} && ";
640
+        }
641
+    }
642
+    $template =~ s/ && $//;
643
+    
644
+    # Append something to last of statement
645
+    if ($append_statement =~ s/^where //) {
646
+        if (@where_keys) {
647
+            $template .= " && $append_statement";
648
+        }
649
+        else {
650
+            $template .= " where $append_statement";
651
+        }
652
+    }
653
+    else {
654
+        $template .= " $append_statement";
655
+    }
656
+    
657
+    # Create query
658
+    my $query = $self->create_query($template);
659
+    
660
+    # Query edit
661
+    $query_edit_cb->($query) if $query_edit_cb;
662
+    
663
+    # Execute query
664
+    my $result = $self->execute($query, $where_params);
665
+    
666
+    return $result;
667
+}
668
+
583 669
 sub _query_caches     : ClassAttr { type => 'hash',
584 670
                                     auto_build => sub {shift->_query_caches({}) } }
585 671
                                     
... ...
@@ -909,6 +995,39 @@ If tranzation is died, rollback is execute.
909 995
     
910 996
 This method is same as DBI last_insert_id;
911 997
 
998
+=head2 select
999
+    
1000
+    # Select
1001
+    $dbi->select(
1002
+        $table,                # must be string or array;
1003
+        [@$columns],           # must be array reference. this is optional
1004
+        {%$where_params},      # must be hash reference.  this is optional
1005
+        $append_statement,     # must be string.          this is optional
1006
+        $query_edit_callback   # must be code reference.  this is optional
1007
+    );
1008
+    
1009
+    # Sample
1010
+    $dbi->select(
1011
+        'Books',
1012
+        ['title', 'author'],
1013
+        {id => 1},
1014
+        "for update",
1015
+        sub {
1016
+            my $query = shift;
1017
+            $query->bind_filter(sub {
1018
+                # ...
1019
+            });
1020
+        }
1021
+    );
1022
+    
1023
+    # The way to join multi tables
1024
+    $dbi->select(
1025
+        ['table1', 'table2'],
1026
+        ['table1.id as table1_id', 'title'],
1027
+        {table1.id => 1},
1028
+        "where table1.id = table2.id",
1029
+    );
1030
+
912 1031
 =head1 Class Accessors
913 1032
 
914 1033
 =head2 query_cache_max