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

            
many changed
Yuki Kimoto authored on 2011-01-23
3
our $VERSION = '0.1636';
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,
many changed
Yuki Kimoto authored on 2011-01-23
23
    filter_check  => 1,
24
    query_builder => sub {DBIx::Custom::QueryBuilder->new},
25
    result_class  => 'DBIx::Custom::Result',
26
    table_class   => 'DBIx::Custom::Table'
27
);
28

            
29
__PACKAGE__->attr(
30
    cache_method => sub {
31
        sub {
32
            my $self = shift;
33
            
34
            $self->{_cached} ||= {};
35
            
36
            if (@_ > 1) {
37
                $self->{_cached}{$_[0]} = $_[1] 
38
            }
39
            else {
40
                return $self->{_cached}{$_[0]}
41
            }
42
        }
43
    }
44
);
45

            
46
__PACKAGE__->attr(
fix tests
Yuki Kimoto authored on 2011-01-13
47
    filters => sub {
48
        {
49
            encode_utf8 => sub { encode_utf8($_[0]) },
50
            decode_utf8 => sub { decode_utf8($_[0]) }
51
        }
many changed
Yuki Kimoto authored on 2011-01-23
52
    }
fix tests
Yuki Kimoto authored on 2011-01-13
53
);
cleanup
yuki-kimoto authored on 2010-10-17
54

            
55
# DBI methods
56
foreach my $method (qw/begin_work commit rollback/) {
57
    my $code = sub {
58
        my $self = shift;
59
        my $ret = eval {$self->dbh->$method};
60
        croak $@ if $@;
61
        return $ret;
62
    };
63
    no strict 'refs';
64
    my $pkg = __PACKAGE__;
65
    *{"${pkg}::$method"} = $code;
66
};
67

            
added helper method
yuki-kimoto authored on 2010-10-17
68
our $AUTOLOAD;
69

            
70
sub AUTOLOAD {
71
    my $self = shift;
72

            
73
    # Method
74
    my ($package, $method) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
75

            
76
    # Helper
77
    $self->{_helpers} ||= {};
78
    croak qq/Can't locate object method "$method" via "$package"/
79
      unless my $helper = $self->{_helpers}->{$method};
80

            
81
    # Run
82
    return $self->$helper(@_);
83
}
84

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
85
sub apply_filter {
many changed
Yuki Kimoto authored on 2011-01-23
86
    my ($self, $table, @cinfos) = @_;
87

            
88
    # Initialize filters
cleanup
Yuki Kimoto authored on 2011-01-12
89
    $self->{filter} ||= {};
many changed
Yuki Kimoto authored on 2011-01-23
90
    $self->{filter}{out} ||= {};
91
    $self->{filter}{in} ||= {};
cleanup
Yuki Kimoto authored on 2010-12-22
92
    
many changed
Yuki Kimoto authored on 2011-01-23
93
    # Create filters
94
    my $usage = "Usage: \$dbi->apply_filter(" .
95
                "TABLE, COLUMN1, {in => INFILTER1, out => OUTFILTER1}, " .
96
                "COLUMN2, {in => INFILTER2, out => OUTFILTER2}, ...)";
97

            
98
    for (my $i = 0; $i < @cinfos; $i += 2) {
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
99
        
many changed
Yuki Kimoto authored on 2011-01-23
100
        # Column
101
        my $column = $cinfos[$i];
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
102
        
many changed
Yuki Kimoto authored on 2011-01-23
103
        # Filter
104
        my $filter = $cinfos[$i + 1] || {};
105
        croak $usage unless  ref $filter eq 'HASH';
106
        foreach my $ftype (keys %$filter) {
107
            croak $usage unless $ftype eq 'in' || $ftype eq 'out'; 
108
        }
109
        my $in_filter = $filter->{in};
110
        my $out_filter = $filter->{out};
111
        
112
        # Out filter
113
        if (ref $out_filter eq 'CODE') {
114
            $self->{filter}{out}{$table}{$column}
115
              = $out_filter;
116
            $self->{filter}{out}{$table}{"$table.$column"}
117
              = $out_filter;
118
        }
119
        elsif (defined $out_filter) {
120
            croak qq{Filter "$out_filter" is not registered}
121
              unless exists $self->filters->{$out_filter};
cleanup
Yuki Kimoto authored on 2010-12-22
122
            
many changed
Yuki Kimoto authored on 2011-01-23
123
            $self->{filter}{out}{$table}{$column}
124
              = $self->filters->{$out_filter};
125
            $self->{filter}{out}{$table}{"$table.$column"}
126
              = $self->filters->{$out_filter};
cleanup
Yuki Kimoto authored on 2010-12-21
127
        }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
128
        
many changed
Yuki Kimoto authored on 2011-01-23
129
        # In filter
130
        if (ref $in_filter eq 'CODE') {
131
            $self->{filter}{in}{$table}{$column}
132
              = $in_filter;
133
            $self->{filter}{in}{$table}{"$table.$column"}
134
              = $in_filter;
135
        }
136
        elsif (defined $in_filter) {
137
            croak qq{Filter "$in_filter" is not registered}
138
              unless exists $self->filters->{$in_filter};
139
            $self->{filter}{in}{$table}{$column}
140
              = $self->filters->{$in_filter};
141
            $self->{filter}{in}{$table}{"$table.$column"}
142
              = $self->filters->{$in_filter};
143
        }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
144
    }
145
    
many changed
Yuki Kimoto authored on 2011-01-23
146
    return $self;
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
147
}
148

            
added helper method
yuki-kimoto authored on 2010-10-17
149
sub helper {
150
    my $self = shift;
151
    
152
    # Merge
153
    my $helpers = ref $_[0] eq 'HASH' ? $_[0] : {@_};
154
    $self->{_helpers} = {%{$self->{_helpers} || {}}, %$helpers};
155
    
156
    return $self;
157
}
158

            
packaging one directory
yuki-kimoto authored on 2009-11-16
159
sub connect {
many changed
Yuki Kimoto authored on 2011-01-23
160
    my $self = ref $_[0] ? shift : shift->SUPER::new(@_);;
removed register_format()
yuki-kimoto authored on 2010-05-26
161
    
many changed
Yuki Kimoto authored on 2011-01-23
162
    # Attributes
packaging one directory
yuki-kimoto authored on 2009-11-16
163
    my $data_source = $self->data_source;
many changed
Yuki Kimoto authored on 2011-01-23
164
    croak qq{"data_source" must be specified to connect()"}
check arguments of connect m...
Yuki Kimoto authored on 2010-12-20
165
      unless $data_source;
packaging one directory
yuki-kimoto authored on 2009-11-16
166
    my $user        = $self->user;
167
    my $password    = $self->password;
added dbi_options attribute
kimoto authored on 2010-12-20
168
    my $dbi_options = $self->dbi_options || {};
169
    
update document
yuki-kimoto authored on 2010-01-30
170
    # Connect
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
171
    my $dbh = eval {DBI->connect(
packaging one directory
yuki-kimoto authored on 2009-11-16
172
        $data_source,
173
        $user,
174
        $password,
175
        {
176
            RaiseError => 1,
177
            PrintError => 0,
178
            AutoCommit => 1,
added dbi_options attribute
kimoto authored on 2010-12-20
179
            %$dbi_options
packaging one directory
yuki-kimoto authored on 2009-11-16
180
        }
181
    )};
182
    
update document
yuki-kimoto authored on 2010-01-30
183
    # Connect error
packaging one directory
yuki-kimoto authored on 2009-11-16
184
    croak $@ if $@;
185
    
update document
yuki-kimoto authored on 2010-01-30
186
    # Database handle
packaging one directory
yuki-kimoto authored on 2009-11-16
187
    $self->dbh($dbh);
update document
yuki-kimoto authored on 2010-01-30
188
    
packaging one directory
yuki-kimoto authored on 2009-11-16
189
    return $self;
190
}
191

            
cleanup
yuki-kimoto authored on 2010-10-17
192
sub create_query {
193
    my ($self, $source) = @_;
update document
yuki-kimoto authored on 2010-01-30
194
    
cleanup
yuki-kimoto authored on 2010-10-17
195
    # Cache
196
    my $cache = $self->cache;
update document
yuki-kimoto authored on 2010-01-30
197
    
cleanup
yuki-kimoto authored on 2010-10-17
198
    # Create query
199
    my $query;
200
    if ($cache) {
201
        
202
        # Get query
203
        my $q = $self->cache_method->($self, $source);
204
        
205
        # Create query
206
        $query = DBIx::Custom::Query->new($q) if $q;
207
    }
208
    
209
    unless ($query) {
cleanup insert
yuki-kimoto authored on 2010-04-28
210

            
cleanup
yuki-kimoto authored on 2010-10-17
211
        # Create SQL object
212
        my $builder = $self->query_builder;
213
        
214
        # Create query
215
        $query = $builder->build_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
216

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
238
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
239
    my ($self, %args) = @_;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
240
    
241
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
242
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
243
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
244
          unless $VALID_DELETE_ARGS{$name};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
245
    }
246
    
247
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
248
    my $table            = $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
249
    croak qq{"table" option must be specified} unless $table;
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
250
    my $where            = $args{where} || {};
cleanup
yuki-kimoto authored on 2010-10-17
251
    my $append = $args{append};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
252
    my $filter           = $args{filter};
cleanup
yuki-kimoto authored on 2010-10-17
253
    my $allow_delete_all = $args{allow_delete_all};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
254

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

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

            
added helper method
yuki-kimoto authored on 2010-10-17
289
sub DESTROY { }
290

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

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

            
366
        return $result;
367
    }
368
    return $affected;
369
}
370

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

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

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

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

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

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

            
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
469
sub register_tag_processor {
470
    return shift->query_builder->register_tag_processor(@_);
471
}
472

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

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

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

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-01-12
734
sub default_bind_filter {
735
    my $self = shift;
736
    
737
    if (@_) {
738
        my $fname = $_[0];
739
        
740
        if (@_ && !$fname) {
741
            $self->{default_out_filter} = undef;
742
        }
743
        else {
many changed
Yuki Kimoto authored on 2011-01-23
744
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
745
              unless exists $self->filters->{$fname};
746
        
747
            $self->{default_out_filter} = $self->filters->{$fname};
748
        }
749
        return $self;
750
    }
751
    
752
    return $self->{default_out_filter};
753
}
754

            
755
sub default_fetch_filter {
756
    my $self = shift;
757
    
758
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
759
        my $fname = $_[0];
760

            
cleanup
Yuki Kimoto authored on 2011-01-12
761
        if (@_ && !$fname) {
762
            $self->{default_in_filter} = undef;
763
        }
764
        else {
many changed
Yuki Kimoto authored on 2011-01-23
765
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
766
              unless exists $self->filters->{$fname};
767
        
768
            $self->{default_in_filter} = $self->filters->{$fname};
769
        }
770
        
771
        return $self;
772
    }
773
    
many changed
Yuki Kimoto authored on 2011-01-23
774
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
775
}
776

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
779
=head1 NAME
780

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

            
783
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
784

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

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

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

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

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

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
847
    # Execute SQL
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
848
    $dbi->execute("select title from book");
removed register_format()
yuki-kimoto authored on 2010-05-26
849
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
850
    # Execute SQL with hash binding and filtering
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
851
    $dbi->execute("select id from book where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
852
                  param  => {author => 'ken', title => '%Perl%'},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
853
                  filter => {title => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
854

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

            
updated document
yuki-kimoto authored on 2010-08-08
861
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
862

            
863
    # Get DBI object
864
    my $dbh = $dbi->dbh;
865

            
866
Fetch row.
867

            
removed register_format()
yuki-kimoto authored on 2010-05-26
868
    # Fetch
869
    while (my $row = $result->fetch) {
870
        # ...
871
    }
872
    
873
    # Fetch hash
874
    while (my $row = $result->fetch_hash) {
875
        
876
    }
877
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
878
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
879

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

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

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

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

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

            
902
=head1 GUIDE
903

            
904
L<DBIx::Custom::Guide> - L<DBIx::Custom> complete guide
905

            
906
=head1 EXAMPLES
907

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
953
=head2 C<filter_check>
954

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

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

            
961
=head2 C<password>
962

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1033
    $dbi->commit;
1034

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1087
The following hash
1088

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

            
1091
is expanded to
1092

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

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

            
1097

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1175
B<Example:>
1176

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

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

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

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

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

            
pod fix
Yuki Kimoto authored on 2011-01-21
1191
    $dbi->each_column(
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1192
        sub {
pod fix
Yuki Kimoto authored on 2011-01-21
1193
            my ($table, $column, $info) = @_;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1194
            
pod fix
Yuki Kimoto authored on 2011-01-21
1195
            my $type = $info->{TYPE_NAME};
1196
            
1197
            if ($type eq 'DATE') {
1198
                # ...
1199
            }
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1200
        }
1201
    );
pod fix
Yuki Kimoto authored on 2011-01-21
1202
Get column informations from database.
1203
Argument is callback.
1204
You can do anything in callback.
1205
Callback receive three arguments, table name, column name and column
1206
information.
experimental extended select...
Yuki Kimoto authored on 2011-01-17
1207

            
cleanup
yuki-kimoto authored on 2010-10-17
1208
=head2 C<register_filter>
1209

            
1210
    $dbi->register_filter(%filters);
1211
    $dbi->register_filter(\%filters);
1212
    
1213
Register filter. Registered filters is available in the following attributes
1214
or arguments.
1215

            
1216
=over 4
1217

            
1218
=item *
1219

            
1220
C<filter> argument of C<insert()>, C<update()>,
1221
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1222
methods
1223

            
1224
=item *
1225

            
1226
C<execute()> method
1227

            
1228
=item *
1229

            
1230
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1231

            
1232
=item *
1233

            
1234
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1235

            
1236
=back
1237

            
1238
B<Example:>
1239

            
1240
    $dbi->register_filter(
1241
        encode_utf8 => sub {
1242
            my $value = shift;
1243
            
1244
            require Encode;
1245
            
1246
            return Encode::encode('UTF-8', $value);
1247
        },
1248
        decode_utf8 => sub {
1249
            my $value = shift;
1250
            
1251
            require Encode;
1252
            
1253
            return Encode::decode('UTF-8', $value)
1254
        }
1255
    );
1256

            
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1257
=head2 C<register_tag_processor>
1258

            
1259
    $dbi->register_tag_processor(
1260
        limit => sub {
1261
            ...;
1262
        }
1263
    );
1264

            
1265
Register tag processor.
1266

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

            
1269
    $dbi->rollback;
1270

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1374
Note that this is feature of L<DBIx::TransactionManager>
1375
L<DBIx::TransactionManager> is required.
1376

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

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

            
1386
Create a L<DBIx::Custom::Table> object,
1387
or get a L<DBIx::Custom::Table> object.
1388

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

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

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

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

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

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

            
1409
    my $where = $dbi->where;
1410

            
1411
Create a new L<DBIx::Custom::Where> object.
1412

            
cleanup
Yuki Kimoto authored on 2011-01-12
1413
=head2 C<(deprecated) cache_method>
1414

            
1415
    $dbi          = $dbi->cache_method(\&cache_method);
1416
    $cache_method = $dbi->cache_method
1417

            
1418
Method to set and get caches.
1419

            
1420
B<Example:>
1421

            
1422
    $dbi->cache_method(
1423
        sub {
1424
            my $self = shift;
1425
            
1426
            $self->{_cached} ||= {};
1427
            
1428
            if (@_ > 1) {
1429
                $self->{_cached}{$_[0]} = $_[1] 
1430
            }
1431
            else {
1432
                return $self->{_cached}{$_[0]}
1433
            }
1434
        }
1435
    );
1436

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

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

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

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

            
1445
C<< <kimoto.yuki at gmail.com> >>
1446

            
1447
L<http://github.com/yuki-kimoto/DBIx-Custom>
1448

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1449
=head1 AUTHOR
1450

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

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

            
1455
Copyright 2009 Yuki Kimoto, all rights reserved.
1456

            
1457
This program is free software; you can redistribute it and/or modify it
1458
under the same terms as Perl itself.
1459

            
1460
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1461

            
1462