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

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
3
our $VERSION = '0.1631';
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

            
fix tests
Yuki Kimoto authored on 2011-01-13
18
__PACKAGE__->attr(
19
    [qw/data_source dbh dbi_options password user/],
20

            
21
    cache => 1,
22
    filters => sub {
23
        {
24
            encode_utf8 => sub { encode_utf8($_[0]) },
25
            decode_utf8 => sub { decode_utf8($_[0]) }
26
        }
27
    },
28
    
29
    filter_check  => 1,
30
    query_builder => sub {DBIx::Custom::QueryBuilder->new},
31
    result_class  => 'DBIx::Custom::Result',
32
    table_class   => 'DBIx::Custom::Table'
33
);
cleanup
yuki-kimoto authored on 2010-10-17
34

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
847
Fetch row.
848

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
926
=head2 C<filter_check>
927

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

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

            
934
=head2 C<password>
935

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1006
    $dbi->commit;
1007

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1060
The following hash
1061

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

            
1064
is expanded to
1065

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

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

            
1070

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1142
B<Example:>
1143

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

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

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

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

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

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

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

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

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

            
1177
=over 4
1178

            
1179
=item *
1180

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

            
1185
=item *
1186

            
1187
C<execute()> method
1188

            
1189
=item *
1190

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

            
1193
=item *
1194

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

            
1197
=back
1198

            
1199
B<Example:>
1200

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

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

            
1220
    $dbi->rollback;
1221

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1357
Default filter when parameter binding is executed.
1358

            
1359
=head2 C<(deprecated) default_fetch_filter>
1360

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

            
1364
=head2 C<(deprecated) cache_method>
1365

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

            
1369
Method to set and get caches.
1370

            
1371
B<Example:>
1372

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

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

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

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

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

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

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

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

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

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

            
1406
Copyright 2009 Yuki Kimoto, all rights reserved.
1407

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

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

            
1413