DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1508 lines | 40.048kb
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) {
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
96
        croak qq{"$name" is invalid name}
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) {
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
129
        croak qq{"$name" is invalid name}
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} || {};
137
    my $append_statement = $args{append} || '';
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
cleanup
yuki-kimoto authored on 2010-08-09
148
    croak qq{"where" must contain the pairs of column name and value}
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
149
      if !@where_keys && !$allow_update_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
150
    
151
    # Update clause
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
152
    my $update_clause = '{update_param ' . join(' ', @update_keys) . '}';
packaging one directory
yuki-kimoto authored on 2009-11-16
153
    
154
    # Where clause
155
    my $where_clause = '';
simplify filtering system
yuki-kimoto authored on 2010-05-01
156
    my $new_where = {};
many change
yuki-kimoto authored on 2010-04-30
157
    
packaging one directory
yuki-kimoto authored on 2009-11-16
158
    if (@where_keys) {
159
        $where_clause = 'where ';
160
        foreach my $where_key (@where_keys) {
simplify filtering system
yuki-kimoto authored on 2010-05-01
161
            
162
            $where_clause .= "{= $where_key} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
163
        }
164
        $where_clause =~ s/ and $//;
165
    }
166
    
167
    # Template for update
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
168
    my $source = "update $table $update_clause $where_clause";
169
    $source .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
170
    
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
171
    # Rearrange parameters
removed register_format()
yuki-kimoto authored on 2010-05-26
172
    foreach my $wkey (@where_keys) {
simplify filtering system
yuki-kimoto authored on 2010-05-01
173
        
removed register_format()
yuki-kimoto authored on 2010-05-26
174
        if (exists $param->{$wkey}) {
175
            $param->{$wkey} = [$param->{$wkey}]
176
              unless ref $param->{$wkey} eq 'ARRAY';
simplify filtering system
yuki-kimoto authored on 2010-05-01
177
            
removed register_format()
yuki-kimoto authored on 2010-05-26
178
            push @{$param->{$wkey}}, $where->{$wkey};
simplify filtering system
yuki-kimoto authored on 2010-05-01
179
        }
add tests
yuki-kimoto authored on 2010-05-01
180
        else {
removed register_format()
yuki-kimoto authored on 2010-05-26
181
            $param->{$wkey} = $where->{$wkey};
add tests
yuki-kimoto authored on 2010-05-01
182
        }
simplify filtering system
yuki-kimoto authored on 2010-05-01
183
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
184
    
185
    # Execute query
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
186
    my $ret_val = $self->execute($source, param  => $param, 
removed register_format()
yuki-kimoto authored on 2010-05-26
187
                                            filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
188
    
189
    return $ret_val;
190
}
191

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

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

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

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

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

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

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

            
341
        # Create SQL object
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
342
        my $builder = $self->query_builder;
added cache_method attribute
yuki-kimoto authored on 2010-06-25
343
        
344
        # Create query
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
345
        $query = eval{$builder->build_query($source)};
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
346
        croak $@ if $@;
removed reconnect method
yuki-kimoto authored on 2010-05-28
347
        
added cache_method attribute
yuki-kimoto authored on 2010-06-25
348
        # Cache query
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
349
        $self->cache_method->($self, $source,
added cache_method attribute
yuki-kimoto authored on 2010-06-25
350
                             {sql     => $query->sql, 
351
                              columns => $query->columns})
352
          if $cache;
removed reconnect method
yuki-kimoto authored on 2010-05-28
353
    }
version 0.0901
yuki-kimoto authored on 2009-12-17
354
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
355
    # Prepare statement handle
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
356
    my $sth = eval {$self->dbh->prepare($query->{sql})};
update document
yuki-kimoto authored on 2010-08-07
357
    croak qq{$@ SQL: "$query->{sql}"} if $@;
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
358
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
359
    # Set statement handle
360
    $query->sth($sth);
361
    
362
    return $query;
363
}
364

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
403
        return $result;
404
    }
405
    return $affected;
406
}
407

            
408
sub _build_bind_values {
409
    my ($self, $query, $params, $filter) = @_;
410
    
411
    # binding values
412
    my @bind_values;
add tests
yuki-kimoto authored on 2010-08-08
413

            
414
    # Filter
415
    $filter ||= {};
416
    
417
    # Parameter
418
    $params ||= {};
419
    
420
    # Check filter
421
    $self->_check_filter($self->filters, $filter,
422
                         $self->default_bind_filter, $params)
423
      if $self->filter_check;
removed reconnect method
yuki-kimoto authored on 2010-05-28
424
    
425
    # Build bind values
426
    my $count = {};
427
    foreach my $column (@{$query->columns}) {
428
        
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
429
        croak qq{"$column" is not exists in params}
removed reconnect method
yuki-kimoto authored on 2010-05-28
430
          unless exists $params->{$column};
431
        
432
        # Value
433
        my $value = ref $params->{$column} eq 'ARRAY'
434
                  ? $params->{$column}->[$count->{$column} || 0]
435
                  : $params->{$column};
436
        
437
        # Filter name
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
438
        my $fname = $filter->{$column} || $self->default_bind_filter || '';
removed reconnect method
yuki-kimoto authored on 2010-05-28
439
        
440
        my $filter_func;
441
        if ($fname) {
442
            
443
            if (ref $fname eq 'CODE') {
444
                $filter_func = $fname;
445
            }
446
            else {
447
                my $filters = $self->filters;
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
448
                croak qq{Not exists filter "$fname"}
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
449
                  unless exists $filters->{$fname};
removed reconnect method
yuki-kimoto authored on 2010-05-28
450
                $filter_func = $filters->{$fname};
451
            }            
452
        }
453
        
454
        push @bind_values, $filter_func
455
                         ? $filter_func->($value)
456
                         : $value;
457
        
458
        # Count up 
459
        $count->{$column}++;
460
    }
461
    
462
    return \@bind_values;
463
}
464

            
add tests
yuki-kimoto authored on 2010-08-08
465
sub _check_filter {
466
    my ($self, $filters, $filter, $default_filter, $params) = @_;
467
    
468
    # Filter name not exists
469
    foreach my $fname (values %$filter) {
470
        croak qq{Bind filter "$fname" is not registered}
471
          unless exists $filters->{$fname};
472
    }
473
    
474
    # Default filter name not exists
475
    croak qq{Default bind filter "$default_filter" is not registered}
476
      if $default_filter && ! exists $filters->{$default_filter};
477
    
478
    # Column name not exists
479
    foreach my $column (keys %$filter) {
480
        
481
        croak qq{Column name "$column" in bind filter is not found in paramters}
482
          unless exists $params->{$column};
483
    }
484
}
485

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
486
=head1 NAME
487

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

            
490
=cut
491

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
492
our $VERSION = '0.1611';
removed reconnect method
yuki-kimoto authored on 2010-05-28
493

            
494
=head1 STABILITY
495

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

            
499
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
500

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
509
    # Insert 
510
    $dbi->insert(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
511
                 param  => {title => 'Perl', author => 'Ken'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
512
                 filter => {title => 'encode_utf8'});
513
    
514
    # Update 
515
    $dbi->update(table  => 'books', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
516
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
517
                 where  => {id => 5},
518
                 filter => {title => 'encode_utf8'});
519
    
520
    # Update all
521
    $dbi->update_all(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
522
                     param  => {title => 'Perl'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
523
                     filter => {title => 'encode_utf8'});
524
    
525
    # Delete
526
    $dbi->delete(table  => 'books',
527
                 where  => {author => 'Ken'},
528
                 filter => {title => 'encode_utf8'});
529
    
530
    # Delete all
531
    $dbi->delete_all(table => 'books');
cleanup
yuki-kimoto authored on 2010-08-05
532

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

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

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
563
    # Execute SQL
removed register_format()
yuki-kimoto authored on 2010-05-26
564
    $dbi->execute("select title from books");
565
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
566
    # Execute SQL with hash binding and filtering
updated document
yuki-kimoto authored on 2010-08-08
567
    $dbi->execute("select id from books where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
568
                  param  => {author => 'ken', title => '%Perl%'},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
569
                  filter => {title => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
570

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

            
updated document
yuki-kimoto authored on 2010-08-08
577
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
578

            
removed register_format()
yuki-kimoto authored on 2010-05-26
579
    # Default filter
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
580
    $dbi->default_bind_filter('encode_utf8');
removed register_format()
yuki-kimoto authored on 2010-05-26
581
    $dbi->default_fetch_filter('decode_utf8');
cleanup
yuki-kimoto authored on 2010-08-05
582

            
583
    # Get DBI object
584
    my $dbh = $dbi->dbh;
585

            
586
Fetch row.
587

            
removed register_format()
yuki-kimoto authored on 2010-05-26
588
    # Fetch
589
    while (my $row = $result->fetch) {
590
        # ...
591
    }
592
    
593
    # Fetch hash
594
    while (my $row = $result->fetch_hash) {
595
        
596
    }
597
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
598
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
599

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
600
=head2 1. Features
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 one of L<DBI> interface modules,
603
such as L<DBIx::Class>, L<DBIx::Simple>.
removed reconnect method
yuki-kimoto authored on 2010-05-28
604

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

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

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

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

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

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

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

            
634
    use DBIx::Custom::SQLite;
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
635
    my $dbi = DBIx::Custom::SQLite->connect(database => 'dbname');
update document
yuki-kimoto authored on 2010-08-07
636
    
cleanup
yuki-kimoto authored on 2010-08-09
637
If database is  MySQL, use L<DBIx::Custom::MySQL>.
update document
yuki-kimoto authored on 2010-08-07
638

            
639
    use DBIx::Custom::MySQL;
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
640
    my $dbi = DBIx::Custom::MySQL->connect(
641
        database => 'dbname',
642
        user     => 'ken',
643
        password => '!LFKD%$&'
644
    );
update document
yuki-kimoto authored on 2010-08-07
645

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

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

            
update document
yuki-kimoto authored on 2010-08-07
652
=head3 insert()
653

            
updated document
yuki-kimoto authored on 2010-08-08
654
Execute insert statement.
655

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

            
659
The following SQL is executed.
660

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

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

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

            
668
=head3 update()
669

            
updated document
yuki-kimoto authored on 2010-08-08
670
Execute update statement.
671

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

            
676
The following SQL is executed.
677

            
678
    update books set title = ?, author = ?;
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 "METHOD" section.
update document
yuki-kimoto authored on 2010-08-07
684

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

            
687
=head3 delete()
688

            
updated document
yuki-kimoto authored on 2010-08-08
689
Execute delete statement.
690

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

            
694
The following SQL is executed.
695

            
696
    delete from books where id = ?;
697

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

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

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

            
705
=head3 select()
706

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

            
709
    my $result = $dbi->select(table => 'books');
710

            
711
The following SQL is executed.
712

            
713
    select * from books;
714

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

            
718
    while (my $row = $result->fetch) {
719
        my $title  = $row->[0];
720
        my $author = $row->[1];
721
    }
722

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

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

            
728
    my $result = $dbi->select(
729
        table  => 'books',
730
        column => [qw/author title/],
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
731
        where  => {author => 'Ken'}
732
    );
update document
yuki-kimoto authored on 2010-08-07
733

            
734
The following SQL is executed.
735

            
736
    select author, title from books where author = ?;
737

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

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

            
742
    my $result = $dbi->select(
743
        table    => ['books', 'rental'],
744
        column   => ['books.name as book_name']
745
        relation => {'books.id' => 'rental.book_id'}
746
    );
747

            
748
The following SQL is executed.
749

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

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

            
756
    my $result = $dbi->select(
757
        table  => 'books',
758
        where  => {author => 'Ken'},
759
        append => 'order by price limit 5',
760
    );
761

            
762
The following SQL is executed.
763

            
764
    select * books where author = ? order by price limit 5;
765

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

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

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

            
776
Fetch row into array.
777
    
778
    while (my $row = $result->fetch) {
779
        my $author = $row->[0];
780
        my $title  = $row->[1];
781
        
782
    }
783

            
784
Fetch only a first row into array.
785

            
786
    my $row = $result->fetch_first;
787

            
788
Fetch multiple rows into array of array.
789

            
790
    while (my $rows = $result->fetch_multi(5)) {
791
        my $first_author  = $rows->[0][0];
792
        my $first_title   = $rows->[0][1];
793
        my $second_author = $rows->[1][0];
794
        my $second_value  = $rows->[1][1];
795
    
796
    }
797
    
798
Fetch all rows into array of array.
799

            
800
    my $rows = $result->fetch_all;
801

            
802
Fetch row into hash.
803

            
804
    # Fetch a row into hash
805
    while (my $row = $result->fetch_hash) {
806
        my $title  = $row->{title};
807
        my $author = $row->{author};
808
        
809
    }
810

            
811
Fetch only a first row into hash
812

            
813
    my $row = $result->fetch_hash_first;
814
    
815
Fetch multiple rows into array of hash
816

            
817
    while (my $rows = $result->fetch_hash_multi(5)) {
818
        my $first_title   = $rows->[0]{title};
819
        my $first_author  = $rows->[0]{author};
820
        my $second_title  = $rows->[1]{title};
821
        my $second_author = $rows->[1]{author};
822
    
823
    }
824
    
825
Fetch all rows into array of hash
826

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

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

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

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

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

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

            
839
    use DBI;
840
    my $dbh = DBI->connect(...);
841
    my $sth = $dbh->prepare(
842
        "select * from books where author = ? and title like ?;"
843
    );
844
    $sth->execute('Ken', '%Perl%');
845

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

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

            
852
    my $result = $dbi->execute(
853
        "select * from books where {= author} and {like title};"
854
        param => {author => 'Ken', title => '%Perl%'}
855
    );
856

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

            
861
    select * from books where {= author} and {like title}
862
      -> select * from books where author = ? and title like ?;
863

            
updated document
yuki-kimoto authored on 2010-08-08
864
The following tags is available.
865

            
866
    [TAG]                       [REPLACED]
867
    {? NAME}               ->   ?
868
    {= NAME}               ->   NAME = ?
869
    {<> NAME}              ->   NAME <> ?
870
    
871
    {< NAME}               ->   NAME < ?
872
    {> NAME}               ->   NAME > ?
873
    {>= NAME}              ->   NAME >= ?
874
    {<= NAME}              ->   NAME <= ?
875
    
876
    {like NAME}            ->   NAME like ?
877
    {in NAME COUNT}        ->   NAME in [?, ?, ..]
878
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
879
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
880
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
updated document
yuki-kimoto authored on 2010-08-08
881

            
882
See also L<DBIx::Custom::QueryBuilder>.
883

            
884
Default start tag is '{'. end tag is '}'.
885
You can change this tag.
886

            
887
    $dbi->query_builder->start_tag('|');
888
    $dbi->query_builder->end_tag('|');
update document
yuki-kimoto authored on 2010-08-07
889

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

            
892
Usually, Perl string is kept as internal string.
893
If you want to save the string to database, You must encode the string.
894
Filtering system help you to convert a data to another data
895
when you save to the data and get the data form database.
896

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

            
899
    $dbi->register_filter(
900
        to_upper_case => sub {
901
            my $value = shift;
902
            return uc $value;
903
        }
904
    );
905

            
906
C<encode_utf8> and C<decode_utf8> filter is registerd by default.
907

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

            
910
    my $result = $dbi->execute(
911
        "select * from books where {= author} and {like title};"
updated document
yuki-kimoto authored on 2010-08-08
912
        param  => {author => 'Ken', title => '%Perl%'},
update document
yuki-kimoto authored on 2010-08-07
913
        filter => {author => 'to_upper_case, title => 'encode_utf8'}
914
    );
915

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

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
920
    # insert(), having filter argument
update document
yuki-kimoto authored on 2010-08-07
921
    $dbi->insert(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
922
                 param  => {title => 'Perl', author => 'Ken'},
update document
yuki-kimoto authored on 2010-08-07
923
                 filter => {title => 'encode_utf8'});
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
924
    
925
    # select(), having filter argument
update document
yuki-kimoto authored on 2010-08-07
926
    my $result = $dbi->select(
927
        table  => 'books',
928
        column => [qw/author title/],
929
        where  => {author => 'Ken'},
930
        append => 'order by id limit 1',
931
        filter => {title => 'encode_utf8'}
932
    );
933

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

            
936
    $dbi->default_bind_filter('encode_utf8');
937

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
938
C<filter()> argument overwrites this default filter.
update document
yuki-kimoto authored on 2010-08-07
939
    
940
    $dbi->default_bind_filter('encode_utf8');
941
    $dbi->insert(
942
        table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
943
        param  => {title => 'Perl', author => 'Ken', price => 1000},
update document
yuki-kimoto authored on 2010-08-07
944
        filter => {author => 'to_upper_case', price => undef}
945
    );
946

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

            
949
    $dbi->insert(
950
        table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
951
        param  => {title => 'Perl', author => 'Ken', price => 1000},
update document
yuki-kimoto authored on 2010-08-07
952
        filter => {title => 'encode_uft8' author => 'to_upper_case'}
953
    );
954

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

            
957
    my $result = $dbi->select(table => 'books');
958
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
959

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

            
963
    $dbi->default_fetch_filter('decode_utf8');
964

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

            
968
    $dbi->default_fetch_filter('decode_utf8');
969
    my $result = $dbi->select(
970
        table => 'books',
971
        columns => ['title', 'author', 'price']
972
    );
973
    $result->filter({author => 'to_upper_case', price => undef});
974

            
975
This is same as the following one.
976

            
977
    my $result = $dbi->select(
978
        table => 'books',
979
        columns => ['title', 'author', 'price']
980
    );
981
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
982

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

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

            
updated document
yuki-kimoto authored on 2010-08-08
989
=head3 Disable filter checking
990

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

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

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

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

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

            
1005
C<insert()> method is a little slow because SQL statement and statement handle
1006
is created every time.
1007

            
1008
In that case, you can prepare a query by C<create_query()> method.
1009
    
1010
    my $query = $dbi->create_query(
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1011
        "insert into books {insert_param title author};"
updated document
yuki-kimoto authored on 2010-08-08
1012
    );
cleanup
yuki-kimoto authored on 2010-08-09
1013

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

            
1017
    {
1018
        sql     => 'insert into books (title, author) values (?, ?);',
1019
        columns => ['title', 'author']
1020
    }
1021

            
1022
Execute query repeatedly.
updated document
yuki-kimoto authored on 2010-08-08
1023
    
1024
    my $inputs = [
1025
        {title => 'Perl',      author => 'Ken'},
1026
        {title => 'Good days', author => 'Mike'}
1027
    ];
1028
    
1029
    foreach my $input (@$inputs) {
1030
        $dbi->execute($query, $input);
1031
    }
1032

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1035
=head3 caching
updated document
yuki-kimoto authored on 2010-08-08
1036

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

            
1040
    $dbi->cache(1);
1041

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1052
    $dbi->cache_method(sub {
updated document
yuki-kimoto authored on 2010-08-08
1053
        sub {
1054
            my $self = shift;
1055
            
1056
            $self->{_cached} ||= {};
1057
            
1058
            # Set cache
1059
            if (@_ > 1) {
1060
                $self->{_cached}{$_[0]} = $_[1] 
1061
            }
1062
            
1063
            # Get cache
1064
            else {
1065
                return $self->{_cached}{$_[0]}
1066
            }
1067
        }
1068
    });
1069

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

            
1072
=head3 Get DBI object
1073

            
1074
You can get L<DBI> object and call any method of L<DBI>.
1075

            
1076
    $dbi->dbh->begin_work;
1077
    $dbi->dbh->commit;
1078
    $dbi->dbh->rollback;
1079

            
1080
=head3 Change Result class
1081

            
1082
You can change Result class if you need.
1083

            
1084
    package Your::Result;
1085
    use base 'DBIx::Custom::Result';
1086
    
1087
    sub some_method { ... }
1088

            
1089
    1;
1090
    
1091
    package main;
1092
    
1093
    use Your::Result;
1094
    
1095
    my $dbi = DBIx::Custom->connect(...);
1096
    $dbi->result_class('Your::Result');
1097

            
1098
=head3 Custamize SQL builder object
1099

            
1100
You can custamize SQL builder object
1101

            
1102
    my $dbi = DBIx::Custom->connect(...);
1103
    $dbi->query_builder->start_tag('|');
1104
    $dbi->query_builder->end_tag('|');
1105
    $dbi->query_builder->register_tag_processor(
1106
        name => sub {
1107
           ...
1108
        }
1109
    );
1110

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1183
=head2 C<cache>
1184

            
1185
    my $cache = $dbi->cache;
1186
    $dbi      = $dbi->cache(1);
1187

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

            
1191
=head2 C<cache_method>
1192

            
1193
    $dbi          = $dbi->cache_method(\&cache_method);
1194
    $cache_method = $dbi->cache_method
1195

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

            
1198
B<Example:>
1199

            
1200
    $dbi->cache_method(
1201
        sub {
1202
            my $self = shift;
1203
            
1204
            $self->{_cached} ||= {};
1205
            
1206
            if (@_ > 1) {
1207
                $self->{_cached}{$_[0]} = $_[1] 
1208
            }
1209
            else {
1210
                return $self->{_cached}{$_[0]}
1211
            }
1212
        }
1213
    );
added commit method
yuki-kimoto authored on 2010-05-27
1214

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

            
1217
    my $filter_check = $dbi->filter_check;
1218
    $dbi             = $dbi->filter_check(0);
1219

            
1220
Enable filter check. 
1221
Default to 1.
1222
This check maybe damege performance.
cleanup
yuki-kimoto authored on 2010-08-09
1223
If you require performance, set C<filter_check> attribute to 0.
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1224

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1242
    $dbi->insert(table  => $table, 
1243
                 param  => \%param,
1244
                 append => $append,
1245
                 filter => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1246

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1247
Execute insert statement.
1248
C<insert> method have C<table>, C<param>, C<append>
1249
and C<filter> arguments.
1250
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1251
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
1252
C<append> is a string added at the end of the SQL statement.
1253
C<filter> is filters when parameter binding is executed.
1254
This is overwrites C<default_bind_filter>.
cleanup
yuki-kimoto authored on 2010-08-09
1255
Return value of C<insert()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1256

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1259
    $dbi->insert(table  => 'books', 
1260
                 param  => {title => 'Perl', author => 'Taro'},
1261
                 append => "some statement",
1262
                 filter => {title => 'encode_utf8'})
version 0.0901
yuki-kimoto authored on 2009-12-17
1263

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1266
    $dbi->update(table  => $table, 
1267
                 param  => \%params,
1268
                 where  => \%where,
1269
                 append => $append,
1270
                 filter => \%filter)
version 0.0901
yuki-kimoto authored on 2009-12-17
1271

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1293
    $dbi->update_all(table  => $table, 
1294
                     param  => \%params,
1295
                     filter => \%filter,
1296
                     append => $append);
update document
yuki-kimoto authored on 2009-11-19
1297

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1311
    $dbi->delete(table  => $table,
1312
                 where  => \%where,
1313
                 append => $append,
1314
                 filter => \%filter);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1315

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1316
Execute delete statement.
1317
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
1318
C<table> is a table name.
1319
C<where> is where clause. this must be hash reference.
1320
C<append> is a string added at the end of the SQL statement.
1321
C<filter> is filters when parameter binding is executed.
cleanup
yuki-kimoto authored on 2010-08-09
1322
Return value of C<delete()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1323

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

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

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

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

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

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1344
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1345
    
cleanup
yuki-kimoto authored on 2010-08-05
1346
    my $result = $dbi->select(table    => $table,
1347
                              column   => [@column],
1348
                              where    => \%where,
1349
                              append   => $append,
1350
                              relation => \%relation,
1351
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1352

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1353
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1354
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1355
C<relation> and C<filter> arguments.
1356
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1357
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1358
C<append> is a string added at the end of the SQL statement.
1359
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
1360

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

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

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

            
1388
    my $result = $dbi->select(
1389
        table  => 'books',
1390
        column => ['title', 'author'],
1391
        where  => ['{= title} or {like author}',
1392
                   {title => '%Perl%', author => 'Ken'}]
1393
    );
1394

            
1395
First element is a string. it contains tags,
1396
such as "{= title} or {like author}".
1397
Second element is paramters.
1398

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1424
    my $result = $dbi->execute(
1425
        "select * from books where {= author} and {like title}", 
1426
        param => {author => 'Ken', title => '%Perl%'}
1427
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1428
    
1429
    while (my $row = $result->fetch) {
cleanup
yuki-kimoto authored on 2010-08-09
1430
        my $author = $row->[0];
1431
        my $title  = $row->[1];
removed reconnect method
yuki-kimoto authored on 2010-05-28
1432
    }
1433

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

            
1436
    $dbi->register_filter(%filters);
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1437
    $dbi->register_filter(\%filters);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1438
    
cleanup
yuki-kimoto authored on 2010-08-09
1439
Register filter. Registered filters is available in the following attributes
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1440
or arguments.
1441

            
1442
=over 4
1443

            
1444
=item *
1445

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

            
1448
=item *
1449

            
1450
C<filter> argument of C<insert()>, C<update()>,
cleanup
yuki-kimoto authored on 2010-08-09
1451
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1452
methods
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1453

            
1454
=item *
1455

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

            
1458
=item *
1459

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

            
1462
=item *
1463

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

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1470
    $dbi->register_filter(
1471
        encode_utf8 => sub {
1472
            my $value = shift;
1473
            
1474
            require Encode;
1475
            
1476
            return Encode::encode('UTF-8', $value);
1477
        },
1478
        decode_utf8 => sub {
1479
            my $value = shift;
1480
            
1481
            require Encode;
1482
            
1483
            return Encode::decode('UTF-8', $value)
1484
        }
1485
    );
1486

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

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

            
1491
C<< <kimoto.yuki at gmail.com> >>
1492

            
1493
L<http://github.com/yuki-kimoto/DBIx-Custom>
1494

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1495
=head1 AUTHOR
1496

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

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

            
1501
Copyright 2009 Yuki Kimoto, all rights reserved.
1502

            
1503
This program is free software; you can redistribute it and/or modify it
1504
under the same terms as Perl itself.
1505

            
1506
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1507

            
1508