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

            
update pod
Yuki Kimoto authored on 2011-03-13
3
our $VERSION = '0.1660';
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
4

            
5
use 5.008001;
cleanup
yuki-kimoto authored on 2009-12-22
6
use strict;
7
use warnings;
8

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
9
use base 'Object::Simple';
many change
yuki-kimoto authored on 2010-02-11
10

            
packaging one directory
yuki-kimoto authored on 2009-11-16
11
use Carp 'croak';
12
use DBI;
13
use DBIx::Custom::Result;
cleanup
yuki-kimoto authored on 2010-02-11
14
use DBIx::Custom::Query;
cleanup
yuki-kimoto authored on 2010-08-05
15
use DBIx::Custom::QueryBuilder;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
16
use DBIx::Custom::Where;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
17
use DBIx::Custom::Model;
cleanup
Yuki Kimoto authored on 2011-01-25
18
use DBIx::Custom::Tag;
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
19
use DBIx::Custom::Util;
update document
yuki-kimoto authored on 2010-05-27
20
use Encode qw/encode_utf8 decode_utf8/;
packaging one directory
yuki-kimoto authored on 2009-11-16
21

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
797
sub select_at {
798
    my ($self, %args) = @_;
799
    
cleanup
Yuki Kimoto authored on 2011-03-09
800
    # Check argument names
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
801
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
802
        croak qq{Argument "$name" is invalid name}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
803
          unless $VALID_SELECT_AT_ARGS{$name};
804
    }
805
    
806
    # Primary key
807
    my $primary_keys = delete $args{primary_key};
808
    $primary_keys = [$primary_keys] unless ref $primary_keys;
809
    
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
810
    # Table
811
    croak qq{"table" option must be specified} unless $args{table};
812
    my $table = ref $args{table} ? $args{table}->[-1] : $args{table};
813
    
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
814
    # Where clause
815
    my $where = {};
816
    if (exists $args{where}) {
817
        my $where_columns = delete $args{where};
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
818
        
819
        croak qq{"where" must be constant value or array reference}
820
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
821
        
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
822
        $where_columns = [$where_columns] unless ref $where_columns;
823
        
824
        for(my $i = 0; $i < @$primary_keys; $i ++) {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
825
           $where->{$table . '.' . $primary_keys->[$i]} = $where_columns->[$i];
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
826
        }
827
    }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
828
    
829
    if (exists $args{param}) {
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
830
        my $param = delete $args{param};
831
        for(my $i = 0; $i < @$primary_keys; $i ++) {
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
832
             delete $param->{$primary_keys->[$i]};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
833
        }
834
    }
835
    
836
    return $self->select(where => $where, %args);
837
}
838

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
839
sub model {
840
    my ($self, $name, $model) = @_;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
841
    
842
    # Set
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
843
    if ($model) {
add models() attribute
Yuki Kimoto authored on 2011-02-21
844
        $self->models->{$name} = $model;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
845
        return $self;
846
    }
847
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
848
    # Check model existance
849
    croak qq{Model "$name" is not included}
add models() attribute
Yuki Kimoto authored on 2011-02-21
850
      unless $self->models->{$name};
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
851
    
852
    # Get
add models() attribute
Yuki Kimoto authored on 2011-02-21
853
    return $self->models->{$name};
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
854
}
855

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
856
sub include_model {
857
    my ($self, $name_space, $model_infos) = @_;
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
858
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
859
    $name_space ||= '';
860
    unless ($model_infos) {
861
        # Load name space module
862
        croak qq{"$name_space" is invalid class name}
863
          if $name_space =~ /[^\w:]/;
864
        eval "use $name_space";
865
        croak qq{Name space module "$name_space.pm" is needed. $@} if $@;
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
866
        
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
867
        # Search model modules
868
        my $path = $INC{"$name_space.pm"};
869
        $path =~ s/\.pm$//;
870
        opendir my $dh, $path
871
          or croak qq{Can't open directory "$path": $!};
872
        $model_infos = [];
873
        while (my $module = readdir $dh) {
874
            push @$model_infos, $module
875
              if $module =~ s/\.pm$//;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
876
        }
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
877
        
878
        close $dh;
879
    }
880
    
881
    foreach my $model_info (@$model_infos) {
882
        
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
883
        # Model class, name, table
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
884
        my $model_class;
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
885
        my $model_name;
886
        my $model_table;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
887
        if (ref $model_info eq 'HASH') {
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
888
            $model_class = $model_info->{class};
889
            $model_name  = $model_info->{name};
890
            $model_table = $model_info->{table};
891
            
892
            $model_name  ||= $model_class;
893
            $model_table ||= $model_name;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
894
        }
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
895
        else { $model_class =$model_name = $model_table = $model_info }
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
896
        my $mclass = "${name_space}::$model_class";
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
897
        
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
898
        # Load
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
899
        croak qq{"$mclass" is invalid class name}
900
          if $mclass =~ /[^\w:]/;
901
        unless ($mclass->can('isa')) {
902
            eval "use $mclass";
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
903
            croak $@ if $@;
904
        }
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
905
        
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
906
        # Instantiate
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
907
        my $model = $mclass->new(dbi => $self);
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
908
        $model->name($model_name) unless $model->name;
909
        $model->table($model_table) unless $model->table;
removed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-28
910
        
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
911
        # Set
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
912
        $self->model($model->name, $model);
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
913
        
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
914
        # View
915
        $self->view($model->table, $model->view) if $model->view;
916
        
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
917
        # Apply filter
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
918
        croak "${name_space}::$model_class filter must be array reference"
919
          unless ref $model->filter eq 'ARRAY';
920
        $self->apply_filter($model->table, @{$model->filter});
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
921
    }
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
922
    return $self;
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
923
}
924

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
925
sub setup_model {
926
    my $self = shift;
927
    
928
    $self->each_column(
929
        sub {
930
            my ($self, $table, $column, $column_info) = @_;
931
            
932
            if (my $model = $self->models->{$table}) {
933
                push @{$model->columns}, $column;
934
            }
935
        }
936
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
937
    return $self;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
938
}
939

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

            
944
sub update {
945
    my ($self, %args) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
946
    
cleanup
Yuki Kimoto authored on 2011-03-09
947
    # Check argument names
cleanup
yuki-kimoto authored on 2010-10-17
948
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
949
        croak qq{Argument "$name" is invalid name}
cleanup
yuki-kimoto authored on 2010-10-17
950
          unless $VALID_UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
951
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
952
    
cleanup
yuki-kimoto authored on 2010-10-17
953
    # Arguments
954
    my $table            = $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
955
    croak qq{"table" option must be specified} unless $table;
cleanup
yuki-kimoto authored on 2010-10-17
956
    my $param            = $args{param} || {};
957
    my $where            = $args{where} || {};
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
958
    my $append           = $args{append} || '';
cleanup
yuki-kimoto authored on 2010-10-17
959
    my $filter           = $args{filter};
960
    my $allow_update_all = $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
961
    
cleanup
yuki-kimoto authored on 2010-10-17
962
    # Update keys
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
963
    my @clumns = keys %$param;
964

            
965
    # Columns
966
    my @columns;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
967
    my $safety = $self->safety_character;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
968
    foreach my $column (keys %$param) {
969
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
970
          unless $column =~ /^[$safety\.]+$/;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
971
        push @columns, $column;
972
    }
973
        
cleanup
yuki-kimoto authored on 2010-10-17
974
    # Update clause
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
975
    my $update_clause = '{update_param ' . join(' ', @clumns) . '}';
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
976

            
977
    # Where
978
    my $w;
979
    if (ref $where eq 'HASH') {
980
        my $clause = ['and'];
981
        push @$clause, "{= $_}" for keys %$where;
982
        $w = $self->where;
983
        $w->clause($clause);
984
        $w->param($where);
985
    }
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
986
    elsif (ref $where eq 'DBIx::Custom::Where') {
987
        $w = $where;
988
        $where = $w->param;
989
    }  
removed experimental registe...
yuki-kimoto authored on 2010-08-24
990
    
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
991
    croak qq{"where" must be hash refernce or DBIx::Custom::Where object}
992
      unless ref $w eq 'DBIx::Custom::Where';
removed reconnect method
yuki-kimoto authored on 2010-05-28
993
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
994
    # String where
995
    my $swhere = "$w";
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
996
    
997
    croak qq{"where" must be specified}
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
998
      if "$swhere" eq '' && !$allow_update_all;
removed reconnect method
yuki-kimoto authored on 2010-05-28
999
    
cleanup
Yuki Kimoto authored on 2011-01-27
1000
    # SQL stack
1001
    my @sql;
1002
    
1003
    # Update
1004
    push @sql, "update $table $update_clause $swhere";
1005
    push @sql, $append if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1006
    
cleanup
yuki-kimoto authored on 2010-10-17
1007
    # Rearrange parameters
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
1008
    foreach my $wkey (keys %$where) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1009
        
cleanup
yuki-kimoto authored on 2010-10-17
1010
        if (exists $param->{$wkey}) {
1011
            $param->{$wkey} = [$param->{$wkey}]
1012
              unless ref $param->{$wkey} eq 'ARRAY';
1013
            
1014
            push @{$param->{$wkey}}, $where->{$wkey};
1015
        }
1016
        else {
1017
            $param->{$wkey} = $where->{$wkey};
1018
        }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1019
    }
cleanup
yuki-kimoto authored on 2010-10-17
1020
    
cleanup
Yuki Kimoto authored on 2011-01-27
1021
    # SQL
1022
    my $sql = join(' ', @sql);
1023
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1024
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
1025
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1026
    return $query if $args{query};
1027
    
cleanup
yuki-kimoto authored on 2010-10-17
1028
    # Execute query
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1029
    my $ret_val = $self->execute($query, param  => $param, 
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1030
                                 filter => $filter,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1031
                                 table => $table);
cleanup
yuki-kimoto authored on 2010-10-17
1032
    
1033
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1034
}
1035

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

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

            
1042
sub update_at {
1043
    my ($self, %args) = @_;
1044
    
cleanup
Yuki Kimoto authored on 2011-03-09
1045
    # Check argument names
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1046
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
1047
        croak qq{Argument "$name" is invalid name}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1048
          unless $VALID_UPDATE_AT_ARGS{$name};
1049
    }
1050
    
1051
    # Primary key
1052
    my $primary_keys = delete $args{primary_key};
1053
    $primary_keys = [$primary_keys] unless ref $primary_keys;
1054
    
1055
    # Where clause
1056
    my $where = {};
1057
    my $param = {};
1058
    
1059
    if (exists $args{where}) {
1060
        my $where_columns = delete $args{where};
1061
        $where_columns = [$where_columns] unless ref $where_columns;
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1062

            
1063
        croak qq{"where" must be constant value or array reference}
1064
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1065
        
1066
        for(my $i = 0; $i < @$primary_keys; $i ++) {
1067
           $where->{$primary_keys->[$i]} = $where_columns->[$i];
1068
        }
1069
    }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1070
    
1071
    if (exists $args{param}) {
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1072
        $param = delete $args{param};
1073
        for(my $i = 0; $i < @$primary_keys; $i ++) {
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1074
            delete $param->{$primary_keys->[$i]};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1075
        }
1076
    }
1077
    
1078
    return $self->update(where => $where, param => $param, %args);
1079
}
1080

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1081
sub update_param {
1082
    my ($self, $param) = @_;
1083
    
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1084
    # Update parameter tag
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1085
    my @tag;
1086
    push @tag, '{update_param';
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1087
    my $safety = $self->safety_character;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1088
    foreach my $column (keys %$param) {
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1089
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1090
          unless $column =~ /^[$safety\.]+$/;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1091
        push @tag, $column;
1092
    }
1093
    push @tag, '}';
1094
    
1095
    return join ' ', @tag;
1096
}
1097

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
1098
sub view {
1099
    my $self = shift;
1100
    my $name = shift;
1101
    
1102
    # View
1103
    $self->{view} ||= {};
1104
    if (@_) {
1105
        $self->{view}->{$name} = $_[0];
1106
        return $self;
1107
    }
1108
    else {
1109
        return $name && $self->{view}->{$name}
1110
             ? "(" . $self->{view}->{$name} . ") as $name"
1111
             : undef;
1112
    }
1113
}
1114

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

            
1118
    return DBIx::Custom::Where->new(
1119
        query_builder => $self->query_builder,
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1120
        safety_character => $self->safety_character,
cleanup
Yuki Kimoto authored on 2011-03-09
1121
        @_
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1122
    );
cleanup
Yuki Kimoto authored on 2011-01-25
1123
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1124

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1125
sub _bind {
cleanup
Yuki Kimoto authored on 2011-01-12
1126
    my ($self, $params, $columns, $filter) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1127
    
cleanup
Yuki Kimoto authored on 2011-01-12
1128
    # bind values
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1129
    my @bind;
add tests
yuki-kimoto authored on 2010-08-08
1130
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
1131
    # Build bind values
1132
    my $count = {};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1133
    my $not_exists = {};
cleanup
Yuki Kimoto authored on 2011-01-12
1134
    foreach my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1135
        
1136
        # Value
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1137
        my $value;
1138
        if(ref $params->{$column} eq 'ARRAY') {
1139
            my $i = $count->{$column} || 0;
1140
            $i += $not_exists->{$column} || 0;
1141
            my $found;
1142
            for (my $k = $i; $i < @{$params->{$column}}; $k++) {
1143
                if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
1144
                    $not_exists->{$column}++;
1145
                }
1146
                else  {
1147
                    $value = $params->{$column}->[$k];
1148
                    $found = 1;
1149
                    last
1150
                }
1151
            }
1152
            next unless $found;
1153
        }
1154
        else { $value = $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1155
        
cleanup
Yuki Kimoto authored on 2011-01-12
1156
        # Filter
1157
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
1158
        
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1159
        push @bind, $f ? $f->($value) : $value;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1160
        
1161
        # Count up 
1162
        $count->{$column}++;
1163
    }
1164
    
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1165
    return \@bind;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1166
}
1167

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1168
sub _connect {
1169
    my $self = shift;
1170
    
1171
    # Attributes
1172
    my $data_source = $self->data_source;
1173
    croak qq{"data_source" must be specified to connect()"}
1174
      unless $data_source;
1175
    my $user        = $self->user;
1176
    my $password    = $self->password;
1177
    my $dbi_option = {%{$self->dbi_options}, %{$self->dbi_option}};
1178
    
1179
    # Connect
1180
    my $dbh = eval {DBI->connect(
1181
        $data_source,
1182
        $user,
1183
        $password,
1184
        {
1185
            %{$self->default_dbi_option},
1186
            %$dbi_option
1187
        }
1188
    )};
1189
    
1190
    # Connect error
1191
    croak $@ if $@;
1192
    
1193
    return $dbh;
1194
}
1195

            
cleanup
yuki-kimoto authored on 2010-10-17
1196
sub _croak {
1197
    my ($self, $error, $append) = @_;
1198
    $append ||= "";
1199
    
1200
    # Verbose
1201
    if ($Carp::Verbose) { croak $error }
1202
    
1203
    # Not verbose
1204
    else {
1205
        
1206
        # Remove line and module infromation
1207
        my $at_pos = rindex($error, ' at ');
1208
        $error = substr($error, 0, $at_pos);
1209
        $error =~ s/\s+$//;
1210
        
1211
        croak "$error$append";
1212
    }
1213
}
1214

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1215
sub _need_tables {
1216
    my ($self, $tree, $need_tables, $tables) = @_;
1217
    
1218
    foreach my $table (@$tables) {
1219
        
1220
        if ($tree->{$table}) {
1221
            $need_tables->{$table} = 1;
1222
            $self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}])
1223
        }
1224
    }
1225
}
1226

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1227
sub _tables {
1228
    my ($self, $source) = @_;
1229
    
1230
    my $tables = [];
1231
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1232
    my $safety_character = $self->safety_character;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1233
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1234
    while ($source =~ /\b($safety_character+)\./g) {
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1235
        push @$tables, $1;
1236
    }
1237
    
1238
    return $tables;
1239
}
1240

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1241
sub _push_join {
1242
    my ($self, $sql, $join, $join_tables) = @_;
1243
    
1244
    return unless @$join;
1245
    
1246
    my $tree = {};
1247
    
1248
    for (my $i = 0; $i < @$join; $i++) {
1249
        
1250
        my $join_clause = $join->[$i];
1251
        
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-11
1252
        if ($join_clause =~ /\s([^\.\s]+?)\..+\s([^\.\s]+?)\..+?$/) {
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1253
            
1254
            my $table1 = $1;
1255
            my $table2 = $2;
1256
            
1257
            croak qq{right side table of "$join_clause" must be uniq}
1258
              if exists $tree->{$table2};
1259
            
1260
            $tree->{$table2}
1261
              = {position => $i, parent => $table1, join => $join_clause};
1262
        }
1263
        else {
1264
            croak qq{join "$join_clause" must be two table name};
1265
        }
1266
    }
1267
    
1268
    my $need_tables = {};
1269
    $self->_need_tables($tree, $need_tables, $join_tables);
1270
    
1271
    my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} } keys %$need_tables;
cleanup
Yuki Kimoto authored on 2011-03-08
1272

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1273
    foreach my $need_table (@need_tables) {
1274
        push @$sql, $tree->{$need_table}{join};
1275
    }
1276
}
cleanup
Yuki Kimoto authored on 2011-03-08
1277

            
cleanup
Yuki Kimoto authored on 2011-01-25
1278
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-23
1279
__PACKAGE__->attr(
1280
    dbi_options => sub { {} },
1281
    filter_check  => 1
1282
);
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1283

            
cleanup
Yuki Kimoto authored on 2011-01-25
1284
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1285
sub default_bind_filter {
1286
    my $self = shift;
1287
    
1288
    if (@_) {
1289
        my $fname = $_[0];
1290
        
1291
        if (@_ && !$fname) {
1292
            $self->{default_out_filter} = undef;
1293
        }
1294
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1295
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1296
              unless exists $self->filters->{$fname};
1297
        
1298
            $self->{default_out_filter} = $self->filters->{$fname};
1299
        }
1300
        return $self;
1301
    }
1302
    
1303
    return $self->{default_out_filter};
1304
}
1305

            
cleanup
Yuki Kimoto authored on 2011-01-25
1306
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1307
sub default_fetch_filter {
1308
    my $self = shift;
1309
    
1310
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
1311
        my $fname = $_[0];
1312

            
cleanup
Yuki Kimoto authored on 2011-01-12
1313
        if (@_ && !$fname) {
1314
            $self->{default_in_filter} = undef;
1315
        }
1316
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1317
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1318
              unless exists $self->filters->{$fname};
1319
        
1320
            $self->{default_in_filter} = $self->filters->{$fname};
1321
        }
1322
        
1323
        return $self;
1324
    }
1325
    
many changed
Yuki Kimoto authored on 2011-01-23
1326
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
1327
}
1328

            
cleanup
Yuki Kimoto authored on 2011-01-25
1329
# DEPRECATED!
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1330
sub register_tag_processor {
1331
    return shift->query_builder->register_tag_processor(@_);
1332
}
1333

            
cleanup
Yuki Kimoto authored on 2011-03-08
1334
# DEPRECATED!
1335
sub _push_relation {
1336
    my ($self, $sql, $tables, $relation, $need_where) = @_;
1337
    
1338
    if (keys %{$relation || {}}) {
1339
        push @$sql, $need_where ? 'where' : 'and';
1340
        foreach my $rcolumn (keys %$relation) {
1341
            my $table1 = (split (/\./, $rcolumn))[0];
1342
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1343
            push @$tables, ($table1, $table2);
1344
            push @$sql, ("$rcolumn = " . $relation->{$rcolumn},  'and');
1345
        }
1346
    }
1347
    pop @$sql if $sql->[-1] eq 'and';    
1348
}
1349

            
1350
# DEPRECATED!
1351
sub _add_relation_table {
cleanup
Yuki Kimoto authored on 2011-03-09
1352
    my ($self, $tables, $relation) = @_;
cleanup
Yuki Kimoto authored on 2011-03-08
1353
    
1354
    if (keys %{$relation || {}}) {
1355
        foreach my $rcolumn (keys %$relation) {
1356
            my $table1 = (split (/\./, $rcolumn))[0];
1357
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1358
            my $table1_exists;
1359
            my $table2_exists;
1360
            foreach my $table (@$tables) {
1361
                $table1_exists = 1 if $table eq $table1;
1362
                $table2_exists = 1 if $table eq $table2;
1363
            }
1364
            unshift @$tables, $table1 unless $table1_exists;
1365
            unshift @$tables, $table2 unless $table2_exists;
1366
        }
1367
    }
1368
}
1369

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1372
=head1 NAME
1373

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

            
1376
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
1377

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1378
    use DBIx::Custom;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1379
    
1380
    # Connect
1381
    my $dbi = DBIx::Custom->connect(
1382
        data_source => "dbi:mysql:database=dbname",
1383
        user => 'ken',
1384
        password => '!LFKD%$&',
1385
        dbi_option => {mysql_enable_utf8 => 1}
1386
    );
cleanup
yuki-kimoto authored on 2010-08-05
1387

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1388
    # Insert 
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1389
    $dbi->insert(
1390
        table  => 'book',
1391
        param  => {title => 'Perl', author => 'Ken'}
1392
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1393
    
1394
    # Update 
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1395
    $dbi->update(
1396
        table  => 'book', 
1397
        param  => {title => 'Perl', author => 'Ken'}, 
1398
        where  => {id => 5},
1399
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1400
    
1401
    # Delete
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1402
    $dbi->delete(
1403
        table  => 'book',
1404
        where  => {author => 'Ken'},
1405
    );
cleanup
yuki-kimoto authored on 2010-08-05
1406

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1413
    # Select, more complex
1414
    my $result = $dbi->select(
1415
        table  => 'book',
1416
        column => [
1417
            'book.author as book__author',
1418
            'company.name as company__name'
1419
        ],
1420
        where  => {'book.author' => 'Ken'},
1421
        join => ['left outer join company on book.company_id = company.id'],
1422
        append => 'order by id limit 5'
removed reconnect method
yuki-kimoto authored on 2010-05-28
1423
    );
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1424
    
removed register_format()
yuki-kimoto authored on 2010-05-26
1425
    # Fetch
1426
    while (my $row = $result->fetch) {
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1427
        
removed register_format()
yuki-kimoto authored on 2010-05-26
1428
    }
1429
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1430
    # Fetch as hash
removed register_format()
yuki-kimoto authored on 2010-05-26
1431
    while (my $row = $result->fetch_hash) {
1432
        
1433
    }
1434
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1435
    # Execute SQL with parameter.
1436
    $dbi->execute(
1437
        "select id from book where {= author} and {like title}",
1438
        param  => {author => 'ken', title => '%Perl%'}
1439
    );
1440
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1441
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
1442

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

            
1445
=head1 FEATURES
removed reconnect method
yuki-kimoto authored on 2010-05-28
1446

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1451
There are many basic methods to execute various queries.
1452
C<insert()>, C<update()>, C<update_all()>,C<delete()>,
1453
C<delete_all()>, C<select()>,
1454
C<insert_at()>, C<update_at()>, 
1455
C<delete_at()>, C<select_at()>, C<execute()>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1456

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1457
=item *
1458

            
1459
Filter when data is send or receive.
1460

            
1461
=item *
1462

            
1463
Data filtering system
1464

            
1465
=item *
1466

            
1467
Model support.
1468

            
1469
=item *
1470

            
1471
Generate where clause dinamically.
1472

            
1473
=item *
1474

            
1475
Generate join clause dinamically.
1476

            
1477
=back
pod fix
Yuki Kimoto authored on 2011-01-21
1478

            
1479
=head1 GUIDE
1480

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1512
=head2 C<default_dbi_option>
1513

            
1514
    my $default_dbi_option = $dbi->default_dbi_option;
1515
    $dbi            = $dbi->default_dbi_option($default_dbi_option);
1516

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1520
    {
1521
        RaiseError => 1,
1522
        PrintError => 0,
1523
        AutoCommit => 1,
1524
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
1525

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

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

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

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

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

            
1538
    my $models = $dbi->models;
1539
    $dbi       = $dbi->models(\%models);
1540

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1543
=head2 C<password>
1544

            
1545
    my $password = $dbi->password;
1546
    $dbi         = $dbi->password('lkj&le`@s');
1547

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

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1574
    my $user = $dbi->user;
1575
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1576

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

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

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

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

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1587
    $dbi->apply_filter(
cleanup
Yuki Kimoto authored on 2011-03-10
1588
        'book',
update pod
Yuki Kimoto authored on 2011-03-13
1589
        'issue_date' => {
1590
            out => 'tp_to_date',
1591
            in  => 'date_to_tp',
1592
            end => 'tp_to_displaydate'
1593
        },
1594
        'write_date' => {
1595
            out => 'tp_to_date',
1596
            in  => 'date_to_tp',
1597
            end => 'tp_to_displaydate'
1598
        }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1599
    );
1600

            
update pod
Yuki Kimoto authored on 2011-03-13
1601
Apply filter to columns.
1602
C<out> filter is executed before data is send to database.
1603
C<in> filter is executed after a row is fetch.
1604
C<end> filter is execute after C<in> filter is executed.
1605

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1608
       PETTERN         EXAMPLE
1609
    1. Column        : author
1610
    2. Table.Column  : book.author
1611
    3. Table__Column : book__author
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1612

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

            
1616
You can set multiple filters at once.
1617

            
1618
    $dbi->apply_filter(
1619
        'book',
1620
        [qw/issue_date write_date/] => {
1621
            out => 'tp_to_date',
1622
            in  => 'date_to_tp',
1623
            end => 'tp_to_displaydate'
1624
        }
1625
    );
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1626

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

            
1629
    $dbi          = $dbi->cache_method(\&cache_method);
1630
    $cache_method = $dbi->cache_method
1631

            
update pod
Yuki Kimoto authored on 2011-03-13
1632
Method to set and get cache.
1633
Default to the following one.
1634

            
1635
    sub {
1636
        my $self = shift;
1637
        
1638
        $self->{_cached} ||= {};
1639
        
1640
        if (@_ > 1) {
1641
            $self->{_cached}{$_[0]} = $_[1];
1642
        }
1643
        else {
1644
            return $self->{_cached}{$_[0]};
1645
        }
1646
    }
update pod
Yuki Kimoto authored on 2011-03-13
1647

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1650
    my $dbi = DBIx::Custom->connect(
1651
        data_source => "dbi:mysql:database=dbname",
1652
        user => 'ken',
1653
        password => '!LFKD%$&',
1654
        dbi_option => {mysql_enable_utf8 => 1}
1655
    );
1656

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1663
=head2 C<create_query>
1664
    
1665
    my $query = $dbi->create_query(
update pod
Yuki Kimoto authored on 2011-03-13
1666
        "insert into book {insert_param title author};";
cleanup
yuki-kimoto authored on 2010-10-17
1667
    );
update document
yuki-kimoto authored on 2009-11-19
1668

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

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

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

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

            
1679
    my $dbh = $dbi->dbh;
1680
    $dbi    = $dbi->dbh($dbh);
1681

            
1682
Get and set database handle of L<DBI>.
1683

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1689
    my $result = $dbi->execute(
1690
        "select * from book where {= title} and {like author}",
1691
        param => {title => 'Perl', author => '%Ken%'}
1692
    );
1693

            
1694
Execute SQL, containing tags.
1695
Return value is L<DBIx::Custom::Result> in select statement, or
1696
the count of affected rows in insert, update, delete statement.
1697

            
1698
Tag is turned into the statement containing place holder
1699
before SQL is executed.
1700

            
1701
    select * from where title = ? and author like ?;
1702

            
1703
See also L<Tags/Tags>.
1704

            
1705
The following opitons are currently available.
1706

            
1707
=over 4
1708

            
1709
=item C<filter>
1710

            
1711
Filter, executed before data is send to database. This is array reference.
1712
Filter value is code reference or
1713
filter name registerd by C<register_filter()>.
1714

            
1715
    # Basic
1716
    $dbi->execute(
1717
        $sql,
1718
        filter => [
1719
            title  => sub { uc $_[0] }
1720
            author => sub { uc $_[0] }
1721
        ]
1722
    );
1723
    
1724
    # At once
1725
    $dbi->execute(
1726
        $sql,
1727
        filter => [
1728
            [qw/title author/]  => sub { uc $_[0] }
1729
        ]
1730
    );
1731
    
1732
    # Filter name
1733
    $dbi->execute(
1734
        $sql,
1735
        filter => [
1736
            title  => 'upper_case',
1737
            author => 'upper_case'
1738
        ]
1739
    );
1740

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

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

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

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

            
1749
Delete statement.
1750

            
1751
The following opitons are currently available.
1752

            
update pod
Yuki Kimoto authored on 2011-03-13
1753
=over 4
1754

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

            
1757
Table name.
1758

            
1759
    $dbi->delete(table => 'book');
1760

            
1761
=item C<where>
1762

            
1763
Where clause. This is hash reference or L<DBIx::Custom::Where> object.
1764
    
1765
    # Hash reference
1766
    $dbi->delete(where => {title => 'Perl'});
1767
    
1768
    # DBIx::Custom::Where object
1769
    my $where = $dbi->where(
1770
        clause => ['and', '{= author}', '{like title}'],
1771
        param  => {author => 'Ken', title => '%Perl%'}
1772
    );
1773
    $dbi->delete(where => $where);
1774

            
1775
=item C<append>
1776

            
1777
Append statement to last of SQL. This is string.
1778

            
1779
    $dbi->delete(append => 'order by title');
1780

            
1781
=item C<filter>
1782

            
1783
Filter, executed before data is send to database. This is array reference.
1784
Filter value is code reference or
1785
filter name registerd by C<register_filter()>.
1786

            
1787
    # Basic
1788
    $dbi->delete(
1789
        filter => [
1790
            title  => sub { uc $_[0] }
1791
            author => sub { uc $_[0] }
1792
        ]
1793
    );
1794
    
1795
    # At once
1796
    $dbi->delete(
1797
        filter => [
1798
            [qw/title author/]  => sub { uc $_[0] }
1799
        ]
1800
    );
1801
    
1802
    # Filter name
1803
    $dbi->delete(
1804
        filter => [
1805
            title  => 'upper_case',
1806
            author => 'upper_case'
1807
        ]
1808
    );
1809

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

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

            
1814
Get L<DBIx::Custom::Query> object instead of executing SQL.
1815
This is true or false value.
1816

            
1817
    my $query = $dbi->delete(query => 1);
1818

            
1819
You can check SQL.
1820

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1823
=back
1824

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

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

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

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

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

            
1836
    $dbi->delete_at(
1837
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
1838
        primary_key => 'id',
1839
        where => '5'
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1840
    );
1841

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1846
=over 4
1847

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1850
Primary key. This is constant value or array reference.
1851
    
1852
    # Constant value
1853
    $dbi->delete(primary_key => 'id');
1854

            
1855
    # Array reference
1856
    $dbi->delete(primary_key => ['id1', 'id2' ]);
1857

            
1858
This is used to create where clause.
1859

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

            
1862
Where clause, created from primary key information.
1863
This is constant value or array reference.
1864

            
1865
    # Constant value
1866
    $dbi->delete(where => 5);
1867

            
1868
    # Array reference
1869
    $dbi->delete(where => [3, 5]);
1870

            
1871
In first examle, the following SQL is created.
1872

            
1873
    delete from book where id = ?;
1874

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1877
=back
1878

            
cleanup
yuki-kimoto authored on 2010-10-17
1879
=head2 C<insert>
1880

            
update pod
Yuki Kimoto authored on 2011-03-13
1881
    $dbi->insert(
1882
        table  => 'book', 
1883
        param  => {title => 'Perl', author => 'Ken'}
1884
    );
1885

            
1886
Insert statement.
1887

            
1888
The following opitons are currently available.
1889

            
update pod
Yuki Kimoto authored on 2011-03-13
1890
=over 4
1891

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

            
1894
Table name.
1895

            
1896
    $dbi->insert(table => 'book');
1897

            
1898
=item C<param>
1899

            
1900
Insert data. This is hash reference.
1901

            
1902
    $dbi->insert(param => {title => 'Perl'});
1903

            
1904
=item C<append>
1905

            
1906
Append statement to last of SQL. This is string.
1907

            
1908
    $dbi->insert(append => 'order by title');
1909

            
1910
=item C<filter>
1911

            
1912
Filter, executed before data is send to database. This is array reference.
1913
Filter value is code reference or
1914
filter name registerd by C<register_filter()>.
1915

            
1916
    # Basic
1917
    $dbi->insert(
1918
        filter => [
1919
            title  => sub { uc $_[0] }
1920
            author => sub { uc $_[0] }
1921
        ]
1922
    );
1923
    
1924
    # At once
1925
    $dbi->insert(
1926
        filter => [
1927
            [qw/title author/]  => sub { uc $_[0] }
1928
        ]
1929
    );
1930
    
1931
    # Filter name
1932
    $dbi->insert(
1933
        filter => [
1934
            title  => 'upper_case',
1935
            author => 'upper_case'
1936
        ]
1937
    );
1938

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

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

            
1943
Get L<DBIx::Custom::Query> object instead of executing SQL.
1944
This is true or false value.
1945

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

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

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

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

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

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

            
1958
    $dbi->insert_at(
1959
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
1960
        primary_key => 'id',
1961
        where => '5',
1962
        param => {title => 'Perl'}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
1963
    );
1964

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1969
=over 4
1970

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

            
1973
Primary key. This is constant value or array reference.
1974
    
1975
    # Constant value
1976
    $dbi->insert(primary_key => 'id');
1977

            
1978
    # Array reference
1979
    $dbi->insert(primary_key => ['id1', 'id2' ]);
1980

            
1981
This is used to create parts of insert data.
1982

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

            
1985
Parts of Insert data, create from primary key information.
1986
This is constant value or array reference.
1987

            
1988
    # Constant value
1989
    $dbi->insert(where => 5);
1990

            
1991
    # Array reference
1992
    $dbi->insert(where => [3, 5]);
1993

            
1994
In first examle, the following SQL is created.
1995

            
1996
    insert into book (id, title) values (?, ?);
1997

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2000
=back
2001

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

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

            
2006
Create insert parameter tag.
2007

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

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

            
pod fix
Yuki Kimoto authored on 2011-01-21
2012
    $dbi->each_column(
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
2013
        sub {
update pod
Yuki Kimoto authored on 2011-03-13
2014
            my ($dbi, $table, $column, $column_info) = @_;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
2015
            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
2016
            my $type = $column_info->{TYPE_NAME};
pod fix
Yuki Kimoto authored on 2011-01-21
2017
            
2018
            if ($type eq 'DATE') {
2019
                # ...
2020
            }
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
2021
        }
2022
    );
update pod
Yuki Kimoto authored on 2011-03-13
2023

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

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

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2036
    lib / MyModel.pm
2037
        / MyModel / book.pm
2038
                  / company.pm
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2039

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

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

            
2044
    package MyModel;
2045
    
2046
    use base 'DBIx::Custom::Model';
update pod
Yuki Kimoto authored on 2011-03-13
2047
    
2048
    1;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2049

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2054
    package MyModel::book;
2055
    
2056
    use base 'MyModel';
2057
    
2058
    1;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2059

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2062
    package MyModel::company;
2063
    
2064
    use base 'MyModel';
2065
    
2066
    1;
2067
    
2068
MyModel::book and MyModel::company is included by C<include_model()>.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2069

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

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

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

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

            
2079
    $dbi->method(
2080
        update_or_insert => sub {
2081
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-03-13
2082
            
2083
            # Process
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2084
        },
2085
        find_or_create   => sub {
2086
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-03-13
2087
            
2088
            # Process
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2089
        }
2090
    );
2091

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

            
2094
    $dbi->update_or_insert;
2095
    $dbi->find_or_create;
2096

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

            
2099
    $dbi->model('book')->method(
2100
        insert => sub { ... },
2101
        update => sub { ... }
2102
    );
2103
    
2104
    my $model = $dbi->model('book');
2105

            
2106
Set and get a L<DBIx::Custom::Model> object,
2107

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2110
    my $dbi = DBIx::Custom->new(
2111
        data_source => "dbi:mysql:database=dbname",
2112
        user => 'ken',
2113
        password => '!LFKD%$&',
2114
        dbi_option => {mysql_enable_utf8 => 1}
2115
    );
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2116

            
2117
Create a new L<DBIx::Custom> object.
2118

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

            
2121
    my $not_exists = $dbi->not_exists;
2122

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

            
cleanup
yuki-kimoto authored on 2010-10-17
2126
=head2 C<register_filter>
2127

            
update pod
Yuki Kimoto authored on 2011-03-13
2128
    $dbi->register_filter(
2129
        # Time::Piece object to database DATE format
2130
        tp_to_date => sub {
2131
            my $tp = shift;
2132
            return $tp->strftime('%Y-%m-%d');
2133
        },
2134
        # database DATE format to Time::Piece object
2135
        date_to_tp => sub {
2136
           my $date = shift;
2137
           return Time::Piece->strptime($date, '%Y-%m-%d');
2138
        }
2139
    );
cleanup
yuki-kimoto authored on 2010-10-17
2140
    
update pod
Yuki Kimoto authored on 2011-03-13
2141
Register filters, used by C<filter> option of many methods.
cleanup
yuki-kimoto authored on 2010-10-17
2142

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2145
    $dbi->register_tag(
2146
        update => sub {
2147
            my @columns = @_;
2148
            
2149
            # Update parameters
2150
            my $s = 'set ';
2151
            $s .= "$_ = ?, " for @columns;
2152
            $s =~ s/, $//;
2153
            
2154
            return [$s, \@columns];
2155
        }
2156
    );
cleanup
yuki-kimoto authored on 2010-10-17
2157

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

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

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

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

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

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

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

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

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

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

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
2181
    my $result = $dbi->select(
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2182
        table  => 'book',
2183
        column => ['author', 'title'],
2184
        where  => {author => 'Ken'},
select method column option ...
Yuki Kimoto authored on 2011-02-22
2185
    );
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2186
    
update pod
Yuki Kimoto authored on 2011-03-12
2187
Select statement.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2188

            
2189
The following opitons are currently available.
2190

            
2191
=over 4
2192

            
2193
=item C<table>
2194

            
2195
Table name.
2196

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

            
2199
=item C<column>
2200

            
2201
Column clause. This is array reference or constant value.
2202

            
2203
    # Hash refernce
2204
    $dbi->select(column => ['author', 'title']);
2205
    
2206
    # Constant value
2207
    $dbi->select(column => 'author');
2208

            
2209
Default is '*' unless C<column> is specified.
2210

            
2211
    # Default
2212
    $dbi->select(column => '*');
2213

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

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

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

            
2220
If main table is C<book> and joined table is C<company>,
2221
This create the following column clause.
2222

            
2223
    book.author as author
2224
    book.company_id as company_id
2225
    company.id as company__id
2226
    company.name as company__name
2227

            
2228
Columns of main table is consist of only column name,
2229
Columns of joined table is consist of table and column name joined C<__>.
2230

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

            
2234
    # Generally do the following way before using all_column option
2235
    $dbi->include_model('MyModel')->setup_model;
2236

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
2237
You can also specify table names to C<all_column>.
2238

            
2239
    $dbi->select(all_column => ['book', 'company']);
2240

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

            
2243
Where clause. This is hash reference or L<DBIx::Custom::Where> object.
2244
    
2245
    # Hash reference
update pod
Yuki Kimoto authored on 2011-03-12
2246
    $dbi->select(where => {author => 'Ken', 'title' => 'Perl'});
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2247
    
update pod
Yuki Kimoto authored on 2011-03-12
2248
    # DBIx::Custom::Where object
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2249
    my $where = $dbi->where(
2250
        clause => ['and', '{= author}', '{like title}'],
2251
        param  => {author => 'Ken', title => '%Perl%'}
2252
    );
update pod
Yuki Kimoto authored on 2011-03-12
2253
    $dbi->select(where => $where);
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2254

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

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

            
2259
    $dbi->select(join =>
2260
        [
2261
            'left outer join company on book.company_id = company_id',
2262
            'left outer join location on company.location_id = location.id'
2263
        ]
2264
    );
2265

            
2266
If column cluase or where clause contain table name like "company.name",
2267
needed join clause is used automatically.
2268

            
2269
    $dbi->select(
2270
        table => 'book',
2271
        column => ['company.location_id as company__location_id'],
2272
        where => {'company.name' => 'Orange'},
2273
        join => [
2274
            'left outer join company on book.company_id = company.id',
2275
            'left outer join location on company.location_id = location.id'
2276
        ]
2277
    );
2278

            
2279
In above select, the following SQL is created.
2280

            
2281
    select company.location_id as company__location_id
2282
    from book
2283
      left outer join company on book.company_id = company.id
2284
    where company.name = Orange
2285

            
2286
=item C<append>
2287

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

            
2290
    $dbi->select(append => 'order by title');
2291

            
2292
=item C<filter>
2293

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

            
2298
    # Basic
2299
    $dbi->select(
2300
        filter => [
2301
            title  => sub { uc $_[0] }
2302
            author => sub { uc $_[0] }
2303
        ]
2304
    );
2305
    
2306
    # At once
2307
    $dbi->select(
2308
        filter => [
2309
            [qw/title author/]  => sub { uc $_[0] }
2310
        ]
2311
    );
2312
    
2313
    # Filter name
2314
    $dbi->select(
2315
        filter => [
2316
            title  => 'upper_case',
2317
            author => 'upper_case'
2318
        ]
2319
    );
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
2320

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

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

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

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

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

            
2332
    my $sql = $query->sql;
2333

            
2334
=back
cleanup
Yuki Kimoto authored on 2011-03-08
2335

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

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

            
2340
    $dbi->select_at(
2341
        table => 'book',
2342
        primary_key => 'id',
2343
        where => '5'
2344
    );
2345

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2350
=over 4
2351

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

            
update pod
Yuki Kimoto authored on 2011-03-12
2354
Primary key. This is constant value or array reference.
2355
    
2356
    # Constant value
2357
    $dbi->select(primary_key => 'id');
2358

            
2359
    # Array reference
2360
    $dbi->select(primary_key => ['id1', 'id2' ]);
2361

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

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

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

            
2369
    # Constant value
2370
    $dbi->select(where => 5);
2371

            
2372
    # Array reference
2373
    $dbi->select(where => [3, 5]);
2374

            
2375
In first examle, the following SQL is created.
2376

            
2377
    select * from book where id = ?
2378

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2381
=back
2382

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2385
    $dbi->update(
2386
        table  => 'book',
2387
        param  => {title => 'Perl'},
2388
        where  => {id => 4}
2389
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
2390

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2395
=over 4
2396

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2399
Table name.
2400

            
2401
    $dbi->update(table => 'book');
2402

            
2403
=item C<param>
2404

            
2405
Update data. This is hash reference.
2406

            
2407
    $dbi->update(param => {title => 'Perl'});
2408

            
2409
=item C<where>
2410

            
2411
Where clause. This is hash reference or L<DBIx::Custom::Where> object.
2412
    
2413
    # Hash reference
2414
    $dbi->update(where => {author => 'Ken', 'title' => 'Perl'});
2415
    
2416
    # DBIx::Custom::Where object
2417
    my $where = $dbi->where(
2418
        clause => ['and', '{= author}', '{like title}'],
2419
        param  => {author => 'Ken', title => '%Perl%'}
2420
    );
2421
    $dbi->update(where => $where);
2422

            
2423
=item C<append>
2424

            
2425
Append statement to last of SQL. This is string.
2426

            
2427
    $dbi->update(append => 'order by title');
2428

            
2429
=item C<filter>
2430

            
2431
Filter, executed before data is send to database. This is array reference.
2432
Filter value is code reference or
2433
filter name registerd by C<register_filter()>.
2434

            
2435
    # Basic
2436
    $dbi->update(
2437
        filter => [
2438
            title  => sub { uc $_[0] }
2439
            author => sub { uc $_[0] }
2440
        ]
2441
    );
2442
    
2443
    # At once
2444
    $dbi->update(
2445
        filter => [
2446
            [qw/title author/]  => sub { uc $_[0] }
2447
        ]
2448
    );
2449
    
2450
    # Filter name
2451
    $dbi->update(
2452
        filter => [
2453
            title  => 'upper_case',
2454
            author => 'upper_case'
2455
        ]
2456
    );
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2457

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

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

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

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

            
2467
You can check SQL.
2468

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2471
=back
2472

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

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

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

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

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

            
2484
    $dbi->update_at(
2485
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
2486
        primary_key => 'id',
2487
        where => '5',
2488
        param => {title => 'Perl'}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2489
    );
2490

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2495
=over 4
2496

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

            
2499
Primary key. This is constant value or array reference.
2500
    
2501
    # Constant value
2502
    $dbi->update(primary_key => 'id');
2503

            
2504
    # Array reference
2505
    $dbi->update(primary_key => ['id1', 'id2' ]);
2506

            
2507
This is used to create where clause.
2508

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

            
2511
Where clause, created from primary key information.
2512
This is constant value or array reference.
2513

            
2514
    # Constant value
2515
    $dbi->update(where => 5);
2516

            
2517
    # Array reference
2518
    $dbi->update(where => [3, 5]);
2519

            
2520
In first examle, the following SQL is created.
2521

            
2522
    update book set title = ? where id = ?
2523

            
2524
Place holders are set to 'Perl' and 5.
2525

            
update pod
Yuki Kimoto authored on 2011-03-13
2526
=back
2527

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

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

            
2532
Create update parameter tag.
2533

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

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

            
cleanup
Yuki Kimoto authored on 2011-03-09
2538
    my $where = $dbi->where(
2539
        clause => ['and', '{= title}', '{= author}'],
2540
        param => {title => 'Perl', author => 'Ken'}
2541
    );
fix tests
Yuki Kimoto authored on 2011-01-18
2542

            
2543
Create a new L<DBIx::Custom::Where> object.
2544

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

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

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

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
2552
=head2 C<view> EXPERIMENTAL
2553

            
2554
    # Register view
2555
    $dbi->view(
2556
        book_issue_data
2557
          => 'select id, DATE(issue_datatime) as issue_date from book');
2558
    );
2559
    
2560
    # Get view
2561
    my $view = $dbi->view('book_issue_date');
2562

            
2563
View.
2564

            
2565
C<view()> return the following statement when you get a view.
2566

            
2567
    (select id, DATE(issue_datetime) from book) as book_issue_date
2568

            
2569
You can use this view in from clause
2570

            
2571
    "select issue_date from " . $dbi->view('book_issue_date')
2572

            
cleanup
Yuki Kimoto authored on 2011-01-25
2573
=head1 Tags
2574

            
2575
The following tags is available.
2576

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

            
2579
Table tag
2580

            
2581
    {table TABLE}    ->    TABLE
2582

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
2585
=head2 C<?>
2586

            
2587
Placeholder tag.
2588

            
2589
    {? NAME}    ->   ?
2590

            
2591
=head2 C<=>
2592

            
2593
Equal tag.
2594

            
2595
    {= NAME}    ->   NAME = ?
2596

            
2597
=head2 C<E<lt>E<gt>>
2598

            
2599
Not equal tag.
2600

            
2601
    {<> NAME}   ->   NAME <> ?
2602

            
2603
=head2 C<E<lt>>
2604

            
2605
Lower than tag
2606

            
2607
    {< NAME}    ->   NAME < ?
2608

            
2609
=head2 C<E<gt>>
2610

            
2611
Greater than tag
2612

            
2613
    {> NAME}    ->   NAME > ?
2614

            
2615
=head2 C<E<gt>=>
2616

            
2617
Greater than or equal tag
2618

            
2619
    {>= NAME}   ->   NAME >= ?
2620

            
2621
=head2 C<E<lt>=>
2622

            
2623
Lower than or equal tag
2624

            
2625
    {<= NAME}   ->   NAME <= ?
2626

            
2627
=head2 C<like>
2628

            
2629
Like tag
2630

            
2631
    {like NAME}   ->   NAME like ?
2632

            
2633
=head2 C<in>
2634

            
2635
In tag.
2636

            
2637
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
2638

            
2639
=head2 C<insert_param>
2640

            
2641
Insert parameter tag.
2642

            
2643
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
2644

            
2645
=head2 C<update_param>
2646

            
2647
Updata parameter tag.
2648

            
2649
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
2650

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

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

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

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

            
2660
C<< <kimoto.yuki at gmail.com> >>
2661

            
2662
L<http://github.com/yuki-kimoto/DBIx-Custom>
2663

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
2664
=head1 AUTHOR
2665

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

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

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

            
2672
This program is free software; you can redistribute it and/or modify it
2673
under the same terms as Perl itself.
2674

            
2675
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
2676

            
2677