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

            
DBIx::Custom is now stable
yuki-kimoto authored on 2010-09-07
3
our $VERSION = '0.1617';
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
4

            
5
use 5.008001;
cleanup
yuki-kimoto authored on 2009-12-22
6
use strict;
7
use warnings;
8

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
342
        # Create SQL object
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
343
        my $builder = $self->query_builder;
added cache_method attribute
yuki-kimoto authored on 2010-06-25
344
        
345
        # Create query
fixed Carp trast relationshi...
yuki-kimoto authored on 2010-08-12
346
        $query = $builder->build_query($source);
347

            
added cache_method attribute
yuki-kimoto authored on 2010-06-25
348
        # Cache query
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
349
        $self->cache_method->($self, $source,
added cache_method attribute
yuki-kimoto authored on 2010-06-25
350
                             {sql     => $query->sql, 
351
                              columns => $query->columns})
352
          if $cache;
removed reconnect method
yuki-kimoto authored on 2010-05-28
353
    }
version 0.0901
yuki-kimoto authored on 2009-12-17
354
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
355
    # Prepare statement handle
add tests
yuki-kimoto authored on 2010-08-10
356
    my $sth;
357
    eval { $sth = $self->dbh->prepare($query->{sql})};
removed experimental registe...
yuki-kimoto authored on 2010-08-24
358
    $self->_croak($@, qq{. SQL: "$query->{sql}"}) if $@;
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

            
removed experimental registe...
yuki-kimoto authored on 2010-08-24
366
sub _croak {
367
    my ($self, $error, $append) = @_;
368
    $append ||= "";
369
    
370
    # Verbose
371
    if ($Carp::Verbose) { croak $error }
372
    
373
    # Not verbose
374
    else {
375
        
376
        # Remove line and module infromation
377
        my $at_pos = rindex($error, ' at ');
378
        $error = substr($error, 0, $at_pos);
379
        $error =~ s/\s+$//;
380
        
381
        croak "$error$append";
382
    }
383
}
384

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
385
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/;
386

            
387
sub execute{
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
388
    my ($self, $query, %args)  = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
389
    
390
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
391
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
392
        croak qq{"$name" is invalid argument}
removed reconnect method
yuki-kimoto authored on 2010-05-28
393
          unless $VALID_EXECUTE_ARGS{$name};
394
    }
395
    
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
396
    my $params = $args{param} || {};
removed reconnect method
yuki-kimoto authored on 2010-05-28
397
    
add tests
yuki-kimoto authored on 2010-08-10
398
    # First argument is the soruce of SQL
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
399
    $query = $self->create_query($query)
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
400
      unless ref $query;
removed reconnect method
yuki-kimoto authored on 2010-05-28
401
    
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
402
    my $filter = $args{filter} || $query->filter || {};
removed reconnect method
yuki-kimoto authored on 2010-05-28
403
    
404
    # Create bind value
405
    my $bind_values = $self->_build_bind_values($query, $params, $filter);
406
    
407
    # Execute
408
    my $sth      = $query->sth;
add tests
yuki-kimoto authored on 2010-08-10
409
    my $affected;
410
    eval {$affected = $sth->execute(@$bind_values)};
removed experimental registe...
yuki-kimoto authored on 2010-08-24
411
    $self->_croak($@) if $@;
removed reconnect method
yuki-kimoto authored on 2010-05-28
412
    
413
    # Return resultset if select statement is executed
414
    if ($sth->{NUM_OF_FIELDS}) {
415
        
416
        # Create result
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
417
        my $result = $self->result_class->new(
418
            sth            => $sth,
419
            default_filter => $self->default_fetch_filter,
added check_filter attribute
yuki-kimoto authored on 2010-08-08
420
            filters        => $self->filters,
421
            filter_check   => $self->filter_check
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
422
        );
423

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
424
        return $result;
425
    }
426
    return $affected;
427
}
428

            
429
sub _build_bind_values {
430
    my ($self, $query, $params, $filter) = @_;
431
    
432
    # binding values
433
    my @bind_values;
add tests
yuki-kimoto authored on 2010-08-08
434

            
435
    # Filter
436
    $filter ||= {};
437
    
438
    # Parameter
439
    $params ||= {};
440
    
441
    # Check filter
442
    $self->_check_filter($self->filters, $filter,
443
                         $self->default_bind_filter, $params)
444
      if $self->filter_check;
removed reconnect method
yuki-kimoto authored on 2010-05-28
445
    
446
    # Build bind values
447
    my $count = {};
448
    foreach my $column (@{$query->columns}) {
449
        
450
        # Value
451
        my $value = ref $params->{$column} eq 'ARRAY'
452
                  ? $params->{$column}->[$count->{$column} || 0]
453
                  : $params->{$column};
454
        
add tests
yuki-kimoto authored on 2010-08-10
455
        # Filtering
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
456
        my $fname = $filter->{$column} || $self->default_bind_filter || '';
add tests
yuki-kimoto authored on 2010-08-10
457
        my $filter_func = $fname ? $self->filters->{$fname} : undef;
removed reconnect method
yuki-kimoto authored on 2010-05-28
458
        push @bind_values, $filter_func
459
                         ? $filter_func->($value)
460
                         : $value;
461
        
462
        # Count up 
463
        $count->{$column}++;
464
    }
465
    
466
    return \@bind_values;
467
}
468

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

            
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
490
1;
491

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
492
=head1 NAME
493

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

            
496
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
497

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

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

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

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

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

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

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

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

            
updated document
yuki-kimoto authored on 2010-08-08
574
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
575

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

            
580
    # Get DBI object
581
    my $dbh = $dbi->dbh;
582

            
583
Fetch row.
584

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
update document
yuki-kimoto authored on 2010-08-07
649
=head3 insert()
650

            
updated document
yuki-kimoto authored on 2010-08-08
651
Execute insert statement.
652

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

            
656
The following SQL is executed.
657

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

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

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

            
665
=head3 update()
666

            
updated document
yuki-kimoto authored on 2010-08-08
667
Execute update statement.
668

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

            
673
The following SQL is executed.
674

            
675
    update books set title = ?, author = ?;
676

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

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

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

            
684
=head3 delete()
685

            
updated document
yuki-kimoto authored on 2010-08-08
686
Execute delete statement.
687

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

            
691
The following SQL is executed.
692

            
693
    delete from books where id = ?;
694

            
updated document
yuki-kimoto authored on 2010-08-08
695
The value of C<id> is embedded into the placehodler.
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 "METHODS" 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 delete all rows, use C<delete_all()> method.
update document
yuki-kimoto authored on 2010-08-07
701

            
702
=head3 select()
703

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

            
706
    my $result = $dbi->select(table => 'books');
707

            
708
The following SQL is executed.
709

            
710
    select * from books;
711

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

            
715
    while (my $row = $result->fetch) {
716
        my $title  = $row->[0];
717
        my $author = $row->[1];
718
    }
719

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

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

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

            
731
The following SQL is executed.
732

            
733
    select author, title from books where author = ?;
734

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

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

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

            
745
The following SQL is executed.
746

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

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

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

            
759
The following SQL is executed.
760

            
761
    select * books where author = ? order by price limit 5;
762

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

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

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

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

            
781
Fetch only a first row into array.
782

            
783
    my $row = $result->fetch_first;
784

            
785
Fetch multiple rows into array of array.
786

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

            
797
    my $rows = $result->fetch_all;
798

            
799
Fetch row into hash.
800

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

            
808
Fetch only a first row into hash
809

            
810
    my $row = $result->fetch_hash_first;
811
    
812
Fetch multiple rows into array of hash
813

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

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

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

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

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

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

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

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

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

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

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

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

            
858
    select * from books where {= author} and {like title}
859
      -> select * from books where author = ? and title like ?;
860

            
updated document
yuki-kimoto authored on 2010-08-08
861
The following tags is available.
862

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

            
879
See also L<DBIx::Custom::QueryBuilder>.
880

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

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

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

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

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

            
896
    $dbi->register_filter(
897
        to_upper_case => sub {
898
            my $value = shift;
899
            return uc $value;
900
        }
901
    );
902

            
903
C<encode_utf8> and C<decode_utf8> filter is registerd by default.
904

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

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

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

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

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

            
933
    $dbi->default_bind_filter('encode_utf8');
934

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

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

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

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

            
954
    my $result = $dbi->select(table => 'books');
955
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
956

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

            
960
    $dbi->default_fetch_filter('decode_utf8');
961

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

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

            
972
This is same as the following one.
973

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

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

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

            
updated document
yuki-kimoto authored on 2010-08-08
986
=head3 Disable filter checking
987

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

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

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

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

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

            
1002
C<insert()> method is a little slow because SQL statement and statement handle
1003
is created every time.
1004

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

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

            
1014
    {
1015
        sql     => 'insert into books (title, author) values (?, ?);',
1016
        columns => ['title', 'author']
1017
    }
1018

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

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1032
=head3 caching
updated document
yuki-kimoto authored on 2010-08-08
1033

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

            
1037
    $dbi->cache(1);
1038

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

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

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

            
1069
=head3 Get DBI object
1070

            
1071
You can get L<DBI> object and call any method of L<DBI>.
1072

            
1073
    $dbi->dbh->begin_work;
1074
    $dbi->dbh->commit;
1075
    $dbi->dbh->rollback;
1076

            
1077
=head3 Change Result class
1078

            
1079
You can change Result class if you need.
1080

            
1081
    package Your::Result;
1082
    use base 'DBIx::Custom::Result';
1083
    
1084
    sub some_method { ... }
1085

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

            
1095
=head3 Custamize SQL builder object
1096

            
1097
You can custamize SQL builder object
1098

            
1099
    my $dbi = DBIx::Custom->connect(...);
1100
    $dbi->query_builder->register_tag_processor(
1101
        name => sub {
1102
           ...
1103
        }
1104
    );
1105

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1178
=head2 C<cache>
1179

            
1180
    my $cache = $dbi->cache;
1181
    $dbi      = $dbi->cache(1);
1182

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

            
1186
=head2 C<cache_method>
1187

            
1188
    $dbi          = $dbi->cache_method(\&cache_method);
1189
    $cache_method = $dbi->cache_method
1190

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

            
1193
B<Example:>
1194

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

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

            
1212
    my $filter_check = $dbi->filter_check;
1213
    $dbi             = $dbi->filter_check(0);
1214

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1237
    $dbi->insert(table  => $table, 
1238
                 param  => \%param,
1239
                 append => $append,
1240
                 filter => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1241

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1288
    $dbi->update_all(table  => $table, 
1289
                     param  => \%params,
1290
                     filter => \%filter,
1291
                     append => $append);
update document
yuki-kimoto authored on 2009-11-19
1292

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1383
    my $result = $dbi->select(
1384
        table  => 'books',
1385
        column => ['title', 'author'],
1386
        where  => ['{= title} or {like author}',
1387
                   {title => '%Perl%', author => 'Ken'}]
1388
    );
1389

            
1390
First element is a string. it contains tags,
1391
such as "{= title} or {like author}".
1392
Second element is paramters.
1393

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

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

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

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

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

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

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

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

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

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

            
1437
=over 4
1438

            
1439
=item *
1440

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

            
1443
=item *
1444

            
1445
C<filter> argument of C<insert()>, C<update()>,
cleanup
yuki-kimoto authored on 2010-08-09
1446
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1447
methods
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1448

            
1449
=item *
1450

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

            
1453
=item *
1454

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

            
1457
=item *
1458

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

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

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

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

            
DBIx::Custom is now stable
yuki-kimoto authored on 2010-09-07
1482
=head1 STABILITY
1483

            
1484
L<DBIx::Custom> is now stable. APIs keep backword compatible in the feature.
1485

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

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

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

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

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

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

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

            
1500
Copyright 2009 Yuki Kimoto, all rights reserved.
1501

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

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

            
1507