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

            
add examples
Yuki Kimoto authored on 2011-01-07
3
our $VERSION = '0.1628';
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);
cleanup (removed undocumente...
yuki-kimoto authored on 2010-11-10
23
__PACKAGE__->attr(filters => sub {
24
    {
25
        encode_utf8 => sub { encode_utf8($_[0]) },
26
        decode_utf8 => sub { decode_utf8($_[0]) }
27
    }
28
});
added check_filter attribute
yuki-kimoto authored on 2010-08-08
29
__PACKAGE__->attr(filter_check => 1);
cleanup
yuki-kimoto authored on 2010-10-17
30
__PACKAGE__->attr(query_builder  => sub {DBIx::Custom::QueryBuilder->new});
31
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result');
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
32
__PACKAGE__->attr(table_class => 'DBIx::Custom::Table');
cleanup
yuki-kimoto authored on 2010-10-17
33

            
34
# DBI methods
35
foreach my $method (qw/begin_work commit rollback/) {
36
    my $code = sub {
37
        my $self = shift;
38
        my $ret = eval {$self->dbh->$method};
39
        croak $@ if $@;
40
        return $ret;
41
    };
42
    no strict 'refs';
43
    my $pkg = __PACKAGE__;
44
    *{"${pkg}::$method"} = $code;
45
};
46

            
added helper method
yuki-kimoto authored on 2010-10-17
47
our $AUTOLOAD;
48

            
49
sub AUTOLOAD {
50
    my $self = shift;
51

            
52
    # Method
53
    my ($package, $method) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
54

            
55
    # Helper
56
    $self->{_helpers} ||= {};
57
    croak qq/Can't locate object method "$method" via "$package"/
58
      unless my $helper = $self->{_helpers}->{$method};
59

            
60
    # Run
61
    return $self->$helper(@_);
62
}
63

            
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
64
sub auto_filter {
65
    my $self = shift;
66
    
cleanup
Yuki Kimoto authored on 2010-12-22
67
    $self->{auto_filter} ||= {};
68
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
69
    # Table
70
    my $table = shift;
71
    
cleanup
Yuki Kimoto authored on 2010-12-22
72
    if (@_) {
73
        # Column infomations
74
        my @cs = @_;
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
75
        
cleanup
Yuki Kimoto authored on 2010-12-22
76
        # Initialize filters
cleanup
Yuki Kimoto authored on 2011-01-12
77
        $self->{auto_filter}{out} ||= {};
78
        $self->{auto_filter}{in} ||= {};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
79
        
cleanup
Yuki Kimoto authored on 2010-12-22
80
        # Create auto filters
81
        foreach my $c (@cs) {
82
            croak "Usage \$dbi->auto_filter(" .
cleanup
Yuki Kimoto authored on 2011-01-12
83
                  "TABLE, [COLUMN, OUT_FILTER, IN_FILTER], [...])"
cleanup
Yuki Kimoto authored on 2010-12-22
84
              unless ref $c eq 'ARRAY' && @$c == 3;
85
            
86
            # Column
87
            my $column = $c->[0];
88
            
89
            # Bind filter
cleanup
Yuki Kimoto authored on 2011-01-12
90
            my $out_filter  = $c->[1];
91
            if (ref $out_filter eq 'CODE') {
92
    	        $self->{auto_filter}{out}{$table}{$column}
93
    	          = $out_filter;
94
    	        $self->{auto_filter}{out}{$table}{"$table.$column"}
95
    	          = $out_filter;
cleanup
Yuki Kimoto authored on 2010-12-22
96
            }
97
            else {
cleanup
Yuki Kimoto authored on 2011-01-12
98
    	        croak qq{"$out_filter" is not registered}
99
    	          unless exists $self->filters->{$out_filter};
cleanup
Yuki Kimoto authored on 2010-12-22
100
    	        
cleanup
Yuki Kimoto authored on 2011-01-12
101
    	        $self->{auto_filter}{out}{$table}{$column}
102
    	          = $self->filters->{$out_filter};
103
    	        $self->{auto_filter}{out}{$table}{"$table.$column"}
104
    	          = $self->filters->{$out_filter};
cleanup
Yuki Kimoto authored on 2010-12-22
105
    	    }
106
            
107
            # Fetch filter
cleanup
Yuki Kimoto authored on 2011-01-12
108
            my $in_filter = $c->[2];
109
            if (ref $in_filter eq 'CODE') {
110
    	        $self->{auto_filter}{in}{$table}{$column}
111
    	          = $in_filter;
112
    	        $self->{auto_filter}{in}{$table}{"$table.$column"}
113
    	          = $in_filter;
cleanup
Yuki Kimoto authored on 2010-12-22
114
            }
115
            else {
cleanup
Yuki Kimoto authored on 2011-01-12
116
                croak qq{"$in_filter" is not registered}
117
                  unless exists $self->filters->{$in_filter};
118
                $self->{auto_filter}{in}{$table}{$column}
119
                  = $self->filters->{$in_filter};
120
                $self->{auto_filter}{in}{$table}{"$table.$column"}
121
                  = $self->filters->{$in_filter};
cleanup
Yuki Kimoto authored on 2010-12-22
122
            }
cleanup
Yuki Kimoto authored on 2010-12-21
123
        }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
124
        
cleanup
Yuki Kimoto authored on 2010-12-22
125
        return $self;
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
126
    }
127
    
cleanup
Yuki Kimoto authored on 2010-12-22
128
    return $self->{auto_filter};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
129
}
130

            
added helper method
yuki-kimoto authored on 2010-10-17
131
sub helper {
132
    my $self = shift;
133
    
134
    # Merge
135
    my $helpers = ref $_[0] eq 'HASH' ? $_[0] : {@_};
136
    $self->{_helpers} = {%{$self->{_helpers} || {}}, %$helpers};
137
    
138
    return $self;
139
}
140

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

            
cleanup
yuki-kimoto authored on 2010-10-17
197
sub create_query {
198
    my ($self, $source) = @_;
update document
yuki-kimoto authored on 2010-01-30
199
    
cleanup
yuki-kimoto authored on 2010-10-17
200
    # Cache
201
    my $cache = $self->cache;
update document
yuki-kimoto authored on 2010-01-30
202
    
cleanup
yuki-kimoto authored on 2010-10-17
203
    # Create query
204
    my $query;
205
    if ($cache) {
206
        
207
        # Get query
208
        my $q = $self->cache_method->($self, $source);
209
        
210
        # Create query
211
        $query = DBIx::Custom::Query->new($q) if $q;
212
    }
213
    
214
    unless ($query) {
cleanup insert
yuki-kimoto authored on 2010-04-28
215

            
cleanup
yuki-kimoto authored on 2010-10-17
216
        # Create SQL object
217
        my $builder = $self->query_builder;
218
        
219
        # Create query
220
        $query = $builder->build_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
221

            
cleanup
yuki-kimoto authored on 2010-10-17
222
        # Cache query
223
        $self->cache_method->($self, $source,
224
                             {sql     => $query->sql, 
225
                              columns => $query->columns})
226
          if $cache;
cleanup insert
yuki-kimoto authored on 2010-04-28
227
    }
228
    
cleanup
yuki-kimoto authored on 2010-10-17
229
    # Prepare statement handle
230
    my $sth;
231
    eval { $sth = $self->dbh->prepare($query->{sql})};
232
    $self->_croak($@, qq{. SQL: "$query->{sql}"}) if $@;
packaging one directory
yuki-kimoto authored on 2009-11-16
233
    
cleanup
yuki-kimoto authored on 2010-10-17
234
    # Set statement handle
235
    $query->sth($sth);
packaging one directory
yuki-kimoto authored on 2009-11-16
236
    
cleanup
yuki-kimoto authored on 2010-10-17
237
    return $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
238
}
239

            
cleanup
yuki-kimoto authored on 2010-10-17
240
our %VALID_DELETE_ARGS
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
241
  = 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
242

            
cleanup
yuki-kimoto authored on 2010-10-17
243
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
244
    my ($self, %args) = @_;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
245
    
246
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
247
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
248
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
249
          unless $VALID_DELETE_ARGS{$name};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
250
    }
251
    
252
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
253
    my $table            = $args{table} || '';
254
    my $where            = $args{where} || {};
cleanup
yuki-kimoto authored on 2010-10-17
255
    my $append = $args{append};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
256
    my $filter           = $args{filter};
cleanup
yuki-kimoto authored on 2010-10-17
257
    my $allow_delete_all = $args{allow_delete_all};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
258

            
259
    my $auto_filter_table = exists $args{auto_filter_table}
260
                          ? $args{auto_filter_table}
261
                          : [$table];
262
    $auto_filter_table ||= [];    
263

            
packaging one directory
yuki-kimoto authored on 2009-11-16
264
    # Where keys
removed register_format()
yuki-kimoto authored on 2010-05-26
265
    my @where_keys = keys %$where;
packaging one directory
yuki-kimoto authored on 2009-11-16
266
    
267
    # Not exists where keys
add tests
yuki-kimoto authored on 2010-08-10
268
    croak qq{"where" argument must be specified and } .
269
          qq{contains the pairs of column name and value}
cleanup
yuki-kimoto authored on 2010-10-17
270
      if !@where_keys && !$allow_delete_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
271
    
272
    # Where clause
273
    my $where_clause = '';
274
    if (@where_keys) {
275
        $where_clause = 'where ';
add tests
yuki-kimoto authored on 2010-08-10
276
        $where_clause .= "{= $_} and " for @where_keys;
packaging one directory
yuki-kimoto authored on 2009-11-16
277
        $where_clause =~ s/ and $//;
278
    }
279
    
add tests
yuki-kimoto authored on 2010-08-10
280
    # Source of SQL
cleanup
yuki-kimoto authored on 2010-10-17
281
    my $source = "delete from $table $where_clause";
add tests
yuki-kimoto authored on 2010-08-10
282
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
283
    
284
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
285
    my $ret_val = $self->execute(
286
        $source, param  => $where, filter => $filter,
287
        auto_filter_table => $auto_filter_table);
packaging one directory
yuki-kimoto authored on 2009-11-16
288
    
289
    return $ret_val;
290
}
291

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

            
added helper method
yuki-kimoto authored on 2010-10-17
294
sub DESTROY { }
295

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

            
cleanup
yuki-kimoto authored on 2010-10-17
298
sub execute{
299
    my ($self, $query, %args)  = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
300
    
301
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
302
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
303
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
304
          unless $VALID_EXECUTE_ARGS{$name};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
305
    }
306
    
cleanup
yuki-kimoto authored on 2010-10-17
307
    my $params = $args{param} || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
308
    
cleanup
yuki-kimoto authored on 2010-10-17
309
    # First argument is the soruce of SQL
310
    $query = $self->create_query($query)
311
      unless ref $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
312
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
313
    # Auto filter
314
    my $auto_filter = {};
315
    my $auto_filter_tables = $args{auto_filter_table} || [];
316
    foreach my $table (@$auto_filter_tables) {
317
        $auto_filter = {
318
            %$auto_filter,
cleanup
Yuki Kimoto authored on 2011-01-12
319
            %{$self->{auto_filter}{in}->{$table} || {}}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
320
        }
321
    }
322
    
323
    # Filter
cleanup
yuki-kimoto authored on 2010-10-17
324
    my $filter = $args{filter} || $query->filter || {};
cleanup
Yuki Kimoto authored on 2010-12-21
325
    foreach my $column (keys %$filter) {
326
        my $fname = $filter->{$column};
327
        unless (ref $fname eq 'CODE') {
328
          croak qq{"$fname" is not registered"}
329
            unless exists $self->filters->{$fname};
330
          
331
          $filter->{$column} = $self->filters->{$fname};
332
        }
333
    }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
334
    $filter = {%$auto_filter, %$filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
335
    
cleanup
Yuki Kimoto authored on 2011-01-12
336
    # Create bind values
337
    my $binds = $self->_build_binds($params, $query->columns, $filter);
cleanup
yuki-kimoto authored on 2010-10-17
338
    
339
    # Execute
340
    my $sth      = $query->sth;
341
    my $affected;
cleanup
Yuki Kimoto authored on 2011-01-12
342
    eval {$affected = $sth->execute(@$binds)};
cleanup
yuki-kimoto authored on 2010-10-17
343
    $self->_croak($@) if $@;
344
    
345
    # Return resultset if select statement is executed
346
    if ($sth->{NUM_OF_FIELDS}) {
347
        
cleanup
Yuki Kimoto authored on 2011-01-12
348
        # Auto in filter
349
        my $auto_in_filter = {};
350
	      foreach my $table (@$auto_filter_tables) {
351
	          $auto_in_filter = {
352
	              %$auto_filter,
353
	              %{$self->{auto_filter}{in}{$table} || {}}
354
	          }
355
	      }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
356
	    
357
		# Result
358
		my $result = $self->result_class->new(
cleanup
Yuki Kimoto authored on 2010-12-22
359
            sth            => $sth,
360
            filters        => $self->filters,
361
            filter_check   => $self->filter_check,
cleanup
Yuki Kimoto authored on 2011-01-12
362
            default_filter => $self->{default_in_filter},
363
            _auto_filter   => $auto_in_filter || {}
cleanup
yuki-kimoto authored on 2010-10-17
364
        );
365

            
366
        return $result;
367
    }
368
    return $affected;
369
}
370

            
added experimental expand me...
yuki-kimoto authored on 2010-10-20
371
sub expand {
372
    my $self = shift;
373
    my $source = ref $_[0] eq 'HASH' ? $_[0] : {@_};
374
    my $table = (keys %$source)[0];
375
    my $param = $source->{$table};
376
    
377
    # Expand table name
378
    my $expand = {};
379
    foreach my $column (keys %$param) {
380
        $expand->{"$table.$column"} = $param->{$column};
381
    }
382
    
383
    return %$expand;
384
}
385

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

            
391
    # Check arguments
392
    foreach my $name (keys %args) {
393
        croak qq{"$name" is invalid argument}
394
          unless $VALID_INSERT_ARGS{$name};
packaging one directory
yuki-kimoto authored on 2009-11-16
395
    }
396
    
cleanup
yuki-kimoto authored on 2010-10-17
397
    # Arguments
398
    my $table  = $args{table} || '';
399
    my $param  = $args{param} || {};
400
    my $append = $args{append} || '';
401
    my $filter = $args{filter};
402
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
403
    my $auto_filter_table = exists $args{auto_filter_table}
404
                          ? $args{auto_filter_table}
405
                          : [$table];
406
    $auto_filter_table ||= [];
407
    
cleanup
yuki-kimoto authored on 2010-10-17
408
    # Insert keys
409
    my @insert_keys = keys %$param;
410
    
411
    # Templte for insert
412
    my $source = "insert into $table {insert_param "
413
               . join(' ', @insert_keys) . '}';
add tests
yuki-kimoto authored on 2010-08-10
414
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
415
    
416
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
417
    my $ret_val = $self->execute(
418
        $source,
419
        param  => $param,
420
        filter => $filter,
421
        auto_filter_table => $auto_filter_table
422
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
423
    
424
    return $ret_val;
425
}
426

            
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
427
sub iterate_all_columns {
428
    my ($self, $cb) = @_;
429
    
430
    # Iterate all tables
431
    my $sth_tables = $self->dbh->table_info;
432
    while (my $table_info = $sth_tables->fetchrow_hashref) {
433
        
434
        # Table
435
        my $table = $table_info->{TABLE_NAME};
436
        
437
        # Iterate all columns
438
        my $sth_columns = $self->dbh->column_info(undef, undef, $table, '%');
439
        while (my $column_info = $sth_columns->fetchrow_hashref) {
440
            my $column = $column_info->{COLUMN_NAME};
441
            $cb->($table, $column, $column_info);
442
        }
443
    }
444
}
445

            
added dbi_options attribute
kimoto authored on 2010-12-20
446
sub new {
447
    my $self = shift->SUPER::new(@_);
448
    
449
    # Check attribute names
450
    my @attrs = keys %$self;
451
    foreach my $attr (@attrs) {
452
        croak qq{"$attr" is invalid attribute name}
453
          unless $self->can($attr);
454
    }
455
    
456
    return $self;
457
}
458

            
cleanup
yuki-kimoto authored on 2010-10-17
459
sub register_filter {
460
    my $invocant = shift;
461
    
462
    # Register filter
463
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
464
    $invocant->filters({%{$invocant->filters}, %$filters});
465
    
466
    return $invocant;
467
}
packaging one directory
yuki-kimoto authored on 2009-11-16
468

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
472
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
473
    my ($self, %args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
474
    
refactoring select
yuki-kimoto authored on 2010-04-28
475
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
476
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
477
        croak qq{"$name" is invalid argument}
refactoring select
yuki-kimoto authored on 2010-04-28
478
          unless $VALID_SELECT_ARGS{$name};
479
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
480
    
refactoring select
yuki-kimoto authored on 2010-04-28
481
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
482
    my $tables = $args{table} || [];
removed register_format()
yuki-kimoto authored on 2010-05-26
483
    $tables = [$tables] unless ref $tables eq 'ARRAY';
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
484
    my $columns  = $args{column} || [];
update document
yuki-kimoto authored on 2010-08-07
485
    my $where    = $args{where};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
486
    my $relation = $args{relation};
487
    my $append   = $args{append};
488
    my $filter   = $args{filter};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
489

            
490
    my $auto_filter_table = exists $args{auto_filter_table}
491
                          ? $args{auto_filter_table}
492
                          : $tables;
packaging one directory
yuki-kimoto authored on 2009-11-16
493
    
add tests
yuki-kimoto authored on 2010-08-10
494
    # Source of SQL
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
495
    my $source = 'select ';
packaging one directory
yuki-kimoto authored on 2009-11-16
496
    
added commit method
yuki-kimoto authored on 2010-05-27
497
    # Column clause
packaging one directory
yuki-kimoto authored on 2009-11-16
498
    if (@$columns) {
499
        foreach my $column (@$columns) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
500
            $source .= "$column, ";
packaging one directory
yuki-kimoto authored on 2009-11-16
501
        }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
502
        $source =~ s/, $/ /;
packaging one directory
yuki-kimoto authored on 2009-11-16
503
    }
504
    else {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
505
        $source .= '* ';
packaging one directory
yuki-kimoto authored on 2009-11-16
506
    }
507
    
added commit method
yuki-kimoto authored on 2010-05-27
508
    # Table
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
509
    $source .= 'from ';
packaging one directory
yuki-kimoto authored on 2009-11-16
510
    foreach my $table (@$tables) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
511
        $source .= "$table, ";
packaging one directory
yuki-kimoto authored on 2009-11-16
512
    }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
513
    $source =~ s/, $/ /;
packaging one directory
yuki-kimoto authored on 2009-11-16
514
    
added commit method
yuki-kimoto authored on 2010-05-27
515
    # Where clause
update document
yuki-kimoto authored on 2010-08-07
516
    my $param;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
517
    if (ref $where eq 'HASH' && keys %$where) {
update document
yuki-kimoto authored on 2010-08-07
518
        $param = $where;
519
        $source .= 'where (';
520
        foreach my $where_key (keys %$where) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
521
            $source .= "{= $where_key} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
522
        }
update document
yuki-kimoto authored on 2010-08-07
523
        $source =~ s/ and $//;
524
        $source .= ') ';
525
    }
526
    elsif (ref $where eq 'ARRAY') {
527
        my$where_str = $where->[0] || '';
528
        $param = $where->[1];
529
        
530
        $source .= "where ($where_str) ";
packaging one directory
yuki-kimoto authored on 2009-11-16
531
    }
532
    
added commit method
yuki-kimoto authored on 2010-05-27
533
    # Relation
534
    if ($relation) {
update document
yuki-kimoto authored on 2010-08-07
535
        $source .= $where ? "and " : "where ";
added commit method
yuki-kimoto authored on 2010-05-27
536
        foreach my $rkey (keys %$relation) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
537
            $source .= "$rkey = " . $relation->{$rkey} . " and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
538
        }
539
    }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
540
    $source =~ s/ and $//;
added commit method
yuki-kimoto authored on 2010-05-27
541
    
542
    # Append some statement
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
543
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
544
    
545
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
546
    my $result = $self->execute(
547
        $source, param  => $param, filter => $filter,
548
        auto_filter_table => $auto_filter_table);    
packaging one directory
yuki-kimoto authored on 2009-11-16
549
    
550
    return $result;
551
}
552

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
553
sub table {
554
    my $self = shift;
555
    my $name = shift;
556
    
557
    # Table class
558
    my $table_class = $self->table_class;
559
    croak qq{Invalid table class name "$table_class"}
560
      unless $table_class =~ /^[\w:]+$/;
561
    unless ($table_class->can('isa')) {
562
        eval "require $table_class";
563
        croak $@ if $@;
564
    }
565
    # Create table
566
    $self->{_tables} ||= {};
567
    $self->{_tables}->{$name}
568
        = $table_class->new(name => $name, dbi => $self)
569
      unless defined $self->{_tables}->{$name};
570
    
571
    # Helper
572
    $self->{_tables}->{$name}->helper(@_) if @_;
573
    
574
    return $self->{_tables}{$name};
575
}
576

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
577
sub txn_scope {
578
    my $self = shift;
579
    
580
    require DBIx::TransactionManager;
581
    
582
    $self->{_transaction_manager}
583
      ||= DBIx::TransactionManager->new($self->dbh);
584
    
585
    return $self->{_transaction_manager}->txn_scope;
586
}
587

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

            
592
sub update {
593
    my ($self, %args) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
594
    
cleanup
yuki-kimoto authored on 2010-10-17
595
    # Check arguments
596
    foreach my $name (keys %args) {
597
        croak qq{"$name" is invalid argument}
598
          unless $VALID_UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
599
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
600
    
cleanup
yuki-kimoto authored on 2010-10-17
601
    # Arguments
602
    my $table            = $args{table} || '';
603
    my $param            = $args{param} || {};
604
    my $where            = $args{where} || {};
605
    my $append = $args{append} || '';
606
    my $filter           = $args{filter};
607
    my $allow_update_all = $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
608
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
609
    my $auto_filter_table = exists $args{auto_filter_table}
610
                          ? $args{auto_filter_table}
611
                          : [$table];
612
    $auto_filter_table ||= [];
613
    
cleanup
yuki-kimoto authored on 2010-10-17
614
    # Update keys
615
    my @update_keys = keys %$param;
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
616
    
cleanup
yuki-kimoto authored on 2010-10-17
617
    # Where keys
618
    my @where_keys = keys %$where;
removed reconnect method
yuki-kimoto authored on 2010-05-28
619
    
cleanup
yuki-kimoto authored on 2010-10-17
620
    # Not exists where keys
621
    croak qq{"where" argument must be specified and } .
622
          qq{contains the pairs of column name and value}
623
      if !@where_keys && !$allow_update_all;
removed experimental registe...
yuki-kimoto authored on 2010-08-24
624
    
cleanup
yuki-kimoto authored on 2010-10-17
625
    # Update clause
626
    my $update_clause = '{update_param ' . join(' ', @update_keys) . '}';
removed experimental registe...
yuki-kimoto authored on 2010-08-24
627
    
cleanup
yuki-kimoto authored on 2010-10-17
628
    # Where clause
629
    my $where_clause = '';
630
    my $new_where = {};
removed reconnect method
yuki-kimoto authored on 2010-05-28
631
    
cleanup
yuki-kimoto authored on 2010-10-17
632
    if (@where_keys) {
633
        $where_clause = 'where ';
634
        $where_clause .= "{= $_} and " for @where_keys;
635
        $where_clause =~ s/ and $//;
removed reconnect method
yuki-kimoto authored on 2010-05-28
636
    }
637
    
cleanup
yuki-kimoto authored on 2010-10-17
638
    # Source of SQL
639
    my $source = "update $table $update_clause $where_clause";
640
    $source .= " $append" if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
641
    
cleanup
yuki-kimoto authored on 2010-10-17
642
    # Rearrange parameters
643
    foreach my $wkey (@where_keys) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
644
        
cleanup
yuki-kimoto authored on 2010-10-17
645
        if (exists $param->{$wkey}) {
646
            $param->{$wkey} = [$param->{$wkey}]
647
              unless ref $param->{$wkey} eq 'ARRAY';
648
            
649
            push @{$param->{$wkey}}, $where->{$wkey};
650
        }
651
        else {
652
            $param->{$wkey} = $where->{$wkey};
653
        }
removed reconnect method
yuki-kimoto authored on 2010-05-28
654
    }
cleanup
yuki-kimoto authored on 2010-10-17
655
    
656
    # Execute query
657
    my $ret_val = $self->execute($source, param  => $param, 
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
658
                                 filter => $filter,
659
                                 auto_filter_table => $auto_filter_table);
cleanup
yuki-kimoto authored on 2010-10-17
660
    
661
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
662
}
663

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

            
cleanup
Yuki Kimoto authored on 2011-01-12
666
sub _build_binds {
667
    my ($self, $params, $columns, $filter) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
668
    
cleanup
Yuki Kimoto authored on 2011-01-12
669
    # bind values
670
    my @binds;
add tests
yuki-kimoto authored on 2010-08-08
671
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
672
    # Build bind values
673
    my $count = {};
cleanup
Yuki Kimoto authored on 2011-01-12
674
    foreach my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
675
        
676
        # Value
677
        my $value = ref $params->{$column} eq 'ARRAY'
678
                  ? $params->{$column}->[$count->{$column} || 0]
679
                  : $params->{$column};
680
        
cleanup
Yuki Kimoto authored on 2011-01-12
681
        # Filter
682
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
683
        
cleanup
Yuki Kimoto authored on 2011-01-12
684
        push @binds, $f ? $f->($value) : $value;
removed reconnect method
yuki-kimoto authored on 2010-05-28
685
        
686
        # Count up 
687
        $count->{$column}++;
688
    }
689
    
cleanup
Yuki Kimoto authored on 2011-01-12
690
    return \@binds;
removed reconnect method
yuki-kimoto authored on 2010-05-28
691
}
692

            
cleanup
yuki-kimoto authored on 2010-10-17
693
sub _croak {
694
    my ($self, $error, $append) = @_;
695
    $append ||= "";
696
    
697
    # Verbose
698
    if ($Carp::Verbose) { croak $error }
699
    
700
    # Not verbose
701
    else {
702
        
703
        # Remove line and module infromation
704
        my $at_pos = rindex($error, ' at ');
705
        $error = substr($error, 0, $at_pos);
706
        $error =~ s/\s+$//;
707
        
708
        croak "$error$append";
709
    }
710
}
711

            
cleanup
Yuki Kimoto authored on 2011-01-12
712
# Deprecated
713
__PACKAGE__->attr(cache_method => sub {
714
    sub {
715
        my $self = shift;
716
        
717
        $self->{_cached} ||= {};
718
        
719
        if (@_ > 1) {
720
            $self->{_cached}{$_[0]} = $_[1] 
721
        }
722
        else {
723
            return $self->{_cached}{$_[0]}
724
        }
725
    }
726
});
727

            
728
sub default_bind_filter {
729
    my $self = shift;
730
    
731
    if (@_) {
732
        my $fname = $_[0];
733
        
734
        if (@_ && !$fname) {
735
            $self->{default_out_filter} = undef;
736
        }
737
        else {
738
            croak qq{"$fname" is not registered}
739
              unless exists $self->filters->{$fname};
740
        
741
            $self->{default_out_filter} = $self->filters->{$fname};
742
        }
743
        return $self;
744
    }
745
    
746
    return $self->{default_out_filter};
747
}
748

            
749
sub default_fetch_filter {
750
    my $self = shift;
751
    my $fname = $_[0];
752
    
753
    if (@_) {
754
        if (@_ && !$fname) {
755
            $self->{default_in_filter} = undef;
756
        }
757
        else {
758
            croak qq{"$fname" is not registered}
759
              unless exists $self->filters->{$fname};
760
        
761
            $self->{default_in_filter} = $self->filters->{$fname};
762
        }
763
        
764
        return $self;
765
    }
766
    
767
    return $self->{default_in_filter}
768
}
769

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
772
=head1 NAME
773

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

            
776
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
777

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
786
    # Insert 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
787
    $dbi->insert(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
788
                 param  => {title => 'Perl', author => 'Ken'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
789
                 filter => {title => 'encode_utf8'});
790
    
791
    # Update 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
792
    $dbi->update(table  => 'book', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
793
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
794
                 where  => {id => 5},
795
                 filter => {title => 'encode_utf8'});
796
    
797
    # Update all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
798
    $dbi->update_all(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
799
                     param  => {title => 'Perl'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
800
                     filter => {title => 'encode_utf8'});
801
    
802
    # Delete
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
803
    $dbi->delete(table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
804
                 where  => {author => 'Ken'},
805
                 filter => {title => 'encode_utf8'});
806
    
807
    # Delete all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
808
    $dbi->delete_all(table => 'book');
cleanup
yuki-kimoto authored on 2010-08-05
809

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
812
    # Select
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
813
    my $result = $dbi->select(table => 'book');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
814
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
815
    # Select, more complex
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
816
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
817
        table  => 'book',
update document
yuki-kimoto authored on 2010-05-27
818
        column => [qw/author title/],
819
        where  => {author => 'Ken'},
updated document
yuki-kimoto authored on 2010-08-08
820
        append => 'order by id limit 5',
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
821
        filter => {title => 'encode_utf8'}
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
822
    );
added commit method
yuki-kimoto authored on 2010-05-27
823
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
824
    # Select, join table
added commit method
yuki-kimoto authored on 2010-05-27
825
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
826
        table    => ['book', 'rental'],
827
        column   => ['book.name as book_name']
828
        relation => {'book.id' => 'rental.book_id'}
added commit method
yuki-kimoto authored on 2010-05-27
829
    );
updated document
yuki-kimoto authored on 2010-08-08
830
    
831
    # Select, more flexible where
832
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
833
        table  => 'book',
updated document
yuki-kimoto authored on 2010-08-08
834
        where  => ['{= author} and {like title}', 
835
                   {author => 'Ken', title => '%Perl%'}]
836
    );
cleanup
yuki-kimoto authored on 2010-08-05
837

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
840
    # Execute SQL
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
841
    $dbi->execute("select title from book");
removed register_format()
yuki-kimoto authored on 2010-05-26
842
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
843
    # Execute SQL with hash binding and filtering
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
844
    $dbi->execute("select id from book where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
845
                  param  => {author => 'ken', title => '%Perl%'},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
846
                  filter => {title => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
847

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

            
updated document
yuki-kimoto authored on 2010-08-08
854
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
855

            
856
    # Get DBI object
857
    my $dbh = $dbi->dbh;
858

            
859
Fetch row.
860

            
removed register_format()
yuki-kimoto authored on 2010-05-26
861
    # Fetch
862
    while (my $row = $result->fetch) {
863
        # ...
864
    }
865
    
866
    # Fetch hash
867
    while (my $row = $result->fetch_hash) {
868
        
869
    }
870
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
871
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
872

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
added dbi_options attribute
kimoto authored on 2010-12-20
920
=head2 C<dbi_options>
921

            
922
    my $dbi_options = $dbi->dbi_options;
923
    $dbi            = $dbi->dbi_options($dbi_options);
924

            
925
DBI options.
926
C<connect()> method use this value to connect the database.
927

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
935
Filter functions.
936
"encode_utf8" and "decode_utf8" is registered by default.
937

            
938
=head2 C<filter_check>
939

            
940
    my $filter_check = $dbi->filter_check;
941
    $dbi             = $dbi->filter_check(0);
942

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

            
946
=head2 C<password>
947

            
948
    my $password = $dbi->password;
949
    $dbi         = $dbi->password('lkj&le`@s');
950

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
973
    my $user = $dbi->user;
974
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
975

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

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

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

            
986
    $dbi->auto_filter(
987
        $table,
988
        [$column1, $bind_filter1, $fetch_filter1],
989
        [$column2, $bind_filter2, $fetch_filter2],
990
        [...],
991
    );
992

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

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

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

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

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

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

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

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

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

            
1022
    $dbi->commit;
1023

            
1024
Commit transaction.
1025
This is same as L<DBI>'s C<commit>.
1026

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1062
    my $result = $dbi->execute(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1063
        "select * from book where {= author} and {like title}", 
cleanup
yuki-kimoto authored on 2010-10-17
1064
        param => {author => 'Ken', title => '%Perl%'}
1065
    );
1066
    
1067
    while (my $row = $result->fetch) {
1068
        my $author = $row->[0];
1069
        my $title  = $row->[1];
1070
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
1071

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

            
1074
    my %expand = $dbi->expand($source);
1075

            
1076
The following hash
1077

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

            
1080
is expanded to
1081

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

            
1084
This is used in C<select()>
1085

            
1086

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1090
    $dbi->delete(table  => $table,
1091
                 where  => \%where,
1092
                 append => $append,
1093
                 filter => \%filter);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1094

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1095
Execute delete statement.
1096
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
1097
C<table> is a table name.
1098
C<where> is where clause. this must be hash reference.
1099
C<append> is a string added at the end of the SQL statement.
1100
C<filter> is filters when parameter binding is executed.
cleanup
yuki-kimoto authored on 2010-08-09
1101
Return value of C<delete()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1102

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

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

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

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

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

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

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

            
1125
    $dbi->helper(
1126
        update_or_insert => sub {
1127
            my $self = shift;
1128
            # do something
1129
        },
1130
        find_or_create   => sub {
1131
            my $self = shift;
1132
            # do something
1133
        }
1134
    );
1135

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

            
1138
    $dbi->update_or_insert;
1139
    $dbi->find_or_create;
1140

            
cleanup
yuki-kimoto authored on 2010-10-17
1141
=head2 C<insert>
1142

            
1143
    $dbi->insert(table  => $table, 
1144
                 param  => \%param,
1145
                 append => $append,
1146
                 filter => \%filter);
1147

            
1148
Execute insert statement.
1149
C<insert> method have C<table>, C<param>, C<append>
1150
and C<filter> arguments.
1151
C<table> is a table name.
1152
C<param> is the pairs of column name value. this must be hash reference.
1153
C<append> is a string added at the end of the SQL statement.
1154
C<filter> is filters when parameter binding is executed.
1155
This is overwrites C<default_bind_filter>.
1156
Return value of C<insert()> is the count of affected rows.
1157

            
1158
B<Example:>
1159

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

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

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

            
1170
Create a new L<DBIx::Custom> object.
1171

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

            
1174
    $dbi->iterate_all_columns(
1175
        sub {
1176
            my ($table, $column, $column_info) = @_;
1177
            
1178
            # do something;
1179
        }
1180
    );
1181

            
1182
Iterate all columns of all tables. Argument is callback.
1183
You can do anything by callback.
1184

            
cleanup
yuki-kimoto authored on 2010-10-17
1185
=head2 C<register_filter>
1186

            
1187
    $dbi->register_filter(%filters);
1188
    $dbi->register_filter(\%filters);
1189
    
1190
Register filter. Registered filters is available in the following attributes
1191
or arguments.
1192

            
1193
=over 4
1194

            
1195
=item *
1196

            
1197
C<filter> argument of C<insert()>, C<update()>,
1198
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1199
methods
1200

            
1201
=item *
1202

            
1203
C<execute()> method
1204

            
1205
=item *
1206

            
1207
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1208

            
1209
=item *
1210

            
1211
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1212

            
1213
=back
1214

            
1215
B<Example:>
1216

            
1217
    $dbi->register_filter(
1218
        encode_utf8 => sub {
1219
            my $value = shift;
1220
            
1221
            require Encode;
1222
            
1223
            return Encode::encode('UTF-8', $value);
1224
        },
1225
        decode_utf8 => sub {
1226
            my $value = shift;
1227
            
1228
            require Encode;
1229
            
1230
            return Encode::decode('UTF-8', $value)
1231
        }
1232
    );
1233

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

            
1236
    $dbi->rollback;
1237

            
1238
Rollback transaction.
1239
This is same as L<DBI>'s C<rollback>.
1240

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1241
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1242
    
cleanup
yuki-kimoto authored on 2010-08-05
1243
    my $result = $dbi->select(table    => $table,
1244
                              column   => [@column],
1245
                              where    => \%where,
1246
                              append   => $append,
1247
                              relation => \%relation,
1248
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1249

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1250
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1251
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1252
C<relation> and C<filter> arguments.
1253
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1254
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1255
C<append> is a string added at the end of the SQL statement.
1256
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
1257

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

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1260
    # select * from book;
1261
    my $result = $dbi->select(table => 'book');
packaging one directory
yuki-kimoto authored on 2009-11-16
1262
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1263
    # select * from book where title = ?;
1264
    my $result = $dbi->select(table => 'book', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1265
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1266
    # select title, author from book where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1267
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1268
        table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1269
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1270
        where  => {id => 1},
1271
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1272
    );
1273
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1274
    # select book.name as book_name from book, rental
1275
    # where book.id = rental.book_id;
added commit method
yuki-kimoto authored on 2010-05-27
1276
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1277
        table    => ['book', 'rental'],
1278
        column   => ['book.name as book_name']
1279
        relation => {'book.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1280
    );
1281

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

            
1285
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1286
        table  => 'book',
cleanup
yuki-kimoto authored on 2010-08-09
1287
        column => ['title', 'author'],
1288
        where  => ['{= title} or {like author}',
1289
                   {title => '%Perl%', author => 'Ken'}]
1290
    );
1291

            
1292
First element is a string. it contains tags,
1293
such as "{= title} or {like author}".
1294
Second element is paramters.
1295

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1298
    $dbi->update(table  => $table, 
1299
                 param  => \%params,
1300
                 where  => \%where,
1301
                 append => $append,
1302
                 filter => \%filter)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1303

            
cleanup
yuki-kimoto authored on 2010-10-17
1304
Execute update statement.
1305
C<update> method have C<table>, C<param>, C<where>, C<append>
1306
and C<filter> arguments.
1307
C<table> is a table name.
1308
C<param> is column-value pairs. this must be hash reference.
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.
1312
This is overwrites C<default_bind_filter>.
1313
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1314

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

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

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

            
1325
    {
1326
        my $txn = $dbi->txn_scope;
1327
        $dbi->insert(table => 'book', param => {title => 'Perl'});
1328
        $dbi->insert(table => 'book', param => {title => 'Good days'});
1329
        $txn->commit;
1330
    }
1331

            
1332
Create transaction scope. If you escape scope(that is { .. }) and commited,
1333
Rollback is automatically done.
1334

            
1335
Note that this is feature of L<DBIx::TransactionManager>
1336
L<DBIx::TransactionManager> is required.
1337

            
cleanup
Yuki Kimoto authored on 2011-01-12
1338
=head2 C<(experimental) table>
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1339

            
1340
    $dbi->table('book',
1341
        insert => sub { ... },
1342
        update => sub { ... }
1343
    );
1344
    
1345
    my $table = $dbi->table('book');
1346

            
1347
Create a L<DBIx::Custom::Table> object,
1348
or get a L<DBIx::Custom::Table> object.
1349

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-01-12
1368
=head2 C<(deprecated) default_bind_filter>
1369

            
1370
    my $default_bind_filter = $dbi->default_bind_filter;
1371
    $dbi                    = $dbi->default_bind_filter($fname);
1372

            
1373
Default filter when parameter binding is executed.
1374

            
1375
=head2 C<(deprecated) default_fetch_filter>
1376

            
1377
    my $default_fetch_filter = $dbi->default_fetch_filter;
1378
    $dbi = $dbi->default_fetch_filter($fname);
1379

            
1380
=head2 C<(deprecated) cache_method>
1381

            
1382
    $dbi          = $dbi->cache_method(\&cache_method);
1383
    $cache_method = $dbi->cache_method
1384

            
1385
Method to set and get caches.
1386

            
1387
B<Example:>
1388

            
1389
    $dbi->cache_method(
1390
        sub {
1391
            my $self = shift;
1392
            
1393
            $self->{_cached} ||= {};
1394
            
1395
            if (@_ > 1) {
1396
                $self->{_cached}{$_[0]} = $_[1] 
1397
            }
1398
            else {
1399
                return $self->{_cached}{$_[0]}
1400
            }
1401
        }
1402
    );
1403

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

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

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

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

            
1412
C<< <kimoto.yuki at gmail.com> >>
1413

            
1414
L<http://github.com/yuki-kimoto/DBIx-Custom>
1415

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1416
=head1 AUTHOR
1417

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

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

            
1422
Copyright 2009 Yuki Kimoto, all rights reserved.
1423

            
1424
This program is free software; you can redistribute it and/or modify it
1425
under the same terms as Perl itself.
1426

            
1427
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1428

            
1429