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

            
cleanup (removed undocumente...
yuki-kimoto authored on 2010-11-10
3
our $VERSION = '0.1621';
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 (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

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

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

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

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

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

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

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

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

            
added helper method
yuki-kimoto authored on 2010-10-17
211
sub DESTROY { }
212

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

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

            
252
        return $result;
253
    }
254
    return $affected;
255
}
256

            
added experimental expand me...
yuki-kimoto authored on 2010-10-20
257
sub expand {
258
    my $self = shift;
259
    my $source = ref $_[0] eq 'HASH' ? $_[0] : {@_};
260
    my $table = (keys %$source)[0];
261
    my $param = $source->{$table};
262
    
263
    # Expand table name
264
    my $expand = {};
265
    foreach my $column (keys %$param) {
266
        $expand->{"$table.$column"} = $param->{$column};
267
    }
268
    
269
    return %$expand;
270
}
271

            
cleanup
yuki-kimoto authored on 2010-10-17
272
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append filter/;
273

            
274
sub insert {
275
    my ($self, %args) = @_;
276

            
277
    # Check arguments
278
    foreach my $name (keys %args) {
279
        croak qq{"$name" is invalid argument}
280
          unless $VALID_INSERT_ARGS{$name};
packaging one directory
yuki-kimoto authored on 2009-11-16
281
    }
282
    
cleanup
yuki-kimoto authored on 2010-10-17
283
    # Arguments
284
    my $table  = $args{table} || '';
285
    my $param  = $args{param} || {};
286
    my $append = $args{append} || '';
287
    my $filter = $args{filter};
288
    
289
    # Insert keys
290
    my @insert_keys = keys %$param;
291
    
292
    # Templte for insert
293
    my $source = "insert into $table {insert_param "
294
               . join(' ', @insert_keys) . '}';
add tests
yuki-kimoto authored on 2010-08-10
295
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
296
    
297
    # Execute query
cleanup
yuki-kimoto authored on 2010-10-17
298
    my $ret_val = $self->execute($source, param  => $param, 
299
                                          filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
300
    
301
    return $ret_val;
302
}
303

            
cleanup
yuki-kimoto authored on 2010-10-17
304
sub register_filter {
305
    my $invocant = shift;
306
    
307
    # Register filter
308
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
309
    $invocant->filters({%{$invocant->filters}, %$filters});
310
    
311
    return $invocant;
312
}
packaging one directory
yuki-kimoto authored on 2009-11-16
313

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

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

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

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
464
sub _build_bind_values {
465
    my ($self, $query, $params, $filter) = @_;
466
    
467
    # binding values
468
    my @bind_values;
add tests
yuki-kimoto authored on 2010-08-08
469

            
470
    # Filter
471
    $filter ||= {};
472
    
473
    # Parameter
474
    $params ||= {};
475
    
476
    # Check filter
477
    $self->_check_filter($self->filters, $filter,
478
                         $self->default_bind_filter, $params)
479
      if $self->filter_check;
removed reconnect method
yuki-kimoto authored on 2010-05-28
480
    
481
    # Build bind values
482
    my $count = {};
483
    foreach my $column (@{$query->columns}) {
484
        
485
        # Value
486
        my $value = ref $params->{$column} eq 'ARRAY'
487
                  ? $params->{$column}->[$count->{$column} || 0]
488
                  : $params->{$column};
489
        
add tests
yuki-kimoto authored on 2010-08-10
490
        # Filtering
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
491
        my $fname = $filter->{$column} || $self->default_bind_filter || '';
add tests
yuki-kimoto authored on 2010-08-10
492
        my $filter_func = $fname ? $self->filters->{$fname} : undef;
removed reconnect method
yuki-kimoto authored on 2010-05-28
493
        push @bind_values, $filter_func
494
                         ? $filter_func->($value)
495
                         : $value;
496
        
497
        # Count up 
498
        $count->{$column}++;
499
    }
500
    
501
    return \@bind_values;
502
}
503

            
add tests
yuki-kimoto authored on 2010-08-08
504
sub _check_filter {
505
    my ($self, $filters, $filter, $default_filter, $params) = @_;
506
    
507
    # Filter name not exists
508
    foreach my $fname (values %$filter) {
509
        croak qq{Bind filter "$fname" is not registered}
510
          unless exists $filters->{$fname};
511
    }
512
    
513
    # Default filter name not exists
514
    croak qq{Default bind filter "$default_filter" is not registered}
515
      if $default_filter && ! exists $filters->{$default_filter};
516
    
517
    # Column name not exists
518
    foreach my $column (keys %$filter) {
519
        
520
        croak qq{Column name "$column" in bind filter is not found in paramters}
521
          unless exists $params->{$column};
522
    }
523
}
524

            
cleanup
yuki-kimoto authored on 2010-10-17
525
sub _croak {
526
    my ($self, $error, $append) = @_;
527
    $append ||= "";
528
    
529
    # Verbose
530
    if ($Carp::Verbose) { croak $error }
531
    
532
    # Not verbose
533
    else {
534
        
535
        # Remove line and module infromation
536
        my $at_pos = rindex($error, ' at ');
537
        $error = substr($error, 0, $at_pos);
538
        $error =~ s/\s+$//;
539
        
540
        croak "$error$append";
541
    }
542
}
543

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
546
=head1 NAME
547

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

            
550
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
551

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
560
    # Insert 
561
    $dbi->insert(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
562
                 param  => {title => 'Perl', author => 'Ken'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
563
                 filter => {title => 'encode_utf8'});
564
    
565
    # Update 
566
    $dbi->update(table  => 'books', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
567
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
568
                 where  => {id => 5},
569
                 filter => {title => 'encode_utf8'});
570
    
571
    # Update all
572
    $dbi->update_all(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
573
                     param  => {title => 'Perl'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
574
                     filter => {title => 'encode_utf8'});
575
    
576
    # Delete
577
    $dbi->delete(table  => 'books',
578
                 where  => {author => 'Ken'},
579
                 filter => {title => 'encode_utf8'});
580
    
581
    # Delete all
582
    $dbi->delete_all(table => 'books');
cleanup
yuki-kimoto authored on 2010-08-05
583

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
586
    # Select
587
    my $result = $dbi->select(table => 'books');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
588
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
589
    # Select, more complex
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
590
    my $result = $dbi->select(
update document
yuki-kimoto authored on 2010-05-27
591
        table  => 'books',
592
        column => [qw/author title/],
593
        where  => {author => 'Ken'},
updated document
yuki-kimoto authored on 2010-08-08
594
        append => 'order by id limit 5',
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
595
        filter => {title => 'encode_utf8'}
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
596
    );
added commit method
yuki-kimoto authored on 2010-05-27
597
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
598
    # Select, join table
added commit method
yuki-kimoto authored on 2010-05-27
599
    my $result = $dbi->select(
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
600
        table    => ['books', 'rental'],
601
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
602
        relation => {'books.id' => 'rental.book_id'}
603
    );
updated document
yuki-kimoto authored on 2010-08-08
604
    
605
    # Select, more flexible where
606
    my $result = $dbi->select(
607
        table  => 'books',
608
        where  => ['{= author} and {like title}', 
609
                   {author => 'Ken', title => '%Perl%'}]
610
    );
cleanup
yuki-kimoto authored on 2010-08-05
611

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
614
    # Execute SQL
removed register_format()
yuki-kimoto authored on 2010-05-26
615
    $dbi->execute("select title from books");
616
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
617
    # Execute SQL with hash binding and filtering
updated document
yuki-kimoto authored on 2010-08-08
618
    $dbi->execute("select id from books where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
619
                  param  => {author => 'ken', title => '%Perl%'},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
620
                  filter => {title => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
621

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

            
updated document
yuki-kimoto authored on 2010-08-08
628
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
629

            
removed register_format()
yuki-kimoto authored on 2010-05-26
630
    # Default filter
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
631
    $dbi->default_bind_filter('encode_utf8');
removed register_format()
yuki-kimoto authored on 2010-05-26
632
    $dbi->default_fetch_filter('decode_utf8');
cleanup
yuki-kimoto authored on 2010-08-05
633

            
634
    # Get DBI object
635
    my $dbh = $dbi->dbh;
636

            
637
Fetch row.
638

            
removed register_format()
yuki-kimoto authored on 2010-05-26
639
    # Fetch
640
    while (my $row = $result->fetch) {
641
        # ...
642
    }
643
    
644
    # Fetch hash
645
    while (my $row = $result->fetch_hash) {
646
        
647
    }
648
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
649
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
650

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
685
    $dbi          = $dbi->cache_method(\&cache_method);
686
    $cache_method = $dbi->cache_method
687

            
688
Method to set and get caches.
689

            
690
B<Example:>
691

            
692
    $dbi->cache_method(
693
        sub {
694
            my $self = shift;
695
            
696
            $self->{_cached} ||= {};
697
            
698
            if (@_ > 1) {
699
                $self->{_cached}{$_[0]} = $_[1] 
700
            }
701
            else {
702
                return $self->{_cached}{$_[0]}
703
            }
704
        }
705
    );
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
706

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
741
Filter functions.
742
"encode_utf8" and "decode_utf8" is registered by default.
743

            
744
=head2 C<filter_check>
745

            
746
    my $filter_check = $dbi->filter_check;
747
    $dbi             = $dbi->filter_check(0);
748

            
749
Enable filter check. 
750
Default to 1.
751
This check maybe damege performance.
752
If you require performance, set C<filter_check> attribute to 0.
753

            
754
=head2 C<password>
755

            
756
    my $password = $dbi->password;
757
    $dbi         = $dbi->password('lkj&le`@s');
758

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
781
    my $user = $dbi->user;
782
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
783

            
cleanup
yuki-kimoto authored on 2010-10-17
784
User name.
785
C<connect()> method use this value to connect the database.
786
    
787
=head1 METHODS
added commit method
yuki-kimoto authored on 2010-05-27
788

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
802
=head2 commit
803

            
804
    $dbi->commit;
805

            
806
Commit transaction.
807
This is same as L<DBI>'s C<commit>.
808

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
819
=head2 C<create_query>
820
    
821
    my $query = $dbi->create_query(
822
        "select * from books where {= author} and {like title};"
823
    );
update document
yuki-kimoto authored on 2009-11-19
824

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
844
    my $result = $dbi->execute(
845
        "select * from books where {= author} and {like title}", 
846
        param => {author => 'Ken', title => '%Perl%'}
847
    );
848
    
849
    while (my $row = $result->fetch) {
850
        my $author = $row->[0];
851
        my $title  = $row->[1];
852
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
853

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

            
856
    my %expand = $dbi->expand($source);
857

            
858
The following hash
859

            
860
    {books => {title => 'Perl', author => 'Ken'}}
861

            
862
is expanded to
863

            
864
    ('books.title' => 'Perl', 'books.author' => 'Ken')
865

            
866
This is used in C<select()>
867

            
868

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

            
cleanup
yuki-kimoto authored on 2010-08-05
872
    $dbi->delete(table  => $table,
873
                 where  => \%where,
874
                 append => $append,
875
                 filter => \%filter);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
876

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
877
Execute delete statement.
878
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
879
C<table> is a table name.
880
C<where> is where clause. this must be hash reference.
881
C<append> is a string added at the end of the SQL statement.
882
C<filter> is filters when parameter binding is executed.
cleanup
yuki-kimoto authored on 2010-08-09
883
Return value of C<delete()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
884

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

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

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

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

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

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

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

            
907
    $dbi->helper(
908
        update_or_insert => sub {
909
            my $self = shift;
910
            # do something
911
        },
912
        find_or_create   => sub {
913
            my $self = shift;
914
            # do something
915
        }
916
    );
917

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

            
920
    $dbi->update_or_insert;
921
    $dbi->find_or_create;
922

            
cleanup
yuki-kimoto authored on 2010-10-17
923
=head2 C<insert>
924

            
925
    $dbi->insert(table  => $table, 
926
                 param  => \%param,
927
                 append => $append,
928
                 filter => \%filter);
929

            
930
Execute insert statement.
931
C<insert> method have C<table>, C<param>, C<append>
932
and C<filter> arguments.
933
C<table> is a table name.
934
C<param> is the pairs of column name value. this must be hash reference.
935
C<append> is a string added at the end of the SQL statement.
936
C<filter> is filters when parameter binding is executed.
937
This is overwrites C<default_bind_filter>.
938
Return value of C<insert()> is the count of affected rows.
939

            
940
B<Example:>
941

            
942
    $dbi->insert(table  => 'books', 
943
                 param  => {title => 'Perl', author => 'Taro'},
944
                 append => "some statement",
945
                 filter => {title => 'encode_utf8'})
946

            
947
=head2 C<register_filter>
948

            
949
    $dbi->register_filter(%filters);
950
    $dbi->register_filter(\%filters);
951
    
952
Register filter. Registered filters is available in the following attributes
953
or arguments.
954

            
955
=over 4
956

            
957
=item *
958

            
959
C<default_bind_filter>, C<default_fetch_filter>
960

            
961
=item *
962

            
963
C<filter> argument of C<insert()>, C<update()>,
964
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
965
methods
966

            
967
=item *
968

            
969
C<execute()> method
970

            
971
=item *
972

            
973
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
974

            
975
=item *
976

            
977
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
978

            
979
=back
980

            
981
B<Example:>
982

            
983
    $dbi->register_filter(
984
        encode_utf8 => sub {
985
            my $value = shift;
986
            
987
            require Encode;
988
            
989
            return Encode::encode('UTF-8', $value);
990
        },
991
        decode_utf8 => sub {
992
            my $value = shift;
993
            
994
            require Encode;
995
            
996
            return Encode::decode('UTF-8', $value)
997
        }
998
    );
999

            
1000
=head2 rollback
1001

            
1002
    $dbi->rollback;
1003

            
1004
Rollback transaction.
1005
This is same as L<DBI>'s C<rollback>.
1006

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1007
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1008
    
cleanup
yuki-kimoto authored on 2010-08-05
1009
    my $result = $dbi->select(table    => $table,
1010
                              column   => [@column],
1011
                              where    => \%where,
1012
                              append   => $append,
1013
                              relation => \%relation,
1014
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1015

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1016
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1017
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1018
C<relation> and C<filter> arguments.
1019
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1020
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1021
C<append> is a string added at the end of the SQL statement.
1022
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
1023

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

            
added commit method
yuki-kimoto authored on 2010-05-27
1026
    # select * from books;
cleanup
yuki-kimoto authored on 2010-08-05
1027
    my $result = $dbi->select(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
1028
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1029
    # select * from books where title = ?;
1030
    my $result = $dbi->select(table => 'books', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1031
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1032
    # select title, author from books where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1033
    my $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
1034
        table  => 'books',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1035
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1036
        where  => {id => 1},
1037
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1038
    );
1039
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1040
    # select books.name as book_name from books, rental
added commit method
yuki-kimoto authored on 2010-05-27
1041
    # where books.id = rental.book_id;
1042
    my $result = $dbi->select(
removed reconnect method
yuki-kimoto authored on 2010-05-28
1043
        table    => ['books', 'rental'],
1044
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
1045
        relation => {'books.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1046
    );
1047

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

            
1051
    my $result = $dbi->select(
1052
        table  => 'books',
1053
        column => ['title', 'author'],
1054
        where  => ['{= title} or {like author}',
1055
                   {title => '%Perl%', author => 'Ken'}]
1056
    );
1057

            
1058
First element is a string. it contains tags,
1059
such as "{= title} or {like author}".
1060
Second element is paramters.
1061

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1064
    $dbi->update(table  => $table, 
1065
                 param  => \%params,
1066
                 where  => \%where,
1067
                 append => $append,
1068
                 filter => \%filter)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1069

            
cleanup
yuki-kimoto authored on 2010-10-17
1070
Execute update statement.
1071
C<update> method have C<table>, C<param>, C<where>, C<append>
1072
and C<filter> arguments.
1073
C<table> is a table name.
1074
C<param> is column-value pairs. this must be hash reference.
1075
C<where> is where clause. this must be hash reference.
1076
C<append> is a string added at the end of the SQL statement.
1077
C<filter> is filters when parameter binding is executed.
1078
This is overwrites C<default_bind_filter>.
1079
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1080

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1083
    $dbi->update(table  => 'books',
1084
                 param  => {title => 'Perl', author => 'Taro'},
1085
                 where  => {id => 5},
1086
                 append => "some statement",
1087
                 filter => {title => 'encode_utf8'});
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1088

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1091
    $dbi->update_all(table  => $table, 
1092
                     param  => \%params,
1093
                     filter => \%filter,
1094
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1095

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

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

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

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

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

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

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

            
1115
C<< <kimoto.yuki at gmail.com> >>
1116

            
1117
L<http://github.com/yuki-kimoto/DBIx-Custom>
1118

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1119
=head1 AUTHOR
1120

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

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

            
1125
Copyright 2009 Yuki Kimoto, all rights reserved.
1126

            
1127
This program is free software; you can redistribute it and/or modify it
1128
under the same terms as Perl itself.
1129

            
1130
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1131

            
1132