bind_filter argument is changed to ($value, $key...
..., $dbi, $infos) (not backword compa...
| ... | ... | @@ -10,13 +10,11 @@ my $builder = Module::Build->new( | 
| 10 | 10 |      build_requires => { | 
| 11 | 11 | 'Test::More' => 0, | 
| 12 | 12 | }, | 
| 13 | -    recommends => { | |
| 14 | - 'DBD::SQLite' => 1.25, | |
| 15 | - 'Time::Piece' => 1.15 | |
| 16 | - }, | |
| 17 | 13 |      requires => { | 
| 18 | 14 | 'Object::Simple' => 2.0702, | 
| 19 | 15 | 'DBI' => 1.605, | 
| 16 | + 'DBD::SQLite' => 1.25, | |
| 17 | + 'Time::Piece' => 1.15 | |
| 20 | 18 | }, | 
| 21 | 19 | add_to_cleanup => [ 'DBIx-Custom-*' ], | 
| 22 | 20 | create_makefile_pl => 'traditional', | 
| ... | ... | @@ -1,3 +1,7 @@ | 
| 1 | +0.0601 | |
| 2 | + bind_filter argument is changed to ($value, $key, $dbi, $infos) (not backword compatible) | |
| 3 | + fetch_filter argument is changed to ($value, $key, $dbi, $infos) (not backword compatible) | |
| 4 | + run_transaction argument is changed to ($dbi) | |
| 1 | 5 | 0.0502 | 
| 2 | 6 | update document | 
| 3 | 7 | 0.0501 | 
| ... | ... | @@ -3,7 +3,7 @@ use 5.008001; | 
| 3 | 3 | package DBIx::Custom; | 
| 4 | 4 | use Object::Simple; | 
| 5 | 5 |  | 
| 6 | -our $VERSION = '0.0502'; | |
| 6 | +our $VERSION = '0.0601'; | |
| 7 | 7 |  | 
| 8 | 8 | use Carp 'croak'; | 
| 9 | 9 | use DBI; | 
| ... | ... | @@ -12,7 +12,7 @@ use DBIx::Custom::Result; | 
| 12 | 12 | use DBIx::Custom::SQL::Template; | 
| 13 | 13 |  | 
| 14 | 14 |  | 
| 15 | -### Class-Object Accessors | |
| 15 | +### Accessors | |
| 16 | 16 |  sub user        : ClassObjectAttr { initialize => {clone => 'scalar'} } | 
| 17 | 17 |  sub password    : ClassObjectAttr { initialize => {clone => 'scalar'} } | 
| 18 | 18 |  sub data_source : ClassObjectAttr { initialize => {clone => 'scalar'} } | 
| ... | ... | @@ -60,8 +60,7 @@ sub sql_template : ClassObjectAttr { | 
| 60 | 60 | } | 
| 61 | 61 | } | 
| 62 | 62 |  | 
| 63 | -### Object Accessor | |
| 64 | -sub dbh          : Attr {} | |
| 63 | +sub dbh : Attr {} | |
| 65 | 64 |  | 
| 66 | 65 |  | 
| 67 | 66 | ### Methods | 
| ... | ... | @@ -276,6 +275,7 @@ sub execute { | 
| 276 | 275 |  | 
| 277 | 276 | # Create result | 
| 278 | 277 |          my $result = $result_class->new({ | 
| 278 | + _dbi => $self, | |
| 279 | 279 | sth => $sth, | 
| 280 | 280 | fetch_filter => $query->fetch_filter, | 
| 281 | 281 | no_fetch_filters => $query->no_fetch_filters | 
| ... | ... | @@ -328,8 +328,8 @@ sub _build_bind_values { | 
| 328 | 328 |                          { | 
| 329 | 329 | push @bind_values, | 
| 330 | 330 | $bind_filter->($root_params->[$current_key->[0]], | 
| 331 | - $original_key, | |
| 332 | - $table, $column); | |
| 331 | + $original_key, $self, | |
| 332 | +                                                {table => $table, column => $column}); | |
| 333 | 333 | } | 
| 334 | 334 | # Not filtering | 
| 335 | 335 |                          else { | 
| ... | ... | @@ -349,8 +349,8 @@ sub _build_bind_values { | 
| 349 | 349 |                          { | 
| 350 | 350 | push @bind_values, | 
| 351 | 351 |                                   $bind_filter->($root_params->{$current_key}, | 
| 352 | - $original_key, | |
| 353 | - $table, $column); | |
| 352 | + $original_key, $self, | |
| 353 | +                                                {table => $table, column => $column}); | |
| 354 | 354 | } | 
| 355 | 355 | # Not filtering | 
| 356 | 356 |                          else { | 
| ... | ... | @@ -408,7 +408,7 @@ sub run_transaction { | 
| 408 | 408 | $self->_auto_commit(0); | 
| 409 | 409 |  | 
| 410 | 410 | # Run transaction | 
| 411 | -    eval {$transaction->()}; | |
| 411 | +    eval {$transaction->($self)}; | |
| 412 | 412 |  | 
| 413 | 413 | # Tranzaction error | 
| 414 | 414 | my $transaction_error = $@; | 
| ... | ... | @@ -753,11 +753,11 @@ Object::Simple->build_class; | 
| 753 | 753 |  | 
| 754 | 754 | DBIx::Custom - Customizable simple DBI | 
| 755 | 755 |  | 
| 756 | -=head1 VERSION | |
| 756 | +=head1 Version | |
| 757 | 757 |  | 
| 758 | -Version 0.0502 | |
| 758 | +Version 0.0601 | |
| 759 | 759 |  | 
| 760 | -=head1 CAUTION | |
| 760 | +=head1 Caution | |
| 761 | 761 |  | 
| 762 | 762 | This module is now experimental stage. | 
| 763 | 763 |  | 
| ... | ... | @@ -766,38 +766,56 @@ because I want this module stable, and not to damage your DB data by this module | 
| 766 | 766 |  | 
| 767 | 767 | Please tell me bug if you find | 
| 768 | 768 |  | 
| 769 | -=head1 SYNOPSIS | |
| 769 | +=head1 Synopsys | |
| 770 | 770 |  | 
| 771 | 771 | my $dbi = DBIx::Custom->new; | 
| 772 | 772 |  | 
| 773 | 773 | my $query = $dbi->create_query($template); | 
| 774 | 774 | $dbi->execute($query); | 
| 775 | 775 |  | 
| 776 | -=head1 CLASS-OBJECT ACCESSORS | |
| 776 | +=head1 Accessors | |
| 777 | 777 |  | 
| 778 | 778 | =head2 user | 
| 779 | 779 |  | 
| 780 | - # Set and get database user name | |
| 781 | - $self = $dbi->user($user); | |
| 782 | - $user = $dbi->user; | |
| 780 | +Set and get database user name | |
| 781 | + | |
| 782 | + # For object | |
| 783 | + $self = $self->user($user); | |
| 784 | + $user = $self->user; | |
| 785 | + | |
| 786 | + # For class | |
| 787 | + $class = $class->user($user); | |
| 788 | + $user = $class->user; | |
| 783 | 789 |  | 
| 784 | 790 | # Sample | 
| 785 | 791 |      $dbi->user('taro'); | 
| 786 | - | |
| 792 | + | |
| 787 | 793 | =head2 password | 
| 788 | 794 |  | 
| 789 | - # Set and get database password | |
| 790 | - $self = $dbi->password($password); | |
| 791 | - $password = $dbi->password; | |
| 795 | +Set and get database password | |
| 796 | + | |
| 797 | + # For object | |
| 798 | + $self = $self->password($password); | |
| 799 | + $password = $self->password; | |
| 800 | + | |
| 801 | + # For class | |
| 802 | + $class = $class->password($password); | |
| 803 | + $password = $class->password; | |
| 792 | 804 |  | 
| 793 | 805 | # Sample | 
| 794 | 806 |      $dbi->password('lkj&le`@s'); | 
| 795 | 807 |  | 
| 796 | 808 | =head2 data_source | 
| 797 | 809 |  | 
| 798 | - # Set and get database data source | |
| 799 | - $self = $dbi->data_source($data_soruce); | |
| 800 | - $data_source = $dbi->data_source; | |
| 810 | +Set and get database data source | |
| 811 | + | |
| 812 | + # For object | |
| 813 | + $self = $self->data_source($data_soruce); | |
| 814 | + $data_source = $self->data_source; | |
| 815 | + | |
| 816 | + # For class | |
| 817 | + $class = $class->data_source($data_soruce); | |
| 818 | + $data_source = $class->data_source; | |
| 801 | 819 |  | 
| 802 | 820 | # Sample(SQLite) | 
| 803 | 821 | $dbi->data_source(dbi:SQLite:dbname=$database); | 
| ... | ... | @@ -810,120 +828,227 @@ Please tell me bug if you find | 
| 810 | 828 |  | 
| 811 | 829 | =head2 database | 
| 812 | 830 |  | 
| 813 | - # Set and get database name | |
| 814 | - $self = $dbi->database($database); | |
| 815 | - $database = $dbi->database; | |
| 831 | +Set and get database name | |
| 832 | + | |
| 833 | + # For object | |
| 834 | + $self = $self->database($database); | |
| 835 | + $database = $self->database; | |
| 836 | + | |
| 837 | + # For class | |
| 838 | + $class = $class->database($database); | |
| 839 | + $database = $class->database; | |
| 840 | + | |
| 841 | + # Sample | |
| 842 | +    $dbi->database('books'); | |
| 816 | 843 |  | 
| 817 | 844 | =head2 host | 
| 818 | 845 |  | 
| 819 | - # Set and get host name | |
| 820 | - $self = $dbi->host($host); | |
| 821 | - $host = $dbi->host; | |
| 846 | +Set and get host name | |
| 847 | + | |
| 848 | + # For object | |
| 849 | + $self = $self->host($host); | |
| 850 | + $host = $self->host; | |
| 851 | + | |
| 852 | + # For class | |
| 853 | + $class = $class->host($host); | |
| 854 | + $host = $class->host; | |
| 855 | + | |
| 856 | + # Sample | |
| 857 | +    $dbi->host('somehost.com'); | |
| 858 | +    $dbi->host('127.1.2.3'); | |
| 822 | 859 |  | 
| 823 | 860 | =head2 port | 
| 824 | 861 |  | 
| 825 | - # Set and get port | |
| 826 | - $self = $dbi->port($port); | |
| 827 | - $port = $dbi->port; | |
| 862 | +Set and get port | |
| 863 | + | |
| 864 | + # For object | |
| 865 | + $self = $self->port($port); | |
| 866 | + $port = $self->port; | |
| 828 | 867 |  | 
| 829 | -This method will be used in subclass connect method. | |
| 868 | + # For class | |
| 869 | + $class = $class->port($port); | |
| 870 | + $port = $class->port; | |
| 871 | + | |
| 872 | + # Sample | |
| 873 | + $dbi->port(1198); | |
| 830 | 874 |  | 
| 831 | 875 | =head2 dbi_options | 
| 832 | 876 |  | 
| 833 | - # Set and get DBI option | |
| 834 | -    $self       = $dbi->dbi_options({$options => $value, ...}); | |
| 835 | - $dbi_options = $dbi->dbi_options; | |
| 877 | +Set and get DBI option | |
| 836 | 878 |  | 
| 879 | + # For object | |
| 880 | +    $self        = $self->dbi_options({$options => $value, ...}); | |
| 881 | + $dbi_options = $self->dbi_options; | |
| 882 | + | |
| 883 | + # For class | |
| 884 | +    $class       = $class->dbi_options({$options => $value, ...}); | |
| 885 | + $dbi_options = $class->dbi_options; | |
| 886 | + | |
| 837 | 887 | # Sample | 
| 838 | 888 |      $dbi->dbi_options({PrintError => 0, RaiseError => 1}); | 
| 839 | 889 |  | 
| 840 | -dbi_options is used when you connect database by using connect. | |
| 890 | +=head2 sql_template | |
| 841 | 891 |  | 
| 842 | -=head2 prepare | |
| 892 | +Set and get SQL::Template object | |
| 843 | 893 |  | 
| 844 | - $sth = $dbi->prepare($sql); | |
| 894 | + # For object | |
| 895 | + $self = $self->sql_template($sql_template); | |
| 896 | + $sql_template = $self->sql_template; | |
| 845 | 897 |  | 
| 846 | -This method is same as DBI::prepare | |
| 898 | + # For class | |
| 899 | + $class = $class->sql_template($sql_template); | |
| 900 | + $sql_template = $class->sql_template; | |
| 847 | 901 |  | 
| 848 | -=head2 do | |
| 902 | + # Sample | |
| 903 | + $dbi->sql_template(DBI::Cutom::SQL::Template->new); | |
| 849 | 904 |  | 
| 850 | - $dbi->do($sql, @bind_values); | |
| 905 | +=head2 filters | |
| 851 | 906 |  | 
| 852 | -This method is same as DBI::do | |
| 907 | +Set and get filters | |
| 853 | 908 |  | 
| 854 | -=head2 sql_template | |
| 909 | + # For object | |
| 910 | + $self = $self->filters($filters); | |
| 911 | + $filters = $self->filters; | |
| 855 | 912 |  | 
| 856 | - # Set and get SQL::Template object | |
| 857 | - $self = $dbi->sql_template($sql_template); | |
| 858 | - $sql_template = $dbi->sql_template; | |
| 913 | + # For class | |
| 914 | + $class = $class->filters($filters); | |
| 915 | + $filters = $class->filters; | |
| 859 | 916 |  | 
| 860 | 917 | # Sample | 
| 861 | - $dbi->sql_template(DBI::Cutom::SQL::Template->new); | |
| 918 | +    $ret = $dbi->filters->{encode_utf8}->($value); | |
| 862 | 919 |  | 
| 863 | -=head2 filters | |
| 920 | +=head2 formats | |
| 864 | 921 |  | 
| 865 | - # Set and get filters | |
| 866 | - $self = $dbi->filters($filters); | |
| 867 | - $filters = $dbi->filters; | |
| 922 | +Set and get formats | |
| 868 | 923 |  | 
| 869 | -=head2 formats | |
| 924 | + # For object | |
| 925 | + $self = $self->formats($formats); | |
| 926 | + $formats = $self->formats; | |
| 927 | + | |
| 928 | + # For class | |
| 929 | + $self = $self->formats($formats); | |
| 930 | + $formats = $self->formats; | |
| 931 | + | |
| 932 | + # Sample | |
| 933 | +    $datetime_format = $dbi->formats->{datetime}; | |
| 870 | 934 |  | 
| 871 | - # Set and get formats | |
| 872 | - $self = $dbi->formats($formats); | |
| 873 | - $formats = $dbi->formats; | |
| 874 | - | |
| 875 | 935 | =head2 bind_filter | 
| 876 | 936 |  | 
| 877 | - # Set and get binding filter | |
| 878 | - $self = $dbi->bind_filter($bind_filter); | |
| 879 | - $bind_filter = $dbi->bind_filter | |
| 937 | +Set and get binding filter | |
| 880 | 938 |  | 
| 881 | - # Sample | |
| 882 | -    $dbi->bind_filter($self->filters->{default_bind_filter}); | |
| 883 | - | |
| 939 | + # For object | |
| 940 | + $self = $self->bind_filter($bind_filter); | |
| 941 | + $bind_filter = $self->bind_filter | |
| 884 | 942 |  | 
| 885 | -you can get DBI database handle if you need. | |
| 943 | + # For object | |
| 944 | + $class = $class->bind_filter($bind_filter); | |
| 945 | + $bind_filter = $class->bind_filter | |
| 946 | + | |
| 947 | + # Sample | |
| 948 | +    $dbi->bind_filter(sub { | |
| 949 | + my ($value, $key, $dbi, $infos) = @_; | |
| 950 | + | |
| 951 | + # edit $value | |
| 952 | + | |
| 953 | + return $value; | |
| 954 | + }); | |
| 886 | 955 |  | 
| 887 | 956 | =head2 fetch_filter | 
| 888 | 957 |  | 
| 889 | - # Set and get Fetch filter | |
| 890 | - $self = $dbi->fetch_filter($fetch_filter); | |
| 891 | - $fetch_filter = $dbi->fetch_filter; | |
| 958 | +Set and get Fetch filter | |
| 959 | + | |
| 960 | + # For object | |
| 961 | + $self = $self->fetch_filter($fetch_filter); | |
| 962 | + $fetch_filter = $self->fetch_filter; | |
| 963 | + | |
| 964 | + # For class | |
| 965 | + $class = $class->fetch_filter($fetch_filter); | |
| 966 | + $fetch_filter = $class->fetch_filter; | |
| 892 | 967 |  | 
| 893 | 968 | # Sample | 
| 894 | -    $dbi->fetch_filter($self->filters->{default_fetch_filter}); | |
| 969 | +    $dbi->fetch_filter(sub { | |
| 970 | + my ($value, $key, $dbi, $infos) = @_; | |
| 971 | + | |
| 972 | + # edit $value | |
| 973 | + | |
| 974 | + return $value; | |
| 975 | + }); | |
| 895 | 976 |  | 
| 896 | 977 | =head2 no_bind_filters | 
| 897 | 978 |  | 
| 898 | - # Set and get no filter keys when binding | |
| 899 | - $self = $dbi->no_bind_filters($no_bind_filters); | |
| 900 | - $no_bind_filters = $dbi->no_bind_filters; | |
| 979 | +Set and get no filter keys when binding | |
| 980 | + | |
| 981 | + # For object | |
| 982 | + $self = $self->no_bind_filters($no_bind_filters); | |
| 983 | + $no_bind_filters = $self->no_bind_filters; | |
| 984 | + | |
| 985 | + # For class | |
| 986 | + $class = $class->no_bind_filters($no_bind_filters); | |
| 987 | + $no_bind_filters = $class->no_bind_filters; | |
| 988 | + | |
| 989 | + # Sample | |
| 990 | + $dbi->no_bind_filters(qw/title author/); | |
| 901 | 991 |  | 
| 902 | 992 | =head2 no_fetch_filters | 
| 903 | 993 |  | 
| 904 | - # Set and get no filter keys when fetching | |
| 905 | - $self = $dbi->no_fetch_filters($no_fetch_filters); | |
| 906 | - $no_fetch_filters = $dbi->no_fetch_filters; | |
| 994 | +Set and get no filter keys when fetching | |
| 995 | + | |
| 996 | + # For object | |
| 997 | + $self = $self->no_fetch_filters($no_fetch_filters); | |
| 998 | + $no_fetch_filters = $self->no_fetch_filters; | |
| 999 | + | |
| 1000 | + # For class | |
| 1001 | + $class = $class->no_fetch_filters($no_fetch_filters); | |
| 1002 | + $no_fetch_filters = $class->no_fetch_filters; | |
| 1003 | + | |
| 1004 | + # Sample | |
| 1005 | + $dbi->no_fetch_filters(qw/title author/); | |
| 907 | 1006 |  | 
| 908 | 1007 | =head2 result_class | 
| 909 | 1008 |  | 
| 910 | - # Set and get resultset class | |
| 1009 | +Set and get resultset class | |
| 1010 | + | |
| 1011 | + # For object | |
| 911 | 1012 | $self = $dbi->result_class($result_class); | 
| 912 | 1013 | $result_class = $dbi->result_class; | 
| 913 | 1014 |  | 
| 1015 | + # For class | |
| 1016 | + $class = $class->result_class($result_class); | |
| 1017 | + $result_class = $class->result_class; | |
| 1018 | + | |
| 914 | 1019 | # Sample | 
| 915 | 1020 |      $dbi->result_class('DBIx::Custom::Result'); | 
| 916 | 1021 |  | 
| 917 | 1022 | =head2 dbh | 
| 918 | 1023 |  | 
| 919 | - # Get database handle | |
| 920 | - $dbh = $self->dbh; | |
| 1024 | +Get database handle | |
| 1025 | + | |
| 1026 | + $self = $self->dbh($dbh); | |
| 1027 | + $dbh = $self->dbh; | |
| 1028 | + | |
| 1029 | + # Sample | |
| 1030 | + $table_info = $dbi->dbh->table_info | |
| 1031 | + | |
| 1032 | +=head2 query_cache_max | |
| 1033 | + | |
| 1034 | +Set and get query cache max | |
| 1035 | + | |
| 1036 | + $class = $class->query_cache_max($query_cache_max); | |
| 1037 | + $query_cache_max = $class->query_cache_max; | |
| 1038 | + | |
| 1039 | + # Sample | |
| 1040 | + DBIx::Custom->query_cache_max(50); | |
| 921 | 1041 |  | 
| 922 | -=head1 METHODS | |
| 1042 | +DBIx::Custom cache queries for performance. | |
| 1043 | + | |
| 1044 | +Default is 50 | |
| 1045 | + | |
| 1046 | +=head1 Methods | |
| 923 | 1047 |  | 
| 924 | 1048 | =head2 connect | 
| 925 | 1049 |  | 
| 926 | - # Connect to database | |
| 1050 | +Connect to database | |
| 1051 | + | |
| 927 | 1052 | $self = $dbi->connect; | 
| 928 | 1053 |  | 
| 929 | 1054 | # Sample | 
| ... | ... | @@ -933,92 +1058,114 @@ you can get DBI database handle if you need. | 
| 933 | 1058 |  | 
| 934 | 1059 | =head2 disconnect | 
| 935 | 1060 |  | 
| 936 | - # Disconnect database | |
| 1061 | +Disconnect database | |
| 1062 | + | |
| 1063 | + $self = $dbi->disconnect; | |
| 1064 | + | |
| 1065 | + # Sample | |
| 937 | 1066 | $dbi->disconnect; | 
| 938 | 1067 |  | 
| 939 | 1068 | If database is already disconnected, this method do noting. | 
| 940 | 1069 |  | 
| 941 | 1070 | =head2 reconnect | 
| 942 | 1071 |  | 
| 943 | - # Reconnect | |
| 1072 | +Reconnect to database | |
| 1073 | + | |
| 1074 | + $self = $dbi->reconnect; | |
| 1075 | + | |
| 1076 | + # Sample | |
| 944 | 1077 | $dbi->reconnect; | 
| 945 | 1078 |  | 
| 946 | 1079 | =head2 connected | 
| 947 | 1080 |  | 
| 948 | - # Check connected | |
| 949 | - $dbi->connected | |
| 1081 | +Check connected | |
| 1082 | + | |
| 1083 | + $is_connected = $self->connected; | |
| 1084 | + | |
| 1085 | + # Sample | |
| 1086 | +    if ($dbi->connected) { # do something } | |
| 950 | 1087 |  | 
| 951 | 1088 | =head2 filter_off | 
| 952 | 1089 |  | 
| 953 | - # bind_filter and fitch_filter off | |
| 954 | - $self->filter_off; | |
| 1090 | +bind_filter and fitch_filter off | |
| 1091 | + | |
| 1092 | + $self = $self->filter_off | |
| 1093 | + | |
| 1094 | + # Sample | |
| 1095 | + $dbi->filter_off; | |
| 955 | 1096 |  | 
| 956 | 1097 | This is equeal to | 
| 957 | 1098 |  | 
| 958 | - $self->bind_filter(undef); | |
| 959 | - $self->fetch_filter(undef); | |
| 1099 | + $dbi->bind_filter(undef); | |
| 1100 | + $dbi->fetch_filter(undef); | |
| 960 | 1101 |  | 
| 961 | 1102 | =head2 add_filter | 
| 962 | 1103 |  | 
| 963 | - # Add filter (hash ref or hash can be recieve) | |
| 964 | -    $self = $dbi->add_filter({$filter_name => $filter, ...}); | |
| 965 | - $self = $dbi->add_filter($filetr_name => $filter, ...); | |
| 1104 | +Resist filter | |
| 966 | 1105 |  | 
| 967 | - # Sample | |
| 1106 | +    $self = $self->add_filter({$name => $filter, ...}); | |
| 1107 | + # or | |
| 1108 | + $self = $self->add_filter($name => $filter, ...); | |
| 1109 | + | |
| 1110 | + # Sample (For example DBIx::Custom::Basic) | |
| 968 | 1111 | $dbi->add_filter( | 
| 969 | -        decode_utf8 => sub { | |
| 970 | - my ($key, $value, $table, $column) = @_; | |
| 971 | -            return Encode::decode('UTF-8', $value); | |
| 972 | - }, | |
| 973 | -        datetime_to_string => sub { | |
| 974 | - my ($key, $value, $table, $column) = @_; | |
| 975 | -            return $value->strftime('%Y-%m-%d %H:%M:%S') | |
| 976 | - }, | |
| 977 | -        default_bind_filter => sub { | |
| 978 | - my ($key, $value, $table, $column) = @_; | |
| 979 | -            if (ref $value eq 'Time::Piece') { | |
| 980 | -                return $dbi->filters->{datetime_to_string}->($value); | |
| 981 | - } | |
| 982 | -            else { | |
| 983 | -                return $dbi->filters->{decode_utf8}->($value); | |
| 984 | - } | |
| 985 | - }, | |
| 986 | - | |
| 987 | 1112 |          encode_utf8 => sub { | 
| 988 | - my ($key, $value) = @_; | |
| 989 | -            return Encode::encode('UTF-8', $value); | |
| 990 | - }, | |
| 991 | -        string_to_datetime => sub { | |
| 992 | - my ($key, $value) = @_; | |
| 993 | - return DateTime::Format::MySQL->parse_datetime($value); | |
| 1113 | + my ($value, $key, $dbi, $infos) = @_; | |
| 1114 | + utf8::upgrade($value) unless Encode::is_utf8($value); | |
| 1115 | +            return encode('UTF-8', $value); | |
| 994 | 1116 | }, | 
| 995 | -        default_fetch_filter => sub { | |
| 996 | - my ($key, $value, $type, $sth, $i) = @_; | |
| 997 | -            if ($type eq 'DATETIME') { | |
| 998 | -                return $dbi->filters->{string_to_datetime}->($value); | |
| 999 | - } | |
| 1000 | -            else { | |
| 1001 | -                return $dbi->filters->{encode_utf8}->($value); | |
| 1002 | - } | |
| 1117 | +        decode_utf8 => sub { | |
| 1118 | + my ($value, $key, $dbi, $infos) = @_; | |
| 1119 | +            return decode('UTF-8', $value) | |
| 1003 | 1120 | } | 
| 1004 | 1121 | ); | 
| 1005 | 1122 |  | 
| 1006 | -add_filter add filter to filters | |
| 1007 | - | |
| 1008 | 1123 | =head2 add_format | 
| 1009 | 1124 |  | 
| 1010 | - $dbi->add_format(date => '%Y:%m:%d'); | |
| 1125 | +Resist format | |
| 1126 | + | |
| 1127 | +    $self = $self->add_format({$name => $format, ...}); | |
| 1128 | + # or | |
| 1129 | + $self = $self->add_format($name => $format, ...); | |
| 1130 | + | |
| 1131 | + # Sample | |
| 1132 | + $dbi->add_format(date => '%Y:%m:%d', datetime => '%Y-%m-%d %H:%M:%S'); | |
| 1133 | + | |
| 1134 | +=head2 prepare | |
| 1135 | + | |
| 1136 | +Prepare statement handle | |
| 1137 | + | |
| 1138 | + $sth = $self->prepare($sql); | |
| 1139 | + | |
| 1140 | + # Sample | |
| 1141 | +    $sth = $dbi->prepare('select * from books;'); | |
| 1142 | + | |
| 1143 | +This method is same as DBI prepare method. | |
| 1144 | + | |
| 1145 | +=head2 do | |
| 1146 | + | |
| 1147 | +Execute SQL | |
| 1148 | + | |
| 1149 | + $ret_val = $self->do($sql, @bind_values); | |
| 1150 | + | |
| 1151 | + # Sample | |
| 1152 | +    $ret_val = $dbi->do('insert into books (title, author) values (?, ?)', | |
| 1153 | + 'Perl', 'taro'); | |
| 1154 | + | |
| 1155 | +This method is same as DBI do method. | |
| 1011 | 1156 |  | 
| 1012 | 1157 | =head2 create_query | 
| 1013 | 1158 |  | 
| 1014 | - # Create Query object from SQL template | |
| 1159 | +Create Query object from SQL template | |
| 1160 | + | |
| 1015 | 1161 | my $query = $dbi->create_query($template); | 
| 1016 | 1162 |  | 
| 1017 | 1163 | =head2 execute | 
| 1018 | 1164 |  | 
| 1019 | - # Parse SQL template and execute SQL | |
| 1165 | +Parse SQL template and execute SQL | |
| 1166 | + | |
| 1020 | 1167 | $result = $dbi->query($query, $params); | 
| 1021 | - $result = $dbi->query($template, $params); # Shorcut | |
| 1168 | + $result = $dbi->query($template, $params); # Shortcut | |
| 1022 | 1169 |  | 
| 1023 | 1170 | # Sample | 
| 1024 | 1171 |      $result = $dbi->query("select * from authors where {= name} and {= age}",  | 
| ... | ... | @@ -1032,8 +1179,11 @@ See also L<DBIx::Custom::SQL::Template> | 
| 1032 | 1179 |  | 
| 1033 | 1180 | =head2 run_transaction | 
| 1034 | 1181 |  | 
| 1035 | - # Run transaction | |
| 1182 | +Run transaction | |
| 1183 | + | |
| 1036 | 1184 |      $dbi->run_transaction(sub { | 
| 1185 | + my $dbi = shift; | |
| 1186 | + | |
| 1037 | 1187 | # do something | 
| 1038 | 1188 | }); | 
| 1039 | 1189 |  | 
| ... | ... | @@ -1042,7 +1192,8 @@ If tranzation is died, rollback is execute. | 
| 1042 | 1192 |  | 
| 1043 | 1193 | =head2 insert | 
| 1044 | 1194 |  | 
| 1045 | - # Insert | |
| 1195 | +Insert | |
| 1196 | + | |
| 1046 | 1197 | $dbi->insert($table, $insert_values); | 
| 1047 | 1198 |  | 
| 1048 | 1199 | # Sample | 
| ... | ... | @@ -1050,7 +1201,8 @@ If tranzation is died, rollback is execute. | 
| 1050 | 1201 |  | 
| 1051 | 1202 | =head2 update | 
| 1052 | 1203 |  | 
| 1053 | - # Update | |
| 1204 | +Update | |
| 1205 | + | |
| 1054 | 1206 | $dbi->update($table, $update_values, $where); | 
| 1055 | 1207 |  | 
| 1056 | 1208 | # Sample | 
| ... | ... | @@ -1058,12 +1210,14 @@ If tranzation is died, rollback is execute. | 
| 1058 | 1210 |  | 
| 1059 | 1211 | =head2 update_all | 
| 1060 | 1212 |  | 
| 1061 | - # Update all rows | |
| 1213 | +Update all rows | |
| 1214 | + | |
| 1062 | 1215 | $dbi->update($table, $updat_values); | 
| 1063 | 1216 |  | 
| 1064 | 1217 | =head2 delete | 
| 1065 | 1218 |  | 
| 1066 | - # Delete | |
| 1219 | +Delete | |
| 1220 | + | |
| 1067 | 1221 | $dbi->delete($table, $where); | 
| 1068 | 1222 |  | 
| 1069 | 1223 | # Sample | 
| ... | ... | @@ -1071,19 +1225,22 @@ If tranzation is died, rollback is execute. | 
| 1071 | 1225 |  | 
| 1072 | 1226 | =head2 delete_all | 
| 1073 | 1227 |  | 
| 1074 | - # Delete all rows | |
| 1228 | +Delete all rows | |
| 1229 | + | |
| 1075 | 1230 | $dbi->delete_all($table); | 
| 1076 | 1231 |  | 
| 1077 | 1232 | =head2 last_insert_id | 
| 1078 | 1233 |  | 
| 1079 | - # Get last insert id | |
| 1234 | +Get last insert id | |
| 1235 | + | |
| 1080 | 1236 | $last_insert_id = $dbi->last_insert_id; | 
| 1081 | - | |
| 1082 | -This method is same as DBI last_insert_id; | |
| 1237 | + | |
| 1238 | +This method is implemented by subclass. | |
| 1083 | 1239 |  | 
| 1084 | 1240 | =head2 select | 
| 1085 | 1241 |  | 
| 1086 | - # Select | |
| 1242 | +Select | |
| 1243 | + | |
| 1087 | 1244 | $dbi->select( | 
| 1088 | 1245 | $table, # must be string or array; | 
| 1089 | 1246 | [@$columns], # must be array reference. this is optional | 
| ... | ... | @@ -1114,16 +1271,6 @@ This method is same as DBI last_insert_id; | 
| 1114 | 1271 | "where table1.id = table2.id", | 
| 1115 | 1272 | ); | 
| 1116 | 1273 |  | 
| 1117 | -=head1 Class Accessors | |
| 1118 | - | |
| 1119 | -=head2 query_cache_max | |
| 1120 | - | |
| 1121 | - # Max query cache count | |
| 1122 | - $class = $class->query_cache_max($query_cache_max); | |
| 1123 | - $query_cache_max = $class->query_cache_max; | |
| 1124 | - | |
| 1125 | - # Sample | |
| 1126 | - DBIx::Custom->query_cache_max(50); | |
| 1127 | 1274 |  | 
| 1128 | 1275 | =head1 CAUTION | 
| 1129 | 1276 |  | 
| ... | ... | @@ -6,6 +6,7 @@ use warnings; | 
| 6 | 6 | use Carp 'croak'; | 
| 7 | 7 |  | 
| 8 | 8 | # Attributes | 
| 9 | +sub _dbi             : Attr {} | |
| 9 | 10 |  sub sth              : Attr {} | 
| 10 | 11 |  sub fetch_filter     : Attr {} | 
| 11 | 12 |  sub no_fetch_filters      : Attr { type => 'array', trigger => sub { | 
| ... | ... | @@ -34,8 +35,8 @@ sub fetch { | 
| 34 | 35 |          my $types = $sth->{TYPE}; | 
| 35 | 36 |          for (my $i = 0; $i < @$keys; $i++) { | 
| 36 | 37 |              next if $self->_no_fetch_filters_map->{$keys->[$i]}; | 
| 37 | - $row->[$i]= $fetch_filter->($row->[$i], $keys->[$i], $types->[$i], | |
| 38 | - $sth, $i); | |
| 38 | + $row->[$i]= $fetch_filter->($row->[$i], $keys->[$i], $self->_dbi, | |
| 39 | +                                        {type => $types->[$i], sth => $sth, index => $i}); | |
| 39 | 40 | } | 
| 40 | 41 | } | 
| 41 | 42 | return wantarray ? @$row : $row; | 
| ... | ... | @@ -66,8 +67,8 @@ sub fetch_hash { | 
| 66 | 67 | } | 
| 67 | 68 |              else { | 
| 68 | 69 |                  $row_hash->{$keys->[$i]} | 
| 69 | - = $fetch_filter->($row->[$i], $keys->[$i], | |
| 70 | - $types->[$i], $sth, $i); | |
| 70 | + = $fetch_filter->($row->[$i], $keys->[$i], $self->_dbi, | |
| 71 | +                                    {type => $types->[$i], sth => $sth, index => $i}); | |
| 71 | 72 | } | 
| 72 | 73 | } | 
| 73 | 74 | } | 
| ... | ... | @@ -182,8 +182,12 @@ $dbi->do($CREATE_TABLE->{0}); | 
| 182 | 182 |  $insert_tmpl  = "insert into table1 {insert key1 key2};";
 | 
| 183 | 183 | $insert_query = $dbi->create_query($insert_tmpl); | 
| 184 | 184 |  $insert_query->bind_filter(sub {
 | 
| 185 | - my ($value, $key, $table, $column) = @_; | |
| 186 | -    if ($key eq 'key1' && $table eq '' && $column eq 'key1') {
 | |
| 185 | + my ($value, $key, $dbi, $infos) = @_; | |
| 186 | +    my ($table, $column) = @{$infos}{qw/table column/};
 | |
| 187 | + | |
| 188 | + if ($key eq 'key1' && $table eq '' && $column eq 'key1' | |
| 189 | +        && $dbi->isa('DBIx::Custom'))
 | |
| 190 | +    {
 | |
| 187 | 191 | return $value * 2; | 
| 188 | 192 | } | 
| 189 | 193 | return $value; | 
| ... | ... | @@ -191,8 +195,12 @@ $insert_query->bind_filter(sub { | 
| 191 | 195 |  $dbi->execute($insert_query, {key1 => 1, key2 => 2});
 | 
| 192 | 196 |  $select_query = $dbi->create_query($SELECT_TMPL->{0});
 | 
| 193 | 197 |  $select_query->fetch_filter(sub {
 | 
| 194 | - my ($value, $key, $type, $sth, $i) = @_; | |
| 195 | -    if ($key eq 'key2' && $type =~ /char/ && $sth->can('execute') && $i == 1) {
 | |
| 198 | + my ($value, $key, $dbi, $infos) = @_; | |
| 199 | +    my ($type, $sth, $i) = @{$infos}{qw/type sth index/};
 | |
| 200 | + | |
| 201 | + if ($key eq 'key2' && $type =~ /char/ && ref $sth eq 'DBI::st' | |
| 202 | +        && $i == 1 && $dbi->isa('DBIx::Custom'))
 | |
| 203 | +    {
 | |
| 196 | 204 | return $value * 3; | 
| 197 | 205 | } | 
| 198 | 206 | return $value; | 
| ... | ... | @@ -214,7 +222,9 @@ $dbi->do($CREATE_TABLE->{0}); | 
| 214 | 222 |  $insert_tmpl  = "insert into table1 {insert table1.key1 table1.key2}";
 | 
| 215 | 223 | $insert_query = $dbi->create_query($insert_tmpl); | 
| 216 | 224 |  $insert_query->bind_filter(sub {
 | 
| 217 | - my ($value, $key, $table, $column) = @_; | |
| 225 | + my ($value, $key, $dbi, $infos) = @_; | |
| 226 | +    my ($table, $column) = @{$infos}{qw/table column/};
 | |
| 227 | + | |
| 218 | 228 |      if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1') {
 | 
| 219 | 229 | return $value * 3; | 
| 220 | 230 | } | 
| ... | ... | @@ -233,7 +243,9 @@ $dbi->execute($insert_query, {key1 => 2, key2 => 4}); | 
| 233 | 243 |  $select_tmpl = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
 | 
| 234 | 244 | $select_query = $dbi->create_query($select_tmpl); | 
| 235 | 245 |  $select_query->bind_filter(sub {
 | 
| 236 | - my ($value, $key, $table, $column) = @_; | |
| 246 | + my ($value, $key, $dbi, $infos) = @_; | |
| 247 | +    my ($table, $column) = @{$infos}{qw/table column/};
 | |
| 248 | + | |
| 237 | 249 |      if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1' || $key eq 'table1.key2') {
 | 
| 238 | 250 | return $value * 2; | 
| 239 | 251 | } | 
| ... | ... | @@ -407,6 +419,7 @@ test 'run_tansaction'; | 
| 407 | 419 |  $dbi->do($DROP_TABLE->{0});
 | 
| 408 | 420 |  $dbi->do($CREATE_TABLE->{0});
 | 
| 409 | 421 |  $dbi->run_transaction(sub {
 | 
| 422 | + my $dbi = shift; | |
| 410 | 423 |      $insert_tmpl = 'insert into table1 {insert key1 key2}';
 | 
| 411 | 424 |      $dbi->execute($insert_tmpl, {key1 => 1, key2 => 2});
 | 
| 412 | 425 |      $dbi->execute($insert_tmpl, {key1 => 3, key2 => 4});
 | 
| ... | ... | @@ -420,6 +433,7 @@ $dbi->do($CREATE_TABLE->{0}); | 
| 420 | 433 |  $dbi->dbh->{RaiseError} = 0;
 | 
| 421 | 434 |  eval{
 | 
| 422 | 435 |      $dbi->run_transaction(sub {
 | 
| 436 | + my $dbi = shift; | |
| 423 | 437 |          $insert_tmpl = 'insert into table1 {insert key1 key2}';
 | 
| 424 | 438 |          $dbi->execute($insert_tmpl, {key1 => 1, key2 => 2});
 | 
| 425 | 439 | die "Fatal Error"; | 
| ... | ... | @@ -208,7 +208,9 @@ is_deeply(\@rows, [[1, 2], [3, 4]], $test); | 
| 208 | 208 |  | 
| 209 | 209 | test 'fetch filter'; | 
| 210 | 210 |  $fetch_filter = sub { | 
| 211 | - my ($value, $key, $type, $sth, $i) = @_; | |
| 211 | + my ($value, $key, $dbi, $infos) = @_; | |
| 212 | +    my ($type, $sth, $i) = @{$infos}{qw/type sth index/}; | |
| 213 | + | |
| 212 | 214 |      if ($key eq 'key1' && $value == 1 && $type =~ /char/i && $i == 0 && $sth->{TYPE}->[$i] eq $type) { | 
| 213 | 215 | return $value * 3; | 
| 214 | 216 | } |