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

            
update pod
Yuki Kimoto authored on 2011-03-13
3
our $VERSION = '0.1660';
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;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
17
use DBIx::Custom::Model;
cleanup
Yuki Kimoto authored on 2011-01-25
18
use DBIx::Custom::Tag;
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
19
use DBIx::Custom::Util;
update document
yuki-kimoto authored on 2010-05-27
20
use Encode qw/encode_utf8 decode_utf8/;
packaging one directory
yuki-kimoto authored on 2009-11-16
21

            
fix tests
Yuki Kimoto authored on 2011-01-13
22
__PACKAGE__->attr(
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
23
    [qw/data_source password pid user/],
fix tests
Yuki Kimoto authored on 2011-01-13
24
    cache => 1,
many changed
Yuki Kimoto authored on 2011-01-23
25
    cache_method => sub {
26
        sub {
27
            my $self = shift;
28
            
29
            $self->{_cached} ||= {};
30
            
31
            if (@_ > 1) {
update pod
Yuki Kimoto authored on 2011-03-13
32
                $self->{_cached}{$_[0]} = $_[1];
many changed
Yuki Kimoto authored on 2011-01-23
33
            }
34
            else {
update pod
Yuki Kimoto authored on 2011-03-13
35
                return $self->{_cached}{$_[0]};
many changed
Yuki Kimoto authored on 2011-01-23
36
            }
37
        }
update pod
Yuki Kimoto authored on 2011-03-13
38
    },
39
    dbi_option => sub { {} },
40
    default_dbi_option => sub {
41
        {
42
            RaiseError => 1,
43
            PrintError => 0,
44
            AutoCommit => 1
45
        }
46
    },
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
        }
update pod
Yuki Kimoto authored on 2011-03-13
52
    },
53
    models => sub { {} },
54
    query_builder => sub { DBIx::Custom::QueryBuilder->new },
55
    result_class  => 'DBIx::Custom::Result',
56
    safety_character => '\w',
57
    stash => sub { {} }
fix tests
Yuki Kimoto authored on 2011-01-13
58
);
cleanup
yuki-kimoto authored on 2010-10-17
59

            
added helper method
yuki-kimoto authored on 2010-10-17
60
our $AUTOLOAD;
61
sub AUTOLOAD {
62
    my $self = shift;
63

            
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
64
    # Method name
65
    my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
added helper method
yuki-kimoto authored on 2010-10-17
66

            
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
67
    # Method
68
    $self->{_methods} ||= {};
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
69
    if (my $method = $self->{_methods}->{$mname}) {
70
        return $self->$method(@_)
71
    }
72
    elsif ($self->dbh->can($mname)) {
73
        $self->dbh->$mname(@_);
74
    }
75
    else {
76
        croak qq/Can't locate object method "$mname" via "$package"/
77
    }
added helper method
yuki-kimoto authored on 2010-10-17
78
}
79

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

            
83
    # Initialize filters
cleanup
Yuki Kimoto authored on 2011-01-12
84
    $self->{filter} ||= {};
many changed
Yuki Kimoto authored on 2011-01-23
85
    $self->{filter}{out} ||= {};
86
    $self->{filter}{in} ||= {};
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
87
    $self->{filter}{end} ||= {};
cleanup
Yuki Kimoto authored on 2010-12-22
88
    
many changed
Yuki Kimoto authored on 2011-01-23
89
    # Create filters
90
    my $usage = "Usage: \$dbi->apply_filter(" .
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
91
                "TABLE, COLUMN1, {in => INFILTER1, out => OUTFILTER1, end => ENDFILTER1}, " .
92
                "COLUMN2, {in => INFILTER2, out => OUTFILTER2, end => ENDFILTER2}, ...)";
many changed
Yuki Kimoto authored on 2011-01-23
93

            
94
    for (my $i = 0; $i < @cinfos; $i += 2) {
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
95
        
many changed
Yuki Kimoto authored on 2011-01-23
96
        # Column
97
        my $column = $cinfos[$i];
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
98
        
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
99
        if (ref $column eq 'ARRAY') {
100
            foreach my $c (@$column) {
101
                push @cinfos, $c, $cinfos[$i + 1];
102
            }
103
            next;
104
        }
105
        
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
106
        # Filter info
107
        my $finfo = $cinfos[$i + 1] || {};
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
108
        croak "$usage (table: $table)" unless  ref $finfo eq 'HASH';
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
109
        foreach my $ftype (keys %$finfo) {
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
110
            croak "$usage (table: $table 2)" unless $ftype eq 'in' || $ftype eq 'out'
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
111
                             || $ftype eq 'end'; 
many changed
Yuki Kimoto authored on 2011-01-23
112
        }
113
        
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
114
        foreach my $way (qw/in out end/) {
115
            my $filter = $finfo->{$way};
cleanup
Yuki Kimoto authored on 2010-12-22
116
            
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
117
            # State
118
            my $state = !exists $finfo->{$way} ? 'not_exists'
119
                      : !defined $filter        ? 'not_defined'
120
                      : ref $filter eq 'CODE'   ? 'code'
121
                      : 'name';
122
            
123
            next if $state eq 'not_exists';
124
            
125
            # Check filter
126
            croak qq{Filter "$filter" is not registered}
127
              if  $state eq 'name'
128
               && ! exists $self->filters->{$filter};
129
            
130
            # Filter
131
            my $f = $state eq 'not_defined' ? undef
132
                  : $state eq 'code'        ? $filter
133
                  : $self->filters->{$filter};
134
            $self->{filter}{$way}{$table}{$column} = $f;
135
            $self->{filter}{$way}{$table}{"$table.$column"} = $f;
136
            $self->{filter}{$way}{$table}{"${table}__$column"} = $f;
many changed
Yuki Kimoto authored on 2011-01-23
137
        }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
138
    }
139
    
many changed
Yuki Kimoto authored on 2011-01-23
140
    return $self;
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
141
}
142

            
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
143
sub method {
added helper method
yuki-kimoto authored on 2010-10-17
144
    my $self = shift;
145
    
146
    # Merge
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
147
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
148
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
added helper method
yuki-kimoto authored on 2010-10-17
149
    
150
    return $self;
151
}
152

            
packaging one directory
yuki-kimoto authored on 2009-11-16
153
sub connect {
cleanup
Yuki Kimoto authored on 2011-01-25
154
    my $self = ref $_[0] ? shift : shift->new(@_);;
removed register_format()
yuki-kimoto authored on 2010-05-26
155
    
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
156
    my $dbh = $self->_connect;
packaging one directory
yuki-kimoto authored on 2009-11-16
157
    
update document
yuki-kimoto authored on 2010-01-30
158
    # Database handle
packaging one directory
yuki-kimoto authored on 2009-11-16
159
    $self->dbh($dbh);
update document
yuki-kimoto authored on 2010-01-30
160
    
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
161
    # Process ID
162
    $self->pid($$);
163
    
packaging one directory
yuki-kimoto authored on 2009-11-16
164
    return $self;
165
}
166

            
cleanup
yuki-kimoto authored on 2010-10-17
167
sub create_query {
168
    my ($self, $source) = @_;
update document
yuki-kimoto authored on 2010-01-30
169
    
cleanup
yuki-kimoto authored on 2010-10-17
170
    # Cache
171
    my $cache = $self->cache;
update document
yuki-kimoto authored on 2010-01-30
172
    
cleanup
yuki-kimoto authored on 2010-10-17
173
    # Create query
174
    my $query;
175
    if ($cache) {
176
        
177
        # Get query
178
        my $q = $self->cache_method->($self, $source);
179
        
180
        # Create query
add table tag
Yuki Kimoto authored on 2011-02-09
181
        if ($q) {
182
            $query = DBIx::Custom::Query->new($q);
183
            $query->filters($self->filters);
184
        }
cleanup
yuki-kimoto authored on 2010-10-17
185
    }
186
    
187
    unless ($query) {
cleanup insert
yuki-kimoto authored on 2010-04-28
188

            
cleanup
yuki-kimoto authored on 2010-10-17
189
        # Create SQL object
190
        my $builder = $self->query_builder;
191
        
192
        # Create query
193
        $query = $builder->build_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
194

            
cleanup
yuki-kimoto authored on 2010-10-17
195
        # Cache query
196
        $self->cache_method->($self, $source,
197
                             {sql     => $query->sql, 
add table tag
Yuki Kimoto authored on 2011-02-09
198
                              columns => $query->columns,
199
                              tables  => $query->tables})
cleanup
yuki-kimoto authored on 2010-10-17
200
          if $cache;
cleanup insert
yuki-kimoto authored on 2010-04-28
201
    }
202
    
cleanup
yuki-kimoto authored on 2010-10-17
203
    # Prepare statement handle
204
    my $sth;
205
    eval { $sth = $self->dbh->prepare($query->{sql})};
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
206
    $self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@;
packaging one directory
yuki-kimoto authored on 2009-11-16
207
    
cleanup
yuki-kimoto authored on 2010-10-17
208
    # Set statement handle
209
    $query->sth($sth);
packaging one directory
yuki-kimoto authored on 2009-11-16
210
    
cleanup
Yuki Kimoto authored on 2011-02-09
211
    # Set filters
212
    $query->filters($self->filters);
213
    
cleanup
yuki-kimoto authored on 2010-10-17
214
    return $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
215
}
216

            
update pod
Yuki Kimoto authored on 2011-03-13
217
sub dbh {
218
    my $self = shift;
219

            
220
    if (@_) {
221
        $self->{dbh} = $_[0];
222
        return $self;
223
    }
224
    else {
225
        my $pid = $$;
226
        if ($self->pid eq $pid) {
227
            return $self->{dbh};
228
        }
229
        else {
230
            # Create new connection in child process
231
            croak "Process is forked in transaction"
232
              unless $self->{dbh}->{AutoCommit};
233
            $self->pid($pid);
234
            $self->{dbh}->{InactiveDestroy} = 1;
235
            return $self->{dbh} = $self->_connect;
236
        }
237
    }
238
}
239

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

            
cleanup
yuki-kimoto authored on 2010-10-17
243
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
244
    my ($self, %args) = @_;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
245
    
cleanup
Yuki Kimoto authored on 2011-03-09
246
    # Check argument names
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
247
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
248
        croak qq{Argument "$name" is invalid name}
cleanup
yuki-kimoto authored on 2010-10-17
249
          unless $VALID_DELETE_ARGS{$name};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
250
    }
251
    
252
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
253
    my $table            = $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
254
    croak qq{"table" option must be specified} unless $table;
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
255
    my $where            = $args{where} || {};
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
256
    my $append           = $args{append};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
257
    my $filter           = $args{filter};
cleanup
yuki-kimoto authored on 2010-10-17
258
    my $allow_delete_all = $args{allow_delete_all};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
259

            
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
260
    # Where
261
    my $w;
262
    if (ref $where eq 'HASH') {
263
        my $clause = ['and'];
264
        push @$clause, "{= $_}" for keys %$where;
265
        $w = $self->where;
266
        $w->clause($clause);
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
267
        $w->param($where);
packaging one directory
yuki-kimoto authored on 2009-11-16
268
    }
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
269
    elsif (ref $where eq 'DBIx::Custom::Where') {
270
        $w = $where;
271
        $where = $w->param;
272
    }    
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
273
    croak qq{"where" must be hash refernce or DBIx::Custom::Where object}
274
      unless ref $w eq 'DBIx::Custom::Where';
275
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
276
    # String where
277
    my $swhere = "$w";
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
278
    
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
279
    croak qq{"where" must be specified}
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
280
      if $swhere eq '' && !$allow_delete_all;
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
281

            
cleanup
Yuki Kimoto authored on 2011-01-27
282
    # SQL stack
283
    my @sql;
284

            
285
    # Delete
286
    push @sql, "delete from $table $swhere";
287
    push @sql, $append if $append;
288
    
289
    my $sql = join(' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
290
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
291
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
292
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
293
    return $query if $args{query};
294
    
packaging one directory
yuki-kimoto authored on 2009-11-16
295
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
296
    my $ret_val = $self->execute(
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
297
        $query, param  => $where, filter => $filter,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
298
        table => $table);
packaging one directory
yuki-kimoto authored on 2009-11-16
299
    
300
    return $ret_val;
301
}
302

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

            
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
305
our %VALID_DELETE_AT_ARGS
update pod
Yuki Kimoto authored on 2011-03-13
306
  = map { $_ => 1 } qw/table where append filter query primary_key param/;
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
307

            
308
sub delete_at {
309
    my ($self, %args) = @_;
310
    
cleanup
Yuki Kimoto authored on 2011-03-09
311
    # Check argument names
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
312
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
313
        croak qq{Argument "$name" is invalid name}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
314
          unless $VALID_DELETE_AT_ARGS{$name};
315
    }
316
    
317
    # Primary key
318
    my $primary_keys = delete $args{primary_key};
319
    $primary_keys = [$primary_keys] unless ref $primary_keys;
320
    
321
    # Where clause
322
    my $where = {};
323
    if (exists $args{where}) {
324
        my $where_columns = delete $args{where};
325
        $where_columns = [$where_columns] unless ref $where_columns;
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
326

            
327
        croak qq{"where" must be constant value or array reference}
328
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
329
        
330
        for(my $i = 0; $i < @$primary_keys; $i ++) {
331
           $where->{$primary_keys->[$i]} = $where_columns->[$i];
332
        }
333
    }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
334
    
335
    if (exists $args{param}) {
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
336
        my $param = delete $args{param};
337
        
338
        for(my $i = 0; $i < @$primary_keys; $i ++) {
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
339
            delete $param->{$primary_keys->[$i]};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
340
        }
341
    }
342
    
343
    return $self->delete(where => $where, %args);
344
}
345

            
added helper method
yuki-kimoto authored on 2010-10-17
346
sub DESTROY { }
347

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

            
cleanup
yuki-kimoto authored on 2010-10-17
350
sub execute{
351
    my ($self, $query, %args)  = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
352
    
cleanup
Yuki Kimoto authored on 2011-03-09
353
    # Check argument names
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
354
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
355
        croak qq{Argument "$name" is invalid name}
cleanup
yuki-kimoto authored on 2010-10-17
356
          unless $VALID_EXECUTE_ARGS{$name};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
357
    }
358
    
cleanup
yuki-kimoto authored on 2010-10-17
359
    my $params = $args{param} || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
360
    
cleanup
yuki-kimoto authored on 2010-10-17
361
    # First argument is the soruce of SQL
362
    $query = $self->create_query($query)
363
      unless ref $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
364
    
add table tag
Yuki Kimoto authored on 2011-02-09
365
    # Applied filter
cleanup
Yuki Kimoto authored on 2011-01-12
366
    my $filter = {};
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
367
    
add table tag
Yuki Kimoto authored on 2011-02-09
368
    my $tables = $query->tables;
369
    my $arg_tables = $args{table} || [];
370
    $arg_tables = [$arg_tables]
371
      unless ref $arg_tables eq 'ARRAY';
372
    push @$tables, @$arg_tables;
cleanup
Yuki Kimoto authored on 2011-03-09
373

            
374
    # Organize tables
375
    my %table_set = map {defined $_ ? ($_ => 1) : ()} @$tables;
376
    my $main_table = pop @$tables;
377
    delete $table_set{$main_table} if $main_table;
378
    $tables = [keys %table_set];
379
    push @$tables, $main_table if $main_table;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
380
    
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
381
    foreach my $table (@$tables) {
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
382
        next unless $table;
cleanup
Yuki Kimoto authored on 2011-01-12
383
        $filter = {
384
            %$filter,
385
            %{$self->{filter}{out}->{$table} || {}}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
386
        }
387
    }
388
    
cleanup
Yuki Kimoto authored on 2011-01-12
389
    # Filter argument
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
390
    my $f = DBIx::Custom::Util::array_filter_to_hash($args{filter})
391
         || $query->filter || {};
cleanup
Yuki Kimoto authored on 2011-01-12
392
    foreach my $column (keys %$f) {
393
        my $fname = $f->{$column};
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
394
        if (!defined $fname) {
cleanup
Yuki Kimoto authored on 2011-01-12
395
            $f->{$column} = undef;
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
396
        }
397
        elsif (ref $fname ne 'CODE') {
many changed
Yuki Kimoto authored on 2011-01-23
398
          croak qq{Filter "$fname" is not registered"}
cleanup
Yuki Kimoto authored on 2010-12-21
399
            unless exists $self->filters->{$fname};
400
          
cleanup
Yuki Kimoto authored on 2011-01-12
401
          $f->{$column} = $self->filters->{$fname};
cleanup
Yuki Kimoto authored on 2010-12-21
402
        }
403
    }
cleanup
Yuki Kimoto authored on 2011-01-12
404
    $filter = {%$filter, %$f};
packaging one directory
yuki-kimoto authored on 2009-11-16
405
    
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
406
    # Bind
407
    my $bind = $self->_bind($params, $query->columns, $filter);
cleanup
yuki-kimoto authored on 2010-10-17
408
    
409
    # Execute
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
410
    my $sth = $query->sth;
cleanup
yuki-kimoto authored on 2010-10-17
411
    my $affected;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
412
    eval {$affected = $sth->execute(@$bind)};
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
413
    $self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@;
cleanup
yuki-kimoto authored on 2010-10-17
414
    
415
    # Return resultset if select statement is executed
416
    if ($sth->{NUM_OF_FIELDS}) {
417
        
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
418
        # Result in and end filter
419
        my $in_filter  = {};
420
        my $end_filter = {};
cleanup
Yuki Kimoto authored on 2011-01-12
421
        foreach my $table (@$tables) {
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
422
            next unless $table;
cleanup
Yuki Kimoto authored on 2011-01-12
423
            $in_filter = {
424
                %$in_filter,
425
                %{$self->{filter}{in}{$table} || {}}
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
426
            };
427
            $end_filter = {
428
                %$end_filter,
429
                %{$self->{filter}{end}{$table} || {}}
430
            };
cleanup
Yuki Kimoto authored on 2011-01-12
431
        }
432
        
433
        # Result
434
        my $result = $self->result_class->new(
cleanup
Yuki Kimoto authored on 2010-12-22
435
            sth            => $sth,
436
            filters        => $self->filters,
437
            filter_check   => $self->filter_check,
cleanup
Yuki Kimoto authored on 2011-01-12
438
            default_filter => $self->{default_in_filter},
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
439
            filter         => $in_filter || {},
440
            end_filter     => $end_filter || {}
cleanup
yuki-kimoto authored on 2010-10-17
441
        );
442

            
443
        return $result;
444
    }
445
    return $affected;
446
}
447

            
update pod
Yuki Kimoto authored on 2011-03-13
448
our %VALID_INSERT_ARGS
449
  = map { $_ => 1 } qw/table param append filter query/;
450

            
cleanup
yuki-kimoto authored on 2010-10-17
451
sub insert {
452
    my ($self, %args) = @_;
453

            
cleanup
Yuki Kimoto authored on 2011-03-09
454
    # Check argument names
cleanup
yuki-kimoto authored on 2010-10-17
455
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
456
        croak qq{Argument "$name" is invalid name}
cleanup
yuki-kimoto authored on 2010-10-17
457
          unless $VALID_INSERT_ARGS{$name};
packaging one directory
yuki-kimoto authored on 2009-11-16
458
    }
459
    
cleanup
yuki-kimoto authored on 2010-10-17
460
    # Arguments
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
461
    my $table  = $args{table};
462
    croak qq{"table" option must be specified} unless $table;
cleanup
yuki-kimoto authored on 2010-10-17
463
    my $param  = $args{param} || {};
464
    my $append = $args{append} || '';
465
    my $filter = $args{filter};
466
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
467
    # Columns
468
    my @columns;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
469
    my $safety = $self->safety_character;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
470
    foreach my $column (keys %$param) {
471
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
472
          unless $column =~ /^[$safety\.]+$/;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
473
        push @columns, $column;
474
    }
cleanup
yuki-kimoto authored on 2010-10-17
475
    
cleanup
Yuki Kimoto authored on 2011-01-27
476
    # SQL stack
477
    my @sql;
478
    
479
    # Insert
480
    push @sql, "insert into $table {insert_param ". join(' ', @columns) . '}';
481
    push @sql, $append if $append;
482
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
483
    # SQL
cleanup
Yuki Kimoto authored on 2011-01-27
484
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
485
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
486
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
487
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
488
    return $query if $args{query};
489
    
packaging one directory
yuki-kimoto authored on 2009-11-16
490
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
491
    my $ret_val = $self->execute(
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
492
        $query,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
493
        param  => $param,
494
        filter => $filter,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
495
        table => $table
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
496
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
497
    
498
    return $ret_val;
499
}
500

            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
501
our %VALID_INSERT_AT_ARGS
update pod
Yuki Kimoto authored on 2011-03-13
502
  = map { $_ => 1 } qw/table param where append filter
503
                       query primary_key param/;
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
504

            
505
sub insert_at {
506
    my ($self, %args) = @_;
507
    
cleanup
Yuki Kimoto authored on 2011-03-09
508
    # Check argument names
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
509
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
510
        croak qq{Argument "$name" is invalid name}
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
511
          unless $VALID_INSERT_AT_ARGS{$name};
512
    }
513
    
514
    # Primary key
515
    my $primary_keys = delete $args{primary_key};
516
    $primary_keys = [$primary_keys] unless ref $primary_keys;
517
    
518
    # Where clause
519
    my $where = {};
520
    my $param = {};
521
    
522
    if (exists $args{where}) {
523
        my $where_columns = delete $args{where};
524
        $where_columns = [$where_columns] unless ref $where_columns;
525

            
526
        croak qq{"where" must be constant value or array reference}
527
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
528
        
529
        for(my $i = 0; $i < @$primary_keys; $i ++) {
530
           $where->{$primary_keys->[$i]} = $where_columns->[$i];
531
        }
532
    }
533
    
534
    if (exists $args{param}) {
535
        $param = delete $args{param};
536
        for(my $i = 0; $i < @$primary_keys; $i ++) {
537
             delete $param->{$primary_keys->[$i]};
538
        }
539
    }
540
    
541
    $param = {%$param, %$where};
542
    
543
    return $self->insert(param => $param, %args);
544
}
545

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
546
sub insert_param {
547
    my ($self, $param) = @_;
548
    
update pod
Yuki Kimoto authored on 2011-03-13
549
    # Insert parameter tag
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
550
    my @tag;
551
    push @tag, '{insert_param';
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
552
    my $safety = $self->safety_character;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
553
    foreach my $column (keys %$param) {
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
554
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
555
          unless $column =~ /^[$safety\.]+$/;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
556
        push @tag, $column;
557
    }
558
    push @tag, '}';
559
    
560
    return join ' ', @tag;
561
}
562

            
pod fix
Yuki Kimoto authored on 2011-01-21
563
sub each_column {
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
564
    my ($self, $cb) = @_;
565
    
566
    # Iterate all tables
567
    my $sth_tables = $self->dbh->table_info;
568
    while (my $table_info = $sth_tables->fetchrow_hashref) {
569
        
570
        # Table
571
        my $table = $table_info->{TABLE_NAME};
572
        
573
        # Iterate all columns
574
        my $sth_columns = $self->dbh->column_info(undef, undef, $table, '%');
575
        while (my $column_info = $sth_columns->fetchrow_hashref) {
576
            my $column = $column_info->{COLUMN_NAME};
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
577
            $self->$cb($table, $column, $column_info);
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
578
        }
579
    }
580
}
581

            
added dbi_options attribute
kimoto authored on 2010-12-20
582
sub new {
583
    my $self = shift->SUPER::new(@_);
584
    
585
    # Check attribute names
586
    my @attrs = keys %$self;
587
    foreach my $attr (@attrs) {
588
        croak qq{"$attr" is invalid attribute name}
589
          unless $self->can($attr);
590
    }
cleanup
Yuki Kimoto authored on 2011-01-25
591

            
592
    $self->register_tag(
593
        '?'     => \&DBIx::Custom::Tag::placeholder,
594
        '='     => \&DBIx::Custom::Tag::equal,
595
        '<>'    => \&DBIx::Custom::Tag::not_equal,
596
        '>'     => \&DBIx::Custom::Tag::greater_than,
597
        '<'     => \&DBIx::Custom::Tag::lower_than,
598
        '>='    => \&DBIx::Custom::Tag::greater_than_equal,
599
        '<='    => \&DBIx::Custom::Tag::lower_than_equal,
600
        'like'  => \&DBIx::Custom::Tag::like,
601
        'in'    => \&DBIx::Custom::Tag::in,
602
        'insert_param' => \&DBIx::Custom::Tag::insert_param,
603
        'update_param' => \&DBIx::Custom::Tag::update_param
604
    );
added dbi_options attribute
kimoto authored on 2010-12-20
605
    
606
    return $self;
607
}
608

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
609
sub not_exists { bless {}, 'DBIx::Custom::NotExists' }
610

            
cleanup
yuki-kimoto authored on 2010-10-17
611
sub register_filter {
612
    my $invocant = shift;
613
    
614
    # Register filter
615
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
616
    $invocant->filters({%{$invocant->filters}, %$filters});
617
    
618
    return $invocant;
619
}
packaging one directory
yuki-kimoto authored on 2009-11-16
620

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
621
sub register_tag { shift->query_builder->register_tag(@_) }
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
622

            
refactoring select
yuki-kimoto authored on 2010-04-28
623
our %VALID_SELECT_ARGS
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
624
  = map { $_ => 1 } qw/table column where append relation filter query
625
                       selection join all_column/;
refactoring select
yuki-kimoto authored on 2010-04-28
626

            
packaging one directory
yuki-kimoto authored on 2009-11-16
627
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
628
    my ($self, %args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
629
    
cleanup
Yuki Kimoto authored on 2011-03-09
630
    # Check argument names
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
631
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
632
        croak qq{Argument "$name" is invalid name}
refactoring select
yuki-kimoto authored on 2010-04-28
633
          unless $VALID_SELECT_ARGS{$name};
634
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
635
    
refactoring select
yuki-kimoto authored on 2010-04-28
636
    # Arguments
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
637
    my $table = $args{table};
638
    my $tables = ref $table eq 'ARRAY' ? $table
639
               : defined $table ? [$table]
640
               : [];
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
641
    my $columns   = $args{column} || [];
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
642
    $columns = [$columns] unless ref $columns eq 'ARRAY';
643
    my $all_column = $args{all_column};
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
644
    my $selection = $args{selection} || '';
645
    my $where     = $args{where} || {};
646
    my $append    = $args{append};
647
    my $filter    = $args{filter};
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
648
    my $join =     $args{join} || [];
649
    croak qq{"join" must be array reference}
650
      unless ref $join eq 'ARRAY';
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
651
    
cleanup
Yuki Kimoto authored on 2011-03-09
652
    # Add relation tables(DEPRECATED!);
653
    $self->_add_relation_table($tables, $args{relation});
packaging one directory
yuki-kimoto authored on 2009-11-16
654
    
cleanup
Yuki Kimoto authored on 2011-01-27
655
    # SQL stack
656
    my @sql;
657
    push @sql, 'select';
packaging one directory
yuki-kimoto authored on 2009-11-16
658
    
cleanup
Yuki Kimoto authored on 2011-03-09
659
    # Selection
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
660
    if ($selection) { 
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
661
        push @sql, $selection;
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
662
        if ($selection =~ /from\s+(?:\{table\s+)?([^\s\{]+?)\b/) {
663
             unshift @$tables, $1;
664
        }
665
        unshift @$tables, @{$self->_tables($selection)};
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
666
    }
cleanup
Yuki Kimoto authored on 2011-03-09
667
    
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
668
    # Clumn clause, countains all columns of joined tables
669
    elsif ($all_column) {
670
    
671
        # Find tables
672
        my $main_table = $tables->[-1] || '';
673
        my %tables;
674
        foreach my $j (@$join) {
675
            my $tables = $self->_tables($j);
676
            foreach my $table (@$tables) {
677
                $tables{$table} = 1;
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
678
            }
679
        }
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
680
        delete $tables{$main_table};
681
        my @column_clause;
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
682
        
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
683
        # Column clause of main table
684
        push @sql, $self->model($main_table)->column_clause;
685
        push @sql, ',';
686
        
687
        # Column cluase of other tables
688
        foreach my $table (keys %tables) {
689
            unshift @$tables, $table;
690
            push @sql, $self->model($table)
691
                            ->column_clause(prefix => "${table}__");
692
            push @sql, ',';
693
        }
694
        pop @sql if $sql[-1] eq ',';
695
    }
696
    
697
    # Column clause
698
    elsif (@$columns) {
699
        foreach my $column (@$columns) {
700
            unshift @$tables, @{$self->_tables($column)};
701
            push @sql, ($column, ',');
702
        }
703
        pop @sql if $sql[-1] eq ',';
704
    }
705
    
706
    # "*" is default
707
    else { push @sql, '*' }
708
    
709
    # Table
710
    unless ($selection) {
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
711
        push @sql, 'from';
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
712
        if ($args{relation}) {
713
            my $found = {};
714
            foreach my $table (@$tables) {
715
                push @sql, ($table, ',') unless $found->{$table};
716
                $found->{$table} = 1;
717
            }
packaging one directory
yuki-kimoto authored on 2009-11-16
718
        }
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
719
        else { push @sql, $tables->[-1] }
720
        pop @sql if ($sql[-1] || '') eq ',';
packaging one directory
yuki-kimoto authored on 2009-11-16
721
    }
722
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
723
    # Main table
724
    croak "Not found table name" unless $tables->[-1];
725
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
726
    # Where
727
    my $w;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
728
    if (ref $where eq 'HASH') {
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
729
        my $clause = ['and'];
730
        push @$clause, "{= $_}" for keys %$where;
cleanup
Yuki Kimoto authored on 2011-03-09
731
        $w = $self->where(clause => $clause, param => $where);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
732
    }
733
    elsif (ref $where eq 'DBIx::Custom::Where') {
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
734
        $w = $where;
735
        $where = $w->param;
packaging one directory
yuki-kimoto authored on 2009-11-16
736
    }
737
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
738
    croak qq{"where" must be hash reference or DBIx::Custom::Where object}
739
      unless ref $w eq 'DBIx::Custom::Where';
740
    
741
    # String where
742
    my $swhere = "$w";
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
743
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
744
    # Add table names in where clause
745
    unshift @$tables, @{$self->_tables($swhere)};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
746
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
747
    # Push join
748
    $self->_push_join(\@sql, $join, $tables);
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
749
    
cleanup
Yuki Kimoto authored on 2011-03-09
750
    # Add where clause
cleanup
Yuki Kimoto authored on 2011-01-27
751
    push @sql, $swhere;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
752
    
cleanup
Yuki Kimoto authored on 2011-03-08
753
    # Relation(DEPRECATED!);
754
    $self->_push_relation(\@sql, $tables, $args{relation}, $swhere eq '' ? 1 : 0);
755
    
cleanup
Yuki Kimoto authored on 2011-01-27
756
    # Append statement
757
    push @sql, $append if $append;
758
    
759
    # SQL
760
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
761
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
762
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
763
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
764
    return $query if $args{query};
765
    
packaging one directory
yuki-kimoto authored on 2009-11-16
766
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
767
    my $result = $self->execute(
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
768
        $query, param  => $where, filter => $filter,
769
        table => $tables);
packaging one directory
yuki-kimoto authored on 2009-11-16
770
    
771
    return $result;
772
}
773

            
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
774
our %VALID_SELECT_AT_ARGS
775
  = map { $_ => 1 } qw/table column where append relation filter query selection
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
776
                       param primary_key join all_column/;
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
777

            
778
sub select_at {
779
    my ($self, %args) = @_;
780
    
cleanup
Yuki Kimoto authored on 2011-03-09
781
    # Check argument names
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
782
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
783
        croak qq{Argument "$name" is invalid name}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
784
          unless $VALID_SELECT_AT_ARGS{$name};
785
    }
786
    
787
    # Primary key
788
    my $primary_keys = delete $args{primary_key};
789
    $primary_keys = [$primary_keys] unless ref $primary_keys;
790
    
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
791
    # Table
792
    croak qq{"table" option must be specified} unless $args{table};
793
    my $table = ref $args{table} ? $args{table}->[-1] : $args{table};
794
    
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
795
    # Where clause
796
    my $where = {};
797
    if (exists $args{where}) {
798
        my $where_columns = delete $args{where};
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
799
        
800
        croak qq{"where" must be constant value or array reference}
801
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
802
        
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
803
        $where_columns = [$where_columns] unless ref $where_columns;
804
        
805
        for(my $i = 0; $i < @$primary_keys; $i ++) {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
806
           $where->{$table . '.' . $primary_keys->[$i]} = $where_columns->[$i];
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
807
        }
808
    }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
809
    
810
    if (exists $args{param}) {
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
811
        my $param = delete $args{param};
812
        for(my $i = 0; $i < @$primary_keys; $i ++) {
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
813
             delete $param->{$primary_keys->[$i]};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
814
        }
815
    }
816
    
817
    return $self->select(where => $where, %args);
818
}
819

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
820
sub model {
821
    my ($self, $name, $model) = @_;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
822
    
823
    # Set
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
824
    if ($model) {
add models() attribute
Yuki Kimoto authored on 2011-02-21
825
        $self->models->{$name} = $model;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
826
        return $self;
827
    }
828
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
829
    # Check model existance
830
    croak qq{Model "$name" is not included}
add models() attribute
Yuki Kimoto authored on 2011-02-21
831
      unless $self->models->{$name};
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
832
    
833
    # Get
add models() attribute
Yuki Kimoto authored on 2011-02-21
834
    return $self->models->{$name};
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
835
}
836

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
837
sub include_model {
838
    my ($self, $name_space, $model_infos) = @_;
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
839
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
840
    $name_space ||= '';
841
    unless ($model_infos) {
842
        # Load name space module
843
        croak qq{"$name_space" is invalid class name}
844
          if $name_space =~ /[^\w:]/;
845
        eval "use $name_space";
846
        croak qq{Name space module "$name_space.pm" is needed. $@} if $@;
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
847
        
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
848
        # Search model modules
849
        my $path = $INC{"$name_space.pm"};
850
        $path =~ s/\.pm$//;
851
        opendir my $dh, $path
852
          or croak qq{Can't open directory "$path": $!};
853
        $model_infos = [];
854
        while (my $module = readdir $dh) {
855
            push @$model_infos, $module
856
              if $module =~ s/\.pm$//;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
857
        }
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
858
        
859
        close $dh;
860
    }
861
    
862
    foreach my $model_info (@$model_infos) {
863
        
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
864
        # Model class, name, table
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
865
        my $model_class;
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
866
        my $model_name;
867
        my $model_table;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
868
        if (ref $model_info eq 'HASH') {
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
869
            $model_class = $model_info->{class};
870
            $model_name  = $model_info->{name};
871
            $model_table = $model_info->{table};
872
            
873
            $model_name  ||= $model_class;
874
            $model_table ||= $model_name;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
875
        }
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
876
        else { $model_class =$model_name = $model_table = $model_info }
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
877
        my $mclass = "${name_space}::$model_class";
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
878
        
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
879
        # Load
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
880
        croak qq{"$mclass" is invalid class name}
881
          if $mclass =~ /[^\w:]/;
882
        unless ($mclass->can('isa')) {
883
            eval "use $mclass";
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
884
            croak $@ if $@;
885
        }
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
886
        
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
887
        # Instantiate
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
888
        my $model = $mclass->new(dbi => $self);
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
889
        $model->name($model_name) unless $model->name;
890
        $model->table($model_table) unless $model->table;
removed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-28
891
        
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
892
        # Set
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
893
        $self->model($model->name, $model);
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
894
        
895
        # Apply filter
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
896
        croak "${name_space}::$model_class filter must be array reference"
897
          unless ref $model->filter eq 'ARRAY';
898
        $self->apply_filter($model->table, @{$model->filter});
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
899
    }
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
900
    return $self;
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
901
}
902

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
903
sub setup_model {
904
    my $self = shift;
905
    
906
    $self->each_column(
907
        sub {
908
            my ($self, $table, $column, $column_info) = @_;
909
            
910
            if (my $model = $self->models->{$table}) {
911
                push @{$model->columns}, $column;
912
            }
913
        }
914
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
915
    return $self;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
916
}
917

            
cleanup
yuki-kimoto authored on 2010-10-17
918
our %VALID_UPDATE_ARGS
update pod
Yuki Kimoto authored on 2011-03-13
919
  = map { $_ => 1 } qw/table param where append filter
920
                       allow_update_all query/;
cleanup
yuki-kimoto authored on 2010-10-17
921

            
922
sub update {
923
    my ($self, %args) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
924
    
cleanup
Yuki Kimoto authored on 2011-03-09
925
    # Check argument names
cleanup
yuki-kimoto authored on 2010-10-17
926
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
927
        croak qq{Argument "$name" is invalid name}
cleanup
yuki-kimoto authored on 2010-10-17
928
          unless $VALID_UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
929
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
930
    
cleanup
yuki-kimoto authored on 2010-10-17
931
    # Arguments
932
    my $table            = $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
933
    croak qq{"table" option must be specified} unless $table;
cleanup
yuki-kimoto authored on 2010-10-17
934
    my $param            = $args{param} || {};
935
    my $where            = $args{where} || {};
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
936
    my $append           = $args{append} || '';
cleanup
yuki-kimoto authored on 2010-10-17
937
    my $filter           = $args{filter};
938
    my $allow_update_all = $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
939
    
cleanup
yuki-kimoto authored on 2010-10-17
940
    # Update keys
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
941
    my @clumns = keys %$param;
942

            
943
    # Columns
944
    my @columns;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
945
    my $safety = $self->safety_character;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
946
    foreach my $column (keys %$param) {
947
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
948
          unless $column =~ /^[$safety\.]+$/;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
949
        push @columns, $column;
950
    }
951
        
cleanup
yuki-kimoto authored on 2010-10-17
952
    # Update clause
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
953
    my $update_clause = '{update_param ' . join(' ', @clumns) . '}';
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
954

            
955
    # Where
956
    my $w;
957
    if (ref $where eq 'HASH') {
958
        my $clause = ['and'];
959
        push @$clause, "{= $_}" for keys %$where;
960
        $w = $self->where;
961
        $w->clause($clause);
962
        $w->param($where);
963
    }
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
964
    elsif (ref $where eq 'DBIx::Custom::Where') {
965
        $w = $where;
966
        $where = $w->param;
967
    }  
removed experimental registe...
yuki-kimoto authored on 2010-08-24
968
    
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
969
    croak qq{"where" must be hash refernce or DBIx::Custom::Where object}
970
      unless ref $w eq 'DBIx::Custom::Where';
removed reconnect method
yuki-kimoto authored on 2010-05-28
971
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
972
    # String where
973
    my $swhere = "$w";
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
974
    
975
    croak qq{"where" must be specified}
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
976
      if "$swhere" eq '' && !$allow_update_all;
removed reconnect method
yuki-kimoto authored on 2010-05-28
977
    
cleanup
Yuki Kimoto authored on 2011-01-27
978
    # SQL stack
979
    my @sql;
980
    
981
    # Update
982
    push @sql, "update $table $update_clause $swhere";
983
    push @sql, $append if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
984
    
cleanup
yuki-kimoto authored on 2010-10-17
985
    # Rearrange parameters
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
986
    foreach my $wkey (keys %$where) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
987
        
cleanup
yuki-kimoto authored on 2010-10-17
988
        if (exists $param->{$wkey}) {
989
            $param->{$wkey} = [$param->{$wkey}]
990
              unless ref $param->{$wkey} eq 'ARRAY';
991
            
992
            push @{$param->{$wkey}}, $where->{$wkey};
993
        }
994
        else {
995
            $param->{$wkey} = $where->{$wkey};
996
        }
removed reconnect method
yuki-kimoto authored on 2010-05-28
997
    }
cleanup
yuki-kimoto authored on 2010-10-17
998
    
cleanup
Yuki Kimoto authored on 2011-01-27
999
    # SQL
1000
    my $sql = join(' ', @sql);
1001
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1002
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
1003
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1004
    return $query if $args{query};
1005
    
cleanup
yuki-kimoto authored on 2010-10-17
1006
    # Execute query
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1007
    my $ret_val = $self->execute($query, param  => $param, 
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1008
                                 filter => $filter,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1009
                                 table => $table);
cleanup
yuki-kimoto authored on 2010-10-17
1010
    
1011
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1012
}
1013

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

            
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1016
our %VALID_UPDATE_AT_ARGS
update pod
Yuki Kimoto authored on 2011-03-13
1017
  = map { $_ => 1 } qw/table param where append filter
1018
                       query primary_key param/;
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1019

            
1020
sub update_at {
1021
    my ($self, %args) = @_;
1022
    
cleanup
Yuki Kimoto authored on 2011-03-09
1023
    # Check argument names
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1024
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
1025
        croak qq{Argument "$name" is invalid name}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1026
          unless $VALID_UPDATE_AT_ARGS{$name};
1027
    }
1028
    
1029
    # Primary key
1030
    my $primary_keys = delete $args{primary_key};
1031
    $primary_keys = [$primary_keys] unless ref $primary_keys;
1032
    
1033
    # Where clause
1034
    my $where = {};
1035
    my $param = {};
1036
    
1037
    if (exists $args{where}) {
1038
        my $where_columns = delete $args{where};
1039
        $where_columns = [$where_columns] unless ref $where_columns;
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1040

            
1041
        croak qq{"where" must be constant value or array reference}
1042
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1043
        
1044
        for(my $i = 0; $i < @$primary_keys; $i ++) {
1045
           $where->{$primary_keys->[$i]} = $where_columns->[$i];
1046
        }
1047
    }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1048
    
1049
    if (exists $args{param}) {
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1050
        $param = delete $args{param};
1051
        for(my $i = 0; $i < @$primary_keys; $i ++) {
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1052
            delete $param->{$primary_keys->[$i]};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1053
        }
1054
    }
1055
    
1056
    return $self->update(where => $where, param => $param, %args);
1057
}
1058

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1059
sub update_param {
1060
    my ($self, $param) = @_;
1061
    
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1062
    # Update parameter tag
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1063
    my @tag;
1064
    push @tag, '{update_param';
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1065
    my $safety = $self->safety_character;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1066
    foreach my $column (keys %$param) {
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1067
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1068
          unless $column =~ /^[$safety\.]+$/;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1069
        push @tag, $column;
1070
    }
1071
    push @tag, '}';
1072
    
1073
    return join ' ', @tag;
1074
}
1075

            
cleanup
Yuki Kimoto authored on 2011-01-25
1076
sub where {
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1077
    my $self = shift;
1078

            
1079
    return DBIx::Custom::Where->new(
1080
        query_builder => $self->query_builder,
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1081
        safety_character => $self->safety_character,
cleanup
Yuki Kimoto authored on 2011-03-09
1082
        @_
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1083
    );
cleanup
Yuki Kimoto authored on 2011-01-25
1084
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1085

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1086
sub _bind {
cleanup
Yuki Kimoto authored on 2011-01-12
1087
    my ($self, $params, $columns, $filter) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1088
    
cleanup
Yuki Kimoto authored on 2011-01-12
1089
    # bind values
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1090
    my @bind;
add tests
yuki-kimoto authored on 2010-08-08
1091
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
1092
    # Build bind values
1093
    my $count = {};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1094
    my $not_exists = {};
cleanup
Yuki Kimoto authored on 2011-01-12
1095
    foreach my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1096
        
1097
        # Value
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1098
        my $value;
1099
        if(ref $params->{$column} eq 'ARRAY') {
1100
            my $i = $count->{$column} || 0;
1101
            $i += $not_exists->{$column} || 0;
1102
            my $found;
1103
            for (my $k = $i; $i < @{$params->{$column}}; $k++) {
1104
                if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
1105
                    $not_exists->{$column}++;
1106
                }
1107
                else  {
1108
                    $value = $params->{$column}->[$k];
1109
                    $found = 1;
1110
                    last
1111
                }
1112
            }
1113
            next unless $found;
1114
        }
1115
        else { $value = $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1116
        
cleanup
Yuki Kimoto authored on 2011-01-12
1117
        # Filter
1118
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
1119
        
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1120
        push @bind, $f ? $f->($value) : $value;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1121
        
1122
        # Count up 
1123
        $count->{$column}++;
1124
    }
1125
    
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1126
    return \@bind;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1127
}
1128

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1129
sub _connect {
1130
    my $self = shift;
1131
    
1132
    # Attributes
1133
    my $data_source = $self->data_source;
1134
    croak qq{"data_source" must be specified to connect()"}
1135
      unless $data_source;
1136
    my $user        = $self->user;
1137
    my $password    = $self->password;
1138
    my $dbi_option = {%{$self->dbi_options}, %{$self->dbi_option}};
1139
    
1140
    # Connect
1141
    my $dbh = eval {DBI->connect(
1142
        $data_source,
1143
        $user,
1144
        $password,
1145
        {
1146
            %{$self->default_dbi_option},
1147
            %$dbi_option
1148
        }
1149
    )};
1150
    
1151
    # Connect error
1152
    croak $@ if $@;
1153
    
1154
    return $dbh;
1155
}
1156

            
cleanup
yuki-kimoto authored on 2010-10-17
1157
sub _croak {
1158
    my ($self, $error, $append) = @_;
1159
    $append ||= "";
1160
    
1161
    # Verbose
1162
    if ($Carp::Verbose) { croak $error }
1163
    
1164
    # Not verbose
1165
    else {
1166
        
1167
        # Remove line and module infromation
1168
        my $at_pos = rindex($error, ' at ');
1169
        $error = substr($error, 0, $at_pos);
1170
        $error =~ s/\s+$//;
1171
        
1172
        croak "$error$append";
1173
    }
1174
}
1175

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1176
sub _need_tables {
1177
    my ($self, $tree, $need_tables, $tables) = @_;
1178
    
1179
    foreach my $table (@$tables) {
1180
        
1181
        if ($tree->{$table}) {
1182
            $need_tables->{$table} = 1;
1183
            $self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}])
1184
        }
1185
    }
1186
}
1187

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1188
sub _tables {
1189
    my ($self, $source) = @_;
1190
    
1191
    my $tables = [];
1192
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1193
    my $safety_character = $self->safety_character;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1194
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1195
    while ($source =~ /\b($safety_character+)\./g) {
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1196
        push @$tables, $1;
1197
    }
1198
    
1199
    return $tables;
1200
}
1201

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1202
sub _push_join {
1203
    my ($self, $sql, $join, $join_tables) = @_;
1204
    
1205
    return unless @$join;
1206
    
1207
    my $tree = {};
1208
    
1209
    for (my $i = 0; $i < @$join; $i++) {
1210
        
1211
        my $join_clause = $join->[$i];
1212
        
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-11
1213
        if ($join_clause =~ /\s([^\.\s]+?)\..+\s([^\.\s]+?)\..+?$/) {
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1214
            
1215
            my $table1 = $1;
1216
            my $table2 = $2;
1217
            
1218
            croak qq{right side table of "$join_clause" must be uniq}
1219
              if exists $tree->{$table2};
1220
            
1221
            $tree->{$table2}
1222
              = {position => $i, parent => $table1, join => $join_clause};
1223
        }
1224
        else {
1225
            croak qq{join "$join_clause" must be two table name};
1226
        }
1227
    }
1228
    
1229
    my $need_tables = {};
1230
    $self->_need_tables($tree, $need_tables, $join_tables);
1231
    
1232
    my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} } keys %$need_tables;
cleanup
Yuki Kimoto authored on 2011-03-08
1233

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1234
    foreach my $need_table (@need_tables) {
1235
        push @$sql, $tree->{$need_table}{join};
1236
    }
1237
}
cleanup
Yuki Kimoto authored on 2011-03-08
1238

            
cleanup
Yuki Kimoto authored on 2011-01-25
1239
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-23
1240
__PACKAGE__->attr(
1241
    dbi_options => sub { {} },
1242
    filter_check  => 1
1243
);
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1244

            
cleanup
Yuki Kimoto authored on 2011-01-25
1245
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1246
sub default_bind_filter {
1247
    my $self = shift;
1248
    
1249
    if (@_) {
1250
        my $fname = $_[0];
1251
        
1252
        if (@_ && !$fname) {
1253
            $self->{default_out_filter} = undef;
1254
        }
1255
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1256
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1257
              unless exists $self->filters->{$fname};
1258
        
1259
            $self->{default_out_filter} = $self->filters->{$fname};
1260
        }
1261
        return $self;
1262
    }
1263
    
1264
    return $self->{default_out_filter};
1265
}
1266

            
cleanup
Yuki Kimoto authored on 2011-01-25
1267
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1268
sub default_fetch_filter {
1269
    my $self = shift;
1270
    
1271
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
1272
        my $fname = $_[0];
1273

            
cleanup
Yuki Kimoto authored on 2011-01-12
1274
        if (@_ && !$fname) {
1275
            $self->{default_in_filter} = undef;
1276
        }
1277
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1278
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1279
              unless exists $self->filters->{$fname};
1280
        
1281
            $self->{default_in_filter} = $self->filters->{$fname};
1282
        }
1283
        
1284
        return $self;
1285
    }
1286
    
many changed
Yuki Kimoto authored on 2011-01-23
1287
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
1288
}
1289

            
cleanup
Yuki Kimoto authored on 2011-01-25
1290
# DEPRECATED!
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1291
sub register_tag_processor {
1292
    return shift->query_builder->register_tag_processor(@_);
1293
}
1294

            
cleanup
Yuki Kimoto authored on 2011-03-08
1295
# DEPRECATED!
1296
sub _push_relation {
1297
    my ($self, $sql, $tables, $relation, $need_where) = @_;
1298
    
1299
    if (keys %{$relation || {}}) {
1300
        push @$sql, $need_where ? 'where' : 'and';
1301
        foreach my $rcolumn (keys %$relation) {
1302
            my $table1 = (split (/\./, $rcolumn))[0];
1303
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1304
            push @$tables, ($table1, $table2);
1305
            push @$sql, ("$rcolumn = " . $relation->{$rcolumn},  'and');
1306
        }
1307
    }
1308
    pop @$sql if $sql->[-1] eq 'and';    
1309
}
1310

            
1311
# DEPRECATED!
1312
sub _add_relation_table {
cleanup
Yuki Kimoto authored on 2011-03-09
1313
    my ($self, $tables, $relation) = @_;
cleanup
Yuki Kimoto authored on 2011-03-08
1314
    
1315
    if (keys %{$relation || {}}) {
1316
        foreach my $rcolumn (keys %$relation) {
1317
            my $table1 = (split (/\./, $rcolumn))[0];
1318
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1319
            my $table1_exists;
1320
            my $table2_exists;
1321
            foreach my $table (@$tables) {
1322
                $table1_exists = 1 if $table eq $table1;
1323
                $table2_exists = 1 if $table eq $table2;
1324
            }
1325
            unshift @$tables, $table1 unless $table1_exists;
1326
            unshift @$tables, $table2 unless $table2_exists;
1327
        }
1328
    }
1329
}
1330

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1333
=head1 NAME
1334

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1335
DBIx::Custom - Useful database access, respecting SQL!
removed reconnect method
yuki-kimoto authored on 2010-05-28
1336

            
1337
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
1338

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1339
    use DBIx::Custom;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1340
    
1341
    # Connect
1342
    my $dbi = DBIx::Custom->connect(
1343
        data_source => "dbi:mysql:database=dbname",
1344
        user => 'ken',
1345
        password => '!LFKD%$&',
1346
        dbi_option => {mysql_enable_utf8 => 1}
1347
    );
cleanup
yuki-kimoto authored on 2010-08-05
1348

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1349
    # Insert 
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1350
    $dbi->insert(
1351
        table  => 'book',
1352
        param  => {title => 'Perl', author => 'Ken'}
1353
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1354
    
1355
    # Update 
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1356
    $dbi->update(
1357
        table  => 'book', 
1358
        param  => {title => 'Perl', author => 'Ken'}, 
1359
        where  => {id => 5},
1360
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1361
    
1362
    # Delete
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1363
    $dbi->delete(
1364
        table  => 'book',
1365
        where  => {author => 'Ken'},
1366
    );
cleanup
yuki-kimoto authored on 2010-08-05
1367

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1368
    # Select
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
1369
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1370
        table  => 'book',
update document
yuki-kimoto authored on 2010-05-27
1371
        where  => {author => 'Ken'},
added commit method
yuki-kimoto authored on 2010-05-27
1372
    );
cleanup
yuki-kimoto authored on 2010-08-05
1373

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1374
    # Select, more complex
1375
    my $result = $dbi->select(
1376
        table  => 'book',
1377
        column => [
1378
            'book.author as book__author',
1379
            'company.name as company__name'
1380
        ],
1381
        where  => {'book.author' => 'Ken'},
1382
        join => ['left outer join company on book.company_id = company.id'],
1383
        append => 'order by id limit 5'
removed reconnect method
yuki-kimoto authored on 2010-05-28
1384
    );
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1385
    
removed register_format()
yuki-kimoto authored on 2010-05-26
1386
    # Fetch
1387
    while (my $row = $result->fetch) {
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1388
        
removed register_format()
yuki-kimoto authored on 2010-05-26
1389
    }
1390
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1391
    # Fetch as hash
removed register_format()
yuki-kimoto authored on 2010-05-26
1392
    while (my $row = $result->fetch_hash) {
1393
        
1394
    }
1395
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1396
    # Execute SQL with parameter.
1397
    $dbi->execute(
1398
        "select id from book where {= author} and {like title}",
1399
        param  => {author => 'ken', title => '%Perl%'}
1400
    );
1401
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1402
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
1403

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1404
L<DBIx::Custom> is L<DBI> wrapper module.
1405

            
1406
=head1 FEATURES
removed reconnect method
yuki-kimoto authored on 2010-05-28
1407

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1408
=over 4
removed reconnect method
yuki-kimoto authored on 2010-05-28
1409

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1410
=item *
removed reconnect method
yuki-kimoto authored on 2010-05-28
1411

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1412
There are many basic methods to execute various queries.
1413
C<insert()>, C<update()>, C<update_all()>,C<delete()>,
1414
C<delete_all()>, C<select()>,
1415
C<insert_at()>, C<update_at()>, 
1416
C<delete_at()>, C<select_at()>, C<execute()>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1417

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1418
=item *
1419

            
1420
Filter when data is send or receive.
1421

            
1422
=item *
1423

            
1424
Data filtering system
1425

            
1426
=item *
1427

            
1428
Model support.
1429

            
1430
=item *
1431

            
1432
Generate where clause dinamically.
1433

            
1434
=item *
1435

            
1436
Generate join clause dinamically.
1437

            
1438
=back
pod fix
Yuki Kimoto authored on 2011-01-21
1439

            
1440
=head1 GUIDE
1441

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1442
L<DBIx::Custom::Guide> - L<DBIx::Custom> Guide
pod fix
Yuki Kimoto authored on 2011-01-21
1443

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1444
=head1 Wiki
pod fix
Yuki Kimoto authored on 2011-01-21
1445

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1446
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki>
updated document
yuki-kimoto authored on 2010-08-08
1447

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

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1455
Enable caching L<DBIx::Custom::Query>,
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1456
default to 1.
packaging one directory
yuki-kimoto authored on 2009-11-16
1457

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1463
Data source, used when C<connect()> is executed.
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1464

            
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1465
=head2 C<dbi_option>
added dbi_options attribute
kimoto authored on 2010-12-20
1466

            
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1467
    my $dbi_option = $dbi->dbi_option;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1468
    $dbi           = $dbi->dbi_option($dbi_option);
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1469

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1470
L<DBI> option, used when C<connect()> is executed.
1471
Each value in option override the value of C<default_dbi_option>.
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1472

            
1473
=head2 C<default_dbi_option>
1474

            
1475
    my $default_dbi_option = $dbi->default_dbi_option;
1476
    $dbi            = $dbi->default_dbi_option($default_dbi_option);
1477

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1478
L<DBI> default option, used when C<connect()> is executed,
1479
default to the following values.
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1480

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1481
    {
1482
        RaiseError => 1,
1483
        PrintError => 0,
1484
        AutoCommit => 1,
1485
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
1486

            
update pod
Yuki Kimoto authored on 2011-03-13
1487
You should not change C<AutoCommit> value directly,
1488
the value is used to check if the process is in transaction.
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1489

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1495
Filters, registered by C<register_filter()>.
add models() attribute
Yuki Kimoto authored on 2011-02-21
1496

            
update pod
Yuki Kimoto authored on 2011-03-13
1497
=head2 C<models> EXPERIMENTAL
add models() attribute
Yuki Kimoto authored on 2011-02-21
1498

            
1499
    my $models = $dbi->models;
1500
    $dbi       = $dbi->models(\%models);
1501

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1502
Models, included by C<include_model()>.
add models() attribute
Yuki Kimoto authored on 2011-02-21
1503

            
cleanup
yuki-kimoto authored on 2010-10-17
1504
=head2 C<password>
1505

            
1506
    my $password = $dbi->password;
1507
    $dbi         = $dbi->password('lkj&le`@s');
1508

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1509
Password, used when C<connect()> is executed.
update document
yuki-kimoto authored on 2010-01-30
1510

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1516
Query builder, default to L<DBIx::Custom::QueryBuilder> object.
cleanup
yuki-kimoto authored on 2010-08-05
1517

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1523
Result class, default to L<DBIx::Custom::Result>.
cleanup
yuki-kimoto authored on 2010-08-05
1524

            
update pod
Yuki Kimoto authored on 2011-03-13
1525
=head2 C<safety_character> EXPERIMENTAL
update pod
Yuki Kimoto authored on 2011-01-27
1526

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1527
    my $safety_character = $self->safety_character;
cleanup
Yuki Kimoto authored on 2011-03-10
1528
    $dbi                 = $self->safety_character($character);
update pod
Yuki Kimoto authored on 2011-01-27
1529

            
update pod
Yuki Kimoto authored on 2011-03-13
1530
Regex of safety character for table and column name, default to '\w'.
cleanup
Yuki Kimoto authored on 2011-03-10
1531
Note that you don't have to specify like '[\w]'.
update pod
Yuki Kimoto authored on 2011-01-27
1532

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1535
    my $user = $dbi->user;
1536
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1537

            
cleanup
Yuki Kimoto authored on 2011-03-10
1538
User name, used when C<connect()> is executed.
update pod
Yuki Kimoto authored on 2011-01-27
1539

            
cleanup
yuki-kimoto authored on 2010-10-17
1540
=head1 METHODS
added commit method
yuki-kimoto authored on 2010-05-27
1541

            
cleanup
yuki-kimoto authored on 2010-10-17
1542
L<DBIx::Custom> inherits all methods from L<Object::Simple>
cleanup
Yuki Kimoto authored on 2011-03-10
1543
and use all methods of L<DBI>
cleanup
yuki-kimoto authored on 2010-10-17
1544
and implements the following new ones.
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1545

            
update pod
Yuki Kimoto authored on 2011-03-13
1546
=head2 C<apply_filter> EXPERIMENTAL
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1547

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1548
    $dbi->apply_filter(
cleanup
Yuki Kimoto authored on 2011-03-10
1549
        'book',
update pod
Yuki Kimoto authored on 2011-03-13
1550
        'issue_date' => {
1551
            out => 'tp_to_date',
1552
            in  => 'date_to_tp',
1553
            end => 'tp_to_displaydate'
1554
        },
1555
        'write_date' => {
1556
            out => 'tp_to_date',
1557
            in  => 'date_to_tp',
1558
            end => 'tp_to_displaydate'
1559
        }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1560
    );
1561

            
update pod
Yuki Kimoto authored on 2011-03-13
1562
Apply filter to columns.
1563
C<out> filter is executed before data is send to database.
1564
C<in> filter is executed after a row is fetch.
1565
C<end> filter is execute after C<in> filter is executed.
1566

            
1567
Filter is applied to the follwoing tree column name pattern.
cleanup
Yuki Kimoto authored on 2010-12-21
1568

            
update pod
Yuki Kimoto authored on 2011-03-13
1569
       PETTERN         EXAMPLE
1570
    1. Column        : author
1571
    2. Table.Column  : book.author
1572
    3. Table__Column : book__author
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1573

            
update pod
Yuki Kimoto authored on 2011-03-13
1574
If column name is duplicate with other table,
1575
Main filter specified by C<table> option is used.
1576

            
1577
You can set multiple filters at once.
1578

            
1579
    $dbi->apply_filter(
1580
        'book',
1581
        [qw/issue_date write_date/] => {
1582
            out => 'tp_to_date',
1583
            in  => 'date_to_tp',
1584
            end => 'tp_to_displaydate'
1585
        }
1586
    );
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1587

            
update pod
Yuki Kimoto authored on 2011-03-13
1588
=head2 C<cache_method>
1589

            
1590
    $dbi          = $dbi->cache_method(\&cache_method);
1591
    $cache_method = $dbi->cache_method
1592

            
update pod
Yuki Kimoto authored on 2011-03-13
1593
Method to set and get cache.
1594
Default to the following one.
1595

            
1596
    sub {
1597
        my $self = shift;
1598
        
1599
        $self->{_cached} ||= {};
1600
        
1601
        if (@_ > 1) {
1602
            $self->{_cached}{$_[0]} = $_[1];
1603
        }
1604
        else {
1605
            return $self->{_cached}{$_[0]};
1606
        }
1607
    }
update pod
Yuki Kimoto authored on 2011-03-13
1608

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1611
    my $dbi = DBIx::Custom->connect(
1612
        data_source => "dbi:mysql:database=dbname",
1613
        user => 'ken',
1614
        password => '!LFKD%$&',
1615
        dbi_option => {mysql_enable_utf8 => 1}
1616
    );
1617

            
1618
Connect to the database and create a new L<DBIx::Custom> object.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1619

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1620
L<DBIx::Custom> is a wrapper of L<DBI>.
cleanup
yuki-kimoto authored on 2010-08-09
1621
C<AutoCommit> and C<RaiseError> options are true, 
update pod
Yuki Kimoto authored on 2011-03-13
1622
and C<PrintError> option is false by default.
packaging one directory
yuki-kimoto authored on 2009-11-16
1623

            
cleanup
yuki-kimoto authored on 2010-10-17
1624
=head2 C<create_query>
1625
    
1626
    my $query = $dbi->create_query(
update pod
Yuki Kimoto authored on 2011-03-13
1627
        "insert into book {insert_param title author};";
cleanup
yuki-kimoto authored on 2010-10-17
1628
    );
update document
yuki-kimoto authored on 2009-11-19
1629

            
update pod
Yuki Kimoto authored on 2011-03-13
1630
Create L<DBIx::Custom::Query> object.
1631

            
cleanup
yuki-kimoto authored on 2010-10-17
1632
If you want to get high performance,
update pod
Yuki Kimoto authored on 2011-03-13
1633
create L<DBIx::Custom::Query> object and execute the query by C<execute()>
1634
instead of other methods, such as C<insert>, C<update>.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1635

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

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1638
=head2 C<dbh>
1639

            
1640
    my $dbh = $dbi->dbh;
1641
    $dbi    = $dbi->dbh($dbh);
1642

            
1643
Get and set database handle of L<DBI>.
1644

            
update pod
Yuki Kimoto authored on 2011-03-13
1645
If process is spawn by forking, new connection is created automatically.
1646
This feature is EXPERIMETNAL.
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1647

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1650
    my $result = $dbi->execute(
1651
        "select * from book where {= title} and {like author}",
1652
        param => {title => 'Perl', author => '%Ken%'}
1653
    );
1654

            
1655
Execute SQL, containing tags.
1656
Return value is L<DBIx::Custom::Result> in select statement, or
1657
the count of affected rows in insert, update, delete statement.
1658

            
1659
Tag is turned into the statement containing place holder
1660
before SQL is executed.
1661

            
1662
    select * from where title = ? and author like ?;
1663

            
1664
See also L<Tags/Tags>.
1665

            
1666
The following opitons are currently available.
1667

            
1668
=over 4
1669

            
1670
=item C<filter>
1671

            
1672
Filter, executed before data is send to database. This is array reference.
1673
Filter value is code reference or
1674
filter name registerd by C<register_filter()>.
1675

            
1676
    # Basic
1677
    $dbi->execute(
1678
        $sql,
1679
        filter => [
1680
            title  => sub { uc $_[0] }
1681
            author => sub { uc $_[0] }
1682
        ]
1683
    );
1684
    
1685
    # At once
1686
    $dbi->execute(
1687
        $sql,
1688
        filter => [
1689
            [qw/title author/]  => sub { uc $_[0] }
1690
        ]
1691
    );
1692
    
1693
    # Filter name
1694
    $dbi->execute(
1695
        $sql,
1696
        filter => [
1697
            title  => 'upper_case',
1698
            author => 'upper_case'
1699
        ]
1700
    );
1701

            
1702
These filters are added to the C<out> filters, set by C<apply_filter()>.
update document
yuki-kimoto authored on 2009-11-19
1703

            
update pod
Yuki Kimoto authored on 2011-03-13
1704
=back
version 0.0901
yuki-kimoto authored on 2009-12-17
1705

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1708
    $dbi->delete(table => 'book', where => {title => 'Perl'});
1709

            
1710
Delete statement.
1711

            
1712
The following opitons are currently available.
1713

            
update pod
Yuki Kimoto authored on 2011-03-13
1714
=over 4
1715

            
update pod
Yuki Kimoto authored on 2011-03-13
1716
=item C<table>
1717

            
1718
Table name.
1719

            
1720
    $dbi->delete(table => 'book');
1721

            
1722
=item C<where>
1723

            
1724
Where clause. This is hash reference or L<DBIx::Custom::Where> object.
1725
    
1726
    # Hash reference
1727
    $dbi->delete(where => {title => 'Perl'});
1728
    
1729
    # DBIx::Custom::Where object
1730
    my $where = $dbi->where(
1731
        clause => ['and', '{= author}', '{like title}'],
1732
        param  => {author => 'Ken', title => '%Perl%'}
1733
    );
1734
    $dbi->delete(where => $where);
1735

            
1736
=item C<append>
1737

            
1738
Append statement to last of SQL. This is string.
1739

            
1740
    $dbi->delete(append => 'order by title');
1741

            
1742
=item C<filter>
1743

            
1744
Filter, executed before data is send to database. This is array reference.
1745
Filter value is code reference or
1746
filter name registerd by C<register_filter()>.
1747

            
1748
    # Basic
1749
    $dbi->delete(
1750
        filter => [
1751
            title  => sub { uc $_[0] }
1752
            author => sub { uc $_[0] }
1753
        ]
1754
    );
1755
    
1756
    # At once
1757
    $dbi->delete(
1758
        filter => [
1759
            [qw/title author/]  => sub { uc $_[0] }
1760
        ]
1761
    );
1762
    
1763
    # Filter name
1764
    $dbi->delete(
1765
        filter => [
1766
            title  => 'upper_case',
1767
            author => 'upper_case'
1768
        ]
1769
    );
1770

            
1771
These filters are added to the C<out> filters, set by C<apply_filter()>.
1772

            
update pod
Yuki Kimoto authored on 2011-03-13
1773
=item C<query> EXPERIMENTAL
update pod
Yuki Kimoto authored on 2011-03-13
1774

            
1775
Get L<DBIx::Custom::Query> object instead of executing SQL.
1776
This is true or false value.
1777

            
1778
    my $query = $dbi->delete(query => 1);
1779

            
1780
You can check SQL.
1781

            
1782
    my $sql = $query->sql;
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1783

            
update pod
Yuki Kimoto authored on 2011-03-13
1784
=back
1785

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1790
Delete statement to delete all rows.
1791
Options is same as C<delete()>.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1792

            
update pod
Yuki Kimoto authored on 2011-03-13
1793
=head2 C<delete_at()> EXPERIMENTAL
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1794

            
update pod
Yuki Kimoto authored on 2011-03-13
1795
Delete statement, using primary key.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1796

            
1797
    $dbi->delete_at(
1798
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
1799
        primary_key => 'id',
1800
        where => '5'
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1801
    );
1802

            
update pod
Yuki Kimoto authored on 2011-03-13
1803
This method is same as C<delete()> exept that
1804
C<primary_key> is specified and C<where> is constant value or array refrence.
1805
all option of C<delete()> is available.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1806

            
update pod
Yuki Kimoto authored on 2011-03-13
1807
=over 4
1808

            
1809
=item C<primary_key>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1810

            
update pod
Yuki Kimoto authored on 2011-03-13
1811
Primary key. This is constant value or array reference.
1812
    
1813
    # Constant value
1814
    $dbi->delete(primary_key => 'id');
1815

            
1816
    # Array reference
1817
    $dbi->delete(primary_key => ['id1', 'id2' ]);
1818

            
1819
This is used to create where clause.
1820

            
update pod
Yuki Kimoto authored on 2011-03-13
1821
=item C<where>
update pod
Yuki Kimoto authored on 2011-03-13
1822

            
1823
Where clause, created from primary key information.
1824
This is constant value or array reference.
1825

            
1826
    # Constant value
1827
    $dbi->delete(where => 5);
1828

            
1829
    # Array reference
1830
    $dbi->delete(where => [3, 5]);
1831

            
1832
In first examle, the following SQL is created.
1833

            
1834
    delete from book where id = ?;
1835

            
1836
Place holder is set to 5.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1837

            
update pod
Yuki Kimoto authored on 2011-03-13
1838
=back
1839

            
cleanup
yuki-kimoto authored on 2010-10-17
1840
=head2 C<insert>
1841

            
update pod
Yuki Kimoto authored on 2011-03-13
1842
    $dbi->insert(
1843
        table  => 'book', 
1844
        param  => {title => 'Perl', author => 'Ken'}
1845
    );
1846

            
1847
Insert statement.
1848

            
1849
The following opitons are currently available.
1850

            
update pod
Yuki Kimoto authored on 2011-03-13
1851
=over 4
1852

            
update pod
Yuki Kimoto authored on 2011-03-13
1853
=item C<table>
1854

            
1855
Table name.
1856

            
1857
    $dbi->insert(table => 'book');
1858

            
1859
=item C<param>
1860

            
1861
Insert data. This is hash reference.
1862

            
1863
    $dbi->insert(param => {title => 'Perl'});
1864

            
1865
=item C<append>
1866

            
1867
Append statement to last of SQL. This is string.
1868

            
1869
    $dbi->insert(append => 'order by title');
1870

            
1871
=item C<filter>
1872

            
1873
Filter, executed before data is send to database. This is array reference.
1874
Filter value is code reference or
1875
filter name registerd by C<register_filter()>.
1876

            
1877
    # Basic
1878
    $dbi->insert(
1879
        filter => [
1880
            title  => sub { uc $_[0] }
1881
            author => sub { uc $_[0] }
1882
        ]
1883
    );
1884
    
1885
    # At once
1886
    $dbi->insert(
1887
        filter => [
1888
            [qw/title author/]  => sub { uc $_[0] }
1889
        ]
1890
    );
1891
    
1892
    # Filter name
1893
    $dbi->insert(
1894
        filter => [
1895
            title  => 'upper_case',
1896
            author => 'upper_case'
1897
        ]
1898
    );
1899

            
1900
These filters are added to the C<out> filters, set by C<apply_filter()>.
1901

            
update pod
Yuki Kimoto authored on 2011-03-13
1902
=item C<query> EXPERIMENTAL
update pod
Yuki Kimoto authored on 2011-03-13
1903

            
1904
Get L<DBIx::Custom::Query> object instead of executing SQL.
1905
This is true or false value.
1906

            
1907
    my $query = $dbi->insert(query => 1);
cleanup
yuki-kimoto authored on 2010-10-17
1908

            
update pod
Yuki Kimoto authored on 2011-03-13
1909
You can check SQL.
cleanup
yuki-kimoto authored on 2010-10-17
1910

            
update pod
Yuki Kimoto authored on 2011-03-13
1911
    my $sql = $query->sql;
1912

            
update pod
Yuki Kimoto authored on 2011-03-13
1913
=back
1914

            
1915
=head2 C<insert_at()> EXPERIMENTAL
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
1916

            
update pod
Yuki Kimoto authored on 2011-03-13
1917
Insert statement, using primary key.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
1918

            
1919
    $dbi->insert_at(
1920
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
1921
        primary_key => 'id',
1922
        where => '5',
1923
        param => {title => 'Perl'}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
1924
    );
1925

            
update pod
Yuki Kimoto authored on 2011-03-13
1926
This method is same as C<insert()> exept that
1927
C<primary_key> is specified and C<where> is constant value or array refrence.
1928
all option of C<insert()> is available.
1929

            
update pod
Yuki Kimoto authored on 2011-03-13
1930
=over 4
1931

            
1932
=item C<primary_key>
update pod
Yuki Kimoto authored on 2011-03-13
1933

            
1934
Primary key. This is constant value or array reference.
1935
    
1936
    # Constant value
1937
    $dbi->insert(primary_key => 'id');
1938

            
1939
    # Array reference
1940
    $dbi->insert(primary_key => ['id1', 'id2' ]);
1941

            
1942
This is used to create parts of insert data.
1943

            
update pod
Yuki Kimoto authored on 2011-03-13
1944
=item C<where>
update pod
Yuki Kimoto authored on 2011-03-13
1945

            
1946
Parts of Insert data, create from primary key information.
1947
This is constant value or array reference.
1948

            
1949
    # Constant value
1950
    $dbi->insert(where => 5);
1951

            
1952
    # Array reference
1953
    $dbi->insert(where => [3, 5]);
1954

            
1955
In first examle, the following SQL is created.
1956

            
1957
    insert into book (id, title) values (?, ?);
1958

            
1959
Place holders are set to 5 and 'Perl'.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
1960

            
update pod
Yuki Kimoto authored on 2011-03-13
1961
=back
1962

            
1963
=head2 C<insert_param> EXPERIMENTAL
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1964

            
1965
    my $insert_param = $dbi->insert_param({title => 'a', age => 2});
1966

            
1967
Create insert parameter tag.
1968

            
update pod
Yuki Kimoto authored on 2011-03-13
1969
    {insert_param title age}
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1970

            
update pod
Yuki Kimoto authored on 2011-03-13
1971
=head2 C<each_column> EXPERIMENTAL
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1972

            
pod fix
Yuki Kimoto authored on 2011-01-21
1973
    $dbi->each_column(
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1974
        sub {
update pod
Yuki Kimoto authored on 2011-03-13
1975
            my ($dbi, $table, $column, $column_info) = @_;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1976
            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1977
            my $type = $column_info->{TYPE_NAME};
pod fix
Yuki Kimoto authored on 2011-01-21
1978
            
1979
            if ($type eq 'DATE') {
1980
                # ...
1981
            }
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1982
        }
1983
    );
update pod
Yuki Kimoto authored on 2011-03-13
1984

            
1985
Iterate all column informations of all table from database.
1986
Argument is callback when one column is found.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1987
Callback receive four arguments, dbi object, table name,
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1988
column name and column information.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1989

            
update pod
Yuki Kimoto authored on 2011-03-13
1990
=head2 C<include_model> EXPERIMENTAL
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1991

            
update pod
Yuki Kimoto authored on 2011-03-13
1992
    $dbi->include_model('MyModel');
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1993

            
update pod
Yuki Kimoto authored on 2011-03-13
1994
Include models from specified namespace,
1995
the following layout is needed to include models.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1996

            
update pod
Yuki Kimoto authored on 2011-03-13
1997
    lib / MyModel.pm
1998
        / MyModel / book.pm
1999
                  / company.pm
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2000

            
update pod
Yuki Kimoto authored on 2011-03-13
2001
Name space module, extending L<DBIx::Custom::Model>.
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2002

            
update pod
Yuki Kimoto authored on 2011-03-13
2003
B<MyModel.pm>
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2004

            
2005
    package MyModel;
2006
    
2007
    use base 'DBIx::Custom::Model';
update pod
Yuki Kimoto authored on 2011-03-13
2008
    
2009
    1;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2010

            
update pod
Yuki Kimoto authored on 2011-03-13
2011
Model modules, extending name space module.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2012

            
update pod
Yuki Kimoto authored on 2011-03-13
2013
B<MyModel/book.pm>
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2014

            
update pod
Yuki Kimoto authored on 2011-03-13
2015
    package MyModel::book;
2016
    
2017
    use base 'MyModel';
2018
    
2019
    1;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2020

            
update pod
Yuki Kimoto authored on 2011-03-13
2021
B<MyModel/company.pm>
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2022

            
update pod
Yuki Kimoto authored on 2011-03-13
2023
    package MyModel::company;
2024
    
2025
    use base 'MyModel';
2026
    
2027
    1;
2028
    
2029
MyModel::book and MyModel::company is included by C<include_model()>.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2030

            
update pod
Yuki Kimoto authored on 2011-03-13
2031
You can get model object by C<model()>.
2032

            
2033
    my $book_model    = $dbi->model('book');
2034
    my $company_model = $dbi->model('company');
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2035

            
update pod
Yuki Kimoto authored on 2011-03-13
2036
See L<DBIx::Custom::Model> to know model features.
2037

            
2038
=head2 C<method> EXPERIMENTAL
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2039

            
2040
    $dbi->method(
2041
        update_or_insert => sub {
2042
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-03-13
2043
            
2044
            # Process
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2045
        },
2046
        find_or_create   => sub {
2047
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-03-13
2048
            
2049
            # Process
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2050
        }
2051
    );
2052

            
update pod
Yuki Kimoto authored on 2011-03-13
2053
Register method. These method is called directly from L<DBIx::Custom> object.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2054

            
2055
    $dbi->update_or_insert;
2056
    $dbi->find_or_create;
2057

            
update pod
Yuki Kimoto authored on 2011-03-13
2058
=head2 C<model> EXPERIMENTAL
2059

            
2060
    $dbi->model('book')->method(
2061
        insert => sub { ... },
2062
        update => sub { ... }
2063
    );
2064
    
2065
    my $model = $dbi->model('book');
2066

            
2067
Set and get a L<DBIx::Custom::Model> object,
2068

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2069
=head2 C<new>
2070

            
update pod
Yuki Kimoto authored on 2011-03-13
2071
    my $dbi = DBIx::Custom->new(
2072
        data_source => "dbi:mysql:database=dbname",
2073
        user => 'ken',
2074
        password => '!LFKD%$&',
2075
        dbi_option => {mysql_enable_utf8 => 1}
2076
    );
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2077

            
2078
Create a new L<DBIx::Custom> object.
2079

            
update pod
Yuki Kimoto authored on 2011-03-13
2080
=head2 C<not_exists> EXPERIMENTAL
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2081

            
2082
    my $not_exists = $dbi->not_exists;
2083

            
update pod
Yuki Kimoto authored on 2011-03-13
2084
DBIx::Custom::NotExists object, indicating the column is not exists.
2085
This is used by C<clause> of L<DBIx::Custom::Where> .
experimental extended select...
Yuki Kimoto authored on 2011-01-17
2086

            
cleanup
yuki-kimoto authored on 2010-10-17
2087
=head2 C<register_filter>
2088

            
update pod
Yuki Kimoto authored on 2011-03-13
2089
    $dbi->register_filter(
2090
        # Time::Piece object to database DATE format
2091
        tp_to_date => sub {
2092
            my $tp = shift;
2093
            return $tp->strftime('%Y-%m-%d');
2094
        },
2095
        # database DATE format to Time::Piece object
2096
        date_to_tp => sub {
2097
           my $date = shift;
2098
           return Time::Piece->strptime($date, '%Y-%m-%d');
2099
        }
2100
    );
cleanup
yuki-kimoto authored on 2010-10-17
2101
    
update pod
Yuki Kimoto authored on 2011-03-13
2102
Register filters, used by C<filter> option of many methods.
cleanup
yuki-kimoto authored on 2010-10-17
2103

            
update pod
Yuki Kimoto authored on 2011-03-13
2104
=head2 C<register_tag>
cleanup
yuki-kimoto authored on 2010-10-17
2105

            
update pod
Yuki Kimoto authored on 2011-03-13
2106
    $dbi->register_tag(
2107
        update => sub {
2108
            my @columns = @_;
2109
            
2110
            # Update parameters
2111
            my $s = 'set ';
2112
            $s .= "$_ = ?, " for @columns;
2113
            $s =~ s/, $//;
2114
            
2115
            return [$s, \@columns];
2116
        }
2117
    );
cleanup
yuki-kimoto authored on 2010-10-17
2118

            
update pod
Yuki Kimoto authored on 2011-03-13
2119
Register tag, used by C<execute()>.
cleanup
yuki-kimoto authored on 2010-10-17
2120

            
update pod
Yuki Kimoto authored on 2011-03-13
2121
See also L<Tags/Tags> about tag registered by default.
cleanup
yuki-kimoto authored on 2010-10-17
2122

            
update pod
Yuki Kimoto authored on 2011-03-13
2123
Tag parser receive arguments specified in tag.
2124
In the following tag, 'title' and 'author' is parser arguments
cleanup
yuki-kimoto authored on 2010-10-17
2125

            
update pod
Yuki Kimoto authored on 2011-03-13
2126
    {update_param title author} 
cleanup
yuki-kimoto authored on 2010-10-17
2127

            
update pod
Yuki Kimoto authored on 2011-03-13
2128
Tag parser must return array refrence,
2129
first element is the result statement, 
2130
second element is column names corresponding to place holders.
cleanup
yuki-kimoto authored on 2010-10-17
2131

            
update pod
Yuki Kimoto authored on 2011-03-13
2132
In this example, result statement is 
cleanup
yuki-kimoto authored on 2010-10-17
2133

            
update pod
Yuki Kimoto authored on 2011-03-13
2134
    set title = ?, author = ?
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
2135

            
update pod
Yuki Kimoto authored on 2011-03-13
2136
Column names is
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
2137

            
update pod
Yuki Kimoto authored on 2011-03-13
2138
    ['title', 'author']
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
2139

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
2140
=head2 C<select>
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2141

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
2142
    my $result = $dbi->select(
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2143
        table  => 'book',
2144
        column => ['author', 'title'],
2145
        where  => {author => 'Ken'},
select method column option ...
Yuki Kimoto authored on 2011-02-22
2146
    );
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2147
    
update pod
Yuki Kimoto authored on 2011-03-12
2148
Select statement.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2149

            
2150
The following opitons are currently available.
2151

            
2152
=over 4
2153

            
2154
=item C<table>
2155

            
2156
Table name.
2157

            
update pod
Yuki Kimoto authored on 2011-03-12
2158
    $dbi->select(table => 'book');
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2159

            
2160
=item C<column>
2161

            
2162
Column clause. This is array reference or constant value.
2163

            
2164
    # Hash refernce
2165
    $dbi->select(column => ['author', 'title']);
2166
    
2167
    # Constant value
2168
    $dbi->select(column => 'author');
2169

            
2170
Default is '*' unless C<column> is specified.
2171

            
2172
    # Default
2173
    $dbi->select(column => '*');
2174

            
update pod
Yuki Kimoto authored on 2011-03-13
2175
=item C<all_column> EXPERIMENTAL
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2176

            
update pod
Yuki Kimoto authored on 2011-03-12
2177
Colum clause, contains all columns of joined table. This is true or false value
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2178

            
update pod
Yuki Kimoto authored on 2011-03-12
2179
    $dbi->select(all_column => 1);
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2180

            
2181
If main table is C<book> and joined table is C<company>,
2182
This create the following column clause.
2183

            
2184
    book.author as author
2185
    book.company_id as company_id
2186
    company.id as company__id
2187
    company.name as company__name
2188

            
2189
Columns of main table is consist of only column name,
2190
Columns of joined table is consist of table and column name joined C<__>.
2191

            
update pod
Yuki Kimoto authored on 2011-03-12
2192
Note that this option is failed unless L<DBIx::Custom::Model> object is set to
2193
C<model> and C<columns> of the object is set.
2194

            
2195
    # Generally do the following way before using all_column option
2196
    $dbi->include_model('MyModel')->setup_model;
2197

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2198
=item C<where>
2199

            
2200
Where clause. This is hash reference or L<DBIx::Custom::Where> object.
2201
    
2202
    # Hash reference
update pod
Yuki Kimoto authored on 2011-03-12
2203
    $dbi->select(where => {author => 'Ken', 'title' => 'Perl'});
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2204
    
update pod
Yuki Kimoto authored on 2011-03-12
2205
    # DBIx::Custom::Where object
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2206
    my $where = $dbi->where(
2207
        clause => ['and', '{= author}', '{like title}'],
2208
        param  => {author => 'Ken', title => '%Perl%'}
2209
    );
update pod
Yuki Kimoto authored on 2011-03-12
2210
    $dbi->select(where => $where);
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2211

            
update pod
Yuki Kimoto authored on 2011-03-13
2212
=item C<join> EXPERIMENTAL
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2213

            
update pod
Yuki Kimoto authored on 2011-03-12
2214
Join clause used in need. This is array reference.
2215

            
2216
    $dbi->select(join =>
2217
        [
2218
            'left outer join company on book.company_id = company_id',
2219
            'left outer join location on company.location_id = location.id'
2220
        ]
2221
    );
2222

            
2223
If column cluase or where clause contain table name like "company.name",
2224
needed join clause is used automatically.
2225

            
2226
    $dbi->select(
2227
        table => 'book',
2228
        column => ['company.location_id as company__location_id'],
2229
        where => {'company.name' => 'Orange'},
2230
        join => [
2231
            'left outer join company on book.company_id = company.id',
2232
            'left outer join location on company.location_id = location.id'
2233
        ]
2234
    );
2235

            
2236
In above select, the following SQL is created.
2237

            
2238
    select company.location_id as company__location_id
2239
    from book
2240
      left outer join company on book.company_id = company.id
2241
    where company.name = Orange
2242

            
2243
=item C<append>
2244

            
update pod
Yuki Kimoto authored on 2011-03-13
2245
Append statement to last of SQL. This is string.
update pod
Yuki Kimoto authored on 2011-03-12
2246

            
2247
    $dbi->select(append => 'order by title');
2248

            
2249
=item C<filter>
2250

            
update pod
Yuki Kimoto authored on 2011-03-13
2251
Filter, executed before data is send to database. This is array reference.
2252
Filter value is code reference or
update pod
Yuki Kimoto authored on 2011-03-12
2253
filter name registerd by C<register_filter()>.
2254

            
2255
    # Basic
2256
    $dbi->select(
2257
        filter => [
2258
            title  => sub { uc $_[0] }
2259
            author => sub { uc $_[0] }
2260
        ]
2261
    );
2262
    
2263
    # At once
2264
    $dbi->select(
2265
        filter => [
2266
            [qw/title author/]  => sub { uc $_[0] }
2267
        ]
2268
    );
2269
    
2270
    # Filter name
2271
    $dbi->select(
2272
        filter => [
2273
            title  => 'upper_case',
2274
            author => 'upper_case'
2275
        ]
2276
    );
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
2277

            
update pod
Yuki Kimoto authored on 2011-03-13
2278
These filters are added to the C<out> filters, set by C<apply_filter()>.
update document
yuki-kimoto authored on 2009-11-19
2279

            
update pod
Yuki Kimoto authored on 2011-03-13
2280
=item C<query> EXPERIMENTAL
cleanup
yuki-kimoto authored on 2010-08-09
2281

            
update pod
Yuki Kimoto authored on 2011-03-12
2282
Get L<DBIx::Custom::Query> object instead of executing SQL.
2283
This is true or false value.
2284

            
update pod
Yuki Kimoto authored on 2011-03-13
2285
    my $query = $dbi->select(query => 1);
update pod
Yuki Kimoto authored on 2011-03-12
2286

            
update pod
Yuki Kimoto authored on 2011-03-13
2287
You can check SQL.
update pod
Yuki Kimoto authored on 2011-03-12
2288

            
2289
    my $sql = $query->sql;
2290

            
2291
=back
cleanup
Yuki Kimoto authored on 2011-03-08
2292

            
update pod
Yuki Kimoto authored on 2011-03-13
2293
=head2 C<select_at()> EXPERIMENTAL
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2294

            
update pod
Yuki Kimoto authored on 2011-03-12
2295
Select statement, using primary key.
2296

            
2297
    $dbi->select_at(
2298
        table => 'book',
2299
        primary_key => 'id',
2300
        where => '5'
2301
    );
2302

            
update pod
Yuki Kimoto authored on 2011-03-13
2303
This method is same as C<select()> exept that
2304
C<primary_key> is specified and C<where> is constant value or array refrence.
update pod
Yuki Kimoto authored on 2011-03-12
2305
all option of C<select()> is available.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2306

            
update pod
Yuki Kimoto authored on 2011-03-13
2307
=over 4
2308

            
2309
=item C<primary_key>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2310

            
update pod
Yuki Kimoto authored on 2011-03-12
2311
Primary key. This is constant value or array reference.
2312
    
2313
    # Constant value
2314
    $dbi->select(primary_key => 'id');
2315

            
2316
    # Array reference
2317
    $dbi->select(primary_key => ['id1', 'id2' ]);
2318

            
update pod
Yuki Kimoto authored on 2011-03-13
2319
This is used to create where clause.
2320

            
update pod
Yuki Kimoto authored on 2011-03-13
2321
=item C<where>
update pod
Yuki Kimoto authored on 2011-03-12
2322

            
update pod
Yuki Kimoto authored on 2011-03-13
2323
Where clause, created from primary key information.
update pod
Yuki Kimoto authored on 2011-03-12
2324
This is constant value or array reference.
2325

            
2326
    # Constant value
2327
    $dbi->select(where => 5);
2328

            
2329
    # Array reference
2330
    $dbi->select(where => [3, 5]);
2331

            
2332
In first examle, the following SQL is created.
2333

            
2334
    select * from book where id = ?
2335

            
2336
Place holder is set to 5.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2337

            
update pod
Yuki Kimoto authored on 2011-03-13
2338
=back
2339

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2342
    $dbi->update(
2343
        table  => 'book',
2344
        param  => {title => 'Perl'},
2345
        where  => {id => 4}
2346
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
2347

            
update pod
Yuki Kimoto authored on 2011-03-13
2348
Update statement.
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2349

            
update pod
Yuki Kimoto authored on 2011-03-13
2350
The following opitons are currently available.
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2351

            
update pod
Yuki Kimoto authored on 2011-03-13
2352
=over 4
2353

            
update pod
Yuki Kimoto authored on 2011-03-13
2354
=item C<table>
2355

            
update pod
Yuki Kimoto authored on 2011-03-13
2356
Table name.
2357

            
2358
    $dbi->update(table => 'book');
2359

            
2360
=item C<param>
2361

            
2362
Update data. This is hash reference.
2363

            
2364
    $dbi->update(param => {title => 'Perl'});
2365

            
2366
=item C<where>
2367

            
2368
Where clause. This is hash reference or L<DBIx::Custom::Where> object.
2369
    
2370
    # Hash reference
2371
    $dbi->update(where => {author => 'Ken', 'title' => 'Perl'});
2372
    
2373
    # DBIx::Custom::Where object
2374
    my $where = $dbi->where(
2375
        clause => ['and', '{= author}', '{like title}'],
2376
        param  => {author => 'Ken', title => '%Perl%'}
2377
    );
2378
    $dbi->update(where => $where);
2379

            
2380
=item C<append>
2381

            
2382
Append statement to last of SQL. This is string.
2383

            
2384
    $dbi->update(append => 'order by title');
2385

            
2386
=item C<filter>
2387

            
2388
Filter, executed before data is send to database. This is array reference.
2389
Filter value is code reference or
2390
filter name registerd by C<register_filter()>.
2391

            
2392
    # Basic
2393
    $dbi->update(
2394
        filter => [
2395
            title  => sub { uc $_[0] }
2396
            author => sub { uc $_[0] }
2397
        ]
2398
    );
2399
    
2400
    # At once
2401
    $dbi->update(
2402
        filter => [
2403
            [qw/title author/]  => sub { uc $_[0] }
2404
        ]
2405
    );
2406
    
2407
    # Filter name
2408
    $dbi->update(
2409
        filter => [
2410
            title  => 'upper_case',
2411
            author => 'upper_case'
2412
        ]
2413
    );
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2414

            
update pod
Yuki Kimoto authored on 2011-03-13
2415
These filters are added to the C<out> filters, set by C<apply_filter()>.
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2416

            
update pod
Yuki Kimoto authored on 2011-03-13
2417
=item C<query> EXPERIMENTAL
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
2418

            
update pod
Yuki Kimoto authored on 2011-03-13
2419
Get L<DBIx::Custom::Query> object instead of executing SQL.
2420
This is true or false value.
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
2421

            
update pod
Yuki Kimoto authored on 2011-03-13
2422
    my $query = $dbi->update(query => 1);
2423

            
2424
You can check SQL.
2425

            
2426
    my $sql = $query->sql;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
2427

            
update pod
Yuki Kimoto authored on 2011-03-13
2428
=back
2429

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2432
    $dbi->update_all(table => 'book', param => {title => 'Perl'});
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
2433

            
update pod
Yuki Kimoto authored on 2011-03-13
2434
Update statement to update all rows.
2435
Options is same as C<update()>.
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
2436

            
update pod
Yuki Kimoto authored on 2011-03-13
2437
=head2 C<update_at()> EXPERIMENTAL
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2438

            
update pod
Yuki Kimoto authored on 2011-03-13
2439
Update statement, using primary key.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2440

            
2441
    $dbi->update_at(
2442
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
2443
        primary_key => 'id',
2444
        where => '5',
2445
        param => {title => 'Perl'}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2446
    );
2447

            
update pod
Yuki Kimoto authored on 2011-03-13
2448
This method is same as C<update()> exept that
2449
C<primary_key> is specified and C<where> is constant value or array refrence.
2450
all option of C<update()> is available.
2451

            
update pod
Yuki Kimoto authored on 2011-03-13
2452
=over 4
2453

            
2454
=item C<primary_key>
update pod
Yuki Kimoto authored on 2011-03-13
2455

            
2456
Primary key. This is constant value or array reference.
2457
    
2458
    # Constant value
2459
    $dbi->update(primary_key => 'id');
2460

            
2461
    # Array reference
2462
    $dbi->update(primary_key => ['id1', 'id2' ]);
2463

            
2464
This is used to create where clause.
2465

            
update pod
Yuki Kimoto authored on 2011-03-13
2466
=item C<where>
update pod
Yuki Kimoto authored on 2011-03-13
2467

            
2468
Where clause, created from primary key information.
2469
This is constant value or array reference.
2470

            
2471
    # Constant value
2472
    $dbi->update(where => 5);
2473

            
2474
    # Array reference
2475
    $dbi->update(where => [3, 5]);
2476

            
2477
In first examle, the following SQL is created.
2478

            
2479
    update book set title = ? where id = ?
2480

            
2481
Place holders are set to 'Perl' and 5.
2482

            
update pod
Yuki Kimoto authored on 2011-03-13
2483
=back
2484

            
2485
=head2 C<update_param> EXPERIMENTAL
update pod
Yuki Kimoto authored on 2011-03-13
2486

            
2487
    my $update_param = $dbi->update_param({title => 'a', age => 2});
2488

            
2489
Create update parameter tag.
2490

            
2491
    {update_param title age}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2492

            
update pod
Yuki Kimoto authored on 2011-03-13
2493
=head2 C<where> EXPERIMENTAL
fix tests
Yuki Kimoto authored on 2011-01-18
2494

            
cleanup
Yuki Kimoto authored on 2011-03-09
2495
    my $where = $dbi->where(
2496
        clause => ['and', '{= title}', '{= author}'],
2497
        param => {title => 'Perl', author => 'Ken'}
2498
    );
fix tests
Yuki Kimoto authored on 2011-01-18
2499

            
2500
Create a new L<DBIx::Custom::Where> object.
2501

            
update pod
Yuki Kimoto authored on 2011-03-13
2502
=head2 C<setup_model> EXPERIMENTAL
cleanup
Yuki Kimoto authored on 2011-01-12
2503

            
update pod
Yuki Kimoto authored on 2011-03-13
2504
    $dbi->setup_model;
cleanup
Yuki Kimoto authored on 2011-01-12
2505

            
update pod
Yuki Kimoto authored on 2011-03-13
2506
Setup all model objects.
update pod
Yuki Kimoto authored on 2011-03-13
2507
C<columns> of model object is automatically set, parsing database information.
cleanup
Yuki Kimoto authored on 2011-01-12
2508

            
cleanup
Yuki Kimoto authored on 2011-01-25
2509
=head1 Tags
2510

            
2511
The following tags is available.
2512

            
update pod
Yuki Kimoto authored on 2011-03-13
2513
=head2 C<table> EXPERIMENTAL
add table tag
Yuki Kimoto authored on 2011-02-09
2514

            
2515
Table tag
2516

            
2517
    {table TABLE}    ->    TABLE
2518

            
update pod
Yuki Kimoto authored on 2011-03-13
2519
This is used to tell C<execute()> what table is needed .
add table tag
Yuki Kimoto authored on 2011-02-09
2520

            
cleanup
Yuki Kimoto authored on 2011-01-25
2521
=head2 C<?>
2522

            
2523
Placeholder tag.
2524

            
2525
    {? NAME}    ->   ?
2526

            
2527
=head2 C<=>
2528

            
2529
Equal tag.
2530

            
2531
    {= NAME}    ->   NAME = ?
2532

            
2533
=head2 C<E<lt>E<gt>>
2534

            
2535
Not equal tag.
2536

            
2537
    {<> NAME}   ->   NAME <> ?
2538

            
2539
=head2 C<E<lt>>
2540

            
2541
Lower than tag
2542

            
2543
    {< NAME}    ->   NAME < ?
2544

            
2545
=head2 C<E<gt>>
2546

            
2547
Greater than tag
2548

            
2549
    {> NAME}    ->   NAME > ?
2550

            
2551
=head2 C<E<gt>=>
2552

            
2553
Greater than or equal tag
2554

            
2555
    {>= NAME}   ->   NAME >= ?
2556

            
2557
=head2 C<E<lt>=>
2558

            
2559
Lower than or equal tag
2560

            
2561
    {<= NAME}   ->   NAME <= ?
2562

            
2563
=head2 C<like>
2564

            
2565
Like tag
2566

            
2567
    {like NAME}   ->   NAME like ?
2568

            
2569
=head2 C<in>
2570

            
2571
In tag.
2572

            
2573
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
2574

            
2575
=head2 C<insert_param>
2576

            
2577
Insert parameter tag.
2578

            
2579
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
2580

            
2581
=head2 C<update_param>
2582

            
2583
Updata parameter tag.
2584

            
2585
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
2586

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
2589
L<DBIx::Custom> is stable. APIs keep backword compatible
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2590
except EXPERIMENTAL one in the feature.
DBIx::Custom is now stable
yuki-kimoto authored on 2010-09-07
2591

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

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

            
2596
C<< <kimoto.yuki at gmail.com> >>
2597

            
2598
L<http://github.com/yuki-kimoto/DBIx-Custom>
2599

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
2600
=head1 AUTHOR
2601

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

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
2606
Copyright 2009-2011 Yuki Kimoto, all rights reserved.
packaging one directory
yuki-kimoto authored on 2009-11-16
2607

            
2608
This program is free software; you can redistribute it and/or modify it
2609
under the same terms as Perl itself.
2610

            
2611
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
2612

            
2613