DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1490 lines | 38.25kb
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

            
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
481
sub register_tag_processor {
482
    return shift->query_builder->register_tag_processor(@_);
483
}
484

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
802
=head1 NAME
803

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

            
806
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
807

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

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

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

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

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

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

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

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

            
updated document
yuki-kimoto authored on 2010-08-08
884
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
885

            
886
    # Get DBI object
887
    my $dbh = $dbi->dbh;
888

            
889
Fetch row.
890

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
added dbi_options attribute
kimoto authored on 2010-12-20
950
=head2 C<dbi_options>
951

            
952
    my $dbi_options = $dbi->dbi_options;
953
    $dbi            = $dbi->dbi_options($dbi_options);
954

            
955
DBI options.
956
C<connect()> method use this value to connect the database.
957

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
965
Filter functions.
966
"encode_utf8" and "decode_utf8" is registered by default.
967

            
968
=head2 C<filter_check>
969

            
970
    my $filter_check = $dbi->filter_check;
971
    $dbi             = $dbi->filter_check(0);
972

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

            
976
=head2 C<password>
977

            
978
    my $password = $dbi->password;
979
    $dbi         = $dbi->password('lkj&le`@s');
980

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1003
    my $user = $dbi->user;
1004
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1005

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

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

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

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

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

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

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

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

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

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

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

            
1048
    $dbi->commit;
1049

            
1050
Commit transaction.
1051
This is same as L<DBI>'s C<commit>.
1052

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

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

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

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

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

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

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

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

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

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

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

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

            
1100
    my %expand = $dbi->expand($source);
1101

            
1102
The following hash
1103

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

            
1106
is expanded to
1107

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

            
1110
This is used in C<select()>
1111

            
1112

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

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

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

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

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

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

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

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

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

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

            
1154
    $dbi->helper(
1155
        update_or_insert => sub {
1156
            my $self = shift;
1157
            # do something
1158
        },
1159
        find_or_create   => sub {
1160
            my $self = shift;
1161
            # do something
1162
        }
1163
    );
1164

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

            
1167
    $dbi->update_or_insert;
1168
    $dbi->find_or_create;
1169

            
cleanup
yuki-kimoto authored on 2010-10-17
1170
=head2 C<insert>
1171

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

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

            
1190
B<Example:>
1191

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

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

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

            
1202
Create a new L<DBIx::Custom> object.
1203

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

            
1206
    $dbi->iterate_all_columns(
1207
        sub {
1208
            my ($table, $column, $column_info) = @_;
1209
            
1210
            # do something;
1211
        }
1212
    );
1213

            
1214
Iterate all columns of all tables. Argument is callback.
1215
You can do anything by callback.
1216

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

            
1219
    $or = $dbi->or(1, 5);
1220

            
1221
Create L<DBIx::Custom::Or> object. This is used with select method's
1222
where option.
1223

            
cleanup
yuki-kimoto authored on 2010-10-17
1224
=head2 C<register_filter>
1225

            
1226
    $dbi->register_filter(%filters);
1227
    $dbi->register_filter(\%filters);
1228
    
1229
Register filter. Registered filters is available in the following attributes
1230
or arguments.
1231

            
1232
=over 4
1233

            
1234
=item *
1235

            
1236
C<filter> argument of C<insert()>, C<update()>,
1237
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1238
methods
1239

            
1240
=item *
1241

            
1242
C<execute()> method
1243

            
1244
=item *
1245

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

            
1248
=item *
1249

            
1250
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1251

            
1252
=back
1253

            
1254
B<Example:>
1255

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

            
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1273
=head2 C<register_tag_processor>
1274

            
1275
    $dbi->register_tag_processor(
1276
        limit => sub {
1277
            ...;
1278
        }
1279
    );
1280

            
1281
Register tag processor.
1282

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

            
1285
    $dbi->rollback;
1286

            
1287
Rollback transaction.
1288
This is same as L<DBI>'s C<rollback>.
1289

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1290
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1291
    
cleanup
yuki-kimoto authored on 2010-08-05
1292
    my $result = $dbi->select(table    => $table,
1293
                              column   => [@column],
1294
                              where    => \%where,
1295
                              append   => $append,
1296
                              relation => \%relation,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1297
                              filter   => \%filter,
1298
                              query    => 1);
update document
yuki-kimoto authored on 2009-11-19
1299

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

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

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1312
    # select * from book;
1313
    my $result = $dbi->select(table => 'book');
packaging one directory
yuki-kimoto authored on 2009-11-16
1314
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1315
    # select * from book where title = ?;
1316
    my $result = $dbi->select(table => 'book', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1317
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1318
    # select title, author from book where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1319
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1320
        table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1321
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1322
        where  => {id => 1},
1323
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1324
    );
1325
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1326
    # select book.name as book_name from book, rental
1327
    # where book.id = rental.book_id;
added commit method
yuki-kimoto authored on 2010-05-27
1328
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1329
        table    => ['book', 'rental'],
1330
        column   => ['book.name as book_name']
1331
        relation => {'book.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1332
    );
1333

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

            
1337
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1338
        table  => 'book',
cleanup
yuki-kimoto authored on 2010-08-09
1339
        column => ['title', 'author'],
1340
        where  => ['{= title} or {like author}',
1341
                   {title => '%Perl%', author => 'Ken'}]
1342
    );
1343

            
1344
First element is a string. it contains tags,
1345
such as "{= title} or {like author}".
1346
Second element is paramters.
1347

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1350
    $dbi->update(table  => $table, 
1351
                 param  => \%params,
1352
                 where  => \%where,
1353
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1354
                 filter => \%filter,
1355
                 query  => 1)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1356

            
cleanup
yuki-kimoto authored on 2010-10-17
1357
Execute update statement.
1358
C<update> method have C<table>, C<param>, C<where>, C<append>
1359
and C<filter> arguments.
1360
C<table> is a table name.
1361
C<param> is column-value pairs. this must be hash reference.
1362
C<where> is where clause. this must be hash reference.
1363
C<append> is a string added at the end of the SQL statement.
1364
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1365
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1366
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1367
This is overwrites C<default_bind_filter>.
1368
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1369

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

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

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

            
1380
    {
1381
        my $txn = $dbi->txn_scope;
1382
        $dbi->insert(table => 'book', param => {title => 'Perl'});
1383
        $dbi->insert(table => 'book', param => {title => 'Good days'});
1384
        $txn->commit;
1385
    }
1386

            
1387
Create transaction scope. If you escape scope(that is { .. }) and commited,
1388
Rollback is automatically done.
1389

            
1390
Note that this is feature of L<DBIx::TransactionManager>
1391
L<DBIx::TransactionManager> is required.
1392

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

            
1395
    $dbi->table('book',
1396
        insert => sub { ... },
1397
        update => sub { ... }
1398
    );
1399
    
1400
    my $table = $dbi->table('book');
1401

            
1402
Create a L<DBIx::Custom::Table> object,
1403
or get a L<DBIx::Custom::Table> object.
1404

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1407
    $dbi->update_all(table  => $table, 
1408
                     param  => \%params,
1409
                     filter => \%filter,
1410
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1411

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

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

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

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

            
1425
    my $where = $dbi->where;
1426

            
1427
Create a new L<DBIx::Custom::Where> object.
1428

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

            
1431
    my $default_bind_filter = $dbi->default_bind_filter;
1432
    $dbi                    = $dbi->default_bind_filter($fname);
1433

            
1434
Default filter when parameter binding is executed.
1435

            
1436
=head2 C<(deprecated) default_fetch_filter>
1437

            
1438
    my $default_fetch_filter = $dbi->default_fetch_filter;
1439
    $dbi = $dbi->default_fetch_filter($fname);
1440

            
1441
=head2 C<(deprecated) cache_method>
1442

            
1443
    $dbi          = $dbi->cache_method(\&cache_method);
1444
    $cache_method = $dbi->cache_method
1445

            
1446
Method to set and get caches.
1447

            
1448
B<Example:>
1449

            
1450
    $dbi->cache_method(
1451
        sub {
1452
            my $self = shift;
1453
            
1454
            $self->{_cached} ||= {};
1455
            
1456
            if (@_ > 1) {
1457
                $self->{_cached}{$_[0]} = $_[1] 
1458
            }
1459
            else {
1460
                return $self->{_cached}{$_[0]}
1461
            }
1462
        }
1463
    );
1464

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

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

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

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

            
1473
C<< <kimoto.yuki at gmail.com> >>
1474

            
1475
L<http://github.com/yuki-kimoto/DBIx-Custom>
1476

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1477
=head1 AUTHOR
1478

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

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

            
1483
Copyright 2009 Yuki Kimoto, all rights reserved.
1484

            
1485
This program is free software; you can redistribute it and/or modify it
1486
under the same terms as Perl itself.
1487

            
1488
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1489

            
1490