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

            
cleanup
yuki-kimoto authored on 2010-10-17
3
our $VERSION = '0.1618';
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-10-17
18
__PACKAGE__->attr([qw/data_source dbh default_bind_filter
19
                      default_fetch_filter 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
yuki-kimoto authored on 2010-10-17
37
__PACKAGE__->dual_attr('filters', default => sub { {} },
38
                                  inherit => 'hash_copy');
added check_filter attribute
yuki-kimoto authored on 2010-08-08
39
__PACKAGE__->attr(filter_check => 1);
cleanup
yuki-kimoto authored on 2010-10-17
40
__PACKAGE__->attr(query_builder  => sub {DBIx::Custom::QueryBuilder->new});
41
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result');
42

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

            
56
# Regster filter
57
__PACKAGE__->register_filter(
58
    encode_utf8 => sub { encode_utf8($_[0]) },
59
    decode_utf8 => sub { decode_utf8($_[0]) }
60
);
added check_filter attribute
yuki-kimoto authored on 2010-08-08
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

            
79
sub helper {
80
    my $self = shift;
81
    
82
    # Merge
83
    my $helpers = ref $_[0] eq 'HASH' ? $_[0] : {@_};
84
    $self->{_helpers} = {%{$self->{_helpers} || {}}, %$helpers};
85
    
86
    return $self;
87
}
88

            
packaging one directory
yuki-kimoto authored on 2009-11-16
89
sub connect {
removed register_format()
yuki-kimoto authored on 2010-05-26
90
    my $proto = shift;
91
    
92
    # Create
93
    my $self = ref $proto ? $proto : $proto->new(@_);
update document
yuki-kimoto authored on 2010-01-30
94
    
95
    # Information
packaging one directory
yuki-kimoto authored on 2009-11-16
96
    my $data_source = $self->data_source;
97
    my $user        = $self->user;
98
    my $password    = $self->password;
99
    
removed experimental registe...
yuki-kimoto authored on 2010-08-24
100
    
update document
yuki-kimoto authored on 2010-01-30
101
    # Connect
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
102
    my $dbh = eval {DBI->connect(
packaging one directory
yuki-kimoto authored on 2009-11-16
103
        $data_source,
104
        $user,
105
        $password,
106
        {
107
            RaiseError => 1,
108
            PrintError => 0,
109
            AutoCommit => 1,
110
        }
111
    )};
112
    
update document
yuki-kimoto authored on 2010-01-30
113
    # Connect error
packaging one directory
yuki-kimoto authored on 2009-11-16
114
    croak $@ if $@;
115
    
update document
yuki-kimoto authored on 2010-01-30
116
    # Database handle
packaging one directory
yuki-kimoto authored on 2009-11-16
117
    $self->dbh($dbh);
update document
yuki-kimoto authored on 2010-01-30
118
    
packaging one directory
yuki-kimoto authored on 2009-11-16
119
    return $self;
120
}
121

            
cleanup
yuki-kimoto authored on 2010-10-17
122
sub create_query {
123
    my ($self, $source) = @_;
update document
yuki-kimoto authored on 2010-01-30
124
    
cleanup
yuki-kimoto authored on 2010-10-17
125
    # Cache
126
    my $cache = $self->cache;
update document
yuki-kimoto authored on 2010-01-30
127
    
cleanup
yuki-kimoto authored on 2010-10-17
128
    # Create query
129
    my $query;
130
    if ($cache) {
131
        
132
        # Get query
133
        my $q = $self->cache_method->($self, $source);
134
        
135
        # Create query
136
        $query = DBIx::Custom::Query->new($q) if $q;
137
    }
138
    
139
    unless ($query) {
cleanup insert
yuki-kimoto authored on 2010-04-28
140

            
cleanup
yuki-kimoto authored on 2010-10-17
141
        # Create SQL object
142
        my $builder = $self->query_builder;
143
        
144
        # Create query
145
        $query = $builder->build_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
146

            
cleanup
yuki-kimoto authored on 2010-10-17
147
        # Cache query
148
        $self->cache_method->($self, $source,
149
                             {sql     => $query->sql, 
150
                              columns => $query->columns})
151
          if $cache;
cleanup insert
yuki-kimoto authored on 2010-04-28
152
    }
153
    
cleanup
yuki-kimoto authored on 2010-10-17
154
    # Prepare statement handle
155
    my $sth;
156
    eval { $sth = $self->dbh->prepare($query->{sql})};
157
    $self->_croak($@, qq{. SQL: "$query->{sql}"}) if $@;
packaging one directory
yuki-kimoto authored on 2009-11-16
158
    
cleanup
yuki-kimoto authored on 2010-10-17
159
    # Set statement handle
160
    $query->sth($sth);
packaging one directory
yuki-kimoto authored on 2009-11-16
161
    
cleanup
yuki-kimoto authored on 2010-10-17
162
    return $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
163
}
164

            
cleanup
yuki-kimoto authored on 2010-10-17
165
our %VALID_DELETE_ARGS
166
  = map { $_ => 1 } qw/table where append filter allow_delete_all/;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
167

            
cleanup
yuki-kimoto authored on 2010-10-17
168
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
169
    my ($self, %args) = @_;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
170
    
171
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
172
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
173
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
174
          unless $VALID_DELETE_ARGS{$name};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
175
    }
176
    
177
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
178
    my $table            = $args{table} || '';
179
    my $where            = $args{where} || {};
cleanup
yuki-kimoto authored on 2010-10-17
180
    my $append = $args{append};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
181
    my $filter           = $args{filter};
cleanup
yuki-kimoto authored on 2010-10-17
182
    my $allow_delete_all = $args{allow_delete_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
183
    
184
    # Where keys
removed register_format()
yuki-kimoto authored on 2010-05-26
185
    my @where_keys = keys %$where;
packaging one directory
yuki-kimoto authored on 2009-11-16
186
    
187
    # Not exists where keys
add tests
yuki-kimoto authored on 2010-08-10
188
    croak qq{"where" argument must be specified and } .
189
          qq{contains the pairs of column name and value}
cleanup
yuki-kimoto authored on 2010-10-17
190
      if !@where_keys && !$allow_delete_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
191
    
192
    # Where clause
193
    my $where_clause = '';
194
    if (@where_keys) {
195
        $where_clause = 'where ';
add tests
yuki-kimoto authored on 2010-08-10
196
        $where_clause .= "{= $_} and " for @where_keys;
packaging one directory
yuki-kimoto authored on 2009-11-16
197
        $where_clause =~ s/ and $//;
198
    }
199
    
add tests
yuki-kimoto authored on 2010-08-10
200
    # Source of SQL
cleanup
yuki-kimoto authored on 2010-10-17
201
    my $source = "delete from $table $where_clause";
add tests
yuki-kimoto authored on 2010-08-10
202
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
203
    
204
    # Execute query
cleanup
yuki-kimoto authored on 2010-10-17
205
    my $ret_val = $self->execute($source, param  => $where, 
add tests
yuki-kimoto authored on 2010-08-10
206
                                 filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
207
    
208
    return $ret_val;
209
}
210

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

            
added helper method
yuki-kimoto authored on 2010-10-17
213
sub DESTROY { }
214

            
cleanup
yuki-kimoto authored on 2010-10-17
215
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
216

            
cleanup
yuki-kimoto authored on 2010-10-17
217
sub execute{
218
    my ($self, $query, %args)  = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
219
    
220
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
221
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
222
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
223
          unless $VALID_EXECUTE_ARGS{$name};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
224
    }
225
    
cleanup
yuki-kimoto authored on 2010-10-17
226
    my $params = $args{param} || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
227
    
cleanup
yuki-kimoto authored on 2010-10-17
228
    # First argument is the soruce of SQL
229
    $query = $self->create_query($query)
230
      unless ref $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
231
    
cleanup
yuki-kimoto authored on 2010-10-17
232
    my $filter = $args{filter} || $query->filter || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
233
    
cleanup
yuki-kimoto authored on 2010-10-17
234
    # Create bind value
235
    my $bind_values = $self->_build_bind_values($query, $params, $filter);
236
    
237
    # Execute
238
    my $sth      = $query->sth;
239
    my $affected;
240
    eval {$affected = $sth->execute(@$bind_values)};
241
    $self->_croak($@) if $@;
242
    
243
    # Return resultset if select statement is executed
244
    if ($sth->{NUM_OF_FIELDS}) {
245
        
246
        # Create result
247
        my $result = $self->result_class->new(
248
            sth            => $sth,
249
            default_filter => $self->default_fetch_filter,
250
            filters        => $self->filters,
251
            filter_check   => $self->filter_check
252
        );
253

            
254
        return $result;
255
    }
256
    return $affected;
257
}
258

            
259
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append filter/;
260

            
261
sub insert {
262
    my ($self, %args) = @_;
263

            
264
    # Check arguments
265
    foreach my $name (keys %args) {
266
        croak qq{"$name" is invalid argument}
267
          unless $VALID_INSERT_ARGS{$name};
packaging one directory
yuki-kimoto authored on 2009-11-16
268
    }
269
    
cleanup
yuki-kimoto authored on 2010-10-17
270
    # Arguments
271
    my $table  = $args{table} || '';
272
    my $param  = $args{param} || {};
273
    my $append = $args{append} || '';
274
    my $filter = $args{filter};
275
    
276
    # Insert keys
277
    my @insert_keys = keys %$param;
278
    
279
    # Templte for insert
280
    my $source = "insert into $table {insert_param "
281
               . join(' ', @insert_keys) . '}';
add tests
yuki-kimoto authored on 2010-08-10
282
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
283
    
284
    # Execute query
cleanup
yuki-kimoto authored on 2010-10-17
285
    my $ret_val = $self->execute($source, param  => $param, 
286
                                          filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
287
    
288
    return $ret_val;
289
}
290

            
cleanup
yuki-kimoto authored on 2010-10-17
291
sub register_filter {
292
    my $invocant = shift;
293
    
294
    # Register filter
295
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
296
    $invocant->filters({%{$invocant->filters}, %$filters});
297
    
298
    return $invocant;
299
}
packaging one directory
yuki-kimoto authored on 2009-11-16
300

            
refactoring select
yuki-kimoto authored on 2010-04-28
301
our %VALID_SELECT_ARGS
added commit method
yuki-kimoto authored on 2010-05-27
302
  = map { $_ => 1 } qw/table column where append relation filter param/;
refactoring select
yuki-kimoto authored on 2010-04-28
303

            
packaging one directory
yuki-kimoto authored on 2009-11-16
304
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
305
    my ($self, %args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
306
    
refactoring select
yuki-kimoto authored on 2010-04-28
307
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
308
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
309
        croak qq{"$name" is invalid argument}
refactoring select
yuki-kimoto authored on 2010-04-28
310
          unless $VALID_SELECT_ARGS{$name};
311
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
312
    
refactoring select
yuki-kimoto authored on 2010-04-28
313
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
314
    my $tables = $args{table} || [];
removed register_format()
yuki-kimoto authored on 2010-05-26
315
    $tables = [$tables] unless ref $tables eq 'ARRAY';
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
316
    my $columns  = $args{column} || [];
update document
yuki-kimoto authored on 2010-08-07
317
    my $where    = $args{where};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
318
    my $relation = $args{relation};
319
    my $append   = $args{append};
320
    my $filter   = $args{filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
321
    
add tests
yuki-kimoto authored on 2010-08-10
322
    # Source of SQL
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
323
    my $source = 'select ';
packaging one directory
yuki-kimoto authored on 2009-11-16
324
    
added commit method
yuki-kimoto authored on 2010-05-27
325
    # Column clause
packaging one directory
yuki-kimoto authored on 2009-11-16
326
    if (@$columns) {
327
        foreach my $column (@$columns) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
328
            $source .= "$column, ";
packaging one directory
yuki-kimoto authored on 2009-11-16
329
        }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
330
        $source =~ s/, $/ /;
packaging one directory
yuki-kimoto authored on 2009-11-16
331
    }
332
    else {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
333
        $source .= '* ';
packaging one directory
yuki-kimoto authored on 2009-11-16
334
    }
335
    
added commit method
yuki-kimoto authored on 2010-05-27
336
    # Table
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
337
    $source .= 'from ';
packaging one directory
yuki-kimoto authored on 2009-11-16
338
    foreach my $table (@$tables) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
339
        $source .= "$table, ";
packaging one directory
yuki-kimoto authored on 2009-11-16
340
    }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
341
    $source =~ s/, $/ /;
packaging one directory
yuki-kimoto authored on 2009-11-16
342
    
added commit method
yuki-kimoto authored on 2010-05-27
343
    # Where clause
update document
yuki-kimoto authored on 2010-08-07
344
    my $param;
345
    if (ref $where eq 'HASH') {
346
        $param = $where;
347
        $source .= 'where (';
348
        foreach my $where_key (keys %$where) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
349
            $source .= "{= $where_key} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
350
        }
update document
yuki-kimoto authored on 2010-08-07
351
        $source =~ s/ and $//;
352
        $source .= ') ';
353
    }
354
    elsif (ref $where eq 'ARRAY') {
355
        my$where_str = $where->[0] || '';
356
        $param = $where->[1];
357
        
358
        $source .= "where ($where_str) ";
packaging one directory
yuki-kimoto authored on 2009-11-16
359
    }
360
    
added commit method
yuki-kimoto authored on 2010-05-27
361
    # Relation
362
    if ($relation) {
update document
yuki-kimoto authored on 2010-08-07
363
        $source .= $where ? "and " : "where ";
added commit method
yuki-kimoto authored on 2010-05-27
364
        foreach my $rkey (keys %$relation) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
365
            $source .= "$rkey = " . $relation->{$rkey} . " and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
366
        }
367
    }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
368
    $source =~ s/ and $//;
added commit method
yuki-kimoto authored on 2010-05-27
369
    
370
    # Append some statement
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
371
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
372
    
373
    # Execute query
update document
yuki-kimoto authored on 2010-08-07
374
    my $result = $self->execute($source, param  => $param, 
375
                                         filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
376
    
377
    return $result;
378
}
379

            
cleanup
yuki-kimoto authored on 2010-10-17
380
our %VALID_UPDATE_ARGS
381
  = map { $_ => 1 } qw/table param where append filter allow_update_all/;
382

            
383
sub update {
384
    my ($self, %args) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
385
    
cleanup
yuki-kimoto authored on 2010-10-17
386
    # Check arguments
387
    foreach my $name (keys %args) {
388
        croak qq{"$name" is invalid argument}
389
          unless $VALID_UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
390
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
391
    
cleanup
yuki-kimoto authored on 2010-10-17
392
    # Arguments
393
    my $table            = $args{table} || '';
394
    my $param            = $args{param} || {};
395
    my $where            = $args{where} || {};
396
    my $append = $args{append} || '';
397
    my $filter           = $args{filter};
398
    my $allow_update_all = $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
399
    
cleanup
yuki-kimoto authored on 2010-10-17
400
    # Update keys
401
    my @update_keys = keys %$param;
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
402
    
cleanup
yuki-kimoto authored on 2010-10-17
403
    # Where keys
404
    my @where_keys = keys %$where;
removed reconnect method
yuki-kimoto authored on 2010-05-28
405
    
cleanup
yuki-kimoto authored on 2010-10-17
406
    # Not exists where keys
407
    croak qq{"where" argument must be specified and } .
408
          qq{contains the pairs of column name and value}
409
      if !@where_keys && !$allow_update_all;
removed experimental registe...
yuki-kimoto authored on 2010-08-24
410
    
cleanup
yuki-kimoto authored on 2010-10-17
411
    # Update clause
412
    my $update_clause = '{update_param ' . join(' ', @update_keys) . '}';
removed experimental registe...
yuki-kimoto authored on 2010-08-24
413
    
cleanup
yuki-kimoto authored on 2010-10-17
414
    # Where clause
415
    my $where_clause = '';
416
    my $new_where = {};
removed reconnect method
yuki-kimoto authored on 2010-05-28
417
    
cleanup
yuki-kimoto authored on 2010-10-17
418
    if (@where_keys) {
419
        $where_clause = 'where ';
420
        $where_clause .= "{= $_} and " for @where_keys;
421
        $where_clause =~ s/ and $//;
removed reconnect method
yuki-kimoto authored on 2010-05-28
422
    }
423
    
cleanup
yuki-kimoto authored on 2010-10-17
424
    # Source of SQL
425
    my $source = "update $table $update_clause $where_clause";
426
    $source .= " $append" if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
427
    
cleanup
yuki-kimoto authored on 2010-10-17
428
    # Rearrange parameters
429
    foreach my $wkey (@where_keys) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
430
        
cleanup
yuki-kimoto authored on 2010-10-17
431
        if (exists $param->{$wkey}) {
432
            $param->{$wkey} = [$param->{$wkey}]
433
              unless ref $param->{$wkey} eq 'ARRAY';
434
            
435
            push @{$param->{$wkey}}, $where->{$wkey};
436
        }
437
        else {
438
            $param->{$wkey} = $where->{$wkey};
439
        }
removed reconnect method
yuki-kimoto authored on 2010-05-28
440
    }
cleanup
yuki-kimoto authored on 2010-10-17
441
    
442
    # Execute query
443
    my $ret_val = $self->execute($source, param  => $param, 
444
                                 filter => $filter);
445
    
446
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
447
}
448

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
451
sub _build_bind_values {
452
    my ($self, $query, $params, $filter) = @_;
453
    
454
    # binding values
455
    my @bind_values;
add tests
yuki-kimoto authored on 2010-08-08
456

            
457
    # Filter
458
    $filter ||= {};
459
    
460
    # Parameter
461
    $params ||= {};
462
    
463
    # Check filter
464
    $self->_check_filter($self->filters, $filter,
465
                         $self->default_bind_filter, $params)
466
      if $self->filter_check;
removed reconnect method
yuki-kimoto authored on 2010-05-28
467
    
468
    # Build bind values
469
    my $count = {};
470
    foreach my $column (@{$query->columns}) {
471
        
472
        # Value
473
        my $value = ref $params->{$column} eq 'ARRAY'
474
                  ? $params->{$column}->[$count->{$column} || 0]
475
                  : $params->{$column};
476
        
add tests
yuki-kimoto authored on 2010-08-10
477
        # Filtering
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
478
        my $fname = $filter->{$column} || $self->default_bind_filter || '';
add tests
yuki-kimoto authored on 2010-08-10
479
        my $filter_func = $fname ? $self->filters->{$fname} : undef;
removed reconnect method
yuki-kimoto authored on 2010-05-28
480
        push @bind_values, $filter_func
481
                         ? $filter_func->($value)
482
                         : $value;
483
        
484
        # Count up 
485
        $count->{$column}++;
486
    }
487
    
488
    return \@bind_values;
489
}
490

            
add tests
yuki-kimoto authored on 2010-08-08
491
sub _check_filter {
492
    my ($self, $filters, $filter, $default_filter, $params) = @_;
493
    
494
    # Filter name not exists
495
    foreach my $fname (values %$filter) {
496
        croak qq{Bind filter "$fname" is not registered}
497
          unless exists $filters->{$fname};
498
    }
499
    
500
    # Default filter name not exists
501
    croak qq{Default bind filter "$default_filter" is not registered}
502
      if $default_filter && ! exists $filters->{$default_filter};
503
    
504
    # Column name not exists
505
    foreach my $column (keys %$filter) {
506
        
507
        croak qq{Column name "$column" in bind filter is not found in paramters}
508
          unless exists $params->{$column};
509
    }
510
}
511

            
cleanup
yuki-kimoto authored on 2010-10-17
512
sub _croak {
513
    my ($self, $error, $append) = @_;
514
    $append ||= "";
515
    
516
    # Verbose
517
    if ($Carp::Verbose) { croak $error }
518
    
519
    # Not verbose
520
    else {
521
        
522
        # Remove line and module infromation
523
        my $at_pos = rindex($error, ' at ');
524
        $error = substr($error, 0, $at_pos);
525
        $error =~ s/\s+$//;
526
        
527
        croak "$error$append";
528
    }
529
}
530

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
533
=head1 NAME
534

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

            
537
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
538

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
547
    # Insert 
548
    $dbi->insert(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
549
                 param  => {title => 'Perl', author => 'Ken'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
550
                 filter => {title => 'encode_utf8'});
551
    
552
    # Update 
553
    $dbi->update(table  => 'books', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
554
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
555
                 where  => {id => 5},
556
                 filter => {title => 'encode_utf8'});
557
    
558
    # Update all
559
    $dbi->update_all(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
560
                     param  => {title => 'Perl'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
561
                     filter => {title => 'encode_utf8'});
562
    
563
    # Delete
564
    $dbi->delete(table  => 'books',
565
                 where  => {author => 'Ken'},
566
                 filter => {title => 'encode_utf8'});
567
    
568
    # Delete all
569
    $dbi->delete_all(table => 'books');
cleanup
yuki-kimoto authored on 2010-08-05
570

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
573
    # Select
574
    my $result = $dbi->select(table => 'books');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
575
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
576
    # Select, more complex
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
577
    my $result = $dbi->select(
update document
yuki-kimoto authored on 2010-05-27
578
        table  => 'books',
579
        column => [qw/author title/],
580
        where  => {author => 'Ken'},
updated document
yuki-kimoto authored on 2010-08-08
581
        append => 'order by id limit 5',
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
582
        filter => {title => 'encode_utf8'}
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
583
    );
added commit method
yuki-kimoto authored on 2010-05-27
584
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
585
    # Select, join table
added commit method
yuki-kimoto authored on 2010-05-27
586
    my $result = $dbi->select(
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
587
        table    => ['books', 'rental'],
588
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
589
        relation => {'books.id' => 'rental.book_id'}
590
    );
updated document
yuki-kimoto authored on 2010-08-08
591
    
592
    # Select, more flexible where
593
    my $result = $dbi->select(
594
        table  => 'books',
595
        where  => ['{= author} and {like title}', 
596
                   {author => 'Ken', title => '%Perl%'}]
597
    );
cleanup
yuki-kimoto authored on 2010-08-05
598

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
601
    # Execute SQL
removed register_format()
yuki-kimoto authored on 2010-05-26
602
    $dbi->execute("select title from books");
603
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
604
    # Execute SQL with hash binding and filtering
updated document
yuki-kimoto authored on 2010-08-08
605
    $dbi->execute("select id from books where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
606
                  param  => {author => 'ken', title => '%Perl%'},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
607
                  filter => {title => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
608

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

            
updated document
yuki-kimoto authored on 2010-08-08
615
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
616

            
removed register_format()
yuki-kimoto authored on 2010-05-26
617
    # Default filter
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
618
    $dbi->default_bind_filter('encode_utf8');
removed register_format()
yuki-kimoto authored on 2010-05-26
619
    $dbi->default_fetch_filter('decode_utf8');
cleanup
yuki-kimoto authored on 2010-08-05
620

            
621
    # Get DBI object
622
    my $dbh = $dbi->dbh;
623

            
624
Fetch row.
625

            
removed register_format()
yuki-kimoto authored on 2010-05-26
626
    # Fetch
627
    while (my $row = $result->fetch) {
628
        # ...
629
    }
630
    
631
    # Fetch hash
632
    while (my $row = $result->fetch_hash) {
633
        
634
    }
635
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
636
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
637

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

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
647
L<DBIx::Custom> is middle area between L<DBI> and O/R mapper.
updated document
yuki-kimoto authored on 2010-08-08
648
L<DBIx::Custom> provide flexible hash parameter binding and filtering system,
649
and suger methods, such as C<select()>, C<update()>, C<delete()>, C<select()>
650
to execute SQL easily.
removed reconnect method
yuki-kimoto authored on 2010-05-28
651

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
672
    $dbi          = $dbi->cache_method(\&cache_method);
673
    $cache_method = $dbi->cache_method
674

            
675
Method to set and get caches.
676

            
677
B<Example:>
678

            
679
    $dbi->cache_method(
680
        sub {
681
            my $self = shift;
682
            
683
            $self->{_cached} ||= {};
684
            
685
            if (@_ > 1) {
686
                $self->{_cached}{$_[0]} = $_[1] 
687
            }
688
            else {
689
                return $self->{_cached}{$_[0]}
690
            }
691
        }
692
    );
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
693

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

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

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

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

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

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

            
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
709
=head2 C<default_bind_filter>
packaging one directory
yuki-kimoto authored on 2009-11-16
710

            
cleanup
yuki-kimoto authored on 2010-08-03
711
    my $default_bind_filter = $dbi->default_bind_filter
712
    $dbi                    = $dbi->default_bind_filter('encode_utf8');
packaging one directory
yuki-kimoto authored on 2009-11-16
713

            
cleanup
yuki-kimoto authored on 2010-08-05
714
Default filter when parameter binding is executed.
packaging one directory
yuki-kimoto authored on 2009-11-16
715

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
716
=head2 C<default_fetch_filter>
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
717

            
cleanup
yuki-kimoto authored on 2010-08-03
718
    my $default_fetch_filter = $dbi->default_fetch_filter;
719
    $dbi                     = $dbi->default_fetch_filter('decode_utf8');
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
720

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
728
Filter functions.
729
"encode_utf8" and "decode_utf8" is registered by default.
730

            
731
=head2 C<filter_check>
732

            
733
    my $filter_check = $dbi->filter_check;
734
    $dbi             = $dbi->filter_check(0);
735

            
736
Enable filter check. 
737
Default to 1.
738
This check maybe damege performance.
739
If you require performance, set C<filter_check> attribute to 0.
740

            
741
=head2 C<password>
742

            
743
    my $password = $dbi->password;
744
    $dbi         = $dbi->password('lkj&le`@s');
745

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
768
    my $user = $dbi->user;
769
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
770

            
cleanup
yuki-kimoto authored on 2010-10-17
771
User name.
772
C<connect()> method use this value to connect the database.
773
    
774
=head1 METHODS
added commit method
yuki-kimoto authored on 2010-05-27
775

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

            
cleanup
yuki-kimoto authored on 2010-10-17
779
=head2 begin_work
added check_filter attribute
yuki-kimoto authored on 2010-08-08
780

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
789
=head2 commit
790

            
791
    $dbi->commit;
792

            
793
Commit transaction.
794
This is same as L<DBI>'s C<commit>.
795

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
806
=head2 C<create_query>
807
    
808
    my $query = $dbi->create_query(
809
        "select * from books where {= author} and {like title};"
810
    );
update document
yuki-kimoto authored on 2009-11-19
811

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
831
    my $result = $dbi->execute(
832
        "select * from books where {= author} and {like title}", 
833
        param => {author => 'Ken', title => '%Perl%'}
834
    );
835
    
836
    while (my $row = $result->fetch) {
837
        my $author = $row->[0];
838
        my $title  = $row->[1];
839
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
840

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

            
cleanup
yuki-kimoto authored on 2010-08-05
843
    $dbi->delete(table  => $table,
844
                 where  => \%where,
845
                 append => $append,
846
                 filter => \%filter);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
847

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
848
Execute delete statement.
849
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
850
C<table> is a table name.
851
C<where> is where clause. this must be hash reference.
852
C<append> is a string added at the end of the SQL statement.
853
C<filter> is filters when parameter binding is executed.
cleanup
yuki-kimoto authored on 2010-08-09
854
Return value of C<delete()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
855

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

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

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

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

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

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

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

            
878
    $dbi->helper(
879
        update_or_insert => sub {
880
            my $self = shift;
881
            # do something
882
        },
883
        find_or_create   => sub {
884
            my $self = shift;
885
            # do something
886
        }
887
    );
888

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

            
891
    $dbi->update_or_insert;
892
    $dbi->find_or_create;
893

            
cleanup
yuki-kimoto authored on 2010-10-17
894
=head2 C<insert>
895

            
896
    $dbi->insert(table  => $table, 
897
                 param  => \%param,
898
                 append => $append,
899
                 filter => \%filter);
900

            
901
Execute insert statement.
902
C<insert> method have C<table>, C<param>, C<append>
903
and C<filter> arguments.
904
C<table> is a table name.
905
C<param> is the pairs of column name value. this must be hash reference.
906
C<append> is a string added at the end of the SQL statement.
907
C<filter> is filters when parameter binding is executed.
908
This is overwrites C<default_bind_filter>.
909
Return value of C<insert()> is the count of affected rows.
910

            
911
B<Example:>
912

            
913
    $dbi->insert(table  => 'books', 
914
                 param  => {title => 'Perl', author => 'Taro'},
915
                 append => "some statement",
916
                 filter => {title => 'encode_utf8'})
917

            
918
=head2 C<register_filter>
919

            
920
    $dbi->register_filter(%filters);
921
    $dbi->register_filter(\%filters);
922
    
923
Register filter. Registered filters is available in the following attributes
924
or arguments.
925

            
926
=over 4
927

            
928
=item *
929

            
930
C<default_bind_filter>, C<default_fetch_filter>
931

            
932
=item *
933

            
934
C<filter> argument of C<insert()>, C<update()>,
935
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
936
methods
937

            
938
=item *
939

            
940
C<execute()> method
941

            
942
=item *
943

            
944
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
945

            
946
=item *
947

            
948
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
949

            
950
=back
951

            
952
B<Example:>
953

            
954
    $dbi->register_filter(
955
        encode_utf8 => sub {
956
            my $value = shift;
957
            
958
            require Encode;
959
            
960
            return Encode::encode('UTF-8', $value);
961
        },
962
        decode_utf8 => sub {
963
            my $value = shift;
964
            
965
            require Encode;
966
            
967
            return Encode::decode('UTF-8', $value)
968
        }
969
    );
970

            
971
=head2 rollback
972

            
973
    $dbi->rollback;
974

            
975
Rollback transaction.
976
This is same as L<DBI>'s C<rollback>.
977

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
978
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
979
    
cleanup
yuki-kimoto authored on 2010-08-05
980
    my $result = $dbi->select(table    => $table,
981
                              column   => [@column],
982
                              where    => \%where,
983
                              append   => $append,
984
                              relation => \%relation,
985
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
986

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
987
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
988
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
989
C<relation> and C<filter> arguments.
990
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
991
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
992
C<append> is a string added at the end of the SQL statement.
993
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
994

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

            
added commit method
yuki-kimoto authored on 2010-05-27
997
    # select * from books;
cleanup
yuki-kimoto authored on 2010-08-05
998
    my $result = $dbi->select(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
999
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1000
    # select * from books where title = ?;
1001
    my $result = $dbi->select(table => 'books', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1002
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1003
    # select title, author from books where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1004
    my $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
1005
        table  => 'books',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1006
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1007
        where  => {id => 1},
1008
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1009
    );
1010
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1011
    # select books.name as book_name from books, rental
added commit method
yuki-kimoto authored on 2010-05-27
1012
    # where books.id = rental.book_id;
1013
    my $result = $dbi->select(
removed reconnect method
yuki-kimoto authored on 2010-05-28
1014
        table    => ['books', 'rental'],
1015
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
1016
        relation => {'books.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1017
    );
1018

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

            
1022
    my $result = $dbi->select(
1023
        table  => 'books',
1024
        column => ['title', 'author'],
1025
        where  => ['{= title} or {like author}',
1026
                   {title => '%Perl%', author => 'Ken'}]
1027
    );
1028

            
1029
First element is a string. it contains tags,
1030
such as "{= title} or {like author}".
1031
Second element is paramters.
1032

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1035
    $dbi->update(table  => $table, 
1036
                 param  => \%params,
1037
                 where  => \%where,
1038
                 append => $append,
1039
                 filter => \%filter)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1040

            
cleanup
yuki-kimoto authored on 2010-10-17
1041
Execute update statement.
1042
C<update> method have C<table>, C<param>, C<where>, C<append>
1043
and C<filter> arguments.
1044
C<table> is a table name.
1045
C<param> is column-value pairs. this must be hash reference.
1046
C<where> is where clause. this must be hash reference.
1047
C<append> is a string added at the end of the SQL statement.
1048
C<filter> is filters when parameter binding is executed.
1049
This is overwrites C<default_bind_filter>.
1050
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1051

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1054
    $dbi->update(table  => 'books',
1055
                 param  => {title => 'Perl', author => 'Taro'},
1056
                 where  => {id => 5},
1057
                 append => "some statement",
1058
                 filter => {title => 'encode_utf8'});
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1059

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1062
    $dbi->update_all(table  => $table, 
1063
                     param  => \%params,
1064
                     filter => \%filter,
1065
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1066

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

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

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

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

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

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

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

            
1086
C<< <kimoto.yuki at gmail.com> >>
1087

            
1088
L<http://github.com/yuki-kimoto/DBIx-Custom>
1089

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1090
=head1 AUTHOR
1091

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

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

            
1096
Copyright 2009 Yuki Kimoto, all rights reserved.
1097

            
1098
This program is free software; you can redistribute it and/or modify it
1099
under the same terms as Perl itself.
1100

            
1101
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1102

            
1103