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

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
3
our $VERSION = '0.1627';
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;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
16
use DBIx::Custom::Model;
update document
yuki-kimoto authored on 2010-05-27
17
use Encode qw/encode_utf8 decode_utf8/;
packaging one directory
yuki-kimoto authored on 2009-11-16
18

            
cleanup
Yuki Kimoto authored on 2010-12-21
19
__PACKAGE__->attr([qw/data_source dbh
20
                      dbi_options password user/]);
added cache_method attribute
yuki-kimoto authored on 2010-06-25
21

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

            
cleanup (removed undocumente...
yuki-kimoto authored on 2010-11-10
38
__PACKAGE__->attr(filters => sub {
39
    {
40
        encode_utf8 => sub { encode_utf8($_[0]) },
41
        decode_utf8 => sub { decode_utf8($_[0]) }
42
    }
43
});
added check_filter attribute
yuki-kimoto authored on 2010-08-08
44
__PACKAGE__->attr(filter_check => 1);
cleanup
yuki-kimoto authored on 2010-10-17
45
__PACKAGE__->attr(query_builder  => sub {DBIx::Custom::QueryBuilder->new});
46
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result');
47

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

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

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

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

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

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

            
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
78
sub auto_filter {
79
    my $self = shift;
80
    
cleanup
Yuki Kimoto authored on 2010-12-22
81
    $self->{auto_filter} ||= {};
82
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
83
    # Table
84
    my $table = shift;
85
    
cleanup
Yuki Kimoto authored on 2010-12-22
86
    if (@_) {
87
        # Column infomations
88
        my @cs = @_;
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
89
        
cleanup
Yuki Kimoto authored on 2010-12-22
90
        # Initialize filters
91
        $self->{auto_filter}{bind} ||= {};
92
        $self->{auto_filter}{fetch} ||= {};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
93
        
cleanup
Yuki Kimoto authored on 2010-12-22
94
        # Create auto filters
95
        foreach my $c (@cs) {
96
            croak "Usage \$dbi->auto_filter(" .
97
                  "TABLE, [COLUMN, BIND_FILTER, FETCH_FILTER], [...])"
98
              unless ref $c eq 'ARRAY' && @$c == 3;
99
            
100
            # Column
101
            my $column = $c->[0];
102
            
103
            # Bind filter
104
            my $bind_filter  = $c->[1];
105
            if (ref $bind_filter eq 'CODE') {
106
    	        $self->{auto_filter}{bind}{$table}{$column}
107
    	          = $bind_filter;
108
    	        $self->{auto_filter}{bind}{$table}{"$table.$column"}
109
    	          = $bind_filter;
110
            }
111
            else {
112
    	        croak qq{"$bind_filter" is not registered}
113
    	          unless exists $self->filters->{$bind_filter};
114
    	        
115
    	        $self->{auto_filter}{bind}{$table}{$column}
116
    	          = $self->filters->{$bind_filter};
117
    	        $self->{auto_filter}{bind}{$table}{"$table.$column"}
118
    	          = $self->filters->{$bind_filter};
119
    	    }
120
            
121
            # Fetch filter
122
            my $fetch_filter = $c->[2];
123
            if (ref $fetch_filter eq 'CODE') {
124
    	        $self->{auto_filter}{fetch}{$table}{$column}
125
    	          = $fetch_filter;
126
    	        $self->{auto_filter}{fetch}{$table}{"$table.$column"}
127
    	          = $fetch_filter;
128
            }
129
            else {
130
                croak qq{"$fetch_filter" is not registered}
131
                  unless exists $self->filters->{$fetch_filter};
132
                $self->{auto_filter}{fetch}{$table}{$column}
133
                  = $self->filters->{$fetch_filter};
134
                $self->{auto_filter}{fetch}{$table}{"$table.$column"}
135
                  = $self->filters->{$fetch_filter};
136
            }
cleanup
Yuki Kimoto authored on 2010-12-21
137
        }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
138
        
cleanup
Yuki Kimoto authored on 2010-12-22
139
        return $self;
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
140
    }
141
    
cleanup
Yuki Kimoto authored on 2010-12-22
142
    return $self->{auto_filter};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
143
}
144

            
added helper method
yuki-kimoto authored on 2010-10-17
145
sub helper {
146
    my $self = shift;
147
    
148
    # Merge
149
    my $helpers = ref $_[0] eq 'HASH' ? $_[0] : {@_};
150
    $self->{_helpers} = {%{$self->{_helpers} || {}}, %$helpers};
151
    
152
    return $self;
153
}
154

            
packaging one directory
yuki-kimoto authored on 2009-11-16
155
sub connect {
removed register_format()
yuki-kimoto authored on 2010-05-26
156
    my $proto = shift;
157
    
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
158
    my $self;
removed register_format()
yuki-kimoto authored on 2010-05-26
159
    # Create
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
160
    if (my $class = ref $proto) {
161
        my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
162
        $self = $proto;
163
        
164
        foreach my $attr (keys %$args) {
165
            $self->{$attr} = $args->{$attr};
166
        }
167
        
168
        # Check attribute names
169
        my @attrs = keys %$self;
170
        foreach my $attr (@attrs) {
171
            croak qq{"$attr" is invalid attribute name}
172
              unless $self->can($attr);
173
        }
174
    }
175
    else {
176
        $self = $proto->new(@_);
177
    }
update document
yuki-kimoto authored on 2010-01-30
178
    
179
    # Information
packaging one directory
yuki-kimoto authored on 2009-11-16
180
    my $data_source = $self->data_source;
check arguments of connect m...
Yuki Kimoto authored on 2010-12-20
181
    
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
182
    croak qq{"data_source" must be specified to connect method"}
check arguments of connect m...
Yuki Kimoto authored on 2010-12-20
183
      unless $data_source;
184
    
packaging one directory
yuki-kimoto authored on 2009-11-16
185
    my $user        = $self->user;
186
    my $password    = $self->password;
added dbi_options attribute
kimoto authored on 2010-12-20
187
    my $dbi_options = $self->dbi_options || {};
188
    
update document
yuki-kimoto authored on 2010-01-30
189
    # Connect
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
190
    my $dbh = eval {DBI->connect(
packaging one directory
yuki-kimoto authored on 2009-11-16
191
        $data_source,
192
        $user,
193
        $password,
194
        {
195
            RaiseError => 1,
196
            PrintError => 0,
197
            AutoCommit => 1,
added dbi_options attribute
kimoto authored on 2010-12-20
198
            %$dbi_options
packaging one directory
yuki-kimoto authored on 2009-11-16
199
        }
200
    )};
201
    
update document
yuki-kimoto authored on 2010-01-30
202
    # Connect error
packaging one directory
yuki-kimoto authored on 2009-11-16
203
    croak $@ if $@;
204
    
update document
yuki-kimoto authored on 2010-01-30
205
    # Database handle
packaging one directory
yuki-kimoto authored on 2009-11-16
206
    $self->dbh($dbh);
update document
yuki-kimoto authored on 2010-01-30
207
    
packaging one directory
yuki-kimoto authored on 2009-11-16
208
    return $self;
209
}
210

            
cleanup
yuki-kimoto authored on 2010-10-17
211
sub create_query {
212
    my ($self, $source) = @_;
update document
yuki-kimoto authored on 2010-01-30
213
    
cleanup
yuki-kimoto authored on 2010-10-17
214
    # Cache
215
    my $cache = $self->cache;
update document
yuki-kimoto authored on 2010-01-30
216
    
cleanup
yuki-kimoto authored on 2010-10-17
217
    # Create query
218
    my $query;
219
    if ($cache) {
220
        
221
        # Get query
222
        my $q = $self->cache_method->($self, $source);
223
        
224
        # Create query
225
        $query = DBIx::Custom::Query->new($q) if $q;
226
    }
227
    
228
    unless ($query) {
cleanup insert
yuki-kimoto authored on 2010-04-28
229

            
cleanup
yuki-kimoto authored on 2010-10-17
230
        # Create SQL object
231
        my $builder = $self->query_builder;
232
        
233
        # Create query
234
        $query = $builder->build_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
235

            
cleanup
yuki-kimoto authored on 2010-10-17
236
        # Cache query
237
        $self->cache_method->($self, $source,
238
                             {sql     => $query->sql, 
239
                              columns => $query->columns})
240
          if $cache;
cleanup insert
yuki-kimoto authored on 2010-04-28
241
    }
242
    
cleanup
yuki-kimoto authored on 2010-10-17
243
    # Prepare statement handle
244
    my $sth;
245
    eval { $sth = $self->dbh->prepare($query->{sql})};
246
    $self->_croak($@, qq{. SQL: "$query->{sql}"}) if $@;
packaging one directory
yuki-kimoto authored on 2009-11-16
247
    
cleanup
yuki-kimoto authored on 2010-10-17
248
    # Set statement handle
249
    $query->sth($sth);
packaging one directory
yuki-kimoto authored on 2009-11-16
250
    
cleanup
yuki-kimoto authored on 2010-10-17
251
    return $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
252
}
253

            
cleanup
Yuki Kimoto authored on 2010-12-21
254
sub default_bind_filter {
255
    my $self = shift;
256
    
cleanup
Yuki Kimoto authored on 2010-12-22
257
    if (@_) {
258
        my $fname = $_[0];
259
        
260
        if (@_ && !$fname) {
261
            $self->{default_bind_filter} = undef;
262
        }
263
        else {
264
            croak qq{"$fname" is not registered}
265
              unless exists $self->filters->{$fname};
266
        
267
            $self->{default_bind_filter} = $self->filters->{$fname};
268
        }
269
        return $self;
cleanup
Yuki Kimoto authored on 2010-12-21
270
    }
271
    
cleanup
Yuki Kimoto authored on 2010-12-22
272
    return $self->{default_bind_filter};
cleanup
Yuki Kimoto authored on 2010-12-21
273
}
274

            
275
sub default_fetch_filter {
276
    my $self = shift;
277
    my $fname = $_[0];
278
    
cleanup
Yuki Kimoto authored on 2010-12-22
279
    if (@_) {
280
        if (@_ && !$fname) {
281
            $self->{default_fetch_filter} = undef;
282
        }
283
        else {
284
            croak qq{"$fname" is not registered}
285
              unless exists $self->filters->{$fname};
286
        
287
            $self->{default_fetch_filter} = $self->filters->{$fname};
288
        }
289
        
290
        return $self;
cleanup
Yuki Kimoto authored on 2010-12-21
291
    }
292
    
cleanup
Yuki Kimoto authored on 2010-12-22
293
    return $self->{default_fetch_filter}
cleanup
Yuki Kimoto authored on 2010-12-21
294
}
295

            
cleanup
yuki-kimoto authored on 2010-10-17
296
our %VALID_DELETE_ARGS
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
297
  = map { $_ => 1 } qw/auto_filter_table table where append filter allow_delete_all/;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
298

            
cleanup
yuki-kimoto authored on 2010-10-17
299
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
300
    my ($self, %args) = @_;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
301
    
302
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
303
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
304
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
305
          unless $VALID_DELETE_ARGS{$name};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
306
    }
307
    
308
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
309
    my $table            = $args{table} || '';
310
    my $where            = $args{where} || {};
cleanup
yuki-kimoto authored on 2010-10-17
311
    my $append = $args{append};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
312
    my $filter           = $args{filter};
cleanup
yuki-kimoto authored on 2010-10-17
313
    my $allow_delete_all = $args{allow_delete_all};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
314

            
315
    my $auto_filter_table = exists $args{auto_filter_table}
316
                          ? $args{auto_filter_table}
317
                          : [$table];
318
    $auto_filter_table ||= [];    
319

            
packaging one directory
yuki-kimoto authored on 2009-11-16
320
    # Where keys
removed register_format()
yuki-kimoto authored on 2010-05-26
321
    my @where_keys = keys %$where;
packaging one directory
yuki-kimoto authored on 2009-11-16
322
    
323
    # Not exists where keys
add tests
yuki-kimoto authored on 2010-08-10
324
    croak qq{"where" argument must be specified and } .
325
          qq{contains the pairs of column name and value}
cleanup
yuki-kimoto authored on 2010-10-17
326
      if !@where_keys && !$allow_delete_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
327
    
328
    # Where clause
329
    my $where_clause = '';
330
    if (@where_keys) {
331
        $where_clause = 'where ';
add tests
yuki-kimoto authored on 2010-08-10
332
        $where_clause .= "{= $_} and " for @where_keys;
packaging one directory
yuki-kimoto authored on 2009-11-16
333
        $where_clause =~ s/ and $//;
334
    }
335
    
add tests
yuki-kimoto authored on 2010-08-10
336
    # Source of SQL
cleanup
yuki-kimoto authored on 2010-10-17
337
    my $source = "delete from $table $where_clause";
add tests
yuki-kimoto authored on 2010-08-10
338
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
339
    
340
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
341
    my $ret_val = $self->execute(
342
        $source, param  => $where, filter => $filter,
343
        auto_filter_table => $auto_filter_table);
packaging one directory
yuki-kimoto authored on 2009-11-16
344
    
345
    return $ret_val;
346
}
347

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

            
added helper method
yuki-kimoto authored on 2010-10-17
350
sub DESTROY { }
351

            
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
352
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter auto_filter_table/;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
353

            
cleanup
yuki-kimoto authored on 2010-10-17
354
sub execute{
355
    my ($self, $query, %args)  = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
356
    
357
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
358
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
359
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
360
          unless $VALID_EXECUTE_ARGS{$name};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
361
    }
362
    
cleanup
yuki-kimoto authored on 2010-10-17
363
    my $params = $args{param} || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
364
    
cleanup
yuki-kimoto authored on 2010-10-17
365
    # First argument is the soruce of SQL
366
    $query = $self->create_query($query)
367
      unless ref $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
368
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
369
    # Auto filter
370
    my $auto_filter = {};
371
    my $auto_filter_tables = $args{auto_filter_table} || [];
372
    foreach my $table (@$auto_filter_tables) {
373
        $auto_filter = {
374
            %$auto_filter,
cleanup
Yuki Kimoto authored on 2010-12-22
375
            %{$self->{auto_filter}{bind}->{$table} || {}}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
376
        }
377
    }
378
    
379
    # Filter
cleanup
yuki-kimoto authored on 2010-10-17
380
    my $filter = $args{filter} || $query->filter || {};
cleanup
Yuki Kimoto authored on 2010-12-21
381
    foreach my $column (keys %$filter) {
382
        my $fname = $filter->{$column};
383
        unless (ref $fname eq 'CODE') {
384
          croak qq{"$fname" is not registered"}
385
            unless exists $self->filters->{$fname};
386
          
387
          $filter->{$column} = $self->filters->{$fname};
388
        }
389
    }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
390
    $filter = {%$auto_filter, %$filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
391
    
cleanup
yuki-kimoto authored on 2010-10-17
392
    # Create bind value
393
    my $bind_values = $self->_build_bind_values($query, $params, $filter);
394
    
395
    # Execute
396
    my $sth      = $query->sth;
397
    my $affected;
398
    eval {$affected = $sth->execute(@$bind_values)};
399
    $self->_croak($@) if $@;
400
    
401
    # Return resultset if select statement is executed
402
    if ($sth->{NUM_OF_FIELDS}) {
403
        
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
404
        # Auto fetch filter
405
        my $auto_fetch_filter = {};
406
	    foreach my $table (@$auto_filter_tables) {
407
	        $auto_fetch_filter = {
408
	            %$auto_filter,
cleanup
Yuki Kimoto authored on 2010-12-22
409
	            %{$self->{auto_filter}{fetch}{$table} || {}}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
410
	        }
411
	    }
412
	    
413
		# Result
414
		my $result = $self->result_class->new(
cleanup
Yuki Kimoto authored on 2010-12-22
415
            sth            => $sth,
416
            filters        => $self->filters,
417
            filter_check   => $self->filter_check,
418
            default_filter => $self->default_fetch_filter,
419
            _auto_filter   => $auto_fetch_filter || {}
cleanup
yuki-kimoto authored on 2010-10-17
420
        );
421

            
422
        return $result;
423
    }
424
    return $affected;
425
}
426

            
added experimental expand me...
yuki-kimoto authored on 2010-10-20
427
sub expand {
428
    my $self = shift;
429
    my $source = ref $_[0] eq 'HASH' ? $_[0] : {@_};
430
    my $table = (keys %$source)[0];
431
    my $param = $source->{$table};
432
    
433
    # Expand table name
434
    my $expand = {};
435
    foreach my $column (keys %$param) {
436
        $expand->{"$table.$column"} = $param->{$column};
437
    }
438
    
439
    return %$expand;
440
}
441

            
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
442
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append
443
                                            filter auto_filter_table/;
cleanup
yuki-kimoto authored on 2010-10-17
444
sub insert {
445
    my ($self, %args) = @_;
446

            
447
    # Check arguments
448
    foreach my $name (keys %args) {
449
        croak qq{"$name" is invalid argument}
450
          unless $VALID_INSERT_ARGS{$name};
packaging one directory
yuki-kimoto authored on 2009-11-16
451
    }
452
    
cleanup
yuki-kimoto authored on 2010-10-17
453
    # Arguments
454
    my $table  = $args{table} || '';
455
    my $param  = $args{param} || {};
456
    my $append = $args{append} || '';
457
    my $filter = $args{filter};
458
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
459
    my $auto_filter_table = exists $args{auto_filter_table}
460
                          ? $args{auto_filter_table}
461
                          : [$table];
462
    $auto_filter_table ||= [];
463
    
cleanup
yuki-kimoto authored on 2010-10-17
464
    # Insert keys
465
    my @insert_keys = keys %$param;
466
    
467
    # Templte for insert
468
    my $source = "insert into $table {insert_param "
469
               . join(' ', @insert_keys) . '}';
add tests
yuki-kimoto authored on 2010-08-10
470
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
471
    
472
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
473
    my $ret_val = $self->execute(
474
        $source,
475
        param  => $param,
476
        filter => $filter,
477
        auto_filter_table => $auto_filter_table
478
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
479
    
480
    return $ret_val;
481
}
482

            
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
483
sub iterate_all_columns {
484
    my ($self, $cb) = @_;
485
    
486
    # Iterate all tables
487
    my $sth_tables = $self->dbh->table_info;
488
    while (my $table_info = $sth_tables->fetchrow_hashref) {
489
        
490
        # Table
491
        my $table = $table_info->{TABLE_NAME};
492
        
493
        # Iterate all columns
494
        my $sth_columns = $self->dbh->column_info(undef, undef, $table, '%');
495
        while (my $column_info = $sth_columns->fetchrow_hashref) {
496
            my $column = $column_info->{COLUMN_NAME};
497
            $cb->($table, $column, $column_info);
498
        }
499
    }
500
}
501

            
added dbi_options attribute
kimoto authored on 2010-12-20
502
sub new {
503
    my $self = shift->SUPER::new(@_);
504
    
505
    # Check attribute names
506
    my @attrs = keys %$self;
507
    foreach my $attr (@attrs) {
508
        croak qq{"$attr" is invalid attribute name}
509
          unless $self->can($attr);
510
    }
511
    
512
    return $self;
513
}
514

            
cleanup
yuki-kimoto authored on 2010-10-17
515
sub register_filter {
516
    my $invocant = shift;
517
    
518
    # Register filter
519
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
520
    $invocant->filters({%{$invocant->filters}, %$filters});
521
    
522
    return $invocant;
523
}
packaging one directory
yuki-kimoto authored on 2009-11-16
524

            
refactoring select
yuki-kimoto authored on 2010-04-28
525
our %VALID_SELECT_ARGS
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
526
  = map { $_ => 1 } qw/auto_filter_table table column where append relation filter/;
refactoring select
yuki-kimoto authored on 2010-04-28
527

            
packaging one directory
yuki-kimoto authored on 2009-11-16
528
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
529
    my ($self, %args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
530
    
refactoring select
yuki-kimoto authored on 2010-04-28
531
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
532
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
533
        croak qq{"$name" is invalid argument}
refactoring select
yuki-kimoto authored on 2010-04-28
534
          unless $VALID_SELECT_ARGS{$name};
535
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
536
    
refactoring select
yuki-kimoto authored on 2010-04-28
537
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
538
    my $tables = $args{table} || [];
removed register_format()
yuki-kimoto authored on 2010-05-26
539
    $tables = [$tables] unless ref $tables eq 'ARRAY';
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
540
    my $columns  = $args{column} || [];
update document
yuki-kimoto authored on 2010-08-07
541
    my $where    = $args{where};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
542
    my $relation = $args{relation};
543
    my $append   = $args{append};
544
    my $filter   = $args{filter};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
545

            
546
    my $auto_filter_table = exists $args{auto_filter_table}
547
                          ? $args{auto_filter_table}
548
                          : $tables;
packaging one directory
yuki-kimoto authored on 2009-11-16
549
    
add tests
yuki-kimoto authored on 2010-08-10
550
    # Source of SQL
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
551
    my $source = 'select ';
packaging one directory
yuki-kimoto authored on 2009-11-16
552
    
added commit method
yuki-kimoto authored on 2010-05-27
553
    # Column clause
packaging one directory
yuki-kimoto authored on 2009-11-16
554
    if (@$columns) {
555
        foreach my $column (@$columns) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
556
            $source .= "$column, ";
packaging one directory
yuki-kimoto authored on 2009-11-16
557
        }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
558
        $source =~ s/, $/ /;
packaging one directory
yuki-kimoto authored on 2009-11-16
559
    }
560
    else {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
561
        $source .= '* ';
packaging one directory
yuki-kimoto authored on 2009-11-16
562
    }
563
    
added commit method
yuki-kimoto authored on 2010-05-27
564
    # Table
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
565
    $source .= 'from ';
packaging one directory
yuki-kimoto authored on 2009-11-16
566
    foreach my $table (@$tables) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
567
        $source .= "$table, ";
packaging one directory
yuki-kimoto authored on 2009-11-16
568
    }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
569
    $source =~ s/, $/ /;
packaging one directory
yuki-kimoto authored on 2009-11-16
570
    
added commit method
yuki-kimoto authored on 2010-05-27
571
    # Where clause
update document
yuki-kimoto authored on 2010-08-07
572
    my $param;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
573
    if (ref $where eq 'HASH' && keys %$where) {
update document
yuki-kimoto authored on 2010-08-07
574
        $param = $where;
575
        $source .= 'where (';
576
        foreach my $where_key (keys %$where) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
577
            $source .= "{= $where_key} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
578
        }
update document
yuki-kimoto authored on 2010-08-07
579
        $source =~ s/ and $//;
580
        $source .= ') ';
581
    }
582
    elsif (ref $where eq 'ARRAY') {
583
        my$where_str = $where->[0] || '';
584
        $param = $where->[1];
585
        
586
        $source .= "where ($where_str) ";
packaging one directory
yuki-kimoto authored on 2009-11-16
587
    }
588
    
added commit method
yuki-kimoto authored on 2010-05-27
589
    # Relation
590
    if ($relation) {
update document
yuki-kimoto authored on 2010-08-07
591
        $source .= $where ? "and " : "where ";
added commit method
yuki-kimoto authored on 2010-05-27
592
        foreach my $rkey (keys %$relation) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
593
            $source .= "$rkey = " . $relation->{$rkey} . " and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
594
        }
595
    }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
596
    $source =~ s/ and $//;
added commit method
yuki-kimoto authored on 2010-05-27
597
    
598
    # Append some statement
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
599
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
600
    
601
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
602
    my $result = $self->execute(
603
        $source, param  => $param, filter => $filter,
604
        auto_filter_table => $auto_filter_table);    
packaging one directory
yuki-kimoto authored on 2009-11-16
605
    
606
    return $result;
607
}
608

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
609
sub txn_scope {
610
    my $self = shift;
611
    
612
    require DBIx::TransactionManager;
613
    
614
    $self->{_transaction_manager}
615
      ||= DBIx::TransactionManager->new($self->dbh);
616
    
617
    return $self->{_transaction_manager}->txn_scope;
618
}
619

            
cleanup
yuki-kimoto authored on 2010-10-17
620
our %VALID_UPDATE_ARGS
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
621
  = map { $_ => 1 } qw/auto_filter_table table param
622
                       where append filter allow_update_all/;
cleanup
yuki-kimoto authored on 2010-10-17
623

            
624
sub update {
625
    my ($self, %args) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
626
    
cleanup
yuki-kimoto authored on 2010-10-17
627
    # Check arguments
628
    foreach my $name (keys %args) {
629
        croak qq{"$name" is invalid argument}
630
          unless $VALID_UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
631
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
632
    
cleanup
yuki-kimoto authored on 2010-10-17
633
    # Arguments
634
    my $table            = $args{table} || '';
635
    my $param            = $args{param} || {};
636
    my $where            = $args{where} || {};
637
    my $append = $args{append} || '';
638
    my $filter           = $args{filter};
639
    my $allow_update_all = $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
640
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
641
    my $auto_filter_table = exists $args{auto_filter_table}
642
                          ? $args{auto_filter_table}
643
                          : [$table];
644
    $auto_filter_table ||= [];
645
    
cleanup
yuki-kimoto authored on 2010-10-17
646
    # Update keys
647
    my @update_keys = keys %$param;
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
648
    
cleanup
yuki-kimoto authored on 2010-10-17
649
    # Where keys
650
    my @where_keys = keys %$where;
removed reconnect method
yuki-kimoto authored on 2010-05-28
651
    
cleanup
yuki-kimoto authored on 2010-10-17
652
    # Not exists where keys
653
    croak qq{"where" argument must be specified and } .
654
          qq{contains the pairs of column name and value}
655
      if !@where_keys && !$allow_update_all;
removed experimental registe...
yuki-kimoto authored on 2010-08-24
656
    
cleanup
yuki-kimoto authored on 2010-10-17
657
    # Update clause
658
    my $update_clause = '{update_param ' . join(' ', @update_keys) . '}';
removed experimental registe...
yuki-kimoto authored on 2010-08-24
659
    
cleanup
yuki-kimoto authored on 2010-10-17
660
    # Where clause
661
    my $where_clause = '';
662
    my $new_where = {};
removed reconnect method
yuki-kimoto authored on 2010-05-28
663
    
cleanup
yuki-kimoto authored on 2010-10-17
664
    if (@where_keys) {
665
        $where_clause = 'where ';
666
        $where_clause .= "{= $_} and " for @where_keys;
667
        $where_clause =~ s/ and $//;
removed reconnect method
yuki-kimoto authored on 2010-05-28
668
    }
669
    
cleanup
yuki-kimoto authored on 2010-10-17
670
    # Source of SQL
671
    my $source = "update $table $update_clause $where_clause";
672
    $source .= " $append" if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
673
    
cleanup
yuki-kimoto authored on 2010-10-17
674
    # Rearrange parameters
675
    foreach my $wkey (@where_keys) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
676
        
cleanup
yuki-kimoto authored on 2010-10-17
677
        if (exists $param->{$wkey}) {
678
            $param->{$wkey} = [$param->{$wkey}]
679
              unless ref $param->{$wkey} eq 'ARRAY';
680
            
681
            push @{$param->{$wkey}}, $where->{$wkey};
682
        }
683
        else {
684
            $param->{$wkey} = $where->{$wkey};
685
        }
removed reconnect method
yuki-kimoto authored on 2010-05-28
686
    }
cleanup
yuki-kimoto authored on 2010-10-17
687
    
688
    # Execute query
689
    my $ret_val = $self->execute($source, param  => $param, 
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
690
                                 filter => $filter,
691
                                 auto_filter_table => $auto_filter_table);
cleanup
yuki-kimoto authored on 2010-10-17
692
    
693
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
694
}
695

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
698
sub _build_bind_values {
699
    my ($self, $query, $params, $filter) = @_;
700
    
701
    # binding values
702
    my @bind_values;
add tests
yuki-kimoto authored on 2010-08-08
703

            
704
    # Filter
705
    $filter ||= {};
706
    
707
    # Parameter
708
    $params ||= {};
709
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
710
    # Build bind values
711
    my $count = {};
712
    foreach my $column (@{$query->columns}) {
713
        
714
        # Value
715
        my $value = ref $params->{$column} eq 'ARRAY'
716
                  ? $params->{$column}->[$count->{$column} || 0]
717
                  : $params->{$column};
718
        
add tests
yuki-kimoto authored on 2010-08-10
719
        # Filtering
cleanup
Yuki Kimoto authored on 2010-12-22
720
        my $f = $filter->{$column} || $self->{default_bind_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
721
        
cleanup
Yuki Kimoto authored on 2010-12-21
722
        push @bind_values, $f ? $f->($value) : $value;
removed reconnect method
yuki-kimoto authored on 2010-05-28
723
        
724
        # Count up 
725
        $count->{$column}++;
726
    }
727
    
728
    return \@bind_values;
729
}
730

            
cleanup
yuki-kimoto authored on 2010-10-17
731
sub _croak {
732
    my ($self, $error, $append) = @_;
733
    $append ||= "";
734
    
735
    # Verbose
736
    if ($Carp::Verbose) { croak $error }
737
    
738
    # Not verbose
739
    else {
740
        
741
        # Remove line and module infromation
742
        my $at_pos = rindex($error, ' at ');
743
        $error = substr($error, 0, $at_pos);
744
        $error =~ s/\s+$//;
745
        
746
        croak "$error$append";
747
    }
748
}
749

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
752
=head1 NAME
753

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

            
756
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
757

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
766
    # Insert 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
767
    $dbi->insert(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
768
                 param  => {title => 'Perl', author => 'Ken'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
769
                 filter => {title => 'encode_utf8'});
770
    
771
    # Update 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
772
    $dbi->update(table  => 'book', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
773
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
774
                 where  => {id => 5},
775
                 filter => {title => 'encode_utf8'});
776
    
777
    # Update all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
778
    $dbi->update_all(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
779
                     param  => {title => 'Perl'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
780
                     filter => {title => 'encode_utf8'});
781
    
782
    # Delete
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
783
    $dbi->delete(table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
784
                 where  => {author => 'Ken'},
785
                 filter => {title => 'encode_utf8'});
786
    
787
    # Delete all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
788
    $dbi->delete_all(table => 'book');
cleanup
yuki-kimoto authored on 2010-08-05
789

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
792
    # Select
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
793
    my $result = $dbi->select(table => 'book');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
794
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
795
    # Select, more complex
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
796
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
797
        table  => 'book',
update document
yuki-kimoto authored on 2010-05-27
798
        column => [qw/author title/],
799
        where  => {author => 'Ken'},
updated document
yuki-kimoto authored on 2010-08-08
800
        append => 'order by id limit 5',
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
801
        filter => {title => 'encode_utf8'}
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
802
    );
added commit method
yuki-kimoto authored on 2010-05-27
803
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
804
    # Select, join table
added commit method
yuki-kimoto authored on 2010-05-27
805
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
806
        table    => ['book', 'rental'],
807
        column   => ['book.name as book_name']
808
        relation => {'book.id' => 'rental.book_id'}
added commit method
yuki-kimoto authored on 2010-05-27
809
    );
updated document
yuki-kimoto authored on 2010-08-08
810
    
811
    # Select, more flexible where
812
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
813
        table  => 'book',
updated document
yuki-kimoto authored on 2010-08-08
814
        where  => ['{= author} and {like title}', 
815
                   {author => 'Ken', title => '%Perl%'}]
816
    );
cleanup
yuki-kimoto authored on 2010-08-05
817

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
820
    # Execute SQL
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
821
    $dbi->execute("select title from book");
removed register_format()
yuki-kimoto authored on 2010-05-26
822
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
823
    # Execute SQL with hash binding and filtering
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
824
    $dbi->execute("select id from book where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
825
                  param  => {author => 'ken', title => '%Perl%'},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
826
                  filter => {title => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
827

            
828
    # Create query and execute it
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
829
    my $query = $dbi->create_query(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
830
        "select id from book where {= author} and {like title}"
removed reconnect method
yuki-kimoto authored on 2010-05-28
831
    );
updated document
yuki-kimoto authored on 2010-08-08
832
    $dbi->execute($query, param => {author => 'Ken', title => '%Perl%'})
cleanup
yuki-kimoto authored on 2010-08-05
833

            
updated document
yuki-kimoto authored on 2010-08-08
834
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
835

            
836
    # Get DBI object
837
    my $dbh = $dbi->dbh;
838

            
839
Fetch row.
840

            
removed register_format()
yuki-kimoto authored on 2010-05-26
841
    # Fetch
842
    while (my $row = $result->fetch) {
843
        # ...
844
    }
845
    
846
    # Fetch hash
847
    while (my $row = $result->fetch_hash) {
848
        
849
    }
850
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
851
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
852

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

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
862
L<DBIx::Custom> is middle area between L<DBI> and O/R mapper.
updated document
yuki-kimoto authored on 2010-08-08
863
L<DBIx::Custom> provide flexible hash parameter binding and filtering system,
added experimental expand me...
yuki-kimoto authored on 2010-10-20
864
and suger methods, such as C<insert()>, C<update()>, C<delete()>, C<select()>
updated document
yuki-kimoto authored on 2010-08-08
865
to execute SQL easily.
removed reconnect method
yuki-kimoto authored on 2010-05-28
866

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

            
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
873
See L<DBIx::Custom::Guides> for more details.
updated document
yuki-kimoto authored on 2010-08-08
874

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
887
    $dbi          = $dbi->cache_method(\&cache_method);
888
    $cache_method = $dbi->cache_method
889

            
890
Method to set and get caches.
891

            
892
B<Example:>
893

            
894
    $dbi->cache_method(
895
        sub {
896
            my $self = shift;
897
            
898
            $self->{_cached} ||= {};
899
            
900
            if (@_ > 1) {
901
                $self->{_cached}{$_[0]} = $_[1] 
902
            }
903
            else {
904
                return $self->{_cached}{$_[0]}
905
            }
906
        }
907
    );
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
908

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

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

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

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

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

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

            
added dbi_options attribute
kimoto authored on 2010-12-20
924
=head2 C<dbi_options>
925

            
926
    my $dbi_options = $dbi->dbi_options;
927
    $dbi            = $dbi->dbi_options($dbi_options);
928

            
929
DBI options.
930
C<connect()> method use this value to connect the database.
931

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
939
Filter functions.
940
"encode_utf8" and "decode_utf8" is registered by default.
941

            
942
=head2 C<filter_check>
943

            
944
    my $filter_check = $dbi->filter_check;
945
    $dbi             = $dbi->filter_check(0);
946

            
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
947
B<this attribute is now deprecated and has no mean
948
because check is always done>. 
cleanup
yuki-kimoto authored on 2010-10-17
949

            
950
=head2 C<password>
951

            
952
    my $password = $dbi->password;
953
    $dbi         = $dbi->password('lkj&le`@s');
954

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
977
    my $user = $dbi->user;
978
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
979

            
cleanup
yuki-kimoto authored on 2010-10-17
980
User name.
981
C<connect()> method use this value to connect the database.
982
    
983
=head1 METHODS
added commit method
yuki-kimoto authored on 2010-05-27
984

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

            
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
988
=head2 C<(experimental) auto_filter >
989

            
990
    $dbi->auto_filter(
991
        $table,
992
        [$column1, $bind_filter1, $fetch_filter1],
993
        [$column2, $bind_filter2, $fetch_filter2],
994
        [...],
995
    );
996

            
997
C<auto_filter> is automatically filter for columns of table.
998
This have effect C<insert>, C<update>, C<delete>. C<select>
cleanup
Yuki Kimoto authored on 2010-12-21
999
and L<DBIx::Custom::Result> object. but this has'nt C<execute> method.
1000

            
1001
If you want to have effect <execute< method, use C<auto_filter_table>
1002
arguments.
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1003

            
cleanup
Yuki Kimoto authored on 2010-12-21
1004
    $result = $dbi->execute(
1005
        "select * from table1 where {= key1} and {= key2};",
1006
         param => {key1 => 1, key2 => 2},
1007
         auto_filter_table => ['table1']
1008
    );
1009
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1010
B<Example:>
1011

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1012
    $dbi->auto_filter('book', 'sale_date', 'to_date', 'date_to');
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1013

            
1014
=head2 C<begin_work>
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1015

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

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

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

            
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1024
=head2 C<commit>
cleanup
yuki-kimoto authored on 2010-10-17
1025

            
1026
    $dbi->commit;
1027

            
1028
Commit transaction.
1029
This is same as L<DBI>'s C<commit>.
1030

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1041
=head2 C<create_query>
1042
    
1043
    my $query = $dbi->create_query(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1044
        "select * from book where {= author} and {like title};"
cleanup
yuki-kimoto authored on 2010-10-17
1045
    );
update document
yuki-kimoto authored on 2009-11-19
1046

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

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

            
cleanup
Yuki Kimoto authored on 2010-12-21
1054
=head2 C<(deprecated) default_bind_filter>
1055

            
cleanup
Yuki Kimoto authored on 2010-12-22
1056
    my $default_bind_filter = $dbi->default_bind_filter;
1057
    $dbi                    = $dbi->default_bind_filter($fname);
cleanup
Yuki Kimoto authored on 2010-12-21
1058

            
1059
Default filter when parameter binding is executed.
1060

            
1061
=head2 C<(deprecated) default_fetch_filter>
1062

            
cleanup
Yuki Kimoto authored on 2010-12-22
1063
    my $default_fetch_filter = $dbi->default_fetch_filter;
cleanup
Yuki Kimoto authored on 2010-12-21
1064
    $dbi = $dbi->default_fetch_filter($fname);
1065

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1078
    my $result = $dbi->execute(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1079
        "select * from book where {= author} and {like title}", 
cleanup
yuki-kimoto authored on 2010-10-17
1080
        param => {author => 'Ken', title => '%Perl%'}
1081
    );
1082
    
1083
    while (my $row = $result->fetch) {
1084
        my $author = $row->[0];
1085
        my $title  = $row->[1];
1086
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
1087

            
added experimental expand me...
yuki-kimoto authored on 2010-10-20
1088
=head2 C<(experimental) expand>
1089

            
1090
    my %expand = $dbi->expand($source);
1091

            
1092
The following hash
1093

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1094
    {book => {title => 'Perl', author => 'Ken'}}
added experimental expand me...
yuki-kimoto authored on 2010-10-20
1095

            
1096
is expanded to
1097

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1098
    ('book.title' => 'Perl', 'book.author' => 'Ken')
added experimental expand me...
yuki-kimoto authored on 2010-10-20
1099

            
1100
This is used in C<select()>
1101

            
1102

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1106
    $dbi->delete(table  => $table,
1107
                 where  => \%where,
1108
                 append => $append,
1109
                 filter => \%filter);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1110

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1111
Execute delete statement.
1112
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
1113
C<table> is a table name.
1114
C<where> is where clause. this must be hash reference.
1115
C<append> is a string added at the end of the SQL statement.
1116
C<filter> is filters when parameter binding is executed.
cleanup
yuki-kimoto authored on 2010-08-09
1117
Return value of C<delete()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1118

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

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1121
    $dbi->delete(table  => 'book',
removed register_format()
yuki-kimoto authored on 2010-05-26
1122
                 where  => {id => 5},
1123
                 append => 'some statement',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1124
                 filter => {id => 'encode_utf8'});
version 0.0901
yuki-kimoto authored on 2009-12-17
1125

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

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

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

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1135
B<Example:>
removed register_format()
yuki-kimoto authored on 2010-05-26
1136
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1137
    $dbi->delete_all(table => 'book');
packaging one directory
yuki-kimoto authored on 2009-11-16
1138

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

            
1141
    $dbi->helper(
1142
        update_or_insert => sub {
1143
            my $self = shift;
1144
            # do something
1145
        },
1146
        find_or_create   => sub {
1147
            my $self = shift;
1148
            # do something
1149
        }
1150
    );
1151

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

            
1154
    $dbi->update_or_insert;
1155
    $dbi->find_or_create;
1156

            
cleanup
yuki-kimoto authored on 2010-10-17
1157
=head2 C<insert>
1158

            
1159
    $dbi->insert(table  => $table, 
1160
                 param  => \%param,
1161
                 append => $append,
1162
                 filter => \%filter);
1163

            
1164
Execute insert statement.
1165
C<insert> method have C<table>, C<param>, C<append>
1166
and C<filter> arguments.
1167
C<table> is a table name.
1168
C<param> is the pairs of column name value. this must be hash reference.
1169
C<append> is a string added at the end of the SQL statement.
1170
C<filter> is filters when parameter binding is executed.
1171
This is overwrites C<default_bind_filter>.
1172
Return value of C<insert()> is the count of affected rows.
1173

            
1174
B<Example:>
1175

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1176
    $dbi->insert(table  => 'book', 
cleanup
yuki-kimoto authored on 2010-10-17
1177
                 param  => {title => 'Perl', author => 'Taro'},
1178
                 append => "some statement",
1179
                 filter => {title => 'encode_utf8'})
1180

            
added dbi_options attribute
kimoto authored on 2010-12-20
1181
=head2 C<new>
1182

            
1183
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname",
1184
                                    user => 'ken', password => '!LFKD%$&');
1185

            
1186
Create a new L<DBIx::Custom> object.
1187

            
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1188
=head2 C<(experimental) iterate_all_columns>
1189

            
1190
    $dbi->iterate_all_columns(
1191
        sub {
1192
            my ($table, $column, $column_info) = @_;
1193
            
1194
            # do something;
1195
        }
1196
    );
1197

            
1198
Iterate all columns of all tables. Argument is callback.
1199
You can do anything by callback.
1200

            
cleanup
yuki-kimoto authored on 2010-10-17
1201
=head2 C<register_filter>
1202

            
1203
    $dbi->register_filter(%filters);
1204
    $dbi->register_filter(\%filters);
1205
    
1206
Register filter. Registered filters is available in the following attributes
1207
or arguments.
1208

            
1209
=over 4
1210

            
1211
=item *
1212

            
1213
C<filter> argument of C<insert()>, C<update()>,
1214
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1215
methods
1216

            
1217
=item *
1218

            
1219
C<execute()> method
1220

            
1221
=item *
1222

            
1223
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1224

            
1225
=item *
1226

            
1227
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1228

            
1229
=back
1230

            
1231
B<Example:>
1232

            
1233
    $dbi->register_filter(
1234
        encode_utf8 => sub {
1235
            my $value = shift;
1236
            
1237
            require Encode;
1238
            
1239
            return Encode::encode('UTF-8', $value);
1240
        },
1241
        decode_utf8 => sub {
1242
            my $value = shift;
1243
            
1244
            require Encode;
1245
            
1246
            return Encode::decode('UTF-8', $value)
1247
        }
1248
    );
1249

            
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1250
=head2 C<rollback>
cleanup
yuki-kimoto authored on 2010-10-17
1251

            
1252
    $dbi->rollback;
1253

            
1254
Rollback transaction.
1255
This is same as L<DBI>'s C<rollback>.
1256

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1257
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1258
    
cleanup
yuki-kimoto authored on 2010-08-05
1259
    my $result = $dbi->select(table    => $table,
1260
                              column   => [@column],
1261
                              where    => \%where,
1262
                              append   => $append,
1263
                              relation => \%relation,
1264
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1265

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1266
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1267
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1268
C<relation> and C<filter> arguments.
1269
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1270
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1271
C<append> is a string added at the end of the SQL statement.
1272
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
1273

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

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1276
    # select * from book;
1277
    my $result = $dbi->select(table => 'book');
packaging one directory
yuki-kimoto authored on 2009-11-16
1278
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1279
    # select * from book where title = ?;
1280
    my $result = $dbi->select(table => 'book', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1281
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1282
    # select title, author from book where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1283
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1284
        table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1285
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1286
        where  => {id => 1},
1287
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1288
    );
1289
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1290
    # select book.name as book_name from book, rental
1291
    # where book.id = rental.book_id;
added commit method
yuki-kimoto authored on 2010-05-27
1292
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1293
        table    => ['book', 'rental'],
1294
        column   => ['book.name as book_name']
1295
        relation => {'book.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1296
    );
1297

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

            
1301
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1302
        table  => 'book',
cleanup
yuki-kimoto authored on 2010-08-09
1303
        column => ['title', 'author'],
1304
        where  => ['{= title} or {like author}',
1305
                   {title => '%Perl%', author => 'Ken'}]
1306
    );
1307

            
1308
First element is a string. it contains tags,
1309
such as "{= title} or {like author}".
1310
Second element is paramters.
1311

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1314
    $dbi->update(table  => $table, 
1315
                 param  => \%params,
1316
                 where  => \%where,
1317
                 append => $append,
1318
                 filter => \%filter)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1319

            
cleanup
yuki-kimoto authored on 2010-10-17
1320
Execute update statement.
1321
C<update> method have C<table>, C<param>, C<where>, C<append>
1322
and C<filter> arguments.
1323
C<table> is a table name.
1324
C<param> is column-value pairs. this must be hash reference.
1325
C<where> is where clause. this must be hash reference.
1326
C<append> is a string added at the end of the SQL statement.
1327
C<filter> is filters when parameter binding is executed.
1328
This is overwrites C<default_bind_filter>.
1329
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1330

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

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1333
    $dbi->update(table  => 'book',
cleanup
yuki-kimoto authored on 2010-10-17
1334
                 param  => {title => 'Perl', author => 'Taro'},
1335
                 where  => {id => 5},
1336
                 append => "some statement",
1337
                 filter => {title => 'encode_utf8'});
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1338

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1339
=head2 C<(experimental) txn_scope>
1340

            
1341
    {
1342
        my $txn = $dbi->txn_scope;
1343
        $dbi->insert(table => 'book', param => {title => 'Perl'});
1344
        $dbi->insert(table => 'book', param => {title => 'Good days'});
1345
        $txn->commit;
1346
    }
1347

            
1348
Create transaction scope. If you escape scope(that is { .. }) and commited,
1349
Rollback is automatically done.
1350

            
1351
Note that this is feature of L<DBIx::TransactionManager>
1352
L<DBIx::TransactionManager> is required.
1353

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1356
    $dbi->update_all(table  => $table, 
1357
                     param  => \%params,
1358
                     filter => \%filter,
1359
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1360

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

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

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1368
    $dbi->update_all(table  => 'book', 
cleanup
yuki-kimoto authored on 2010-10-17
1369
                     param  => {author => 'taro'},
1370
                     filter => {author => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
1371

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

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

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

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

            
1380
C<< <kimoto.yuki at gmail.com> >>
1381

            
1382
L<http://github.com/yuki-kimoto/DBIx-Custom>
1383

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1384
=head1 AUTHOR
1385

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

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

            
1390
Copyright 2009 Yuki Kimoto, all rights reserved.
1391

            
1392
This program is free software; you can redistribute it and/or modify it
1393
under the same terms as Perl itself.
1394

            
1395
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1396

            
1397