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

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
64
sub apply_filter {
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
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
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
241
  = map { $_ => 1 } qw/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

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

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

            
added helper method
yuki-kimoto authored on 2010-10-17
289
sub DESTROY { }
290

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
291
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter table/;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
292

            
cleanup
yuki-kimoto authored on 2010-10-17
293
sub execute{
294
    my ($self, $query, %args)  = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
295
    
296
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
297
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
298
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
299
          unless $VALID_EXECUTE_ARGS{$name};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
300
    }
301
    
cleanup
yuki-kimoto authored on 2010-10-17
302
    my $params = $args{param} || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
303
    
cleanup
yuki-kimoto authored on 2010-10-17
304
    # First argument is the soruce of SQL
305
    $query = $self->create_query($query)
306
      unless ref $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
307
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
308
    # Auto filter
309
    my $auto_filter = {};
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
310
    my $tables = $args{table} || [];
311
    $tables = [$tables]
312
      unless ref $tables eq 'ARRAY';
313
    foreach my $table (@$tables) {
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
314
        $auto_filter = {
315
            %$auto_filter,
cleanup
Yuki Kimoto authored on 2011-01-12
316
            %{$self->{auto_filter}{in}->{$table} || {}}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
317
        }
318
    }
319
    
320
    # Filter
cleanup
yuki-kimoto authored on 2010-10-17
321
    my $filter = $args{filter} || $query->filter || {};
cleanup
Yuki Kimoto authored on 2010-12-21
322
    foreach my $column (keys %$filter) {
323
        my $fname = $filter->{$column};
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
324
        if (!defined $fname) {
325
            $filter->{$column} = undef;
326
        }
327
        elsif (ref $fname ne 'CODE') {
cleanup
Yuki Kimoto authored on 2010-12-21
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 = {};
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
350
	      foreach my $table (@$tables) {
cleanup
Yuki Kimoto authored on 2011-01-12
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
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
387
                                            filter/;
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
    
403
    # Insert keys
404
    my @insert_keys = keys %$param;
405
    
406
    # Templte for insert
407
    my $source = "insert into $table {insert_param "
408
               . join(' ', @insert_keys) . '}';
add tests
yuki-kimoto authored on 2010-08-10
409
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
410
    
411
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
412
    my $ret_val = $self->execute(
413
        $source,
414
        param  => $param,
415
        filter => $filter,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
416
        table => $table
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
417
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
418
    
419
    return $ret_val;
420
}
421

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

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

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

            
refactoring select
yuki-kimoto authored on 2010-04-28
464
our %VALID_SELECT_ARGS
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
465
  = map { $_ => 1 } qw/table column where append relation filter/;
refactoring select
yuki-kimoto authored on 2010-04-28
466

            
packaging one directory
yuki-kimoto authored on 2009-11-16
467
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
468
    my ($self, %args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
469
    
refactoring select
yuki-kimoto authored on 2010-04-28
470
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
471
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
472
        croak qq{"$name" is invalid argument}
refactoring select
yuki-kimoto authored on 2010-04-28
473
          unless $VALID_SELECT_ARGS{$name};
474
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
475
    
refactoring select
yuki-kimoto authored on 2010-04-28
476
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
477
    my $tables = $args{table} || [];
removed register_format()
yuki-kimoto authored on 2010-05-26
478
    $tables = [$tables] unless ref $tables eq 'ARRAY';
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
479
    my $columns  = $args{column} || [];
update document
yuki-kimoto authored on 2010-08-07
480
    my $where    = $args{where};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
481
    my $relation = $args{relation};
482
    my $append   = $args{append};
483
    my $filter   = $args{filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
484
    
add tests
yuki-kimoto authored on 2010-08-10
485
    # Source of SQL
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
486
    my $source = 'select ';
packaging one directory
yuki-kimoto authored on 2009-11-16
487
    
added commit method
yuki-kimoto authored on 2010-05-27
488
    # Column clause
packaging one directory
yuki-kimoto authored on 2009-11-16
489
    if (@$columns) {
490
        foreach my $column (@$columns) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
491
            $source .= "$column, ";
packaging one directory
yuki-kimoto authored on 2009-11-16
492
        }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
493
        $source =~ s/, $/ /;
packaging one directory
yuki-kimoto authored on 2009-11-16
494
    }
495
    else {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
496
        $source .= '* ';
packaging one directory
yuki-kimoto authored on 2009-11-16
497
    }
498
    
added commit method
yuki-kimoto authored on 2010-05-27
499
    # Table
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
500
    $source .= 'from ';
packaging one directory
yuki-kimoto authored on 2009-11-16
501
    foreach my $table (@$tables) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
502
        $source .= "$table, ";
packaging one directory
yuki-kimoto authored on 2009-11-16
503
    }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
504
    $source =~ s/, $/ /;
packaging one directory
yuki-kimoto authored on 2009-11-16
505
    
added commit method
yuki-kimoto authored on 2010-05-27
506
    # Where clause
update document
yuki-kimoto authored on 2010-08-07
507
    my $param;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
508
    if (ref $where eq 'HASH' && keys %$where) {
update document
yuki-kimoto authored on 2010-08-07
509
        $param = $where;
510
        $source .= 'where (';
511
        foreach my $where_key (keys %$where) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
512
            $source .= "{= $where_key} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
513
        }
update document
yuki-kimoto authored on 2010-08-07
514
        $source =~ s/ and $//;
515
        $source .= ') ';
516
    }
517
    elsif (ref $where eq 'ARRAY') {
518
        my$where_str = $where->[0] || '';
519
        $param = $where->[1];
520
        
521
        $source .= "where ($where_str) ";
packaging one directory
yuki-kimoto authored on 2009-11-16
522
    }
523
    
added commit method
yuki-kimoto authored on 2010-05-27
524
    # Relation
525
    if ($relation) {
update document
yuki-kimoto authored on 2010-08-07
526
        $source .= $where ? "and " : "where ";
added commit method
yuki-kimoto authored on 2010-05-27
527
        foreach my $rkey (keys %$relation) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
528
            $source .= "$rkey = " . $relation->{$rkey} . " and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
529
        }
530
    }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
531
    $source =~ s/ and $//;
added commit method
yuki-kimoto authored on 2010-05-27
532
    
533
    # Append some statement
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
534
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
535
    
536
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
537
    my $result = $self->execute(
538
        $source, param  => $param, filter => $filter,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
539
        table => $tables);    
packaging one directory
yuki-kimoto authored on 2009-11-16
540
    
541
    return $result;
542
}
543

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

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
568
sub txn_scope {
569
    my $self = shift;
570
    
571
    require DBIx::TransactionManager;
572
    
573
    $self->{_transaction_manager}
574
      ||= DBIx::TransactionManager->new($self->dbh);
575
    
576
    return $self->{_transaction_manager}->txn_scope;
577
}
578

            
cleanup
yuki-kimoto authored on 2010-10-17
579
our %VALID_UPDATE_ARGS
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
580
  = map { $_ => 1 } qw/table param
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
581
                       where append filter allow_update_all/;
cleanup
yuki-kimoto authored on 2010-10-17
582

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

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

            
cleanup
Yuki Kimoto authored on 2011-01-12
652
sub _build_binds {
653
    my ($self, $params, $columns, $filter) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
654
    
cleanup
Yuki Kimoto authored on 2011-01-12
655
    # bind values
656
    my @binds;
add tests
yuki-kimoto authored on 2010-08-08
657
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
658
    # Build bind values
659
    my $count = {};
cleanup
Yuki Kimoto authored on 2011-01-12
660
    foreach my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
661
        
662
        # Value
663
        my $value = ref $params->{$column} eq 'ARRAY'
664
                  ? $params->{$column}->[$count->{$column} || 0]
665
                  : $params->{$column};
666
        
cleanup
Yuki Kimoto authored on 2011-01-12
667
        # Filter
668
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
669
        
cleanup
Yuki Kimoto authored on 2011-01-12
670
        push @binds, $f ? $f->($value) : $value;
removed reconnect method
yuki-kimoto authored on 2010-05-28
671
        
672
        # Count up 
673
        $count->{$column}++;
674
    }
675
    
cleanup
Yuki Kimoto authored on 2011-01-12
676
    return \@binds;
removed reconnect method
yuki-kimoto authored on 2010-05-28
677
}
678

            
cleanup
yuki-kimoto authored on 2010-10-17
679
sub _croak {
680
    my ($self, $error, $append) = @_;
681
    $append ||= "";
682
    
683
    # Verbose
684
    if ($Carp::Verbose) { croak $error }
685
    
686
    # Not verbose
687
    else {
688
        
689
        # Remove line and module infromation
690
        my $at_pos = rindex($error, ' at ');
691
        $error = substr($error, 0, $at_pos);
692
        $error =~ s/\s+$//;
693
        
694
        croak "$error$append";
695
    }
696
}
697

            
cleanup
Yuki Kimoto authored on 2011-01-12
698
# Deprecated
699
__PACKAGE__->attr(cache_method => sub {
700
    sub {
701
        my $self = shift;
702
        
703
        $self->{_cached} ||= {};
704
        
705
        if (@_ > 1) {
706
            $self->{_cached}{$_[0]} = $_[1] 
707
        }
708
        else {
709
            return $self->{_cached}{$_[0]}
710
        }
711
    }
712
});
713

            
714
sub default_bind_filter {
715
    my $self = shift;
716
    
717
    if (@_) {
718
        my $fname = $_[0];
719
        
720
        if (@_ && !$fname) {
721
            $self->{default_out_filter} = undef;
722
        }
723
        else {
724
            croak qq{"$fname" is not registered}
725
              unless exists $self->filters->{$fname};
726
        
727
            $self->{default_out_filter} = $self->filters->{$fname};
728
        }
729
        return $self;
730
    }
731
    
732
    return $self->{default_out_filter};
733
}
734

            
735
sub default_fetch_filter {
736
    my $self = shift;
737
    my $fname = $_[0];
738
    
739
    if (@_) {
740
        if (@_ && !$fname) {
741
            $self->{default_in_filter} = undef;
742
        }
743
        else {
744
            croak qq{"$fname" is not registered}
745
              unless exists $self->filters->{$fname};
746
        
747
            $self->{default_in_filter} = $self->filters->{$fname};
748
        }
749
        
750
        return $self;
751
    }
752
    
753
    return $self->{default_in_filter}
754
}
755

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
758
=head1 NAME
759

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

            
762
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
763

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

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

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

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

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

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

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

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

            
updated document
yuki-kimoto authored on 2010-08-08
840
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
841

            
842
    # Get DBI object
843
    my $dbh = $dbi->dbh;
844

            
845
Fetch row.
846

            
removed register_format()
yuki-kimoto authored on 2010-05-26
847
    # Fetch
848
    while (my $row = $result->fetch) {
849
        # ...
850
    }
851
    
852
    # Fetch hash
853
    while (my $row = $result->fetch_hash) {
854
        
855
    }
856
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
857
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
858

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
added dbi_options attribute
kimoto authored on 2010-12-20
906
=head2 C<dbi_options>
907

            
908
    my $dbi_options = $dbi->dbi_options;
909
    $dbi            = $dbi->dbi_options($dbi_options);
910

            
911
DBI options.
912
C<connect()> method use this value to connect the database.
913

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
921
Filter functions.
922
"encode_utf8" and "decode_utf8" is registered by default.
923

            
924
=head2 C<filter_check>
925

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

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

            
932
=head2 C<password>
933

            
934
    my $password = $dbi->password;
935
    $dbi         = $dbi->password('lkj&le`@s');
936

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
959
    my $user = $dbi->user;
960
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
961

            
cleanup
yuki-kimoto authored on 2010-10-17
962
User name.
963
C<connect()> method use this value to connect the database.
964
    
965
=head1 METHODS
added commit method
yuki-kimoto authored on 2010-05-27
966

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

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
970
=head2 C<(experimental) apply_filter >
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
971

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
972
    $dbi->apply_filter(
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
973
        $table,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
974
        $column1 => {in => $infilter1, out => $outfilter1}
975
        $column2 => {in => $infilter2, out => $outfilter2}
976
        ...,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
977
    );
978

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
979
C<apply_filter> is automatically filter for columns of table.
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
980
This have effect C<insert>, C<update>, C<delete>. C<select>
cleanup
Yuki Kimoto authored on 2010-12-21
981
and L<DBIx::Custom::Result> object. but this has'nt C<execute> method.
982

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
983
If you want to have effect <execute< method, use C<table>
cleanup
Yuki Kimoto authored on 2010-12-21
984
arguments.
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
985

            
cleanup
Yuki Kimoto authored on 2010-12-21
986
    $result = $dbi->execute(
987
        "select * from table1 where {= key1} and {= key2};",
988
         param => {key1 => 1, key2 => 2},
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
989
         table => ['table1']
cleanup
Yuki Kimoto authored on 2010-12-21
990
    );
991
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
992
=head2 C<begin_work>
added check_filter attribute
yuki-kimoto authored on 2010-08-08
993

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

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

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

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

            
1004
    $dbi->commit;
1005

            
1006
Commit transaction.
1007
This is same as L<DBI>'s C<commit>.
1008

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1044
    my $result = $dbi->execute(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1045
        "select * from book where {= author} and {like title}", 
cleanup
yuki-kimoto authored on 2010-10-17
1046
        param => {author => 'Ken', title => '%Perl%'}
1047
    );
1048
    
1049
    while (my $row = $result->fetch) {
1050
        my $author = $row->[0];
1051
        my $title  = $row->[1];
1052
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
1053

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

            
1056
    my %expand = $dbi->expand($source);
1057

            
1058
The following hash
1059

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

            
1062
is expanded to
1063

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

            
1066
This is used in C<select()>
1067

            
1068

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1072
    $dbi->delete(table  => $table,
1073
                 where  => \%where,
1074
                 append => $append,
1075
                 filter => \%filter);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1076

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1077
Execute delete statement.
1078
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
1079
C<table> is a table name.
1080
C<where> is where clause. this must be hash reference.
1081
C<append> is a string added at the end of the SQL statement.
1082
C<filter> is filters when parameter binding is executed.
cleanup
yuki-kimoto authored on 2010-08-09
1083
Return value of C<delete()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1084

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

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

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

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

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

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

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

            
1107
    $dbi->helper(
1108
        update_or_insert => sub {
1109
            my $self = shift;
1110
            # do something
1111
        },
1112
        find_or_create   => sub {
1113
            my $self = shift;
1114
            # do something
1115
        }
1116
    );
1117

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

            
1120
    $dbi->update_or_insert;
1121
    $dbi->find_or_create;
1122

            
cleanup
yuki-kimoto authored on 2010-10-17
1123
=head2 C<insert>
1124

            
1125
    $dbi->insert(table  => $table, 
1126
                 param  => \%param,
1127
                 append => $append,
1128
                 filter => \%filter);
1129

            
1130
Execute insert statement.
1131
C<insert> method have C<table>, C<param>, C<append>
1132
and C<filter> arguments.
1133
C<table> is a table name.
1134
C<param> is the pairs of column name value. this must be hash reference.
1135
C<append> is a string added at the end of the SQL statement.
1136
C<filter> is filters when parameter binding is executed.
1137
This is overwrites C<default_bind_filter>.
1138
Return value of C<insert()> is the count of affected rows.
1139

            
1140
B<Example:>
1141

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

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

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

            
1152
Create a new L<DBIx::Custom> object.
1153

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

            
1156
    $dbi->iterate_all_columns(
1157
        sub {
1158
            my ($table, $column, $column_info) = @_;
1159
            
1160
            # do something;
1161
        }
1162
    );
1163

            
1164
Iterate all columns of all tables. Argument is callback.
1165
You can do anything by callback.
1166

            
cleanup
yuki-kimoto authored on 2010-10-17
1167
=head2 C<register_filter>
1168

            
1169
    $dbi->register_filter(%filters);
1170
    $dbi->register_filter(\%filters);
1171
    
1172
Register filter. Registered filters is available in the following attributes
1173
or arguments.
1174

            
1175
=over 4
1176

            
1177
=item *
1178

            
1179
C<filter> argument of C<insert()>, C<update()>,
1180
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1181
methods
1182

            
1183
=item *
1184

            
1185
C<execute()> method
1186

            
1187
=item *
1188

            
1189
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1190

            
1191
=item *
1192

            
1193
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1194

            
1195
=back
1196

            
1197
B<Example:>
1198

            
1199
    $dbi->register_filter(
1200
        encode_utf8 => sub {
1201
            my $value = shift;
1202
            
1203
            require Encode;
1204
            
1205
            return Encode::encode('UTF-8', $value);
1206
        },
1207
        decode_utf8 => sub {
1208
            my $value = shift;
1209
            
1210
            require Encode;
1211
            
1212
            return Encode::decode('UTF-8', $value)
1213
        }
1214
    );
1215

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

            
1218
    $dbi->rollback;
1219

            
1220
Rollback transaction.
1221
This is same as L<DBI>'s C<rollback>.
1222

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1223
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1224
    
cleanup
yuki-kimoto authored on 2010-08-05
1225
    my $result = $dbi->select(table    => $table,
1226
                              column   => [@column],
1227
                              where    => \%where,
1228
                              append   => $append,
1229
                              relation => \%relation,
1230
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1231

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1232
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1233
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1234
C<relation> and C<filter> arguments.
1235
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1236
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1237
C<append> is a string added at the end of the SQL statement.
1238
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
1239

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

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1242
    # select * from book;
1243
    my $result = $dbi->select(table => 'book');
packaging one directory
yuki-kimoto authored on 2009-11-16
1244
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1245
    # select * from book where title = ?;
1246
    my $result = $dbi->select(table => 'book', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1247
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1248
    # select title, author from book where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1249
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1250
        table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1251
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1252
        where  => {id => 1},
1253
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1254
    );
1255
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1256
    # select book.name as book_name from book, rental
1257
    # where book.id = rental.book_id;
added commit method
yuki-kimoto authored on 2010-05-27
1258
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1259
        table    => ['book', 'rental'],
1260
        column   => ['book.name as book_name']
1261
        relation => {'book.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1262
    );
1263

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

            
1267
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1268
        table  => 'book',
cleanup
yuki-kimoto authored on 2010-08-09
1269
        column => ['title', 'author'],
1270
        where  => ['{= title} or {like author}',
1271
                   {title => '%Perl%', author => 'Ken'}]
1272
    );
1273

            
1274
First element is a string. it contains tags,
1275
such as "{= title} or {like author}".
1276
Second element is paramters.
1277

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1280
    $dbi->update(table  => $table, 
1281
                 param  => \%params,
1282
                 where  => \%where,
1283
                 append => $append,
1284
                 filter => \%filter)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1285

            
cleanup
yuki-kimoto authored on 2010-10-17
1286
Execute update statement.
1287
C<update> method have C<table>, C<param>, C<where>, C<append>
1288
and C<filter> arguments.
1289
C<table> is a table name.
1290
C<param> is column-value pairs. this must be hash reference.
1291
C<where> is where clause. this must be hash reference.
1292
C<append> is a string added at the end of the SQL statement.
1293
C<filter> is filters when parameter binding is executed.
1294
This is overwrites C<default_bind_filter>.
1295
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1296

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

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

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

            
1307
    {
1308
        my $txn = $dbi->txn_scope;
1309
        $dbi->insert(table => 'book', param => {title => 'Perl'});
1310
        $dbi->insert(table => 'book', param => {title => 'Good days'});
1311
        $txn->commit;
1312
    }
1313

            
1314
Create transaction scope. If you escape scope(that is { .. }) and commited,
1315
Rollback is automatically done.
1316

            
1317
Note that this is feature of L<DBIx::TransactionManager>
1318
L<DBIx::TransactionManager> is required.
1319

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

            
1322
    $dbi->table('book',
1323
        insert => sub { ... },
1324
        update => sub { ... }
1325
    );
1326
    
1327
    my $table = $dbi->table('book');
1328

            
1329
Create a L<DBIx::Custom::Table> object,
1330
or get a L<DBIx::Custom::Table> object.
1331

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1334
    $dbi->update_all(table  => $table, 
1335
                     param  => \%params,
1336
                     filter => \%filter,
1337
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1338

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

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

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

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

            
1352
    my $default_bind_filter = $dbi->default_bind_filter;
1353
    $dbi                    = $dbi->default_bind_filter($fname);
1354

            
1355
Default filter when parameter binding is executed.
1356

            
1357
=head2 C<(deprecated) default_fetch_filter>
1358

            
1359
    my $default_fetch_filter = $dbi->default_fetch_filter;
1360
    $dbi = $dbi->default_fetch_filter($fname);
1361

            
1362
=head2 C<(deprecated) cache_method>
1363

            
1364
    $dbi          = $dbi->cache_method(\&cache_method);
1365
    $cache_method = $dbi->cache_method
1366

            
1367
Method to set and get caches.
1368

            
1369
B<Example:>
1370

            
1371
    $dbi->cache_method(
1372
        sub {
1373
            my $self = shift;
1374
            
1375
            $self->{_cached} ||= {};
1376
            
1377
            if (@_ > 1) {
1378
                $self->{_cached}{$_[0]} = $_[1] 
1379
            }
1380
            else {
1381
                return $self->{_cached}{$_[0]}
1382
            }
1383
        }
1384
    );
1385

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

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

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

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

            
1394
C<< <kimoto.yuki at gmail.com> >>
1395

            
1396
L<http://github.com/yuki-kimoto/DBIx-Custom>
1397

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1398
=head1 AUTHOR
1399

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

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

            
1404
Copyright 2009 Yuki Kimoto, all rights reserved.
1405

            
1406
This program is free software; you can redistribute it and/or modify it
1407
under the same terms as Perl itself.
1408

            
1409
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1410

            
1411