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

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
3
our $VERSION = '0.1659';
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,
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
25
    dbi_option => sub { {} },
26
    default_dbi_option => sub {{
27
        RaiseError => 1,
28
        PrintError => 0,
29
        AutoCommit => 1
30
    }},
add models() attribute
Yuki Kimoto authored on 2011-02-21
31
    models => sub { {} },
cleanup
Yuki Kimoto authored on 2011-01-23
32
    query_builder => sub { DBIx::Custom::QueryBuilder->new },
many changed
Yuki Kimoto authored on 2011-01-23
33
    result_class  => 'DBIx::Custom::Result',
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
34
    safety_character => '\w',
update pod
Yuki Kimoto authored on 2011-02-07
35
    stash => sub { {} }
many changed
Yuki Kimoto authored on 2011-01-23
36
);
37

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
38
sub dbh {
39
    my $self = shift;
40

            
41
    if (@_) {
42
        $self->{dbh} = $_[0];
43
        return $self;
44
    }
45
    else {
46
        my $pid = $$;
47
        if ($self->pid eq $pid) {
48
            return $self->{dbh};
49
        }
50
        else {
51
            # Create new connection in child process
52
            croak "Process is forked in transaction"
53
              unless $self->{dbh}->{AutoCommit};
54
            $self->pid($pid);
55
            $self->{dbh}->{InactiveDestroy} = 1;
56
            return $self->{dbh} = $self->_connect;
57
        }
58
    }
59
}
60

            
many changed
Yuki Kimoto authored on 2011-01-23
61
__PACKAGE__->attr(
62
    cache_method => sub {
63
        sub {
64
            my $self = shift;
65
            
66
            $self->{_cached} ||= {};
67
            
68
            if (@_ > 1) {
69
                $self->{_cached}{$_[0]} = $_[1] 
70
            }
71
            else {
72
                return $self->{_cached}{$_[0]}
73
            }
74
        }
75
    }
76
);
77

            
78
__PACKAGE__->attr(
fix tests
Yuki Kimoto authored on 2011-01-13
79
    filters => sub {
80
        {
81
            encode_utf8 => sub { encode_utf8($_[0]) },
82
            decode_utf8 => sub { decode_utf8($_[0]) }
83
        }
many changed
Yuki Kimoto authored on 2011-01-23
84
    }
fix tests
Yuki Kimoto authored on 2011-01-13
85
);
cleanup
yuki-kimoto authored on 2010-10-17
86

            
added helper method
yuki-kimoto authored on 2010-10-17
87
our $AUTOLOAD;
88
sub AUTOLOAD {
89
    my $self = shift;
90

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

            
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
94
    # Method
95
    $self->{_methods} ||= {};
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
96
    if (my $method = $self->{_methods}->{$mname}) {
97
        return $self->$method(@_)
98
    }
99
    elsif ($self->dbh->can($mname)) {
100
        $self->dbh->$mname(@_);
101
    }
102
    else {
103
        croak qq/Can't locate object method "$mname" via "$package"/
104
    }
added helper method
yuki-kimoto authored on 2010-10-17
105
}
106

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

            
110
    # Initialize filters
cleanup
Yuki Kimoto authored on 2011-01-12
111
    $self->{filter} ||= {};
many changed
Yuki Kimoto authored on 2011-01-23
112
    $self->{filter}{out} ||= {};
113
    $self->{filter}{in} ||= {};
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
114
    $self->{filter}{end} ||= {};
cleanup
Yuki Kimoto authored on 2010-12-22
115
    
many changed
Yuki Kimoto authored on 2011-01-23
116
    # Create filters
117
    my $usage = "Usage: \$dbi->apply_filter(" .
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
118
                "TABLE, COLUMN1, {in => INFILTER1, out => OUTFILTER1, end => ENDFILTER1}, " .
119
                "COLUMN2, {in => INFILTER2, out => OUTFILTER2, end => ENDFILTER2}, ...)";
many changed
Yuki Kimoto authored on 2011-01-23
120

            
121
    for (my $i = 0; $i < @cinfos; $i += 2) {
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
122
        
many changed
Yuki Kimoto authored on 2011-01-23
123
        # Column
124
        my $column = $cinfos[$i];
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
125
        
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
126
        if (ref $column eq 'ARRAY') {
127
            foreach my $c (@$column) {
128
                push @cinfos, $c, $cinfos[$i + 1];
129
            }
130
            next;
131
        }
132
        
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
133
        # Filter info
134
        my $finfo = $cinfos[$i + 1] || {};
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
135
        croak "$usage (table: $table)" unless  ref $finfo eq 'HASH';
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
136
        foreach my $ftype (keys %$finfo) {
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
137
            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
138
                             || $ftype eq 'end'; 
many changed
Yuki Kimoto authored on 2011-01-23
139
        }
140
        
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
141
        foreach my $way (qw/in out end/) {
142
            my $filter = $finfo->{$way};
cleanup
Yuki Kimoto authored on 2010-12-22
143
            
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
144
            # State
145
            my $state = !exists $finfo->{$way} ? 'not_exists'
146
                      : !defined $filter        ? 'not_defined'
147
                      : ref $filter eq 'CODE'   ? 'code'
148
                      : 'name';
149
            
150
            next if $state eq 'not_exists';
151
            
152
            # Check filter
153
            croak qq{Filter "$filter" is not registered}
154
              if  $state eq 'name'
155
               && ! exists $self->filters->{$filter};
156
            
157
            # Filter
158
            my $f = $state eq 'not_defined' ? undef
159
                  : $state eq 'code'        ? $filter
160
                  : $self->filters->{$filter};
161
            $self->{filter}{$way}{$table}{$column} = $f;
162
            $self->{filter}{$way}{$table}{"$table.$column"} = $f;
163
            $self->{filter}{$way}{$table}{"${table}__$column"} = $f;
many changed
Yuki Kimoto authored on 2011-01-23
164
        }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
165
    }
166
    
many changed
Yuki Kimoto authored on 2011-01-23
167
    return $self;
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
168
}
169

            
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
170
sub method {
added helper method
yuki-kimoto authored on 2010-10-17
171
    my $self = shift;
172
    
173
    # Merge
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
174
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
175
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
added helper method
yuki-kimoto authored on 2010-10-17
176
    
177
    return $self;
178
}
179

            
packaging one directory
yuki-kimoto authored on 2009-11-16
180
sub connect {
cleanup
Yuki Kimoto authored on 2011-01-25
181
    my $self = ref $_[0] ? shift : shift->new(@_);;
removed register_format()
yuki-kimoto authored on 2010-05-26
182
    
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
183
    my $dbh = $self->_connect;
packaging one directory
yuki-kimoto authored on 2009-11-16
184
    
update document
yuki-kimoto authored on 2010-01-30
185
    # Database handle
packaging one directory
yuki-kimoto authored on 2009-11-16
186
    $self->dbh($dbh);
update document
yuki-kimoto authored on 2010-01-30
187
    
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
188
    # Process ID
189
    $self->pid($$);
190
    
packaging one directory
yuki-kimoto authored on 2009-11-16
191
    return $self;
192
}
193

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

            
cleanup
yuki-kimoto authored on 2010-10-17
216
        # Create SQL object
217
        my $builder = $self->query_builder;
218
        
219
        # Create query
220
        $query = $builder->build_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
221

            
cleanup
yuki-kimoto authored on 2010-10-17
222
        # Cache query
223
        $self->cache_method->($self, $source,
224
                             {sql     => $query->sql, 
add table tag
Yuki Kimoto authored on 2011-02-09
225
                              columns => $query->columns,
226
                              tables  => $query->tables})
cleanup
yuki-kimoto authored on 2010-10-17
227
          if $cache;
cleanup insert
yuki-kimoto authored on 2010-04-28
228
    }
229
    
cleanup
yuki-kimoto authored on 2010-10-17
230
    # Prepare statement handle
231
    my $sth;
232
    eval { $sth = $self->dbh->prepare($query->{sql})};
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
233
    $self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@;
packaging one directory
yuki-kimoto authored on 2009-11-16
234
    
cleanup
yuki-kimoto authored on 2010-10-17
235
    # Set statement handle
236
    $query->sth($sth);
packaging one directory
yuki-kimoto authored on 2009-11-16
237
    
cleanup
Yuki Kimoto authored on 2011-02-09
238
    # Set filters
239
    $query->filters($self->filters);
240
    
cleanup
yuki-kimoto authored on 2010-10-17
241
    return $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
242
}
243

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-01-27
286
    # SQL stack
287
    my @sql;
288

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

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

            
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
309
our %VALID_DELETE_AT_ARGS
310
  = map { $_ => 1 } qw/table where append filter query
311
                       primary_key param/;
312

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

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

            
added helper method
yuki-kimoto authored on 2010-10-17
351
sub DESTROY { }
352

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

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

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

            
448
        return $result;
449
    }
450
    return $affected;
451
}
452

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

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

            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
505
our %VALID_INSERT_AT_ARGS
506
  = map { $_ => 1 } qw/table param
507
                       where append filter query
508
                       primary_key param/;
509

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1021
our %VALID_UPDATE_AT_ARGS
1022
  = map { $_ => 1 } qw/table param
1023
                       where append filter query
1024
                       primary_key param/;
1025

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

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

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

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

            
1085
    return DBIx::Custom::Where->new(
1086
        query_builder => $self->query_builder,
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1087
        safety_character => $self->safety_character,
cleanup
Yuki Kimoto authored on 2011-03-09
1088
        @_
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1089
    );
cleanup
Yuki Kimoto authored on 2011-01-25
1090
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1091

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

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

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

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1182
sub _need_tables {
1183
    my ($self, $tree, $need_tables, $tables) = @_;
1184
    
1185
    foreach my $table (@$tables) {
1186
        
1187
        if ($tree->{$table}) {
1188
            $need_tables->{$table} = 1;
1189
            $self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}])
1190
        }
1191
    }
1192
}
1193

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

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

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1240
    foreach my $need_table (@need_tables) {
1241
        push @$sql, $tree->{$need_table}{join};
1242
    }
1243
}
cleanup
Yuki Kimoto authored on 2011-03-08
1244

            
cleanup
Yuki Kimoto authored on 2011-01-25
1245
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-23
1246
__PACKAGE__->attr(
1247
    dbi_options => sub { {} },
1248
    filter_check  => 1
1249
);
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1250

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
1273
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1274
sub default_fetch_filter {
1275
    my $self = shift;
1276
    
1277
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
1278
        my $fname = $_[0];
1279

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
1296
# DEPRECATED!
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1297
sub register_tag_processor {
1298
    return shift->query_builder->register_tag_processor(@_);
1299
}
1300

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

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1339
=head1 NAME
1340

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

            
1343
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
1344

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

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

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

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

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

            
1412
=head1 FEATURES
removed reconnect method
yuki-kimoto authored on 2010-05-28
1413

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1418
There are many basic methods to execute various queries.
1419
C<insert()>, C<update()>, C<update_all()>,C<delete()>,
1420
C<delete_all()>, C<select()>,
1421
C<insert_at()>, C<update_at()>, 
1422
C<delete_at()>, C<select_at()>, C<execute()>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1423

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1424
=item *
1425

            
1426
Filter when data is send or receive.
1427

            
1428
=item *
1429

            
1430
Data filtering system
1431

            
1432
=item *
1433

            
1434
Model support.
1435

            
1436
=item *
1437

            
1438
Generate where clause dinamically.
1439

            
1440
=item *
1441

            
1442
Generate join clause dinamically.
1443

            
1444
=back
pod fix
Yuki Kimoto authored on 2011-01-21
1445

            
1446
=head1 GUIDE
1447

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1479
=head2 C<default_dbi_option>
1480

            
1481
    my $default_dbi_option = $dbi->default_dbi_option;
1482
    $dbi            = $dbi->default_dbi_option($default_dbi_option);
1483

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1487
    {
1488
        RaiseError => 1,
1489
        PrintError => 0,
1490
        AutoCommit => 1,
1491
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
1492

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1493
You should not change C<AutoCommit> value directly
update pod
Yuki Kimoto authored on 2011-03-13
1494
to check if the process is in transaction.
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1495
L<DBIx::Custom> determin the process is in transaction
1496
if AutoCommit is 0. 
1497

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

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

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

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1505
=head2 C<models EXPERIMENTAL>
add models() attribute
Yuki Kimoto authored on 2011-02-21
1506

            
1507
    my $models = $dbi->models;
1508
    $dbi       = $dbi->models(\%models);
1509

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1512
=head2 C<password>
1513

            
1514
    my $password = $dbi->password;
1515
    $dbi         = $dbi->password('lkj&le`@s');
1516

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

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

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

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

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

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

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

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1533
=head2 C<safety_character EXPERIMENTAL>
update pod
Yuki Kimoto authored on 2011-01-27
1534

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1538
Regex of safety character consist of table and column name, default to '\w'.
cleanup
Yuki Kimoto authored on 2011-03-10
1539
Note that you don't have to specify like '[\w]'.
update pod
Yuki Kimoto authored on 2011-01-27
1540

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1543
    my $user = $dbi->user;
1544
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1545

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

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

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

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1554
=head2 C<apply_filter EXPERIMENTAL>
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1555

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1556
    $dbi->apply_filter(
cleanup
Yuki Kimoto authored on 2011-03-10
1557
        'book',
1558
        $column1 => {out => $outfilter1, in => $infilter1, end => $endfilter1}
1559
        $column2 => {out => $outfilter2, in => $infilter2, end => $endfilter2}
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1560
        ...,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1561
    );
1562

            
cleanup
Yuki Kimoto authored on 2011-03-10
1563
Apply filter to specified column, C<out> is the direction from perl to database,
1564
C<in> is the direction from database to perl,
1565
C<end> is filter after C<in> filter.
cleanup
Yuki Kimoto authored on 2010-12-21
1566

            
cleanup
Yuki Kimoto authored on 2011-03-10
1567
You can use three column name to apply filter to column data.
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1568

            
cleanup
Yuki Kimoto authored on 2011-03-10
1569
                       (Example)
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1570
    1. column        : author
1571
    2. table.column  : book.author
1572
    3. table__column : book__author
1573

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

            
1576
    $dbi          = $dbi->cache_method(\&cache_method);
1577
    $cache_method = $dbi->cache_method
1578

            
1579
Method to set and get caches.
1580

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

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

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

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

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

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

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

            
1606
    my $dbh = $dbi->dbh;
1607
    $dbi    = $dbi->dbh($dbh);
1608

            
1609
Get and set database handle of L<DBI>.
1610

            
1611
If process is changed by forking, new connection is created
1612
and get new database hande ofL<DBI>. This feature is EXPERIMETNAL.
1613

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

            
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1616
    my $result = $dbi->execute($query,  param => $params, filter => \@filter);
1617
    my $result = $dbi->execute($source, param => $params, filter => \@filter);
update document
yuki-kimoto authored on 2009-11-19
1618

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

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

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

            
1628
Delete statement.
1629

            
1630
The following opitons are currently available.
1631

            
1632
=item C<table>
1633

            
1634
Table name.
1635

            
1636
    $dbi->delete(table => 'book');
1637

            
1638
=item C<where>
1639

            
1640
Where clause. This is hash reference or L<DBIx::Custom::Where> object.
1641
    
1642
    # Hash reference
1643
    $dbi->delete(where => {title => 'Perl'});
1644
    
1645
    # DBIx::Custom::Where object
1646
    my $where = $dbi->where(
1647
        clause => ['and', '{= author}', '{like title}'],
1648
        param  => {author => 'Ken', title => '%Perl%'}
1649
    );
1650
    $dbi->delete(where => $where);
1651

            
1652
=item C<append>
1653

            
1654
Append statement to last of SQL. This is string.
1655

            
1656
    $dbi->delete(append => 'order by title');
1657

            
1658
=item C<filter>
1659

            
1660
Filter, executed before data is send to database. This is array reference.
1661
Filter value is code reference or
1662
filter name registerd by C<register_filter()>.
1663

            
1664
    # Basic
1665
    $dbi->delete(
1666
        filter => [
1667
            title  => sub { uc $_[0] }
1668
            author => sub { uc $_[0] }
1669
        ]
1670
    );
1671
    
1672
    # At once
1673
    $dbi->delete(
1674
        filter => [
1675
            [qw/title author/]  => sub { uc $_[0] }
1676
        ]
1677
    );
1678
    
1679
    # Filter name
1680
    $dbi->delete(
1681
        filter => [
1682
            title  => 'upper_case',
1683
            author => 'upper_case'
1684
        ]
1685
    );
1686

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

            
1689
=item C<query EXPERIMENTAL>
1690

            
1691
Get L<DBIx::Custom::Query> object instead of executing SQL.
1692
This is true or false value.
1693

            
1694
    my $query = $dbi->delete(query => 1);
1695

            
1696
You can check SQL.
1697

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

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

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

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

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

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

            
1711
    $dbi->delete_at(
1712
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
1713
        primary_key => 'id',
1714
        where => '5'
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1715
    );
1716

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1721
=head2 C<primary_key>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1722

            
update pod
Yuki Kimoto authored on 2011-03-13
1723
Primary key. This is constant value or array reference.
1724
    
1725
    # Constant value
1726
    $dbi->delete(primary_key => 'id');
1727

            
1728
    # Array reference
1729
    $dbi->delete(primary_key => ['id1', 'id2' ]);
1730

            
1731
This is used to create where clause.
1732

            
1733
=head2 C<where>
1734

            
1735
Where clause, created from primary key information.
1736
This is constant value or array reference.
1737

            
1738
    # Constant value
1739
    $dbi->delete(where => 5);
1740

            
1741
    # Array reference
1742
    $dbi->delete(where => [3, 5]);
1743

            
1744
In first examle, the following SQL is created.
1745

            
1746
    delete from book where id = ?;
1747

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1750
=head2 C<insert>
1751

            
update pod
Yuki Kimoto authored on 2011-03-13
1752
    $dbi->insert(
1753
        table  => 'book', 
1754
        param  => {title => 'Perl', author => 'Ken'}
1755
    );
1756

            
1757
Insert statement.
1758

            
1759
The following opitons are currently available.
1760

            
1761
=item C<table>
1762

            
1763
Table name.
1764

            
1765
    $dbi->insert(table => 'book');
1766

            
1767
=item C<param>
1768

            
1769
=item C<param>
1770

            
1771
Insert data. This is hash reference.
1772

            
1773
    $dbi->insert(param => {title => 'Perl'});
1774

            
1775
=item C<append>
1776

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

            
1779
    $dbi->insert(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->insert(
1789
        filter => [
1790
            title  => sub { uc $_[0] }
1791
            author => sub { uc $_[0] }
1792
        ]
1793
    );
1794
    
1795
    # At once
1796
    $dbi->insert(
1797
        filter => [
1798
            [qw/title author/]  => sub { uc $_[0] }
1799
        ]
1800
    );
1801
    
1802
    # Filter name
1803
    $dbi->insert(
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

            
1812
=item C<query EXPERIMENTAL>
1813

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

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

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

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

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

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

            
1827
    $dbi->insert_at(
1828
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
1829
        primary_key => 'id',
1830
        where => '5',
1831
        param => {title => 'Perl'}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
1832
    );
1833

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

            
1838
=head2 C<primary_key>
1839

            
1840
Primary key. This is constant value or array reference.
1841
    
1842
    # Constant value
1843
    $dbi->insert(primary_key => 'id');
1844

            
1845
    # Array reference
1846
    $dbi->insert(primary_key => ['id1', 'id2' ]);
1847

            
1848
This is used to create parts of insert data.
1849

            
1850
=head2 C<where>
1851

            
1852
Parts of Insert data, create from primary key information.
1853
This is constant value or array reference.
1854

            
1855
    # Constant value
1856
    $dbi->insert(where => 5);
1857

            
1858
    # Array reference
1859
    $dbi->insert(where => [3, 5]);
1860

            
1861
In first examle, the following SQL is created.
1862

            
1863
    insert into book (id, title) values (?, ?);
1864

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

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1867
=head2 C<insert_param EXPERIMENTAL>
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1868

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

            
1871
Create insert parameter tag.
1872

            
1873
    {title => 'a', age => 2}   ->   {insert_param title age}
1874

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1875
=head2 C<each_column EXPERIMENTAL>
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1876

            
pod fix
Yuki Kimoto authored on 2011-01-21
1877
    $dbi->each_column(
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1878
        sub {
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1879
            my ($self, $table, $column, $column_info) = @_;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1880
            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1881
            my $type = $column_info->{TYPE_NAME};
pod fix
Yuki Kimoto authored on 2011-01-21
1882
            
1883
            if ($type eq 'DATE') {
1884
                # ...
1885
            }
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1886
        }
1887
    );
pod fix
Yuki Kimoto authored on 2011-01-21
1888
Get column informations from database.
1889
Argument is callback.
1890
You can do anything in callback.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1891
Callback receive four arguments, dbi object, table name,
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1892
column name and column information.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1893

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1894
=head2 C<include_model EXPERIMENTAL>
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1895

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1896
    $dbi->include_model(
1897
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1898
            'book', 'person', 'company'
1899
        ]
1900
    );
1901

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1902
Include models. First argument is name space.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1903
Second argument is array reference of class base names.
1904

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1905
If you don't specify second argument, All models under name space is
1906
included.
1907

            
1908
    $dbi->include_model('MyModel');
1909

            
1910
Note that in this case name spece module is needed.
1911

            
1912
    # MyModel.pm
1913
    package MyModel;
1914
    
1915
    use base 'DBIx::Custom::Model';
1916

            
1917
The following model is instantiated and included.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1918

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1919
    MyModel::book
1920
    MyModel::person
1921
    MyModel::company
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1922

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1923
You can get these instance by C<model()>.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1924

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1925
    my $book_model = $dbi->model('book');
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1926

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1927
If you want to other name as model class,
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1928
you can do like this.
1929

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1930
    $dbi->include_model(
1931
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1932
            {'book' => 'Book'},
1933
            {'person' => 'Person'}
1934
        ]
1935
    );
1936

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1937
=head2 C<method EXPERIMENTAL>
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1938

            
1939
    $dbi->method(
1940
        update_or_insert => sub {
1941
            my $self = shift;
1942
            # do something
1943
        },
1944
        find_or_create   => sub {
1945
            my $self = shift;
1946
            # do something
1947
        }
1948
    );
1949

            
1950
Register method. These method is called from L<DBIx::Custom> object directory.
1951

            
1952
    $dbi->update_or_insert;
1953
    $dbi->find_or_create;
1954

            
1955
=head2 C<new>
1956

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

            
1960
Create a new L<DBIx::Custom> object.
1961

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1962
=head2 C<not_exists EXPERIMENTAL>
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1963

            
1964
    my $not_exists = $dbi->not_exists;
1965

            
1966
Get DBIx::Custom::NotExists object.
experimental extended select...
Yuki Kimoto authored on 2011-01-17
1967

            
cleanup
yuki-kimoto authored on 2010-10-17
1968
=head2 C<register_filter>
1969

            
1970
    $dbi->register_filter(%filters);
1971
    $dbi->register_filter(\%filters);
1972
    
1973
Register filter. Registered filters is available in the following attributes
1974
or arguments.
1975

            
1976
=over 4
1977

            
1978
=item *
1979

            
1980
C<filter> argument of C<insert()>, C<update()>,
1981
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1982
methods
1983

            
1984
=item *
1985

            
1986
C<execute()> method
1987

            
1988
=item *
1989

            
1990
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1991

            
1992
=item *
1993

            
1994
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1995

            
1996
=back
1997

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1998
=head2 C<register_tag>
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1999

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
2000
    $dbi->register_tag(
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
2001
        limit => sub {
2002
            ...;
2003
        }
2004
    );
2005

            
cleanup
Yuki Kimoto authored on 2011-01-25
2006
Register tag.
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
2007

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

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
2010
    my $result = $dbi->select(
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2011
        table  => 'book',
2012
        column => ['author', 'title'],
2013
        where  => {author => 'Ken'},
select method column option ...
Yuki Kimoto authored on 2011-02-22
2014
    );
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2015
    
update pod
Yuki Kimoto authored on 2011-03-12
2016
Select statement.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2017

            
2018
The following opitons are currently available.
2019

            
2020
=over 4
2021

            
2022
=item C<table>
2023

            
2024
Table name.
2025

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

            
2028
=item C<column>
2029

            
2030
Column clause. This is array reference or constant value.
2031

            
2032
    # Hash refernce
2033
    $dbi->select(column => ['author', 'title']);
2034
    
2035
    # Constant value
2036
    $dbi->select(column => 'author');
2037

            
2038
Default is '*' unless C<column> is specified.
2039

            
2040
    # Default
2041
    $dbi->select(column => '*');
2042

            
2043
=item C<all_column EXPERIMENTAL>
2044

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

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

            
2049
If main table is C<book> and joined table is C<company>,
2050
This create the following column clause.
2051

            
2052
    book.author as author
2053
    book.company_id as company_id
2054
    company.id as company__id
2055
    company.name as company__name
2056

            
2057
Columns of main table is consist of only column name,
2058
Columns of joined table is consist of table and column name joined C<__>.
2059

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

            
2063
    # Generally do the following way before using all_column option
2064
    $dbi->include_model('MyModel')->setup_model;
2065

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

            
2068
Where clause. This is hash reference or L<DBIx::Custom::Where> object.
2069
    
2070
    # Hash reference
update pod
Yuki Kimoto authored on 2011-03-12
2071
    $dbi->select(where => {author => 'Ken', 'title' => 'Perl'});
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2072
    
update pod
Yuki Kimoto authored on 2011-03-12
2073
    # DBIx::Custom::Where object
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2074
    my $where = $dbi->where(
2075
        clause => ['and', '{= author}', '{like title}'],
2076
        param  => {author => 'Ken', title => '%Perl%'}
2077
    );
update pod
Yuki Kimoto authored on 2011-03-12
2078
    $dbi->select(where => $where);
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2079

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

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

            
2084
    $dbi->select(join =>
2085
        [
2086
            'left outer join company on book.company_id = company_id',
2087
            'left outer join location on company.location_id = location.id'
2088
        ]
2089
    );
2090

            
2091
If column cluase or where clause contain table name like "company.name",
2092
needed join clause is used automatically.
2093

            
2094
    $dbi->select(
2095
        table => 'book',
2096
        column => ['company.location_id as company__location_id'],
2097
        where => {'company.name' => 'Orange'},
2098
        join => [
2099
            'left outer join company on book.company_id = company.id',
2100
            'left outer join location on company.location_id = location.id'
2101
        ]
2102
    );
2103

            
2104
In above select, the following SQL is created.
2105

            
2106
    select company.location_id as company__location_id
2107
    from book
2108
      left outer join company on book.company_id = company.id
2109
    where company.name = Orange
2110

            
2111
=item C<append>
2112

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

            
2115
    $dbi->select(append => 'order by title');
2116

            
2117
=item C<filter>
2118

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

            
2123
    # Basic
2124
    $dbi->select(
2125
        filter => [
2126
            title  => sub { uc $_[0] }
2127
            author => sub { uc $_[0] }
2128
        ]
2129
    );
2130
    
2131
    # At once
2132
    $dbi->select(
2133
        filter => [
2134
            [qw/title author/]  => sub { uc $_[0] }
2135
        ]
2136
    );
2137
    
2138
    # Filter name
2139
    $dbi->select(
2140
        filter => [
2141
            title  => 'upper_case',
2142
            author => 'upper_case'
2143
        ]
2144
    );
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
2145

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

            
update pod
Yuki Kimoto authored on 2011-03-12
2148
=item C<query EXPERIMENTAL>
cleanup
yuki-kimoto authored on 2010-08-09
2149

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

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

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

            
2157
    my $sql = $query->sql;
2158

            
2159
=back
cleanup
Yuki Kimoto authored on 2011-03-08
2160

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

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

            
2165
    $dbi->select_at(
2166
        table => 'book',
2167
        primary_key => 'id',
2168
        where => '5'
2169
    );
2170

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

            
update pod
Yuki Kimoto authored on 2011-03-12
2175
=head2 C<primary_key>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2176

            
update pod
Yuki Kimoto authored on 2011-03-12
2177
Primary key. This is constant value or array reference.
2178
    
2179
    # Constant value
2180
    $dbi->select(primary_key => 'id');
2181

            
2182
    # Array reference
2183
    $dbi->select(primary_key => ['id1', 'id2' ]);
2184

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

            
update pod
Yuki Kimoto authored on 2011-03-12
2187
=head2 C<where>
2188

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

            
2192
    # Constant value
2193
    $dbi->select(where => 5);
2194

            
2195
    # Array reference
2196
    $dbi->select(where => [3, 5]);
2197

            
2198
In first examle, the following SQL is created.
2199

            
2200
    select * from book where id = ?
2201

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2206
    $dbi->update(
2207
        table  => 'book',
2208
        param  => {title => 'Perl'},
2209
        where  => {id => 4}
2210
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
2211

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2216
=over 4
2217

            
2218
Table name.
2219

            
2220
    $dbi->update(table => 'book');
2221

            
2222
=item C<param>
2223

            
2224
Update data. This is hash reference.
2225

            
2226
    $dbi->update(param => {title => 'Perl'});
2227

            
2228
=item C<where>
2229

            
2230
Where clause. This is hash reference or L<DBIx::Custom::Where> object.
2231
    
2232
    # Hash reference
2233
    $dbi->update(where => {author => 'Ken', 'title' => 'Perl'});
2234
    
2235
    # DBIx::Custom::Where object
2236
    my $where = $dbi->where(
2237
        clause => ['and', '{= author}', '{like title}'],
2238
        param  => {author => 'Ken', title => '%Perl%'}
2239
    );
2240
    $dbi->update(where => $where);
2241

            
2242
=item C<append>
2243

            
2244
Append statement to last of SQL. This is string.
2245

            
2246
    $dbi->update(append => 'order by title');
2247

            
2248
=item C<filter>
2249

            
2250
Filter, executed before data is send to database. This is array reference.
2251
Filter value is code reference or
2252
filter name registerd by C<register_filter()>.
2253

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

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

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2279
=head2 C<model EXPERIMENTAL>
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
2280

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2281
    $dbi->model('book')->method(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
2282
        insert => sub { ... },
2283
        update => sub { ... }
2284
    );
2285
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2286
    my $model = $dbi->model('book');
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
2287

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2288
Set and get a L<DBIx::Custom::Model> object,
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
2289

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

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

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

            
2297
You can check SQL.
2298

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

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

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

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

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

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

            
2312
    $dbi->update_at(
2313
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
2314
        primary_key => 'id',
2315
        where => '5',
2316
        param => {title => 'Perl'}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2317
    );
2318

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

            
2323
=head2 C<primary_key>
2324

            
2325
Primary key. This is constant value or array reference.
2326
    
2327
    # Constant value
2328
    $dbi->update(primary_key => 'id');
2329

            
2330
    # Array reference
2331
    $dbi->update(primary_key => ['id1', 'id2' ]);
2332

            
2333
This is used to create where clause.
2334

            
2335
=head2 C<where>
2336

            
2337
Where clause, created from primary key information.
2338
This is constant value or array reference.
2339

            
2340
    # Constant value
2341
    $dbi->update(where => 5);
2342

            
2343
    # Array reference
2344
    $dbi->update(where => [3, 5]);
2345

            
2346
In first examle, the following SQL is created.
2347

            
2348
    update book set title = ? where id = ?
2349

            
2350
Place holders are set to 'Perl' and 5.
2351

            
2352
=head2 C<update_param EXPERIMENTAL>
2353

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

            
2356
Create update parameter tag.
2357

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

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2360
=head2 C<where EXPERIMENTAL>
fix tests
Yuki Kimoto authored on 2011-01-18
2361

            
cleanup
Yuki Kimoto authored on 2011-03-09
2362
    my $where = $dbi->where(
2363
        clause => ['and', '{= title}', '{= author}'],
2364
        param => {title => 'Perl', author => 'Ken'}
2365
    );
fix tests
Yuki Kimoto authored on 2011-01-18
2366

            
2367
Create a new L<DBIx::Custom::Where> object.
2368

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2373
Setup all model objects.
2374
C<columns> and C<primary_key> is automatically set.
cleanup
Yuki Kimoto authored on 2011-01-12
2375

            
cleanup
Yuki Kimoto authored on 2011-01-25
2376
=head1 Tags
2377

            
2378
The following tags is available.
2379

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2380
=head2 C<table EXPERIMENTAL>
add table tag
Yuki Kimoto authored on 2011-02-09
2381

            
2382
Table tag
2383

            
2384
    {table TABLE}    ->    TABLE
2385

            
2386
This is used to teach what is applied table to C<execute()>.
2387

            
cleanup
Yuki Kimoto authored on 2011-01-25
2388
=head2 C<?>
2389

            
2390
Placeholder tag.
2391

            
2392
    {? NAME}    ->   ?
2393

            
2394
=head2 C<=>
2395

            
2396
Equal tag.
2397

            
2398
    {= NAME}    ->   NAME = ?
2399

            
2400
=head2 C<E<lt>E<gt>>
2401

            
2402
Not equal tag.
2403

            
2404
    {<> NAME}   ->   NAME <> ?
2405

            
2406
=head2 C<E<lt>>
2407

            
2408
Lower than tag
2409

            
2410
    {< NAME}    ->   NAME < ?
2411

            
2412
=head2 C<E<gt>>
2413

            
2414
Greater than tag
2415

            
2416
    {> NAME}    ->   NAME > ?
2417

            
2418
=head2 C<E<gt>=>
2419

            
2420
Greater than or equal tag
2421

            
2422
    {>= NAME}   ->   NAME >= ?
2423

            
2424
=head2 C<E<lt>=>
2425

            
2426
Lower than or equal tag
2427

            
2428
    {<= NAME}   ->   NAME <= ?
2429

            
2430
=head2 C<like>
2431

            
2432
Like tag
2433

            
2434
    {like NAME}   ->   NAME like ?
2435

            
2436
=head2 C<in>
2437

            
2438
In tag.
2439

            
2440
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
2441

            
2442
=head2 C<insert_param>
2443

            
2444
Insert parameter tag.
2445

            
2446
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
2447

            
2448
=head2 C<update_param>
2449

            
2450
Updata parameter tag.
2451

            
2452
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
2453

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

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

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

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

            
2463
C<< <kimoto.yuki at gmail.com> >>
2464

            
2465
L<http://github.com/yuki-kimoto/DBIx-Custom>
2466

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
2467
=head1 AUTHOR
2468

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

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

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

            
2475
This program is free software; you can redistribute it and/or modify it
2476
under the same terms as Perl itself.
2477

            
2478
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
2479

            
2480