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

            
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
3
our $VERSION = '0.1634';
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
4

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

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

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

            
fix tests
Yuki Kimoto authored on 2011-01-13
19
__PACKAGE__->attr(
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
20
    [qw/data_source dbh password user/],
21
    dbi_options => sub { {} },
fix tests
Yuki Kimoto authored on 2011-01-13
22
    cache => 1,
23
    filters => sub {
24
        {
25
            encode_utf8 => sub { encode_utf8($_[0]) },
26
            decode_utf8 => sub { decode_utf8($_[0]) }
27
        }
28
    },
29
    
30
    filter_check  => 1,
31
    query_builder => sub {DBIx::Custom::QueryBuilder->new},
32
    result_class  => 'DBIx::Custom::Result',
33
    table_class   => 'DBIx::Custom::Table'
34
);
cleanup
yuki-kimoto authored on 2010-10-17
35

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

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

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
243
our %VALID_DELETE_ARGS
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
244
  = map { $_ => 1 } qw/table where append filter allow_delete_all query/;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
245

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

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

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

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

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

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

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

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

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

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

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

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

            
experimental extended select...
Yuki Kimoto authored on 2011-01-17
465
sub or {
466
    my $self = shift;
467
    my $values = ref $_[0] eq 'ARRAY' ? $_[0] : [@_];
468
    return DBIx::Custom::Or->new(values => $values);
469
}
470

            
cleanup
yuki-kimoto authored on 2010-10-17
471
sub register_filter {
472
    my $invocant = shift;
473
    
474
    # Register filter
475
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
476
    $invocant->filters({%{$invocant->filters}, %$filters});
477
    
478
    return $invocant;
479
}
packaging one directory
yuki-kimoto authored on 2009-11-16
480

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

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

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

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
601
sub txn_scope {
602
    my $self = shift;
603
    
604
    require DBIx::TransactionManager;
605
    
606
    $self->{_transaction_manager}
607
      ||= DBIx::TransactionManager->new($self->dbh);
608
    
609
    return $self->{_transaction_manager}->txn_scope;
610
}
611

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

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

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

            
added test
Yuki Kimoto authored on 2011-01-19
689
sub where { DBIx::Custom::Where->new(
690
              query_builder => shift->query_builder) }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
691

            
cleanup
Yuki Kimoto authored on 2011-01-12
692
sub _build_binds {
693
    my ($self, $params, $columns, $filter) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
694
    
cleanup
Yuki Kimoto authored on 2011-01-12
695
    # bind values
696
    my @binds;
add tests
yuki-kimoto authored on 2010-08-08
697
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
698
    # Build bind values
699
    my $count = {};
cleanup
Yuki Kimoto authored on 2011-01-12
700
    foreach my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
701
        
702
        # Value
703
        my $value = ref $params->{$column} eq 'ARRAY'
704
                  ? $params->{$column}->[$count->{$column} || 0]
705
                  : $params->{$column};
706
        
cleanup
Yuki Kimoto authored on 2011-01-12
707
        # Filter
708
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
709
        
cleanup
Yuki Kimoto authored on 2011-01-12
710
        push @binds, $f ? $f->($value) : $value;
removed reconnect method
yuki-kimoto authored on 2010-05-28
711
        
712
        # Count up 
713
        $count->{$column}++;
714
    }
715
    
cleanup
Yuki Kimoto authored on 2011-01-12
716
    return \@binds;
removed reconnect method
yuki-kimoto authored on 2010-05-28
717
}
718

            
cleanup
yuki-kimoto authored on 2010-10-17
719
sub _croak {
720
    my ($self, $error, $append) = @_;
721
    $append ||= "";
722
    
723
    # Verbose
724
    if ($Carp::Verbose) { croak $error }
725
    
726
    # Not verbose
727
    else {
728
        
729
        # Remove line and module infromation
730
        my $at_pos = rindex($error, ' at ');
731
        $error = substr($error, 0, $at_pos);
732
        $error =~ s/\s+$//;
733
        
734
        croak "$error$append";
735
    }
736
}
737

            
cleanup
Yuki Kimoto authored on 2011-01-12
738
# Deprecated
739
__PACKAGE__->attr(cache_method => sub {
740
    sub {
741
        my $self = shift;
742
        
743
        $self->{_cached} ||= {};
744
        
745
        if (@_ > 1) {
746
            $self->{_cached}{$_[0]} = $_[1] 
747
        }
748
        else {
749
            return $self->{_cached}{$_[0]}
750
        }
751
    }
752
});
753

            
754
sub default_bind_filter {
755
    my $self = shift;
756
    
757
    if (@_) {
758
        my $fname = $_[0];
759
        
760
        if (@_ && !$fname) {
761
            $self->{default_out_filter} = undef;
762
        }
763
        else {
764
            croak qq{"$fname" is not registered}
765
              unless exists $self->filters->{$fname};
766
        
767
            $self->{default_out_filter} = $self->filters->{$fname};
768
        }
769
        return $self;
770
    }
771
    
772
    return $self->{default_out_filter};
773
}
774

            
775
sub default_fetch_filter {
776
    my $self = shift;
777
    my $fname = $_[0];
778
    
779
    if (@_) {
780
        if (@_ && !$fname) {
781
            $self->{default_in_filter} = undef;
782
        }
783
        else {
784
            croak qq{"$fname" is not registered}
785
              unless exists $self->filters->{$fname};
786
        
787
            $self->{default_in_filter} = $self->filters->{$fname};
788
        }
789
        
790
        return $self;
791
    }
792
    
793
    return $self->{default_in_filter}
794
}
795

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
798
=head1 NAME
799

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

            
802
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
803

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
812
    # Insert 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
813
    $dbi->insert(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
814
                 param  => {title => 'Perl', author => 'Ken'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
815
                 filter => {title => 'encode_utf8'});
816
    
817
    # Update 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
818
    $dbi->update(table  => 'book', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
819
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
820
                 where  => {id => 5},
821
                 filter => {title => 'encode_utf8'});
822
    
823
    # Update all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
824
    $dbi->update_all(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
825
                     param  => {title => 'Perl'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
826
                     filter => {title => 'encode_utf8'});
827
    
828
    # Delete
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
829
    $dbi->delete(table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
830
                 where  => {author => 'Ken'},
831
                 filter => {title => 'encode_utf8'});
832
    
833
    # Delete all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
834
    $dbi->delete_all(table => 'book');
cleanup
yuki-kimoto authored on 2010-08-05
835

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

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

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
866
    # Execute SQL
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
867
    $dbi->execute("select title from book");
removed register_format()
yuki-kimoto authored on 2010-05-26
868
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
869
    # Execute SQL with hash binding and filtering
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
870
    $dbi->execute("select id from book where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
871
                  param  => {author => 'ken', title => '%Perl%'},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
872
                  filter => {title => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
873

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

            
updated document
yuki-kimoto authored on 2010-08-08
880
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
881

            
882
    # Get DBI object
883
    my $dbh = $dbi->dbh;
884

            
885
Fetch row.
886

            
removed register_format()
yuki-kimoto authored on 2010-05-26
887
    # Fetch
888
    while (my $row = $result->fetch) {
889
        # ...
890
    }
891
    
892
    # Fetch hash
893
    while (my $row = $result->fetch_hash) {
894
        
895
    }
896
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
897
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
898

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
added dbi_options attribute
kimoto authored on 2010-12-20
946
=head2 C<dbi_options>
947

            
948
    my $dbi_options = $dbi->dbi_options;
949
    $dbi            = $dbi->dbi_options($dbi_options);
950

            
951
DBI options.
952
C<connect()> method use this value to connect the database.
953

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
961
Filter functions.
962
"encode_utf8" and "decode_utf8" is registered by default.
963

            
964
=head2 C<filter_check>
965

            
966
    my $filter_check = $dbi->filter_check;
967
    $dbi             = $dbi->filter_check(0);
968

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

            
972
=head2 C<password>
973

            
974
    my $password = $dbi->password;
975
    $dbi         = $dbi->password('lkj&le`@s');
976

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
999
    my $user = $dbi->user;
1000
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1001

            
cleanup
yuki-kimoto authored on 2010-10-17
1002
User name.
1003
C<connect()> method use this value to connect the database.
1004
    
1005
=head1 METHODS
added commit method
yuki-kimoto authored on 2010-05-27
1006

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

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

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1012
    $dbi->apply_filter(
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1013
        $table,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1014
        $column1 => {in => $infilter1, out => $outfilter1}
1015
        $column2 => {in => $infilter2, out => $outfilter2}
1016
        ...,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1017
    );
1018

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

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

            
cleanup
Yuki Kimoto authored on 2010-12-21
1026
    $result = $dbi->execute(
1027
        "select * from table1 where {= key1} and {= key2};",
1028
         param => {key1 => 1, key2 => 2},
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1029
         table => ['table1']
cleanup
Yuki Kimoto authored on 2010-12-21
1030
    );
1031
    
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1032
=head2 C<begin_work>
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1033

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

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

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

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

            
1044
    $dbi->commit;
1045

            
1046
Commit transaction.
1047
This is same as L<DBI>'s C<commit>.
1048

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1084
    my $result = $dbi->execute(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1085
        "select * from book where {= author} and {like title}", 
cleanup
yuki-kimoto authored on 2010-10-17
1086
        param => {author => 'Ken', title => '%Perl%'}
1087
    );
1088
    
1089
    while (my $row = $result->fetch) {
1090
        my $author = $row->[0];
1091
        my $title  = $row->[1];
1092
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
1093

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

            
1096
    my %expand = $dbi->expand($source);
1097

            
1098
The following hash
1099

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

            
1102
is expanded to
1103

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

            
1106
This is used in C<select()>
1107

            
1108

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1112
    $dbi->delete(table  => $table,
1113
                 where  => \%where,
1114
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1115
                 filter => \%filter,
1116
                 query  => 1);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1117

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

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

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

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

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

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

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

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

            
1150
    $dbi->helper(
1151
        update_or_insert => sub {
1152
            my $self = shift;
1153
            # do something
1154
        },
1155
        find_or_create   => sub {
1156
            my $self = shift;
1157
            # do something
1158
        }
1159
    );
1160

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

            
1163
    $dbi->update_or_insert;
1164
    $dbi->find_or_create;
1165

            
cleanup
yuki-kimoto authored on 2010-10-17
1166
=head2 C<insert>
1167

            
1168
    $dbi->insert(table  => $table, 
1169
                 param  => \%param,
1170
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1171
                 filter => \%filter,
1172
                 query  => 1);
cleanup
yuki-kimoto authored on 2010-10-17
1173

            
1174
Execute insert statement.
1175
C<insert> method have C<table>, C<param>, C<append>
1176
and C<filter> arguments.
1177
C<table> is a table name.
1178
C<param> is the pairs of column name value. this must be hash reference.
1179
C<append> is a string added at the end of the SQL statement.
1180
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1181
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1182
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1183
This is overwrites C<default_bind_filter>.
1184
Return value of C<insert()> is the count of affected rows.
1185

            
1186
B<Example:>
1187

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

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

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

            
1198
Create a new L<DBIx::Custom> object.
1199

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

            
1202
    $dbi->iterate_all_columns(
1203
        sub {
1204
            my ($table, $column, $column_info) = @_;
1205
            
1206
            # do something;
1207
        }
1208
    );
1209

            
1210
Iterate all columns of all tables. Argument is callback.
1211
You can do anything by callback.
1212

            
experimental extended select...
Yuki Kimoto authored on 2011-01-17
1213
=head2 C<(experimental) or>
1214

            
1215
    $or = $dbi->or(1, 5);
1216

            
1217
Create L<DBIx::Custom::Or> object. This is used with select method's
1218
where option.
1219

            
cleanup
yuki-kimoto authored on 2010-10-17
1220
=head2 C<register_filter>
1221

            
1222
    $dbi->register_filter(%filters);
1223
    $dbi->register_filter(\%filters);
1224
    
1225
Register filter. Registered filters is available in the following attributes
1226
or arguments.
1227

            
1228
=over 4
1229

            
1230
=item *
1231

            
1232
C<filter> argument of C<insert()>, C<update()>,
1233
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1234
methods
1235

            
1236
=item *
1237

            
1238
C<execute()> method
1239

            
1240
=item *
1241

            
1242
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1243

            
1244
=item *
1245

            
1246
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1247

            
1248
=back
1249

            
1250
B<Example:>
1251

            
1252
    $dbi->register_filter(
1253
        encode_utf8 => sub {
1254
            my $value = shift;
1255
            
1256
            require Encode;
1257
            
1258
            return Encode::encode('UTF-8', $value);
1259
        },
1260
        decode_utf8 => sub {
1261
            my $value = shift;
1262
            
1263
            require Encode;
1264
            
1265
            return Encode::decode('UTF-8', $value)
1266
        }
1267
    );
1268

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

            
1271
    $dbi->rollback;
1272

            
1273
Rollback transaction.
1274
This is same as L<DBI>'s C<rollback>.
1275

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1276
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1277
    
cleanup
yuki-kimoto authored on 2010-08-05
1278
    my $result = $dbi->select(table    => $table,
1279
                              column   => [@column],
1280
                              where    => \%where,
1281
                              append   => $append,
1282
                              relation => \%relation,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1283
                              filter   => \%filter,
1284
                              query    => 1);
update document
yuki-kimoto authored on 2009-11-19
1285

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

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

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1298
    # select * from book;
1299
    my $result = $dbi->select(table => 'book');
packaging one directory
yuki-kimoto authored on 2009-11-16
1300
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1301
    # select * from book where title = ?;
1302
    my $result = $dbi->select(table => 'book', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1303
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1304
    # select title, author from book where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1305
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1306
        table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1307
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1308
        where  => {id => 1},
1309
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1310
    );
1311
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1312
    # select book.name as book_name from book, rental
1313
    # where book.id = rental.book_id;
added commit method
yuki-kimoto authored on 2010-05-27
1314
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1315
        table    => ['book', 'rental'],
1316
        column   => ['book.name as book_name']
1317
        relation => {'book.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1318
    );
1319

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

            
1323
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1324
        table  => 'book',
cleanup
yuki-kimoto authored on 2010-08-09
1325
        column => ['title', 'author'],
1326
        where  => ['{= title} or {like author}',
1327
                   {title => '%Perl%', author => 'Ken'}]
1328
    );
1329

            
1330
First element is a string. it contains tags,
1331
such as "{= title} or {like author}".
1332
Second element is paramters.
1333

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1336
    $dbi->update(table  => $table, 
1337
                 param  => \%params,
1338
                 where  => \%where,
1339
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1340
                 filter => \%filter,
1341
                 query  => 1)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1342

            
cleanup
yuki-kimoto authored on 2010-10-17
1343
Execute update statement.
1344
C<update> method have C<table>, C<param>, C<where>, C<append>
1345
and C<filter> arguments.
1346
C<table> is a table name.
1347
C<param> is column-value pairs. this must be hash reference.
1348
C<where> is where clause. this must be hash reference.
1349
C<append> is a string added at the end of the SQL statement.
1350
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1351
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1352
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1353
This is overwrites C<default_bind_filter>.
1354
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1355

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

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

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

            
1366
    {
1367
        my $txn = $dbi->txn_scope;
1368
        $dbi->insert(table => 'book', param => {title => 'Perl'});
1369
        $dbi->insert(table => 'book', param => {title => 'Good days'});
1370
        $txn->commit;
1371
    }
1372

            
1373
Create transaction scope. If you escape scope(that is { .. }) and commited,
1374
Rollback is automatically done.
1375

            
1376
Note that this is feature of L<DBIx::TransactionManager>
1377
L<DBIx::TransactionManager> is required.
1378

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

            
1381
    $dbi->table('book',
1382
        insert => sub { ... },
1383
        update => sub { ... }
1384
    );
1385
    
1386
    my $table = $dbi->table('book');
1387

            
1388
Create a L<DBIx::Custom::Table> object,
1389
or get a L<DBIx::Custom::Table> object.
1390

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1393
    $dbi->update_all(table  => $table, 
1394
                     param  => \%params,
1395
                     filter => \%filter,
1396
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1397

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

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

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

            
fix tests
Yuki Kimoto authored on 2011-01-18
1409
=head2 C<(experimental) where>
1410

            
1411
    my $where = $dbi->where;
1412

            
1413
Create a new L<DBIx::Custom::Where> object.
1414

            
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
1415
=head2 C<experimental) build_where>
1416

            
1417

            
1418

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

            
1421
    my $default_bind_filter = $dbi->default_bind_filter;
1422
    $dbi                    = $dbi->default_bind_filter($fname);
1423

            
1424
Default filter when parameter binding is executed.
1425

            
1426
=head2 C<(deprecated) default_fetch_filter>
1427

            
1428
    my $default_fetch_filter = $dbi->default_fetch_filter;
1429
    $dbi = $dbi->default_fetch_filter($fname);
1430

            
1431
=head2 C<(deprecated) cache_method>
1432

            
1433
    $dbi          = $dbi->cache_method(\&cache_method);
1434
    $cache_method = $dbi->cache_method
1435

            
1436
Method to set and get caches.
1437

            
1438
B<Example:>
1439

            
1440
    $dbi->cache_method(
1441
        sub {
1442
            my $self = shift;
1443
            
1444
            $self->{_cached} ||= {};
1445
            
1446
            if (@_ > 1) {
1447
                $self->{_cached}{$_[0]} = $_[1] 
1448
            }
1449
            else {
1450
                return $self->{_cached}{$_[0]}
1451
            }
1452
        }
1453
    );
1454

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

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

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

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

            
1463
C<< <kimoto.yuki at gmail.com> >>
1464

            
1465
L<http://github.com/yuki-kimoto/DBIx-Custom>
1466

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1467
=head1 AUTHOR
1468

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

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

            
1473
Copyright 2009 Yuki Kimoto, all rights reserved.
1474

            
1475
This program is free software; you can redistribute it and/or modify it
1476
under the same terms as Perl itself.
1477

            
1478
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1479

            
1480