DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1424 lines | 36.321kb
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;
fix select method empty wher...
Yuki Kimoto authored on 2011-01-17
510
    my $wexists;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
511
    if (ref $where eq 'HASH' && keys %$where) {
update document
yuki-kimoto authored on 2010-08-07
512
        $param = $where;
fix select method empty wher...
Yuki Kimoto authored on 2011-01-17
513
        $wexists = keys %$where;
514
        
515
        if ($wexists) {
516
            $source .= 'where (';
517
            foreach my $where_key (keys %$where) {
518
                $source .= "{= $where_key} and ";
519
            }
520
            $source =~ s/ and $//;
521
            $source .= ') ';
packaging one directory
yuki-kimoto authored on 2009-11-16
522
        }
update document
yuki-kimoto authored on 2010-08-07
523
    }
524
    elsif (ref $where eq 'ARRAY') {
fix select method empty wher...
Yuki Kimoto authored on 2011-01-17
525
        my $w = $where->[0] || '';
update document
yuki-kimoto authored on 2010-08-07
526
        $param = $where->[1];
527
        
fix select method empty wher...
Yuki Kimoto authored on 2011-01-17
528
        if (ref $w eq 'HASH') {
529
            
530
        }
531
        else {
532
            $wexists = $w =~ /\S/;
533
            $source .= "where ($w) " if $wexists;
534
        }
packaging one directory
yuki-kimoto authored on 2009-11-16
535
    }
536
    
added commit method
yuki-kimoto authored on 2010-05-27
537
    # Relation
538
    if ($relation) {
fix select method empty wher...
Yuki Kimoto authored on 2011-01-17
539
        $source .= $wexists ? "and " : "where ";
added commit method
yuki-kimoto authored on 2010-05-27
540
        foreach my $rkey (keys %$relation) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
541
            $source .= "$rkey = " . $relation->{$rkey} . " and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
542
        }
543
    }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
544
    $source =~ s/ and $//;
added commit method
yuki-kimoto authored on 2010-05-27
545
    
546
    # Append some statement
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
547
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
548
    
549
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
550
    my $result = $self->execute(
551
        $source, param  => $param, filter => $filter,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
552
        table => $tables);    
packaging one directory
yuki-kimoto authored on 2009-11-16
553
    
554
    return $result;
555
}
556

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
557
sub table {
558
    my $self = shift;
559
    my $name = shift;
560
    
561
    # Table class
562
    my $table_class = $self->table_class;
563
    croak qq{Invalid table class name "$table_class"}
564
      unless $table_class =~ /^[\w:]+$/;
565
    unless ($table_class->can('isa')) {
566
        eval "require $table_class";
567
        croak $@ if $@;
568
    }
569
    # Create table
570
    $self->{_tables} ||= {};
571
    $self->{_tables}->{$name}
572
        = $table_class->new(name => $name, dbi => $self)
573
      unless defined $self->{_tables}->{$name};
574
    
575
    # Helper
576
    $self->{_tables}->{$name}->helper(@_) if @_;
577
    
578
    return $self->{_tables}{$name};
579
}
580

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
581
sub txn_scope {
582
    my $self = shift;
583
    
584
    require DBIx::TransactionManager;
585
    
586
    $self->{_transaction_manager}
587
      ||= DBIx::TransactionManager->new($self->dbh);
588
    
589
    return $self->{_transaction_manager}->txn_scope;
590
}
591

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-01-12
665
sub _build_binds {
666
    my ($self, $params, $columns, $filter) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
667
    
cleanup
Yuki Kimoto authored on 2011-01-12
668
    # bind values
669
    my @binds;
add tests
yuki-kimoto authored on 2010-08-08
670
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
671
    # Build bind values
672
    my $count = {};
cleanup
Yuki Kimoto authored on 2011-01-12
673
    foreach my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
674
        
675
        # Value
676
        my $value = ref $params->{$column} eq 'ARRAY'
677
                  ? $params->{$column}->[$count->{$column} || 0]
678
                  : $params->{$column};
679
        
cleanup
Yuki Kimoto authored on 2011-01-12
680
        # Filter
681
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
682
        
cleanup
Yuki Kimoto authored on 2011-01-12
683
        push @binds, $f ? $f->($value) : $value;
removed reconnect method
yuki-kimoto authored on 2010-05-28
684
        
685
        # Count up 
686
        $count->{$column}++;
687
    }
688
    
cleanup
Yuki Kimoto authored on 2011-01-12
689
    return \@binds;
removed reconnect method
yuki-kimoto authored on 2010-05-28
690
}
691

            
cleanup
yuki-kimoto authored on 2010-10-17
692
sub _croak {
693
    my ($self, $error, $append) = @_;
694
    $append ||= "";
695
    
696
    # Verbose
697
    if ($Carp::Verbose) { croak $error }
698
    
699
    # Not verbose
700
    else {
701
        
702
        # Remove line and module infromation
703
        my $at_pos = rindex($error, ' at ');
704
        $error = substr($error, 0, $at_pos);
705
        $error =~ s/\s+$//;
706
        
707
        croak "$error$append";
708
    }
709
}
710

            
cleanup
Yuki Kimoto authored on 2011-01-12
711
# Deprecated
712
__PACKAGE__->attr(cache_method => sub {
713
    sub {
714
        my $self = shift;
715
        
716
        $self->{_cached} ||= {};
717
        
718
        if (@_ > 1) {
719
            $self->{_cached}{$_[0]} = $_[1] 
720
        }
721
        else {
722
            return $self->{_cached}{$_[0]}
723
        }
724
    }
725
});
726

            
727
sub default_bind_filter {
728
    my $self = shift;
729
    
730
    if (@_) {
731
        my $fname = $_[0];
732
        
733
        if (@_ && !$fname) {
734
            $self->{default_out_filter} = undef;
735
        }
736
        else {
737
            croak qq{"$fname" is not registered}
738
              unless exists $self->filters->{$fname};
739
        
740
            $self->{default_out_filter} = $self->filters->{$fname};
741
        }
742
        return $self;
743
    }
744
    
745
    return $self->{default_out_filter};
746
}
747

            
748
sub default_fetch_filter {
749
    my $self = shift;
750
    my $fname = $_[0];
751
    
752
    if (@_) {
753
        if (@_ && !$fname) {
754
            $self->{default_in_filter} = undef;
755
        }
756
        else {
757
            croak qq{"$fname" is not registered}
758
              unless exists $self->filters->{$fname};
759
        
760
            $self->{default_in_filter} = $self->filters->{$fname};
761
        }
762
        
763
        return $self;
764
    }
765
    
766
    return $self->{default_in_filter}
767
}
768

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
771
=head1 NAME
772

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

            
775
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
776

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
785
    # Insert 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
786
    $dbi->insert(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
787
                 param  => {title => 'Perl', author => 'Ken'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
788
                 filter => {title => 'encode_utf8'});
789
    
790
    # Update 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
791
    $dbi->update(table  => 'book', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
792
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
793
                 where  => {id => 5},
794
                 filter => {title => 'encode_utf8'});
795
    
796
    # Update all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
797
    $dbi->update_all(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
798
                     param  => {title => 'Perl'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
799
                     filter => {title => 'encode_utf8'});
800
    
801
    # Delete
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
802
    $dbi->delete(table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
803
                 where  => {author => 'Ken'},
804
                 filter => {title => 'encode_utf8'});
805
    
806
    # Delete all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
807
    $dbi->delete_all(table => 'book');
cleanup
yuki-kimoto authored on 2010-08-05
808

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

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

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
839
    # Execute SQL
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
840
    $dbi->execute("select title from book");
removed register_format()
yuki-kimoto authored on 2010-05-26
841
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
842
    # Execute SQL with hash binding and filtering
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
843
    $dbi->execute("select id from book where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
844
                  param  => {author => 'ken', title => '%Perl%'},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
845
                  filter => {title => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
846

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

            
updated document
yuki-kimoto authored on 2010-08-08
853
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
854

            
855
    # Get DBI object
856
    my $dbh = $dbi->dbh;
857

            
858
Fetch row.
859

            
removed register_format()
yuki-kimoto authored on 2010-05-26
860
    # Fetch
861
    while (my $row = $result->fetch) {
862
        # ...
863
    }
864
    
865
    # Fetch hash
866
    while (my $row = $result->fetch_hash) {
867
        
868
    }
869
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
870
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
871

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
added dbi_options attribute
kimoto authored on 2010-12-20
919
=head2 C<dbi_options>
920

            
921
    my $dbi_options = $dbi->dbi_options;
922
    $dbi            = $dbi->dbi_options($dbi_options);
923

            
924
DBI options.
925
C<connect()> method use this value to connect the database.
926

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
934
Filter functions.
935
"encode_utf8" and "decode_utf8" is registered by default.
936

            
937
=head2 C<filter_check>
938

            
939
    my $filter_check = $dbi->filter_check;
940
    $dbi             = $dbi->filter_check(0);
941

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

            
945
=head2 C<password>
946

            
947
    my $password = $dbi->password;
948
    $dbi         = $dbi->password('lkj&le`@s');
949

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
972
    my $user = $dbi->user;
973
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
974

            
cleanup
yuki-kimoto authored on 2010-10-17
975
User name.
976
C<connect()> method use this value to connect the database.
977
    
978
=head1 METHODS
added commit method
yuki-kimoto authored on 2010-05-27
979

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

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

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
985
    $dbi->apply_filter(
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
986
        $table,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
987
        $column1 => {in => $infilter1, out => $outfilter1}
988
        $column2 => {in => $infilter2, out => $outfilter2}
989
        ...,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
990
    );
991

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

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

            
cleanup
Yuki Kimoto authored on 2010-12-21
999
    $result = $dbi->execute(
1000
        "select * from table1 where {= key1} and {= key2};",
1001
         param => {key1 => 1, key2 => 2},
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1002
         table => ['table1']
cleanup
Yuki Kimoto authored on 2010-12-21
1003
    );
1004
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1005
=head2 C<begin_work>
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1006

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

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

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

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

            
1017
    $dbi->commit;
1018

            
1019
Commit transaction.
1020
This is same as L<DBI>'s C<commit>.
1021

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1057
    my $result = $dbi->execute(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1058
        "select * from book where {= author} and {like title}", 
cleanup
yuki-kimoto authored on 2010-10-17
1059
        param => {author => 'Ken', title => '%Perl%'}
1060
    );
1061
    
1062
    while (my $row = $result->fetch) {
1063
        my $author = $row->[0];
1064
        my $title  = $row->[1];
1065
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
1066

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

            
1069
    my %expand = $dbi->expand($source);
1070

            
1071
The following hash
1072

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

            
1075
is expanded to
1076

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

            
1079
This is used in C<select()>
1080

            
1081

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1085
    $dbi->delete(table  => $table,
1086
                 where  => \%where,
1087
                 append => $append,
1088
                 filter => \%filter);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1089

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1090
Execute delete statement.
1091
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
1092
C<table> is a table name.
1093
C<where> is where clause. this must be hash reference.
1094
C<append> is a string added at the end of the SQL statement.
1095
C<filter> is filters when parameter binding is executed.
cleanup
yuki-kimoto authored on 2010-08-09
1096
Return value of C<delete()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1097

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

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

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

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

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

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

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

            
1120
    $dbi->helper(
1121
        update_or_insert => sub {
1122
            my $self = shift;
1123
            # do something
1124
        },
1125
        find_or_create   => sub {
1126
            my $self = shift;
1127
            # do something
1128
        }
1129
    );
1130

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

            
1133
    $dbi->update_or_insert;
1134
    $dbi->find_or_create;
1135

            
cleanup
yuki-kimoto authored on 2010-10-17
1136
=head2 C<insert>
1137

            
1138
    $dbi->insert(table  => $table, 
1139
                 param  => \%param,
1140
                 append => $append,
1141
                 filter => \%filter);
1142

            
1143
Execute insert statement.
1144
C<insert> method have C<table>, C<param>, C<append>
1145
and C<filter> arguments.
1146
C<table> is a table name.
1147
C<param> is the pairs of column name value. this must be hash reference.
1148
C<append> is a string added at the end of the SQL statement.
1149
C<filter> is filters when parameter binding is executed.
1150
This is overwrites C<default_bind_filter>.
1151
Return value of C<insert()> is the count of affected rows.
1152

            
1153
B<Example:>
1154

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

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

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

            
1165
Create a new L<DBIx::Custom> object.
1166

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

            
1169
    $dbi->iterate_all_columns(
1170
        sub {
1171
            my ($table, $column, $column_info) = @_;
1172
            
1173
            # do something;
1174
        }
1175
    );
1176

            
1177
Iterate all columns of all tables. Argument is callback.
1178
You can do anything by callback.
1179

            
cleanup
yuki-kimoto authored on 2010-10-17
1180
=head2 C<register_filter>
1181

            
1182
    $dbi->register_filter(%filters);
1183
    $dbi->register_filter(\%filters);
1184
    
1185
Register filter. Registered filters is available in the following attributes
1186
or arguments.
1187

            
1188
=over 4
1189

            
1190
=item *
1191

            
1192
C<filter> argument of C<insert()>, C<update()>,
1193
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1194
methods
1195

            
1196
=item *
1197

            
1198
C<execute()> method
1199

            
1200
=item *
1201

            
1202
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1203

            
1204
=item *
1205

            
1206
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1207

            
1208
=back
1209

            
1210
B<Example:>
1211

            
1212
    $dbi->register_filter(
1213
        encode_utf8 => sub {
1214
            my $value = shift;
1215
            
1216
            require Encode;
1217
            
1218
            return Encode::encode('UTF-8', $value);
1219
        },
1220
        decode_utf8 => sub {
1221
            my $value = shift;
1222
            
1223
            require Encode;
1224
            
1225
            return Encode::decode('UTF-8', $value)
1226
        }
1227
    );
1228

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

            
1231
    $dbi->rollback;
1232

            
1233
Rollback transaction.
1234
This is same as L<DBI>'s C<rollback>.
1235

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1236
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1237
    
cleanup
yuki-kimoto authored on 2010-08-05
1238
    my $result = $dbi->select(table    => $table,
1239
                              column   => [@column],
1240
                              where    => \%where,
1241
                              append   => $append,
1242
                              relation => \%relation,
1243
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1244

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1245
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1246
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1247
C<relation> and C<filter> arguments.
1248
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1249
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1250
C<append> is a string added at the end of the SQL statement.
1251
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
1252

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

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1255
    # select * from book;
1256
    my $result = $dbi->select(table => 'book');
packaging one directory
yuki-kimoto authored on 2009-11-16
1257
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1258
    # select * from book where title = ?;
1259
    my $result = $dbi->select(table => 'book', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1260
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1261
    # select title, author from book where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1262
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1263
        table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1264
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1265
        where  => {id => 1},
1266
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1267
    );
1268
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1269
    # select book.name as book_name from book, rental
1270
    # where book.id = rental.book_id;
added commit method
yuki-kimoto authored on 2010-05-27
1271
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1272
        table    => ['book', 'rental'],
1273
        column   => ['book.name as book_name']
1274
        relation => {'book.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1275
    );
1276

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

            
1280
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1281
        table  => 'book',
cleanup
yuki-kimoto authored on 2010-08-09
1282
        column => ['title', 'author'],
1283
        where  => ['{= title} or {like author}',
1284
                   {title => '%Perl%', author => 'Ken'}]
1285
    );
1286

            
1287
First element is a string. it contains tags,
1288
such as "{= title} or {like author}".
1289
Second element is paramters.
1290

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1293
    $dbi->update(table  => $table, 
1294
                 param  => \%params,
1295
                 where  => \%where,
1296
                 append => $append,
1297
                 filter => \%filter)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1298

            
cleanup
yuki-kimoto authored on 2010-10-17
1299
Execute update statement.
1300
C<update> method have C<table>, C<param>, C<where>, C<append>
1301
and C<filter> arguments.
1302
C<table> is a table name.
1303
C<param> is column-value pairs. this must be hash reference.
1304
C<where> is where clause. this must be hash reference.
1305
C<append> is a string added at the end of the SQL statement.
1306
C<filter> is filters when parameter binding is executed.
1307
This is overwrites C<default_bind_filter>.
1308
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1309

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

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

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

            
1320
    {
1321
        my $txn = $dbi->txn_scope;
1322
        $dbi->insert(table => 'book', param => {title => 'Perl'});
1323
        $dbi->insert(table => 'book', param => {title => 'Good days'});
1324
        $txn->commit;
1325
    }
1326

            
1327
Create transaction scope. If you escape scope(that is { .. }) and commited,
1328
Rollback is automatically done.
1329

            
1330
Note that this is feature of L<DBIx::TransactionManager>
1331
L<DBIx::TransactionManager> is required.
1332

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

            
1335
    $dbi->table('book',
1336
        insert => sub { ... },
1337
        update => sub { ... }
1338
    );
1339
    
1340
    my $table = $dbi->table('book');
1341

            
1342
Create a L<DBIx::Custom::Table> object,
1343
or get a L<DBIx::Custom::Table> object.
1344

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1347
    $dbi->update_all(table  => $table, 
1348
                     param  => \%params,
1349
                     filter => \%filter,
1350
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1351

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

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

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

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

            
1365
    my $default_bind_filter = $dbi->default_bind_filter;
1366
    $dbi                    = $dbi->default_bind_filter($fname);
1367

            
1368
Default filter when parameter binding is executed.
1369

            
1370
=head2 C<(deprecated) default_fetch_filter>
1371

            
1372
    my $default_fetch_filter = $dbi->default_fetch_filter;
1373
    $dbi = $dbi->default_fetch_filter($fname);
1374

            
1375
=head2 C<(deprecated) cache_method>
1376

            
1377
    $dbi          = $dbi->cache_method(\&cache_method);
1378
    $cache_method = $dbi->cache_method
1379

            
1380
Method to set and get caches.
1381

            
1382
B<Example:>
1383

            
1384
    $dbi->cache_method(
1385
        sub {
1386
            my $self = shift;
1387
            
1388
            $self->{_cached} ||= {};
1389
            
1390
            if (@_ > 1) {
1391
                $self->{_cached}{$_[0]} = $_[1] 
1392
            }
1393
            else {
1394
                return $self->{_cached}{$_[0]}
1395
            }
1396
        }
1397
    );
1398

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

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

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

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

            
1407
C<< <kimoto.yuki at gmail.com> >>
1408

            
1409
L<http://github.com/yuki-kimoto/DBIx-Custom>
1410

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1411
=head1 AUTHOR
1412

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

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

            
1417
Copyright 2009 Yuki Kimoto, all rights reserved.
1418

            
1419
This program is free software; you can redistribute it and/or modify it
1420
under the same terms as Perl itself.
1421

            
1422
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1423

            
1424