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

            
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
3
our $VERSION = '0.1623';
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
4

            
5
use 5.008001;
cleanup
yuki-kimoto authored on 2009-12-22
6
use strict;
7
use warnings;
8

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
9
use base 'Object::Simple';
many change
yuki-kimoto authored on 2010-02-11
10

            
packaging one directory
yuki-kimoto authored on 2009-11-16
11
use Carp 'croak';
12
use DBI;
13
use DBIx::Custom::Result;
cleanup
yuki-kimoto authored on 2010-02-11
14
use DBIx::Custom::Query;
cleanup
yuki-kimoto authored on 2010-08-05
15
use DBIx::Custom::QueryBuilder;
update document
yuki-kimoto authored on 2010-05-27
16
use Encode qw/encode_utf8 decode_utf8/;
packaging one directory
yuki-kimoto authored on 2009-11-16
17

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

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

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

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

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

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

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

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

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

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

            
added helper method
yuki-kimoto authored on 2010-10-17
138
sub helper {
139
    my $self = shift;
140
    
141
    # Merge
142
    my $helpers = ref $_[0] eq 'HASH' ? $_[0] : {@_};
143
    $self->{_helpers} = {%{$self->{_helpers} || {}}, %$helpers};
144
    
145
    return $self;
146
}
147

            
packaging one directory
yuki-kimoto authored on 2009-11-16
148
sub connect {
removed register_format()
yuki-kimoto authored on 2010-05-26
149
    my $proto = shift;
150
    
151
    # Create
152
    my $self = ref $proto ? $proto : $proto->new(@_);
update document
yuki-kimoto authored on 2010-01-30
153
    
154
    # Information
packaging one directory
yuki-kimoto authored on 2009-11-16
155
    my $data_source = $self->data_source;
check arguments of connect m...
Yuki Kimoto authored on 2010-12-20
156
    
157
    croak qq{"data_source" must be specfied to connect method"}
158
      unless $data_source;
159
    
packaging one directory
yuki-kimoto authored on 2009-11-16
160
    my $user        = $self->user;
161
    my $password    = $self->password;
added dbi_options attribute
kimoto authored on 2010-12-20
162
    my $dbi_options = $self->dbi_options || {};
163
    
update document
yuki-kimoto authored on 2010-01-30
164
    # Connect
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
165
    my $dbh = eval {DBI->connect(
packaging one directory
yuki-kimoto authored on 2009-11-16
166
        $data_source,
167
        $user,
168
        $password,
169
        {
170
            RaiseError => 1,
171
            PrintError => 0,
172
            AutoCommit => 1,
added dbi_options attribute
kimoto authored on 2010-12-20
173
            %$dbi_options
packaging one directory
yuki-kimoto authored on 2009-11-16
174
        }
175
    )};
176
    
update document
yuki-kimoto authored on 2010-01-30
177
    # Connect error
packaging one directory
yuki-kimoto authored on 2009-11-16
178
    croak $@ if $@;
179
    
update document
yuki-kimoto authored on 2010-01-30
180
    # Database handle
packaging one directory
yuki-kimoto authored on 2009-11-16
181
    $self->dbh($dbh);
update document
yuki-kimoto authored on 2010-01-30
182
    
packaging one directory
yuki-kimoto authored on 2009-11-16
183
    return $self;
184
}
185

            
cleanup
yuki-kimoto authored on 2010-10-17
186
sub create_query {
187
    my ($self, $source) = @_;
update document
yuki-kimoto authored on 2010-01-30
188
    
cleanup
yuki-kimoto authored on 2010-10-17
189
    # Cache
190
    my $cache = $self->cache;
update document
yuki-kimoto authored on 2010-01-30
191
    
cleanup
yuki-kimoto authored on 2010-10-17
192
    # Create query
193
    my $query;
194
    if ($cache) {
195
        
196
        # Get query
197
        my $q = $self->cache_method->($self, $source);
198
        
199
        # Create query
200
        $query = DBIx::Custom::Query->new($q) if $q;
201
    }
202
    
203
    unless ($query) {
cleanup insert
yuki-kimoto authored on 2010-04-28
204

            
cleanup
yuki-kimoto authored on 2010-10-17
205
        # Create SQL object
206
        my $builder = $self->query_builder;
207
        
208
        # Create query
209
        $query = $builder->build_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
210

            
cleanup
yuki-kimoto authored on 2010-10-17
211
        # Cache query
212
        $self->cache_method->($self, $source,
213
                             {sql     => $query->sql, 
214
                              columns => $query->columns})
215
          if $cache;
cleanup insert
yuki-kimoto authored on 2010-04-28
216
    }
217
    
cleanup
yuki-kimoto authored on 2010-10-17
218
    # Prepare statement handle
219
    my $sth;
220
    eval { $sth = $self->dbh->prepare($query->{sql})};
221
    $self->_croak($@, qq{. SQL: "$query->{sql}"}) if $@;
packaging one directory
yuki-kimoto authored on 2009-11-16
222
    
cleanup
yuki-kimoto authored on 2010-10-17
223
    # Set statement handle
224
    $query->sth($sth);
packaging one directory
yuki-kimoto authored on 2009-11-16
225
    
cleanup
yuki-kimoto authored on 2010-10-17
226
    return $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
227
}
228

            
cleanup
Yuki Kimoto authored on 2010-12-21
229
sub default_bind_filter {
230
    my $self = shift;
231
    my $fname = $_[0];
232
    
233
    if (@_ && !$fname) {
234
        $self->{_default_bind_filter} = undef;
235
    }
236
    else {
237
        croak qq{"$fname" is not registered}
238
          unless exists $self->filters->{$fname};
239
    
240
        $self->{_default_bind_filter} = $self->filters->{$fname};
241
    }
242
    
243
    return $self;
244
}
245

            
246
sub default_fetch_filter {
247
    my $self = shift;
248
    my $fname = $_[0];
249
    
250
    if (@_ && !$fname) {
251
        $self->{_default_fetch_filter} = undef;
252
    }
253
    else {
254
        croak qq{"$fname" is not registered}
255
          unless exists $self->filters->{$fname};
256
    
257
        $self->{_default_fetch_filter} = $self->filters->{$fname};
258
    }
259
    
260
    return $self;
261
}
262

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

            
cleanup
yuki-kimoto authored on 2010-10-17
266
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
267
    my ($self, %args) = @_;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
268
    
269
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
270
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
271
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
272
          unless $VALID_DELETE_ARGS{$name};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
273
    }
274
    
275
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
276
    my $table            = $args{table} || '';
277
    my $where            = $args{where} || {};
cleanup
yuki-kimoto authored on 2010-10-17
278
    my $append = $args{append};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
279
    my $filter           = $args{filter};
cleanup
yuki-kimoto authored on 2010-10-17
280
    my $allow_delete_all = $args{allow_delete_all};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
281

            
282
    my $auto_filter_table = exists $args{auto_filter_table}
283
                          ? $args{auto_filter_table}
284
                          : [$table];
285
    $auto_filter_table ||= [];    
286

            
packaging one directory
yuki-kimoto authored on 2009-11-16
287
    # Where keys
removed register_format()
yuki-kimoto authored on 2010-05-26
288
    my @where_keys = keys %$where;
packaging one directory
yuki-kimoto authored on 2009-11-16
289
    
290
    # Not exists where keys
add tests
yuki-kimoto authored on 2010-08-10
291
    croak qq{"where" argument must be specified and } .
292
          qq{contains the pairs of column name and value}
cleanup
yuki-kimoto authored on 2010-10-17
293
      if !@where_keys && !$allow_delete_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
294
    
295
    # Where clause
296
    my $where_clause = '';
297
    if (@where_keys) {
298
        $where_clause = 'where ';
add tests
yuki-kimoto authored on 2010-08-10
299
        $where_clause .= "{= $_} and " for @where_keys;
packaging one directory
yuki-kimoto authored on 2009-11-16
300
        $where_clause =~ s/ and $//;
301
    }
302
    
add tests
yuki-kimoto authored on 2010-08-10
303
    # Source of SQL
cleanup
yuki-kimoto authored on 2010-10-17
304
    my $source = "delete from $table $where_clause";
add tests
yuki-kimoto authored on 2010-08-10
305
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
306
    
307
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
308
    my $ret_val = $self->execute(
309
        $source, param  => $where, filter => $filter,
310
        auto_filter_table => $auto_filter_table);
packaging one directory
yuki-kimoto authored on 2009-11-16
311
    
312
    return $ret_val;
313
}
314

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

            
added helper method
yuki-kimoto authored on 2010-10-17
317
sub DESTROY { }
318

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

            
cleanup
yuki-kimoto authored on 2010-10-17
321
sub execute{
322
    my ($self, $query, %args)  = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
323
    
324
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
325
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
326
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
327
          unless $VALID_EXECUTE_ARGS{$name};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
328
    }
329
    
cleanup
yuki-kimoto authored on 2010-10-17
330
    my $params = $args{param} || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
331
    
cleanup
yuki-kimoto authored on 2010-10-17
332
    # First argument is the soruce of SQL
333
    $query = $self->create_query($query)
334
      unless ref $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
335
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
336
    # Auto filter
337
    my $auto_filter = {};
338
    my $auto_filter_tables = $args{auto_filter_table} || [];
339
    foreach my $table (@$auto_filter_tables) {
340
        $auto_filter = {
341
            %$auto_filter,
342
            %{$self->{_auto_bind_filter}->{$table} || {}}
343
        }
344
    }
345
    
346
    # Filter
cleanup
yuki-kimoto authored on 2010-10-17
347
    my $filter = $args{filter} || $query->filter || {};
cleanup
Yuki Kimoto authored on 2010-12-21
348
    foreach my $column (keys %$filter) {
349
        my $fname = $filter->{$column};
350
        unless (ref $fname eq 'CODE') {
351
          croak qq{"$fname" is not registered"}
352
            unless exists $self->filters->{$fname};
353
          
354
          $filter->{$column} = $self->filters->{$fname};
355
        }
356
    }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
357
    $filter = {%$auto_filter, %$filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
358
    
cleanup
yuki-kimoto authored on 2010-10-17
359
    # Create bind value
360
    my $bind_values = $self->_build_bind_values($query, $params, $filter);
361
    
362
    # Execute
363
    my $sth      = $query->sth;
364
    my $affected;
365
    eval {$affected = $sth->execute(@$bind_values)};
366
    $self->_croak($@) if $@;
367
    
368
    # Return resultset if select statement is executed
369
    if ($sth->{NUM_OF_FIELDS}) {
370
        
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
371
        # Auto fetch filter
372
        my $auto_fetch_filter = {};
373
	    foreach my $table (@$auto_filter_tables) {
374
	        $auto_fetch_filter = {
375
	            %$auto_filter,
376
	            %{$self->{_auto_fetch_filter}{$table} || {}}
377
	        }
378
	    }
379
	    
380
		# Result
381
		my $result = $self->result_class->new(
cleanup
yuki-kimoto authored on 2010-10-17
382
            sth            => $sth,
383
            filters        => $self->filters,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
384
            filter_check   => $self->filter_check,
cleanup
Yuki Kimoto authored on 2010-12-21
385
            _auto_filter   => $auto_fetch_filter || {},
386
            _default_filter => $self->{_default_fetch_filter}
cleanup
yuki-kimoto authored on 2010-10-17
387
        );
388

            
389
        return $result;
390
    }
391
    return $affected;
392
}
393

            
added experimental expand me...
yuki-kimoto authored on 2010-10-20
394
sub expand {
395
    my $self = shift;
396
    my $source = ref $_[0] eq 'HASH' ? $_[0] : {@_};
397
    my $table = (keys %$source)[0];
398
    my $param = $source->{$table};
399
    
400
    # Expand table name
401
    my $expand = {};
402
    foreach my $column (keys %$param) {
403
        $expand->{"$table.$column"} = $param->{$column};
404
    }
405
    
406
    return %$expand;
407
}
408

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

            
414
    # Check arguments
415
    foreach my $name (keys %args) {
416
        croak qq{"$name" is invalid argument}
417
          unless $VALID_INSERT_ARGS{$name};
packaging one directory
yuki-kimoto authored on 2009-11-16
418
    }
419
    
cleanup
yuki-kimoto authored on 2010-10-17
420
    # Arguments
421
    my $table  = $args{table} || '';
422
    my $param  = $args{param} || {};
423
    my $append = $args{append} || '';
424
    my $filter = $args{filter};
425
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
426
    my $auto_filter_table = exists $args{auto_filter_table}
427
                          ? $args{auto_filter_table}
428
                          : [$table];
429
    $auto_filter_table ||= [];
430
    
cleanup
yuki-kimoto authored on 2010-10-17
431
    # Insert keys
432
    my @insert_keys = keys %$param;
433
    
434
    # Templte for insert
435
    my $source = "insert into $table {insert_param "
436
               . join(' ', @insert_keys) . '}';
add tests
yuki-kimoto authored on 2010-08-10
437
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
438
    
439
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
440
    my $ret_val = $self->execute(
441
        $source,
442
        param  => $param,
443
        filter => $filter,
444
        auto_filter_table => $auto_filter_table
445
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
446
    
447
    return $ret_val;
448
}
449

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

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

            
refactoring select
yuki-kimoto authored on 2010-04-28
473
our %VALID_SELECT_ARGS
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
474
  = map { $_ => 1 } qw/auto_filter_table table column where append relation filter param/;
refactoring select
yuki-kimoto authored on 2010-04-28
475

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

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

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

            
561
sub update {
562
    my ($self, %args) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
563
    
cleanup
yuki-kimoto authored on 2010-10-17
564
    # Check arguments
565
    foreach my $name (keys %args) {
566
        croak qq{"$name" is invalid argument}
567
          unless $VALID_UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
568
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
569
    
cleanup
yuki-kimoto authored on 2010-10-17
570
    # Arguments
571
    my $table            = $args{table} || '';
572
    my $param            = $args{param} || {};
573
    my $where            = $args{where} || {};
574
    my $append = $args{append} || '';
575
    my $filter           = $args{filter};
576
    my $allow_update_all = $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
577
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
578
    my $auto_filter_table = exists $args{auto_filter_table}
579
                          ? $args{auto_filter_table}
580
                          : [$table];
581
    $auto_filter_table ||= [];
582
    
cleanup
yuki-kimoto authored on 2010-10-17
583
    # Update keys
584
    my @update_keys = keys %$param;
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
585
    
cleanup
yuki-kimoto authored on 2010-10-17
586
    # Where keys
587
    my @where_keys = keys %$where;
removed reconnect method
yuki-kimoto authored on 2010-05-28
588
    
cleanup
yuki-kimoto authored on 2010-10-17
589
    # Not exists where keys
590
    croak qq{"where" argument must be specified and } .
591
          qq{contains the pairs of column name and value}
592
      if !@where_keys && !$allow_update_all;
removed experimental registe...
yuki-kimoto authored on 2010-08-24
593
    
cleanup
yuki-kimoto authored on 2010-10-17
594
    # Update clause
595
    my $update_clause = '{update_param ' . join(' ', @update_keys) . '}';
removed experimental registe...
yuki-kimoto authored on 2010-08-24
596
    
cleanup
yuki-kimoto authored on 2010-10-17
597
    # Where clause
598
    my $where_clause = '';
599
    my $new_where = {};
removed reconnect method
yuki-kimoto authored on 2010-05-28
600
    
cleanup
yuki-kimoto authored on 2010-10-17
601
    if (@where_keys) {
602
        $where_clause = 'where ';
603
        $where_clause .= "{= $_} and " for @where_keys;
604
        $where_clause =~ s/ and $//;
removed reconnect method
yuki-kimoto authored on 2010-05-28
605
    }
606
    
cleanup
yuki-kimoto authored on 2010-10-17
607
    # Source of SQL
608
    my $source = "update $table $update_clause $where_clause";
609
    $source .= " $append" if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
610
    
cleanup
yuki-kimoto authored on 2010-10-17
611
    # Rearrange parameters
612
    foreach my $wkey (@where_keys) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
613
        
cleanup
yuki-kimoto authored on 2010-10-17
614
        if (exists $param->{$wkey}) {
615
            $param->{$wkey} = [$param->{$wkey}]
616
              unless ref $param->{$wkey} eq 'ARRAY';
617
            
618
            push @{$param->{$wkey}}, $where->{$wkey};
619
        }
620
        else {
621
            $param->{$wkey} = $where->{$wkey};
622
        }
removed reconnect method
yuki-kimoto authored on 2010-05-28
623
    }
cleanup
yuki-kimoto authored on 2010-10-17
624
    
625
    # Execute query
626
    my $ret_val = $self->execute($source, param  => $param, 
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
627
                                 filter => $filter,
628
                                 auto_filter_table => $auto_filter_table);
cleanup
yuki-kimoto authored on 2010-10-17
629
    
630
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
631
}
632

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
635
sub _build_bind_values {
636
    my ($self, $query, $params, $filter) = @_;
637
    
638
    # binding values
639
    my @bind_values;
add tests
yuki-kimoto authored on 2010-08-08
640

            
641
    # Filter
642
    $filter ||= {};
643
    
644
    # Parameter
645
    $params ||= {};
646
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
647
    # Build bind values
648
    my $count = {};
649
    foreach my $column (@{$query->columns}) {
650
        
651
        # Value
652
        my $value = ref $params->{$column} eq 'ARRAY'
653
                  ? $params->{$column}->[$count->{$column} || 0]
654
                  : $params->{$column};
655
        
add tests
yuki-kimoto authored on 2010-08-10
656
        # Filtering
cleanup
Yuki Kimoto authored on 2010-12-21
657
        my $f = $filter->{$column} || $self->{_default_bind_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
658
        
cleanup
Yuki Kimoto authored on 2010-12-21
659
        push @bind_values, $f ? $f->($value) : $value;
removed reconnect method
yuki-kimoto authored on 2010-05-28
660
        
661
        # Count up 
662
        $count->{$column}++;
663
    }
664
    
665
    return \@bind_values;
666
}
667

            
cleanup
yuki-kimoto authored on 2010-10-17
668
sub _croak {
669
    my ($self, $error, $append) = @_;
670
    $append ||= "";
671
    
672
    # Verbose
673
    if ($Carp::Verbose) { croak $error }
674
    
675
    # Not verbose
676
    else {
677
        
678
        # Remove line and module infromation
679
        my $at_pos = rindex($error, ' at ');
680
        $error = substr($error, 0, $at_pos);
681
        $error =~ s/\s+$//;
682
        
683
        croak "$error$append";
684
    }
685
}
686

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
689
=head1 NAME
690

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

            
693
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
694

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
703
    # Insert 
704
    $dbi->insert(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
705
                 param  => {title => 'Perl', author => 'Ken'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
706
                 filter => {title => 'encode_utf8'});
707
    
708
    # Update 
709
    $dbi->update(table  => 'books', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
710
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
711
                 where  => {id => 5},
712
                 filter => {title => 'encode_utf8'});
713
    
714
    # Update all
715
    $dbi->update_all(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
716
                     param  => {title => 'Perl'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
717
                     filter => {title => 'encode_utf8'});
718
    
719
    # Delete
720
    $dbi->delete(table  => 'books',
721
                 where  => {author => 'Ken'},
722
                 filter => {title => 'encode_utf8'});
723
    
724
    # Delete all
725
    $dbi->delete_all(table => 'books');
cleanup
yuki-kimoto authored on 2010-08-05
726

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
729
    # Select
730
    my $result = $dbi->select(table => 'books');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
731
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
732
    # Select, more complex
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
733
    my $result = $dbi->select(
update document
yuki-kimoto authored on 2010-05-27
734
        table  => 'books',
735
        column => [qw/author title/],
736
        where  => {author => 'Ken'},
updated document
yuki-kimoto authored on 2010-08-08
737
        append => 'order by id limit 5',
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
738
        filter => {title => 'encode_utf8'}
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
739
    );
added commit method
yuki-kimoto authored on 2010-05-27
740
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
741
    # Select, join table
added commit method
yuki-kimoto authored on 2010-05-27
742
    my $result = $dbi->select(
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
743
        table    => ['books', 'rental'],
744
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
745
        relation => {'books.id' => 'rental.book_id'}
746
    );
updated document
yuki-kimoto authored on 2010-08-08
747
    
748
    # Select, more flexible where
749
    my $result = $dbi->select(
750
        table  => 'books',
751
        where  => ['{= author} and {like title}', 
752
                   {author => 'Ken', title => '%Perl%'}]
753
    );
cleanup
yuki-kimoto authored on 2010-08-05
754

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
757
    # Execute SQL
removed register_format()
yuki-kimoto authored on 2010-05-26
758
    $dbi->execute("select title from books");
759
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
760
    # Execute SQL with hash binding and filtering
updated document
yuki-kimoto authored on 2010-08-08
761
    $dbi->execute("select id from books where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
762
                  param  => {author => 'ken', title => '%Perl%'},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
763
                  filter => {title => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
764

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

            
updated document
yuki-kimoto authored on 2010-08-08
771
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
772

            
773
    # Get DBI object
774
    my $dbh = $dbi->dbh;
775

            
776
Fetch row.
777

            
removed register_format()
yuki-kimoto authored on 2010-05-26
778
    # Fetch
779
    while (my $row = $result->fetch) {
780
        # ...
781
    }
782
    
783
    # Fetch hash
784
    while (my $row = $result->fetch_hash) {
785
        
786
    }
787
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
788
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
789

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
824
    $dbi          = $dbi->cache_method(\&cache_method);
825
    $cache_method = $dbi->cache_method
826

            
827
Method to set and get caches.
828

            
829
B<Example:>
830

            
831
    $dbi->cache_method(
832
        sub {
833
            my $self = shift;
834
            
835
            $self->{_cached} ||= {};
836
            
837
            if (@_ > 1) {
838
                $self->{_cached}{$_[0]} = $_[1] 
839
            }
840
            else {
841
                return $self->{_cached}{$_[0]}
842
            }
843
        }
844
    );
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
845

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

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

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

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

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

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

            
added dbi_options attribute
kimoto authored on 2010-12-20
861
=head2 C<dbi_options>
862

            
863
    my $dbi_options = $dbi->dbi_options;
864
    $dbi            = $dbi->dbi_options($dbi_options);
865

            
866
DBI options.
867
C<connect()> method use this value to connect the database.
868

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
876
Filter functions.
877
"encode_utf8" and "decode_utf8" is registered by default.
878

            
879
=head2 C<filter_check>
880

            
881
    my $filter_check = $dbi->filter_check;
882
    $dbi             = $dbi->filter_check(0);
883

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

            
887
=head2 C<password>
888

            
889
    my $password = $dbi->password;
890
    $dbi         = $dbi->password('lkj&le`@s');
891

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
914
    my $user = $dbi->user;
915
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
916

            
cleanup
yuki-kimoto authored on 2010-10-17
917
User name.
918
C<connect()> method use this value to connect the database.
919
    
920
=head1 METHODS
added commit method
yuki-kimoto authored on 2010-05-27
921

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

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

            
927
    $dbi->auto_filter(
928
        $table,
929
        [$column1, $bind_filter1, $fetch_filter1],
930
        [$column2, $bind_filter2, $fetch_filter2],
931
        [...],
932
    );
933

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

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

            
cleanup
Yuki Kimoto authored on 2010-12-21
941
    $result = $dbi->execute(
942
        "select * from table1 where {= key1} and {= key2};",
943
         param => {key1 => 1, key2 => 2},
944
         auto_filter_table => ['table1']
945
    );
946
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
947
B<Example:>
948

            
949
    $dbi->auto_filter('books', 'sale_date', 'to_date', 'date_to');
950

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

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

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

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

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

            
963
    $dbi->commit;
964

            
965
Commit transaction.
966
This is same as L<DBI>'s C<commit>.
967

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
978
=head2 C<create_query>
979
    
980
    my $query = $dbi->create_query(
981
        "select * from books where {= author} and {like title};"
982
    );
update document
yuki-kimoto authored on 2009-11-19
983

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

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

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

            
993
    $dbi = $dbi->default_bind_filter($fname);
994

            
995
Default filter when parameter binding is executed.
996

            
997
=head2 C<(deprecated) default_fetch_filter>
998

            
999
    $dbi = $dbi->default_fetch_filter($fname);
1000

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1013
    my $result = $dbi->execute(
1014
        "select * from books where {= author} and {like title}", 
1015
        param => {author => 'Ken', title => '%Perl%'}
1016
    );
1017
    
1018
    while (my $row = $result->fetch) {
1019
        my $author = $row->[0];
1020
        my $title  = $row->[1];
1021
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
1022

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

            
1025
    my %expand = $dbi->expand($source);
1026

            
1027
The following hash
1028

            
1029
    {books => {title => 'Perl', author => 'Ken'}}
1030

            
1031
is expanded to
1032

            
1033
    ('books.title' => 'Perl', 'books.author' => 'Ken')
1034

            
1035
This is used in C<select()>
1036

            
1037

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1041
    $dbi->delete(table  => $table,
1042
                 where  => \%where,
1043
                 append => $append,
1044
                 filter => \%filter);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1045

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1046
Execute delete statement.
1047
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
1048
C<table> is a table name.
1049
C<where> is where clause. this must be hash reference.
1050
C<append> is a string added at the end of the SQL statement.
1051
C<filter> is filters when parameter binding is executed.
cleanup
yuki-kimoto authored on 2010-08-09
1052
Return value of C<delete()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1053

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

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

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

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

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

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

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

            
1076
    $dbi->helper(
1077
        update_or_insert => sub {
1078
            my $self = shift;
1079
            # do something
1080
        },
1081
        find_or_create   => sub {
1082
            my $self = shift;
1083
            # do something
1084
        }
1085
    );
1086

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

            
1089
    $dbi->update_or_insert;
1090
    $dbi->find_or_create;
1091

            
cleanup
yuki-kimoto authored on 2010-10-17
1092
=head2 C<insert>
1093

            
1094
    $dbi->insert(table  => $table, 
1095
                 param  => \%param,
1096
                 append => $append,
1097
                 filter => \%filter);
1098

            
1099
Execute insert statement.
1100
C<insert> method have C<table>, C<param>, C<append>
1101
and C<filter> arguments.
1102
C<table> is a table name.
1103
C<param> is the pairs of column name value. this must be hash reference.
1104
C<append> is a string added at the end of the SQL statement.
1105
C<filter> is filters when parameter binding is executed.
1106
This is overwrites C<default_bind_filter>.
1107
Return value of C<insert()> is the count of affected rows.
1108

            
1109
B<Example:>
1110

            
1111
    $dbi->insert(table  => 'books', 
1112
                 param  => {title => 'Perl', author => 'Taro'},
1113
                 append => "some statement",
1114
                 filter => {title => 'encode_utf8'})
1115

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

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

            
1121
Create a new L<DBIx::Custom> object.
1122

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

            
1125
    $dbi->register_filter(%filters);
1126
    $dbi->register_filter(\%filters);
1127
    
1128
Register filter. Registered filters is available in the following attributes
1129
or arguments.
1130

            
1131
=over 4
1132

            
1133
=item *
1134

            
1135
C<filter> argument of C<insert()>, C<update()>,
1136
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1137
methods
1138

            
1139
=item *
1140

            
1141
C<execute()> method
1142

            
1143
=item *
1144

            
1145
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1146

            
1147
=item *
1148

            
1149
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1150

            
1151
=back
1152

            
1153
B<Example:>
1154

            
1155
    $dbi->register_filter(
1156
        encode_utf8 => sub {
1157
            my $value = shift;
1158
            
1159
            require Encode;
1160
            
1161
            return Encode::encode('UTF-8', $value);
1162
        },
1163
        decode_utf8 => sub {
1164
            my $value = shift;
1165
            
1166
            require Encode;
1167
            
1168
            return Encode::decode('UTF-8', $value)
1169
        }
1170
    );
1171

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

            
1174
    $dbi->rollback;
1175

            
1176
Rollback transaction.
1177
This is same as L<DBI>'s C<rollback>.
1178

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1179
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1180
    
cleanup
yuki-kimoto authored on 2010-08-05
1181
    my $result = $dbi->select(table    => $table,
1182
                              column   => [@column],
1183
                              where    => \%where,
1184
                              append   => $append,
1185
                              relation => \%relation,
1186
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1187

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1188
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1189
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1190
C<relation> and C<filter> arguments.
1191
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1192
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1193
C<append> is a string added at the end of the SQL statement.
1194
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
1195

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

            
added commit method
yuki-kimoto authored on 2010-05-27
1198
    # select * from books;
cleanup
yuki-kimoto authored on 2010-08-05
1199
    my $result = $dbi->select(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
1200
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1201
    # select * from books where title = ?;
1202
    my $result = $dbi->select(table => 'books', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1203
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1204
    # select title, author from books where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1205
    my $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
1206
        table  => 'books',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1207
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1208
        where  => {id => 1},
1209
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1210
    );
1211
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1212
    # select books.name as book_name from books, rental
added commit method
yuki-kimoto authored on 2010-05-27
1213
    # where books.id = rental.book_id;
1214
    my $result = $dbi->select(
removed reconnect method
yuki-kimoto authored on 2010-05-28
1215
        table    => ['books', 'rental'],
1216
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
1217
        relation => {'books.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1218
    );
1219

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

            
1223
    my $result = $dbi->select(
1224
        table  => 'books',
1225
        column => ['title', 'author'],
1226
        where  => ['{= title} or {like author}',
1227
                   {title => '%Perl%', author => 'Ken'}]
1228
    );
1229

            
1230
First element is a string. it contains tags,
1231
such as "{= title} or {like author}".
1232
Second element is paramters.
1233

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1236
    $dbi->update(table  => $table, 
1237
                 param  => \%params,
1238
                 where  => \%where,
1239
                 append => $append,
1240
                 filter => \%filter)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1241

            
cleanup
yuki-kimoto authored on 2010-10-17
1242
Execute update statement.
1243
C<update> method have C<table>, C<param>, C<where>, C<append>
1244
and C<filter> arguments.
1245
C<table> is a table name.
1246
C<param> is column-value pairs. this must be hash reference.
1247
C<where> is where clause. this must be hash reference.
1248
C<append> is a string added at the end of the SQL statement.
1249
C<filter> is filters when parameter binding is executed.
1250
This is overwrites C<default_bind_filter>.
1251
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1252

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1255
    $dbi->update(table  => 'books',
1256
                 param  => {title => 'Perl', author => 'Taro'},
1257
                 where  => {id => 5},
1258
                 append => "some statement",
1259
                 filter => {title => 'encode_utf8'});
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1260

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1263
    $dbi->update_all(table  => $table, 
1264
                     param  => \%params,
1265
                     filter => \%filter,
1266
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1267

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

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

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

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

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

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

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

            
1287
C<< <kimoto.yuki at gmail.com> >>
1288

            
1289
L<http://github.com/yuki-kimoto/DBIx-Custom>
1290

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1291
=head1 AUTHOR
1292

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

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

            
1297
Copyright 2009 Yuki Kimoto, all rights reserved.
1298

            
1299
This program is free software; you can redistribute it and/or modify it
1300
under the same terms as Perl itself.
1301

            
1302
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1303

            
1304