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

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
3
our $VERSION = '0.1664';
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

            
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
22
our @COMMON_ARGS = qw/table query filter type/;
cleanup
Yuki Kimoto authored on 2011-03-21
23

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
145
sub column {
146
    my ($self, $table, $columns) = @_;
added helper method
yuki-kimoto authored on 2010-10-17
147
    
cleanup
Yuki Kimoto authored on 2011-03-21
148
    $columns ||= [];
added helper method
yuki-kimoto authored on 2010-10-17
149
    
cleanup
Yuki Kimoto authored on 2011-03-21
150
    my @column;
151
    push @column, "$table.$_ as ${table}__$_" for @$columns;
152
    
153
    return join (', ', @column);
added helper method
yuki-kimoto authored on 2010-10-17
154
}
155

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

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

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
220
sub dbh {
221
    my $self = shift;
222

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
243
our %DELETE_ARGS
cleanup
Yuki Kimoto authored on 2011-03-21
244
  = map { $_ => 1 } @COMMON_ARGS, qw/where append allow_delete_all/;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
245

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

            
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
262
    # Where
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
263
    my $w = $self->_where($where);
264
    $where = $w->param;
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
265
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
266
    # String where
267
    my $swhere = "$w";
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
268
    
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
269
    croak qq{"where" must be specified}
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
270
      if $swhere eq '' && !$allow_delete_all;
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
271

            
cleanup
Yuki Kimoto authored on 2011-01-27
272
    # SQL stack
273
    my @sql;
274

            
275
    # Delete
276
    push @sql, "delete from $table $swhere";
277
    push @sql, $append if $append;
278
    
279
    my $sql = join(' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
280
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
281
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
282
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
283
    return $query if $args{query};
284
    
packaging one directory
yuki-kimoto authored on 2009-11-16
285
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
286
    my $ret_val = $self->execute(
cleanup
Yuki Kimoto authored on 2011-03-21
287
        $query,
288
        param  => $where,
289
        table => $table,
290
        %args
291
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
292
    
293
    return $ret_val;
294
}
295

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
298
our %DELETE_AT_ARGS = (%DELETE_ARGS, where => 1, primary_key => 1);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
299

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

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

            
added helper method
yuki-kimoto authored on 2010-10-17
338
sub DESTROY { }
339

            
cleanup
Yuki Kimoto authored on 2011-03-21
340
our %EXECUTE_ARGS = map { $_ => 1 } @COMMON_ARGS, 'param';
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
341

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

            
366
    # Organize tables
367
    my %table_set = map {defined $_ ? ($_ => 1) : ()} @$tables;
368
    my $main_table = pop @$tables;
369
    delete $table_set{$main_table} if $main_table;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
370
    foreach my $table (keys %table_set) {
371
        push @$tables, $table;
372
        
373
        if (my $dist = $self->{_table_alias}->{$table}) {
374
            $self->{filter} ||= {};
375
            
376
            unless ($self->{filter}{out}{$table}) {
377
                $self->{filter}{out} ||= {};
378
                $self->{filter}{in}  ||= {};
379
                $self->{filter}{end} ||= {};
380
                
381
                foreach my $type (qw/out in end/) {
382
                    
383
                    foreach my $filter_name (keys %{$self->{filter}{$type}{$dist} || {}}) {
384
                        my $filter_name_alias = $filter_name;
385
                        $filter_name_alias =~ s/^$dist\./$table\./;
386
                        $filter_name_alias =~ s/^${dist}__/${table}__/; 
387
                        
388
                        $self->{filter}{$type}{$table}{$filter_name_alias}
389
                          = $self->{filter}{$type}{$dist}{$filter_name}
390
                    }
391
                }
392
            }
393
        }
394
    }
395
    
cleanup
Yuki Kimoto authored on 2011-03-09
396
    $tables = [keys %table_set];
397
    push @$tables, $main_table if $main_table;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
398
    
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
399
    foreach my $table (@$tables) {
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
400
        next unless $table;
cleanup
Yuki Kimoto authored on 2011-01-12
401
        $filter = {
402
            %$filter,
403
            %{$self->{filter}{out}->{$table} || {}}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
404
        }
405
    }
406
    
cleanup
Yuki Kimoto authored on 2011-01-12
407
    # Filter argument
cleanup
Yuki Kimoto authored on 2011-03-21
408
    my $f = DBIx::Custom::Util::array_to_hash($args{filter})
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
409
         || $query->filter || {};
cleanup
Yuki Kimoto authored on 2011-01-12
410
    foreach my $column (keys %$f) {
411
        my $fname = $f->{$column};
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
412
        if (!defined $fname) {
cleanup
Yuki Kimoto authored on 2011-01-12
413
            $f->{$column} = undef;
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
414
        }
415
        elsif (ref $fname ne 'CODE') {
many changed
Yuki Kimoto authored on 2011-01-23
416
          croak qq{Filter "$fname" is not registered"}
cleanup
Yuki Kimoto authored on 2010-12-21
417
            unless exists $self->filters->{$fname};
418
          
cleanup
Yuki Kimoto authored on 2011-01-12
419
          $f->{$column} = $self->filters->{$fname};
cleanup
Yuki Kimoto authored on 2010-12-21
420
        }
421
    }
cleanup
Yuki Kimoto authored on 2011-01-12
422
    $filter = {%$filter, %$f};
packaging one directory
yuki-kimoto authored on 2009-11-16
423
    
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
424
    # Type
425
    my $type = DBIx::Custom::Util::array_to_hash($args{type});
426
    
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
427
    # Bind
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
428
    my $bind = $self->_bind($params, $query->columns, $filter, $type);
cleanup
yuki-kimoto authored on 2010-10-17
429
    
430
    # Execute
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
431
    my $sth = $query->sth;
cleanup
yuki-kimoto authored on 2010-10-17
432
    my $affected;
cleanup
Yuki Kimoto authored on 2011-03-21
433
    eval {
434
        for (my $i = 0; $i < @$bind; $i++) {
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
435
            if (my $type = $bind->[$i]->{type}) {
436
                $sth->bind_param($i + 1, $bind->[$i]->{value}, $type);
437
            }
438
            else {
439
                $sth->bind_param($i + 1, $bind->[$i]->{value});
440
            }
cleanup
Yuki Kimoto authored on 2011-03-21
441
        }
442
        $affected = $sth->execute;
443
    };
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
444
    $self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@;
cleanup
yuki-kimoto authored on 2010-10-17
445
    
446
    # Return resultset if select statement is executed
447
    if ($sth->{NUM_OF_FIELDS}) {
448
        
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
449
        # Result in and end filter
450
        my $in_filter  = {};
451
        my $end_filter = {};
cleanup
Yuki Kimoto authored on 2011-01-12
452
        foreach my $table (@$tables) {
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
453
            next unless $table;
cleanup
Yuki Kimoto authored on 2011-01-12
454
            $in_filter = {
455
                %$in_filter,
456
                %{$self->{filter}{in}{$table} || {}}
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
457
            };
458
            $end_filter = {
459
                %$end_filter,
460
                %{$self->{filter}{end}{$table} || {}}
461
            };
cleanup
Yuki Kimoto authored on 2011-01-12
462
        }
463
        
464
        # Result
465
        my $result = $self->result_class->new(
cleanup
Yuki Kimoto authored on 2010-12-22
466
            sth            => $sth,
467
            filters        => $self->filters,
468
            filter_check   => $self->filter_check,
cleanup
Yuki Kimoto authored on 2011-01-12
469
            default_filter => $self->{default_in_filter},
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
470
            filter         => $in_filter || {},
471
            end_filter     => $end_filter || {}
cleanup
yuki-kimoto authored on 2010-10-17
472
        );
473

            
474
        return $result;
475
    }
476
    return $affected;
477
}
478

            
cleanup
Yuki Kimoto authored on 2011-03-21
479
our %INSERT_ARGS = map { $_ => 1 } @COMMON_ARGS, qw/param append/;
update pod
Yuki Kimoto authored on 2011-03-13
480

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

            
cleanup
Yuki Kimoto authored on 2011-03-09
484
    # Check argument names
cleanup
yuki-kimoto authored on 2010-10-17
485
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
486
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
487
          unless $INSERT_ARGS{$name};
packaging one directory
yuki-kimoto authored on 2009-11-16
488
    }
489
    
cleanup
yuki-kimoto authored on 2010-10-17
490
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
491
    my $table  = delete $args{table};
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
492
    croak qq{"table" option must be specified} unless $table;
cleanup
Yuki Kimoto authored on 2011-03-21
493
    my $param  = delete $args{param} || {};
494
    my $append = delete $args{append} || '';
cleanup
yuki-kimoto authored on 2010-10-17
495
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
496
    # Columns
497
    my @columns;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
498
    my $safety = $self->safety_character;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
499
    foreach my $column (keys %$param) {
500
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
501
          unless $column =~ /^[$safety\.]+$/;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
502
        push @columns, $column;
503
    }
cleanup
yuki-kimoto authored on 2010-10-17
504
    
cleanup
Yuki Kimoto authored on 2011-01-27
505
    # SQL stack
506
    my @sql;
507
    
508
    # Insert
509
    push @sql, "insert into $table {insert_param ". join(' ', @columns) . '}';
510
    push @sql, $append if $append;
511
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
512
    # SQL
cleanup
Yuki Kimoto authored on 2011-01-27
513
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
514
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
515
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
516
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
517
    return $query if $args{query};
518
    
packaging one directory
yuki-kimoto authored on 2009-11-16
519
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
520
    my $ret_val = $self->execute(
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
521
        $query,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
522
        param  => $param,
cleanup
Yuki Kimoto authored on 2011-03-21
523
        table => $table,
524
        %args
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
525
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
526
    
527
    return $ret_val;
528
}
529

            
cleanup
Yuki Kimoto authored on 2011-03-21
530
our %INSERT_AT_ARGS = (%INSERT_ARGS, where => 1, primary_key => 1);
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
531

            
532
sub insert_at {
533
    my ($self, %args) = @_;
534
    
cleanup
Yuki Kimoto authored on 2011-03-09
535
    # Check argument names
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
536
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
537
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
538
          unless $INSERT_AT_ARGS{$name};
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
539
    }
540
    
541
    # Primary key
542
    my $primary_keys = delete $args{primary_key};
543
    $primary_keys = [$primary_keys] unless ref $primary_keys;
544
    
545
    # Where clause
546
    my $where = {};
547
    my $param = {};
548
    
549
    if (exists $args{where}) {
550
        my $where_columns = delete $args{where};
551
        $where_columns = [$where_columns] unless ref $where_columns;
552

            
553
        croak qq{"where" must be constant value or array reference}
554
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
555
        
556
        for(my $i = 0; $i < @$primary_keys; $i ++) {
557
           $where->{$primary_keys->[$i]} = $where_columns->[$i];
558
        }
559
    }
560
    
561
    if (exists $args{param}) {
562
        $param = delete $args{param};
563
        for(my $i = 0; $i < @$primary_keys; $i ++) {
564
             delete $param->{$primary_keys->[$i]};
565
        }
566
    }
567
    
568
    $param = {%$param, %$where};
569
    
570
    return $self->insert(param => $param, %args);
571
}
572

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
573
sub insert_param {
574
    my ($self, $param) = @_;
575
    
update pod
Yuki Kimoto authored on 2011-03-13
576
    # Insert parameter tag
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
577
    my @tag;
578
    push @tag, '{insert_param';
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
579
    my $safety = $self->safety_character;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
580
    foreach my $column (keys %$param) {
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
581
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
582
          unless $column =~ /^[$safety\.]+$/;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
583
        push @tag, $column;
584
    }
585
    push @tag, '}';
586
    
587
    return join ' ', @tag;
588
}
589

            
pod fix
Yuki Kimoto authored on 2011-01-21
590
sub each_column {
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
591
    my ($self, $cb) = @_;
592
    
593
    # Iterate all tables
594
    my $sth_tables = $self->dbh->table_info;
595
    while (my $table_info = $sth_tables->fetchrow_hashref) {
596
        
597
        # Table
598
        my $table = $table_info->{TABLE_NAME};
599
        
600
        # Iterate all columns
601
        my $sth_columns = $self->dbh->column_info(undef, undef, $table, '%');
602
        while (my $column_info = $sth_columns->fetchrow_hashref) {
603
            my $column = $column_info->{COLUMN_NAME};
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
604
            $self->$cb($table, $column, $column_info);
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
605
        }
606
    }
607
}
608

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
609
sub include_model {
610
    my ($self, $name_space, $model_infos) = @_;
611
    
612
    $name_space ||= '';
613
    unless ($model_infos) {
614
        # Load name space module
615
        croak qq{"$name_space" is invalid class name}
616
          if $name_space =~ /[^\w:]/;
617
        eval "use $name_space";
618
        croak qq{Name space module "$name_space.pm" is needed. $@} if $@;
619
        
620
        # Search model modules
621
        my $path = $INC{"$name_space.pm"};
622
        $path =~ s/\.pm$//;
623
        opendir my $dh, $path
624
          or croak qq{Can't open directory "$path": $!};
625
        $model_infos = [];
626
        while (my $module = readdir $dh) {
627
            push @$model_infos, $module
628
              if $module =~ s/\.pm$//;
629
        }
630
        
631
        close $dh;
632
    }
633
    
634
    my $table_alias = {};
635
    foreach my $model_info (@$model_infos) {
636
        
637
        # Model class, name, table
638
        my $model_class;
639
        my $model_name;
640
        my $model_table;
641
        if (ref $model_info eq 'HASH') {
642
            $model_class = $model_info->{class};
643
            $model_name  = $model_info->{name};
644
            $model_table = $model_info->{table};
645
            
646
            $model_name  ||= $model_class;
647
            $model_table ||= $model_name;
648
        }
649
        else { $model_class =$model_name = $model_table = $model_info }
650
        my $mclass = "${name_space}::$model_class";
651
        
652
        # Load
653
        croak qq{"$mclass" is invalid class name}
654
          if $mclass =~ /[^\w:]/;
655
        unless ($mclass->can('isa')) {
656
            eval "use $mclass";
657
            croak $@ if $@;
658
        }
659
        
660
        # Instantiate
661
        my $model = $mclass->new(dbi => $self);
662
        $model->name($model_name) unless $model->name;
663
        $model->table($model_table) unless $model->table;
664
        
665
        # Set
666
        $self->model($model->name, $model);
667
        
668
        # Apply filter
669
        croak "${name_space}::$model_class filter must be array reference"
670
          unless ref $model->filter eq 'ARRAY';
671
        $self->apply_filter($model->table, @{$model->filter});
672
        
673
        # Table alias
674
        $table_alias = {%$table_alias, %{$model->table_alias}};
675
        
676
        # Table - Model
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
677
        croak "Table name is duplicated"
678
          if exists $self->{_model_from}->{$model->table};
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
679
        $self->{_model_from}->{$model->table} = $model->name;
680
    }
681
    
682
    $self->{_table_alias} = $table_alias;
683
    
684
    return $self;
685
}
686

            
cleanup
Yuki Kimoto authored on 2011-03-21
687
sub method {
688
    my $self = shift;
689
    
690
    # Merge
691
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
692
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
693
    
694
    return $self;
695
}
696

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
697
sub model {
698
    my ($self, $name, $model) = @_;
699
    
700
    # Set
701
    if ($model) {
702
        $self->models->{$name} = $model;
703
        return $self;
704
    }
705
    
706
    # Check model existance
707
    croak qq{Model "$name" is not included}
708
      unless $self->models->{$name};
709
    
710
    # Get
711
    return $self->models->{$name};
712
}
713

            
cleanup
Yuki Kimoto authored on 2011-03-21
714
sub mycolumn {
715
    my ($self, $table, $columns) = @_;
716
    
717
    $columns ||= [];
718
    my @column;
719
    push @column, "$table.$_ as $_" for @$columns;
720
    
721
    return join (', ', @column);
722
}
723

            
added dbi_options attribute
kimoto authored on 2010-12-20
724
sub new {
725
    my $self = shift->SUPER::new(@_);
726
    
727
    # Check attribute names
728
    my @attrs = keys %$self;
729
    foreach my $attr (@attrs) {
730
        croak qq{"$attr" is invalid attribute name}
731
          unless $self->can($attr);
732
    }
cleanup
Yuki Kimoto authored on 2011-01-25
733

            
734
    $self->register_tag(
735
        '?'     => \&DBIx::Custom::Tag::placeholder,
736
        '='     => \&DBIx::Custom::Tag::equal,
737
        '<>'    => \&DBIx::Custom::Tag::not_equal,
738
        '>'     => \&DBIx::Custom::Tag::greater_than,
739
        '<'     => \&DBIx::Custom::Tag::lower_than,
740
        '>='    => \&DBIx::Custom::Tag::greater_than_equal,
741
        '<='    => \&DBIx::Custom::Tag::lower_than_equal,
742
        'like'  => \&DBIx::Custom::Tag::like,
743
        'in'    => \&DBIx::Custom::Tag::in,
744
        'insert_param' => \&DBIx::Custom::Tag::insert_param,
745
        'update_param' => \&DBIx::Custom::Tag::update_param
746
    );
added dbi_options attribute
kimoto authored on 2010-12-20
747
    
748
    return $self;
749
}
750

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

            
cleanup
yuki-kimoto authored on 2010-10-17
753
sub register_filter {
754
    my $invocant = shift;
755
    
756
    # Register filter
757
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
758
    $invocant->filters({%{$invocant->filters}, %$filters});
759
    
760
    return $invocant;
761
}
packaging one directory
yuki-kimoto authored on 2009-11-16
762

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
765
our %SELECT_ARGS
cleanup
Yuki Kimoto authored on 2011-03-21
766
  = map { $_ => 1 } @COMMON_ARGS, qw/column where append relation
767
                                     selection join/;
refactoring select
yuki-kimoto authored on 2010-04-28
768

            
packaging one directory
yuki-kimoto authored on 2009-11-16
769
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
770
    my ($self, %args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
771
    
cleanup
Yuki Kimoto authored on 2011-03-09
772
    # Check argument names
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
773
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
774
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
775
          unless $SELECT_ARGS{$name};
refactoring select
yuki-kimoto authored on 2010-04-28
776
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
777
    
refactoring select
yuki-kimoto authored on 2010-04-28
778
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
779
    my $table = delete $args{table};
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
780
    my $tables = ref $table eq 'ARRAY' ? $table
781
               : defined $table ? [$table]
782
               : [];
cleanup
Yuki Kimoto authored on 2011-03-21
783
    my $columns   = delete $args{column};
784
    my $selection = delete $args{selection} || '';
785
    my $where     = delete $args{where} || {};
786
    my $append    = delete $args{append};
787
    my $join      = delete $args{join} || [];
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
788
    croak qq{"join" must be array reference}
789
      unless ref $join eq 'ARRAY';
cleanup
Yuki Kimoto authored on 2011-03-21
790
    my $relation = delete $args{relation};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
791
    
cleanup
Yuki Kimoto authored on 2011-03-09
792
    # Add relation tables(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-03-21
793
    $self->_add_relation_table($tables, $relation);
packaging one directory
yuki-kimoto authored on 2009-11-16
794
    
cleanup
Yuki Kimoto authored on 2011-01-27
795
    # SQL stack
796
    my @sql;
797
    push @sql, 'select';
packaging one directory
yuki-kimoto authored on 2009-11-16
798
    
cleanup
Yuki Kimoto authored on 2011-03-09
799
    # Selection
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
800
    if ($selection) { 
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
801
        push @sql, $selection;
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
802
        if ($selection =~ /from\s+(?:\{table\s+)?([^\s\{]+?)\b/) {
803
             unshift @$tables, $1;
804
        }
805
        unshift @$tables, @{$self->_tables($selection)};
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
806
    }
cleanup
Yuki Kimoto authored on 2011-03-09
807
    
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
808
    # Column clause
809
    elsif ($columns) {
810

            
811
        $columns = [$columns] if ! ref $columns;
812
        
813
        if (ref $columns eq 'HASH') {
814
            # Find tables
815
            my $main_table;
816
            my %tables;
817
            if ($columns->{table}) {
818
                foreach my $table (@{$columns->{table}}) {
819
                    if (($table || '') eq $tables->[-1]) {
820
                        $main_table = $table;
821
                    }
822
                    else {
823
                        $tables{$table} = 1;
824
                    }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
825
                }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
826
            }
827
            elsif ($columns->{all}) {
828
                $main_table = $tables->[-1] || '';
829
                foreach my $j (@$join) {
830
                    my $tables = $self->_tables($j);
831
                    foreach my $table (@$tables) {
832
                        $tables{$table} = 1;
833
                    }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
834
                }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
835
                delete $tables{$main_table};
836
            }
837
            
838
            push @sql, $columns->{prepend} if $columns->{prepend};
839
            
840
            # Column clause of main table
841
            if ($main_table) {
cleanup
Yuki Kimoto authored on 2011-03-21
842
                push @sql, $self->model($main_table)->mycolumn;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
843
                push @sql, ',';
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
844
            }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
845
            
846
            # Column cluase of other tables
847
            foreach my $table (keys %tables) {
848
                unshift @$tables, $table;
cleanup
Yuki Kimoto authored on 2011-03-21
849
                push @sql, $self->model($table)->column($table);
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
850
                push @sql, ',';
851
            }
852
            pop @sql if $sql[-1] eq ',';
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
853
        }
854
        else {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
855
            foreach my $column (@$columns) {
856
                unshift @$tables, @{$self->_tables($column)};
857
                push @sql, ($column, ',');
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
858
            }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
859
            pop @sql if $sql[-1] eq ',';
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
860
        }
861
    }
862
    
863
    # "*" is default
864
    else { push @sql, '*' }
865
    
866
    # Table
867
    unless ($selection) {
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
868
        push @sql, 'from';
cleanup
Yuki Kimoto authored on 2011-03-21
869
        if ($relation) {
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
870
            my $found = {};
871
            foreach my $table (@$tables) {
872
                push @sql, ($table, ',') unless $found->{$table};
873
                $found->{$table} = 1;
874
            }
packaging one directory
yuki-kimoto authored on 2009-11-16
875
        }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
876
        else {
877
            my $main_table = $tables->[-1] || '';
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
878
            push @sql, $main_table;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
879
        }
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
880
        pop @sql if ($sql[-1] || '') eq ',';
packaging one directory
yuki-kimoto authored on 2009-11-16
881
    }
882
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
883
    # Main table
884
    croak "Not found table name" unless $tables->[-1];
885
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
886
    # Where
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
887
    my $w = $self->_where($where);
888
    $where = $w->param;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
889
    
890
    # String where
891
    my $swhere = "$w";
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
892
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
893
    # Add table names in where clause
894
    unshift @$tables, @{$self->_tables($swhere)};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
895
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
896
    # Push join
897
    $self->_push_join(\@sql, $join, $tables);
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
898
    
cleanup
Yuki Kimoto authored on 2011-03-09
899
    # Add where clause
cleanup
Yuki Kimoto authored on 2011-01-27
900
    push @sql, $swhere;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
901
    
cleanup
Yuki Kimoto authored on 2011-03-08
902
    # Relation(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-03-21
903
    $self->_push_relation(\@sql, $tables, $relation, $swhere eq '' ? 1 : 0);
cleanup
Yuki Kimoto authored on 2011-03-08
904
    
cleanup
Yuki Kimoto authored on 2011-01-27
905
    # Append statement
906
    push @sql, $append if $append;
907
    
908
    # SQL
909
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
910
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
911
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
912
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
913
    return $query if $args{query};
914
    
packaging one directory
yuki-kimoto authored on 2009-11-16
915
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
916
    my $result = $self->execute(
cleanup
Yuki Kimoto authored on 2011-03-21
917
        $query,
918
        param  => $where, 
919
        table => $tables,
920
        %args
921
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
922
    
923
    return $result;
924
}
925

            
cleanup
Yuki Kimoto authored on 2011-03-21
926
our %SELECT_AT_ARGS = (%SELECT_ARGS, where => 1, primary_key => 1);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
927

            
928
sub select_at {
929
    my ($self, %args) = @_;
930
    
cleanup
Yuki Kimoto authored on 2011-03-09
931
    # Check argument names
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
932
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
933
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
934
          unless $SELECT_AT_ARGS{$name};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
935
    }
936
    
937
    # Primary key
938
    my $primary_keys = delete $args{primary_key};
939
    $primary_keys = [$primary_keys] unless ref $primary_keys;
940
    
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
941
    # Table
942
    croak qq{"table" option must be specified} unless $args{table};
943
    my $table = ref $args{table} ? $args{table}->[-1] : $args{table};
944
    
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
945
    # Where clause
946
    my $where = {};
947
    if (exists $args{where}) {
948
        my $where_columns = delete $args{where};
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
949
        
950
        croak qq{"where" must be constant value or array reference}
951
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
952
        
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
953
        $where_columns = [$where_columns] unless ref $where_columns;
954
        
955
        for(my $i = 0; $i < @$primary_keys; $i ++) {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
956
           $where->{$table . '.' . $primary_keys->[$i]} = $where_columns->[$i];
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
957
        }
958
    }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
959
    
960
    if (exists $args{param}) {
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
961
        my $param = delete $args{param};
962
        for(my $i = 0; $i < @$primary_keys; $i ++) {
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
963
             delete $param->{$primary_keys->[$i]};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
964
        }
965
    }
966
    
967
    return $self->select(where => $where, %args);
968
}
969

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
970
sub setup_model {
971
    my $self = shift;
972
    
973
    $self->each_column(
974
        sub {
975
            my ($self, $table, $column, $column_info) = @_;
976
            
977
            if (my $model = $self->models->{$table}) {
978
                push @{$model->columns}, $column;
979
            }
980
        }
981
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
982
    return $self;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
983
}
984

            
cleanup
Yuki Kimoto authored on 2011-03-21
985
our %UPDATE_ARGS
986
  = map { $_ => 1 } @COMMON_ARGS, qw/param where append allow_update_all/;
cleanup
yuki-kimoto authored on 2010-10-17
987

            
988
sub update {
989
    my ($self, %args) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
990
    
cleanup
Yuki Kimoto authored on 2011-03-09
991
    # Check argument names
cleanup
yuki-kimoto authored on 2010-10-17
992
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
993
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
994
          unless $UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
995
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
996
    
cleanup
yuki-kimoto authored on 2010-10-17
997
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
998
    my $table = delete $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
999
    croak qq{"table" option must be specified} unless $table;
cleanup
Yuki Kimoto authored on 2011-03-21
1000
    my $param            = delete $args{param} || {};
1001
    my $where            = delete $args{where} || {};
1002
    my $append           = delete $args{append} || '';
1003
    my $allow_update_all = delete $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
1004
    
cleanup
yuki-kimoto authored on 2010-10-17
1005
    # Update keys
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1006
    my @clumns = keys %$param;
1007

            
1008
    # Columns
1009
    my @columns;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1010
    my $safety = $self->safety_character;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1011
    foreach my $column (keys %$param) {
1012
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1013
          unless $column =~ /^[$safety\.]+$/;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1014
        push @columns, $column;
1015
    }
1016
        
cleanup
yuki-kimoto authored on 2010-10-17
1017
    # Update clause
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1018
    my $update_clause = '{update_param ' . join(' ', @clumns) . '}';
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
1019

            
1020
    # Where
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1021
    my $w = $self->_where($where);
1022
    $where = $w->param;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1023
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1024
    # String where
1025
    my $swhere = "$w";
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
1026
    
1027
    croak qq{"where" must be specified}
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1028
      if "$swhere" eq '' && !$allow_update_all;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1029
    
cleanup
Yuki Kimoto authored on 2011-01-27
1030
    # SQL stack
1031
    my @sql;
1032
    
1033
    # Update
1034
    push @sql, "update $table $update_clause $swhere";
1035
    push @sql, $append if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1036
    
cleanup
yuki-kimoto authored on 2010-10-17
1037
    # Rearrange parameters
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
1038
    foreach my $wkey (keys %$where) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1039
        
cleanup
yuki-kimoto authored on 2010-10-17
1040
        if (exists $param->{$wkey}) {
1041
            $param->{$wkey} = [$param->{$wkey}]
1042
              unless ref $param->{$wkey} eq 'ARRAY';
1043
            
1044
            push @{$param->{$wkey}}, $where->{$wkey};
1045
        }
1046
        else {
1047
            $param->{$wkey} = $where->{$wkey};
1048
        }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1049
    }
cleanup
yuki-kimoto authored on 2010-10-17
1050
    
cleanup
Yuki Kimoto authored on 2011-01-27
1051
    # SQL
1052
    my $sql = join(' ', @sql);
1053
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1054
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
1055
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1056
    return $query if $args{query};
1057
    
cleanup
yuki-kimoto authored on 2010-10-17
1058
    # Execute query
cleanup
Yuki Kimoto authored on 2011-03-21
1059
    my $ret_val = $self->execute(
1060
        $query,
1061
        param  => $param, 
1062
        table => $table,
1063
        %args
1064
    );
cleanup
yuki-kimoto authored on 2010-10-17
1065
    
1066
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1067
}
1068

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
1071
our %UPDATE_AT_ARGS = (%UPDATE_ARGS, where => 1, primary_key => 1);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1072

            
1073
sub update_at {
1074
    my ($self, %args) = @_;
1075
    
cleanup
Yuki Kimoto authored on 2011-03-09
1076
    # Check argument names
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1077
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
1078
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
1079
          unless $UPDATE_AT_ARGS{$name};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1080
    }
1081
    
1082
    # Primary key
1083
    my $primary_keys = delete $args{primary_key};
1084
    $primary_keys = [$primary_keys] unless ref $primary_keys;
1085
    
1086
    # Where clause
1087
    my $where = {};
1088
    my $param = {};
1089
    
1090
    if (exists $args{where}) {
1091
        my $where_columns = delete $args{where};
1092
        $where_columns = [$where_columns] unless ref $where_columns;
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1093

            
1094
        croak qq{"where" must be constant value or array reference}
1095
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1096
        
1097
        for(my $i = 0; $i < @$primary_keys; $i ++) {
1098
           $where->{$primary_keys->[$i]} = $where_columns->[$i];
1099
        }
1100
    }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1101
    
1102
    if (exists $args{param}) {
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1103
        $param = delete $args{param};
1104
        for(my $i = 0; $i < @$primary_keys; $i ++) {
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1105
            delete $param->{$primary_keys->[$i]};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1106
        }
1107
    }
1108
    
1109
    return $self->update(where => $where, param => $param, %args);
1110
}
1111

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1112
sub update_param {
1113
    my ($self, $param) = @_;
1114
    
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1115
    # Update parameter tag
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1116
    my @tag;
1117
    push @tag, '{update_param';
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1118
    my $safety = $self->safety_character;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1119
    foreach my $column (keys %$param) {
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1120
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1121
          unless $column =~ /^[$safety\.]+$/;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1122
        push @tag, $column;
1123
    }
1124
    push @tag, '}';
1125
    
1126
    return join ' ', @tag;
1127
}
1128

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

            
1132
    return DBIx::Custom::Where->new(
1133
        query_builder => $self->query_builder,
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1134
        safety_character => $self->safety_character,
cleanup
Yuki Kimoto authored on 2011-03-09
1135
        @_
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1136
    );
cleanup
Yuki Kimoto authored on 2011-01-25
1137
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1138

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1139
sub _bind {
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1140
    my ($self, $params, $columns, $filter, $type) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1141
    
cleanup
Yuki Kimoto authored on 2011-01-12
1142
    # bind values
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1143
    my $bind = [];
add tests
yuki-kimoto authored on 2010-08-08
1144
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
1145
    # Build bind values
1146
    my $count = {};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1147
    my $not_exists = {};
cleanup
Yuki Kimoto authored on 2011-01-12
1148
    foreach my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1149
        
1150
        # Value
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1151
        my $value;
1152
        if(ref $params->{$column} eq 'ARRAY') {
1153
            my $i = $count->{$column} || 0;
1154
            $i += $not_exists->{$column} || 0;
1155
            my $found;
1156
            for (my $k = $i; $i < @{$params->{$column}}; $k++) {
1157
                if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
1158
                    $not_exists->{$column}++;
1159
                }
1160
                else  {
1161
                    $value = $params->{$column}->[$k];
1162
                    $found = 1;
1163
                    last
1164
                }
1165
            }
1166
            next unless $found;
1167
        }
1168
        else { $value = $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1169
        
cleanup
Yuki Kimoto authored on 2011-01-12
1170
        # Filter
1171
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
1172
        
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1173
        # Type
1174
        push @$bind, {
1175
            value => $f ? $f->($value) : $value,
1176
            type => $type->{$column}
1177
        };
removed reconnect method
yuki-kimoto authored on 2010-05-28
1178
        
1179
        # Count up 
1180
        $count->{$column}++;
1181
    }
1182
    
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1183
    return $bind;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1184
}
1185

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1186
sub _connect {
1187
    my $self = shift;
1188
    
1189
    # Attributes
1190
    my $data_source = $self->data_source;
1191
    croak qq{"data_source" must be specified to connect()"}
1192
      unless $data_source;
1193
    my $user        = $self->user;
1194
    my $password    = $self->password;
1195
    my $dbi_option = {%{$self->dbi_options}, %{$self->dbi_option}};
1196
    
1197
    # Connect
1198
    my $dbh = eval {DBI->connect(
1199
        $data_source,
1200
        $user,
1201
        $password,
1202
        {
1203
            %{$self->default_dbi_option},
1204
            %$dbi_option
1205
        }
1206
    )};
1207
    
1208
    # Connect error
1209
    croak $@ if $@;
1210
    
1211
    return $dbh;
1212
}
1213

            
cleanup
yuki-kimoto authored on 2010-10-17
1214
sub _croak {
1215
    my ($self, $error, $append) = @_;
1216
    $append ||= "";
1217
    
1218
    # Verbose
1219
    if ($Carp::Verbose) { croak $error }
1220
    
1221
    # Not verbose
1222
    else {
1223
        
1224
        # Remove line and module infromation
1225
        my $at_pos = rindex($error, ' at ');
1226
        $error = substr($error, 0, $at_pos);
1227
        $error =~ s/\s+$//;
1228
        
1229
        croak "$error$append";
1230
    }
1231
}
1232

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1233
sub _need_tables {
1234
    my ($self, $tree, $need_tables, $tables) = @_;
1235
    
1236
    foreach my $table (@$tables) {
1237
        
1238
        if ($tree->{$table}) {
1239
            $need_tables->{$table} = 1;
1240
            $self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}])
1241
        }
1242
    }
1243
}
1244

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1245
sub _tables {
1246
    my ($self, $source) = @_;
1247
    
1248
    my $tables = [];
1249
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1250
    my $safety_character = $self->safety_character;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1251
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1252
    while ($source =~ /\b($safety_character+)\./g) {
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1253
        push @$tables, $1;
1254
    }
1255
    
1256
    return $tables;
1257
}
1258

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1259
sub _push_join {
1260
    my ($self, $sql, $join, $join_tables) = @_;
1261
    
1262
    return unless @$join;
1263
    
1264
    my $tree = {};
1265
    
1266
    for (my $i = 0; $i < @$join; $i++) {
1267
        
1268
        my $join_clause = $join->[$i];
1269
        
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-11
1270
        if ($join_clause =~ /\s([^\.\s]+?)\..+\s([^\.\s]+?)\..+?$/) {
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1271
            
1272
            my $table1 = $1;
1273
            my $table2 = $2;
1274
            
1275
            croak qq{right side table of "$join_clause" must be uniq}
1276
              if exists $tree->{$table2};
1277
            
1278
            $tree->{$table2}
1279
              = {position => $i, parent => $table1, join => $join_clause};
1280
        }
1281
        else {
1282
            croak qq{join "$join_clause" must be two table name};
1283
        }
1284
    }
1285
    
1286
    my $need_tables = {};
1287
    $self->_need_tables($tree, $need_tables, $join_tables);
1288
    
1289
    my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} } keys %$need_tables;
cleanup
Yuki Kimoto authored on 2011-03-08
1290

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1291
    foreach my $need_table (@need_tables) {
1292
        push @$sql, $tree->{$need_table}{join};
1293
    }
1294
}
cleanup
Yuki Kimoto authored on 2011-03-08
1295

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1296
sub _where {
1297
    my ($self, $where) = @_;
1298
    
1299
    my $w;
1300
    if (ref $where eq 'HASH') {
1301
        my $clause = ['and'];
1302
        push @$clause, "{= $_}" for keys %$where;
1303
        $w = $self->where(clause => $clause, param => $where);
1304
    }
1305
    elsif (ref $where eq 'DBIx::Custom::Where') {
1306
        $w = $where;
1307
    }
1308
    elsif (ref $where eq 'ARRAY') {
1309
        $w = $self->where(
1310
            clause => $where->[0],
1311
            param  => $where->[1]
1312
        );
1313
    }
1314
    
1315
    croak qq{"where" must be hash reference or DBIx::Custom::Where object} .
1316
          qq{or array reference, which contains where clause and paramter}
1317
      unless ref $w eq 'DBIx::Custom::Where';
1318
    
1319
    return $w;
1320
}
1321

            
cleanup
Yuki Kimoto authored on 2011-01-25
1322
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-23
1323
__PACKAGE__->attr(
1324
    dbi_options => sub { {} },
1325
    filter_check  => 1
1326
);
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1327

            
cleanup
Yuki Kimoto authored on 2011-01-25
1328
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1329
sub default_bind_filter {
1330
    my $self = shift;
1331
    
1332
    if (@_) {
1333
        my $fname = $_[0];
1334
        
1335
        if (@_ && !$fname) {
1336
            $self->{default_out_filter} = undef;
1337
        }
1338
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1339
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1340
              unless exists $self->filters->{$fname};
1341
        
1342
            $self->{default_out_filter} = $self->filters->{$fname};
1343
        }
1344
        return $self;
1345
    }
1346
    
1347
    return $self->{default_out_filter};
1348
}
1349

            
cleanup
Yuki Kimoto authored on 2011-01-25
1350
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1351
sub default_fetch_filter {
1352
    my $self = shift;
1353
    
1354
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
1355
        my $fname = $_[0];
1356

            
cleanup
Yuki Kimoto authored on 2011-01-12
1357
        if (@_ && !$fname) {
1358
            $self->{default_in_filter} = undef;
1359
        }
1360
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1361
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1362
              unless exists $self->filters->{$fname};
1363
        
1364
            $self->{default_in_filter} = $self->filters->{$fname};
1365
        }
1366
        
1367
        return $self;
1368
    }
1369
    
many changed
Yuki Kimoto authored on 2011-01-23
1370
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
1371
}
1372

            
cleanup
Yuki Kimoto authored on 2011-01-25
1373
# DEPRECATED!
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1374
sub register_tag_processor {
1375
    return shift->query_builder->register_tag_processor(@_);
1376
}
1377

            
cleanup
Yuki Kimoto authored on 2011-03-08
1378
# DEPRECATED!
1379
sub _push_relation {
1380
    my ($self, $sql, $tables, $relation, $need_where) = @_;
1381
    
1382
    if (keys %{$relation || {}}) {
1383
        push @$sql, $need_where ? 'where' : 'and';
1384
        foreach my $rcolumn (keys %$relation) {
1385
            my $table1 = (split (/\./, $rcolumn))[0];
1386
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1387
            push @$tables, ($table1, $table2);
1388
            push @$sql, ("$rcolumn = " . $relation->{$rcolumn},  'and');
1389
        }
1390
    }
1391
    pop @$sql if $sql->[-1] eq 'and';    
1392
}
1393

            
1394
# DEPRECATED!
1395
sub _add_relation_table {
cleanup
Yuki Kimoto authored on 2011-03-09
1396
    my ($self, $tables, $relation) = @_;
cleanup
Yuki Kimoto authored on 2011-03-08
1397
    
1398
    if (keys %{$relation || {}}) {
1399
        foreach my $rcolumn (keys %$relation) {
1400
            my $table1 = (split (/\./, $rcolumn))[0];
1401
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1402
            my $table1_exists;
1403
            my $table2_exists;
1404
            foreach my $table (@$tables) {
1405
                $table1_exists = 1 if $table eq $table1;
1406
                $table2_exists = 1 if $table eq $table2;
1407
            }
1408
            unshift @$tables, $table1 unless $table1_exists;
1409
            unshift @$tables, $table2 unless $table2_exists;
1410
        }
1411
    }
1412
}
1413

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1416
=head1 NAME
1417

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

            
1420
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
1421

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1422
    use DBIx::Custom;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1423
    
1424
    # Connect
1425
    my $dbi = DBIx::Custom->connect(
1426
        data_source => "dbi:mysql:database=dbname",
1427
        user => 'ken',
1428
        password => '!LFKD%$&',
1429
        dbi_option => {mysql_enable_utf8 => 1}
1430
    );
cleanup
yuki-kimoto authored on 2010-08-05
1431

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1432
    # Insert 
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1433
    $dbi->insert(
1434
        table  => 'book',
1435
        param  => {title => 'Perl', author => 'Ken'}
1436
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1437
    
1438
    # Update 
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1439
    $dbi->update(
1440
        table  => 'book', 
1441
        param  => {title => 'Perl', author => 'Ken'}, 
1442
        where  => {id => 5},
1443
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1444
    
1445
    # Delete
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1446
    $dbi->delete(
1447
        table  => 'book',
1448
        where  => {author => 'Ken'},
1449
    );
cleanup
yuki-kimoto authored on 2010-08-05
1450

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1457
    # Select, more complex
1458
    my $result = $dbi->select(
1459
        table  => 'book',
1460
        column => [
1461
            'book.author as book__author',
1462
            'company.name as company__name'
1463
        ],
1464
        where  => {'book.author' => 'Ken'},
1465
        join => ['left outer join company on book.company_id = company.id'],
1466
        append => 'order by id limit 5'
removed reconnect method
yuki-kimoto authored on 2010-05-28
1467
    );
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1468
    
removed register_format()
yuki-kimoto authored on 2010-05-26
1469
    # Fetch
1470
    while (my $row = $result->fetch) {
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1471
        
removed register_format()
yuki-kimoto authored on 2010-05-26
1472
    }
1473
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1474
    # Fetch as hash
removed register_format()
yuki-kimoto authored on 2010-05-26
1475
    while (my $row = $result->fetch_hash) {
1476
        
1477
    }
1478
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1479
    # Execute SQL with parameter.
1480
    $dbi->execute(
1481
        "select id from book where {= author} and {like title}",
1482
        param  => {author => 'ken', title => '%Perl%'}
1483
    );
1484
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1485
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
1486

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

            
1489
=head1 FEATURES
removed reconnect method
yuki-kimoto authored on 2010-05-28
1490

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1495
There are many basic methods to execute various queries.
1496
C<insert()>, C<update()>, C<update_all()>,C<delete()>,
1497
C<delete_all()>, C<select()>,
1498
C<insert_at()>, C<update_at()>, 
1499
C<delete_at()>, C<select_at()>, C<execute()>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1500

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1501
=item *
1502

            
1503
Filter when data is send or receive.
1504

            
1505
=item *
1506

            
1507
Data filtering system
1508

            
1509
=item *
1510

            
1511
Model support.
1512

            
1513
=item *
1514

            
1515
Generate where clause dinamically.
1516

            
1517
=item *
1518

            
1519
Generate join clause dinamically.
1520

            
1521
=back
pod fix
Yuki Kimoto authored on 2011-01-21
1522

            
1523
=head1 GUIDE
1524

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1556
=head2 C<default_dbi_option>
1557

            
1558
    my $default_dbi_option = $dbi->default_dbi_option;
1559
    $dbi            = $dbi->default_dbi_option($default_dbi_option);
1560

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1564
    {
1565
        RaiseError => 1,
1566
        PrintError => 0,
1567
        AutoCommit => 1,
1568
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
1569

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

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

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

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

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

            
1582
    my $models = $dbi->models;
1583
    $dbi       = $dbi->models(\%models);
1584

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1587
=head2 C<password>
1588

            
1589
    my $password = $dbi->password;
1590
    $dbi         = $dbi->password('lkj&le`@s');
1591

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

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1618
    my $user = $dbi->user;
1619
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1620

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

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

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

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

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1631
    $dbi->apply_filter(
cleanup
Yuki Kimoto authored on 2011-03-10
1632
        'book',
update pod
Yuki Kimoto authored on 2011-03-13
1633
        'issue_date' => {
1634
            out => 'tp_to_date',
1635
            in  => 'date_to_tp',
1636
            end => 'tp_to_displaydate'
1637
        },
1638
        'write_date' => {
1639
            out => 'tp_to_date',
1640
            in  => 'date_to_tp',
1641
            end => 'tp_to_displaydate'
1642
        }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1643
    );
1644

            
update pod
Yuki Kimoto authored on 2011-03-13
1645
Apply filter to columns.
1646
C<out> filter is executed before data is send to database.
1647
C<in> filter is executed after a row is fetch.
1648
C<end> filter is execute after C<in> filter is executed.
1649

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1652
       PETTERN         EXAMPLE
1653
    1. Column        : author
1654
    2. Table.Column  : book.author
1655
    3. Table__Column : book__author
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1656

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

            
1660
You can set multiple filters at once.
1661

            
1662
    $dbi->apply_filter(
1663
        'book',
1664
        [qw/issue_date write_date/] => {
1665
            out => 'tp_to_date',
1666
            in  => 'date_to_tp',
1667
            end => 'tp_to_displaydate'
1668
        }
1669
    );
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1670

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

            
1673
    $dbi          = $dbi->cache_method(\&cache_method);
1674
    $cache_method = $dbi->cache_method
1675

            
update pod
Yuki Kimoto authored on 2011-03-13
1676
Method to set and get cache.
1677
Default to the following one.
1678

            
1679
    sub {
1680
        my $self = shift;
1681
        
1682
        $self->{_cached} ||= {};
1683
        
1684
        if (@_ > 1) {
1685
            $self->{_cached}{$_[0]} = $_[1];
1686
        }
1687
        else {
1688
            return $self->{_cached}{$_[0]};
1689
        }
1690
    }
update pod
Yuki Kimoto authored on 2011-03-13
1691

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1694
    my $dbi = DBIx::Custom->connect(
1695
        data_source => "dbi:mysql:database=dbname",
1696
        user => 'ken',
1697
        password => '!LFKD%$&',
1698
        dbi_option => {mysql_enable_utf8 => 1}
1699
    );
1700

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1707
=head2 C<create_query>
1708
    
1709
    my $query = $dbi->create_query(
update pod
Yuki Kimoto authored on 2011-03-13
1710
        "insert into book {insert_param title author};";
cleanup
yuki-kimoto authored on 2010-10-17
1711
    );
update document
yuki-kimoto authored on 2009-11-19
1712

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

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

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

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

            
1723
    my $dbh = $dbi->dbh;
1724
    $dbi    = $dbi->dbh($dbh);
1725

            
1726
Get and set database handle of L<DBI>.
1727

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1733
    my $result = $dbi->execute(
1734
        "select * from book where {= title} and {like author}",
1735
        param => {title => 'Perl', author => '%Ken%'}
1736
    );
1737

            
1738
Execute SQL, containing tags.
1739
Return value is L<DBIx::Custom::Result> in select statement, or
1740
the count of affected rows in insert, update, delete statement.
1741

            
1742
Tag is turned into the statement containing place holder
1743
before SQL is executed.
1744

            
1745
    select * from where title = ? and author like ?;
1746

            
1747
See also L<Tags/Tags>.
1748

            
1749
The following opitons are currently available.
1750

            
1751
=over 4
1752

            
1753
=item C<filter>
1754

            
1755
Filter, executed before data is send to database. This is array reference.
1756
Filter value is code reference or
1757
filter name registerd by C<register_filter()>.
1758

            
1759
    # Basic
1760
    $dbi->execute(
1761
        $sql,
1762
        filter => [
1763
            title  => sub { uc $_[0] }
1764
            author => sub { uc $_[0] }
1765
        ]
1766
    );
1767
    
1768
    # At once
1769
    $dbi->execute(
1770
        $sql,
1771
        filter => [
1772
            [qw/title author/]  => sub { uc $_[0] }
1773
        ]
1774
    );
1775
    
1776
    # Filter name
1777
    $dbi->execute(
1778
        $sql,
1779
        filter => [
1780
            title  => 'upper_case',
1781
            author => 'upper_case'
1782
        ]
1783
    );
1784

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

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

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

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

            
1793
Delete statement.
1794

            
1795
The following opitons are currently available.
1796

            
update pod
Yuki Kimoto authored on 2011-03-13
1797
=over 4
1798

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

            
1801
Table name.
1802

            
1803
    $dbi->delete(table => 'book');
1804

            
1805
=item C<where>
1806

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1807
Where clause. This is hash reference or L<DBIx::Custom::Where> object
1808
or array refrence, which contains where clause and paramter.
update pod
Yuki Kimoto authored on 2011-03-13
1809
    
1810
    # Hash reference
1811
    $dbi->delete(where => {title => 'Perl'});
1812
    
1813
    # DBIx::Custom::Where object
1814
    my $where = $dbi->where(
1815
        clause => ['and', '{= author}', '{like title}'],
1816
        param  => {author => 'Ken', title => '%Perl%'}
1817
    );
1818
    $dbi->delete(where => $where);
1819

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1820
    # Array refrendce (where clause and parameter)
1821
    $dbi->delete(where =>
1822
        [
1823
            ['and', '{= author}', '{like title}'],
1824
            {author => 'Ken', title => '%Perl%'}
1825
        ]
1826
    );
1827
    
update pod
Yuki Kimoto authored on 2011-03-13
1828
=item C<append>
1829

            
1830
Append statement to last of SQL. This is string.
1831

            
1832
    $dbi->delete(append => 'order by title');
1833

            
1834
=item C<filter>
1835

            
1836
Filter, executed before data is send to database. This is array reference.
1837
Filter value is code reference or
1838
filter name registerd by C<register_filter()>.
1839

            
1840
    # Basic
1841
    $dbi->delete(
1842
        filter => [
1843
            title  => sub { uc $_[0] }
1844
            author => sub { uc $_[0] }
1845
        ]
1846
    );
1847
    
1848
    # At once
1849
    $dbi->delete(
1850
        filter => [
1851
            [qw/title author/]  => sub { uc $_[0] }
1852
        ]
1853
    );
1854
    
1855
    # Filter name
1856
    $dbi->delete(
1857
        filter => [
1858
            title  => 'upper_case',
1859
            author => 'upper_case'
1860
        ]
1861
    );
1862

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
1865
=head2 C<column> EXPERIMENTAL
1866

            
1867
    my $column = $self->column(book => ['author', 'title']);
1868

            
1869
Create column clause. The follwoing column clause is created.
1870

            
1871
    book.author as book__author,
1872
    book.title as book__title
1873

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

            
1876
Get L<DBIx::Custom::Query> object instead of executing SQL.
1877
This is true or false value.
1878

            
1879
    my $query = $dbi->delete(query => 1);
1880

            
1881
You can check SQL.
1882

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1885
=back
1886

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

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

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

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

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

            
1898
    $dbi->delete_at(
1899
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
1900
        primary_key => 'id',
1901
        where => '5'
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1902
    );
1903

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1908
=over 4
1909

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1912
Primary key. This is constant value or array reference.
1913
    
1914
    # Constant value
1915
    $dbi->delete(primary_key => 'id');
1916

            
1917
    # Array reference
1918
    $dbi->delete(primary_key => ['id1', 'id2' ]);
1919

            
1920
This is used to create where clause.
1921

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

            
1924
Where clause, created from primary key information.
1925
This is constant value or array reference.
1926

            
1927
    # Constant value
1928
    $dbi->delete(where => 5);
1929

            
1930
    # Array reference
1931
    $dbi->delete(where => [3, 5]);
1932

            
1933
In first examle, the following SQL is created.
1934

            
1935
    delete from book where id = ?;
1936

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1939
=back
1940

            
cleanup
yuki-kimoto authored on 2010-10-17
1941
=head2 C<insert>
1942

            
update pod
Yuki Kimoto authored on 2011-03-13
1943
    $dbi->insert(
1944
        table  => 'book', 
1945
        param  => {title => 'Perl', author => 'Ken'}
1946
    );
1947

            
1948
Insert statement.
1949

            
1950
The following opitons are currently available.
1951

            
update pod
Yuki Kimoto authored on 2011-03-13
1952
=over 4
1953

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

            
1956
Table name.
1957

            
1958
    $dbi->insert(table => 'book');
1959

            
1960
=item C<param>
1961

            
1962
Insert data. This is hash reference.
1963

            
1964
    $dbi->insert(param => {title => 'Perl'});
1965

            
1966
=item C<append>
1967

            
1968
Append statement to last of SQL. This is string.
1969

            
1970
    $dbi->insert(append => 'order by title');
1971

            
1972
=item C<filter>
1973

            
1974
Filter, executed before data is send to database. This is array reference.
1975
Filter value is code reference or
1976
filter name registerd by C<register_filter()>.
1977

            
1978
    # Basic
1979
    $dbi->insert(
1980
        filter => [
1981
            title  => sub { uc $_[0] }
1982
            author => sub { uc $_[0] }
1983
        ]
1984
    );
1985
    
1986
    # At once
1987
    $dbi->insert(
1988
        filter => [
1989
            [qw/title author/]  => sub { uc $_[0] }
1990
        ]
1991
    );
1992
    
1993
    # Filter name
1994
    $dbi->insert(
1995
        filter => [
1996
            title  => 'upper_case',
1997
            author => 'upper_case'
1998
        ]
1999
    );
2000

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

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

            
2005
Get L<DBIx::Custom::Query> object instead of executing SQL.
2006
This is true or false value.
2007

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

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2014
=back
2015

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

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

            
2020
    $dbi->insert_at(
2021
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
2022
        primary_key => 'id',
2023
        where => '5',
2024
        param => {title => 'Perl'}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
2025
    );
2026

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2031
=over 4
2032

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

            
2035
Primary key. This is constant value or array reference.
2036
    
2037
    # Constant value
2038
    $dbi->insert(primary_key => 'id');
2039

            
2040
    # Array reference
2041
    $dbi->insert(primary_key => ['id1', 'id2' ]);
2042

            
2043
This is used to create parts of insert data.
2044

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

            
2047
Parts of Insert data, create from primary key information.
2048
This is constant value or array reference.
2049

            
2050
    # Constant value
2051
    $dbi->insert(where => 5);
2052

            
2053
    # Array reference
2054
    $dbi->insert(where => [3, 5]);
2055

            
2056
In first examle, the following SQL is created.
2057

            
2058
    insert into book (id, title) values (?, ?);
2059

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2062
=back
2063

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

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

            
2068
Create insert parameter tag.
2069

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

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

            
pod fix
Yuki Kimoto authored on 2011-01-21
2074
    $dbi->each_column(
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
2075
        sub {
update pod
Yuki Kimoto authored on 2011-03-13
2076
            my ($dbi, $table, $column, $column_info) = @_;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
2077
            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
2078
            my $type = $column_info->{TYPE_NAME};
pod fix
Yuki Kimoto authored on 2011-01-21
2079
            
2080
            if ($type eq 'DATE') {
2081
                # ...
2082
            }
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
2083
        }
2084
    );
update pod
Yuki Kimoto authored on 2011-03-13
2085

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

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

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2098
    lib / MyModel.pm
2099
        / MyModel / book.pm
2100
                  / company.pm
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2101

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

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

            
2106
    package MyModel;
2107
    
2108
    use base 'DBIx::Custom::Model';
update pod
Yuki Kimoto authored on 2011-03-13
2109
    
2110
    1;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2111

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2116
    package MyModel::book;
2117
    
2118
    use base 'MyModel';
2119
    
2120
    1;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2121

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2124
    package MyModel::company;
2125
    
2126
    use base 'MyModel';
2127
    
2128
    1;
2129
    
2130
MyModel::book and MyModel::company is included by C<include_model()>.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2131

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

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

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

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

            
2141
    $dbi->method(
2142
        update_or_insert => sub {
2143
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-03-13
2144
            
2145
            # Process
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2146
        },
2147
        find_or_create   => sub {
2148
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-03-13
2149
            
2150
            # Process
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2151
        }
2152
    );
2153

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

            
2156
    $dbi->update_or_insert;
2157
    $dbi->find_or_create;
2158

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

            
2161
    $dbi->model('book')->method(
2162
        insert => sub { ... },
2163
        update => sub { ... }
2164
    );
2165
    
2166
    my $model = $dbi->model('book');
2167

            
2168
Set and get a L<DBIx::Custom::Model> object,
2169

            
cleanup
Yuki Kimoto authored on 2011-03-21
2170
=head2 C<mycolumn> EXPERIMENTAL
2171

            
2172
    my $column = $self->mycolumn(book => ['author', 'title']);
2173

            
2174
Create column clause for myself. The follwoing column clause is created.
2175

            
2176
    book.author as author,
2177
    book.title as title
2178

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2181
    my $dbi = DBIx::Custom->new(
2182
        data_source => "dbi:mysql:database=dbname",
2183
        user => 'ken',
2184
        password => '!LFKD%$&',
2185
        dbi_option => {mysql_enable_utf8 => 1}
2186
    );
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2187

            
2188
Create a new L<DBIx::Custom> object.
2189

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

            
2192
    my $not_exists = $dbi->not_exists;
2193

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

            
cleanup
yuki-kimoto authored on 2010-10-17
2197
=head2 C<register_filter>
2198

            
update pod
Yuki Kimoto authored on 2011-03-13
2199
    $dbi->register_filter(
2200
        # Time::Piece object to database DATE format
2201
        tp_to_date => sub {
2202
            my $tp = shift;
2203
            return $tp->strftime('%Y-%m-%d');
2204
        },
2205
        # database DATE format to Time::Piece object
2206
        date_to_tp => sub {
2207
           my $date = shift;
2208
           return Time::Piece->strptime($date, '%Y-%m-%d');
2209
        }
2210
    );
cleanup
yuki-kimoto authored on 2010-10-17
2211
    
update pod
Yuki Kimoto authored on 2011-03-13
2212
Register filters, used by C<filter> option of many methods.
cleanup
yuki-kimoto authored on 2010-10-17
2213

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2216
    $dbi->register_tag(
2217
        update => sub {
2218
            my @columns = @_;
2219
            
2220
            # Update parameters
2221
            my $s = 'set ';
2222
            $s .= "$_ = ?, " for @columns;
2223
            $s =~ s/, $//;
2224
            
2225
            return [$s, \@columns];
2226
        }
2227
    );
cleanup
yuki-kimoto authored on 2010-10-17
2228

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

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

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

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

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

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

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

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

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

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

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
2252
    my $result = $dbi->select(
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2253
        table  => 'book',
2254
        column => ['author', 'title'],
2255
        where  => {author => 'Ken'},
select method column option ...
Yuki Kimoto authored on 2011-02-22
2256
    );
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2257
    
update pod
Yuki Kimoto authored on 2011-03-12
2258
Select statement.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2259

            
2260
The following opitons are currently available.
2261

            
2262
=over 4
2263

            
2264
=item C<table>
2265

            
2266
Table name.
2267

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

            
2270
=item C<column>
2271

            
2272
Column clause. This is array reference or constant value.
2273

            
2274
    # Hash refernce
2275
    $dbi->select(column => ['author', 'title']);
2276
    
2277
    # Constant value
2278
    $dbi->select(column => 'author');
2279

            
2280
Default is '*' unless C<column> is specified.
2281

            
2282
    # Default
2283
    $dbi->select(column => '*');
2284

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
2285
You can use hash option in C<column>
2286

            
2287
=over 4
2288

            
2289
=item all EXPERIMENTAL
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2290

            
update pod
Yuki Kimoto authored on 2011-03-12
2291
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
2292

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
2293
    $dbi->select(column => {all => 1});
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2294

            
2295
If main table is C<book> and joined table is C<company>,
2296
This create the following column clause.
2297

            
2298
    book.author as author
2299
    book.company_id as company_id
2300
    company.id as company__id
2301
    company.name as company__name
2302

            
2303
Columns of main table is consist of only column name,
2304
Columns of joined table is consist of table and column name joined C<__>.
2305

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
2306
Note that this option is failed unless modles is included and
2307
C<columns> attribute is set.
update pod
Yuki Kimoto authored on 2011-03-12
2308

            
2309
    # Generally do the following way before using all_column option
2310
    $dbi->include_model('MyModel')->setup_model;
2311

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
2312
=item table EXPERIMENTAL
2313

            
2314
You can also specify table names by C<table> option
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
2315

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
2316
    $dbi->select(column => {table => ['book', 'company']});
2317

            
2318
=item prepend EXPERIMENTAL
2319

            
2320
You can add before created statement
2321

            
2322
    $dbi->select(column => {prepend => 'SOME', all => 1});
2323

            
2324
=back
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
2325

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

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2328
Where clause. This is hash reference or L<DBIx::Custom::Where> object,
2329
or array refrence, which contains where clause and paramter.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2330
    
2331
    # Hash reference
update pod
Yuki Kimoto authored on 2011-03-12
2332
    $dbi->select(where => {author => 'Ken', 'title' => 'Perl'});
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2333
    
update pod
Yuki Kimoto authored on 2011-03-12
2334
    # DBIx::Custom::Where object
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2335
    my $where = $dbi->where(
2336
        clause => ['and', '{= author}', '{like title}'],
2337
        param  => {author => 'Ken', title => '%Perl%'}
2338
    );
update pod
Yuki Kimoto authored on 2011-03-12
2339
    $dbi->select(where => $where);
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2340

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2341
    # Array refrendce (where clause and parameter)
2342
    $dbi->select(where =>
2343
        [
2344
            ['and', '{= author}', '{like title}'],
2345
            {author => 'Ken', title => '%Perl%'}
2346
        ]
2347
    );
2348
    
update pod
Yuki Kimoto authored on 2011-03-13
2349
=item C<join> EXPERIMENTAL
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2350

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

            
2353
    $dbi->select(join =>
2354
        [
2355
            'left outer join company on book.company_id = company_id',
2356
            'left outer join location on company.location_id = location.id'
2357
        ]
2358
    );
2359

            
2360
If column cluase or where clause contain table name like "company.name",
2361
needed join clause is used automatically.
2362

            
2363
    $dbi->select(
2364
        table => 'book',
2365
        column => ['company.location_id as company__location_id'],
2366
        where => {'company.name' => 'Orange'},
2367
        join => [
2368
            'left outer join company on book.company_id = company.id',
2369
            'left outer join location on company.location_id = location.id'
2370
        ]
2371
    );
2372

            
2373
In above select, the following SQL is created.
2374

            
2375
    select company.location_id as company__location_id
2376
    from book
2377
      left outer join company on book.company_id = company.id
2378
    where company.name = Orange
2379

            
2380
=item C<append>
2381

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

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

            
2386
=item C<filter>
2387

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

            
2392
    # Basic
2393
    $dbi->select(
2394
        filter => [
2395
            title  => sub { uc $_[0] }
2396
            author => sub { uc $_[0] }
2397
        ]
2398
    );
2399
    
2400
    # At once
2401
    $dbi->select(
2402
        filter => [
2403
            [qw/title author/]  => sub { uc $_[0] }
2404
        ]
2405
    );
2406
    
2407
    # Filter name
2408
    $dbi->select(
2409
        filter => [
2410
            title  => 'upper_case',
2411
            author => 'upper_case'
2412
        ]
2413
    );
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
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()>.
update document
yuki-kimoto authored on 2009-11-19
2416

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

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

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

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

            
2426
    my $sql = $query->sql;
2427

            
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
2428
=item C<type> EXPERIMENTAL
2429

            
2430
Specify database data type.
2431

            
2432
    $dbi->select(type => [image => DBI::SQL_BLOB]);
2433
    $dbi->select(type => [[qw/image audio/] => DBI::SQL_BLOB]);
2434

            
2435
This is used to bind paramter by C<bind_param()> of statment handle.
2436

            
2437
    $sth->bind_param($pos, $value, DBI::SQL_BLOB);
2438

            
update pod
Yuki Kimoto authored on 2011-03-12
2439
=back
cleanup
Yuki Kimoto authored on 2011-03-08
2440

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

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

            
2445
    $dbi->select_at(
2446
        table => 'book',
2447
        primary_key => 'id',
2448
        where => '5'
2449
    );
2450

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2455
=over 4
2456

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

            
update pod
Yuki Kimoto authored on 2011-03-12
2459
Primary key. This is constant value or array reference.
2460
    
2461
    # Constant value
2462
    $dbi->select(primary_key => 'id');
2463

            
2464
    # Array reference
2465
    $dbi->select(primary_key => ['id1', 'id2' ]);
2466

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

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

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

            
2474
    # Constant value
2475
    $dbi->select(where => 5);
2476

            
2477
    # Array reference
2478
    $dbi->select(where => [3, 5]);
2479

            
2480
In first examle, the following SQL is created.
2481

            
2482
    select * from book where id = ?
2483

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2486
=back
2487

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2490
    $dbi->update(
2491
        table  => 'book',
2492
        param  => {title => 'Perl'},
2493
        where  => {id => 4}
2494
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
2495

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2500
=over 4
2501

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2504
Table name.
2505

            
2506
    $dbi->update(table => 'book');
2507

            
2508
=item C<param>
2509

            
2510
Update data. This is hash reference.
2511

            
2512
    $dbi->update(param => {title => 'Perl'});
2513

            
2514
=item C<where>
2515

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2516
Where clause. This is hash reference or L<DBIx::Custom::Where> object
2517
or array refrence.
update pod
Yuki Kimoto authored on 2011-03-13
2518
    
2519
    # Hash reference
2520
    $dbi->update(where => {author => 'Ken', 'title' => 'Perl'});
2521
    
2522
    # DBIx::Custom::Where object
2523
    my $where = $dbi->where(
2524
        clause => ['and', '{= author}', '{like title}'],
2525
        param  => {author => 'Ken', title => '%Perl%'}
2526
    );
2527
    $dbi->update(where => $where);
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2528
    
2529
    # Array refrendce (where clause and parameter)
2530
    $dbi->update(where =>
2531
        [
2532
            ['and', '{= author}', '{like title}'],
2533
            {author => 'Ken', title => '%Perl%'}
2534
        ]
2535
    );
update pod
Yuki Kimoto authored on 2011-03-13
2536

            
2537
=item C<append>
2538

            
2539
Append statement to last of SQL. This is string.
2540

            
2541
    $dbi->update(append => 'order by title');
2542

            
2543
=item C<filter>
2544

            
2545
Filter, executed before data is send to database. This is array reference.
2546
Filter value is code reference or
2547
filter name registerd by C<register_filter()>.
2548

            
2549
    # Basic
2550
    $dbi->update(
2551
        filter => [
2552
            title  => sub { uc $_[0] }
2553
            author => sub { uc $_[0] }
2554
        ]
2555
    );
2556
    
2557
    # At once
2558
    $dbi->update(
2559
        filter => [
2560
            [qw/title author/]  => sub { uc $_[0] }
2561
        ]
2562
    );
2563
    
2564
    # Filter name
2565
    $dbi->update(
2566
        filter => [
2567
            title  => 'upper_case',
2568
            author => 'upper_case'
2569
        ]
2570
    );
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2571

            
update pod
Yuki Kimoto authored on 2011-03-13
2572
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
2573

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

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

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

            
2581
You can check SQL.
2582

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2585
=back
2586

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

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

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

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

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

            
2598
    $dbi->update_at(
2599
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
2600
        primary_key => 'id',
2601
        where => '5',
2602
        param => {title => 'Perl'}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2603
    );
2604

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2609
=over 4
2610

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

            
2613
Primary key. This is constant value or array reference.
2614
    
2615
    # Constant value
2616
    $dbi->update(primary_key => 'id');
2617

            
2618
    # Array reference
2619
    $dbi->update(primary_key => ['id1', 'id2' ]);
2620

            
2621
This is used to create where clause.
2622

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

            
2625
Where clause, created from primary key information.
2626
This is constant value or array reference.
2627

            
2628
    # Constant value
2629
    $dbi->update(where => 5);
2630

            
2631
    # Array reference
2632
    $dbi->update(where => [3, 5]);
2633

            
2634
In first examle, the following SQL is created.
2635

            
2636
    update book set title = ? where id = ?
2637

            
2638
Place holders are set to 'Perl' and 5.
2639

            
update pod
Yuki Kimoto authored on 2011-03-13
2640
=back
2641

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

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

            
2646
Create update parameter tag.
2647

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

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

            
cleanup
Yuki Kimoto authored on 2011-03-09
2652
    my $where = $dbi->where(
2653
        clause => ['and', '{= title}', '{= author}'],
2654
        param => {title => 'Perl', author => 'Ken'}
2655
    );
fix tests
Yuki Kimoto authored on 2011-01-18
2656

            
2657
Create a new L<DBIx::Custom::Where> object.
2658

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
2666
=head1 Tags
2667

            
2668
The following tags is available.
2669

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

            
2672
Table tag
2673

            
2674
    {table TABLE}    ->    TABLE
2675

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
2678
=head2 C<?>
2679

            
2680
Placeholder tag.
2681

            
2682
    {? NAME}    ->   ?
2683

            
2684
=head2 C<=>
2685

            
2686
Equal tag.
2687

            
2688
    {= NAME}    ->   NAME = ?
2689

            
2690
=head2 C<E<lt>E<gt>>
2691

            
2692
Not equal tag.
2693

            
2694
    {<> NAME}   ->   NAME <> ?
2695

            
2696
=head2 C<E<lt>>
2697

            
2698
Lower than tag
2699

            
2700
    {< NAME}    ->   NAME < ?
2701

            
2702
=head2 C<E<gt>>
2703

            
2704
Greater than tag
2705

            
2706
    {> NAME}    ->   NAME > ?
2707

            
2708
=head2 C<E<gt>=>
2709

            
2710
Greater than or equal tag
2711

            
2712
    {>= NAME}   ->   NAME >= ?
2713

            
2714
=head2 C<E<lt>=>
2715

            
2716
Lower than or equal tag
2717

            
2718
    {<= NAME}   ->   NAME <= ?
2719

            
2720
=head2 C<like>
2721

            
2722
Like tag
2723

            
2724
    {like NAME}   ->   NAME like ?
2725

            
2726
=head2 C<in>
2727

            
2728
In tag.
2729

            
2730
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
2731

            
2732
=head2 C<insert_param>
2733

            
2734
Insert parameter tag.
2735

            
2736
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
2737

            
2738
=head2 C<update_param>
2739

            
2740
Updata parameter tag.
2741

            
2742
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
2743

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

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

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

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

            
2753
C<< <kimoto.yuki at gmail.com> >>
2754

            
2755
L<http://github.com/yuki-kimoto/DBIx-Custom>
2756

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
2757
=head1 AUTHOR
2758

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

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

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

            
2765
This program is free software; you can redistribute it and/or modify it
2766
under the same terms as Perl itself.
2767

            
2768
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
2769

            
2770