DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1452 lines | 37.408kb
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
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
243
  = map { $_ => 1 } qw/table where append filter allow_delete_all query/;
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
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
281
    # Create query
282
    my $query = $self->create_query($source);
283
    return $query if $args{query};
284
    
packaging one directory
yuki-kimoto authored on 2009-11-16
285
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
286
    my $ret_val = $self->execute(
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
287
        $query, param  => $where, filter => $filter,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
288
        table => $table);
packaging one directory
yuki-kimoto authored on 2009-11-16
289
    
290
    return $ret_val;
291
}
292

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

            
added helper method
yuki-kimoto authored on 2010-10-17
295
sub DESTROY { }
296

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

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

            
372
        return $result;
373
    }
374
    return $affected;
375
}
376

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

            
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
392
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
393
                                            filter query/;
cleanup
yuki-kimoto authored on 2010-10-17
394
sub insert {
395
    my ($self, %args) = @_;
396

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

            
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
432
sub iterate_all_columns {
433
    my ($self, $cb) = @_;
434
    
435
    # Iterate all tables
436
    my $sth_tables = $self->dbh->table_info;
437
    while (my $table_info = $sth_tables->fetchrow_hashref) {
438
        
439
        # Table
440
        my $table = $table_info->{TABLE_NAME};
441
        
442
        # Iterate all columns
443
        my $sth_columns = $self->dbh->column_info(undef, undef, $table, '%');
444
        while (my $column_info = $sth_columns->fetchrow_hashref) {
445
            my $column = $column_info->{COLUMN_NAME};
446
            $cb->($table, $column, $column_info);
447
        }
448
    }
449
}
450

            
added dbi_options attribute
kimoto authored on 2010-12-20
451
sub new {
452
    my $self = shift->SUPER::new(@_);
453
    
454
    # Check attribute names
455
    my @attrs = keys %$self;
456
    foreach my $attr (@attrs) {
457
        croak qq{"$attr" is invalid attribute name}
458
          unless $self->can($attr);
459
    }
460
    
461
    return $self;
462
}
463

            
cleanup
yuki-kimoto authored on 2010-10-17
464
sub register_filter {
465
    my $invocant = shift;
466
    
467
    # Register filter
468
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
469
    $invocant->filters({%{$invocant->filters}, %$filters});
470
    
471
    return $invocant;
472
}
packaging one directory
yuki-kimoto authored on 2009-11-16
473

            
refactoring select
yuki-kimoto authored on 2010-04-28
474
our %VALID_SELECT_ARGS
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
475
  = map { $_ => 1 } qw/table column where append relation filter query/;
refactoring select
yuki-kimoto authored on 2010-04-28
476

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

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
569
sub table {
570
    my $self = shift;
571
    my $name = shift;
572
    
573
    # Table class
574
    my $table_class = $self->table_class;
575
    croak qq{Invalid table class name "$table_class"}
576
      unless $table_class =~ /^[\w:]+$/;
577
    unless ($table_class->can('isa')) {
578
        eval "require $table_class";
579
        croak $@ if $@;
580
    }
581
    # Create table
582
    $self->{_tables} ||= {};
583
    $self->{_tables}->{$name}
584
        = $table_class->new(name => $name, dbi => $self)
585
      unless defined $self->{_tables}->{$name};
586
    
587
    # Helper
588
    $self->{_tables}->{$name}->helper(@_) if @_;
589
    
590
    return $self->{_tables}{$name};
591
}
592

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
593
sub txn_scope {
594
    my $self = shift;
595
    
596
    require DBIx::TransactionManager;
597
    
598
    $self->{_transaction_manager}
599
      ||= DBIx::TransactionManager->new($self->dbh);
600
    
601
    return $self->{_transaction_manager}->txn_scope;
602
}
603

            
cleanup
yuki-kimoto authored on 2010-10-17
604
our %VALID_UPDATE_ARGS
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
605
  = map { $_ => 1 } qw/table param
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
606
                       where append filter allow_update_all query/;
cleanup
yuki-kimoto authored on 2010-10-17
607

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

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

            
cleanup
Yuki Kimoto authored on 2011-01-12
681
sub _build_binds {
682
    my ($self, $params, $columns, $filter) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
683
    
cleanup
Yuki Kimoto authored on 2011-01-12
684
    # bind values
685
    my @binds;
add tests
yuki-kimoto authored on 2010-08-08
686
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
687
    # Build bind values
688
    my $count = {};
cleanup
Yuki Kimoto authored on 2011-01-12
689
    foreach my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
690
        
691
        # Value
692
        my $value = ref $params->{$column} eq 'ARRAY'
693
                  ? $params->{$column}->[$count->{$column} || 0]
694
                  : $params->{$column};
695
        
cleanup
Yuki Kimoto authored on 2011-01-12
696
        # Filter
697
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
698
        
cleanup
Yuki Kimoto authored on 2011-01-12
699
        push @binds, $f ? $f->($value) : $value;
removed reconnect method
yuki-kimoto authored on 2010-05-28
700
        
701
        # Count up 
702
        $count->{$column}++;
703
    }
704
    
cleanup
Yuki Kimoto authored on 2011-01-12
705
    return \@binds;
removed reconnect method
yuki-kimoto authored on 2010-05-28
706
}
707

            
cleanup
yuki-kimoto authored on 2010-10-17
708
sub _croak {
709
    my ($self, $error, $append) = @_;
710
    $append ||= "";
711
    
712
    # Verbose
713
    if ($Carp::Verbose) { croak $error }
714
    
715
    # Not verbose
716
    else {
717
        
718
        # Remove line and module infromation
719
        my $at_pos = rindex($error, ' at ');
720
        $error = substr($error, 0, $at_pos);
721
        $error =~ s/\s+$//;
722
        
723
        croak "$error$append";
724
    }
725
}
726

            
cleanup
Yuki Kimoto authored on 2011-01-12
727
# Deprecated
728
__PACKAGE__->attr(cache_method => sub {
729
    sub {
730
        my $self = shift;
731
        
732
        $self->{_cached} ||= {};
733
        
734
        if (@_ > 1) {
735
            $self->{_cached}{$_[0]} = $_[1] 
736
        }
737
        else {
738
            return $self->{_cached}{$_[0]}
739
        }
740
    }
741
});
742

            
743
sub default_bind_filter {
744
    my $self = shift;
745
    
746
    if (@_) {
747
        my $fname = $_[0];
748
        
749
        if (@_ && !$fname) {
750
            $self->{default_out_filter} = undef;
751
        }
752
        else {
753
            croak qq{"$fname" is not registered}
754
              unless exists $self->filters->{$fname};
755
        
756
            $self->{default_out_filter} = $self->filters->{$fname};
757
        }
758
        return $self;
759
    }
760
    
761
    return $self->{default_out_filter};
762
}
763

            
764
sub default_fetch_filter {
765
    my $self = shift;
766
    my $fname = $_[0];
767
    
768
    if (@_) {
769
        if (@_ && !$fname) {
770
            $self->{default_in_filter} = undef;
771
        }
772
        else {
773
            croak qq{"$fname" is not registered}
774
              unless exists $self->filters->{$fname};
775
        
776
            $self->{default_in_filter} = $self->filters->{$fname};
777
        }
778
        
779
        return $self;
780
    }
781
    
782
    return $self->{default_in_filter}
783
}
784

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
787
=head1 NAME
788

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

            
791
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
792

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
801
    # Insert 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
802
    $dbi->insert(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
803
                 param  => {title => 'Perl', author => 'Ken'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
804
                 filter => {title => 'encode_utf8'});
805
    
806
    # Update 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
807
    $dbi->update(table  => 'book', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
808
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
809
                 where  => {id => 5},
810
                 filter => {title => 'encode_utf8'});
811
    
812
    # Update all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
813
    $dbi->update_all(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
814
                     param  => {title => 'Perl'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
815
                     filter => {title => 'encode_utf8'});
816
    
817
    # Delete
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
818
    $dbi->delete(table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
819
                 where  => {author => 'Ken'},
820
                 filter => {title => 'encode_utf8'});
821
    
822
    # Delete all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
823
    $dbi->delete_all(table => 'book');
cleanup
yuki-kimoto authored on 2010-08-05
824

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
827
    # Select
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
828
    my $result = $dbi->select(table => 'book');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
829
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
830
    # Select, more complex
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
831
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
832
        table  => 'book',
update document
yuki-kimoto authored on 2010-05-27
833
        column => [qw/author title/],
834
        where  => {author => 'Ken'},
updated document
yuki-kimoto authored on 2010-08-08
835
        append => 'order by id limit 5',
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
836
        filter => {title => 'encode_utf8'}
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
837
    );
added commit method
yuki-kimoto authored on 2010-05-27
838
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
839
    # Select, join table
added commit method
yuki-kimoto authored on 2010-05-27
840
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
841
        table    => ['book', 'rental'],
842
        column   => ['book.name as book_name']
843
        relation => {'book.id' => 'rental.book_id'}
added commit method
yuki-kimoto authored on 2010-05-27
844
    );
updated document
yuki-kimoto authored on 2010-08-08
845
    
846
    # Select, more flexible where
847
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
848
        table  => 'book',
updated document
yuki-kimoto authored on 2010-08-08
849
        where  => ['{= author} and {like title}', 
850
                   {author => 'Ken', title => '%Perl%'}]
851
    );
cleanup
yuki-kimoto authored on 2010-08-05
852

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
855
    # Execute SQL
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
856
    $dbi->execute("select title from book");
removed register_format()
yuki-kimoto authored on 2010-05-26
857
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
858
    # Execute SQL with hash binding and filtering
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
859
    $dbi->execute("select id from book where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
860
                  param  => {author => 'ken', title => '%Perl%'},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
861
                  filter => {title => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
862

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

            
updated document
yuki-kimoto authored on 2010-08-08
869
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
870

            
871
    # Get DBI object
872
    my $dbh = $dbi->dbh;
873

            
874
Fetch row.
875

            
removed register_format()
yuki-kimoto authored on 2010-05-26
876
    # Fetch
877
    while (my $row = $result->fetch) {
878
        # ...
879
    }
880
    
881
    # Fetch hash
882
    while (my $row = $result->fetch_hash) {
883
        
884
    }
885
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
886
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
887

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
added dbi_options attribute
kimoto authored on 2010-12-20
935
=head2 C<dbi_options>
936

            
937
    my $dbi_options = $dbi->dbi_options;
938
    $dbi            = $dbi->dbi_options($dbi_options);
939

            
940
DBI options.
941
C<connect()> method use this value to connect the database.
942

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
950
Filter functions.
951
"encode_utf8" and "decode_utf8" is registered by default.
952

            
953
=head2 C<filter_check>
954

            
955
    my $filter_check = $dbi->filter_check;
956
    $dbi             = $dbi->filter_check(0);
957

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

            
961
=head2 C<password>
962

            
963
    my $password = $dbi->password;
964
    $dbi         = $dbi->password('lkj&le`@s');
965

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
988
    my $user = $dbi->user;
989
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
990

            
cleanup
yuki-kimoto authored on 2010-10-17
991
User name.
992
C<connect()> method use this value to connect the database.
993
    
994
=head1 METHODS
added commit method
yuki-kimoto authored on 2010-05-27
995

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

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

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1001
    $dbi->apply_filter(
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1002
        $table,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1003
        $column1 => {in => $infilter1, out => $outfilter1}
1004
        $column2 => {in => $infilter2, out => $outfilter2}
1005
        ...,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1006
    );
1007

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

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

            
cleanup
Yuki Kimoto authored on 2010-12-21
1015
    $result = $dbi->execute(
1016
        "select * from table1 where {= key1} and {= key2};",
1017
         param => {key1 => 1, key2 => 2},
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1018
         table => ['table1']
cleanup
Yuki Kimoto authored on 2010-12-21
1019
    );
1020
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1021
=head2 C<begin_work>
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1022

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

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

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

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

            
1033
    $dbi->commit;
1034

            
1035
Commit transaction.
1036
This is same as L<DBI>'s C<commit>.
1037

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1073
    my $result = $dbi->execute(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1074
        "select * from book where {= author} and {like title}", 
cleanup
yuki-kimoto authored on 2010-10-17
1075
        param => {author => 'Ken', title => '%Perl%'}
1076
    );
1077
    
1078
    while (my $row = $result->fetch) {
1079
        my $author = $row->[0];
1080
        my $title  = $row->[1];
1081
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
1082

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

            
1085
    my %expand = $dbi->expand($source);
1086

            
1087
The following hash
1088

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

            
1091
is expanded to
1092

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

            
1095
This is used in C<select()>
1096

            
1097

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1101
    $dbi->delete(table  => $table,
1102
                 where  => \%where,
1103
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1104
                 filter => \%filter,
1105
                 query  => 1);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1106

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1107
Execute delete statement.
1108
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
1109
C<table> is a table name.
1110
C<where> is where clause. this must be hash reference.
1111
C<append> is a string added at the end of the SQL statement.
1112
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1113
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1114
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-08-09
1115
Return value of C<delete()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1116

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

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

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

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

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

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

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

            
1139
    $dbi->helper(
1140
        update_or_insert => sub {
1141
            my $self = shift;
1142
            # do something
1143
        },
1144
        find_or_create   => sub {
1145
            my $self = shift;
1146
            # do something
1147
        }
1148
    );
1149

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

            
1152
    $dbi->update_or_insert;
1153
    $dbi->find_or_create;
1154

            
cleanup
yuki-kimoto authored on 2010-10-17
1155
=head2 C<insert>
1156

            
1157
    $dbi->insert(table  => $table, 
1158
                 param  => \%param,
1159
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1160
                 filter => \%filter,
1161
                 query  => 1);
cleanup
yuki-kimoto authored on 2010-10-17
1162

            
1163
Execute insert statement.
1164
C<insert> method have C<table>, C<param>, C<append>
1165
and C<filter> arguments.
1166
C<table> is a table name.
1167
C<param> is the pairs of column name value. this must be hash reference.
1168
C<append> is a string added at the end of the SQL statement.
1169
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1170
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1171
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1172
This is overwrites C<default_bind_filter>.
1173
Return value of C<insert()> is the count of affected rows.
1174

            
1175
B<Example:>
1176

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

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

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

            
1187
Create a new L<DBIx::Custom> object.
1188

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

            
1191
    $dbi->iterate_all_columns(
1192
        sub {
1193
            my ($table, $column, $column_info) = @_;
1194
            
1195
            # do something;
1196
        }
1197
    );
1198

            
1199
Iterate all columns of all tables. Argument is callback.
1200
You can do anything by callback.
1201

            
cleanup
yuki-kimoto authored on 2010-10-17
1202
=head2 C<register_filter>
1203

            
1204
    $dbi->register_filter(%filters);
1205
    $dbi->register_filter(\%filters);
1206
    
1207
Register filter. Registered filters is available in the following attributes
1208
or arguments.
1209

            
1210
=over 4
1211

            
1212
=item *
1213

            
1214
C<filter> argument of C<insert()>, C<update()>,
1215
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1216
methods
1217

            
1218
=item *
1219

            
1220
C<execute()> method
1221

            
1222
=item *
1223

            
1224
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1225

            
1226
=item *
1227

            
1228
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1229

            
1230
=back
1231

            
1232
B<Example:>
1233

            
1234
    $dbi->register_filter(
1235
        encode_utf8 => sub {
1236
            my $value = shift;
1237
            
1238
            require Encode;
1239
            
1240
            return Encode::encode('UTF-8', $value);
1241
        },
1242
        decode_utf8 => sub {
1243
            my $value = shift;
1244
            
1245
            require Encode;
1246
            
1247
            return Encode::decode('UTF-8', $value)
1248
        }
1249
    );
1250

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

            
1253
    $dbi->rollback;
1254

            
1255
Rollback transaction.
1256
This is same as L<DBI>'s C<rollback>.
1257

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1258
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1259
    
cleanup
yuki-kimoto authored on 2010-08-05
1260
    my $result = $dbi->select(table    => $table,
1261
                              column   => [@column],
1262
                              where    => \%where,
1263
                              append   => $append,
1264
                              relation => \%relation,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1265
                              filter   => \%filter,
1266
                              query    => 1);
update document
yuki-kimoto authored on 2009-11-19
1267

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1268
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1269
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1270
C<relation> and C<filter> arguments.
1271
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1272
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1273
C<append> is a string added at the end of the SQL statement.
1274
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1275
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1276
default to 0. This is experimental.
update document
yuki-kimoto authored on 2009-11-19
1277

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

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1280
    # select * from book;
1281
    my $result = $dbi->select(table => 'book');
packaging one directory
yuki-kimoto authored on 2009-11-16
1282
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1283
    # select * from book where title = ?;
1284
    my $result = $dbi->select(table => 'book', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1285
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1286
    # select title, author from book where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1287
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1288
        table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1289
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1290
        where  => {id => 1},
1291
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1292
    );
1293
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1294
    # select book.name as book_name from book, rental
1295
    # where book.id = rental.book_id;
added commit method
yuki-kimoto authored on 2010-05-27
1296
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1297
        table    => ['book', 'rental'],
1298
        column   => ['book.name as book_name']
1299
        relation => {'book.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1300
    );
1301

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

            
1305
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1306
        table  => 'book',
cleanup
yuki-kimoto authored on 2010-08-09
1307
        column => ['title', 'author'],
1308
        where  => ['{= title} or {like author}',
1309
                   {title => '%Perl%', author => 'Ken'}]
1310
    );
1311

            
1312
First element is a string. it contains tags,
1313
such as "{= title} or {like author}".
1314
Second element is paramters.
1315

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1318
    $dbi->update(table  => $table, 
1319
                 param  => \%params,
1320
                 where  => \%where,
1321
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1322
                 filter => \%filter,
1323
                 query  => 1)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1324

            
cleanup
yuki-kimoto authored on 2010-10-17
1325
Execute update statement.
1326
C<update> method have C<table>, C<param>, C<where>, C<append>
1327
and C<filter> arguments.
1328
C<table> is a table name.
1329
C<param> is column-value pairs. this must be hash reference.
1330
C<where> is where clause. this must be hash reference.
1331
C<append> is a string added at the end of the SQL statement.
1332
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1333
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1334
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1335
This is overwrites C<default_bind_filter>.
1336
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1337

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

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

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

            
1348
    {
1349
        my $txn = $dbi->txn_scope;
1350
        $dbi->insert(table => 'book', param => {title => 'Perl'});
1351
        $dbi->insert(table => 'book', param => {title => 'Good days'});
1352
        $txn->commit;
1353
    }
1354

            
1355
Create transaction scope. If you escape scope(that is { .. }) and commited,
1356
Rollback is automatically done.
1357

            
1358
Note that this is feature of L<DBIx::TransactionManager>
1359
L<DBIx::TransactionManager> is required.
1360

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

            
1363
    $dbi->table('book',
1364
        insert => sub { ... },
1365
        update => sub { ... }
1366
    );
1367
    
1368
    my $table = $dbi->table('book');
1369

            
1370
Create a L<DBIx::Custom::Table> object,
1371
or get a L<DBIx::Custom::Table> object.
1372

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1375
    $dbi->update_all(table  => $table, 
1376
                     param  => \%params,
1377
                     filter => \%filter,
1378
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1379

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

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

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

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

            
1393
    my $default_bind_filter = $dbi->default_bind_filter;
1394
    $dbi                    = $dbi->default_bind_filter($fname);
1395

            
1396
Default filter when parameter binding is executed.
1397

            
1398
=head2 C<(deprecated) default_fetch_filter>
1399

            
1400
    my $default_fetch_filter = $dbi->default_fetch_filter;
1401
    $dbi = $dbi->default_fetch_filter($fname);
1402

            
1403
=head2 C<(deprecated) cache_method>
1404

            
1405
    $dbi          = $dbi->cache_method(\&cache_method);
1406
    $cache_method = $dbi->cache_method
1407

            
1408
Method to set and get caches.
1409

            
1410
B<Example:>
1411

            
1412
    $dbi->cache_method(
1413
        sub {
1414
            my $self = shift;
1415
            
1416
            $self->{_cached} ||= {};
1417
            
1418
            if (@_ > 1) {
1419
                $self->{_cached}{$_[0]} = $_[1] 
1420
            }
1421
            else {
1422
                return $self->{_cached}{$_[0]}
1423
            }
1424
        }
1425
    );
1426

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

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

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

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

            
1435
C<< <kimoto.yuki at gmail.com> >>
1436

            
1437
L<http://github.com/yuki-kimoto/DBIx-Custom>
1438

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1439
=head1 AUTHOR
1440

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

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

            
1445
Copyright 2009 Yuki Kimoto, all rights reserved.
1446

            
1447
This program is free software; you can redistribute it and/or modify it
1448
under the same terms as Perl itself.
1449

            
1450
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1451

            
1452