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

            
pod fix
Yuki Kimoto authored on 2011-01-21
3
our $VERSION = '0.1635';
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

            
pod fix
Yuki Kimoto authored on 2011-01-21
433
sub each_column {
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
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

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

            
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
475
sub register_tag_processor {
476
    return shift->query_builder->register_tag_processor(@_);
477
}
478

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
796
=head1 NAME
797

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

            
800
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
801

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

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

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

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

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

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

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

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

            
updated document
yuki-kimoto authored on 2010-08-08
878
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
879

            
880
    # Get DBI object
881
    my $dbh = $dbi->dbh;
882

            
883
Fetch row.
884

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

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

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

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

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

            
pod fix
Yuki Kimoto authored on 2011-01-21
917
See L<DBIx::Custom::Guide> for more details.
918

            
919
=head1 GUIDE
920

            
921
L<DBIx::Custom::Guide> - L<DBIx::Custom> complete guide
922

            
923
=head1 EXAMPLES
924

            
925
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki> - Many useful examples
updated document
yuki-kimoto authored on 2010-08-08
926

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

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

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

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

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

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

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

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

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

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

            
added dbi_options attribute
kimoto authored on 2010-12-20
952
=head2 C<dbi_options>
953

            
954
    my $dbi_options = $dbi->dbi_options;
955
    $dbi            = $dbi->dbi_options($dbi_options);
956

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

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

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

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

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

            
970
=head2 C<filter_check>
971

            
972
    my $filter_check = $dbi->filter_check;
973
    $dbi             = $dbi->filter_check(0);
974

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

            
978
=head2 C<password>
979

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1050
    $dbi->commit;
1051

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1102
    my %expand = $dbi->expand($source);
1103

            
1104
The following hash
1105

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

            
1108
is expanded to
1109

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

            
1112
This is used in C<select()>
1113

            
1114

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

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

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

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

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

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

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

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

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

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

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

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

            
1169
    $dbi->update_or_insert;
1170
    $dbi->find_or_create;
1171

            
cleanup
yuki-kimoto authored on 2010-10-17
1172
=head2 C<insert>
1173

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

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

            
1192
B<Example:>
1193

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

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

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

            
1204
Create a new L<DBIx::Custom> object.
1205

            
pod fix
Yuki Kimoto authored on 2011-01-21
1206
=head2 C<(experimental) each_column>
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1207

            
pod fix
Yuki Kimoto authored on 2011-01-21
1208
    $dbi->each_column(
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1209
        sub {
pod fix
Yuki Kimoto authored on 2011-01-21
1210
            my ($table, $column, $info) = @_;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1211
            
pod fix
Yuki Kimoto authored on 2011-01-21
1212
            my $type = $info->{TYPE_NAME};
1213
            
1214
            if ($type eq 'DATE') {
1215
                # ...
1216
            }
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1217
        }
1218
    );
pod fix
Yuki Kimoto authored on 2011-01-21
1219
Get column informations from database.
1220
Argument is callback.
1221
You can do anything in callback.
1222
Callback receive three arguments, table name, column name and column
1223
information.
experimental extended select...
Yuki Kimoto authored on 2011-01-17
1224

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

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

            
1233
=over 4
1234

            
1235
=item *
1236

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

            
1241
=item *
1242

            
1243
C<execute()> method
1244

            
1245
=item *
1246

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

            
1249
=item *
1250

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

            
1253
=back
1254

            
1255
B<Example:>
1256

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

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

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

            
1282
Register tag processor.
1283

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

            
1286
    $dbi->rollback;
1287

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1435
Default filter when parameter binding is executed.
1436

            
1437
=head2 C<(deprecated) default_fetch_filter>
1438

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

            
1442
=head2 C<(deprecated) cache_method>
1443

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

            
1447
Method to set and get caches.
1448

            
1449
B<Example:>
1450

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

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

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

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

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

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

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

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

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

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

            
1484
Copyright 2009 Yuki Kimoto, all rights reserved.
1485

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

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

            
1491