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

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
3
our $VERSION = '0.1655';
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(
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
23
    [qw/data_source dbh password 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',
update pod
Yuki Kimoto authored on 2011-02-07
34
    safety_column_name => sub { qr/^[\w\.]*$/ },
35
    stash => sub { {} }
many changed
Yuki Kimoto authored on 2011-01-23
36
);
37

            
38
__PACKAGE__->attr(
39
    cache_method => sub {
40
        sub {
41
            my $self = shift;
42
            
43
            $self->{_cached} ||= {};
44
            
45
            if (@_ > 1) {
46
                $self->{_cached}{$_[0]} = $_[1] 
47
            }
48
            else {
49
                return $self->{_cached}{$_[0]}
50
            }
51
        }
52
    }
53
);
54

            
55
__PACKAGE__->attr(
fix tests
Yuki Kimoto authored on 2011-01-13
56
    filters => sub {
57
        {
58
            encode_utf8 => sub { encode_utf8($_[0]) },
59
            decode_utf8 => sub { decode_utf8($_[0]) }
60
        }
many changed
Yuki Kimoto authored on 2011-01-23
61
    }
fix tests
Yuki Kimoto authored on 2011-01-13
62
);
cleanup
yuki-kimoto authored on 2010-10-17
63

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
refactoring select
yuki-kimoto authored on 2010-04-28
622
our %VALID_SELECT_ARGS
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
623
  = map { $_ => 1 } qw/table column where append relation filter query selection join/;
624

            
625
sub _need_tables {
626
    my ($self, $tree, $need_tables, $tables) = @_;
627
    
628
    foreach my $table (@$tables) {
629
        
630
        if ($tree->{$table}) {
631
            $need_tables->{$table} = 1;
632
            $self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}])
633
        }
634
    }
635
}
refactoring select
yuki-kimoto authored on 2010-04-28
636

            
packaging one directory
yuki-kimoto authored on 2009-11-16
637
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
638
    my ($self, %args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
639
    
cleanup
Yuki Kimoto authored on 2011-03-09
640
    # Check argument names
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
641
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
642
        croak qq{Argument "$name" is invalid name}
refactoring select
yuki-kimoto authored on 2010-04-28
643
          unless $VALID_SELECT_ARGS{$name};
644
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
645
    
refactoring select
yuki-kimoto authored on 2010-04-28
646
    # Arguments
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
647
    my $table = $args{table};
648
    my $tables = ref $table eq 'ARRAY' ? $table
649
               : defined $table ? [$table]
650
               : [];
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
651
    my $columns   = $args{column} || [];
select method column option ...
Yuki Kimoto authored on 2011-02-22
652
    $columns = [$columns] unless ref $columns;
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
653
    my $selection = $args{selection} || '';
654
    my $where     = $args{where} || {};
655
    my $append    = $args{append};
656
    my $filter    = $args{filter};
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
657
    my $join =     $args{join} || [];
658
    croak qq{"join" must be array reference}
659
      unless ref $join eq 'ARRAY';
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
660
    
cleanup
Yuki Kimoto authored on 2011-03-09
661
    # Join tables
cleanup
Yuki Kimoto authored on 2011-03-08
662
    my @join_tables;
663
    unshift @join_tables, $tables->[-1];
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
664
    
cleanup
Yuki Kimoto authored on 2011-03-09
665
    # Add relation tables(DEPRECATED!);
666
    $self->_add_relation_table($tables, $args{relation});
packaging one directory
yuki-kimoto authored on 2009-11-16
667
    
cleanup
Yuki Kimoto authored on 2011-01-27
668
    # SQL stack
669
    my @sql;
670
    push @sql, 'select';
packaging one directory
yuki-kimoto authored on 2009-11-16
671
    
cleanup
Yuki Kimoto authored on 2011-03-09
672
    # Selection
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
673
    if ($selection) {
674
        push @sql, $selection;
cleanup
Yuki Kimoto authored on 2011-03-08
675
        push @join_tables, @{$self->_tables($selection)};
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
676
    }
cleanup
Yuki Kimoto authored on 2011-03-09
677
    
678
    # Column names and table name
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
679
    else {
cleanup
Yuki Kimoto authored on 2011-03-09
680
        # Column names
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
681
        if (@$columns) {
682
            foreach my $column (@$columns) {
cleanup
Yuki Kimoto authored on 2011-03-08
683
                push @join_tables, @{$self->_tables($column)};
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
684
                push @sql, ($column, ',');
685
            }
686
            pop @sql if $sql[-1] eq ',';
687
        }
688
        else { push @sql, '*' }
689
        
690
        # Table
691
        croak qq{"table" option must be specified} unless @$tables;
692
        push @sql, 'from';
693
        foreach my $table (@$tables) {
694
            push @sql, ($table, ',');
packaging one directory
yuki-kimoto authored on 2009-11-16
695
        }
cleanup
Yuki Kimoto authored on 2011-01-27
696
        pop @sql if $sql[-1] eq ',';
packaging one directory
yuki-kimoto authored on 2009-11-16
697
    }
698
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
699
    # Where
700
    my $w;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
701
    if (ref $where eq 'HASH') {
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
702
        my $clause = ['and'];
703
        push @$clause, "{= $_}" for keys %$where;
cleanup
Yuki Kimoto authored on 2011-03-09
704
        $w = $self->where(clause => $clause, param => $where);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
705
    }
706
    elsif (ref $where eq 'DBIx::Custom::Where') {
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
707
        $w = $where;
708
        $where = $w->param;
packaging one directory
yuki-kimoto authored on 2009-11-16
709
    }
710
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
711
    croak qq{"where" must be hash reference or DBIx::Custom::Where object}
712
      unless ref $w eq 'DBIx::Custom::Where';
713
    
714
    # String where
715
    my $swhere = "$w";
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
716
    
cleanup
Yuki Kimoto authored on 2011-03-09
717
    # Add table names in where clause to join talbes.
cleanup
Yuki Kimoto authored on 2011-03-08
718
    unshift @join_tables, @{$self->_tables($swhere)};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
719
    
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
720
    # Join
721
    if (@$join) {
722
        my $tree = {};
723
        
724
        for (my $i = 0; $i < @$join; $i++) {
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
725
            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
726
            my $join_clause = $join->[$i];
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
727
            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
728
            if ($join_clause =~ /\s([^\.\s]+?)\..+\s([^\.\s]+?)\./) {
729
                
730
                my $table1 = $1;
731
                my $table2 = $2;
732
                
733
                croak qq{right side table of "$join_clause" must be uniq}
734
                  if exists $tree->{$table2};
735
                
736
                $tree->{$table2}
737
                  = {position => $i, parent => $table1, join => $join_clause};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
738
            }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
739
            else {
740
                croak qq{join "$join_clause" must be two table name};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
741
            }
742
        }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
743
        
744
        my $need_tables = {};
745
        $self->_need_tables($tree, $need_tables, \@join_tables);
746
        
747
        
748
        my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} } keys %$need_tables;
749

            
750
        foreach my $need_table (@need_tables) {
751
            push @sql, $tree->{$need_table}{join};
752
        }
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
753
    }
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
    
cleanup
Yuki Kimoto authored on 2011-03-08
771
    unshift @$tables, @join_tables;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
772
    
packaging one directory
yuki-kimoto authored on 2009-11-16
773
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
774
    my $result = $self->execute(
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
775
        $query, param  => $where, filter => $filter,
776
        table => $tables);
packaging one directory
yuki-kimoto authored on 2009-11-16
777
    
778
    return $result;
779
}
780

            
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
781
our %VALID_SELECT_AT_ARGS
782
  = map { $_ => 1 } qw/table column where append relation filter query selection
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
783
                       param primary_key left_join/;
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
784

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1137
sub _croak {
1138
    my ($self, $error, $append) = @_;
1139
    $append ||= "";
1140
    
1141
    # Verbose
1142
    if ($Carp::Verbose) { croak $error }
1143
    
1144
    # Not verbose
1145
    else {
1146
        
1147
        # Remove line and module infromation
1148
        my $at_pos = rindex($error, ' at ');
1149
        $error = substr($error, 0, $at_pos);
1150
        $error =~ s/\s+$//;
1151
        
1152
        croak "$error$append";
1153
    }
1154
}
1155

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1156
sub _tables {
1157
    my ($self, $source) = @_;
1158
    
1159
    my $tables = [];
1160
    
1161
    my $safety_name = $self->safety_column_name;
1162
    
1163
    while ($source =~ /\b(\w+)\./g) {
1164
        push @$tables, $1;
1165
    }
1166
    
1167
    return $tables;
1168
}
1169

            
cleanup
Yuki Kimoto authored on 2011-03-08
1170

            
1171

            
cleanup
Yuki Kimoto authored on 2011-01-25
1172
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-23
1173
__PACKAGE__->attr(
1174
    dbi_options => sub { {} },
1175
    filter_check  => 1
1176
);
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1177

            
cleanup
Yuki Kimoto authored on 2011-01-25
1178
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1179
sub default_bind_filter {
1180
    my $self = shift;
1181
    
1182
    if (@_) {
1183
        my $fname = $_[0];
1184
        
1185
        if (@_ && !$fname) {
1186
            $self->{default_out_filter} = undef;
1187
        }
1188
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1189
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1190
              unless exists $self->filters->{$fname};
1191
        
1192
            $self->{default_out_filter} = $self->filters->{$fname};
1193
        }
1194
        return $self;
1195
    }
1196
    
1197
    return $self->{default_out_filter};
1198
}
1199

            
cleanup
Yuki Kimoto authored on 2011-01-25
1200
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1201
sub default_fetch_filter {
1202
    my $self = shift;
1203
    
1204
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
1205
        my $fname = $_[0];
1206

            
cleanup
Yuki Kimoto authored on 2011-01-12
1207
        if (@_ && !$fname) {
1208
            $self->{default_in_filter} = undef;
1209
        }
1210
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1211
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1212
              unless exists $self->filters->{$fname};
1213
        
1214
            $self->{default_in_filter} = $self->filters->{$fname};
1215
        }
1216
        
1217
        return $self;
1218
    }
1219
    
many changed
Yuki Kimoto authored on 2011-01-23
1220
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
1221
}
1222

            
cleanup
Yuki Kimoto authored on 2011-01-25
1223
# DEPRECATED!
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1224
sub register_tag_processor {
1225
    return shift->query_builder->register_tag_processor(@_);
1226
}
1227

            
cleanup
Yuki Kimoto authored on 2011-03-08
1228
# DEPRECATED!
1229
sub _push_relation {
1230
    my ($self, $sql, $tables, $relation, $need_where) = @_;
1231
    
1232
    if (keys %{$relation || {}}) {
1233
        push @$sql, $need_where ? 'where' : 'and';
1234
        foreach my $rcolumn (keys %$relation) {
1235
            my $table1 = (split (/\./, $rcolumn))[0];
1236
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1237
            push @$tables, ($table1, $table2);
1238
            push @$sql, ("$rcolumn = " . $relation->{$rcolumn},  'and');
1239
        }
1240
    }
1241
    pop @$sql if $sql->[-1] eq 'and';    
1242
}
1243

            
1244
# DEPRECATED!
1245
sub _add_relation_table {
cleanup
Yuki Kimoto authored on 2011-03-09
1246
    my ($self, $tables, $relation) = @_;
cleanup
Yuki Kimoto authored on 2011-03-08
1247
    
1248
    if (keys %{$relation || {}}) {
1249
        foreach my $rcolumn (keys %$relation) {
1250
            my $table1 = (split (/\./, $rcolumn))[0];
1251
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1252
            my $table1_exists;
1253
            my $table2_exists;
1254
            foreach my $table (@$tables) {
1255
                $table1_exists = 1 if $table eq $table1;
1256
                $table2_exists = 1 if $table eq $table2;
1257
            }
1258
            unshift @$tables, $table1 unless $table1_exists;
1259
            unshift @$tables, $table2 unless $table2_exists;
1260
        }
1261
    }
1262
}
1263

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1266
=head1 NAME
1267

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

            
1270
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
1271

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1272
    use DBIx::Custom;
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1273
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname",
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1274
                                    user => 'ken', password => '!LFKD%$&',
1275
                                    dbi_option => {mysql_enable_utf8 => 1});
cleanup
yuki-kimoto authored on 2010-08-05
1276

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1277
    # Insert 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1278
    $dbi->insert(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1279
                 param  => {title => 'Perl', author => 'Ken'},
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1280
                 filter => [title => 'to_something']);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1281
    
1282
    # Update 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1283
    $dbi->update(table  => 'book', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1284
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
1285
                 where  => {id => 5},
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1286
                 filter => [title => 'to_something']);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1287
    
1288
    # Update all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1289
    $dbi->update_all(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1290
                     param  => {title => 'Perl'},
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1291
                     filter => [title => 'to_something']);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1292
    
1293
    # Delete
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1294
    $dbi->delete(table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1295
                 where  => {author => 'Ken'},
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1296
                 filter => [title => 'to_something']);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1297
    
1298
    # Delete all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1299
    $dbi->delete_all(table => 'book');
cleanup
yuki-kimoto authored on 2010-08-05
1300

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1301
    # Select
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
1302
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1303
        table  => 'book',
update document
yuki-kimoto authored on 2010-05-27
1304
        column => [qw/author title/],
1305
        where  => {author => 'Ken'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1306
        relation => {'book.id' => 'rental.book_id'},
updated document
yuki-kimoto authored on 2010-08-08
1307
        append => 'order by id limit 5',
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1308
        filter => [title => 'to_something']
added commit method
yuki-kimoto authored on 2010-05-27
1309
    );
cleanup
yuki-kimoto authored on 2010-08-05
1310

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1311
    # Execute SQL
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1312
    $dbi->execute("select title from book");
removed register_format()
yuki-kimoto authored on 2010-05-26
1313
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1314
    # Execute SQL with hash binding and filtering
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1315
    $dbi->execute("select id from book where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
1316
                  param  => {author => 'ken', title => '%Perl%'},
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1317
                  filter => [title => 'to_something']);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1318

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

            
1325
    # Get DBI object
1326
    my $dbh = $dbi->dbh;
1327

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1328
    # Fetch
1329
    while (my $row = $result->fetch) {
1330
        # ...
1331
    }
1332
    
1333
    # Fetch hash
1334
    while (my $row = $result->fetch_hash) {
1335
        
1336
    }
1337
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1338
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
1339

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

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

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

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

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

            
1362
=head1 GUIDE
1363

            
1364
L<DBIx::Custom::Guide> - L<DBIx::Custom> complete guide
1365

            
1366
=head1 EXAMPLES
1367

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

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

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

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

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

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

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

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

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

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

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

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

            
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1397
    my $dbi_option = $dbi->dbi_option;
1398
    $dbi            = $dbi->dbi_option($dbi_option);
added dbi_options attribute
kimoto authored on 2010-12-20
1399

            
1400
DBI options.
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1401

            
1402
Each option specified can ovewrite C<default_dbi_option>.
1403

            
1404
C<connect()> method use this value to connect the database.
1405

            
1406

            
1407
=head2 C<default_dbi_option>
1408

            
1409
    my $default_dbi_option = $dbi->default_dbi_option;
1410
    $dbi            = $dbi->default_dbi_option($default_dbi_option);
1411

            
1412
DBI default options.
1413

            
1414
    RaiseError => 1,
1415
    PrintError => 0,
1416
    AutoCommit => 1,
1417

            
added dbi_options attribute
kimoto authored on 2010-12-20
1418
C<connect()> method use this value to connect the database.
1419

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

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

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

            
add models() attribute
Yuki Kimoto authored on 2011-02-21
1427
Filters
1428

            
1429
=head2 C<(experimental) models>
1430

            
1431
    my $models = $dbi->models;
1432
    $dbi       = $dbi->models(\%models);
1433

            
1434
Models
1435

            
cleanup
yuki-kimoto authored on 2010-10-17
1436
=head2 C<password>
1437

            
1438
    my $password = $dbi->password;
1439
    $dbi         = $dbi->password('lkj&le`@s');
1440

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

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

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

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

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

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

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

            
update pod
Yuki Kimoto authored on 2011-01-27
1461
=head2 C<(experimental) safety_column_name>
1462

            
1463
    my $safety_column_name = $self->safety_column_name;
1464
    $dbi                   = $self->safety_column_name($name);
1465

            
1466
Safety column name regex.
1467
Default is qr/^[\w\.]*$/
1468

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1471
    my $user = $dbi->user;
1472
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1473

            
cleanup
yuki-kimoto authored on 2010-10-17
1474
User name.
1475
C<connect()> method use this value to connect the database.
update pod
Yuki Kimoto authored on 2011-01-27
1476

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1479
L<DBIx::Custom> inherits all methods from L<Object::Simple>
autoload DBI method
Yuki Kimoto authored on 2011-01-26
1480
and use all method of L<DBI>
cleanup
yuki-kimoto authored on 2010-10-17
1481
and implements the following new ones.
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1482

            
cleanup
Yuki Kimoto authored on 2011-01-25
1483
=head2 C<(experimental) apply_filter>
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1484

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1485
    $dbi->apply_filter(
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1486
        $table,
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1487
        $column1 => {in => $infilter1, out => $outfilter1, end => $endfilter1}
1488
        $column2 => {in => $infilter2, out => $outfilter2, end =. $endfilter2}
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1489
        ...,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1490
    );
1491

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

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

            
cleanup
Yuki Kimoto authored on 2010-12-21
1499
    $result = $dbi->execute(
1500
        "select * from table1 where {= key1} and {= key2};",
1501
         param => {key1 => 1, key2 => 2},
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1502
         table => ['table1']
cleanup
Yuki Kimoto authored on 2010-12-21
1503
    );
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1504

            
1505
You can use three name as column name.
1506

            
1507
    1. column        : author
1508
    2. table.column  : book.author
1509
    3. table__column : book__author
1510

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1546
    $dbi->delete(table  => $table,
1547
                 where  => \%where,
1548
                 append => $append,
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1549
                 filter => \@filter,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1550
                 query  => 1);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1551

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

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

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

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

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
1571
=head3 C<(experimental) delete_at()>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1572

            
1573
To delete row by using primary key, use C<delete_at()>
1574

            
1575
    $dbi->delete_at(
1576
        table => 'book',
1577
        primary_key => ['id'],
1578
        where => ['123']
1579
    );
1580

            
1581
In this example, row which id column is 123 is deleted.
1582
NOTE that you must pass array reference as C<where>.
1583

            
1584
You can also write arguments like this.
1585

            
1586
    $dbi->delete_at(
1587
        table => 'book',
1588
        primary_key => ['id'],
1589
        param => {id => '123'}
1590
    );
1591

            
cleanup
yuki-kimoto authored on 2010-10-17
1592
=head2 C<insert>
1593

            
1594
    $dbi->insert(table  => $table, 
1595
                 param  => \%param,
1596
                 append => $append,
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1597
                 filter => \@filter,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1598
                 query  => 1);
cleanup
yuki-kimoto authored on 2010-10-17
1599

            
1600
Execute insert statement.
1601
C<insert> method have C<table>, C<param>, C<append>
1602
and C<filter> arguments.
1603
C<table> is a table name.
1604
C<param> is the pairs of column name value. this must be hash reference.
1605
C<append> is a string added at the end of the SQL statement.
1606
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1607
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1608
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1609
This is overwrites C<default_bind_filter>.
1610
Return value of C<insert()> is the count of affected rows.
1611

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
1612
=head3 C<(experimental) insert_at()>
1613

            
1614
To insert row by using primary key, use C<insert_at()>
1615

            
1616
    $dbi->insert_at(
1617
        table => 'book',
1618
        primary_key => ['id'],
1619
        where => ['123'],
1620
        param => {name => 'Ken'}
1621
    );
1622

            
1623
In this example, row which id column is 123 is inserted.
1624
NOTE that you must pass array reference as C<where>.
1625
If C<param> contains primary key,
1626
the key and value is delete from C<param>.
1627

            
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1628
=head2 C<(experimental) insert_param>
1629

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

            
1632
Create insert parameter tag.
1633

            
1634
    {title => 'a', age => 2}   ->   {insert_param title age}
1635

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

            
pod fix
Yuki Kimoto authored on 2011-01-21
1638
    $dbi->each_column(
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1639
        sub {
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1640
            my ($self, $table, $column, $column_info) = @_;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1641
            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1642
            my $type = $column_info->{TYPE_NAME};
pod fix
Yuki Kimoto authored on 2011-01-21
1643
            
1644
            if ($type eq 'DATE') {
1645
                # ...
1646
            }
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1647
        }
1648
    );
pod fix
Yuki Kimoto authored on 2011-01-21
1649
Get column informations from database.
1650
Argument is callback.
1651
You can do anything in callback.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1652
Callback receive four arguments, dbi object, table name,
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1653
column name and column information.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1654

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1655
=head2 C<(experimental) include_model>
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1656

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1657
    $dbi->include_model(
1658
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1659
            'book', 'person', 'company'
1660
        ]
1661
    );
1662

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

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

            
1669
    $dbi->include_model('MyModel');
1670

            
1671
Note that in this case name spece module is needed.
1672

            
1673
    # MyModel.pm
1674
    package MyModel;
1675
    
1676
    use base 'DBIx::Custom::Model';
1677

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1680
    MyModel::book
1681
    MyModel::person
1682
    MyModel::company
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1683

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

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

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1691
    $dbi->include_model(
1692
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1693
            {'book' => 'Book'},
1694
            {'person' => 'Person'}
1695
        ]
1696
    );
1697

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1698
=head2 C<(experimental) method>
1699

            
1700
    $dbi->method(
1701
        update_or_insert => sub {
1702
            my $self = shift;
1703
            # do something
1704
        },
1705
        find_or_create   => sub {
1706
            my $self = shift;
1707
            # do something
1708
        }
1709
    );
1710

            
1711
Register method. These method is called from L<DBIx::Custom> object directory.
1712

            
1713
    $dbi->update_or_insert;
1714
    $dbi->find_or_create;
1715

            
1716
=head2 C<new>
1717

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

            
1721
Create a new L<DBIx::Custom> object.
1722

            
1723
=head2 C<(experimental) not_exists>
1724

            
1725
    my $not_exists = $dbi->not_exists;
1726

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1729
=head2 C<register_filter>
1730

            
1731
    $dbi->register_filter(%filters);
1732
    $dbi->register_filter(\%filters);
1733
    
1734
Register filter. Registered filters is available in the following attributes
1735
or arguments.
1736

            
1737
=over 4
1738

            
1739
=item *
1740

            
1741
C<filter> argument of C<insert()>, C<update()>,
1742
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1743
methods
1744

            
1745
=item *
1746

            
1747
C<execute()> method
1748

            
1749
=item *
1750

            
1751
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1752

            
1753
=item *
1754

            
1755
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1756

            
1757
=back
1758

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

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1761
    $dbi->register_tag(
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1762
        limit => sub {
1763
            ...;
1764
        }
1765
    );
1766

            
cleanup
Yuki Kimoto authored on 2011-01-25
1767
Register tag.
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1768

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

            
1771
    $dbi->rollback;
1772

            
1773
Rollback transaction.
1774
This is same as L<DBI>'s C<rollback>.
1775

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1776
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1777
    
select method column option ...
Yuki Kimoto authored on 2011-02-22
1778
    my $result = $dbi->select(
1779
        table     => $table,
1780
        column    => [@column],
1781
        where     => \%where,
1782
        append    => $append,
1783
        relation  => \%relation,
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
1784
        join => ['left outer join company on book.company_id = company.id']
select method column option ...
Yuki Kimoto authored on 2011-02-22
1785
        filter    => \%filter,
1786
        query     => 1,
1787
        selection => $selection
1788
    );
update document
yuki-kimoto authored on 2009-11-19
1789

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1790
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1791
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1792
C<relation> and C<filter> arguments.
1793
C<table> is a table name.
select method column option ...
Yuki Kimoto authored on 2011-02-22
1794
C<column> is column names. this is array reference or string.
cleanup
yuki-kimoto authored on 2010-08-09
1795
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1796
C<append> is a string added at the end of the SQL statement.
1797
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1798
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1799
default to 0. This is experimental.
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
1800
C<selection> is string of column name and tables. This is experimental
1801

            
1802
    selection => 'name, location.name as location_name ' .
1803
                 'from company inner join location'
update document
yuki-kimoto authored on 2009-11-19
1804

            
cleanup
yuki-kimoto authored on 2010-08-09
1805
First element is a string. it contains tags,
1806
such as "{= title} or {like author}".
1807
Second element is paramters.
1808

            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
1809
C<join> is join clause after from clause.
cleanup
Yuki Kimoto authored on 2011-03-08
1810
This is experimental.
1811

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
1812
=head3 C<(experimental) select_at()>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1813

            
1814
To select row by using primary key, use C<select_at()>.
1815

            
1816
    $dbi->select_at(table => 'book', primary_key => ['id'], where => ['123']);
1817

            
1818
In this example, row which id colunm is 123 is selected.
1819
NOTE that you must pass array reference as C<where>.
1820

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1823
    $dbi->update(table  => $table, 
1824
                 param  => \%params,
1825
                 where  => \%where,
1826
                 append => $append,
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1827
                 filter => \@filter,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1828
                 query  => 1)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1829

            
cleanup
yuki-kimoto authored on 2010-10-17
1830
Execute update statement.
1831
C<update> method have C<table>, C<param>, C<where>, C<append>
1832
and C<filter> arguments.
1833
C<table> is a table name.
1834
C<param> is column-value pairs. this must be hash reference.
1835
C<where> is where clause. this must be hash reference.
1836
C<append> is a string added at the end of the SQL statement.
1837
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1838
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1839
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1840
This is overwrites C<default_bind_filter>.
1841
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1842

            
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1843
=head2 C<(experimental) update_param>
1844

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

            
1847
Create update parameter tag.
1848

            
1849
    {title => 'a', age => 2}   ->   {update_param title age}
1850

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1851
=head2 C<(experimental) model>
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1852

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1853
    $dbi->model('book')->method(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1854
        insert => sub { ... },
1855
        update => sub { ... }
1856
    );
1857
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1858
    my $model = $dbi->model('book');
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1859

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

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1862
=head2 C<(experimental) setup_model>
1863

            
1864
    $dbi->setup_model;
1865

            
1866
Setup all model objects.
1867
C<columns> and C<primary_key> is automatically set.
1868

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1871
    $dbi->update_all(table  => $table, 
1872
                     param  => \%params,
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1873
                     filter => \@filter,
cleanup
yuki-kimoto authored on 2010-10-17
1874
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1875

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

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
1881
=head3 C<(experimental) update_at()>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1882

            
1883
To update row by using primary key, use C<update_at()>
1884

            
1885
    $dbi->update_at(
1886
        table => 'book',
1887
        primary_key => ['id'],
1888
        where => ['123'],
1889
        param => {name => 'Ken'}
1890
    );
1891

            
1892
In this example, row which id column is 123 is updated.
1893
NOTE that you must pass array reference as C<where>.
1894
If C<param> contains primary key,
1895
the key and value is delete from C<param>.
1896

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

            
cleanup
Yuki Kimoto authored on 2011-03-09
1899
    my $where = $dbi->where(
1900
        clause => ['and', '{= title}', '{= author}'],
1901
        param => {title => 'Perl', author => 'Ken'}
1902
    );
fix tests
Yuki Kimoto authored on 2011-01-18
1903

            
1904
Create a new L<DBIx::Custom::Where> object.
1905

            
cleanup
Yuki Kimoto authored on 2011-01-25
1906
=head2 C<cache_method>
cleanup
Yuki Kimoto authored on 2011-01-12
1907

            
1908
    $dbi          = $dbi->cache_method(\&cache_method);
1909
    $cache_method = $dbi->cache_method
1910

            
1911
Method to set and get caches.
1912

            
cleanup
Yuki Kimoto authored on 2011-01-25
1913
=head1 Tags
1914

            
1915
The following tags is available.
1916

            
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
1917
=head2 C<(experimental) table>
add table tag
Yuki Kimoto authored on 2011-02-09
1918

            
1919
Table tag
1920

            
1921
    {table TABLE}    ->    TABLE
1922

            
1923
This is used to teach what is applied table to C<execute()>.
1924

            
cleanup
Yuki Kimoto authored on 2011-01-25
1925
=head2 C<?>
1926

            
1927
Placeholder tag.
1928

            
1929
    {? NAME}    ->   ?
1930

            
1931
=head2 C<=>
1932

            
1933
Equal tag.
1934

            
1935
    {= NAME}    ->   NAME = ?
1936

            
1937
=head2 C<E<lt>E<gt>>
1938

            
1939
Not equal tag.
1940

            
1941
    {<> NAME}   ->   NAME <> ?
1942

            
1943
=head2 C<E<lt>>
1944

            
1945
Lower than tag
1946

            
1947
    {< NAME}    ->   NAME < ?
1948

            
1949
=head2 C<E<gt>>
1950

            
1951
Greater than tag
1952

            
1953
    {> NAME}    ->   NAME > ?
1954

            
1955
=head2 C<E<gt>=>
1956

            
1957
Greater than or equal tag
1958

            
1959
    {>= NAME}   ->   NAME >= ?
1960

            
1961
=head2 C<E<lt>=>
1962

            
1963
Lower than or equal tag
1964

            
1965
    {<= NAME}   ->   NAME <= ?
1966

            
1967
=head2 C<like>
1968

            
1969
Like tag
1970

            
1971
    {like NAME}   ->   NAME like ?
1972

            
1973
=head2 C<in>
1974

            
1975
In tag.
1976

            
1977
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
1978

            
1979
=head2 C<insert_param>
1980

            
1981
Insert parameter tag.
1982

            
1983
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
1984

            
1985
=head2 C<update_param>
1986

            
1987
Updata parameter tag.
1988

            
1989
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
1990

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
1993
L<DBIx::Custom> is stable. APIs keep backword compatible
1994
except experimental one in the feature.
DBIx::Custom is now stable
yuki-kimoto authored on 2010-09-07
1995

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

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

            
2000
C<< <kimoto.yuki at gmail.com> >>
2001

            
2002
L<http://github.com/yuki-kimoto/DBIx-Custom>
2003

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
2004
=head1 AUTHOR
2005

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

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

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

            
2012
This program is free software; you can redistribute it and/or modify it
2013
under the same terms as Perl itself.
2014

            
2015
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
2016

            
2017