DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1434 lines | 37.133kb
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);
added cache_method attribute
yuki-kimoto authored on 2010-06-25
23
__PACKAGE__->attr(cache_method => sub {
24
    sub {
25
        my $self = shift;
26
        
27
        $self->{_cached} ||= {};
28
        
29
        if (@_ > 1) {
30
            $self->{_cached}{$_[0]} = $_[1] 
31
        }
32
        else {
33
            return $self->{_cached}{$_[0]}
34
        }
35
    }
36
});
removed register_format()
yuki-kimoto authored on 2010-05-26
37

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
610
sub table {
611
    my $self = shift;
612
    my $name = shift;
613
    
614
    # Table class
615
    my $table_class = $self->table_class;
616
    croak qq{Invalid table class name "$table_class"}
617
      unless $table_class =~ /^[\w:]+$/;
618
    unless ($table_class->can('isa')) {
619
        eval "require $table_class";
620
        croak $@ if $@;
621
    }
622
    # Create table
623
    $self->{_tables} ||= {};
624
    $self->{_tables}->{$name}
625
        = $table_class->new(name => $name, dbi => $self)
626
      unless defined $self->{_tables}->{$name};
627
    
628
    # Helper
629
    $self->{_tables}->{$name}->helper(@_) if @_;
630
    
631
    return $self->{_tables}{$name};
632
}
633

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
634
sub txn_scope {
635
    my $self = shift;
636
    
637
    require DBIx::TransactionManager;
638
    
639
    $self->{_transaction_manager}
640
      ||= DBIx::TransactionManager->new($self->dbh);
641
    
642
    return $self->{_transaction_manager}->txn_scope;
643
}
644

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

            
649
sub update {
650
    my ($self, %args) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
651
    
cleanup
yuki-kimoto authored on 2010-10-17
652
    # Check arguments
653
    foreach my $name (keys %args) {
654
        croak qq{"$name" is invalid argument}
655
          unless $VALID_UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
656
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
657
    
cleanup
yuki-kimoto authored on 2010-10-17
658
    # Arguments
659
    my $table            = $args{table} || '';
660
    my $param            = $args{param} || {};
661
    my $where            = $args{where} || {};
662
    my $append = $args{append} || '';
663
    my $filter           = $args{filter};
664
    my $allow_update_all = $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
665
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
666
    my $auto_filter_table = exists $args{auto_filter_table}
667
                          ? $args{auto_filter_table}
668
                          : [$table];
669
    $auto_filter_table ||= [];
670
    
cleanup
yuki-kimoto authored on 2010-10-17
671
    # Update keys
672
    my @update_keys = keys %$param;
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
673
    
cleanup
yuki-kimoto authored on 2010-10-17
674
    # Where keys
675
    my @where_keys = keys %$where;
removed reconnect method
yuki-kimoto authored on 2010-05-28
676
    
cleanup
yuki-kimoto authored on 2010-10-17
677
    # Not exists where keys
678
    croak qq{"where" argument must be specified and } .
679
          qq{contains the pairs of column name and value}
680
      if !@where_keys && !$allow_update_all;
removed experimental registe...
yuki-kimoto authored on 2010-08-24
681
    
cleanup
yuki-kimoto authored on 2010-10-17
682
    # Update clause
683
    my $update_clause = '{update_param ' . join(' ', @update_keys) . '}';
removed experimental registe...
yuki-kimoto authored on 2010-08-24
684
    
cleanup
yuki-kimoto authored on 2010-10-17
685
    # Where clause
686
    my $where_clause = '';
687
    my $new_where = {};
removed reconnect method
yuki-kimoto authored on 2010-05-28
688
    
cleanup
yuki-kimoto authored on 2010-10-17
689
    if (@where_keys) {
690
        $where_clause = 'where ';
691
        $where_clause .= "{= $_} and " for @where_keys;
692
        $where_clause =~ s/ and $//;
removed reconnect method
yuki-kimoto authored on 2010-05-28
693
    }
694
    
cleanup
yuki-kimoto authored on 2010-10-17
695
    # Source of SQL
696
    my $source = "update $table $update_clause $where_clause";
697
    $source .= " $append" if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
698
    
cleanup
yuki-kimoto authored on 2010-10-17
699
    # Rearrange parameters
700
    foreach my $wkey (@where_keys) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
701
        
cleanup
yuki-kimoto authored on 2010-10-17
702
        if (exists $param->{$wkey}) {
703
            $param->{$wkey} = [$param->{$wkey}]
704
              unless ref $param->{$wkey} eq 'ARRAY';
705
            
706
            push @{$param->{$wkey}}, $where->{$wkey};
707
        }
708
        else {
709
            $param->{$wkey} = $where->{$wkey};
710
        }
removed reconnect method
yuki-kimoto authored on 2010-05-28
711
    }
cleanup
yuki-kimoto authored on 2010-10-17
712
    
713
    # Execute query
714
    my $ret_val = $self->execute($source, param  => $param, 
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
715
                                 filter => $filter,
716
                                 auto_filter_table => $auto_filter_table);
cleanup
yuki-kimoto authored on 2010-10-17
717
    
718
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
719
}
720

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
723
sub _build_bind_values {
724
    my ($self, $query, $params, $filter) = @_;
725
    
726
    # binding values
727
    my @bind_values;
add tests
yuki-kimoto authored on 2010-08-08
728

            
729
    # Filter
730
    $filter ||= {};
731
    
732
    # Parameter
733
    $params ||= {};
734
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
735
    # Build bind values
736
    my $count = {};
737
    foreach my $column (@{$query->columns}) {
738
        
739
        # Value
740
        my $value = ref $params->{$column} eq 'ARRAY'
741
                  ? $params->{$column}->[$count->{$column} || 0]
742
                  : $params->{$column};
743
        
add tests
yuki-kimoto authored on 2010-08-10
744
        # Filtering
cleanup
Yuki Kimoto authored on 2010-12-22
745
        my $f = $filter->{$column} || $self->{default_bind_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
746
        
cleanup
Yuki Kimoto authored on 2010-12-21
747
        push @bind_values, $f ? $f->($value) : $value;
removed reconnect method
yuki-kimoto authored on 2010-05-28
748
        
749
        # Count up 
750
        $count->{$column}++;
751
    }
752
    
753
    return \@bind_values;
754
}
755

            
cleanup
yuki-kimoto authored on 2010-10-17
756
sub _croak {
757
    my ($self, $error, $append) = @_;
758
    $append ||= "";
759
    
760
    # Verbose
761
    if ($Carp::Verbose) { croak $error }
762
    
763
    # Not verbose
764
    else {
765
        
766
        # Remove line and module infromation
767
        my $at_pos = rindex($error, ' at ');
768
        $error = substr($error, 0, $at_pos);
769
        $error =~ s/\s+$//;
770
        
771
        croak "$error$append";
772
    }
773
}
774

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
777
=head1 NAME
778

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

            
781
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
782

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

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

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

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

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

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

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

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

            
updated document
yuki-kimoto authored on 2010-08-08
859
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
860

            
861
    # Get DBI object
862
    my $dbh = $dbi->dbh;
863

            
864
Fetch row.
865

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

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
912
    $dbi          = $dbi->cache_method(\&cache_method);
913
    $cache_method = $dbi->cache_method
914

            
915
Method to set and get caches.
916

            
917
B<Example:>
918

            
919
    $dbi->cache_method(
920
        sub {
921
            my $self = shift;
922
            
923
            $self->{_cached} ||= {};
924
            
925
            if (@_ > 1) {
926
                $self->{_cached}{$_[0]} = $_[1] 
927
            }
928
            else {
929
                return $self->{_cached}{$_[0]}
930
            }
931
        }
932
    );
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
933

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

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

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

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

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

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

            
added dbi_options attribute
kimoto authored on 2010-12-20
949
=head2 C<dbi_options>
950

            
951
    my $dbi_options = $dbi->dbi_options;
952
    $dbi            = $dbi->dbi_options($dbi_options);
953

            
954
DBI options.
955
C<connect()> method use this value to connect the database.
956

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
964
Filter functions.
965
"encode_utf8" and "decode_utf8" is registered by default.
966

            
967
=head2 C<filter_check>
968

            
969
    my $filter_check = $dbi->filter_check;
970
    $dbi             = $dbi->filter_check(0);
971

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

            
975
=head2 C<password>
976

            
977
    my $password = $dbi->password;
978
    $dbi         = $dbi->password('lkj&le`@s');
979

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1002
    my $user = $dbi->user;
1003
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1004

            
cleanup
yuki-kimoto authored on 2010-10-17
1005
User name.
1006
C<connect()> method use this value to connect the database.
1007
    
1008
=head1 METHODS
added commit method
yuki-kimoto authored on 2010-05-27
1009

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

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

            
1015
    $dbi->auto_filter(
1016
        $table,
1017
        [$column1, $bind_filter1, $fetch_filter1],
1018
        [$column2, $bind_filter2, $fetch_filter2],
1019
        [...],
1020
    );
1021

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

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

            
cleanup
Yuki Kimoto authored on 2010-12-21
1029
    $result = $dbi->execute(
1030
        "select * from table1 where {= key1} and {= key2};",
1031
         param => {key1 => 1, key2 => 2},
1032
         auto_filter_table => ['table1']
1033
    );
1034
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1035
B<Example:>
1036

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

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

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

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

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

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

            
1051
    $dbi->commit;
1052

            
1053
Commit transaction.
1054
This is same as L<DBI>'s C<commit>.
1055

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2010-12-22
1081
    my $default_bind_filter = $dbi->default_bind_filter;
1082
    $dbi                    = $dbi->default_bind_filter($fname);
cleanup
Yuki Kimoto authored on 2010-12-21
1083

            
1084
Default filter when parameter binding is executed.
1085

            
1086
=head2 C<(deprecated) default_fetch_filter>
1087

            
cleanup
Yuki Kimoto authored on 2010-12-22
1088
    my $default_fetch_filter = $dbi->default_fetch_filter;
cleanup
Yuki Kimoto authored on 2010-12-21
1089
    $dbi = $dbi->default_fetch_filter($fname);
1090

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1103
    my $result = $dbi->execute(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1104
        "select * from book where {= author} and {like title}", 
cleanup
yuki-kimoto authored on 2010-10-17
1105
        param => {author => 'Ken', title => '%Perl%'}
1106
    );
1107
    
1108
    while (my $row = $result->fetch) {
1109
        my $author = $row->[0];
1110
        my $title  = $row->[1];
1111
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
1112

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

            
1115
    my %expand = $dbi->expand($source);
1116

            
1117
The following hash
1118

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

            
1121
is expanded to
1122

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

            
1125
This is used in C<select()>
1126

            
1127

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1131
    $dbi->delete(table  => $table,
1132
                 where  => \%where,
1133
                 append => $append,
1134
                 filter => \%filter);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1135

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1136
Execute delete statement.
1137
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
1138
C<table> is a table name.
1139
C<where> is where clause. this must be hash reference.
1140
C<append> is a string added at the end of the SQL statement.
1141
C<filter> is filters when parameter binding is executed.
cleanup
yuki-kimoto authored on 2010-08-09
1142
Return value of C<delete()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1143

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

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

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

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

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

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

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

            
1166
    $dbi->helper(
1167
        update_or_insert => sub {
1168
            my $self = shift;
1169
            # do something
1170
        },
1171
        find_or_create   => sub {
1172
            my $self = shift;
1173
            # do something
1174
        }
1175
    );
1176

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

            
1179
    $dbi->update_or_insert;
1180
    $dbi->find_or_create;
1181

            
cleanup
yuki-kimoto authored on 2010-10-17
1182
=head2 C<insert>
1183

            
1184
    $dbi->insert(table  => $table, 
1185
                 param  => \%param,
1186
                 append => $append,
1187
                 filter => \%filter);
1188

            
1189
Execute insert statement.
1190
C<insert> method have C<table>, C<param>, C<append>
1191
and C<filter> arguments.
1192
C<table> is a table name.
1193
C<param> is the pairs of column name value. this must be hash reference.
1194
C<append> is a string added at the end of the SQL statement.
1195
C<filter> is filters when parameter binding is executed.
1196
This is overwrites C<default_bind_filter>.
1197
Return value of C<insert()> is the count of affected rows.
1198

            
1199
B<Example:>
1200

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

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

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

            
1211
Create a new L<DBIx::Custom> object.
1212

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

            
1215
    $dbi->iterate_all_columns(
1216
        sub {
1217
            my ($table, $column, $column_info) = @_;
1218
            
1219
            # do something;
1220
        }
1221
    );
1222

            
1223
Iterate all columns of all tables. Argument is callback.
1224
You can do anything by callback.
1225

            
cleanup
yuki-kimoto authored on 2010-10-17
1226
=head2 C<register_filter>
1227

            
1228
    $dbi->register_filter(%filters);
1229
    $dbi->register_filter(\%filters);
1230
    
1231
Register filter. Registered filters is available in the following attributes
1232
or arguments.
1233

            
1234
=over 4
1235

            
1236
=item *
1237

            
1238
C<filter> argument of C<insert()>, C<update()>,
1239
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1240
methods
1241

            
1242
=item *
1243

            
1244
C<execute()> method
1245

            
1246
=item *
1247

            
1248
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1249

            
1250
=item *
1251

            
1252
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1253

            
1254
=back
1255

            
1256
B<Example:>
1257

            
1258
    $dbi->register_filter(
1259
        encode_utf8 => sub {
1260
            my $value = shift;
1261
            
1262
            require Encode;
1263
            
1264
            return Encode::encode('UTF-8', $value);
1265
        },
1266
        decode_utf8 => sub {
1267
            my $value = shift;
1268
            
1269
            require Encode;
1270
            
1271
            return Encode::decode('UTF-8', $value)
1272
        }
1273
    );
1274

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

            
1277
    $dbi->rollback;
1278

            
1279
Rollback transaction.
1280
This is same as L<DBI>'s C<rollback>.
1281

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1282
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1283
    
cleanup
yuki-kimoto authored on 2010-08-05
1284
    my $result = $dbi->select(table    => $table,
1285
                              column   => [@column],
1286
                              where    => \%where,
1287
                              append   => $append,
1288
                              relation => \%relation,
1289
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1290

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1291
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1292
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1293
C<relation> and C<filter> arguments.
1294
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1295
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1296
C<append> is a string added at the end of the SQL statement.
1297
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
1298

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

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1301
    # select * from book;
1302
    my $result = $dbi->select(table => 'book');
packaging one directory
yuki-kimoto authored on 2009-11-16
1303
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1304
    # select * from book where title = ?;
1305
    my $result = $dbi->select(table => 'book', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1306
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1307
    # select title, author from book where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1308
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1309
        table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1310
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1311
        where  => {id => 1},
1312
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1313
    );
1314
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1315
    # select book.name as book_name from book, rental
1316
    # where book.id = rental.book_id;
added commit method
yuki-kimoto authored on 2010-05-27
1317
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1318
        table    => ['book', 'rental'],
1319
        column   => ['book.name as book_name']
1320
        relation => {'book.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1321
    );
1322

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

            
1326
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1327
        table  => 'book',
cleanup
yuki-kimoto authored on 2010-08-09
1328
        column => ['title', 'author'],
1329
        where  => ['{= title} or {like author}',
1330
                   {title => '%Perl%', author => 'Ken'}]
1331
    );
1332

            
1333
First element is a string. it contains tags,
1334
such as "{= title} or {like author}".
1335
Second element is paramters.
1336

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1339
    $dbi->update(table  => $table, 
1340
                 param  => \%params,
1341
                 where  => \%where,
1342
                 append => $append,
1343
                 filter => \%filter)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1344

            
cleanup
yuki-kimoto authored on 2010-10-17
1345
Execute update statement.
1346
C<update> method have C<table>, C<param>, C<where>, C<append>
1347
and C<filter> arguments.
1348
C<table> is a table name.
1349
C<param> is column-value pairs. this must be hash reference.
1350
C<where> is where clause. this must be hash reference.
1351
C<append> is a string added at the end of the SQL statement.
1352
C<filter> is filters when parameter binding is executed.
1353
This is overwrites C<default_bind_filter>.
1354
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1355

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

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

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

            
1366
    {
1367
        my $txn = $dbi->txn_scope;
1368
        $dbi->insert(table => 'book', param => {title => 'Perl'});
1369
        $dbi->insert(table => 'book', param => {title => 'Good days'});
1370
        $txn->commit;
1371
    }
1372

            
1373
Create transaction scope. If you escape scope(that is { .. }) and commited,
1374
Rollback is automatically done.
1375

            
1376
Note that this is feature of L<DBIx::TransactionManager>
1377
L<DBIx::TransactionManager> is required.
1378

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1379
=head2 C<table>
1380

            
1381
    $dbi->table('book',
1382
        insert => sub { ... },
1383
        update => sub { ... }
1384
    );
1385
    
1386
    my $table = $dbi->table('book');
1387

            
1388
Create a L<DBIx::Custom::Table> object,
1389
or get a L<DBIx::Custom::Table> object.
1390

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1393
    $dbi->update_all(table  => $table, 
1394
                     param  => \%params,
1395
                     filter => \%filter,
1396
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1397

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

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

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

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

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

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

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

            
1417
C<< <kimoto.yuki at gmail.com> >>
1418

            
1419
L<http://github.com/yuki-kimoto/DBIx-Custom>
1420

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1421
=head1 AUTHOR
1422

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

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

            
1427
Copyright 2009 Yuki Kimoto, all rights reserved.
1428

            
1429
This program is free software; you can redistribute it and/or modify it
1430
under the same terms as Perl itself.
1431

            
1432
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1433

            
1434