DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1547 lines | 40.743kb
cleanup
yuki-kimoto authored on 2009-12-22
1
package DBIx::Custom;
2

            
3
use strict;
4
use warnings;
5

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
6
use base 'Object::Simple';
many change
yuki-kimoto authored on 2010-02-11
7

            
packaging one directory
yuki-kimoto authored on 2009-11-16
8
use Carp 'croak';
9
use DBI;
10
use DBIx::Custom::Result;
cleanup
yuki-kimoto authored on 2010-02-11
11
use DBIx::Custom::Query;
cleanup
yuki-kimoto authored on 2010-08-05
12
use DBIx::Custom::QueryBuilder;
update document
yuki-kimoto authored on 2010-05-27
13
use Encode qw/encode_utf8 decode_utf8/;
packaging one directory
yuki-kimoto authored on 2009-11-16
14

            
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
15
__PACKAGE__->attr('dbh');
cleanup
yuki-kimoto authored on 2010-04-28
16
__PACKAGE__->attr([qw/user password data_source/]);
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
17
__PACKAGE__->attr([qw/default_bind_filter default_fetch_filter/]);
packaging one directory
yuki-kimoto authored on 2009-11-16
18

            
removed register_format()
yuki-kimoto authored on 2010-05-26
19
__PACKAGE__->dual_attr('filters', default => sub { {} },
20
                                  inherit => 'hash_copy');
21
__PACKAGE__->register_filter(
update document
yuki-kimoto authored on 2010-05-27
22
    encode_utf8 => sub { encode_utf8($_[0]) },
23
    decode_utf8 => sub { decode_utf8($_[0]) }
removed register_format()
yuki-kimoto authored on 2010-05-26
24
);
packaging one directory
yuki-kimoto authored on 2009-11-16
25

            
cleanup
yuki-kimoto authored on 2010-04-28
26
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result');
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
27
__PACKAGE__->attr(query_builder  => sub {DBIx::Custom::QueryBuilder->new});
added cache_method attribute
yuki-kimoto authored on 2010-06-25
28

            
add cache attribute
yuki-kimoto authored on 2010-06-14
29
__PACKAGE__->attr(cache => 1);
added cache_method attribute
yuki-kimoto authored on 2010-06-25
30
__PACKAGE__->attr(cache_method => sub {
31
    sub {
32
        my $self = shift;
33
        
34
        $self->{_cached} ||= {};
35
        
36
        if (@_ > 1) {
37
            $self->{_cached}{$_[0]} = $_[1] 
38
        }
39
        else {
40
            return $self->{_cached}{$_[0]}
41
        }
42
    }
43
});
removed register_format()
yuki-kimoto authored on 2010-05-26
44

            
added check_filter attribute
yuki-kimoto authored on 2010-08-08
45
__PACKAGE__->attr(filter_check => 1);
46

            
packaging one directory
yuki-kimoto authored on 2009-11-16
47
sub connect {
removed register_format()
yuki-kimoto authored on 2010-05-26
48
    my $proto = shift;
49
    
50
    # Create
51
    my $self = ref $proto ? $proto : $proto->new(@_);
update document
yuki-kimoto authored on 2010-01-30
52
    
53
    # Information
packaging one directory
yuki-kimoto authored on 2009-11-16
54
    my $data_source = $self->data_source;
55
    my $user        = $self->user;
56
    my $password    = $self->password;
57
    
update document
yuki-kimoto authored on 2010-01-30
58
    # Connect
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
59
    my $dbh = eval {DBI->connect(
packaging one directory
yuki-kimoto authored on 2009-11-16
60
        $data_source,
61
        $user,
62
        $password,
63
        {
64
            RaiseError => 1,
65
            PrintError => 0,
66
            AutoCommit => 1,
67
        }
68
    )};
69
    
update document
yuki-kimoto authored on 2010-01-30
70
    # Connect error
packaging one directory
yuki-kimoto authored on 2009-11-16
71
    croak $@ if $@;
72
    
update document
yuki-kimoto authored on 2010-01-30
73
    # Database handle
packaging one directory
yuki-kimoto authored on 2009-11-16
74
    $self->dbh($dbh);
update document
yuki-kimoto authored on 2010-01-30
75
    
packaging one directory
yuki-kimoto authored on 2009-11-16
76
    return $self;
77
}
78

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
79
sub register_filter {
80
    my $invocant = shift;
update document
yuki-kimoto authored on 2010-01-30
81
    
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
82
    # Register filter
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
83
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
84
    $invocant->filters({%{$invocant->filters}, %$filters});
update document
yuki-kimoto authored on 2010-01-30
85
    
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
86
    return $invocant;
packaging one directory
yuki-kimoto authored on 2009-11-16
87
}
88

            
removed register_format()
yuki-kimoto authored on 2010-05-26
89
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append filter/;
cleanup insert
yuki-kimoto authored on 2010-04-28
90

            
packaging one directory
yuki-kimoto authored on 2009-11-16
91
sub insert {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
92
    my ($self, %args) = @_;
removed register_format()
yuki-kimoto authored on 2010-05-26
93

            
cleanup insert
yuki-kimoto authored on 2010-04-28
94
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
95
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
96
        croak qq{"$name" is invalid argument}
cleanup insert
yuki-kimoto authored on 2010-04-28
97
          unless $VALID_INSERT_ARGS{$name};
98
    }
99
    
removed register_format()
yuki-kimoto authored on 2010-05-26
100
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
101
    my $table  = $args{table} || '';
102
    my $param  = $args{param} || {};
103
    my $append = $args{append} || '';
104
    my $filter = $args{filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
105
    
106
    # Insert keys
removed register_format()
yuki-kimoto authored on 2010-05-26
107
    my @insert_keys = keys %$param;
packaging one directory
yuki-kimoto authored on 2009-11-16
108
    
109
    # Templte for insert
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
110
    my $source = "insert into $table {insert_param "
update document
yuki-kimoto authored on 2010-08-07
111
               . join(' ', @insert_keys) . '}';
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
112
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
113
    
114
    # Execute query
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
115
    my $ret_val = $self->execute($source, param  => $param, 
removed register_format()
yuki-kimoto authored on 2010-05-26
116
                                            filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
117
    
118
    return $ret_val;
119
}
120

            
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
121
our %VALID_UPDATE_ARGS
removed register_format()
yuki-kimoto authored on 2010-05-26
122
  = map { $_ => 1 } qw/table param where append filter allow_update_all/;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
123

            
packaging one directory
yuki-kimoto authored on 2009-11-16
124
sub update {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
125
    my ($self, %args) = @_;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
126
    
127
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
128
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
129
        croak qq{"$name" is invalid argument}
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
130
          unless $VALID_UPDATE_ARGS{$name};
131
    }
132
    
133
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
134
    my $table            = $args{table} || '';
135
    my $param            = $args{param} || {};
136
    my $where            = $args{where} || {};
add tests
yuki-kimoto authored on 2010-08-10
137
    my $append = $args{append} || '';
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
138
    my $filter           = $args{filter};
139
    my $allow_update_all = $args{allow_update_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
140
    
141
    # Update keys
removed register_format()
yuki-kimoto authored on 2010-05-26
142
    my @update_keys = keys %$param;
packaging one directory
yuki-kimoto authored on 2009-11-16
143
    
144
    # Where keys
removed register_format()
yuki-kimoto authored on 2010-05-26
145
    my @where_keys = keys %$where;
packaging one directory
yuki-kimoto authored on 2009-11-16
146
    
147
    # Not exists where keys
add tests
yuki-kimoto authored on 2010-08-10
148
    croak qq{"where" argument must be specified and } .
149
          qq{contains the pairs of column name and value}
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
150
      if !@where_keys && !$allow_update_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
151
    
152
    # Update clause
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
153
    my $update_clause = '{update_param ' . join(' ', @update_keys) . '}';
packaging one directory
yuki-kimoto authored on 2009-11-16
154
    
155
    # Where clause
156
    my $where_clause = '';
simplify filtering system
yuki-kimoto authored on 2010-05-01
157
    my $new_where = {};
many change
yuki-kimoto authored on 2010-04-30
158
    
packaging one directory
yuki-kimoto authored on 2009-11-16
159
    if (@where_keys) {
160
        $where_clause = 'where ';
add tests
yuki-kimoto authored on 2010-08-10
161
        $where_clause .= "{= $_} and " for @where_keys;
packaging one directory
yuki-kimoto authored on 2009-11-16
162
        $where_clause =~ s/ and $//;
163
    }
164
    
add tests
yuki-kimoto authored on 2010-08-10
165
    # Source of SQL
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
166
    my $source = "update $table $update_clause $where_clause";
add tests
yuki-kimoto authored on 2010-08-10
167
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
168
    
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
169
    # Rearrange parameters
removed register_format()
yuki-kimoto authored on 2010-05-26
170
    foreach my $wkey (@where_keys) {
simplify filtering system
yuki-kimoto authored on 2010-05-01
171
        
removed register_format()
yuki-kimoto authored on 2010-05-26
172
        if (exists $param->{$wkey}) {
173
            $param->{$wkey} = [$param->{$wkey}]
174
              unless ref $param->{$wkey} eq 'ARRAY';
simplify filtering system
yuki-kimoto authored on 2010-05-01
175
            
removed register_format()
yuki-kimoto authored on 2010-05-26
176
            push @{$param->{$wkey}}, $where->{$wkey};
simplify filtering system
yuki-kimoto authored on 2010-05-01
177
        }
add tests
yuki-kimoto authored on 2010-05-01
178
        else {
removed register_format()
yuki-kimoto authored on 2010-05-26
179
            $param->{$wkey} = $where->{$wkey};
add tests
yuki-kimoto authored on 2010-05-01
180
        }
simplify filtering system
yuki-kimoto authored on 2010-05-01
181
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
182
    
183
    # Execute query
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
184
    my $ret_val = $self->execute($source, param  => $param, 
add tests
yuki-kimoto authored on 2010-08-10
185
                                 filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
186
    
187
    return $ret_val;
188
}
189

            
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
190
sub update_all { shift->update(allow_update_all => 1, @_) };
packaging one directory
yuki-kimoto authored on 2009-11-16
191

            
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
192
our %VALID_DELETE_ARGS
removed register_format()
yuki-kimoto authored on 2010-05-26
193
  = map { $_ => 1 } qw/table where append filter allow_delete_all/;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
194

            
packaging one directory
yuki-kimoto authored on 2009-11-16
195
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
196
    my ($self, %args) = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
197
    
198
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
199
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
200
        croak qq{"$name" is invalid argument}
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
201
          unless $VALID_DELETE_ARGS{$name};
202
    }
203
    
204
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
205
    my $table            = $args{table} || '';
206
    my $where            = $args{where} || {};
add tests
yuki-kimoto authored on 2010-08-10
207
    my $append = $args{append};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
208
    my $filter           = $args{filter};
209
    my $allow_delete_all = $args{allow_delete_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
210
    
211
    # Where keys
removed register_format()
yuki-kimoto authored on 2010-05-26
212
    my @where_keys = keys %$where;
packaging one directory
yuki-kimoto authored on 2009-11-16
213
    
214
    # Not exists where keys
add tests
yuki-kimoto authored on 2010-08-10
215
    croak qq{"where" argument must be specified and } .
216
          qq{contains the pairs of column name and value}
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
217
      if !@where_keys && !$allow_delete_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
218
    
219
    # Where clause
220
    my $where_clause = '';
221
    if (@where_keys) {
222
        $where_clause = 'where ';
add tests
yuki-kimoto authored on 2010-08-10
223
        $where_clause .= "{= $_} and " for @where_keys;
packaging one directory
yuki-kimoto authored on 2009-11-16
224
        $where_clause =~ s/ and $//;
225
    }
226
    
add tests
yuki-kimoto authored on 2010-08-10
227
    # Source of SQL
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
228
    my $source = "delete from $table $where_clause";
add tests
yuki-kimoto authored on 2010-08-10
229
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
230
    
231
    # Execute query
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
232
    my $ret_val = $self->execute($source, param  => $where, 
add tests
yuki-kimoto authored on 2010-08-10
233
                                 filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
234
    
235
    return $ret_val;
236
}
237

            
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
238
sub delete_all { shift->delete(allow_delete_all => 1, @_) }
packaging one directory
yuki-kimoto authored on 2009-11-16
239

            
refactoring select
yuki-kimoto authored on 2010-04-28
240
our %VALID_SELECT_ARGS
added commit method
yuki-kimoto authored on 2010-05-27
241
  = map { $_ => 1 } qw/table column where append relation filter param/;
refactoring select
yuki-kimoto authored on 2010-04-28
242

            
packaging one directory
yuki-kimoto authored on 2009-11-16
243
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
244
    my ($self, %args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
245
    
refactoring select
yuki-kimoto authored on 2010-04-28
246
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
247
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
248
        croak qq{"$name" is invalid argument}
refactoring select
yuki-kimoto authored on 2010-04-28
249
          unless $VALID_SELECT_ARGS{$name};
250
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
251
    
refactoring select
yuki-kimoto authored on 2010-04-28
252
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
253
    my $tables = $args{table} || [];
removed register_format()
yuki-kimoto authored on 2010-05-26
254
    $tables = [$tables] unless ref $tables eq 'ARRAY';
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
255
    my $columns  = $args{column} || [];
update document
yuki-kimoto authored on 2010-08-07
256
    my $where    = $args{where};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
257
    my $relation = $args{relation};
258
    my $append   = $args{append};
259
    my $filter   = $args{filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
260
    
add tests
yuki-kimoto authored on 2010-08-10
261
    # Source of SQL
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
262
    my $source = 'select ';
packaging one directory
yuki-kimoto authored on 2009-11-16
263
    
added commit method
yuki-kimoto authored on 2010-05-27
264
    # Column clause
packaging one directory
yuki-kimoto authored on 2009-11-16
265
    if (@$columns) {
266
        foreach my $column (@$columns) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
267
            $source .= "$column, ";
packaging one directory
yuki-kimoto authored on 2009-11-16
268
        }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
269
        $source =~ s/, $/ /;
packaging one directory
yuki-kimoto authored on 2009-11-16
270
    }
271
    else {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
272
        $source .= '* ';
packaging one directory
yuki-kimoto authored on 2009-11-16
273
    }
274
    
added commit method
yuki-kimoto authored on 2010-05-27
275
    # Table
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
276
    $source .= 'from ';
packaging one directory
yuki-kimoto authored on 2009-11-16
277
    foreach my $table (@$tables) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
278
        $source .= "$table, ";
packaging one directory
yuki-kimoto authored on 2009-11-16
279
    }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
280
    $source =~ s/, $/ /;
packaging one directory
yuki-kimoto authored on 2009-11-16
281
    
added commit method
yuki-kimoto authored on 2010-05-27
282
    # Where clause
update document
yuki-kimoto authored on 2010-08-07
283
    my $param;
284
    if (ref $where eq 'HASH') {
285
        $param = $where;
286
        $source .= 'where (';
287
        foreach my $where_key (keys %$where) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
288
            $source .= "{= $where_key} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
289
        }
update document
yuki-kimoto authored on 2010-08-07
290
        $source =~ s/ and $//;
291
        $source .= ') ';
292
    }
293
    elsif (ref $where eq 'ARRAY') {
294
        my$where_str = $where->[0] || '';
295
        $param = $where->[1];
296
        
297
        $source .= "where ($where_str) ";
packaging one directory
yuki-kimoto authored on 2009-11-16
298
    }
299
    
added commit method
yuki-kimoto authored on 2010-05-27
300
    # Relation
301
    if ($relation) {
update document
yuki-kimoto authored on 2010-08-07
302
        $source .= $where ? "and " : "where ";
added commit method
yuki-kimoto authored on 2010-05-27
303
        foreach my $rkey (keys %$relation) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
304
            $source .= "$rkey = " . $relation->{$rkey} . " and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
305
        }
306
    }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
307
    $source =~ s/ and $//;
added commit method
yuki-kimoto authored on 2010-05-27
308
    
309
    # Append some statement
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
310
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
311
    
312
    # Execute query
update document
yuki-kimoto authored on 2010-08-07
313
    my $result = $self->execute($source, param  => $param, 
314
                                         filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
315
    
316
    return $result;
317
}
318

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
319
sub create_query {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
320
    my ($self, $source) = @_;
removed register_format()
yuki-kimoto authored on 2010-05-26
321
    
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
322
    # Cache
added cache_method attribute
yuki-kimoto authored on 2010-06-25
323
    my $cache = $self->cache;
version 0.0901
yuki-kimoto authored on 2009-12-17
324
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
325
    # Create query
326
    my $query;
added cache_method attribute
yuki-kimoto authored on 2010-06-25
327
    if ($cache) {
328
        
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
329
        # Get query
330
        my $q = $self->cache_method->($self, $source);
added cache_method attribute
yuki-kimoto authored on 2010-06-25
331
        
332
        # Create query
333
        $query = DBIx::Custom::Query->new($q) if $q;
removed reconnect method
yuki-kimoto authored on 2010-05-28
334
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
335
    
336
    unless ($query) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
337

            
338
        # Create SQL object
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
339
        my $builder = $self->query_builder;
added cache_method attribute
yuki-kimoto authored on 2010-06-25
340
        
341
        # Create query
add tests
yuki-kimoto authored on 2010-08-10
342
        {
343
            local $Carp::CarpLevel += 1;
344
            $query = $builder->build_query($source);
345
        }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
346
        # Cache query
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
347
        $self->cache_method->($self, $source,
added cache_method attribute
yuki-kimoto authored on 2010-06-25
348
                             {sql     => $query->sql, 
349
                              columns => $query->columns})
350
          if $cache;
removed reconnect method
yuki-kimoto authored on 2010-05-28
351
    }
version 0.0901
yuki-kimoto authored on 2009-12-17
352
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
353
    # Prepare statement handle
add tests
yuki-kimoto authored on 2010-08-10
354
    my $sth;
355
    eval { $sth = $self->dbh->prepare($query->{sql})};
356
    if ($@) {
357
        my $error = $@;
358
        $error =~ s/\s+at\s+.*?\s+line\s+\d+.*$//s;
359
        croak qq{$error. SQL: "$query->{sql}"};
360
    }
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
361
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
362
    # Set statement handle
363
    $query->sth($sth);
364
    
365
    return $query;
366
}
367

            
368
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/;
369

            
370
sub execute{
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
371
    my ($self, $query, %args)  = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
372
    
373
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
374
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
375
        croak qq{"$name" is invalid argument}
removed reconnect method
yuki-kimoto authored on 2010-05-28
376
          unless $VALID_EXECUTE_ARGS{$name};
377
    }
378
    
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
379
    my $params = $args{param} || {};
removed reconnect method
yuki-kimoto authored on 2010-05-28
380
    
add tests
yuki-kimoto authored on 2010-08-10
381
    # First argument is the soruce of SQL
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
382
    $query = $self->create_query($query)
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
383
      unless ref $query;
removed reconnect method
yuki-kimoto authored on 2010-05-28
384
    
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
385
    my $filter = $args{filter} || $query->filter || {};
removed reconnect method
yuki-kimoto authored on 2010-05-28
386
    
387
    # Create bind value
388
    my $bind_values = $self->_build_bind_values($query, $params, $filter);
389
    
390
    # Execute
391
    my $sth      = $query->sth;
add tests
yuki-kimoto authored on 2010-08-10
392
    my $affected;
393
    eval {$affected = $sth->execute(@$bind_values)};
394
    if ($@) {
395
        my $error = $@;
396
        $error =~ s/\s+at\s+.*?\s+line\s+\d+.*$//s;
397
        croak $error;
398
    }
removed reconnect method
yuki-kimoto authored on 2010-05-28
399
    
400
    # Return resultset if select statement is executed
401
    if ($sth->{NUM_OF_FIELDS}) {
402
        
403
        # Create result
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
404
        my $result = $self->result_class->new(
405
            sth            => $sth,
406
            default_filter => $self->default_fetch_filter,
added check_filter attribute
yuki-kimoto authored on 2010-08-08
407
            filters        => $self->filters,
408
            filter_check   => $self->filter_check
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
409
        );
410

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
411
        return $result;
412
    }
413
    return $affected;
414
}
415

            
416
sub _build_bind_values {
417
    my ($self, $query, $params, $filter) = @_;
418
    
419
    # binding values
420
    my @bind_values;
add tests
yuki-kimoto authored on 2010-08-08
421

            
422
    # Filter
423
    $filter ||= {};
424
    
425
    # Parameter
426
    $params ||= {};
427
    
428
    # Check filter
429
    $self->_check_filter($self->filters, $filter,
430
                         $self->default_bind_filter, $params)
431
      if $self->filter_check;
removed reconnect method
yuki-kimoto authored on 2010-05-28
432
    
433
    # Build bind values
434
    my $count = {};
435
    foreach my $column (@{$query->columns}) {
436
        
437
        # Value
438
        my $value = ref $params->{$column} eq 'ARRAY'
439
                  ? $params->{$column}->[$count->{$column} || 0]
440
                  : $params->{$column};
441
        
add tests
yuki-kimoto authored on 2010-08-10
442
        # Filtering
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
443
        my $fname = $filter->{$column} || $self->default_bind_filter || '';
add tests
yuki-kimoto authored on 2010-08-10
444
        my $filter_func = $fname ? $self->filters->{$fname} : undef;
removed reconnect method
yuki-kimoto authored on 2010-05-28
445
        push @bind_values, $filter_func
446
                         ? $filter_func->($value)
447
                         : $value;
448
        
449
        # Count up 
450
        $count->{$column}++;
451
    }
452
    
453
    return \@bind_values;
454
}
455

            
add tests
yuki-kimoto authored on 2010-08-08
456
sub _check_filter {
457
    my ($self, $filters, $filter, $default_filter, $params) = @_;
458
    
459
    # Filter name not exists
460
    foreach my $fname (values %$filter) {
461
        croak qq{Bind filter "$fname" is not registered}
462
          unless exists $filters->{$fname};
463
    }
464
    
465
    # Default filter name not exists
466
    croak qq{Default bind filter "$default_filter" is not registered}
467
      if $default_filter && ! exists $filters->{$default_filter};
468
    
469
    # Column name not exists
470
    foreach my $column (keys %$filter) {
471
        
472
        croak qq{Column name "$column" in bind filter is not found in paramters}
473
          unless exists $params->{$column};
474
    }
475
}
476

            
added register_method() meth...
yuki-kimoto authored on 2010-08-10
477
__PACKAGE__->attr('methods' => sub { {} });
478

            
479
sub register_method {
480
    my $self = shift;
481
    
482
    # Register method
483
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
484
    $self->methods({%{$self->methods}, %$methods});
485
    
486
    return $self;
487
}
488

            
489
our $AUTOLOAD;
490
sub AUTOLOAD {
491
    my $self = shift;
492
    my $method = $AUTOLOAD;
493
    $method =~ s/.*:://;
494
   
495
   return if $method eq 'DESTROY';
496
   
497
   croak qq{Method "$method" is not registered"}
498
     unless $self->methods->{$method};
499
   
500
   return $self->methods->{$method}->($self, @_);
501
}
502

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
503
=head1 NAME
504

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
505
DBIx::Custom - DBI interface, having hash parameter binding and filtering system
removed reconnect method
yuki-kimoto authored on 2010-05-28
506

            
507
=cut
508

            
added register_method() meth...
yuki-kimoto authored on 2010-08-10
509
our $VERSION = '0.1613';
removed reconnect method
yuki-kimoto authored on 2010-05-28
510

            
511
=head1 STABILITY
512

            
updated document
yuki-kimoto authored on 2010-08-08
513
B<This module is not stable>.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
514
Method name and implementations will be changed for a while.
removed reconnect method
yuki-kimoto authored on 2010-05-28
515

            
516
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
517

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
518
Connect to the database.
519
    
520
    use DBIx::Custom;
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
521
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname",
removed reconnect method
yuki-kimoto authored on 2010-05-28
522
                                    user => 'ken', password => '!LFKD%$&');
cleanup
yuki-kimoto authored on 2010-08-05
523

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
524
Insert, update, and delete
cleanup
yuki-kimoto authored on 2010-08-05
525

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
526
    # Insert 
527
    $dbi->insert(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
528
                 param  => {title => 'Perl', author => 'Ken'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
529
                 filter => {title => 'encode_utf8'});
530
    
531
    # Update 
532
    $dbi->update(table  => 'books', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
533
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
534
                 where  => {id => 5},
535
                 filter => {title => 'encode_utf8'});
536
    
537
    # Update all
538
    $dbi->update_all(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
539
                     param  => {title => 'Perl'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
540
                     filter => {title => 'encode_utf8'});
541
    
542
    # Delete
543
    $dbi->delete(table  => 'books',
544
                 where  => {author => 'Ken'},
545
                 filter => {title => 'encode_utf8'});
546
    
547
    # Delete all
548
    $dbi->delete_all(table => 'books');
cleanup
yuki-kimoto authored on 2010-08-05
549

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
550
Select
cleanup
yuki-kimoto authored on 2010-08-05
551

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
552
    # Select
553
    my $result = $dbi->select(table => 'books');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
554
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
555
    # Select, more complex
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
556
    my $result = $dbi->select(
update document
yuki-kimoto authored on 2010-05-27
557
        table  => 'books',
558
        column => [qw/author title/],
559
        where  => {author => 'Ken'},
updated document
yuki-kimoto authored on 2010-08-08
560
        append => 'order by id limit 5',
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
561
        filter => {title => 'encode_utf8'}
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
562
    );
added commit method
yuki-kimoto authored on 2010-05-27
563
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
564
    # Select, join table
added commit method
yuki-kimoto authored on 2010-05-27
565
    my $result = $dbi->select(
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
566
        table    => ['books', 'rental'],
567
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
568
        relation => {'books.id' => 'rental.book_id'}
569
    );
updated document
yuki-kimoto authored on 2010-08-08
570
    
571
    # Select, more flexible where
572
    my $result = $dbi->select(
573
        table  => 'books',
574
        where  => ['{= author} and {like title}', 
575
                   {author => 'Ken', title => '%Perl%'}]
576
    );
cleanup
yuki-kimoto authored on 2010-08-05
577

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
578
Execute SQL
cleanup
yuki-kimoto authored on 2010-08-05
579

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
580
    # Execute SQL
removed register_format()
yuki-kimoto authored on 2010-05-26
581
    $dbi->execute("select title from books");
582
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
583
    # Execute SQL with hash binding and filtering
updated document
yuki-kimoto authored on 2010-08-08
584
    $dbi->execute("select id from books where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
585
                  param  => {author => 'ken', title => '%Perl%'},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
586
                  filter => {title => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
587

            
588
    # Create query and execute it
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
589
    my $query = $dbi->create_query(
updated document
yuki-kimoto authored on 2010-08-08
590
        "select id from books where {= author} and {like title}"
removed reconnect method
yuki-kimoto authored on 2010-05-28
591
    );
updated document
yuki-kimoto authored on 2010-08-08
592
    $dbi->execute($query, param => {author => 'Ken', title => '%Perl%'})
cleanup
yuki-kimoto authored on 2010-08-05
593

            
updated document
yuki-kimoto authored on 2010-08-08
594
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
595

            
removed register_format()
yuki-kimoto authored on 2010-05-26
596
    # Default filter
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
597
    $dbi->default_bind_filter('encode_utf8');
removed register_format()
yuki-kimoto authored on 2010-05-26
598
    $dbi->default_fetch_filter('decode_utf8');
cleanup
yuki-kimoto authored on 2010-08-05
599

            
600
    # Get DBI object
601
    my $dbh = $dbi->dbh;
602

            
603
Fetch row.
604

            
removed register_format()
yuki-kimoto authored on 2010-05-26
605
    # Fetch
606
    while (my $row = $result->fetch) {
607
        # ...
608
    }
609
    
610
    # Fetch hash
611
    while (my $row = $result->fetch_hash) {
612
        
613
    }
614
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
615
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
616

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
617
=head2 1. Features
removed reconnect method
yuki-kimoto authored on 2010-05-28
618

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
619
L<DBIx::Custom> is one of L<DBI> interface modules,
620
such as L<DBIx::Class>, L<DBIx::Simple>.
removed reconnect method
yuki-kimoto authored on 2010-05-28
621

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
622
This module is not O/R mapper. O/R mapper is useful,
623
but you must learn many syntax of the O/R mapper,
updated document
yuki-kimoto authored on 2010-08-08
624
which is almost another language.
625
Created SQL statement is offten not effcient and damage SQL performance.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
626
so you have to execute raw SQL in the end.
removed reconnect method
yuki-kimoto authored on 2010-05-28
627

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
628
L<DBIx::Custom> is middle area between L<DBI> and O/R mapper.
updated document
yuki-kimoto authored on 2010-08-08
629
L<DBIx::Custom> provide flexible hash parameter binding and filtering system,
630
and suger methods, such as C<select()>, C<update()>, C<delete()>, C<select()>
631
to execute SQL easily.
removed reconnect method
yuki-kimoto authored on 2010-05-28
632

            
updated document
yuki-kimoto authored on 2010-08-08
633
L<DBIx::Custom> respects SQL. SQL is very complex and not beautiful,
634
but de-facto standard,
635
so all people learing database know it.
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
636
If you already know SQL,
637
you learn a little thing to use L<DBIx::Custom>.
removed reconnect method
yuki-kimoto authored on 2010-05-28
638

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
639
=head2 2. Connect to the database
removed reconnect method
yuki-kimoto authored on 2010-05-28
640

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
641
C<connect()> method create a new L<DBIx::Custom>
642
object and connect to the database.
removed reconnect method
yuki-kimoto authored on 2010-05-28
643

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
644
    use DBIx::Custom;
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
645
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname",
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
646
                                    user => 'ken', password => '!LFKD%$&');
647

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
648
If database is SQLite, use L<DBIx::Custom::SQLite> instead.
649
you connect database easily.
update document
yuki-kimoto authored on 2010-08-07
650

            
651
    use DBIx::Custom::SQLite;
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
652
    my $dbi = DBIx::Custom::SQLite->connect(database => 'dbname');
update document
yuki-kimoto authored on 2010-08-07
653
    
cleanup
yuki-kimoto authored on 2010-08-09
654
If database is  MySQL, use L<DBIx::Custom::MySQL>.
update document
yuki-kimoto authored on 2010-08-07
655

            
656
    use DBIx::Custom::MySQL;
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
657
    my $dbi = DBIx::Custom::MySQL->connect(
658
        database => 'dbname',
659
        user     => 'ken',
660
        password => '!LFKD%$&'
661
    );
update document
yuki-kimoto authored on 2010-08-07
662

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
663
=head2 3. Suger methods
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
664

            
665
L<DBIx::Custom> has suger methods, such as C<insert()>, C<update()>,
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
666
C<delete()> or C<select()>. If you want to do small works,
updated document
yuki-kimoto authored on 2010-08-08
667
You don't have to create SQL statements.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
668

            
update document
yuki-kimoto authored on 2010-08-07
669
=head3 insert()
670

            
updated document
yuki-kimoto authored on 2010-08-08
671
Execute insert statement.
672

            
update document
yuki-kimoto authored on 2010-08-07
673
    $dbi->insert(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
674
                 param  => {title => 'Perl', author => 'Ken'});
update document
yuki-kimoto authored on 2010-08-07
675

            
676
The following SQL is executed.
677

            
updated document
yuki-kimoto authored on 2010-08-08
678
    insert into (title, author) values (?, ?);
update document
yuki-kimoto authored on 2010-08-07
679

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
680
The values of C<title> and C<author> is embedded into the placeholders.
update document
yuki-kimoto authored on 2010-08-07
681

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
682
C<append> and C<filter> argument can be specified.
683
See also "METHODS" section.
update document
yuki-kimoto authored on 2010-08-07
684

            
685
=head3 update()
686

            
updated document
yuki-kimoto authored on 2010-08-08
687
Execute update statement.
688

            
update document
yuki-kimoto authored on 2010-08-07
689
    $dbi->update(table  => 'books', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
690
                 param  => {title => 'Perl', author => 'Ken'}, 
update document
yuki-kimoto authored on 2010-08-07
691
                 where  => {id => 5});
692

            
693
The following SQL is executed.
694

            
695
    update books set title = ?, author = ?;
696

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
697
The values of C<title> and C<author> is embedded into the placeholders.
update document
yuki-kimoto authored on 2010-08-07
698

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
699
C<append> and C<filter> argument can be specified.
700
See also "METHOD" section.
update document
yuki-kimoto authored on 2010-08-07
701

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
702
If you want to update all rows, use C<update_all()> method.
update document
yuki-kimoto authored on 2010-08-07
703

            
704
=head3 delete()
705

            
updated document
yuki-kimoto authored on 2010-08-08
706
Execute delete statement.
707

            
update document
yuki-kimoto authored on 2010-08-07
708
    $dbi->delete(table  => 'books',
709
                 where  => {author => 'Ken'});
710

            
711
The following SQL is executed.
712

            
713
    delete from books where id = ?;
714

            
updated document
yuki-kimoto authored on 2010-08-08
715
The value of C<id> is embedded into the placehodler.
update document
yuki-kimoto authored on 2010-08-07
716

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
717
C<append> and C<filter> argument can be specified.
718
see also "METHODS" section.
update document
yuki-kimoto authored on 2010-08-07
719

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
720
If you want to delete all rows, use C<delete_all()> method.
update document
yuki-kimoto authored on 2010-08-07
721

            
722
=head3 select()
723

            
updated document
yuki-kimoto authored on 2010-08-08
724
Execute select statement, only C<table> argument specified :
update document
yuki-kimoto authored on 2010-08-07
725

            
726
    my $result = $dbi->select(table => 'books');
727

            
728
The following SQL is executed.
729

            
730
    select * from books;
731

            
732
the result of C<select()> method is L<DBIx::Custom::Result> object.
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
733
You can fetch a row by C<fetch()> method.
update document
yuki-kimoto authored on 2010-08-07
734

            
735
    while (my $row = $result->fetch) {
736
        my $title  = $row->[0];
737
        my $author = $row->[1];
738
    }
739

            
740
L<DBIx::Custom::Result> has various methods to fetch row.
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
741
See "4. Fetch row".
update document
yuki-kimoto authored on 2010-08-07
742

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
743
C<column> and C<where> arguments specified.
update document
yuki-kimoto authored on 2010-08-07
744

            
745
    my $result = $dbi->select(
746
        table  => 'books',
747
        column => [qw/author title/],
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
748
        where  => {author => 'Ken'}
749
    );
update document
yuki-kimoto authored on 2010-08-07
750

            
751
The following SQL is executed.
752

            
753
    select author, title from books where author = ?;
754

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
755
the value of C<author> is embdded into the placeholder.
update document
yuki-kimoto authored on 2010-08-07
756

            
updated document
yuki-kimoto authored on 2010-08-08
757
If you want to join tables, specify C<relation> argument. 
update document
yuki-kimoto authored on 2010-08-07
758

            
759
    my $result = $dbi->select(
760
        table    => ['books', 'rental'],
761
        column   => ['books.name as book_name']
762
        relation => {'books.id' => 'rental.book_id'}
763
    );
764

            
765
The following SQL is executed.
766

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
767
    select books.name as book_name from books, rental
update document
yuki-kimoto authored on 2010-08-07
768
    where books.id = rental.book_id;
769

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
770
If you want to add some string to the end of SQL statement,
771
use C<append> argument.
update document
yuki-kimoto authored on 2010-08-07
772

            
773
    my $result = $dbi->select(
774
        table  => 'books',
775
        where  => {author => 'Ken'},
776
        append => 'order by price limit 5',
777
    );
778

            
779
The following SQL is executed.
780

            
781
    select * books where author = ? order by price limit 5;
782

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
783
C<filter> argument can be specified.
784
see also "METHODS" section.
update document
yuki-kimoto authored on 2010-08-07
785

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
786
=head2 4. Fetch row
update document
yuki-kimoto authored on 2010-08-07
787

            
updated document
yuki-kimoto authored on 2010-08-08
788
C<select()> method return L<DBIx::Custom::Result> object.
789
You can fetch row by various methods.
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
790
Note that in this section, array means array reference,
791
and hash meanse hash reference.
update document
yuki-kimoto authored on 2010-08-07
792

            
793
Fetch row into array.
794
    
795
    while (my $row = $result->fetch) {
796
        my $author = $row->[0];
797
        my $title  = $row->[1];
798
        
799
    }
800

            
801
Fetch only a first row into array.
802

            
803
    my $row = $result->fetch_first;
804

            
805
Fetch multiple rows into array of array.
806

            
807
    while (my $rows = $result->fetch_multi(5)) {
808
        my $first_author  = $rows->[0][0];
809
        my $first_title   = $rows->[0][1];
810
        my $second_author = $rows->[1][0];
811
        my $second_value  = $rows->[1][1];
812
    
813
    }
814
    
815
Fetch all rows into array of array.
816

            
817
    my $rows = $result->fetch_all;
818

            
819
Fetch row into hash.
820

            
821
    # Fetch a row into hash
822
    while (my $row = $result->fetch_hash) {
823
        my $title  = $row->{title};
824
        my $author = $row->{author};
825
        
826
    }
827

            
828
Fetch only a first row into hash
829

            
830
    my $row = $result->fetch_hash_first;
831
    
832
Fetch multiple rows into array of hash
833

            
834
    while (my $rows = $result->fetch_hash_multi(5)) {
835
        my $first_title   = $rows->[0]{title};
836
        my $first_author  = $rows->[0]{author};
837
        my $second_title  = $rows->[1]{title};
838
        my $second_author = $rows->[1]{author};
839
    
840
    }
841
    
842
Fetch all rows into array of hash
843

            
844
    my $rows = $result->fetch_hash_all;
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
845

            
cleanup
yuki-kimoto authored on 2010-08-09
846
If you want to access statement handle of L<DBI>, use C<sth> attribute.
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
847

            
update document
yuki-kimoto authored on 2010-08-07
848
    my $sth = $result->sth;
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
849

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
850
=head2 5. Hash parameter binding
removed reconnect method
yuki-kimoto authored on 2010-05-28
851

            
update document
yuki-kimoto authored on 2010-08-07
852
L<DBIx::Custom> provides hash parameter binding.
853

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
854
At frist, I show normal parameter binding.
update document
yuki-kimoto authored on 2010-08-07
855

            
856
    use DBI;
857
    my $dbh = DBI->connect(...);
858
    my $sth = $dbh->prepare(
859
        "select * from books where author = ? and title like ?;"
860
    );
861
    $sth->execute('Ken', '%Perl%');
862

            
updated document
yuki-kimoto authored on 2010-08-08
863
This is very good way because database system can enable SQL caching,
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
864
and parameter is quoted automatically. this is secure.
update document
yuki-kimoto authored on 2010-08-07
865

            
updated document
yuki-kimoto authored on 2010-08-08
866
L<DBIx::Custom> hash parameter binding system improve
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
867
normal parameter binding to use hash parameter.
update document
yuki-kimoto authored on 2010-08-07
868

            
869
    my $result = $dbi->execute(
870
        "select * from books where {= author} and {like title};"
871
        param => {author => 'Ken', title => '%Perl%'}
872
    );
873

            
874
This is same as the normal way, execpt that the parameter is hash.
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
875
{= author} and {like title} is called C<tag>.
876
tag is expand to placeholder string internally.
update document
yuki-kimoto authored on 2010-08-07
877

            
878
    select * from books where {= author} and {like title}
879
      -> select * from books where author = ? and title like ?;
880

            
updated document
yuki-kimoto authored on 2010-08-08
881
The following tags is available.
882

            
883
    [TAG]                       [REPLACED]
884
    {? NAME}               ->   ?
885
    {= NAME}               ->   NAME = ?
886
    {<> NAME}              ->   NAME <> ?
887
    
888
    {< NAME}               ->   NAME < ?
889
    {> NAME}               ->   NAME > ?
890
    {>= NAME}              ->   NAME >= ?
891
    {<= NAME}              ->   NAME <= ?
892
    
893
    {like NAME}            ->   NAME like ?
894
    {in NAME COUNT}        ->   NAME in [?, ?, ..]
895
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
896
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
897
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
updated document
yuki-kimoto authored on 2010-08-08
898

            
899
See also L<DBIx::Custom::QueryBuilder>.
900

            
901
Default start tag is '{'. end tag is '}'.
902
You can change this tag.
903

            
904
    $dbi->query_builder->start_tag('|');
905
    $dbi->query_builder->end_tag('|');
update document
yuki-kimoto authored on 2010-08-07
906

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
907
=head2 6. Filtering
update document
yuki-kimoto authored on 2010-08-07
908

            
909
Usually, Perl string is kept as internal string.
910
If you want to save the string to database, You must encode the string.
911
Filtering system help you to convert a data to another data
912
when you save to the data and get the data form database.
913

            
updated document
yuki-kimoto authored on 2010-08-08
914
If you want to register filter, use C<register_filter()> method.
update document
yuki-kimoto authored on 2010-08-07
915

            
916
    $dbi->register_filter(
917
        to_upper_case => sub {
918
            my $value = shift;
919
            return uc $value;
920
        }
921
    );
922

            
923
C<encode_utf8> and C<decode_utf8> filter is registerd by default.
924

            
925
You can specify these filters to C<filter> argument of C<execute()> method.
926

            
927
    my $result = $dbi->execute(
928
        "select * from books where {= author} and {like title};"
updated document
yuki-kimoto authored on 2010-08-08
929
        param  => {author => 'Ken', title => '%Perl%'},
update document
yuki-kimoto authored on 2010-08-07
930
        filter => {author => 'to_upper_case, title => 'encode_utf8'}
931
    );
932

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
933
C<filter> argument can be specified to suger methods, such as
cleanup
yuki-kimoto authored on 2010-08-09
934
C<insert()>, C<update()>, C<update_all()>,
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
935
C<delete()>, C<delete_all()>, C<select()>.
update document
yuki-kimoto authored on 2010-08-07
936

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
937
    # insert(), having filter argument
update document
yuki-kimoto authored on 2010-08-07
938
    $dbi->insert(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
939
                 param  => {title => 'Perl', author => 'Ken'},
update document
yuki-kimoto authored on 2010-08-07
940
                 filter => {title => 'encode_utf8'});
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
941
    
942
    # select(), having filter argument
update document
yuki-kimoto authored on 2010-08-07
943
    my $result = $dbi->select(
944
        table  => 'books',
945
        column => [qw/author title/],
946
        where  => {author => 'Ken'},
947
        append => 'order by id limit 1',
948
        filter => {title => 'encode_utf8'}
949
    );
950

            
updated document
yuki-kimoto authored on 2010-08-08
951
Filter works each parmeter, but you prepare default filter for all parameters.
update document
yuki-kimoto authored on 2010-08-07
952

            
953
    $dbi->default_bind_filter('encode_utf8');
954

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
955
C<filter()> argument overwrites this default filter.
update document
yuki-kimoto authored on 2010-08-07
956
    
957
    $dbi->default_bind_filter('encode_utf8');
958
    $dbi->insert(
959
        table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
960
        param  => {title => 'Perl', author => 'Ken', price => 1000},
update document
yuki-kimoto authored on 2010-08-07
961
        filter => {author => 'to_upper_case', price => undef}
962
    );
963

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
964
This is same as the following example.
update document
yuki-kimoto authored on 2010-08-07
965

            
966
    $dbi->insert(
967
        table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
968
        param  => {title => 'Perl', author => 'Ken', price => 1000},
update document
yuki-kimoto authored on 2010-08-07
969
        filter => {title => 'encode_uft8' author => 'to_upper_case'}
970
    );
971

            
updated document
yuki-kimoto authored on 2010-08-08
972
You can also specify filter when the row is fetched. This is reverse of bind filter.
update document
yuki-kimoto authored on 2010-08-07
973

            
974
    my $result = $dbi->select(table => 'books');
975
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
976

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
977
Filter works each column value, but you prepare a default filter
978
for all clumn value.
update document
yuki-kimoto authored on 2010-08-07
979

            
980
    $dbi->default_fetch_filter('decode_utf8');
981

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
982
C<filter()> method of L<DBIx::Custom::Result>
983
overwrites this default filter.
update document
yuki-kimoto authored on 2010-08-07
984

            
985
    $dbi->default_fetch_filter('decode_utf8');
986
    my $result = $dbi->select(
987
        table => 'books',
988
        columns => ['title', 'author', 'price']
989
    );
990
    $result->filter({author => 'to_upper_case', price => undef});
991

            
992
This is same as the following one.
993

            
994
    my $result = $dbi->select(
995
        table => 'books',
996
        columns => ['title', 'author', 'price']
997
    );
998
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
999

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1000
Note that in fetch filter, column names must be lower case
1001
even if the column name conatains upper case charactors.
1002
This is requirment not to depend database systems.
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1003

            
cleanup
yuki-kimoto authored on 2010-08-09
1004
=head2 7. Get high performance
updated document
yuki-kimoto authored on 2010-08-08
1005

            
updated document
yuki-kimoto authored on 2010-08-08
1006
=head3 Disable filter checking
1007

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1008
Filter checking is executed by default.
1009
This is done to check right filter name is specified,
1010
but sometimes damage performance.
updated document
yuki-kimoto authored on 2010-08-08
1011

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1012
If you disable this filter checking,
1013
Set C<filter_check> attribute to 0.
updated document
yuki-kimoto authored on 2010-08-08
1014

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1015
    $dbi->filter_check(0);
updated document
yuki-kimoto authored on 2010-08-08
1016

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1017
=head3 Use execute() method instead suger methods
1018

            
1019
If you execute insert statement by C<insert()> method,
1020
you sometimes can't get required performance.
updated document
yuki-kimoto authored on 2010-08-08
1021

            
1022
C<insert()> method is a little slow because SQL statement and statement handle
1023
is created every time.
1024

            
1025
In that case, you can prepare a query by C<create_query()> method.
1026
    
1027
    my $query = $dbi->create_query(
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1028
        "insert into books {insert_param title author};"
updated document
yuki-kimoto authored on 2010-08-08
1029
    );
cleanup
yuki-kimoto authored on 2010-08-09
1030

            
1031
Return value of C<create_query()> is L<DBIx::Custom::Query> object.
1032
This keep the information of SQL and column names.
1033

            
1034
    {
1035
        sql     => 'insert into books (title, author) values (?, ?);',
1036
        columns => ['title', 'author']
1037
    }
1038

            
1039
Execute query repeatedly.
updated document
yuki-kimoto authored on 2010-08-08
1040
    
1041
    my $inputs = [
1042
        {title => 'Perl',      author => 'Ken'},
1043
        {title => 'Good days', author => 'Mike'}
1044
    ];
1045
    
1046
    foreach my $input (@$inputs) {
1047
        $dbi->execute($query, $input);
1048
    }
1049

            
cleanup
yuki-kimoto authored on 2010-08-09
1050
This is faster than C<insert()> method.
updated document
yuki-kimoto authored on 2010-08-08
1051

            
cleanup
yuki-kimoto authored on 2010-08-09
1052
=head3 caching
updated document
yuki-kimoto authored on 2010-08-08
1053

            
cleanup
yuki-kimoto authored on 2010-08-09
1054
C<execute()> method caches the parsed result of the source of SQL.
updated document
yuki-kimoto authored on 2010-08-08
1055
Default to 1
1056

            
1057
    $dbi->cache(1);
1058

            
1059
Caching is on memory, but you can change this by C<cache_method()>.
1060
First argument is L<DBIx::Custom> object.
cleanup
yuki-kimoto authored on 2010-08-09
1061
Second argument is a source of SQL,
1062
such as "select * from books where {= title} and {= author};";
updated document
yuki-kimoto authored on 2010-08-08
1063
Third argument is parsed result, such as
1064
{sql => "select * from books where title = ? and author = ?",
1065
 columns => ['title', 'author']}, this is hash reference.
cleanup
yuki-kimoto authored on 2010-08-09
1066
If arguments is more than two, this method is called to set cache.
1067
If not, this method is called to get cache.
updated document
yuki-kimoto authored on 2010-08-08
1068

            
cleanup
yuki-kimoto authored on 2010-08-09
1069
    $dbi->cache_method(sub {
updated document
yuki-kimoto authored on 2010-08-08
1070
        sub {
1071
            my $self = shift;
1072
            
1073
            $self->{_cached} ||= {};
1074
            
1075
            # Set cache
1076
            if (@_ > 1) {
1077
                $self->{_cached}{$_[0]} = $_[1] 
1078
            }
1079
            
1080
            # Get cache
1081
            else {
1082
                return $self->{_cached}{$_[0]}
1083
            }
1084
        }
1085
    });
1086

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1087
=head2 8. More features
updated document
yuki-kimoto authored on 2010-08-08
1088

            
1089
=head3 Get DBI object
1090

            
1091
You can get L<DBI> object and call any method of L<DBI>.
1092

            
1093
    $dbi->dbh->begin_work;
1094
    $dbi->dbh->commit;
1095
    $dbi->dbh->rollback;
1096

            
1097
=head3 Change Result class
1098

            
1099
You can change Result class if you need.
1100

            
1101
    package Your::Result;
1102
    use base 'DBIx::Custom::Result';
1103
    
1104
    sub some_method { ... }
1105

            
1106
    1;
1107
    
1108
    package main;
1109
    
1110
    use Your::Result;
1111
    
1112
    my $dbi = DBIx::Custom->connect(...);
1113
    $dbi->result_class('Your::Result');
1114

            
1115
=head3 Custamize SQL builder object
1116

            
1117
You can custamize SQL builder object
1118

            
1119
    my $dbi = DBIx::Custom->connect(...);
1120
    $dbi->query_builder->start_tag('|');
1121
    $dbi->query_builder->end_tag('|');
1122
    $dbi->query_builder->register_tag_processor(
1123
        name => sub {
1124
           ...
1125
        }
1126
    );
1127

            
update document
yuki-kimoto authored on 2010-01-30
1128
=head1 ATTRIBUTES
packaging one directory
yuki-kimoto authored on 2009-11-16
1129

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1130
=head2 C<user>
packaging one directory
yuki-kimoto authored on 2009-11-16
1131

            
cleanup
yuki-kimoto authored on 2010-08-03
1132
    my $user = $dbi->user;
1133
    $dbi     = $dbi->user('Ken');
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1134

            
cleanup
yuki-kimoto authored on 2010-08-05
1135
User name.
1136
C<connect()> method use this value to connect the database.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1137
    
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1138
=head2 C<password>
packaging one directory
yuki-kimoto authored on 2009-11-16
1139

            
cleanup
yuki-kimoto authored on 2010-08-03
1140
    my $password = $dbi->password;
1141
    $dbi         = $dbi->password('lkj&le`@s');
packaging one directory
yuki-kimoto authored on 2009-11-16
1142

            
cleanup
yuki-kimoto authored on 2010-08-05
1143
Password.
1144
C<connect()> method use this value to connect the database.
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1145

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1146
=head2 C<data_source>
packaging one directory
yuki-kimoto authored on 2009-11-16
1147

            
cleanup
yuki-kimoto authored on 2010-08-03
1148
    my $data_source = $dbi->data_source;
cleanup
yuki-kimoto authored on 2010-08-05
1149
    $dbi            = $dbi->data_source("DBI:mysql:database=dbname");
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1150

            
cleanup
yuki-kimoto authored on 2010-08-05
1151
Data source.
1152
C<connect()> method use this value to connect the database.
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1153

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1154
=head2 C<dbh>
packaging one directory
yuki-kimoto authored on 2009-11-16
1155

            
cleanup
yuki-kimoto authored on 2010-08-03
1156
    my $dbh = $dbi->dbh;
1157
    $dbi    = $dbi->dbh($dbh);
packaging one directory
yuki-kimoto authored on 2009-11-16
1158

            
cleanup
yuki-kimoto authored on 2010-08-05
1159
L<DBI> object. You can call all methods of L<DBI>.
packaging one directory
yuki-kimoto authored on 2009-11-16
1160

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1161
=head2 C<filters>
packaging one directory
yuki-kimoto authored on 2009-11-16
1162

            
cleanup
yuki-kimoto authored on 2010-08-03
1163
    my $filters = $dbi->filters;
cleanup
yuki-kimoto authored on 2010-08-03
1164
    $dbi        = $dbi->filters(\%filters);
version 0.0901
yuki-kimoto authored on 2009-12-17
1165

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1166
Filter functions.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1167
"encode_utf8" and "decode_utf8" is registered by default.
version 0.0901
yuki-kimoto authored on 2009-12-17
1168

            
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
1169
=head2 C<default_bind_filter>
packaging one directory
yuki-kimoto authored on 2009-11-16
1170

            
cleanup
yuki-kimoto authored on 2010-08-03
1171
    my $default_bind_filter = $dbi->default_bind_filter
1172
    $dbi                    = $dbi->default_bind_filter('encode_utf8');
packaging one directory
yuki-kimoto authored on 2009-11-16
1173

            
cleanup
yuki-kimoto authored on 2010-08-05
1174
Default filter when parameter binding is executed.
packaging one directory
yuki-kimoto authored on 2009-11-16
1175

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1176
=head2 C<default_fetch_filter>
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1177

            
cleanup
yuki-kimoto authored on 2010-08-03
1178
    my $default_fetch_filter = $dbi->default_fetch_filter;
1179
    $dbi                     = $dbi->default_fetch_filter('decode_utf8');
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1180

            
cleanup
yuki-kimoto authored on 2010-08-05
1181
Default filter when row is fetched.
packaging one directory
yuki-kimoto authored on 2009-11-16
1182

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1183
=head2 C<result_class>
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1184

            
cleanup
yuki-kimoto authored on 2010-08-03
1185
    my $result_class = $dbi->result_class;
1186
    $dbi             = $dbi->result_class('DBIx::Custom::Result');
packaging one directory
yuki-kimoto authored on 2009-11-16
1187

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1188
Result class for select statement.
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
1189
Default to L<DBIx::Custom::Result>.
update document
yuki-kimoto authored on 2010-01-30
1190

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1191
=head2 C<query_builder>
added commit method
yuki-kimoto authored on 2010-05-27
1192

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1193
    my $sql_class = $dbi->query_builder;
1194
    $dbi          = $dbi->query_builder(DBIx::Custom::QueryBuilder->new);
added commit method
yuki-kimoto authored on 2010-05-27
1195

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1196
SQL builder. C<query_builder()> must be 
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1197
the instance of L<DBIx::Custom::QueryBuilder> subclass.
1198
Default to L<DBIx::Custom::QueryBuilder> object.
cleanup
yuki-kimoto authored on 2010-08-05
1199

            
1200
=head2 C<cache>
1201

            
1202
    my $cache = $dbi->cache;
1203
    $dbi      = $dbi->cache(1);
1204

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1205
Enable parsed L<DBIx::Custom::Query> object caching.
cleanup
yuki-kimoto authored on 2010-08-05
1206
Default to 1.
1207

            
1208
=head2 C<cache_method>
1209

            
1210
    $dbi          = $dbi->cache_method(\&cache_method);
1211
    $cache_method = $dbi->cache_method
1212

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1213
Method to set and get caches.
cleanup
yuki-kimoto authored on 2010-08-05
1214

            
1215
B<Example:>
1216

            
1217
    $dbi->cache_method(
1218
        sub {
1219
            my $self = shift;
1220
            
1221
            $self->{_cached} ||= {};
1222
            
1223
            if (@_ > 1) {
1224
                $self->{_cached}{$_[0]} = $_[1] 
1225
            }
1226
            else {
1227
                return $self->{_cached}{$_[0]}
1228
            }
1229
        }
1230
    );
added commit method
yuki-kimoto authored on 2010-05-27
1231

            
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1232
=head2 C<filter_check>
1233

            
1234
    my $filter_check = $dbi->filter_check;
1235
    $dbi             = $dbi->filter_check(0);
1236

            
1237
Enable filter check. 
1238
Default to 1.
1239
This check maybe damege performance.
cleanup
yuki-kimoto authored on 2010-08-09
1240
If you require performance, set C<filter_check> attribute to 0.
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1241

            
added register_method() meth...
yuki-kimoto authored on 2010-08-10
1242
=head2 C<(experimental) methods>
1243

            
1244
    my $methods = $dbi->methods;
1245
    $dbi        = $dbi->methods(\%methods);
1246

            
1247
Additional methods.
1248

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1249
=head1 METHODS
added commit method
yuki-kimoto authored on 2010-05-27
1250

            
cleanup
yuki-kimoto authored on 2010-08-05
1251
L<DBIx::Custom> inherits all methods from L<Object::Simple>
1252
and implements the following new ones.
added commit method
yuki-kimoto authored on 2010-05-27
1253

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1254
=head2 C<connect>
packaging one directory
yuki-kimoto authored on 2009-11-16
1255

            
cleanup
yuki-kimoto authored on 2010-08-05
1256
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname",
update document
yuki-kimoto authored on 2010-05-27
1257
                                    user => 'ken', password => '!LFKD%$&');
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1258

            
cleanup
yuki-kimoto authored on 2010-08-05
1259
Create a new L<DBIx::Custom> object and connect to the database.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1260
L<DBIx::Custom> is a wrapper of L<DBI>.
cleanup
yuki-kimoto authored on 2010-08-09
1261
C<AutoCommit> and C<RaiseError> options are true, 
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1262
and C<PrintError> option is false by default. 
packaging one directory
yuki-kimoto authored on 2009-11-16
1263

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1264
=head2 C<insert>
packaging one directory
yuki-kimoto authored on 2009-11-16
1265

            
cleanup
yuki-kimoto authored on 2010-08-05
1266
    $dbi->insert(table  => $table, 
1267
                 param  => \%param,
1268
                 append => $append,
1269
                 filter => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1270

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1271
Execute insert statement.
1272
C<insert> method have C<table>, C<param>, C<append>
1273
and C<filter> arguments.
1274
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1275
C<param> is the pairs of column name value. this must be hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1276
C<append> is a string added at the end of the SQL statement.
1277
C<filter> is filters when parameter binding is executed.
1278
This is overwrites C<default_bind_filter>.
cleanup
yuki-kimoto authored on 2010-08-09
1279
Return value of C<insert()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1280

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1281
B<Example:>
version 0.0901
yuki-kimoto authored on 2009-12-17
1282

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1283
    $dbi->insert(table  => 'books', 
1284
                 param  => {title => 'Perl', author => 'Taro'},
1285
                 append => "some statement",
1286
                 filter => {title => 'encode_utf8'})
version 0.0901
yuki-kimoto authored on 2009-12-17
1287

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1288
=head2 C<update>
packaging one directory
yuki-kimoto authored on 2009-11-16
1289

            
cleanup
yuki-kimoto authored on 2010-08-05
1290
    $dbi->update(table  => $table, 
1291
                 param  => \%params,
1292
                 where  => \%where,
1293
                 append => $append,
1294
                 filter => \%filter)
version 0.0901
yuki-kimoto authored on 2009-12-17
1295

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1296
Execute update statement.
1297
C<update> method have C<table>, C<param>, C<where>, C<append>
1298
and C<filter> arguments.
1299
C<table> is a table name.
1300
C<param> is column-value pairs. this must be hash reference.
1301
C<where> is where clause. this must be hash reference.
1302
C<append> is a string added at the end of the SQL statement.
1303
C<filter> is filters when parameter binding is executed.
1304
This is overwrites C<default_bind_filter>.
cleanup
yuki-kimoto authored on 2010-08-09
1305
Return value of C<update()> is the count of affected rows.
update document
yuki-kimoto authored on 2009-11-19
1306

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1307
B<Example:>
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1308

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1309
    $dbi->update(table  => 'books',
1310
                 param  => {title => 'Perl', author => 'Taro'},
1311
                 where  => {id => 5},
cleanup
yuki-kimoto authored on 2010-08-09
1312
                 append => "some statement",
added commit method
yuki-kimoto authored on 2010-05-27
1313
                 filter => {title => 'encode_utf8'});
version 0.0901
yuki-kimoto authored on 2009-12-17
1314

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1315
=head2 C<update_all>
packaging one directory
yuki-kimoto authored on 2009-11-16
1316

            
cleanup
yuki-kimoto authored on 2010-08-05
1317
    $dbi->update_all(table  => $table, 
1318
                     param  => \%params,
1319
                     filter => \%filter,
1320
                     append => $append);
update document
yuki-kimoto authored on 2009-11-19
1321

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1322
Execute update statement to update all rows.
1323
Arguments is same as C<update> method,
1324
except that C<update_all> don't have C<where> argument.
cleanup
yuki-kimoto authored on 2010-08-09
1325
Return value of C<update_all()> is the count of affected rows.
version 0.0901
yuki-kimoto authored on 2009-12-17
1326

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1327
B<Example:>
update document
yuki-kimoto authored on 2009-11-19
1328

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1329
    $dbi->update_all(table  => 'books', 
1330
                     param  => {author => 'taro'},
1331
                     filter => {author => 'encode_utf8'});
packaging one directory
yuki-kimoto authored on 2009-11-16
1332

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1333
=head2 C<delete>
packaging one directory
yuki-kimoto authored on 2009-11-16
1334

            
cleanup
yuki-kimoto authored on 2010-08-05
1335
    $dbi->delete(table  => $table,
1336
                 where  => \%where,
1337
                 append => $append,
1338
                 filter => \%filter);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1339

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1340
Execute delete statement.
1341
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
1342
C<table> is a table name.
1343
C<where> is where clause. this must be hash reference.
1344
C<append> is a string added at the end of the SQL statement.
1345
C<filter> is filters when parameter binding is executed.
cleanup
yuki-kimoto authored on 2010-08-09
1346
Return value of C<delete()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1347

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1348
B<Example:>
packaging one directory
yuki-kimoto authored on 2009-11-16
1349

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1350
    $dbi->delete(table  => 'books',
1351
                 where  => {id => 5},
1352
                 append => 'some statement',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1353
                 filter => {id => 'encode_utf8'});
version 0.0901
yuki-kimoto authored on 2009-12-17
1354

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1355
=head2 C<delete_all>
packaging one directory
yuki-kimoto authored on 2009-11-16
1356

            
cleanup
yuki-kimoto authored on 2010-08-05
1357
    $dbi->delete_all(table => $table);
packaging one directory
yuki-kimoto authored on 2009-11-16
1358

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1359
Execute delete statement to delete all rows.
1360
Arguments is same as C<delete> method,
1361
except that C<delete_all> don't have C<where> argument.
cleanup
yuki-kimoto authored on 2010-08-09
1362
Return value of C<delete_all()> is the count of affected rows.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1363

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1364
B<Example:>
removed register_format()
yuki-kimoto authored on 2010-05-26
1365
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
1366
    $dbi->delete_all(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
1367

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1368
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1369
    
cleanup
yuki-kimoto authored on 2010-08-05
1370
    my $result = $dbi->select(table    => $table,
1371
                              column   => [@column],
1372
                              where    => \%where,
1373
                              append   => $append,
1374
                              relation => \%relation,
1375
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1376

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1377
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1378
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1379
C<relation> and C<filter> arguments.
1380
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1381
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1382
C<append> is a string added at the end of the SQL statement.
1383
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
1384

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1385
B<Example:>
update document
yuki-kimoto authored on 2009-11-19
1386

            
added commit method
yuki-kimoto authored on 2010-05-27
1387
    # select * from books;
cleanup
yuki-kimoto authored on 2010-08-05
1388
    my $result = $dbi->select(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
1389
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1390
    # select * from books where title = ?;
1391
    my $result = $dbi->select(table => 'books', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1392
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1393
    # select title, author from books where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1394
    my $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
1395
        table  => 'books',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1396
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1397
        where  => {id => 1},
1398
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1399
    );
1400
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1401
    # select books.name as book_name from books, rental
added commit method
yuki-kimoto authored on 2010-05-27
1402
    # where books.id = rental.book_id;
1403
    my $result = $dbi->select(
removed reconnect method
yuki-kimoto authored on 2010-05-28
1404
        table    => ['books', 'rental'],
1405
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
1406
        relation => {'books.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1407
    );
1408

            
cleanup
yuki-kimoto authored on 2010-08-09
1409
If you use more complex condition,
1410
you can specify a array reference to C<where> argument.
1411

            
1412
    my $result = $dbi->select(
1413
        table  => 'books',
1414
        column => ['title', 'author'],
1415
        where  => ['{= title} or {like author}',
1416
                   {title => '%Perl%', author => 'Ken'}]
1417
    );
1418

            
1419
First element is a string. it contains tags,
1420
such as "{= title} or {like author}".
1421
Second element is paramters.
1422

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1423
=head2 C<create_query>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1424
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1425
    my $query = $dbi->create_query(
cleanup
yuki-kimoto authored on 2010-08-09
1426
        "select * from books where {= author} and {like title};"
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1427
    );
1428

            
cleanup
yuki-kimoto authored on 2010-08-09
1429
Create the instance of L<DBIx::Custom::Query> from the source of SQL.
1430
If you want to get high performance,
1431
use C<create_query()> method and execute it by C<execute()> method
1432
instead of suger methods.
1433

            
1434
    $dbi->execute($query, {author => 'Ken', title => '%Perl%'});
packaging one directory
yuki-kimoto authored on 2009-11-16
1435

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1436
=head2 C<execute>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1437

            
cleanup
yuki-kimoto authored on 2010-08-05
1438
    my $result = $dbi->execute($query,  param => $params, filter => \%filter);
1439
    my $result = $dbi->execute($source, param => $params, filter => \%filter);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1440

            
cleanup
yuki-kimoto authored on 2010-08-09
1441
Execute query or the source of SQL.
1442
Query is L<DBIx::Custom::Query> object.
1443
Return value is L<DBIx::Custom::Result> if select statement is executed,
1444
or the count of affected rows if insert, update, delete statement is executed.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1445

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1446
B<Example:>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1447

            
cleanup
yuki-kimoto authored on 2010-08-09
1448
    my $result = $dbi->execute(
1449
        "select * from books where {= author} and {like title}", 
1450
        param => {author => 'Ken', title => '%Perl%'}
1451
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1452
    
1453
    while (my $row = $result->fetch) {
cleanup
yuki-kimoto authored on 2010-08-09
1454
        my $author = $row->[0];
1455
        my $title  = $row->[1];
removed reconnect method
yuki-kimoto authored on 2010-05-28
1456
    }
1457

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1458
=head2 C<register_filter>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1459

            
1460
    $dbi->register_filter(%filters);
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1461
    $dbi->register_filter(\%filters);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1462
    
cleanup
yuki-kimoto authored on 2010-08-09
1463
Register filter. Registered filters is available in the following attributes
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1464
or arguments.
1465

            
1466
=over 4
1467

            
1468
=item *
1469

            
cleanup
yuki-kimoto authored on 2010-08-09
1470
C<default_bind_filter>, C<default_fetch_filter>
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1471

            
1472
=item *
1473

            
1474
C<filter> argument of C<insert()>, C<update()>,
cleanup
yuki-kimoto authored on 2010-08-09
1475
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1476
methods
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1477

            
1478
=item *
1479

            
cleanup
yuki-kimoto authored on 2010-08-09
1480
C<execute()> method
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1481

            
1482
=item *
1483

            
cleanup
yuki-kimoto authored on 2010-08-09
1484
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1485

            
1486
=item *
1487

            
cleanup
yuki-kimoto authored on 2010-08-09
1488
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1489

            
1490
=back
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1491

            
1492
B<Example:>
packaging one directory
yuki-kimoto authored on 2009-11-16
1493

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1494
    $dbi->register_filter(
1495
        encode_utf8 => sub {
1496
            my $value = shift;
1497
            
1498
            require Encode;
1499
            
1500
            return Encode::encode('UTF-8', $value);
1501
        },
1502
        decode_utf8 => sub {
1503
            my $value = shift;
1504
            
1505
            require Encode;
1506
            
1507
            return Encode::decode('UTF-8', $value)
1508
        }
1509
    );
1510

            
added register_method() meth...
yuki-kimoto authored on 2010-08-10
1511
=head2 C<(experimental) register_method>
1512

            
1513
    $dbi->register_method(
1514
        begin_work => sub { shift->dbh->begin_work },
1515
        commit     => sub { shift->dbh->commit },
1516
        rollback   => sub { shift->dbh->rollback}
1517
    );
1518

            
1519
Register methods to the object. You can call these methods
1520
from the object.
1521

            
1522
    $dbi->begin_work;
1523
    $dbi->commit;
1524
    $dbi->rollback;
1525

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1526
=head1 BUGS
1527

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1528
Please tell me bugs if found.
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1529

            
1530
C<< <kimoto.yuki at gmail.com> >>
1531

            
1532
L<http://github.com/yuki-kimoto/DBIx-Custom>
1533

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1534
=head1 AUTHOR
1535

            
1536
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
version 0.0901
yuki-kimoto authored on 2009-12-17
1537

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1538
=head1 COPYRIGHT & LICENSE
1539

            
1540
Copyright 2009 Yuki Kimoto, all rights reserved.
1541

            
1542
This program is free software; you can redistribute it and/or modify it
1543
under the same terms as Perl itself.
1544

            
1545
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1546

            
1547