DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1497 lines | 38.596kb
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} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
257
    croak qq{"table" option must be specified} unless $table;
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
258
    my $where            = $args{where} || {};
cleanup
yuki-kimoto authored on 2010-10-17
259
    my $append = $args{append};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
260
    my $filter           = $args{filter};
cleanup
yuki-kimoto authored on 2010-10-17
261
    my $allow_delete_all = $args{allow_delete_all};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
262

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
619
sub update {
620
    my ($self, %args) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
621
    
cleanup
yuki-kimoto authored on 2010-10-17
622
    # Check arguments
623
    foreach my $name (keys %args) {
624
        croak qq{"$name" is invalid argument}
625
          unless $VALID_UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
626
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
627
    
cleanup
yuki-kimoto authored on 2010-10-17
628
    # Arguments
629
    my $table            = $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
630
    croak qq{"table" option must be specified} unless $table;
cleanup
yuki-kimoto authored on 2010-10-17
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

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

            
925
=head1 GUIDE
926

            
927
L<DBIx::Custom::Guide> - L<DBIx::Custom> complete guide
928

            
929
=head1 EXAMPLES
930

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

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

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

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

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

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

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

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

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

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

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

            
added dbi_options attribute
kimoto authored on 2010-12-20
958
=head2 C<dbi_options>
959

            
960
    my $dbi_options = $dbi->dbi_options;
961
    $dbi            = $dbi->dbi_options($dbi_options);
962

            
963
DBI options.
964
C<connect()> method use this value to connect the database.
965

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
973
Filter functions.
974
"encode_utf8" and "decode_utf8" is registered by default.
975

            
976
=head2 C<filter_check>
977

            
978
    my $filter_check = $dbi->filter_check;
979
    $dbi             = $dbi->filter_check(0);
980

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

            
984
=head2 C<password>
985

            
986
    my $password = $dbi->password;
987
    $dbi         = $dbi->password('lkj&le`@s');
988

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1011
    my $user = $dbi->user;
1012
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1013

            
cleanup
yuki-kimoto authored on 2010-10-17
1014
User name.
1015
C<connect()> method use this value to connect the database.
1016
    
1017
=head1 METHODS
added commit method
yuki-kimoto authored on 2010-05-27
1018

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

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

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1024
    $dbi->apply_filter(
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1025
        $table,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1026
        $column1 => {in => $infilter1, out => $outfilter1}
1027
        $column2 => {in => $infilter2, out => $outfilter2}
1028
        ...,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1029
    );
1030

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

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

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

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

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

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

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

            
1056
    $dbi->commit;
1057

            
1058
Commit transaction.
1059
This is same as L<DBI>'s C<commit>.
1060

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

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

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

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

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

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

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

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

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

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

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

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

            
1108
    my %expand = $dbi->expand($source);
1109

            
1110
The following hash
1111

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

            
1114
is expanded to
1115

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

            
1118
This is used in C<select()>
1119

            
1120

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1124
    $dbi->delete(table  => $table,
1125
                 where  => \%where,
1126
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1127
                 filter => \%filter,
1128
                 query  => 1);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1129

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

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

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

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

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

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

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

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

            
1162
    $dbi->helper(
1163
        update_or_insert => sub {
1164
            my $self = shift;
1165
            # do something
1166
        },
1167
        find_or_create   => sub {
1168
            my $self = shift;
1169
            # do something
1170
        }
1171
    );
1172

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

            
1175
    $dbi->update_or_insert;
1176
    $dbi->find_or_create;
1177

            
cleanup
yuki-kimoto authored on 2010-10-17
1178
=head2 C<insert>
1179

            
1180
    $dbi->insert(table  => $table, 
1181
                 param  => \%param,
1182
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1183
                 filter => \%filter,
1184
                 query  => 1);
cleanup
yuki-kimoto authored on 2010-10-17
1185

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

            
1198
B<Example:>
1199

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

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

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

            
1210
Create a new L<DBIx::Custom> object.
1211

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1231
=head2 C<register_filter>
1232

            
1233
    $dbi->register_filter(%filters);
1234
    $dbi->register_filter(\%filters);
1235
    
1236
Register filter. Registered filters is available in the following attributes
1237
or arguments.
1238

            
1239
=over 4
1240

            
1241
=item *
1242

            
1243
C<filter> argument of C<insert()>, C<update()>,
1244
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1245
methods
1246

            
1247
=item *
1248

            
1249
C<execute()> method
1250

            
1251
=item *
1252

            
1253
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1254

            
1255
=item *
1256

            
1257
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1258

            
1259
=back
1260

            
1261
B<Example:>
1262

            
1263
    $dbi->register_filter(
1264
        encode_utf8 => sub {
1265
            my $value = shift;
1266
            
1267
            require Encode;
1268
            
1269
            return Encode::encode('UTF-8', $value);
1270
        },
1271
        decode_utf8 => sub {
1272
            my $value = shift;
1273
            
1274
            require Encode;
1275
            
1276
            return Encode::decode('UTF-8', $value)
1277
        }
1278
    );
1279

            
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1280
=head2 C<register_tag_processor>
1281

            
1282
    $dbi->register_tag_processor(
1283
        limit => sub {
1284
            ...;
1285
        }
1286
    );
1287

            
1288
Register tag processor.
1289

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

            
1292
    $dbi->rollback;
1293

            
1294
Rollback transaction.
1295
This is same as L<DBI>'s C<rollback>.
1296

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

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

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

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

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

            
1344
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1345
        table  => 'book',
cleanup
yuki-kimoto authored on 2010-08-09
1346
        column => ['title', 'author'],
1347
        where  => ['{= title} or {like author}',
1348
                   {title => '%Perl%', author => 'Ken'}]
1349
    );
1350

            
1351
First element is a string. it contains tags,
1352
such as "{= title} or {like author}".
1353
Second element is paramters.
1354

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1357
    $dbi->update(table  => $table, 
1358
                 param  => \%params,
1359
                 where  => \%where,
1360
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1361
                 filter => \%filter,
1362
                 query  => 1)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1363

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

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

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

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

            
1387
    {
1388
        my $txn = $dbi->txn_scope;
1389
        $dbi->insert(table => 'book', param => {title => 'Perl'});
1390
        $dbi->insert(table => 'book', param => {title => 'Good days'});
1391
        $txn->commit;
1392
    }
1393

            
1394
Create transaction scope. If you escape scope(that is { .. }) and commited,
1395
Rollback is automatically done.
1396

            
1397
Note that this is feature of L<DBIx::TransactionManager>
1398
L<DBIx::TransactionManager> is required.
1399

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

            
1402
    $dbi->table('book',
1403
        insert => sub { ... },
1404
        update => sub { ... }
1405
    );
1406
    
1407
    my $table = $dbi->table('book');
1408

            
1409
Create a L<DBIx::Custom::Table> object,
1410
or get a L<DBIx::Custom::Table> object.
1411

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1414
    $dbi->update_all(table  => $table, 
1415
                     param  => \%params,
1416
                     filter => \%filter,
1417
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1418

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

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

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

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

            
1432
    my $where = $dbi->where;
1433

            
1434
Create a new L<DBIx::Custom::Where> object.
1435

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

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

            
1441
Default filter when parameter binding is executed.
1442

            
1443
=head2 C<(deprecated) default_fetch_filter>
1444

            
1445
    my $default_fetch_filter = $dbi->default_fetch_filter;
1446
    $dbi = $dbi->default_fetch_filter($fname);
1447

            
1448
=head2 C<(deprecated) cache_method>
1449

            
1450
    $dbi          = $dbi->cache_method(\&cache_method);
1451
    $cache_method = $dbi->cache_method
1452

            
1453
Method to set and get caches.
1454

            
1455
B<Example:>
1456

            
1457
    $dbi->cache_method(
1458
        sub {
1459
            my $self = shift;
1460
            
1461
            $self->{_cached} ||= {};
1462
            
1463
            if (@_ > 1) {
1464
                $self->{_cached}{$_[0]} = $_[1] 
1465
            }
1466
            else {
1467
                return $self->{_cached}{$_[0]}
1468
            }
1469
        }
1470
    );
1471

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

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

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

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

            
1480
C<< <kimoto.yuki at gmail.com> >>
1481

            
1482
L<http://github.com/yuki-kimoto/DBIx-Custom>
1483

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1484
=head1 AUTHOR
1485

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

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

            
1490
Copyright 2009 Yuki Kimoto, all rights reserved.
1491

            
1492
This program is free software; you can redistribute it and/or modify it
1493
under the same terms as Perl itself.
1494

            
1495
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1496

            
1497