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

            
cleanup
Yuki Kimoto authored on 2011-01-12
3
our $VERSION = '0.1629';
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
4

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

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

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

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

            
add cache attribute
yuki-kimoto authored on 2010-06-14
22
__PACKAGE__->attr(cache => 1);
cleanup (removed undocumente...
yuki-kimoto authored on 2010-11-10
23
__PACKAGE__->attr(filters => sub {
24
    {
25
        encode_utf8 => sub { encode_utf8($_[0]) },
26
        decode_utf8 => sub { decode_utf8($_[0]) }
27
    }
28
});
added check_filter attribute
yuki-kimoto authored on 2010-08-08
29
__PACKAGE__->attr(filter_check => 1);
cleanup
yuki-kimoto authored on 2010-10-17
30
__PACKAGE__->attr(query_builder  => sub {DBIx::Custom::QueryBuilder->new});
31
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result');
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
32
__PACKAGE__->attr(table_class => 'DBIx::Custom::Table');
cleanup
yuki-kimoto authored on 2010-10-17
33

            
34
# DBI methods
35
foreach my $method (qw/begin_work commit rollback/) {
36
    my $code = sub {
37
        my $self = shift;
38
        my $ret = eval {$self->dbh->$method};
39
        croak $@ if $@;
40
        return $ret;
41
    };
42
    no strict 'refs';
43
    my $pkg = __PACKAGE__;
44
    *{"${pkg}::$method"} = $code;
45
};
46

            
added helper method
yuki-kimoto authored on 2010-10-17
47
our $AUTOLOAD;
48

            
49
sub AUTOLOAD {
50
    my $self = shift;
51

            
52
    # Method
53
    my ($package, $method) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
54

            
55
    # Helper
56
    $self->{_helpers} ||= {};
57
    croak qq/Can't locate object method "$method" via "$package"/
58
      unless my $helper = $self->{_helpers}->{$method};
59

            
60
    # Run
61
    return $self->$helper(@_);
62
}
63

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
64
sub apply_filter {
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
65
    my $self = shift;
66
    
cleanup
Yuki Kimoto authored on 2011-01-12
67
    $self->{filter} ||= {};
cleanup
Yuki Kimoto authored on 2010-12-22
68
    
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
69
    # Table and column informations
70
    my ($table, @cs) = @_;
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
71
    
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
72
    if (@cs) {
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
73
        
cleanup
Yuki Kimoto authored on 2010-12-22
74
        # Initialize filters
cleanup
Yuki Kimoto authored on 2011-01-12
75
        $self->{filter}{out} ||= {};
76
        $self->{filter}{in} ||= {};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
77
        
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
78
        # Create filters
79
        for (my $i = 0; $i < @cs; $i += 2) {
cleanup
Yuki Kimoto authored on 2010-12-22
80
            
81
            # Column
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
82
            my $column = $cs[$i];
83
            
84
            # Filter
85
            my $filter = $cs[$i + 1] || {};
86
            my $in_filter = delete $filter->{in};
87
            my $out_filter = delete $filter->{out};
88
            croak "Usage \$dbi->apply_filter(" .
89
                  "TABLE, COLUMN, {in => INFILTER, out => OUTFILTER}, ...)"
90
              if ref $filter ne 'HASH' || keys %$filter;
cleanup
Yuki Kimoto authored on 2010-12-22
91
            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
92
            # Out filter
cleanup
Yuki Kimoto authored on 2011-01-12
93
            if (ref $out_filter eq 'CODE') {
cleanup
Yuki Kimoto authored on 2011-01-12
94
                $self->{filter}{out}{$table}{$column}
95
                  = $out_filter;
96
                $self->{filter}{out}{$table}{"$table.$column"}
97
                  = $out_filter;
cleanup
Yuki Kimoto authored on 2010-12-22
98
            }
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
99
            elsif (defined $out_filter) {
cleanup
Yuki Kimoto authored on 2011-01-12
100
                croak qq{"$out_filter" is not registered}
101
                  unless exists $self->filters->{$out_filter};
102
                
103
                $self->{filter}{out}{$table}{$column}
104
                  = $self->filters->{$out_filter};
105
                $self->{filter}{out}{$table}{"$table.$column"}
106
                  = $self->filters->{$out_filter};
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
107
            }
cleanup
Yuki Kimoto authored on 2010-12-22
108
            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
109
            # In filter
cleanup
Yuki Kimoto authored on 2011-01-12
110
            if (ref $in_filter eq 'CODE') {
cleanup
Yuki Kimoto authored on 2011-01-12
111
                $self->{filter}{in}{$table}{$column}
112
                  = $in_filter;
113
                $self->{filter}{in}{$table}{"$table.$column"}
114
                  = $in_filter;
cleanup
Yuki Kimoto authored on 2010-12-22
115
            }
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
116
            elsif (defined $in_filter) {
cleanup
Yuki Kimoto authored on 2011-01-12
117
                croak qq{"$in_filter" is not registered}
118
                  unless exists $self->filters->{$in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
119
                $self->{filter}{in}{$table}{$column}
cleanup
Yuki Kimoto authored on 2011-01-12
120
                  = $self->filters->{$in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
121
                $self->{filter}{in}{$table}{"$table.$column"}
cleanup
Yuki Kimoto authored on 2011-01-12
122
                  = $self->filters->{$in_filter};
cleanup
Yuki Kimoto authored on 2010-12-22
123
            }
cleanup
Yuki Kimoto authored on 2010-12-21
124
        }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
125
        
cleanup
Yuki Kimoto authored on 2010-12-22
126
        return $self;
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
127
    }
128
    
cleanup
Yuki Kimoto authored on 2011-01-12
129
    return $self->{filter};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
130
}
131

            
added helper method
yuki-kimoto authored on 2010-10-17
132
sub helper {
133
    my $self = shift;
134
    
135
    # Merge
136
    my $helpers = ref $_[0] eq 'HASH' ? $_[0] : {@_};
137
    $self->{_helpers} = {%{$self->{_helpers} || {}}, %$helpers};
138
    
139
    return $self;
140
}
141

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

            
cleanup
yuki-kimoto authored on 2010-10-17
198
sub create_query {
199
    my ($self, $source) = @_;
update document
yuki-kimoto authored on 2010-01-30
200
    
cleanup
yuki-kimoto authored on 2010-10-17
201
    # Cache
202
    my $cache = $self->cache;
update document
yuki-kimoto authored on 2010-01-30
203
    
cleanup
yuki-kimoto authored on 2010-10-17
204
    # Create query
205
    my $query;
206
    if ($cache) {
207
        
208
        # Get query
209
        my $q = $self->cache_method->($self, $source);
210
        
211
        # Create query
212
        $query = DBIx::Custom::Query->new($q) if $q;
213
    }
214
    
215
    unless ($query) {
cleanup insert
yuki-kimoto authored on 2010-04-28
216

            
cleanup
yuki-kimoto authored on 2010-10-17
217
        # Create SQL object
218
        my $builder = $self->query_builder;
219
        
220
        # Create query
221
        $query = $builder->build_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
222

            
cleanup
yuki-kimoto authored on 2010-10-17
223
        # Cache query
224
        $self->cache_method->($self, $source,
225
                             {sql     => $query->sql, 
226
                              columns => $query->columns})
227
          if $cache;
cleanup insert
yuki-kimoto authored on 2010-04-28
228
    }
229
    
cleanup
yuki-kimoto authored on 2010-10-17
230
    # Prepare statement handle
231
    my $sth;
232
    eval { $sth = $self->dbh->prepare($query->{sql})};
233
    $self->_croak($@, qq{. SQL: "$query->{sql}"}) if $@;
packaging one directory
yuki-kimoto authored on 2009-11-16
234
    
cleanup
yuki-kimoto authored on 2010-10-17
235
    # Set statement handle
236
    $query->sth($sth);
packaging one directory
yuki-kimoto authored on 2009-11-16
237
    
cleanup
yuki-kimoto authored on 2010-10-17
238
    return $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
239
}
240

            
cleanup
yuki-kimoto authored on 2010-10-17
241
our %VALID_DELETE_ARGS
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
242
  = map { $_ => 1 } qw/table where append filter allow_delete_all/;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
243

            
cleanup
yuki-kimoto authored on 2010-10-17
244
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
245
    my ($self, %args) = @_;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
246
    
247
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
248
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
249
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
250
          unless $VALID_DELETE_ARGS{$name};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
251
    }
252
    
253
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
254
    my $table            = $args{table} || '';
255
    my $where            = $args{where} || {};
cleanup
yuki-kimoto authored on 2010-10-17
256
    my $append = $args{append};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
257
    my $filter           = $args{filter};
cleanup
yuki-kimoto authored on 2010-10-17
258
    my $allow_delete_all = $args{allow_delete_all};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
259

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
294
sub execute{
295
    my ($self, $query, %args)  = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
296
    
297
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
298
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
299
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
300
          unless $VALID_EXECUTE_ARGS{$name};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
301
    }
302
    
cleanup
yuki-kimoto authored on 2010-10-17
303
    my $params = $args{param} || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
304
    
cleanup
yuki-kimoto authored on 2010-10-17
305
    # First argument is the soruce of SQL
306
    $query = $self->create_query($query)
307
      unless ref $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
308
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
309
    # Auto filter
cleanup
Yuki Kimoto authored on 2011-01-12
310
    my $filter = {};
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
311
    my $tables = $args{table} || [];
312
    $tables = [$tables]
313
      unless ref $tables eq 'ARRAY';
314
    foreach my $table (@$tables) {
cleanup
Yuki Kimoto authored on 2011-01-12
315
        $filter = {
316
            %$filter,
317
            %{$self->{filter}{out}->{$table} || {}}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
318
        }
319
    }
320
    
cleanup
Yuki Kimoto authored on 2011-01-12
321
    # Filter argument
322
    my $f = $args{filter} || $query->filter || {};
323
    foreach my $column (keys %$f) {
324
        my $fname = $f->{$column};
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
325
        if (!defined $fname) {
cleanup
Yuki Kimoto authored on 2011-01-12
326
            $f->{$column} = undef;
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
327
        }
328
        elsif (ref $fname ne 'CODE') {
cleanup
Yuki Kimoto authored on 2010-12-21
329
          croak qq{"$fname" is not registered"}
330
            unless exists $self->filters->{$fname};
331
          
cleanup
Yuki Kimoto authored on 2011-01-12
332
          $f->{$column} = $self->filters->{$fname};
cleanup
Yuki Kimoto authored on 2010-12-21
333
        }
334
    }
cleanup
Yuki Kimoto authored on 2011-01-12
335
    $filter = {%$filter, %$f};
packaging one directory
yuki-kimoto authored on 2009-11-16
336
    
cleanup
Yuki Kimoto authored on 2011-01-12
337
    # Create bind values
338
    my $binds = $self->_build_binds($params, $query->columns, $filter);
cleanup
yuki-kimoto authored on 2010-10-17
339
    
340
    # Execute
341
    my $sth      = $query->sth;
342
    my $affected;
cleanup
Yuki Kimoto authored on 2011-01-12
343
    eval {$affected = $sth->execute(@$binds)};
cleanup
yuki-kimoto authored on 2010-10-17
344
    $self->_croak($@) if $@;
345
    
346
    # Return resultset if select statement is executed
347
    if ($sth->{NUM_OF_FIELDS}) {
348
        
cleanup
Yuki Kimoto authored on 2011-01-12
349
        # Auto in filter
cleanup
Yuki Kimoto authored on 2011-01-12
350
        my $in_filter = {};
351
        foreach my $table (@$tables) {
352
            $in_filter = {
353
                %$in_filter,
354
                %{$self->{filter}{in}{$table} || {}}
355
            }
356
        }
357
        
358
        # Result
359
        my $result = $self->result_class->new(
cleanup
Yuki Kimoto authored on 2010-12-22
360
            sth            => $sth,
361
            filters        => $self->filters,
362
            filter_check   => $self->filter_check,
cleanup
Yuki Kimoto authored on 2011-01-12
363
            default_filter => $self->{default_in_filter},
cleanup
Yuki Kimoto authored on 2011-01-12
364
            filter         => $in_filter || {}
cleanup
yuki-kimoto authored on 2010-10-17
365
        );
366

            
367
        return $result;
368
    }
369
    return $affected;
370
}
371

            
added experimental expand me...
yuki-kimoto authored on 2010-10-20
372
sub expand {
373
    my $self = shift;
374
    my $source = ref $_[0] eq 'HASH' ? $_[0] : {@_};
375
    my $table = (keys %$source)[0];
376
    my $param = $source->{$table};
377
    
378
    # Expand table name
379
    my $expand = {};
380
    foreach my $column (keys %$param) {
381
        $expand->{"$table.$column"} = $param->{$column};
382
    }
383
    
384
    return %$expand;
385
}
386

            
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
387
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
388
                                            filter/;
cleanup
yuki-kimoto authored on 2010-10-17
389
sub insert {
390
    my ($self, %args) = @_;
391

            
392
    # Check arguments
393
    foreach my $name (keys %args) {
394
        croak qq{"$name" is invalid argument}
395
          unless $VALID_INSERT_ARGS{$name};
packaging one directory
yuki-kimoto authored on 2009-11-16
396
    }
397
    
cleanup
yuki-kimoto authored on 2010-10-17
398
    # Arguments
399
    my $table  = $args{table} || '';
400
    my $param  = $args{param} || {};
401
    my $append = $args{append} || '';
402
    my $filter = $args{filter};
403
    
404
    # Insert keys
405
    my @insert_keys = keys %$param;
406
    
407
    # Templte for insert
408
    my $source = "insert into $table {insert_param "
409
               . join(' ', @insert_keys) . '}';
add tests
yuki-kimoto authored on 2010-08-10
410
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
411
    
412
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
413
    my $ret_val = $self->execute(
414
        $source,
415
        param  => $param,
416
        filter => $filter,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
417
        table => $table
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
418
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
419
    
420
    return $ret_val;
421
}
422

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
846
Fetch row.
847

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
925
=head2 C<filter_check>
926

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

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

            
933
=head2 C<password>
934

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1005
    $dbi->commit;
1006

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1059
The following hash
1060

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

            
1063
is expanded to
1064

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

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

            
1069

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1141
B<Example:>
1142

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

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

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

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

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

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

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

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

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

            
1176
=over 4
1177

            
1178
=item *
1179

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

            
1184
=item *
1185

            
1186
C<execute()> method
1187

            
1188
=item *
1189

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

            
1192
=item *
1193

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

            
1196
=back
1197

            
1198
B<Example:>
1199

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

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

            
1219
    $dbi->rollback;
1220

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1356
Default filter when parameter binding is executed.
1357

            
1358
=head2 C<(deprecated) default_fetch_filter>
1359

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

            
1363
=head2 C<(deprecated) cache_method>
1364

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

            
1368
Method to set and get caches.
1369

            
1370
B<Example:>
1371

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

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

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

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

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

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

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

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

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

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

            
1405
Copyright 2009 Yuki Kimoto, all rights reserved.
1406

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

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

            
1412