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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
62
sub connect {
removed register_format()
yuki-kimoto authored on 2010-05-26
63
    my $proto = shift;
64
    
65
    # Create
66
    my $self = ref $proto ? $proto : $proto->new(@_);
update document
yuki-kimoto authored on 2010-01-30
67
    
68
    # Information
packaging one directory
yuki-kimoto authored on 2009-11-16
69
    my $data_source = $self->data_source;
70
    my $user        = $self->user;
71
    my $password    = $self->password;
72
    
removed experimental registe...
yuki-kimoto authored on 2010-08-24
73
    
update document
yuki-kimoto authored on 2010-01-30
74
    # Connect
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
75
    my $dbh = eval {DBI->connect(
packaging one directory
yuki-kimoto authored on 2009-11-16
76
        $data_source,
77
        $user,
78
        $password,
79
        {
80
            RaiseError => 1,
81
            PrintError => 0,
82
            AutoCommit => 1,
83
        }
84
    )};
85
    
update document
yuki-kimoto authored on 2010-01-30
86
    # Connect error
packaging one directory
yuki-kimoto authored on 2009-11-16
87
    croak $@ if $@;
88
    
update document
yuki-kimoto authored on 2010-01-30
89
    # Database handle
packaging one directory
yuki-kimoto authored on 2009-11-16
90
    $self->dbh($dbh);
update document
yuki-kimoto authored on 2010-01-30
91
    
packaging one directory
yuki-kimoto authored on 2009-11-16
92
    return $self;
93
}
94

            
cleanup
yuki-kimoto authored on 2010-10-17
95
sub create_query {
96
    my ($self, $source) = @_;
update document
yuki-kimoto authored on 2010-01-30
97
    
cleanup
yuki-kimoto authored on 2010-10-17
98
    # Cache
99
    my $cache = $self->cache;
update document
yuki-kimoto authored on 2010-01-30
100
    
cleanup
yuki-kimoto authored on 2010-10-17
101
    # Create query
102
    my $query;
103
    if ($cache) {
104
        
105
        # Get query
106
        my $q = $self->cache_method->($self, $source);
107
        
108
        # Create query
109
        $query = DBIx::Custom::Query->new($q) if $q;
110
    }
111
    
112
    unless ($query) {
cleanup insert
yuki-kimoto authored on 2010-04-28
113

            
cleanup
yuki-kimoto authored on 2010-10-17
114
        # Create SQL object
115
        my $builder = $self->query_builder;
116
        
117
        # Create query
118
        $query = $builder->build_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
119

            
cleanup
yuki-kimoto authored on 2010-10-17
120
        # Cache query
121
        $self->cache_method->($self, $source,
122
                             {sql     => $query->sql, 
123
                              columns => $query->columns})
124
          if $cache;
cleanup insert
yuki-kimoto authored on 2010-04-28
125
    }
126
    
cleanup
yuki-kimoto authored on 2010-10-17
127
    # Prepare statement handle
128
    my $sth;
129
    eval { $sth = $self->dbh->prepare($query->{sql})};
130
    $self->_croak($@, qq{. SQL: "$query->{sql}"}) if $@;
packaging one directory
yuki-kimoto authored on 2009-11-16
131
    
cleanup
yuki-kimoto authored on 2010-10-17
132
    # Set statement handle
133
    $query->sth($sth);
packaging one directory
yuki-kimoto authored on 2009-11-16
134
    
cleanup
yuki-kimoto authored on 2010-10-17
135
    return $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
136
}
137

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

            
cleanup
yuki-kimoto authored on 2010-10-17
141
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
142
    my ($self, %args) = @_;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
143
    
144
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
145
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
146
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
147
          unless $VALID_DELETE_ARGS{$name};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
148
    }
149
    
150
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
151
    my $table            = $args{table} || '';
152
    my $where            = $args{where} || {};
cleanup
yuki-kimoto authored on 2010-10-17
153
    my $append = $args{append};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
154
    my $filter           = $args{filter};
cleanup
yuki-kimoto authored on 2010-10-17
155
    my $allow_delete_all = $args{allow_delete_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
156
    
157
    # Where keys
removed register_format()
yuki-kimoto authored on 2010-05-26
158
    my @where_keys = keys %$where;
packaging one directory
yuki-kimoto authored on 2009-11-16
159
    
160
    # Not exists where keys
add tests
yuki-kimoto authored on 2010-08-10
161
    croak qq{"where" argument must be specified and } .
162
          qq{contains the pairs of column name and value}
cleanup
yuki-kimoto authored on 2010-10-17
163
      if !@where_keys && !$allow_delete_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
164
    
165
    # Where clause
166
    my $where_clause = '';
167
    if (@where_keys) {
168
        $where_clause = 'where ';
add tests
yuki-kimoto authored on 2010-08-10
169
        $where_clause .= "{= $_} and " for @where_keys;
packaging one directory
yuki-kimoto authored on 2009-11-16
170
        $where_clause =~ s/ and $//;
171
    }
172
    
add tests
yuki-kimoto authored on 2010-08-10
173
    # Source of SQL
cleanup
yuki-kimoto authored on 2010-10-17
174
    my $source = "delete from $table $where_clause";
add tests
yuki-kimoto authored on 2010-08-10
175
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
176
    
177
    # Execute query
cleanup
yuki-kimoto authored on 2010-10-17
178
    my $ret_val = $self->execute($source, param  => $where, 
add tests
yuki-kimoto authored on 2010-08-10
179
                                 filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
180
    
181
    return $ret_val;
182
}
183

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
188
sub execute{
189
    my ($self, $query, %args)  = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
190
    
191
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
192
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
193
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
194
          unless $VALID_EXECUTE_ARGS{$name};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
195
    }
196
    
cleanup
yuki-kimoto authored on 2010-10-17
197
    my $params = $args{param} || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
198
    
cleanup
yuki-kimoto authored on 2010-10-17
199
    # First argument is the soruce of SQL
200
    $query = $self->create_query($query)
201
      unless ref $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
202
    
cleanup
yuki-kimoto authored on 2010-10-17
203
    my $filter = $args{filter} || $query->filter || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
204
    
cleanup
yuki-kimoto authored on 2010-10-17
205
    # Create bind value
206
    my $bind_values = $self->_build_bind_values($query, $params, $filter);
207
    
208
    # Execute
209
    my $sth      = $query->sth;
210
    my $affected;
211
    eval {$affected = $sth->execute(@$bind_values)};
212
    $self->_croak($@) if $@;
213
    
214
    # Return resultset if select statement is executed
215
    if ($sth->{NUM_OF_FIELDS}) {
216
        
217
        # Create result
218
        my $result = $self->result_class->new(
219
            sth            => $sth,
220
            default_filter => $self->default_fetch_filter,
221
            filters        => $self->filters,
222
            filter_check   => $self->filter_check
223
        );
224

            
225
        return $result;
226
    }
227
    return $affected;
228
}
229

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

            
232
sub insert {
233
    my ($self, %args) = @_;
234

            
235
    # Check arguments
236
    foreach my $name (keys %args) {
237
        croak qq{"$name" is invalid argument}
238
          unless $VALID_INSERT_ARGS{$name};
packaging one directory
yuki-kimoto authored on 2009-11-16
239
    }
240
    
cleanup
yuki-kimoto authored on 2010-10-17
241
    # Arguments
242
    my $table  = $args{table} || '';
243
    my $param  = $args{param} || {};
244
    my $append = $args{append} || '';
245
    my $filter = $args{filter};
246
    
247
    # Insert keys
248
    my @insert_keys = keys %$param;
249
    
250
    # Templte for insert
251
    my $source = "insert into $table {insert_param "
252
               . join(' ', @insert_keys) . '}';
add tests
yuki-kimoto authored on 2010-08-10
253
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
254
    
255
    # Execute query
cleanup
yuki-kimoto authored on 2010-10-17
256
    my $ret_val = $self->execute($source, param  => $param, 
257
                                          filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
258
    
259
    return $ret_val;
260
}
261

            
cleanup
yuki-kimoto authored on 2010-10-17
262
sub register_filter {
263
    my $invocant = shift;
264
    
265
    # Register filter
266
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
267
    $invocant->filters({%{$invocant->filters}, %$filters});
268
    
269
    return $invocant;
270
}
packaging one directory
yuki-kimoto authored on 2009-11-16
271

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
275
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
276
    my ($self, %args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
277
    
refactoring select
yuki-kimoto authored on 2010-04-28
278
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
279
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
280
        croak qq{"$name" is invalid argument}
refactoring select
yuki-kimoto authored on 2010-04-28
281
          unless $VALID_SELECT_ARGS{$name};
282
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
283
    
refactoring select
yuki-kimoto authored on 2010-04-28
284
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
285
    my $tables = $args{table} || [];
removed register_format()
yuki-kimoto authored on 2010-05-26
286
    $tables = [$tables] unless ref $tables eq 'ARRAY';
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
287
    my $columns  = $args{column} || [];
update document
yuki-kimoto authored on 2010-08-07
288
    my $where    = $args{where};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
289
    my $relation = $args{relation};
290
    my $append   = $args{append};
291
    my $filter   = $args{filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
292
    
add tests
yuki-kimoto authored on 2010-08-10
293
    # Source of SQL
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
294
    my $source = 'select ';
packaging one directory
yuki-kimoto authored on 2009-11-16
295
    
added commit method
yuki-kimoto authored on 2010-05-27
296
    # Column clause
packaging one directory
yuki-kimoto authored on 2009-11-16
297
    if (@$columns) {
298
        foreach my $column (@$columns) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
299
            $source .= "$column, ";
packaging one directory
yuki-kimoto authored on 2009-11-16
300
        }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
301
        $source =~ s/, $/ /;
packaging one directory
yuki-kimoto authored on 2009-11-16
302
    }
303
    else {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
304
        $source .= '* ';
packaging one directory
yuki-kimoto authored on 2009-11-16
305
    }
306
    
added commit method
yuki-kimoto authored on 2010-05-27
307
    # Table
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
308
    $source .= 'from ';
packaging one directory
yuki-kimoto authored on 2009-11-16
309
    foreach my $table (@$tables) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
310
        $source .= "$table, ";
packaging one directory
yuki-kimoto authored on 2009-11-16
311
    }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
312
    $source =~ s/, $/ /;
packaging one directory
yuki-kimoto authored on 2009-11-16
313
    
added commit method
yuki-kimoto authored on 2010-05-27
314
    # Where clause
update document
yuki-kimoto authored on 2010-08-07
315
    my $param;
316
    if (ref $where eq 'HASH') {
317
        $param = $where;
318
        $source .= 'where (';
319
        foreach my $where_key (keys %$where) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
320
            $source .= "{= $where_key} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
321
        }
update document
yuki-kimoto authored on 2010-08-07
322
        $source =~ s/ and $//;
323
        $source .= ') ';
324
    }
325
    elsif (ref $where eq 'ARRAY') {
326
        my$where_str = $where->[0] || '';
327
        $param = $where->[1];
328
        
329
        $source .= "where ($where_str) ";
packaging one directory
yuki-kimoto authored on 2009-11-16
330
    }
331
    
added commit method
yuki-kimoto authored on 2010-05-27
332
    # Relation
333
    if ($relation) {
update document
yuki-kimoto authored on 2010-08-07
334
        $source .= $where ? "and " : "where ";
added commit method
yuki-kimoto authored on 2010-05-27
335
        foreach my $rkey (keys %$relation) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
336
            $source .= "$rkey = " . $relation->{$rkey} . " and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
337
        }
338
    }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
339
    $source =~ s/ and $//;
added commit method
yuki-kimoto authored on 2010-05-27
340
    
341
    # Append some statement
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
342
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
343
    
344
    # Execute query
update document
yuki-kimoto authored on 2010-08-07
345
    my $result = $self->execute($source, param  => $param, 
346
                                         filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
347
    
348
    return $result;
349
}
350

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

            
354
sub update {
355
    my ($self, %args) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
356
    
cleanup
yuki-kimoto authored on 2010-10-17
357
    # Check arguments
358
    foreach my $name (keys %args) {
359
        croak qq{"$name" is invalid argument}
360
          unless $VALID_UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
361
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
362
    
cleanup
yuki-kimoto authored on 2010-10-17
363
    # Arguments
364
    my $table            = $args{table} || '';
365
    my $param            = $args{param} || {};
366
    my $where            = $args{where} || {};
367
    my $append = $args{append} || '';
368
    my $filter           = $args{filter};
369
    my $allow_update_all = $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
370
    
cleanup
yuki-kimoto authored on 2010-10-17
371
    # Update keys
372
    my @update_keys = keys %$param;
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
373
    
cleanup
yuki-kimoto authored on 2010-10-17
374
    # Where keys
375
    my @where_keys = keys %$where;
removed reconnect method
yuki-kimoto authored on 2010-05-28
376
    
cleanup
yuki-kimoto authored on 2010-10-17
377
    # Not exists where keys
378
    croak qq{"where" argument must be specified and } .
379
          qq{contains the pairs of column name and value}
380
      if !@where_keys && !$allow_update_all;
removed experimental registe...
yuki-kimoto authored on 2010-08-24
381
    
cleanup
yuki-kimoto authored on 2010-10-17
382
    # Update clause
383
    my $update_clause = '{update_param ' . join(' ', @update_keys) . '}';
removed experimental registe...
yuki-kimoto authored on 2010-08-24
384
    
cleanup
yuki-kimoto authored on 2010-10-17
385
    # Where clause
386
    my $where_clause = '';
387
    my $new_where = {};
removed reconnect method
yuki-kimoto authored on 2010-05-28
388
    
cleanup
yuki-kimoto authored on 2010-10-17
389
    if (@where_keys) {
390
        $where_clause = 'where ';
391
        $where_clause .= "{= $_} and " for @where_keys;
392
        $where_clause =~ s/ and $//;
removed reconnect method
yuki-kimoto authored on 2010-05-28
393
    }
394
    
cleanup
yuki-kimoto authored on 2010-10-17
395
    # Source of SQL
396
    my $source = "update $table $update_clause $where_clause";
397
    $source .= " $append" if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
398
    
cleanup
yuki-kimoto authored on 2010-10-17
399
    # Rearrange parameters
400
    foreach my $wkey (@where_keys) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
401
        
cleanup
yuki-kimoto authored on 2010-10-17
402
        if (exists $param->{$wkey}) {
403
            $param->{$wkey} = [$param->{$wkey}]
404
              unless ref $param->{$wkey} eq 'ARRAY';
405
            
406
            push @{$param->{$wkey}}, $where->{$wkey};
407
        }
408
        else {
409
            $param->{$wkey} = $where->{$wkey};
410
        }
removed reconnect method
yuki-kimoto authored on 2010-05-28
411
    }
cleanup
yuki-kimoto authored on 2010-10-17
412
    
413
    # Execute query
414
    my $ret_val = $self->execute($source, param  => $param, 
415
                                 filter => $filter);
416
    
417
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
418
}
419

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
422
sub _build_bind_values {
423
    my ($self, $query, $params, $filter) = @_;
424
    
425
    # binding values
426
    my @bind_values;
add tests
yuki-kimoto authored on 2010-08-08
427

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
483
sub _croak {
484
    my ($self, $error, $append) = @_;
485
    $append ||= "";
486
    
487
    # Verbose
488
    if ($Carp::Verbose) { croak $error }
489
    
490
    # Not verbose
491
    else {
492
        
493
        # Remove line and module infromation
494
        my $at_pos = rindex($error, ' at ');
495
        $error = substr($error, 0, $at_pos);
496
        $error =~ s/\s+$//;
497
        
498
        croak "$error$append";
499
    }
500
}
501

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
504
=head1 NAME
505

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

            
508
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
509

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

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

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

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

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

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

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

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

            
updated document
yuki-kimoto authored on 2010-08-08
586
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
587

            
removed register_format()
yuki-kimoto authored on 2010-05-26
588
    # Default filter
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
589
    $dbi->default_bind_filter('encode_utf8');
removed register_format()
yuki-kimoto authored on 2010-05-26
590
    $dbi->default_fetch_filter('decode_utf8');
cleanup
yuki-kimoto authored on 2010-08-05
591

            
592
    # Get DBI object
593
    my $dbh = $dbi->dbh;
594

            
595
Fetch row.
596

            
removed register_format()
yuki-kimoto authored on 2010-05-26
597
    # Fetch
598
    while (my $row = $result->fetch) {
599
        # ...
600
    }
601
    
602
    # Fetch hash
603
    while (my $row = $result->fetch_hash) {
604
        
605
    }
606
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
607
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
608

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

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

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

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

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

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

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

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

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

            
643
    use DBIx::Custom::SQLite;
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
644
    my $dbi = DBIx::Custom::SQLite->connect(database => 'dbname');
update document
yuki-kimoto authored on 2010-08-07
645
    
cleanup
yuki-kimoto authored on 2010-08-09
646
If database is  MySQL, use L<DBIx::Custom::MySQL>.
update document
yuki-kimoto authored on 2010-08-07
647

            
648
    use DBIx::Custom::MySQL;
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
649
    my $dbi = DBIx::Custom::MySQL->connect(
650
        database => 'dbname',
651
        user     => 'ken',
652
        password => '!LFKD%$&'
653
    );
update document
yuki-kimoto authored on 2010-08-07
654

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

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

            
update document
yuki-kimoto authored on 2010-08-07
661
=head3 insert()
662

            
updated document
yuki-kimoto authored on 2010-08-08
663
Execute insert statement.
664

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

            
668
The following SQL is executed.
669

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

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

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

            
677
=head3 update()
678

            
updated document
yuki-kimoto authored on 2010-08-08
679
Execute update statement.
680

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

            
685
The following SQL is executed.
686

            
687
    update books set title = ?, author = ?;
688

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

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

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

            
696
=head3 delete()
697

            
updated document
yuki-kimoto authored on 2010-08-08
698
Execute delete statement.
699

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

            
703
The following SQL is executed.
704

            
705
    delete from books where id = ?;
706

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

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

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

            
714
=head3 select()
715

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

            
718
    my $result = $dbi->select(table => 'books');
719

            
720
The following SQL is executed.
721

            
722
    select * from books;
723

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

            
727
    while (my $row = $result->fetch) {
728
        my $title  = $row->[0];
729
        my $author = $row->[1];
730
    }
731

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

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

            
737
    my $result = $dbi->select(
738
        table  => 'books',
739
        column => [qw/author title/],
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
740
        where  => {author => 'Ken'}
741
    );
update document
yuki-kimoto authored on 2010-08-07
742

            
743
The following SQL is executed.
744

            
745
    select author, title from books where author = ?;
746

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

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

            
751
    my $result = $dbi->select(
752
        table    => ['books', 'rental'],
753
        column   => ['books.name as book_name']
754
        relation => {'books.id' => 'rental.book_id'}
755
    );
756

            
757
The following SQL is executed.
758

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

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

            
765
    my $result = $dbi->select(
766
        table  => 'books',
767
        where  => {author => 'Ken'},
768
        append => 'order by price limit 5',
769
    );
770

            
771
The following SQL is executed.
772

            
773
    select * books where author = ? order by price limit 5;
774

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

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

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

            
785
Fetch row into array.
786
    
787
    while (my $row = $result->fetch) {
788
        my $author = $row->[0];
789
        my $title  = $row->[1];
790
        
791
    }
792

            
793
Fetch only a first row into array.
794

            
795
    my $row = $result->fetch_first;
796

            
797
Fetch multiple rows into array of array.
798

            
799
    while (my $rows = $result->fetch_multi(5)) {
800
        my $first_author  = $rows->[0][0];
801
        my $first_title   = $rows->[0][1];
802
        my $second_author = $rows->[1][0];
803
        my $second_value  = $rows->[1][1];
804
    
805
    }
806
    
807
Fetch all rows into array of array.
808

            
809
    my $rows = $result->fetch_all;
810

            
811
Fetch row into hash.
812

            
813
    # Fetch a row into hash
814
    while (my $row = $result->fetch_hash) {
815
        my $title  = $row->{title};
816
        my $author = $row->{author};
817
        
818
    }
819

            
820
Fetch only a first row into hash
821

            
822
    my $row = $result->fetch_hash_first;
823
    
824
Fetch multiple rows into array of hash
825

            
826
    while (my $rows = $result->fetch_hash_multi(5)) {
827
        my $first_title   = $rows->[0]{title};
828
        my $first_author  = $rows->[0]{author};
829
        my $second_title  = $rows->[1]{title};
830
        my $second_author = $rows->[1]{author};
831
    
832
    }
833
    
834
Fetch all rows into array of hash
835

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

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

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

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

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

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

            
848
    use DBI;
849
    my $dbh = DBI->connect(...);
850
    my $sth = $dbh->prepare(
851
        "select * from books where author = ? and title like ?;"
852
    );
853
    $sth->execute('Ken', '%Perl%');
854

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

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

            
861
    my $result = $dbi->execute(
862
        "select * from books where {= author} and {like title};"
863
        param => {author => 'Ken', title => '%Perl%'}
864
    );
865

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

            
870
    select * from books where {= author} and {like title}
871
      -> select * from books where author = ? and title like ?;
872

            
updated document
yuki-kimoto authored on 2010-08-08
873
The following tags is available.
874

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

            
891
See also L<DBIx::Custom::QueryBuilder>.
892

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

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

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

            
901
Usually, Perl string is kept as internal string.
902
If you want to save the string to database, You must encode the string.
903
Filtering system help you to convert a data to another data
904
when you save to the data and get the data form database.
905

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

            
908
    $dbi->register_filter(
909
        to_upper_case => sub {
910
            my $value = shift;
911
            return uc $value;
912
        }
913
    );
914

            
915
C<encode_utf8> and C<decode_utf8> filter is registerd by default.
916

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

            
919
    my $result = $dbi->execute(
920
        "select * from books where {= author} and {like title};"
updated document
yuki-kimoto authored on 2010-08-08
921
        param  => {author => 'Ken', title => '%Perl%'},
update document
yuki-kimoto authored on 2010-08-07
922
        filter => {author => 'to_upper_case, title => 'encode_utf8'}
923
    );
924

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

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

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

            
945
    $dbi->default_bind_filter('encode_utf8');
946

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

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

            
958
    $dbi->insert(
959
        table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
960
        param  => {title => 'Perl', author => 'Ken', price => 1000},
update document
yuki-kimoto authored on 2010-08-07
961
        filter => {title => 'encode_uft8' author => 'to_upper_case'}
962
    );
963

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

            
966
    my $result = $dbi->select(table => 'books');
967
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
968

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

            
972
    $dbi->default_fetch_filter('decode_utf8');
973

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

            
977
    $dbi->default_fetch_filter('decode_utf8');
978
    my $result = $dbi->select(
979
        table => 'books',
980
        columns => ['title', 'author', 'price']
981
    );
982
    $result->filter({author => 'to_upper_case', price => undef});
983

            
984
This is same as the following one.
985

            
986
    my $result = $dbi->select(
987
        table => 'books',
988
        columns => ['title', 'author', 'price']
989
    );
990
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
991

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

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

            
updated document
yuki-kimoto authored on 2010-08-08
998
=head3 Disable filter checking
999

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

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

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

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

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

            
1014
C<insert()> method is a little slow because SQL statement and statement handle
1015
is created every time.
1016

            
1017
In that case, you can prepare a query by C<create_query()> method.
1018
    
1019
    my $query = $dbi->create_query(
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1020
        "insert into books {insert_param title author};"
updated document
yuki-kimoto authored on 2010-08-08
1021
    );
cleanup
yuki-kimoto authored on 2010-08-09
1022

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

            
1026
    {
1027
        sql     => 'insert into books (title, author) values (?, ?);',
1028
        columns => ['title', 'author']
1029
    }
1030

            
1031
Execute query repeatedly.
updated document
yuki-kimoto authored on 2010-08-08
1032
    
1033
    my $inputs = [
1034
        {title => 'Perl',      author => 'Ken'},
1035
        {title => 'Good days', author => 'Mike'}
1036
    ];
1037
    
1038
    foreach my $input (@$inputs) {
1039
        $dbi->execute($query, $input);
1040
    }
1041

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1044
=head3 caching
updated document
yuki-kimoto authored on 2010-08-08
1045

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

            
1049
    $dbi->cache(1);
1050

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

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

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

            
1081
=head3 Get DBI object
1082

            
1083
You can get L<DBI> object and call any method of L<DBI>.
1084

            
1085
    $dbi->dbh->begin_work;
1086
    $dbi->dbh->commit;
1087
    $dbi->dbh->rollback;
1088

            
1089
=head3 Change Result class
1090

            
1091
You can change Result class if you need.
1092

            
1093
    package Your::Result;
1094
    use base 'DBIx::Custom::Result';
1095
    
1096
    sub some_method { ... }
1097

            
1098
    1;
1099
    
1100
    package main;
1101
    
1102
    use Your::Result;
1103
    
1104
    my $dbi = DBIx::Custom->connect(...);
1105
    $dbi->result_class('Your::Result');
1106

            
1107
=head3 Custamize SQL builder object
1108

            
1109
You can custamize SQL builder object
1110

            
1111
    my $dbi = DBIx::Custom->connect(...);
1112
    $dbi->query_builder->register_tag_processor(
1113
        name => sub {
1114
           ...
1115
        }
1116
    );
1117

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1130
    $dbi          = $dbi->cache_method(\&cache_method);
1131
    $cache_method = $dbi->cache_method
1132

            
1133
Method to set and get caches.
1134

            
1135
B<Example:>
1136

            
1137
    $dbi->cache_method(
1138
        sub {
1139
            my $self = shift;
1140
            
1141
            $self->{_cached} ||= {};
1142
            
1143
            if (@_ > 1) {
1144
                $self->{_cached}{$_[0]} = $_[1] 
1145
            }
1146
            else {
1147
                return $self->{_cached}{$_[0]}
1148
            }
1149
        }
1150
    );
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1151

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1186
Filter functions.
1187
"encode_utf8" and "decode_utf8" is registered by default.
1188

            
1189
=head2 C<filter_check>
1190

            
1191
    my $filter_check = $dbi->filter_check;
1192
    $dbi             = $dbi->filter_check(0);
1193

            
1194
Enable filter check. 
1195
Default to 1.
1196
This check maybe damege performance.
1197
If you require performance, set C<filter_check> attribute to 0.
1198

            
1199
=head2 C<password>
1200

            
1201
    my $password = $dbi->password;
1202
    $dbi         = $dbi->password('lkj&le`@s');
1203

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1226
    my $user = $dbi->user;
1227
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1228

            
cleanup
yuki-kimoto authored on 2010-10-17
1229
User name.
1230
C<connect()> method use this value to connect the database.
1231
    
1232
=head1 METHODS
added commit method
yuki-kimoto authored on 2010-05-27
1233

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1247
=head2 commit
1248

            
1249
    $dbi->commit;
1250

            
1251
Commit transaction.
1252
This is same as L<DBI>'s C<commit>.
1253

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1264
=head2 C<create_query>
1265
    
1266
    my $query = $dbi->create_query(
1267
        "select * from books where {= author} and {like title};"
1268
    );
update document
yuki-kimoto authored on 2009-11-19
1269

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1289
    my $result = $dbi->execute(
1290
        "select * from books where {= author} and {like title}", 
1291
        param => {author => 'Ken', title => '%Perl%'}
1292
    );
1293
    
1294
    while (my $row = $result->fetch) {
1295
        my $author = $row->[0];
1296
        my $title  = $row->[1];
1297
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
1298

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1334
=head2 C<insert>
1335

            
1336
    $dbi->insert(table  => $table, 
1337
                 param  => \%param,
1338
                 append => $append,
1339
                 filter => \%filter);
1340

            
1341
Execute insert statement.
1342
C<insert> method have C<table>, C<param>, C<append>
1343
and C<filter> arguments.
1344
C<table> is a table name.
1345
C<param> is the pairs of column name value. this must be hash reference.
1346
C<append> is a string added at the end of the SQL statement.
1347
C<filter> is filters when parameter binding is executed.
1348
This is overwrites C<default_bind_filter>.
1349
Return value of C<insert()> is the count of affected rows.
1350

            
1351
B<Example:>
1352

            
1353
    $dbi->insert(table  => 'books', 
1354
                 param  => {title => 'Perl', author => 'Taro'},
1355
                 append => "some statement",
1356
                 filter => {title => 'encode_utf8'})
1357

            
1358
=head2 C<register_filter>
1359

            
1360
    $dbi->register_filter(%filters);
1361
    $dbi->register_filter(\%filters);
1362
    
1363
Register filter. Registered filters is available in the following attributes
1364
or arguments.
1365

            
1366
=over 4
1367

            
1368
=item *
1369

            
1370
C<default_bind_filter>, C<default_fetch_filter>
1371

            
1372
=item *
1373

            
1374
C<filter> argument of C<insert()>, C<update()>,
1375
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1376
methods
1377

            
1378
=item *
1379

            
1380
C<execute()> method
1381

            
1382
=item *
1383

            
1384
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1385

            
1386
=item *
1387

            
1388
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1389

            
1390
=back
1391

            
1392
B<Example:>
1393

            
1394
    $dbi->register_filter(
1395
        encode_utf8 => sub {
1396
            my $value = shift;
1397
            
1398
            require Encode;
1399
            
1400
            return Encode::encode('UTF-8', $value);
1401
        },
1402
        decode_utf8 => sub {
1403
            my $value = shift;
1404
            
1405
            require Encode;
1406
            
1407
            return Encode::decode('UTF-8', $value)
1408
        }
1409
    );
1410

            
1411
=head2 rollback
1412

            
1413
    $dbi->rollback;
1414

            
1415
Rollback transaction.
1416
This is same as L<DBI>'s C<rollback>.
1417

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1418
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1419
    
cleanup
yuki-kimoto authored on 2010-08-05
1420
    my $result = $dbi->select(table    => $table,
1421
                              column   => [@column],
1422
                              where    => \%where,
1423
                              append   => $append,
1424
                              relation => \%relation,
1425
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1426

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1427
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1428
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1429
C<relation> and C<filter> arguments.
1430
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1431
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1432
C<append> is a string added at the end of the SQL statement.
1433
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
1434

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

            
added commit method
yuki-kimoto authored on 2010-05-27
1437
    # select * from books;
cleanup
yuki-kimoto authored on 2010-08-05
1438
    my $result = $dbi->select(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
1439
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1440
    # select * from books where title = ?;
1441
    my $result = $dbi->select(table => 'books', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1442
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1443
    # select title, author from books where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1444
    my $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
1445
        table  => 'books',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1446
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1447
        where  => {id => 1},
1448
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1449
    );
1450
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1451
    # select books.name as book_name from books, rental
added commit method
yuki-kimoto authored on 2010-05-27
1452
    # where books.id = rental.book_id;
1453
    my $result = $dbi->select(
removed reconnect method
yuki-kimoto authored on 2010-05-28
1454
        table    => ['books', 'rental'],
1455
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
1456
        relation => {'books.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1457
    );
1458

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

            
1462
    my $result = $dbi->select(
1463
        table  => 'books',
1464
        column => ['title', 'author'],
1465
        where  => ['{= title} or {like author}',
1466
                   {title => '%Perl%', author => 'Ken'}]
1467
    );
1468

            
1469
First element is a string. it contains tags,
1470
such as "{= title} or {like author}".
1471
Second element is paramters.
1472

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1475
    $dbi->update(table  => $table, 
1476
                 param  => \%params,
1477
                 where  => \%where,
1478
                 append => $append,
1479
                 filter => \%filter)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1480

            
cleanup
yuki-kimoto authored on 2010-10-17
1481
Execute update statement.
1482
C<update> method have C<table>, C<param>, C<where>, C<append>
1483
and C<filter> arguments.
1484
C<table> is a table name.
1485
C<param> is column-value pairs. this must be hash reference.
1486
C<where> is where clause. this must be hash reference.
1487
C<append> is a string added at the end of the SQL statement.
1488
C<filter> is filters when parameter binding is executed.
1489
This is overwrites C<default_bind_filter>.
1490
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1491

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1494
    $dbi->update(table  => 'books',
1495
                 param  => {title => 'Perl', author => 'Taro'},
1496
                 where  => {id => 5},
1497
                 append => "some statement",
1498
                 filter => {title => 'encode_utf8'});
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1499

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1502
    $dbi->update_all(table  => $table, 
1503
                     param  => \%params,
1504
                     filter => \%filter,
1505
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1506

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

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

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

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

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

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

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

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

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

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

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

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

            
1536
Copyright 2009 Yuki Kimoto, all rights reserved.
1537

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

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

            
1543