DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1543 lines | 40.668kb
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
fixed Carp trast relationshi...
yuki-kimoto authored on 2010-08-12
342
        $query = $builder->build_query($source);
343

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

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

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

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

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

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

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

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

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
501
=head1 NAME
502

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

            
505
=cut
506

            
fixed Carp trast relationshi...
yuki-kimoto authored on 2010-08-12
507
our $VERSION = '0.1614';
removed reconnect method
yuki-kimoto authored on 2010-05-28
508

            
509
=head1 STABILITY
510

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

            
514
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
515

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

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

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

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

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

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

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

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

            
updated document
yuki-kimoto authored on 2010-08-08
592
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
593

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

            
598
    # Get DBI object
599
    my $dbh = $dbi->dbh;
600

            
601
Fetch row.
602

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
update document
yuki-kimoto authored on 2010-08-07
667
=head3 insert()
668

            
updated document
yuki-kimoto authored on 2010-08-08
669
Execute insert statement.
670

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

            
674
The following SQL is executed.
675

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

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

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

            
683
=head3 update()
684

            
updated document
yuki-kimoto authored on 2010-08-08
685
Execute update statement.
686

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

            
691
The following SQL is executed.
692

            
693
    update books set title = ?, author = ?;
694

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

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

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

            
702
=head3 delete()
703

            
updated document
yuki-kimoto authored on 2010-08-08
704
Execute delete statement.
705

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

            
709
The following SQL is executed.
710

            
711
    delete from books where id = ?;
712

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

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

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

            
720
=head3 select()
721

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

            
724
    my $result = $dbi->select(table => 'books');
725

            
726
The following SQL is executed.
727

            
728
    select * from books;
729

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

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

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

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

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

            
749
The following SQL is executed.
750

            
751
    select author, title from books where author = ?;
752

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

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

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

            
763
The following SQL is executed.
764

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

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

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

            
777
The following SQL is executed.
778

            
779
    select * books where author = ? order by price limit 5;
780

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

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

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

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

            
799
Fetch only a first row into array.
800

            
801
    my $row = $result->fetch_first;
802

            
803
Fetch multiple rows into array of array.
804

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

            
815
    my $rows = $result->fetch_all;
816

            
817
Fetch row into hash.
818

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

            
826
Fetch only a first row into hash
827

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
updated document
yuki-kimoto authored on 2010-08-08
879
The following tags is available.
880

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

            
897
See also L<DBIx::Custom::QueryBuilder>.
898

            
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
899
C<{> and C<}> is reserved. If you use these charactors,
900
you must escape them using '\'. Note that '\' is
901
already perl escaped charactor, so you must write '\\'. 
updated document
yuki-kimoto authored on 2010-08-08
902

            
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
903
    'select * from books \\{ something statement \\}'
update document
yuki-kimoto authored on 2010-08-07
904

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

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

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

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

            
921
C<encode_utf8> and C<decode_utf8> filter is registerd by default.
922

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

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

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

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

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

            
951
    $dbi->default_bind_filter('encode_utf8');
952

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

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

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

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

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

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

            
978
    $dbi->default_fetch_filter('decode_utf8');
979

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

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

            
990
This is same as the following one.
991

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

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

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

            
updated document
yuki-kimoto authored on 2010-08-08
1004
=head3 Disable filter checking
1005

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

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1050
=head3 caching
updated document
yuki-kimoto authored on 2010-08-08
1051

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

            
1055
    $dbi->cache(1);
1056

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

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

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

            
1087
=head3 Get DBI object
1088

            
1089
You can get L<DBI> object and call any method of L<DBI>.
1090

            
1091
    $dbi->dbh->begin_work;
1092
    $dbi->dbh->commit;
1093
    $dbi->dbh->rollback;
1094

            
1095
=head3 Change Result class
1096

            
1097
You can change Result class if you need.
1098

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

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

            
1113
=head3 Custamize SQL builder object
1114

            
1115
You can custamize SQL builder object
1116

            
1117
    my $dbi = DBIx::Custom->connect(...);
1118
    $dbi->query_builder->register_tag_processor(
1119
        name => sub {
1120
           ...
1121
        }
1122
    );
1123

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1196
=head2 C<cache>
1197

            
1198
    my $cache = $dbi->cache;
1199
    $dbi      = $dbi->cache(1);
1200

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

            
1204
=head2 C<cache_method>
1205

            
1206
    $dbi          = $dbi->cache_method(\&cache_method);
1207
    $cache_method = $dbi->cache_method
1208

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

            
1211
B<Example:>
1212

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

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

            
1230
    my $filter_check = $dbi->filter_check;
1231
    $dbi             = $dbi->filter_check(0);
1232

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

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

            
1240
    my $methods = $dbi->methods;
1241
    $dbi        = $dbi->methods(\%methods);
1242

            
1243
Additional methods.
1244

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1262
    $dbi->insert(table  => $table, 
1263
                 param  => \%param,
1264
                 append => $append,
1265
                 filter => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1266

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1267
Execute insert statement.
1268
C<insert> method have C<table>, C<param>, C<append>
1269
and C<filter> arguments.
1270
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1271
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
1272
C<append> is a string added at the end of the SQL statement.
1273
C<filter> is filters when parameter binding is executed.
1274
This is overwrites C<default_bind_filter>.
cleanup
yuki-kimoto authored on 2010-08-09
1275
Return value of C<insert()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1276

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1313
    $dbi->update_all(table  => $table, 
1314
                     param  => \%params,
1315
                     filter => \%filter,
1316
                     append => $append);
update document
yuki-kimoto authored on 2009-11-19
1317

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1408
    my $result = $dbi->select(
1409
        table  => 'books',
1410
        column => ['title', 'author'],
1411
        where  => ['{= title} or {like author}',
1412
                   {title => '%Perl%', author => 'Ken'}]
1413
    );
1414

            
1415
First element is a string. it contains tags,
1416
such as "{= title} or {like author}".
1417
Second element is paramters.
1418

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

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

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

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

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

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

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

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

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

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

            
1462
=over 4
1463

            
1464
=item *
1465

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

            
1468
=item *
1469

            
1470
C<filter> argument of C<insert()>, C<update()>,
cleanup
yuki-kimoto authored on 2010-08-09
1471
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1472
methods
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1473

            
1474
=item *
1475

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

            
1478
=item *
1479

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

            
1482
=item *
1483

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

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

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

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

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

            
1509
    $dbi->register_method(
1510
        begin_work => sub { shift->dbh->begin_work },
1511
        commit     => sub { shift->dbh->commit },
1512
        rollback   => sub { shift->dbh->rollback}
1513
    );
1514

            
1515
Register methods to the object. You can call these methods
1516
from the object.
1517

            
1518
    $dbi->begin_work;
1519
    $dbi->commit;
1520
    $dbi->rollback;
1521

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

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

            
1526
C<< <kimoto.yuki at gmail.com> >>
1527

            
1528
L<http://github.com/yuki-kimoto/DBIx-Custom>
1529

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1530
=head1 AUTHOR
1531

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

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

            
1536
Copyright 2009 Yuki Kimoto, all rights reserved.
1537

            
1538
This program is free software; you can redistribute it and/or modify it
1539
under the same terms as Perl itself.
1540

            
1541
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1542

            
1543