DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1499 lines | 39.7kb
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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
477
=head1 NAME
478

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

            
481
=cut
482

            
add tests
yuki-kimoto authored on 2010-08-10
483
our $VERSION = '0.1612';
removed reconnect method
yuki-kimoto authored on 2010-05-28
484

            
485
=head1 STABILITY
486

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

            
490
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
491

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
500
    # Insert 
501
    $dbi->insert(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
502
                 param  => {title => 'Perl', author => 'Ken'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
503
                 filter => {title => 'encode_utf8'});
504
    
505
    # Update 
506
    $dbi->update(table  => 'books', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
507
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
508
                 where  => {id => 5},
509
                 filter => {title => 'encode_utf8'});
510
    
511
    # Update all
512
    $dbi->update_all(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
513
                     param  => {title => 'Perl'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
514
                     filter => {title => 'encode_utf8'});
515
    
516
    # Delete
517
    $dbi->delete(table  => 'books',
518
                 where  => {author => 'Ken'},
519
                 filter => {title => 'encode_utf8'});
520
    
521
    # Delete all
522
    $dbi->delete_all(table => 'books');
cleanup
yuki-kimoto authored on 2010-08-05
523

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
526
    # Select
527
    my $result = $dbi->select(table => 'books');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
528
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
529
    # Select, more complex
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
530
    my $result = $dbi->select(
update document
yuki-kimoto authored on 2010-05-27
531
        table  => 'books',
532
        column => [qw/author title/],
533
        where  => {author => 'Ken'},
updated document
yuki-kimoto authored on 2010-08-08
534
        append => 'order by id limit 5',
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
535
        filter => {title => 'encode_utf8'}
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
536
    );
added commit method
yuki-kimoto authored on 2010-05-27
537
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
538
    # Select, join table
added commit method
yuki-kimoto authored on 2010-05-27
539
    my $result = $dbi->select(
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
540
        table    => ['books', 'rental'],
541
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
542
        relation => {'books.id' => 'rental.book_id'}
543
    );
updated document
yuki-kimoto authored on 2010-08-08
544
    
545
    # Select, more flexible where
546
    my $result = $dbi->select(
547
        table  => 'books',
548
        where  => ['{= author} and {like title}', 
549
                   {author => 'Ken', title => '%Perl%'}]
550
    );
cleanup
yuki-kimoto authored on 2010-08-05
551

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
554
    # Execute SQL
removed register_format()
yuki-kimoto authored on 2010-05-26
555
    $dbi->execute("select title from books");
556
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
557
    # Execute SQL with hash binding and filtering
updated document
yuki-kimoto authored on 2010-08-08
558
    $dbi->execute("select id from books where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
559
                  param  => {author => 'ken', title => '%Perl%'},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
560
                  filter => {title => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
561

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

            
updated document
yuki-kimoto authored on 2010-08-08
568
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
569

            
removed register_format()
yuki-kimoto authored on 2010-05-26
570
    # Default filter
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
571
    $dbi->default_bind_filter('encode_utf8');
removed register_format()
yuki-kimoto authored on 2010-05-26
572
    $dbi->default_fetch_filter('decode_utf8');
cleanup
yuki-kimoto authored on 2010-08-05
573

            
574
    # Get DBI object
575
    my $dbh = $dbi->dbh;
576

            
577
Fetch row.
578

            
removed register_format()
yuki-kimoto authored on 2010-05-26
579
    # Fetch
580
    while (my $row = $result->fetch) {
581
        # ...
582
    }
583
    
584
    # Fetch hash
585
    while (my $row = $result->fetch_hash) {
586
        
587
    }
588
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
589
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
590

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

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

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

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

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

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

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

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

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

            
625
    use DBIx::Custom::SQLite;
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
626
    my $dbi = DBIx::Custom::SQLite->connect(database => 'dbname');
update document
yuki-kimoto authored on 2010-08-07
627
    
cleanup
yuki-kimoto authored on 2010-08-09
628
If database is  MySQL, use L<DBIx::Custom::MySQL>.
update document
yuki-kimoto authored on 2010-08-07
629

            
630
    use DBIx::Custom::MySQL;
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
631
    my $dbi = DBIx::Custom::MySQL->connect(
632
        database => 'dbname',
633
        user     => 'ken',
634
        password => '!LFKD%$&'
635
    );
update document
yuki-kimoto authored on 2010-08-07
636

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

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

            
update document
yuki-kimoto authored on 2010-08-07
643
=head3 insert()
644

            
updated document
yuki-kimoto authored on 2010-08-08
645
Execute insert statement.
646

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

            
650
The following SQL is executed.
651

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

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

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

            
659
=head3 update()
660

            
updated document
yuki-kimoto authored on 2010-08-08
661
Execute update statement.
662

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

            
667
The following SQL is executed.
668

            
669
    update books set title = ?, author = ?;
670

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

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

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

            
678
=head3 delete()
679

            
updated document
yuki-kimoto authored on 2010-08-08
680
Execute delete statement.
681

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

            
685
The following SQL is executed.
686

            
687
    delete from books where id = ?;
688

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

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

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

            
696
=head3 select()
697

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

            
700
    my $result = $dbi->select(table => 'books');
701

            
702
The following SQL is executed.
703

            
704
    select * from books;
705

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

            
709
    while (my $row = $result->fetch) {
710
        my $title  = $row->[0];
711
        my $author = $row->[1];
712
    }
713

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

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

            
719
    my $result = $dbi->select(
720
        table  => 'books',
721
        column => [qw/author title/],
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
722
        where  => {author => 'Ken'}
723
    );
update document
yuki-kimoto authored on 2010-08-07
724

            
725
The following SQL is executed.
726

            
727
    select author, title from books where author = ?;
728

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

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

            
733
    my $result = $dbi->select(
734
        table    => ['books', 'rental'],
735
        column   => ['books.name as book_name']
736
        relation => {'books.id' => 'rental.book_id'}
737
    );
738

            
739
The following SQL is executed.
740

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

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

            
747
    my $result = $dbi->select(
748
        table  => 'books',
749
        where  => {author => 'Ken'},
750
        append => 'order by price limit 5',
751
    );
752

            
753
The following SQL is executed.
754

            
755
    select * books where author = ? order by price limit 5;
756

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

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

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

            
767
Fetch row into array.
768
    
769
    while (my $row = $result->fetch) {
770
        my $author = $row->[0];
771
        my $title  = $row->[1];
772
        
773
    }
774

            
775
Fetch only a first row into array.
776

            
777
    my $row = $result->fetch_first;
778

            
779
Fetch multiple rows into array of array.
780

            
781
    while (my $rows = $result->fetch_multi(5)) {
782
        my $first_author  = $rows->[0][0];
783
        my $first_title   = $rows->[0][1];
784
        my $second_author = $rows->[1][0];
785
        my $second_value  = $rows->[1][1];
786
    
787
    }
788
    
789
Fetch all rows into array of array.
790

            
791
    my $rows = $result->fetch_all;
792

            
793
Fetch row into hash.
794

            
795
    # Fetch a row into hash
796
    while (my $row = $result->fetch_hash) {
797
        my $title  = $row->{title};
798
        my $author = $row->{author};
799
        
800
    }
801

            
802
Fetch only a first row into hash
803

            
804
    my $row = $result->fetch_hash_first;
805
    
806
Fetch multiple rows into array of hash
807

            
808
    while (my $rows = $result->fetch_hash_multi(5)) {
809
        my $first_title   = $rows->[0]{title};
810
        my $first_author  = $rows->[0]{author};
811
        my $second_title  = $rows->[1]{title};
812
        my $second_author = $rows->[1]{author};
813
    
814
    }
815
    
816
Fetch all rows into array of hash
817

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

            
cleanup
yuki-kimoto authored on 2010-08-09
820
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
821

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

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

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

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

            
830
    use DBI;
831
    my $dbh = DBI->connect(...);
832
    my $sth = $dbh->prepare(
833
        "select * from books where author = ? and title like ?;"
834
    );
835
    $sth->execute('Ken', '%Perl%');
836

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

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

            
843
    my $result = $dbi->execute(
844
        "select * from books where {= author} and {like title};"
845
        param => {author => 'Ken', title => '%Perl%'}
846
    );
847

            
848
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
849
{= author} and {like title} is called C<tag>.
850
tag is expand to placeholder string internally.
update document
yuki-kimoto authored on 2010-08-07
851

            
852
    select * from books where {= author} and {like title}
853
      -> select * from books where author = ? and title like ?;
854

            
updated document
yuki-kimoto authored on 2010-08-08
855
The following tags is available.
856

            
857
    [TAG]                       [REPLACED]
858
    {? NAME}               ->   ?
859
    {= NAME}               ->   NAME = ?
860
    {<> NAME}              ->   NAME <> ?
861
    
862
    {< NAME}               ->   NAME < ?
863
    {> NAME}               ->   NAME > ?
864
    {>= NAME}              ->   NAME >= ?
865
    {<= NAME}              ->   NAME <= ?
866
    
867
    {like NAME}            ->   NAME like ?
868
    {in NAME COUNT}        ->   NAME in [?, ?, ..]
869
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
870
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
871
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
updated document
yuki-kimoto authored on 2010-08-08
872

            
873
See also L<DBIx::Custom::QueryBuilder>.
874

            
875
Default start tag is '{'. end tag is '}'.
876
You can change this tag.
877

            
878
    $dbi->query_builder->start_tag('|');
879
    $dbi->query_builder->end_tag('|');
update document
yuki-kimoto authored on 2010-08-07
880

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

            
883
Usually, Perl string is kept as internal string.
884
If you want to save the string to database, You must encode the string.
885
Filtering system help you to convert a data to another data
886
when you save to the data and get the data form database.
887

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

            
890
    $dbi->register_filter(
891
        to_upper_case => sub {
892
            my $value = shift;
893
            return uc $value;
894
        }
895
    );
896

            
897
C<encode_utf8> and C<decode_utf8> filter is registerd by default.
898

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

            
901
    my $result = $dbi->execute(
902
        "select * from books where {= author} and {like title};"
updated document
yuki-kimoto authored on 2010-08-08
903
        param  => {author => 'Ken', title => '%Perl%'},
update document
yuki-kimoto authored on 2010-08-07
904
        filter => {author => 'to_upper_case, title => 'encode_utf8'}
905
    );
906

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

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
911
    # insert(), having filter argument
update document
yuki-kimoto authored on 2010-08-07
912
    $dbi->insert(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
913
                 param  => {title => 'Perl', author => 'Ken'},
update document
yuki-kimoto authored on 2010-08-07
914
                 filter => {title => 'encode_utf8'});
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
915
    
916
    # select(), having filter argument
update document
yuki-kimoto authored on 2010-08-07
917
    my $result = $dbi->select(
918
        table  => 'books',
919
        column => [qw/author title/],
920
        where  => {author => 'Ken'},
921
        append => 'order by id limit 1',
922
        filter => {title => 'encode_utf8'}
923
    );
924

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

            
927
    $dbi->default_bind_filter('encode_utf8');
928

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
929
C<filter()> argument overwrites this default filter.
update document
yuki-kimoto authored on 2010-08-07
930
    
931
    $dbi->default_bind_filter('encode_utf8');
932
    $dbi->insert(
933
        table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
934
        param  => {title => 'Perl', author => 'Ken', price => 1000},
update document
yuki-kimoto authored on 2010-08-07
935
        filter => {author => 'to_upper_case', price => undef}
936
    );
937

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

            
940
    $dbi->insert(
941
        table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
942
        param  => {title => 'Perl', author => 'Ken', price => 1000},
update document
yuki-kimoto authored on 2010-08-07
943
        filter => {title => 'encode_uft8' author => 'to_upper_case'}
944
    );
945

            
updated document
yuki-kimoto authored on 2010-08-08
946
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
947

            
948
    my $result = $dbi->select(table => 'books');
949
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
950

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

            
954
    $dbi->default_fetch_filter('decode_utf8');
955

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

            
959
    $dbi->default_fetch_filter('decode_utf8');
960
    my $result = $dbi->select(
961
        table => 'books',
962
        columns => ['title', 'author', 'price']
963
    );
964
    $result->filter({author => 'to_upper_case', price => undef});
965

            
966
This is same as the following one.
967

            
968
    my $result = $dbi->select(
969
        table => 'books',
970
        columns => ['title', 'author', 'price']
971
    );
972
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
973

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

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

            
updated document
yuki-kimoto authored on 2010-08-08
980
=head3 Disable filter checking
981

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

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

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

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

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

            
996
C<insert()> method is a little slow because SQL statement and statement handle
997
is created every time.
998

            
999
In that case, you can prepare a query by C<create_query()> method.
1000
    
1001
    my $query = $dbi->create_query(
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1002
        "insert into books {insert_param title author};"
updated document
yuki-kimoto authored on 2010-08-08
1003
    );
cleanup
yuki-kimoto authored on 2010-08-09
1004

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

            
1008
    {
1009
        sql     => 'insert into books (title, author) values (?, ?);',
1010
        columns => ['title', 'author']
1011
    }
1012

            
1013
Execute query repeatedly.
updated document
yuki-kimoto authored on 2010-08-08
1014
    
1015
    my $inputs = [
1016
        {title => 'Perl',      author => 'Ken'},
1017
        {title => 'Good days', author => 'Mike'}
1018
    ];
1019
    
1020
    foreach my $input (@$inputs) {
1021
        $dbi->execute($query, $input);
1022
    }
1023

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1026
=head3 caching
updated document
yuki-kimoto authored on 2010-08-08
1027

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

            
1031
    $dbi->cache(1);
1032

            
1033
Caching is on memory, but you can change this by C<cache_method()>.
1034
First argument is L<DBIx::Custom> object.
cleanup
yuki-kimoto authored on 2010-08-09
1035
Second argument is a source of SQL,
1036
such as "select * from books where {= title} and {= author};";
updated document
yuki-kimoto authored on 2010-08-08
1037
Third argument is parsed result, such as
1038
{sql => "select * from books where title = ? and author = ?",
1039
 columns => ['title', 'author']}, this is hash reference.
cleanup
yuki-kimoto authored on 2010-08-09
1040
If arguments is more than two, this method is called to set cache.
1041
If not, this method is called to get cache.
updated document
yuki-kimoto authored on 2010-08-08
1042

            
cleanup
yuki-kimoto authored on 2010-08-09
1043
    $dbi->cache_method(sub {
updated document
yuki-kimoto authored on 2010-08-08
1044
        sub {
1045
            my $self = shift;
1046
            
1047
            $self->{_cached} ||= {};
1048
            
1049
            # Set cache
1050
            if (@_ > 1) {
1051
                $self->{_cached}{$_[0]} = $_[1] 
1052
            }
1053
            
1054
            # Get cache
1055
            else {
1056
                return $self->{_cached}{$_[0]}
1057
            }
1058
        }
1059
    });
1060

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

            
1063
=head3 Get DBI object
1064

            
1065
You can get L<DBI> object and call any method of L<DBI>.
1066

            
1067
    $dbi->dbh->begin_work;
1068
    $dbi->dbh->commit;
1069
    $dbi->dbh->rollback;
1070

            
1071
=head3 Change Result class
1072

            
1073
You can change Result class if you need.
1074

            
1075
    package Your::Result;
1076
    use base 'DBIx::Custom::Result';
1077
    
1078
    sub some_method { ... }
1079

            
1080
    1;
1081
    
1082
    package main;
1083
    
1084
    use Your::Result;
1085
    
1086
    my $dbi = DBIx::Custom->connect(...);
1087
    $dbi->result_class('Your::Result');
1088

            
1089
=head3 Custamize SQL builder object
1090

            
1091
You can custamize SQL builder object
1092

            
1093
    my $dbi = DBIx::Custom->connect(...);
1094
    $dbi->query_builder->start_tag('|');
1095
    $dbi->query_builder->end_tag('|');
1096
    $dbi->query_builder->register_tag_processor(
1097
        name => sub {
1098
           ...
1099
        }
1100
    );
1101

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1174
=head2 C<cache>
1175

            
1176
    my $cache = $dbi->cache;
1177
    $dbi      = $dbi->cache(1);
1178

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

            
1182
=head2 C<cache_method>
1183

            
1184
    $dbi          = $dbi->cache_method(\&cache_method);
1185
    $cache_method = $dbi->cache_method
1186

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

            
1189
B<Example:>
1190

            
1191
    $dbi->cache_method(
1192
        sub {
1193
            my $self = shift;
1194
            
1195
            $self->{_cached} ||= {};
1196
            
1197
            if (@_ > 1) {
1198
                $self->{_cached}{$_[0]} = $_[1] 
1199
            }
1200
            else {
1201
                return $self->{_cached}{$_[0]}
1202
            }
1203
        }
1204
    );
added commit method
yuki-kimoto authored on 2010-05-27
1205

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

            
1208
    my $filter_check = $dbi->filter_check;
1209
    $dbi             = $dbi->filter_check(0);
1210

            
1211
Enable filter check. 
1212
Default to 1.
1213
This check maybe damege performance.
cleanup
yuki-kimoto authored on 2010-08-09
1214
If you require performance, set C<filter_check> attribute to 0.
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1215

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1233
    $dbi->insert(table  => $table, 
1234
                 param  => \%param,
1235
                 append => $append,
1236
                 filter => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1237

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1238
Execute insert statement.
1239
C<insert> method have C<table>, C<param>, C<append>
1240
and C<filter> arguments.
1241
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1242
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
1243
C<append> is a string added at the end of the SQL statement.
1244
C<filter> is filters when parameter binding is executed.
1245
This is overwrites C<default_bind_filter>.
cleanup
yuki-kimoto authored on 2010-08-09
1246
Return value of C<insert()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1247

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1250
    $dbi->insert(table  => 'books', 
1251
                 param  => {title => 'Perl', author => 'Taro'},
1252
                 append => "some statement",
1253
                 filter => {title => 'encode_utf8'})
version 0.0901
yuki-kimoto authored on 2009-12-17
1254

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1257
    $dbi->update(table  => $table, 
1258
                 param  => \%params,
1259
                 where  => \%where,
1260
                 append => $append,
1261
                 filter => \%filter)
version 0.0901
yuki-kimoto authored on 2009-12-17
1262

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1263
Execute update statement.
1264
C<update> method have C<table>, C<param>, C<where>, C<append>
1265
and C<filter> arguments.
1266
C<table> is a table name.
1267
C<param> is column-value pairs. this must be hash reference.
1268
C<where> is where clause. this must be hash reference.
1269
C<append> is a string added at the end of the SQL statement.
1270
C<filter> is filters when parameter binding is executed.
1271
This is overwrites C<default_bind_filter>.
cleanup
yuki-kimoto authored on 2010-08-09
1272
Return value of C<update()> is the count of affected rows.
update document
yuki-kimoto authored on 2009-11-19
1273

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1284
    $dbi->update_all(table  => $table, 
1285
                     param  => \%params,
1286
                     filter => \%filter,
1287
                     append => $append);
update document
yuki-kimoto authored on 2009-11-19
1288

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1302
    $dbi->delete(table  => $table,
1303
                 where  => \%where,
1304
                 append => $append,
1305
                 filter => \%filter);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1306

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1307
Execute delete statement.
1308
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
1309
C<table> is a table name.
1310
C<where> is where clause. this must be hash reference.
1311
C<append> is a string added at the end of the SQL statement.
1312
C<filter> is filters when parameter binding is executed.
cleanup
yuki-kimoto authored on 2010-08-09
1313
Return value of C<delete()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1314

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

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

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

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

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

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1335
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1336
    
cleanup
yuki-kimoto authored on 2010-08-05
1337
    my $result = $dbi->select(table    => $table,
1338
                              column   => [@column],
1339
                              where    => \%where,
1340
                              append   => $append,
1341
                              relation => \%relation,
1342
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1343

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1344
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1345
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1346
C<relation> and C<filter> arguments.
1347
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1348
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1349
C<append> is a string added at the end of the SQL statement.
1350
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
1351

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

            
added commit method
yuki-kimoto authored on 2010-05-27
1354
    # select * from books;
cleanup
yuki-kimoto authored on 2010-08-05
1355
    my $result = $dbi->select(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
1356
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1357
    # select * from books where title = ?;
1358
    my $result = $dbi->select(table => 'books', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1359
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1360
    # select title, author from books where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1361
    my $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
1362
        table  => 'books',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1363
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1364
        where  => {id => 1},
1365
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1366
    );
1367
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1368
    # select books.name as book_name from books, rental
added commit method
yuki-kimoto authored on 2010-05-27
1369
    # where books.id = rental.book_id;
1370
    my $result = $dbi->select(
removed reconnect method
yuki-kimoto authored on 2010-05-28
1371
        table    => ['books', 'rental'],
1372
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
1373
        relation => {'books.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1374
    );
1375

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

            
1379
    my $result = $dbi->select(
1380
        table  => 'books',
1381
        column => ['title', 'author'],
1382
        where  => ['{= title} or {like author}',
1383
                   {title => '%Perl%', author => 'Ken'}]
1384
    );
1385

            
1386
First element is a string. it contains tags,
1387
such as "{= title} or {like author}".
1388
Second element is paramters.
1389

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1415
    my $result = $dbi->execute(
1416
        "select * from books where {= author} and {like title}", 
1417
        param => {author => 'Ken', title => '%Perl%'}
1418
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1419
    
1420
    while (my $row = $result->fetch) {
cleanup
yuki-kimoto authored on 2010-08-09
1421
        my $author = $row->[0];
1422
        my $title  = $row->[1];
removed reconnect method
yuki-kimoto authored on 2010-05-28
1423
    }
1424

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

            
1427
    $dbi->register_filter(%filters);
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1428
    $dbi->register_filter(\%filters);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1429
    
cleanup
yuki-kimoto authored on 2010-08-09
1430
Register filter. Registered filters is available in the following attributes
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1431
or arguments.
1432

            
1433
=over 4
1434

            
1435
=item *
1436

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

            
1439
=item *
1440

            
1441
C<filter> argument of C<insert()>, C<update()>,
cleanup
yuki-kimoto authored on 2010-08-09
1442
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1443
methods
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1444

            
1445
=item *
1446

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

            
1449
=item *
1450

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

            
1453
=item *
1454

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

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1461
    $dbi->register_filter(
1462
        encode_utf8 => sub {
1463
            my $value = shift;
1464
            
1465
            require Encode;
1466
            
1467
            return Encode::encode('UTF-8', $value);
1468
        },
1469
        decode_utf8 => sub {
1470
            my $value = shift;
1471
            
1472
            require Encode;
1473
            
1474
            return Encode::decode('UTF-8', $value)
1475
        }
1476
    );
1477

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

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

            
1482
C<< <kimoto.yuki at gmail.com> >>
1483

            
1484
L<http://github.com/yuki-kimoto/DBIx-Custom>
1485

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1486
=head1 AUTHOR
1487

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

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

            
1492
Copyright 2009 Yuki Kimoto, all rights reserved.
1493

            
1494
This program is free software; you can redistribute it and/or modify it
1495
under the same terms as Perl itself.
1496

            
1497
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1498

            
1499