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

            
cleanup
yuki-kimoto authored on 2010-10-17
3
our $VERSION = '0.1618';
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

            
cleanup
yuki-kimoto authored on 2010-10-17
18
__PACKAGE__->attr([qw/data_source dbh default_bind_filter
19
                      default_fetch_filter password user/]);
added cache_method attribute
yuki-kimoto authored on 2010-06-25
20

            
add cache attribute
yuki-kimoto authored on 2010-06-14
21
__PACKAGE__->attr(cache => 1);
added cache_method attribute
yuki-kimoto authored on 2010-06-25
22
__PACKAGE__->attr(cache_method => sub {
23
    sub {
24
        my $self = shift;
25
        
26
        $self->{_cached} ||= {};
27
        
28
        if (@_ > 1) {
29
            $self->{_cached}{$_[0]} = $_[1] 
30
        }
31
        else {
32
            return $self->{_cached}{$_[0]}
33
        }
34
    }
35
});
removed register_format()
yuki-kimoto authored on 2010-05-26
36

            
cleanup
yuki-kimoto authored on 2010-10-17
37
__PACKAGE__->dual_attr('filters', default => sub { {} },
38
                                  inherit => 'hash_copy');
added check_filter attribute
yuki-kimoto authored on 2010-08-08
39
__PACKAGE__->attr(filter_check => 1);
cleanup
yuki-kimoto authored on 2010-10-17
40
__PACKAGE__->attr(query_builder  => sub {DBIx::Custom::QueryBuilder->new});
41
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result');
42

            
43
# DBI methods
44
foreach my $method (qw/begin_work commit rollback/) {
45
    my $code = sub {
46
        my $self = shift;
47
        my $ret = eval {$self->dbh->$method};
48
        croak $@ if $@;
49
        return $ret;
50
    };
51
    no strict 'refs';
52
    my $pkg = __PACKAGE__;
53
    *{"${pkg}::$method"} = $code;
54
};
55

            
56
# Regster filter
57
__PACKAGE__->register_filter(
58
    encode_utf8 => sub { encode_utf8($_[0]) },
59
    decode_utf8 => sub { decode_utf8($_[0]) }
60
);
added check_filter attribute
yuki-kimoto authored on 2010-08-08
61

            
added helper method
yuki-kimoto authored on 2010-10-17
62
our $AUTOLOAD;
63

            
64
sub AUTOLOAD {
65
    my $self = shift;
66

            
67
    # Method
68
    my ($package, $method) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
69

            
70
    # Helper
71
    $self->{_helpers} ||= {};
72
    croak qq/Can't locate object method "$method" via "$package"/
73
      unless my $helper = $self->{_helpers}->{$method};
74

            
75
    # Run
76
    return $self->$helper(@_);
77
}
78

            
79
sub helper {
80
    my $self = shift;
81
    
82
    # Merge
83
    my $helpers = ref $_[0] eq 'HASH' ? $_[0] : {@_};
84
    $self->{_helpers} = {%{$self->{_helpers} || {}}, %$helpers};
85
    
86
    return $self;
87
}
88

            
packaging one directory
yuki-kimoto authored on 2009-11-16
89
sub connect {
removed register_format()
yuki-kimoto authored on 2010-05-26
90
    my $proto = shift;
91
    
92
    # Create
93
    my $self = ref $proto ? $proto : $proto->new(@_);
update document
yuki-kimoto authored on 2010-01-30
94
    
95
    # Information
packaging one directory
yuki-kimoto authored on 2009-11-16
96
    my $data_source = $self->data_source;
97
    my $user        = $self->user;
98
    my $password    = $self->password;
99
    
removed experimental registe...
yuki-kimoto authored on 2010-08-24
100
    
update document
yuki-kimoto authored on 2010-01-30
101
    # Connect
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
102
    my $dbh = eval {DBI->connect(
packaging one directory
yuki-kimoto authored on 2009-11-16
103
        $data_source,
104
        $user,
105
        $password,
106
        {
107
            RaiseError => 1,
108
            PrintError => 0,
109
            AutoCommit => 1,
110
        }
111
    )};
112
    
update document
yuki-kimoto authored on 2010-01-30
113
    # Connect error
packaging one directory
yuki-kimoto authored on 2009-11-16
114
    croak $@ if $@;
115
    
update document
yuki-kimoto authored on 2010-01-30
116
    # Database handle
packaging one directory
yuki-kimoto authored on 2009-11-16
117
    $self->dbh($dbh);
update document
yuki-kimoto authored on 2010-01-30
118
    
packaging one directory
yuki-kimoto authored on 2009-11-16
119
    return $self;
120
}
121

            
cleanup
yuki-kimoto authored on 2010-10-17
122
sub create_query {
123
    my ($self, $source) = @_;
update document
yuki-kimoto authored on 2010-01-30
124
    
cleanup
yuki-kimoto authored on 2010-10-17
125
    # Cache
126
    my $cache = $self->cache;
update document
yuki-kimoto authored on 2010-01-30
127
    
cleanup
yuki-kimoto authored on 2010-10-17
128
    # Create query
129
    my $query;
130
    if ($cache) {
131
        
132
        # Get query
133
        my $q = $self->cache_method->($self, $source);
134
        
135
        # Create query
136
        $query = DBIx::Custom::Query->new($q) if $q;
137
    }
138
    
139
    unless ($query) {
cleanup insert
yuki-kimoto authored on 2010-04-28
140

            
cleanup
yuki-kimoto authored on 2010-10-17
141
        # Create SQL object
142
        my $builder = $self->query_builder;
143
        
144
        # Create query
145
        $query = $builder->build_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
146

            
cleanup
yuki-kimoto authored on 2010-10-17
147
        # Cache query
148
        $self->cache_method->($self, $source,
149
                             {sql     => $query->sql, 
150
                              columns => $query->columns})
151
          if $cache;
cleanup insert
yuki-kimoto authored on 2010-04-28
152
    }
153
    
cleanup
yuki-kimoto authored on 2010-10-17
154
    # Prepare statement handle
155
    my $sth;
156
    eval { $sth = $self->dbh->prepare($query->{sql})};
157
    $self->_croak($@, qq{. SQL: "$query->{sql}"}) if $@;
packaging one directory
yuki-kimoto authored on 2009-11-16
158
    
cleanup
yuki-kimoto authored on 2010-10-17
159
    # Set statement handle
160
    $query->sth($sth);
packaging one directory
yuki-kimoto authored on 2009-11-16
161
    
cleanup
yuki-kimoto authored on 2010-10-17
162
    return $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
163
}
164

            
cleanup
yuki-kimoto authored on 2010-10-17
165
our %VALID_DELETE_ARGS
166
  = map { $_ => 1 } qw/table where append filter allow_delete_all/;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
167

            
cleanup
yuki-kimoto authored on 2010-10-17
168
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
169
    my ($self, %args) = @_;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
170
    
171
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
172
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
173
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
174
          unless $VALID_DELETE_ARGS{$name};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
175
    }
176
    
177
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
178
    my $table            = $args{table} || '';
179
    my $where            = $args{where} || {};
cleanup
yuki-kimoto authored on 2010-10-17
180
    my $append = $args{append};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
181
    my $filter           = $args{filter};
cleanup
yuki-kimoto authored on 2010-10-17
182
    my $allow_delete_all = $args{allow_delete_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
183
    
184
    # Where keys
removed register_format()
yuki-kimoto authored on 2010-05-26
185
    my @where_keys = keys %$where;
packaging one directory
yuki-kimoto authored on 2009-11-16
186
    
187
    # Not exists where keys
add tests
yuki-kimoto authored on 2010-08-10
188
    croak qq{"where" argument must be specified and } .
189
          qq{contains the pairs of column name and value}
cleanup
yuki-kimoto authored on 2010-10-17
190
      if !@where_keys && !$allow_delete_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
191
    
192
    # Where clause
193
    my $where_clause = '';
194
    if (@where_keys) {
195
        $where_clause = 'where ';
add tests
yuki-kimoto authored on 2010-08-10
196
        $where_clause .= "{= $_} and " for @where_keys;
packaging one directory
yuki-kimoto authored on 2009-11-16
197
        $where_clause =~ s/ and $//;
198
    }
199
    
add tests
yuki-kimoto authored on 2010-08-10
200
    # Source of SQL
cleanup
yuki-kimoto authored on 2010-10-17
201
    my $source = "delete from $table $where_clause";
add tests
yuki-kimoto authored on 2010-08-10
202
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
203
    
204
    # Execute query
cleanup
yuki-kimoto authored on 2010-10-17
205
    my $ret_val = $self->execute($source, param  => $where, 
add tests
yuki-kimoto authored on 2010-08-10
206
                                 filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
207
    
208
    return $ret_val;
209
}
210

            
cleanup
yuki-kimoto authored on 2010-10-17
211
sub delete_all { shift->delete(allow_delete_all => 1, @_) }
packaging one directory
yuki-kimoto authored on 2009-11-16
212

            
added helper method
yuki-kimoto authored on 2010-10-17
213
sub DESTROY { }
214

            
cleanup
yuki-kimoto authored on 2010-10-17
215
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
216

            
cleanup
yuki-kimoto authored on 2010-10-17
217
sub execute{
218
    my ($self, $query, %args)  = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
219
    
220
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
221
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
222
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
223
          unless $VALID_EXECUTE_ARGS{$name};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
224
    }
225
    
cleanup
yuki-kimoto authored on 2010-10-17
226
    my $params = $args{param} || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
227
    
cleanup
yuki-kimoto authored on 2010-10-17
228
    # First argument is the soruce of SQL
229
    $query = $self->create_query($query)
230
      unless ref $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
231
    
cleanup
yuki-kimoto authored on 2010-10-17
232
    my $filter = $args{filter} || $query->filter || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
233
    
cleanup
yuki-kimoto authored on 2010-10-17
234
    # Create bind value
235
    my $bind_values = $self->_build_bind_values($query, $params, $filter);
236
    
237
    # Execute
238
    my $sth      = $query->sth;
239
    my $affected;
240
    eval {$affected = $sth->execute(@$bind_values)};
241
    $self->_croak($@) if $@;
242
    
243
    # Return resultset if select statement is executed
244
    if ($sth->{NUM_OF_FIELDS}) {
245
        
246
        # Create result
247
        my $result = $self->result_class->new(
248
            sth            => $sth,
249
            default_filter => $self->default_fetch_filter,
250
            filters        => $self->filters,
251
            filter_check   => $self->filter_check
252
        );
253

            
254
        return $result;
255
    }
256
    return $affected;
257
}
258

            
259
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append filter/;
260

            
261
sub insert {
262
    my ($self, %args) = @_;
263

            
264
    # Check arguments
265
    foreach my $name (keys %args) {
266
        croak qq{"$name" is invalid argument}
267
          unless $VALID_INSERT_ARGS{$name};
packaging one directory
yuki-kimoto authored on 2009-11-16
268
    }
269
    
cleanup
yuki-kimoto authored on 2010-10-17
270
    # Arguments
271
    my $table  = $args{table} || '';
272
    my $param  = $args{param} || {};
273
    my $append = $args{append} || '';
274
    my $filter = $args{filter};
275
    
276
    # Insert keys
277
    my @insert_keys = keys %$param;
278
    
279
    # Templte for insert
280
    my $source = "insert into $table {insert_param "
281
               . join(' ', @insert_keys) . '}';
add tests
yuki-kimoto authored on 2010-08-10
282
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
283
    
284
    # Execute query
cleanup
yuki-kimoto authored on 2010-10-17
285
    my $ret_val = $self->execute($source, param  => $param, 
286
                                          filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
287
    
288
    return $ret_val;
289
}
290

            
cleanup
yuki-kimoto authored on 2010-10-17
291
sub register_filter {
292
    my $invocant = shift;
293
    
294
    # Register filter
295
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
296
    $invocant->filters({%{$invocant->filters}, %$filters});
297
    
298
    return $invocant;
299
}
packaging one directory
yuki-kimoto authored on 2009-11-16
300

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
304
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
305
    my ($self, %args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
306
    
refactoring select
yuki-kimoto authored on 2010-04-28
307
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
308
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
309
        croak qq{"$name" is invalid argument}
refactoring select
yuki-kimoto authored on 2010-04-28
310
          unless $VALID_SELECT_ARGS{$name};
311
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
312
    
refactoring select
yuki-kimoto authored on 2010-04-28
313
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
314
    my $tables = $args{table} || [];
removed register_format()
yuki-kimoto authored on 2010-05-26
315
    $tables = [$tables] unless ref $tables eq 'ARRAY';
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
316
    my $columns  = $args{column} || [];
update document
yuki-kimoto authored on 2010-08-07
317
    my $where    = $args{where};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
318
    my $relation = $args{relation};
319
    my $append   = $args{append};
320
    my $filter   = $args{filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
321
    
add tests
yuki-kimoto authored on 2010-08-10
322
    # Source of SQL
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
323
    my $source = 'select ';
packaging one directory
yuki-kimoto authored on 2009-11-16
324
    
added commit method
yuki-kimoto authored on 2010-05-27
325
    # Column clause
packaging one directory
yuki-kimoto authored on 2009-11-16
326
    if (@$columns) {
327
        foreach my $column (@$columns) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
328
            $source .= "$column, ";
packaging one directory
yuki-kimoto authored on 2009-11-16
329
        }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
330
        $source =~ s/, $/ /;
packaging one directory
yuki-kimoto authored on 2009-11-16
331
    }
332
    else {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
333
        $source .= '* ';
packaging one directory
yuki-kimoto authored on 2009-11-16
334
    }
335
    
added commit method
yuki-kimoto authored on 2010-05-27
336
    # Table
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
337
    $source .= 'from ';
packaging one directory
yuki-kimoto authored on 2009-11-16
338
    foreach my $table (@$tables) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
339
        $source .= "$table, ";
packaging one directory
yuki-kimoto authored on 2009-11-16
340
    }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
341
    $source =~ s/, $/ /;
packaging one directory
yuki-kimoto authored on 2009-11-16
342
    
added commit method
yuki-kimoto authored on 2010-05-27
343
    # Where clause
update document
yuki-kimoto authored on 2010-08-07
344
    my $param;
345
    if (ref $where eq 'HASH') {
346
        $param = $where;
347
        $source .= 'where (';
348
        foreach my $where_key (keys %$where) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
349
            $source .= "{= $where_key} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
350
        }
update document
yuki-kimoto authored on 2010-08-07
351
        $source =~ s/ and $//;
352
        $source .= ') ';
353
    }
354
    elsif (ref $where eq 'ARRAY') {
355
        my$where_str = $where->[0] || '';
356
        $param = $where->[1];
357
        
358
        $source .= "where ($where_str) ";
packaging one directory
yuki-kimoto authored on 2009-11-16
359
    }
360
    
added commit method
yuki-kimoto authored on 2010-05-27
361
    # Relation
362
    if ($relation) {
update document
yuki-kimoto authored on 2010-08-07
363
        $source .= $where ? "and " : "where ";
added commit method
yuki-kimoto authored on 2010-05-27
364
        foreach my $rkey (keys %$relation) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
365
            $source .= "$rkey = " . $relation->{$rkey} . " and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
366
        }
367
    }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
368
    $source =~ s/ and $//;
added commit method
yuki-kimoto authored on 2010-05-27
369
    
370
    # Append some statement
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
371
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
372
    
373
    # Execute query
update document
yuki-kimoto authored on 2010-08-07
374
    my $result = $self->execute($source, param  => $param, 
375
                                         filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
376
    
377
    return $result;
378
}
379

            
cleanup
yuki-kimoto authored on 2010-10-17
380
our %VALID_UPDATE_ARGS
381
  = map { $_ => 1 } qw/table param where append filter allow_update_all/;
382

            
383
sub update {
384
    my ($self, %args) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
385
    
cleanup
yuki-kimoto authored on 2010-10-17
386
    # Check arguments
387
    foreach my $name (keys %args) {
388
        croak qq{"$name" is invalid argument}
389
          unless $VALID_UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
390
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
391
    
cleanup
yuki-kimoto authored on 2010-10-17
392
    # Arguments
393
    my $table            = $args{table} || '';
394
    my $param            = $args{param} || {};
395
    my $where            = $args{where} || {};
396
    my $append = $args{append} || '';
397
    my $filter           = $args{filter};
398
    my $allow_update_all = $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
399
    
cleanup
yuki-kimoto authored on 2010-10-17
400
    # Update keys
401
    my @update_keys = keys %$param;
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
402
    
cleanup
yuki-kimoto authored on 2010-10-17
403
    # Where keys
404
    my @where_keys = keys %$where;
removed reconnect method
yuki-kimoto authored on 2010-05-28
405
    
cleanup
yuki-kimoto authored on 2010-10-17
406
    # Not exists where keys
407
    croak qq{"where" argument must be specified and } .
408
          qq{contains the pairs of column name and value}
409
      if !@where_keys && !$allow_update_all;
removed experimental registe...
yuki-kimoto authored on 2010-08-24
410
    
cleanup
yuki-kimoto authored on 2010-10-17
411
    # Update clause
412
    my $update_clause = '{update_param ' . join(' ', @update_keys) . '}';
removed experimental registe...
yuki-kimoto authored on 2010-08-24
413
    
cleanup
yuki-kimoto authored on 2010-10-17
414
    # Where clause
415
    my $where_clause = '';
416
    my $new_where = {};
removed reconnect method
yuki-kimoto authored on 2010-05-28
417
    
cleanup
yuki-kimoto authored on 2010-10-17
418
    if (@where_keys) {
419
        $where_clause = 'where ';
420
        $where_clause .= "{= $_} and " for @where_keys;
421
        $where_clause =~ s/ and $//;
removed reconnect method
yuki-kimoto authored on 2010-05-28
422
    }
423
    
cleanup
yuki-kimoto authored on 2010-10-17
424
    # Source of SQL
425
    my $source = "update $table $update_clause $where_clause";
426
    $source .= " $append" if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
427
    
cleanup
yuki-kimoto authored on 2010-10-17
428
    # Rearrange parameters
429
    foreach my $wkey (@where_keys) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
430
        
cleanup
yuki-kimoto authored on 2010-10-17
431
        if (exists $param->{$wkey}) {
432
            $param->{$wkey} = [$param->{$wkey}]
433
              unless ref $param->{$wkey} eq 'ARRAY';
434
            
435
            push @{$param->{$wkey}}, $where->{$wkey};
436
        }
437
        else {
438
            $param->{$wkey} = $where->{$wkey};
439
        }
removed reconnect method
yuki-kimoto authored on 2010-05-28
440
    }
cleanup
yuki-kimoto authored on 2010-10-17
441
    
442
    # Execute query
443
    my $ret_val = $self->execute($source, param  => $param, 
444
                                 filter => $filter);
445
    
446
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
447
}
448

            
cleanup
yuki-kimoto authored on 2010-10-17
449
sub update_all { shift->update(allow_update_all => 1, @_) };
450

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
451
sub _build_bind_values {
452
    my ($self, $query, $params, $filter) = @_;
453
    
454
    # binding values
455
    my @bind_values;
add tests
yuki-kimoto authored on 2010-08-08
456

            
457
    # Filter
458
    $filter ||= {};
459
    
460
    # Parameter
461
    $params ||= {};
462
    
463
    # Check filter
464
    $self->_check_filter($self->filters, $filter,
465
                         $self->default_bind_filter, $params)
466
      if $self->filter_check;
removed reconnect method
yuki-kimoto authored on 2010-05-28
467
    
468
    # Build bind values
469
    my $count = {};
470
    foreach my $column (@{$query->columns}) {
471
        
472
        # Value
473
        my $value = ref $params->{$column} eq 'ARRAY'
474
                  ? $params->{$column}->[$count->{$column} || 0]
475
                  : $params->{$column};
476
        
add tests
yuki-kimoto authored on 2010-08-10
477
        # Filtering
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
478
        my $fname = $filter->{$column} || $self->default_bind_filter || '';
add tests
yuki-kimoto authored on 2010-08-10
479
        my $filter_func = $fname ? $self->filters->{$fname} : undef;
removed reconnect method
yuki-kimoto authored on 2010-05-28
480
        push @bind_values, $filter_func
481
                         ? $filter_func->($value)
482
                         : $value;
483
        
484
        # Count up 
485
        $count->{$column}++;
486
    }
487
    
488
    return \@bind_values;
489
}
490

            
add tests
yuki-kimoto authored on 2010-08-08
491
sub _check_filter {
492
    my ($self, $filters, $filter, $default_filter, $params) = @_;
493
    
494
    # Filter name not exists
495
    foreach my $fname (values %$filter) {
496
        croak qq{Bind filter "$fname" is not registered}
497
          unless exists $filters->{$fname};
498
    }
499
    
500
    # Default filter name not exists
501
    croak qq{Default bind filter "$default_filter" is not registered}
502
      if $default_filter && ! exists $filters->{$default_filter};
503
    
504
    # Column name not exists
505
    foreach my $column (keys %$filter) {
506
        
507
        croak qq{Column name "$column" in bind filter is not found in paramters}
508
          unless exists $params->{$column};
509
    }
510
}
511

            
cleanup
yuki-kimoto authored on 2010-10-17
512
sub _croak {
513
    my ($self, $error, $append) = @_;
514
    $append ||= "";
515
    
516
    # Verbose
517
    if ($Carp::Verbose) { croak $error }
518
    
519
    # Not verbose
520
    else {
521
        
522
        # Remove line and module infromation
523
        my $at_pos = rindex($error, ' at ');
524
        $error = substr($error, 0, $at_pos);
525
        $error =~ s/\s+$//;
526
        
527
        croak "$error$append";
528
    }
529
}
530

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
533
=head1 NAME
534

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

            
537
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
538

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
547
    # Insert 
548
    $dbi->insert(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
549
                 param  => {title => 'Perl', author => 'Ken'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
550
                 filter => {title => 'encode_utf8'});
551
    
552
    # Update 
553
    $dbi->update(table  => 'books', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
554
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
555
                 where  => {id => 5},
556
                 filter => {title => 'encode_utf8'});
557
    
558
    # Update all
559
    $dbi->update_all(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
560
                     param  => {title => 'Perl'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
561
                     filter => {title => 'encode_utf8'});
562
    
563
    # Delete
564
    $dbi->delete(table  => 'books',
565
                 where  => {author => 'Ken'},
566
                 filter => {title => 'encode_utf8'});
567
    
568
    # Delete all
569
    $dbi->delete_all(table => 'books');
cleanup
yuki-kimoto authored on 2010-08-05
570

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
573
    # Select
574
    my $result = $dbi->select(table => 'books');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
575
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
576
    # Select, more complex
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
577
    my $result = $dbi->select(
update document
yuki-kimoto authored on 2010-05-27
578
        table  => 'books',
579
        column => [qw/author title/],
580
        where  => {author => 'Ken'},
updated document
yuki-kimoto authored on 2010-08-08
581
        append => 'order by id limit 5',
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
582
        filter => {title => 'encode_utf8'}
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
583
    );
added commit method
yuki-kimoto authored on 2010-05-27
584
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
585
    # Select, join table
added commit method
yuki-kimoto authored on 2010-05-27
586
    my $result = $dbi->select(
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
587
        table    => ['books', 'rental'],
588
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
589
        relation => {'books.id' => 'rental.book_id'}
590
    );
updated document
yuki-kimoto authored on 2010-08-08
591
    
592
    # Select, more flexible where
593
    my $result = $dbi->select(
594
        table  => 'books',
595
        where  => ['{= author} and {like title}', 
596
                   {author => 'Ken', title => '%Perl%'}]
597
    );
cleanup
yuki-kimoto authored on 2010-08-05
598

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
601
    # Execute SQL
removed register_format()
yuki-kimoto authored on 2010-05-26
602
    $dbi->execute("select title from books");
603
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
604
    # Execute SQL with hash binding and filtering
updated document
yuki-kimoto authored on 2010-08-08
605
    $dbi->execute("select id from books where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
606
                  param  => {author => 'ken', title => '%Perl%'},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
607
                  filter => {title => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
608

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

            
updated document
yuki-kimoto authored on 2010-08-08
615
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
616

            
removed register_format()
yuki-kimoto authored on 2010-05-26
617
    # Default filter
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
618
    $dbi->default_bind_filter('encode_utf8');
removed register_format()
yuki-kimoto authored on 2010-05-26
619
    $dbi->default_fetch_filter('decode_utf8');
cleanup
yuki-kimoto authored on 2010-08-05
620

            
621
    # Get DBI object
622
    my $dbh = $dbi->dbh;
623

            
624
Fetch row.
625

            
removed register_format()
yuki-kimoto authored on 2010-05-26
626
    # Fetch
627
    while (my $row = $result->fetch) {
628
        # ...
629
    }
630
    
631
    # Fetch hash
632
    while (my $row = $result->fetch_hash) {
633
        
634
    }
635
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
636
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
637

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

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

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

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

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

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

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

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

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

            
672
    use DBIx::Custom::SQLite;
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
673
    my $dbi = DBIx::Custom::SQLite->connect(database => 'dbname');
update document
yuki-kimoto authored on 2010-08-07
674
    
cleanup
yuki-kimoto authored on 2010-08-09
675
If database is  MySQL, use L<DBIx::Custom::MySQL>.
update document
yuki-kimoto authored on 2010-08-07
676

            
677
    use DBIx::Custom::MySQL;
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
678
    my $dbi = DBIx::Custom::MySQL->connect(
679
        database => 'dbname',
680
        user     => 'ken',
681
        password => '!LFKD%$&'
682
    );
update document
yuki-kimoto authored on 2010-08-07
683

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

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

            
update document
yuki-kimoto authored on 2010-08-07
690
=head3 insert()
691

            
updated document
yuki-kimoto authored on 2010-08-08
692
Execute insert statement.
693

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

            
697
The following SQL is executed.
698

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

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

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

            
706
=head3 update()
707

            
updated document
yuki-kimoto authored on 2010-08-08
708
Execute update statement.
709

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

            
714
The following SQL is executed.
715

            
716
    update books set title = ?, author = ?;
717

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

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

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

            
725
=head3 delete()
726

            
updated document
yuki-kimoto authored on 2010-08-08
727
Execute delete statement.
728

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

            
732
The following SQL is executed.
733

            
734
    delete from books where id = ?;
735

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

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

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

            
743
=head3 select()
744

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

            
747
    my $result = $dbi->select(table => 'books');
748

            
749
The following SQL is executed.
750

            
751
    select * from books;
752

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

            
756
    while (my $row = $result->fetch) {
757
        my $title  = $row->[0];
758
        my $author = $row->[1];
759
    }
760

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

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

            
766
    my $result = $dbi->select(
767
        table  => 'books',
768
        column => [qw/author title/],
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
769
        where  => {author => 'Ken'}
770
    );
update document
yuki-kimoto authored on 2010-08-07
771

            
772
The following SQL is executed.
773

            
774
    select author, title from books where author = ?;
775

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

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

            
780
    my $result = $dbi->select(
781
        table    => ['books', 'rental'],
782
        column   => ['books.name as book_name']
783
        relation => {'books.id' => 'rental.book_id'}
784
    );
785

            
786
The following SQL is executed.
787

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

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

            
794
    my $result = $dbi->select(
795
        table  => 'books',
796
        where  => {author => 'Ken'},
797
        append => 'order by price limit 5',
798
    );
799

            
800
The following SQL is executed.
801

            
802
    select * books where author = ? order by price limit 5;
803

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

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

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

            
814
Fetch row into array.
815
    
816
    while (my $row = $result->fetch) {
817
        my $author = $row->[0];
818
        my $title  = $row->[1];
819
        
820
    }
821

            
822
Fetch only a first row into array.
823

            
824
    my $row = $result->fetch_first;
825

            
826
Fetch multiple rows into array of array.
827

            
828
    while (my $rows = $result->fetch_multi(5)) {
829
        my $first_author  = $rows->[0][0];
830
        my $first_title   = $rows->[0][1];
831
        my $second_author = $rows->[1][0];
832
        my $second_value  = $rows->[1][1];
833
    
834
    }
835
    
836
Fetch all rows into array of array.
837

            
838
    my $rows = $result->fetch_all;
839

            
840
Fetch row into hash.
841

            
842
    # Fetch a row into hash
843
    while (my $row = $result->fetch_hash) {
844
        my $title  = $row->{title};
845
        my $author = $row->{author};
846
        
847
    }
848

            
849
Fetch only a first row into hash
850

            
851
    my $row = $result->fetch_hash_first;
852
    
853
Fetch multiple rows into array of hash
854

            
855
    while (my $rows = $result->fetch_hash_multi(5)) {
856
        my $first_title   = $rows->[0]{title};
857
        my $first_author  = $rows->[0]{author};
858
        my $second_title  = $rows->[1]{title};
859
        my $second_author = $rows->[1]{author};
860
    
861
    }
862
    
863
Fetch all rows into array of hash
864

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

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

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

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

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

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

            
877
    use DBI;
878
    my $dbh = DBI->connect(...);
879
    my $sth = $dbh->prepare(
880
        "select * from books where author = ? and title like ?;"
881
    );
882
    $sth->execute('Ken', '%Perl%');
883

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

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

            
890
    my $result = $dbi->execute(
891
        "select * from books where {= author} and {like title};"
892
        param => {author => 'Ken', title => '%Perl%'}
893
    );
894

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

            
899
    select * from books where {= author} and {like title}
900
      -> select * from books where author = ? and title like ?;
901

            
updated document
yuki-kimoto authored on 2010-08-08
902
The following tags is available.
903

            
904
    [TAG]                       [REPLACED]
905
    {? NAME}               ->   ?
906
    {= NAME}               ->   NAME = ?
907
    {<> NAME}              ->   NAME <> ?
908
    
909
    {< NAME}               ->   NAME < ?
910
    {> NAME}               ->   NAME > ?
911
    {>= NAME}              ->   NAME >= ?
912
    {<= NAME}              ->   NAME <= ?
913
    
914
    {like NAME}            ->   NAME like ?
915
    {in NAME COUNT}        ->   NAME in [?, ?, ..]
916
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
917
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
918
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
updated document
yuki-kimoto authored on 2010-08-08
919

            
920
See also L<DBIx::Custom::QueryBuilder>.
921

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

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

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

            
930
Usually, Perl string is kept as internal string.
931
If you want to save the string to database, You must encode the string.
932
Filtering system help you to convert a data to another data
933
when you save to the data and get the data form database.
934

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

            
937
    $dbi->register_filter(
938
        to_upper_case => sub {
939
            my $value = shift;
940
            return uc $value;
941
        }
942
    );
943

            
944
C<encode_utf8> and C<decode_utf8> filter is registerd by default.
945

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

            
948
    my $result = $dbi->execute(
949
        "select * from books where {= author} and {like title};"
updated document
yuki-kimoto authored on 2010-08-08
950
        param  => {author => 'Ken', title => '%Perl%'},
update document
yuki-kimoto authored on 2010-08-07
951
        filter => {author => 'to_upper_case, title => 'encode_utf8'}
952
    );
953

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

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
958
    # insert(), having filter argument
update document
yuki-kimoto authored on 2010-08-07
959
    $dbi->insert(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
960
                 param  => {title => 'Perl', author => 'Ken'},
update document
yuki-kimoto authored on 2010-08-07
961
                 filter => {title => 'encode_utf8'});
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
962
    
963
    # select(), having filter argument
update document
yuki-kimoto authored on 2010-08-07
964
    my $result = $dbi->select(
965
        table  => 'books',
966
        column => [qw/author title/],
967
        where  => {author => 'Ken'},
968
        append => 'order by id limit 1',
969
        filter => {title => 'encode_utf8'}
970
    );
971

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

            
974
    $dbi->default_bind_filter('encode_utf8');
975

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
976
C<filter()> argument overwrites this default filter.
update document
yuki-kimoto authored on 2010-08-07
977
    
978
    $dbi->default_bind_filter('encode_utf8');
979
    $dbi->insert(
980
        table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
981
        param  => {title => 'Perl', author => 'Ken', price => 1000},
update document
yuki-kimoto authored on 2010-08-07
982
        filter => {author => 'to_upper_case', price => undef}
983
    );
984

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

            
987
    $dbi->insert(
988
        table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
989
        param  => {title => 'Perl', author => 'Ken', price => 1000},
update document
yuki-kimoto authored on 2010-08-07
990
        filter => {title => 'encode_uft8' author => 'to_upper_case'}
991
    );
992

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

            
995
    my $result = $dbi->select(table => 'books');
996
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
997

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

            
1001
    $dbi->default_fetch_filter('decode_utf8');
1002

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

            
1006
    $dbi->default_fetch_filter('decode_utf8');
1007
    my $result = $dbi->select(
1008
        table => 'books',
1009
        columns => ['title', 'author', 'price']
1010
    );
1011
    $result->filter({author => 'to_upper_case', price => undef});
1012

            
1013
This is same as the following one.
1014

            
1015
    my $result = $dbi->select(
1016
        table => 'books',
1017
        columns => ['title', 'author', 'price']
1018
    );
1019
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
1020

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

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

            
updated document
yuki-kimoto authored on 2010-08-08
1027
=head3 Disable filter checking
1028

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

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

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

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

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

            
1043
C<insert()> method is a little slow because SQL statement and statement handle
1044
is created every time.
1045

            
1046
In that case, you can prepare a query by C<create_query()> method.
1047
    
1048
    my $query = $dbi->create_query(
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1049
        "insert into books {insert_param title author};"
updated document
yuki-kimoto authored on 2010-08-08
1050
    );
cleanup
yuki-kimoto authored on 2010-08-09
1051

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

            
1055
    {
1056
        sql     => 'insert into books (title, author) values (?, ?);',
1057
        columns => ['title', 'author']
1058
    }
1059

            
1060
Execute query repeatedly.
updated document
yuki-kimoto authored on 2010-08-08
1061
    
1062
    my $inputs = [
1063
        {title => 'Perl',      author => 'Ken'},
1064
        {title => 'Good days', author => 'Mike'}
1065
    ];
1066
    
1067
    foreach my $input (@$inputs) {
1068
        $dbi->execute($query, $input);
1069
    }
1070

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1073
=head3 caching
updated document
yuki-kimoto authored on 2010-08-08
1074

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

            
1078
    $dbi->cache(1);
1079

            
1080
Caching is on memory, but you can change this by C<cache_method()>.
1081
First argument is L<DBIx::Custom> object.
cleanup
yuki-kimoto authored on 2010-08-09
1082
Second argument is a source of SQL,
1083
such as "select * from books where {= title} and {= author};";
updated document
yuki-kimoto authored on 2010-08-08
1084
Third argument is parsed result, such as
1085
{sql => "select * from books where title = ? and author = ?",
1086
 columns => ['title', 'author']}, this is hash reference.
cleanup
yuki-kimoto authored on 2010-08-09
1087
If arguments is more than two, this method is called to set cache.
1088
If not, this method is called to get cache.
updated document
yuki-kimoto authored on 2010-08-08
1089

            
cleanup
yuki-kimoto authored on 2010-08-09
1090
    $dbi->cache_method(sub {
updated document
yuki-kimoto authored on 2010-08-08
1091
        sub {
1092
            my $self = shift;
1093
            
1094
            $self->{_cached} ||= {};
1095
            
1096
            # Set cache
1097
            if (@_ > 1) {
1098
                $self->{_cached}{$_[0]} = $_[1] 
1099
            }
1100
            
1101
            # Get cache
1102
            else {
1103
                return $self->{_cached}{$_[0]}
1104
            }
1105
        }
1106
    });
1107

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

            
1110
=head3 Get DBI object
1111

            
1112
You can get L<DBI> object and call any method of L<DBI>.
1113

            
1114
    $dbi->dbh->begin_work;
1115
    $dbi->dbh->commit;
1116
    $dbi->dbh->rollback;
1117

            
1118
=head3 Change Result class
1119

            
1120
You can change Result class if you need.
1121

            
1122
    package Your::Result;
1123
    use base 'DBIx::Custom::Result';
1124
    
1125
    sub some_method { ... }
1126

            
1127
    1;
1128
    
1129
    package main;
1130
    
1131
    use Your::Result;
1132
    
1133
    my $dbi = DBIx::Custom->connect(...);
1134
    $dbi->result_class('Your::Result');
1135

            
1136
=head3 Custamize SQL builder object
1137

            
1138
You can custamize SQL builder object
1139

            
1140
    my $dbi = DBIx::Custom->connect(...);
1141
    $dbi->query_builder->register_tag_processor(
1142
        name => sub {
1143
           ...
1144
        }
1145
    );
1146

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1149
=head2 C<cache>
packaging one directory
yuki-kimoto authored on 2009-11-16
1150

            
cleanup
yuki-kimoto authored on 2010-10-17
1151
    my $cache = $dbi->cache;
1152
    $dbi      = $dbi->cache(1);
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1153

            
cleanup
yuki-kimoto authored on 2010-10-17
1154
Enable parsed L<DBIx::Custom::Query> object caching.
1155
Default to 1.
packaging one directory
yuki-kimoto authored on 2009-11-16
1156

            
cleanup
yuki-kimoto authored on 2010-10-17
1157
=head2 C<cache_method>
packaging one directory
yuki-kimoto authored on 2009-11-16
1158

            
cleanup
yuki-kimoto authored on 2010-10-17
1159
    $dbi          = $dbi->cache_method(\&cache_method);
1160
    $cache_method = $dbi->cache_method
1161

            
1162
Method to set and get caches.
1163

            
1164
B<Example:>
1165

            
1166
    $dbi->cache_method(
1167
        sub {
1168
            my $self = shift;
1169
            
1170
            $self->{_cached} ||= {};
1171
            
1172
            if (@_ > 1) {
1173
                $self->{_cached}{$_[0]} = $_[1] 
1174
            }
1175
            else {
1176
                return $self->{_cached}{$_[0]}
1177
            }
1178
        }
1179
    );
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1180

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

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

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1210
=head2 C<filters>
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1211

            
cleanup
yuki-kimoto authored on 2010-10-17
1212
    my $filters = $dbi->filters;
1213
    $dbi        = $dbi->filters(\%filters);
packaging one directory
yuki-kimoto authored on 2009-11-16
1214

            
cleanup
yuki-kimoto authored on 2010-10-17
1215
Filter functions.
1216
"encode_utf8" and "decode_utf8" is registered by default.
1217

            
1218
=head2 C<filter_check>
1219

            
1220
    my $filter_check = $dbi->filter_check;
1221
    $dbi             = $dbi->filter_check(0);
1222

            
1223
Enable filter check. 
1224
Default to 1.
1225
This check maybe damege performance.
1226
If you require performance, set C<filter_check> attribute to 0.
1227

            
1228
=head2 C<password>
1229

            
1230
    my $password = $dbi->password;
1231
    $dbi         = $dbi->password('lkj&le`@s');
1232

            
1233
Password.
1234
C<connect()> method use this value to connect the database.
update document
yuki-kimoto authored on 2010-01-30
1235

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1245
=head2 C<result_class>
cleanup
yuki-kimoto authored on 2010-08-05
1246

            
cleanup
yuki-kimoto authored on 2010-10-17
1247
    my $result_class = $dbi->result_class;
1248
    $dbi             = $dbi->result_class('DBIx::Custom::Result');
cleanup
yuki-kimoto authored on 2010-08-05
1249

            
cleanup
yuki-kimoto authored on 2010-10-17
1250
Result class for select statement.
1251
Default to L<DBIx::Custom::Result>.
cleanup
yuki-kimoto authored on 2010-08-05
1252

            
cleanup
yuki-kimoto authored on 2010-10-17
1253
=head2 C<user>
cleanup
yuki-kimoto authored on 2010-08-05
1254

            
cleanup
yuki-kimoto authored on 2010-10-17
1255
    my $user = $dbi->user;
1256
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1257

            
cleanup
yuki-kimoto authored on 2010-10-17
1258
User name.
1259
C<connect()> method use this value to connect the database.
1260
    
1261
=head1 METHODS
added commit method
yuki-kimoto authored on 2010-05-27
1262

            
cleanup
yuki-kimoto authored on 2010-10-17
1263
L<DBIx::Custom> inherits all methods from L<Object::Simple>
1264
and implements the following new ones.
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1265

            
cleanup
yuki-kimoto authored on 2010-10-17
1266
=head2 begin_work
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1267

            
cleanup
yuki-kimoto authored on 2010-10-17
1268
    $dbi->begin_work;
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1269

            
cleanup
yuki-kimoto authored on 2010-10-17
1270
Start transaction.
1271
This is same as L<DBI>'s C<begin_work>.
added commit method
yuki-kimoto authored on 2010-05-27
1272

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1276
=head2 commit
1277

            
1278
    $dbi->commit;
1279

            
1280
Commit transaction.
1281
This is same as L<DBI>'s C<commit>.
1282

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1293
=head2 C<create_query>
1294
    
1295
    my $query = $dbi->create_query(
1296
        "select * from books where {= author} and {like title};"
1297
    );
update document
yuki-kimoto authored on 2009-11-19
1298

            
cleanup
yuki-kimoto authored on 2010-10-17
1299
Create the instance of L<DBIx::Custom::Query> from the source of SQL.
1300
If you want to get high performance,
1301
use C<create_query()> method and execute it by C<execute()> method
1302
instead of suger methods.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1303

            
cleanup
yuki-kimoto authored on 2010-10-17
1304
    $dbi->execute($query, {author => 'Ken', title => '%Perl%'});
version 0.0901
yuki-kimoto authored on 2009-12-17
1305

            
cleanup
yuki-kimoto authored on 2010-10-17
1306
=head2 C<execute>
packaging one directory
yuki-kimoto authored on 2009-11-16
1307

            
cleanup
yuki-kimoto authored on 2010-10-17
1308
    my $result = $dbi->execute($query,  param => $params, filter => \%filter);
1309
    my $result = $dbi->execute($source, param => $params, filter => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1310

            
cleanup
yuki-kimoto authored on 2010-10-17
1311
Execute query or the source of SQL.
1312
Query is L<DBIx::Custom::Query> object.
1313
Return value is L<DBIx::Custom::Result> if select statement is executed,
1314
or the count of affected rows if insert, update, delete statement is executed.
version 0.0901
yuki-kimoto authored on 2009-12-17
1315

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1318
    my $result = $dbi->execute(
1319
        "select * from books where {= author} and {like title}", 
1320
        param => {author => 'Ken', title => '%Perl%'}
1321
    );
1322
    
1323
    while (my $row = $result->fetch) {
1324
        my $author = $row->[0];
1325
        my $title  = $row->[1];
1326
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
1327

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

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

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

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

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

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

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

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

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

            
added helper method
yuki-kimoto authored on 2010-10-17
1363
=head2 C<(experimental) helper>
1364

            
1365
    $dbi->helper(
1366
        update_or_insert => sub {
1367
            my $self = shift;
1368
            # do something
1369
        },
1370
        find_or_create   => sub {
1371
            my $self = shift;
1372
            # do something
1373
        }
1374
    );
1375

            
1376
Register helper methods. These method is called from L<DBIx::Custom> object directory.
1377

            
1378
    $dbi->update_or_insert;
1379
    $dbi->find_or_create;
1380

            
cleanup
yuki-kimoto authored on 2010-10-17
1381
=head2 C<insert>
1382

            
1383
    $dbi->insert(table  => $table, 
1384
                 param  => \%param,
1385
                 append => $append,
1386
                 filter => \%filter);
1387

            
1388
Execute insert statement.
1389
C<insert> method have C<table>, C<param>, C<append>
1390
and C<filter> arguments.
1391
C<table> is a table name.
1392
C<param> is the pairs of column name value. this must be hash reference.
1393
C<append> is a string added at the end of the SQL statement.
1394
C<filter> is filters when parameter binding is executed.
1395
This is overwrites C<default_bind_filter>.
1396
Return value of C<insert()> is the count of affected rows.
1397

            
1398
B<Example:>
1399

            
1400
    $dbi->insert(table  => 'books', 
1401
                 param  => {title => 'Perl', author => 'Taro'},
1402
                 append => "some statement",
1403
                 filter => {title => 'encode_utf8'})
1404

            
1405
=head2 C<register_filter>
1406

            
1407
    $dbi->register_filter(%filters);
1408
    $dbi->register_filter(\%filters);
1409
    
1410
Register filter. Registered filters is available in the following attributes
1411
or arguments.
1412

            
1413
=over 4
1414

            
1415
=item *
1416

            
1417
C<default_bind_filter>, C<default_fetch_filter>
1418

            
1419
=item *
1420

            
1421
C<filter> argument of C<insert()>, C<update()>,
1422
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1423
methods
1424

            
1425
=item *
1426

            
1427
C<execute()> method
1428

            
1429
=item *
1430

            
1431
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1432

            
1433
=item *
1434

            
1435
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1436

            
1437
=back
1438

            
1439
B<Example:>
1440

            
1441
    $dbi->register_filter(
1442
        encode_utf8 => sub {
1443
            my $value = shift;
1444
            
1445
            require Encode;
1446
            
1447
            return Encode::encode('UTF-8', $value);
1448
        },
1449
        decode_utf8 => sub {
1450
            my $value = shift;
1451
            
1452
            require Encode;
1453
            
1454
            return Encode::decode('UTF-8', $value)
1455
        }
1456
    );
1457

            
1458
=head2 rollback
1459

            
1460
    $dbi->rollback;
1461

            
1462
Rollback transaction.
1463
This is same as L<DBI>'s C<rollback>.
1464

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1465
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1466
    
cleanup
yuki-kimoto authored on 2010-08-05
1467
    my $result = $dbi->select(table    => $table,
1468
                              column   => [@column],
1469
                              where    => \%where,
1470
                              append   => $append,
1471
                              relation => \%relation,
1472
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1473

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1474
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1475
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1476
C<relation> and C<filter> arguments.
1477
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1478
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1479
C<append> is a string added at the end of the SQL statement.
1480
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
1481

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

            
added commit method
yuki-kimoto authored on 2010-05-27
1484
    # select * from books;
cleanup
yuki-kimoto authored on 2010-08-05
1485
    my $result = $dbi->select(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
1486
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1487
    # select * from books where title = ?;
1488
    my $result = $dbi->select(table => 'books', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1489
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1490
    # select title, author from books where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1491
    my $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
1492
        table  => 'books',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1493
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1494
        where  => {id => 1},
1495
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1496
    );
1497
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1498
    # select books.name as book_name from books, rental
added commit method
yuki-kimoto authored on 2010-05-27
1499
    # where books.id = rental.book_id;
1500
    my $result = $dbi->select(
removed reconnect method
yuki-kimoto authored on 2010-05-28
1501
        table    => ['books', 'rental'],
1502
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
1503
        relation => {'books.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1504
    );
1505

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

            
1509
    my $result = $dbi->select(
1510
        table  => 'books',
1511
        column => ['title', 'author'],
1512
        where  => ['{= title} or {like author}',
1513
                   {title => '%Perl%', author => 'Ken'}]
1514
    );
1515

            
1516
First element is a string. it contains tags,
1517
such as "{= title} or {like author}".
1518
Second element is paramters.
1519

            
cleanup
yuki-kimoto authored on 2010-10-17
1520
=head2 C<update>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1521

            
cleanup
yuki-kimoto authored on 2010-10-17
1522
    $dbi->update(table  => $table, 
1523
                 param  => \%params,
1524
                 where  => \%where,
1525
                 append => $append,
1526
                 filter => \%filter)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1527

            
cleanup
yuki-kimoto authored on 2010-10-17
1528
Execute update statement.
1529
C<update> method have C<table>, C<param>, C<where>, C<append>
1530
and C<filter> arguments.
1531
C<table> is a table name.
1532
C<param> is column-value pairs. this must be hash reference.
1533
C<where> is where clause. this must be hash reference.
1534
C<append> is a string added at the end of the SQL statement.
1535
C<filter> is filters when parameter binding is executed.
1536
This is overwrites C<default_bind_filter>.
1537
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1538

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1541
    $dbi->update(table  => 'books',
1542
                 param  => {title => 'Perl', author => 'Taro'},
1543
                 where  => {id => 5},
1544
                 append => "some statement",
1545
                 filter => {title => 'encode_utf8'});
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1546

            
cleanup
yuki-kimoto authored on 2010-10-17
1547
=head2 C<update_all>
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1548

            
cleanup
yuki-kimoto authored on 2010-10-17
1549
    $dbi->update_all(table  => $table, 
1550
                     param  => \%params,
1551
                     filter => \%filter,
1552
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1553

            
cleanup
yuki-kimoto authored on 2010-10-17
1554
Execute update statement to update all rows.
1555
Arguments is same as C<update> method,
1556
except that C<update_all> don't have C<where> argument.
1557
Return value of C<update_all()> is the count of affected rows.
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1558

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1561
    $dbi->update_all(table  => 'books', 
1562
                     param  => {author => 'taro'},
1563
                     filter => {author => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
1564

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

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

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

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

            
1573
C<< <kimoto.yuki at gmail.com> >>
1574

            
1575
L<http://github.com/yuki-kimoto/DBIx-Custom>
1576

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1577
=head1 AUTHOR
1578

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

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

            
1583
Copyright 2009 Yuki Kimoto, all rights reserved.
1584

            
1585
This program is free software; you can redistribute it and/or modify it
1586
under the same terms as Perl itself.
1587

            
1588
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1589

            
1590