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

            
selection can contain where ...
Yuki Kimoto authored on 2011-03-06
3
our $VERSION = '0.1654';
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
    
244
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
245
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
246
        croak qq{"$name" is invalid argument}
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
    
310
    # Check arguments
311
    foreach my $name (keys %args) {
312
        croak qq{"$name" is invalid argument}
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
    
352
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
353
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
354
        croak qq{"$name" is invalid argument}
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;
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
372
    foreach my $table (@$tables) {
cleanup
Yuki Kimoto authored on 2011-01-12
373
        $filter = {
374
            %$filter,
375
            %{$self->{filter}{out}->{$table} || {}}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
376
        }
377
    }
378
    
cleanup
Yuki Kimoto authored on 2011-01-12
379
    # Filter argument
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
380
    my $f = DBIx::Custom::Util::array_filter_to_hash($args{filter})
381
         || $query->filter || {};
cleanup
Yuki Kimoto authored on 2011-01-12
382
    foreach my $column (keys %$f) {
383
        my $fname = $f->{$column};
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
384
        if (!defined $fname) {
cleanup
Yuki Kimoto authored on 2011-01-12
385
            $f->{$column} = undef;
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
386
        }
387
        elsif (ref $fname ne 'CODE') {
many changed
Yuki Kimoto authored on 2011-01-23
388
          croak qq{Filter "$fname" is not registered"}
cleanup
Yuki Kimoto authored on 2010-12-21
389
            unless exists $self->filters->{$fname};
390
          
cleanup
Yuki Kimoto authored on 2011-01-12
391
          $f->{$column} = $self->filters->{$fname};
cleanup
Yuki Kimoto authored on 2010-12-21
392
        }
393
    }
cleanup
Yuki Kimoto authored on 2011-01-12
394
    $filter = {%$filter, %$f};
packaging one directory
yuki-kimoto authored on 2009-11-16
395
    
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
396
    # Bind
397
    my $bind = $self->_bind($params, $query->columns, $filter);
cleanup
yuki-kimoto authored on 2010-10-17
398
    
399
    # Execute
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
400
    my $sth = $query->sth;
cleanup
yuki-kimoto authored on 2010-10-17
401
    my $affected;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
402
    eval {$affected = $sth->execute(@$bind)};
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
403
    $self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@;
cleanup
yuki-kimoto authored on 2010-10-17
404
    
405
    # Return resultset if select statement is executed
406
    if ($sth->{NUM_OF_FIELDS}) {
407
        
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
408
        # Result in and end filter
409
        my $in_filter  = {};
410
        my $end_filter = {};
cleanup
Yuki Kimoto authored on 2011-01-12
411
        foreach my $table (@$tables) {
412
            $in_filter = {
413
                %$in_filter,
414
                %{$self->{filter}{in}{$table} || {}}
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
415
            };
416
            $end_filter = {
417
                %$end_filter,
418
                %{$self->{filter}{end}{$table} || {}}
419
            };
cleanup
Yuki Kimoto authored on 2011-01-12
420
        }
421
        
422
        # Result
423
        my $result = $self->result_class->new(
cleanup
Yuki Kimoto authored on 2010-12-22
424
            sth            => $sth,
425
            filters        => $self->filters,
426
            filter_check   => $self->filter_check,
cleanup
Yuki Kimoto authored on 2011-01-12
427
            default_filter => $self->{default_in_filter},
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
428
            filter         => $in_filter || {},
429
            end_filter     => $end_filter || {}
cleanup
yuki-kimoto authored on 2010-10-17
430
        );
431

            
432
        return $result;
433
    }
434
    return $affected;
435
}
436

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

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

            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
489
our %VALID_INSERT_AT_ARGS
490
  = map { $_ => 1 } qw/table param
491
                       where append filter query
492
                       primary_key param/;
493

            
494
sub insert_at {
495
    my ($self, %args) = @_;
496
    
497
    # Check arguments
498
    foreach my $name (keys %args) {
499
        croak qq{"$name" is invalid argument}
500
          unless $VALID_INSERT_AT_ARGS{$name};
501
    }
502
    
503
    # Primary key
504
    my $primary_keys = delete $args{primary_key};
505
    $primary_keys = [$primary_keys] unless ref $primary_keys;
506
    
507
    # Where clause
508
    my $where = {};
509
    my $param = {};
510
    
511
    if (exists $args{where}) {
512
        my $where_columns = delete $args{where};
513
        $where_columns = [$where_columns] unless ref $where_columns;
514

            
515
        croak qq{"where" must be constant value or array reference}
516
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
517
        
518
        for(my $i = 0; $i < @$primary_keys; $i ++) {
519
           $where->{$primary_keys->[$i]} = $where_columns->[$i];
520
        }
521
    }
522
    
523
    if (exists $args{param}) {
524
        $param = delete $args{param};
525
        for(my $i = 0; $i < @$primary_keys; $i ++) {
526
             delete $param->{$primary_keys->[$i]};
527
        }
528
    }
529
    
530
    $param = {%$param, %$where};
531
    
532
    return $self->insert(param => $param, %args);
533
}
534

            
pod fix
Yuki Kimoto authored on 2011-01-21
535
sub each_column {
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
536
    my ($self, $cb) = @_;
537
    
538
    # Iterate all tables
539
    my $sth_tables = $self->dbh->table_info;
540
    while (my $table_info = $sth_tables->fetchrow_hashref) {
541
        
542
        # Table
543
        my $table = $table_info->{TABLE_NAME};
544
        
545
        # Iterate all columns
546
        my $sth_columns = $self->dbh->column_info(undef, undef, $table, '%');
547
        while (my $column_info = $sth_columns->fetchrow_hashref) {
548
            my $column = $column_info->{COLUMN_NAME};
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
549
            $self->$cb($table, $column, $column_info);
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
550
        }
551
    }
552
}
553

            
added dbi_options attribute
kimoto authored on 2010-12-20
554
sub new {
555
    my $self = shift->SUPER::new(@_);
556
    
557
    # Check attribute names
558
    my @attrs = keys %$self;
559
    foreach my $attr (@attrs) {
560
        croak qq{"$attr" is invalid attribute name}
561
          unless $self->can($attr);
562
    }
cleanup
Yuki Kimoto authored on 2011-01-25
563

            
564
    $self->register_tag(
565
        '?'     => \&DBIx::Custom::Tag::placeholder,
566
        '='     => \&DBIx::Custom::Tag::equal,
567
        '<>'    => \&DBIx::Custom::Tag::not_equal,
568
        '>'     => \&DBIx::Custom::Tag::greater_than,
569
        '<'     => \&DBIx::Custom::Tag::lower_than,
570
        '>='    => \&DBIx::Custom::Tag::greater_than_equal,
571
        '<='    => \&DBIx::Custom::Tag::lower_than_equal,
572
        'like'  => \&DBIx::Custom::Tag::like,
573
        'in'    => \&DBIx::Custom::Tag::in,
574
        'insert_param' => \&DBIx::Custom::Tag::insert_param,
575
        'update_param' => \&DBIx::Custom::Tag::update_param
576
    );
added dbi_options attribute
kimoto authored on 2010-12-20
577
    
578
    return $self;
579
}
580

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

            
cleanup
yuki-kimoto authored on 2010-10-17
583
sub register_filter {
584
    my $invocant = shift;
585
    
586
    # Register filter
587
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
588
    $invocant->filters({%{$invocant->filters}, %$filters});
589
    
590
    return $invocant;
591
}
packaging one directory
yuki-kimoto authored on 2009-11-16
592

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

            
refactoring select
yuki-kimoto authored on 2010-04-28
595
our %VALID_SELECT_ARGS
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
596
  = map { $_ => 1 } qw/table column where append relation filter query selection/;
refactoring select
yuki-kimoto authored on 2010-04-28
597

            
packaging one directory
yuki-kimoto authored on 2009-11-16
598
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
599
    my ($self, %args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
600
    
refactoring select
yuki-kimoto authored on 2010-04-28
601
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
602
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
603
        croak qq{"$name" is invalid argument}
refactoring select
yuki-kimoto authored on 2010-04-28
604
          unless $VALID_SELECT_ARGS{$name};
605
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
606
    
refactoring select
yuki-kimoto authored on 2010-04-28
607
    # Arguments
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
608
    my $table = $args{table};
609
    my $tables = ref $table eq 'ARRAY' ? $table
610
               : defined $table ? [$table]
611
               : [];
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
612
    my $columns   = $args{column} || [];
select method column option ...
Yuki Kimoto authored on 2011-02-22
613
    $columns = [$columns] unless ref $columns;
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
614
    my $selection = $args{selection} || '';
615
    my $where     = $args{where} || {};
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
616
    my $relation  = $args{relation} || {};
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
617
    my $append    = $args{append};
618
    my $filter    = $args{filter};
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
619

            
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
620
    # Relation
621
    if (!$selection && keys %$relation) {
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
622
        foreach my $rcolumn (keys %$relation) {
623
            my $table1 = (split (/\./, $rcolumn))[0];
624
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
625
            
626
            my $table1_exists;
627
            my $table2_exists;
628
            foreach my $table (@$tables) {
629
                $table1_exists = 1 if $table eq $table1;
630
                $table2_exists = 1 if $table eq $table2;
631
            }
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
632
            unshift @$tables, $table1 unless $table1_exists;
633
            unshift @$tables, $table2 unless $table2_exists;
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
634
        }
635
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
636
    
cleanup
Yuki Kimoto authored on 2011-01-27
637
    # SQL stack
638
    my @sql;
639
    
640
    push @sql, 'select';
packaging one directory
yuki-kimoto authored on 2009-11-16
641
    
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
642
    if ($selection) {
643
        push @sql, $selection;
644
    }
645
    else {
646
        # Column clause
647
        if (@$columns) {
648
            foreach my $column (@$columns) {
649
                push @sql, ($column, ',');
650
            }
651
            pop @sql if $sql[-1] eq ',';
652
        }
653
        else { push @sql, '*' }
654
        
655
        # Table
656
        croak qq{"table" option must be specified} unless @$tables;
657
        push @sql, 'from';
658
        foreach my $table (@$tables) {
659
            push @sql, ($table, ',');
packaging one directory
yuki-kimoto authored on 2009-11-16
660
        }
cleanup
Yuki Kimoto authored on 2011-01-27
661
        pop @sql if $sql[-1] eq ',';
packaging one directory
yuki-kimoto authored on 2009-11-16
662
    }
663
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
664
    # Where
665
    my $w;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
666
    if (ref $where eq 'HASH') {
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
667
        my $clause = ['and'];
668
        push @$clause, "{= $_}" for keys %$where;
669
        $w = $self->where;
670
        $w->clause($clause);
671
        $w->param($where);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
672
    }
673
    elsif (ref $where eq 'DBIx::Custom::Where') {
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
674
        $w = $where;
675
        $where = $w->param;
packaging one directory
yuki-kimoto authored on 2009-11-16
676
    }
677
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
678
    croak qq{"where" must be hash reference or DBIx::Custom::Where object}
679
      unless ref $w eq 'DBIx::Custom::Where';
680
    
681
    # String where
682
    my $swhere = "$w";
cleanup
Yuki Kimoto authored on 2011-01-27
683
    push @sql, $swhere;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
684
    
added commit method
yuki-kimoto authored on 2010-05-27
685
    # Relation
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
686
    if (!$selection && keys %$relation) {
cleanup
Yuki Kimoto authored on 2011-01-27
687
        push @sql, $swhere eq '' ? 'where' : 'and';
688
        foreach my $rcolumn (keys %$relation) {
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
689
            my $table1 = (split (/\./, $rcolumn))[0];
690
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
691
            push @$tables, ($table1, $table2);
692
            
cleanup
Yuki Kimoto authored on 2011-01-27
693
            push @sql, ("$rcolumn = " . $relation->{$rcolumn},  'and');
packaging one directory
yuki-kimoto authored on 2009-11-16
694
        }
695
    }
cleanup
Yuki Kimoto authored on 2011-01-27
696
    pop @sql if $sql[-1] eq 'and';
added commit method
yuki-kimoto authored on 2010-05-27
697
    
cleanup
Yuki Kimoto authored on 2011-01-27
698
    # Append statement
699
    push @sql, $append if $append;
700
    
701
    # SQL
702
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
703
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
704
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
705
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
706
    return $query if $args{query};
707
    
packaging one directory
yuki-kimoto authored on 2009-11-16
708
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
709
    my $result = $self->execute(
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
710
        $query, param  => $where, filter => $filter,
711
        table => $tables);
packaging one directory
yuki-kimoto authored on 2009-11-16
712
    
713
    return $result;
714
}
715

            
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
716
our %VALID_SELECT_AT_ARGS
717
  = map { $_ => 1 } qw/table column where append relation filter query selection
718
                       param primary_key/;
719

            
720
sub select_at {
721
    my ($self, %args) = @_;
722
    
723
    # Check arguments
724
    foreach my $name (keys %args) {
725
        croak qq{"$name" is invalid argument}
726
          unless $VALID_SELECT_AT_ARGS{$name};
727
    }
728
    
729
    # Primary key
730
    my $primary_keys = delete $args{primary_key};
731
    $primary_keys = [$primary_keys] unless ref $primary_keys;
732
    
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
733
    # Table
734
    croak qq{"table" option must be specified} unless $args{table};
735
    my $table = ref $args{table} ? $args{table}->[-1] : $args{table};
736
    
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
737
    # Where clause
738
    my $where = {};
739
    if (exists $args{where}) {
740
        my $where_columns = delete $args{where};
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
741
        
742
        croak qq{"where" must be constant value or array reference}
743
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
744
        
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
745
        $where_columns = [$where_columns] unless ref $where_columns;
746
        
747
        for(my $i = 0; $i < @$primary_keys; $i ++) {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
748
           $where->{$table . '.' . $primary_keys->[$i]} = $where_columns->[$i];
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
749
        }
750
    }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
751
    
752
    if (exists $args{param}) {
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
753
        my $param = delete $args{param};
754
        for(my $i = 0; $i < @$primary_keys; $i ++) {
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
755
             delete $param->{$primary_keys->[$i]};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
756
        }
757
    }
758
    
759
    return $self->select(where => $where, %args);
760
}
761

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
762
sub model {
763
    my ($self, $name, $model) = @_;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
764
    
765
    # Set
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
766
    if ($model) {
add models() attribute
Yuki Kimoto authored on 2011-02-21
767
        $self->models->{$name} = $model;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
768
        return $self;
769
    }
770
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
771
    # Check model existance
772
    croak qq{Model "$name" is not included}
add models() attribute
Yuki Kimoto authored on 2011-02-21
773
      unless $self->models->{$name};
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
774
    
775
    # Get
add models() attribute
Yuki Kimoto authored on 2011-02-21
776
    return $self->models->{$name};
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
777
}
778

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
779
sub include_model {
780
    my ($self, $name_space, $model_infos) = @_;
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
781
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
782
    $name_space ||= '';
783
    unless ($model_infos) {
784
        # Load name space module
785
        croak qq{"$name_space" is invalid class name}
786
          if $name_space =~ /[^\w:]/;
787
        eval "use $name_space";
788
        croak qq{Name space module "$name_space.pm" is needed. $@} if $@;
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
789
        
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
790
        # Search model modules
791
        my $path = $INC{"$name_space.pm"};
792
        $path =~ s/\.pm$//;
793
        opendir my $dh, $path
794
          or croak qq{Can't open directory "$path": $!};
795
        $model_infos = [];
796
        while (my $module = readdir $dh) {
797
            push @$model_infos, $module
798
              if $module =~ s/\.pm$//;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
799
        }
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
800
        
801
        close $dh;
802
    }
803
    
804
    foreach my $model_info (@$model_infos) {
805
        
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
806
        # Model class, name, table
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
807
        my $model_class;
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
808
        my $model_name;
809
        my $model_table;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
810
        if (ref $model_info eq 'HASH') {
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
811
            $model_class = $model_info->{class};
812
            $model_name  = $model_info->{name};
813
            $model_table = $model_info->{table};
814
            
815
            $model_name  ||= $model_class;
816
            $model_table ||= $model_name;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
817
        }
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
818
        else { $model_class =$model_name = $model_table = $model_info }
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
819
        my $mclass = "${name_space}::$model_class";
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
820
        
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
821
        # Load
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
822
        croak qq{"$mclass" is invalid class name}
823
          if $mclass =~ /[^\w:]/;
824
        unless ($mclass->can('isa')) {
825
            eval "use $mclass";
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
826
            croak $@ if $@;
827
        }
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
828
        
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
829
        # Instantiate
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
830
        my $model = $mclass->new(dbi => $self);
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
831
        $model->name($model_name) unless $model->name;
832
        $model->table($model_table) unless $model->table;
removed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-28
833
        
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
834
        # Set
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
835
        $self->model($model->name, $model);
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
836
        
837
        # Apply filter
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
838
        croak "${name_space}::$model_class filter must be array reference"
839
          unless ref $model->filter eq 'ARRAY';
840
        $self->apply_filter($model->table, @{$model->filter});
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
841
    }
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
842
    return $self;
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
843
}
844

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
845
sub setup_model {
846
    my $self = shift;
847
    
848
    $self->each_column(
849
        sub {
850
            my ($self, $table, $column, $column_info) = @_;
851
            
852
            if (my $model = $self->models->{$table}) {
853
                push @{$model->columns}, $column;
854
            }
855
        }
856
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
857
    return $self;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
858
}
859

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

            
864
sub update {
865
    my ($self, %args) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
866
    
cleanup
yuki-kimoto authored on 2010-10-17
867
    # Check arguments
868
    foreach my $name (keys %args) {
869
        croak qq{"$name" is invalid argument}
870
          unless $VALID_UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
871
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
872
    
cleanup
yuki-kimoto authored on 2010-10-17
873
    # Arguments
874
    my $table            = $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
875
    croak qq{"table" option must be specified} unless $table;
cleanup
yuki-kimoto authored on 2010-10-17
876
    my $param            = $args{param} || {};
877
    my $where            = $args{where} || {};
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
878
    my $append           = $args{append} || '';
cleanup
yuki-kimoto authored on 2010-10-17
879
    my $filter           = $args{filter};
880
    my $allow_update_all = $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
881
    
cleanup
yuki-kimoto authored on 2010-10-17
882
    # Update keys
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
883
    my @clumns = keys %$param;
884

            
885
    # Columns
886
    my @columns;
887
    my $safety = $self->safety_column_name;
888
    foreach my $column (keys %$param) {
889
        croak qq{"$column" is not safety column name}
890
          unless $column =~ /$safety/;
891
        push @columns, $column;
892
    }
893
        
cleanup
yuki-kimoto authored on 2010-10-17
894
    # Update clause
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
895
    my $update_clause = '{update_param ' . join(' ', @clumns) . '}';
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
896

            
897
    # Where
898
    my $w;
899
    if (ref $where eq 'HASH') {
900
        my $clause = ['and'];
901
        push @$clause, "{= $_}" for keys %$where;
902
        $w = $self->where;
903
        $w->clause($clause);
904
        $w->param($where);
905
    }
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
906
    elsif (ref $where eq 'DBIx::Custom::Where') {
907
        $w = $where;
908
        $where = $w->param;
909
    }  
removed experimental registe...
yuki-kimoto authored on 2010-08-24
910
    
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
911
    croak qq{"where" must be hash refernce or DBIx::Custom::Where object}
912
      unless ref $w eq 'DBIx::Custom::Where';
removed reconnect method
yuki-kimoto authored on 2010-05-28
913
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
914
    # String where
915
    my $swhere = "$w";
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
916
    
917
    croak qq{"where" must be specified}
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
918
      if "$swhere" eq '' && !$allow_update_all;
removed reconnect method
yuki-kimoto authored on 2010-05-28
919
    
cleanup
Yuki Kimoto authored on 2011-01-27
920
    # SQL stack
921
    my @sql;
922
    
923
    # Update
924
    push @sql, "update $table $update_clause $swhere";
925
    push @sql, $append if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
926
    
cleanup
yuki-kimoto authored on 2010-10-17
927
    # Rearrange parameters
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
928
    foreach my $wkey (keys %$where) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
929
        
cleanup
yuki-kimoto authored on 2010-10-17
930
        if (exists $param->{$wkey}) {
931
            $param->{$wkey} = [$param->{$wkey}]
932
              unless ref $param->{$wkey} eq 'ARRAY';
933
            
934
            push @{$param->{$wkey}}, $where->{$wkey};
935
        }
936
        else {
937
            $param->{$wkey} = $where->{$wkey};
938
        }
removed reconnect method
yuki-kimoto authored on 2010-05-28
939
    }
cleanup
yuki-kimoto authored on 2010-10-17
940
    
cleanup
Yuki Kimoto authored on 2011-01-27
941
    # SQL
942
    my $sql = join(' ', @sql);
943
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
944
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
945
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
946
    return $query if $args{query};
947
    
cleanup
yuki-kimoto authored on 2010-10-17
948
    # Execute query
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
949
    my $ret_val = $self->execute($query, param  => $param, 
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
950
                                 filter => $filter,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
951
                                 table => $table);
cleanup
yuki-kimoto authored on 2010-10-17
952
    
953
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
954
}
955

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

            
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
958
our %VALID_UPDATE_AT_ARGS
959
  = map { $_ => 1 } qw/table param
960
                       where append filter query
961
                       primary_key param/;
962

            
963
sub update_at {
964
    my ($self, %args) = @_;
965
    
966
    # Check arguments
967
    foreach my $name (keys %args) {
968
        croak qq{"$name" is invalid argument}
969
          unless $VALID_UPDATE_AT_ARGS{$name};
970
    }
971
    
972
    # Primary key
973
    my $primary_keys = delete $args{primary_key};
974
    $primary_keys = [$primary_keys] unless ref $primary_keys;
975
    
976
    # Where clause
977
    my $where = {};
978
    my $param = {};
979
    
980
    if (exists $args{where}) {
981
        my $where_columns = delete $args{where};
982
        $where_columns = [$where_columns] unless ref $where_columns;
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
983

            
984
        croak qq{"where" must be constant value or array reference}
985
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
986
        
987
        for(my $i = 0; $i < @$primary_keys; $i ++) {
988
           $where->{$primary_keys->[$i]} = $where_columns->[$i];
989
        }
990
    }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
991
    
992
    if (exists $args{param}) {
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
993
        $param = delete $args{param};
994
        for(my $i = 0; $i < @$primary_keys; $i ++) {
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
995
            delete $param->{$primary_keys->[$i]};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
996
        }
997
    }
998
    
999
    return $self->update(where => $where, param => $param, %args);
1000
}
1001

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

            
1005
    return DBIx::Custom::Where->new(
1006
        query_builder => $self->query_builder,
1007
        safety_column_name => $self->safety_column_name
1008
    );
cleanup
Yuki Kimoto authored on 2011-01-25
1009
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1010

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1011
sub _bind {
cleanup
Yuki Kimoto authored on 2011-01-12
1012
    my ($self, $params, $columns, $filter) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1013
    
cleanup
Yuki Kimoto authored on 2011-01-12
1014
    # bind values
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1015
    my @bind;
add tests
yuki-kimoto authored on 2010-08-08
1016
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
1017
    # Build bind values
1018
    my $count = {};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1019
    my $not_exists = {};
cleanup
Yuki Kimoto authored on 2011-01-12
1020
    foreach my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1021
        
1022
        # Value
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1023
        my $value;
1024
        if(ref $params->{$column} eq 'ARRAY') {
1025
            my $i = $count->{$column} || 0;
1026
            $i += $not_exists->{$column} || 0;
1027
            my $found;
1028
            for (my $k = $i; $i < @{$params->{$column}}; $k++) {
1029
                if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
1030
                    $not_exists->{$column}++;
1031
                }
1032
                else  {
1033
                    $value = $params->{$column}->[$k];
1034
                    $found = 1;
1035
                    last
1036
                }
1037
            }
1038
            next unless $found;
1039
        }
1040
        else { $value = $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1041
        
cleanup
Yuki Kimoto authored on 2011-01-12
1042
        # Filter
1043
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
1044
        
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1045
        push @bind, $f ? $f->($value) : $value;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1046
        
1047
        # Count up 
1048
        $count->{$column}++;
1049
    }
1050
    
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1051
    return \@bind;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1052
}
1053

            
cleanup
yuki-kimoto authored on 2010-10-17
1054
sub _croak {
1055
    my ($self, $error, $append) = @_;
1056
    $append ||= "";
1057
    
1058
    # Verbose
1059
    if ($Carp::Verbose) { croak $error }
1060
    
1061
    # Not verbose
1062
    else {
1063
        
1064
        # Remove line and module infromation
1065
        my $at_pos = rindex($error, ' at ');
1066
        $error = substr($error, 0, $at_pos);
1067
        $error =~ s/\s+$//;
1068
        
1069
        croak "$error$append";
1070
    }
1071
}
1072

            
cleanup
Yuki Kimoto authored on 2011-01-25
1073
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-23
1074
__PACKAGE__->attr(
1075
    dbi_options => sub { {} },
1076
    filter_check  => 1
1077
);
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1078

            
cleanup
Yuki Kimoto authored on 2011-01-25
1079
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1080
sub default_bind_filter {
1081
    my $self = shift;
1082
    
1083
    if (@_) {
1084
        my $fname = $_[0];
1085
        
1086
        if (@_ && !$fname) {
1087
            $self->{default_out_filter} = undef;
1088
        }
1089
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1090
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1091
              unless exists $self->filters->{$fname};
1092
        
1093
            $self->{default_out_filter} = $self->filters->{$fname};
1094
        }
1095
        return $self;
1096
    }
1097
    
1098
    return $self->{default_out_filter};
1099
}
1100

            
cleanup
Yuki Kimoto authored on 2011-01-25
1101
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1102
sub default_fetch_filter {
1103
    my $self = shift;
1104
    
1105
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
1106
        my $fname = $_[0];
1107

            
cleanup
Yuki Kimoto authored on 2011-01-12
1108
        if (@_ && !$fname) {
1109
            $self->{default_in_filter} = undef;
1110
        }
1111
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1112
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1113
              unless exists $self->filters->{$fname};
1114
        
1115
            $self->{default_in_filter} = $self->filters->{$fname};
1116
        }
1117
        
1118
        return $self;
1119
    }
1120
    
many changed
Yuki Kimoto authored on 2011-01-23
1121
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
1122
}
1123

            
cleanup
Yuki Kimoto authored on 2011-01-25
1124
# DEPRECATED!
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1125
sub register_tag_processor {
1126
    return shift->query_builder->register_tag_processor(@_);
1127
}
1128

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1131
=head1 NAME
1132

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

            
1135
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
1136

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1142
    # Insert 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1143
    $dbi->insert(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1144
                 param  => {title => 'Perl', author => 'Ken'},
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1145
                 filter => [title => 'to_something']);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1146
    
1147
    # Update 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1148
    $dbi->update(table  => 'book', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1149
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
1150
                 where  => {id => 5},
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1151
                 filter => [title => 'to_something']);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1152
    
1153
    # Update all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1154
    $dbi->update_all(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1155
                     param  => {title => 'Perl'},
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1156
                     filter => [title => 'to_something']);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1157
    
1158
    # Delete
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1159
    $dbi->delete(table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1160
                 where  => {author => 'Ken'},
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1161
                 filter => [title => 'to_something']);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1162
    
1163
    # Delete all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1164
    $dbi->delete_all(table => 'book');
cleanup
yuki-kimoto authored on 2010-08-05
1165

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1166
    # Select
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
1167
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1168
        table  => 'book',
update document
yuki-kimoto authored on 2010-05-27
1169
        column => [qw/author title/],
1170
        where  => {author => 'Ken'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1171
        relation => {'book.id' => 'rental.book_id'},
updated document
yuki-kimoto authored on 2010-08-08
1172
        append => 'order by id limit 5',
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1173
        filter => [title => 'to_something']
added commit method
yuki-kimoto authored on 2010-05-27
1174
    );
cleanup
yuki-kimoto authored on 2010-08-05
1175

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

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

            
1190
    # Get DBI object
1191
    my $dbh = $dbi->dbh;
1192

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1193
    # Fetch
1194
    while (my $row = $result->fetch) {
1195
        # ...
1196
    }
1197
    
1198
    # Fetch hash
1199
    while (my $row = $result->fetch_hash) {
1200
        
1201
    }
1202
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1203
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
1204

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

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

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

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

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

            
1227
=head1 GUIDE
1228

            
1229
L<DBIx::Custom::Guide> - L<DBIx::Custom> complete guide
1230

            
1231
=head1 EXAMPLES
1232

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1265
DBI options.
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1266

            
1267
Each option specified can ovewrite C<default_dbi_option>.
1268

            
1269
C<connect()> method use this value to connect the database.
1270

            
1271

            
1272
=head2 C<default_dbi_option>
1273

            
1274
    my $default_dbi_option = $dbi->default_dbi_option;
1275
    $dbi            = $dbi->default_dbi_option($default_dbi_option);
1276

            
1277
DBI default options.
1278

            
1279
    RaiseError => 1,
1280
    PrintError => 0,
1281
    AutoCommit => 1,
1282

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

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

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

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

            
add models() attribute
Yuki Kimoto authored on 2011-02-21
1292
Filters
1293

            
1294
=head2 C<(experimental) models>
1295

            
1296
    my $models = $dbi->models;
1297
    $dbi       = $dbi->models(\%models);
1298

            
1299
Models
1300

            
cleanup
yuki-kimoto authored on 2010-10-17
1301
=head2 C<password>
1302

            
1303
    my $password = $dbi->password;
1304
    $dbi         = $dbi->password('lkj&le`@s');
1305

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

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

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

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

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

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

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

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

            
1328
    my $safety_column_name = $self->safety_column_name;
1329
    $dbi                   = $self->safety_column_name($name);
1330

            
1331
Safety column name regex.
1332
Default is qr/^[\w\.]*$/
1333

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1336
    my $user = $dbi->user;
1337
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1338

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

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

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

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

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1350
    $dbi->apply_filter(
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1351
        $table,
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1352
        $column1 => {in => $infilter1, out => $outfilter1, end => $endfilter1}
1353
        $column2 => {in => $infilter2, out => $outfilter2, end =. $endfilter2}
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1354
        ...,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1355
    );
1356

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

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

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

            
1370
You can use three name as column name.
1371

            
1372
    1. column        : author
1373
    2. table.column  : book.author
1374
    3. table__column : book__author
1375

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1438
To delete row by using primary key, use C<delete_at()>
1439

            
1440
    $dbi->delete_at(
1441
        table => 'book',
1442
        primary_key => ['id'],
1443
        where => ['123']
1444
    );
1445

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

            
1449
You can also write arguments like this.
1450

            
1451
    $dbi->delete_at(
1452
        table => 'book',
1453
        primary_key => ['id'],
1454
        param => {id => '123'}
1455
    );
1456

            
cleanup
yuki-kimoto authored on 2010-10-17
1457
=head2 C<insert>
1458

            
1459
    $dbi->insert(table  => $table, 
1460
                 param  => \%param,
1461
                 append => $append,
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1462
                 filter => \@filter,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1463
                 query  => 1);
cleanup
yuki-kimoto authored on 2010-10-17
1464

            
1465
Execute insert statement.
1466
C<insert> method have C<table>, C<param>, C<append>
1467
and C<filter> arguments.
1468
C<table> is a table name.
1469
C<param> is the pairs of column name value. this must be hash reference.
1470
C<append> is a string added at the end of the SQL statement.
1471
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1472
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1473
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1474
This is overwrites C<default_bind_filter>.
1475
Return value of C<insert()> is the count of affected rows.
1476

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

            
1479
To insert row by using primary key, use C<insert_at()>
1480

            
1481
    $dbi->insert_at(
1482
        table => 'book',
1483
        primary_key => ['id'],
1484
        where => ['123'],
1485
        param => {name => 'Ken'}
1486
    );
1487

            
1488
In this example, row which id column is 123 is inserted.
1489
NOTE that you must pass array reference as C<where>.
1490
If C<param> contains primary key,
1491
the key and value is delete from C<param>.
1492

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

            
pod fix
Yuki Kimoto authored on 2011-01-21
1495
    $dbi->each_column(
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1496
        sub {
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1497
            my ($self, $table, $column, $column_info) = @_;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1498
            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1499
            my $type = $column_info->{TYPE_NAME};
pod fix
Yuki Kimoto authored on 2011-01-21
1500
            
1501
            if ($type eq 'DATE') {
1502
                # ...
1503
            }
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1504
        }
1505
    );
pod fix
Yuki Kimoto authored on 2011-01-21
1506
Get column informations from database.
1507
Argument is callback.
1508
You can do anything in callback.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1509
Callback receive four arguments, dbi object, table name,
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1510
column name and column information.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1511

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1514
    $dbi->include_model(
1515
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1516
            'book', 'person', 'company'
1517
        ]
1518
    );
1519

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

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

            
1526
    $dbi->include_model('MyModel');
1527

            
1528
Note that in this case name spece module is needed.
1529

            
1530
    # MyModel.pm
1531
    package MyModel;
1532
    
1533
    use base 'DBIx::Custom::Model';
1534

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1537
    MyModel::book
1538
    MyModel::person
1539
    MyModel::company
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1540

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

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

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1548
    $dbi->include_model(
1549
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1550
            {'book' => 'Book'},
1551
            {'person' => 'Person'}
1552
        ]
1553
    );
1554

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

            
1557
    $dbi->method(
1558
        update_or_insert => sub {
1559
            my $self = shift;
1560
            # do something
1561
        },
1562
        find_or_create   => sub {
1563
            my $self = shift;
1564
            # do something
1565
        }
1566
    );
1567

            
1568
Register method. These method is called from L<DBIx::Custom> object directory.
1569

            
1570
    $dbi->update_or_insert;
1571
    $dbi->find_or_create;
1572

            
1573
=head2 C<new>
1574

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

            
1578
Create a new L<DBIx::Custom> object.
1579

            
1580
=head2 C<(experimental) not_exists>
1581

            
1582
    my $not_exists = $dbi->not_exists;
1583

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1586
=head2 C<register_filter>
1587

            
1588
    $dbi->register_filter(%filters);
1589
    $dbi->register_filter(\%filters);
1590
    
1591
Register filter. Registered filters is available in the following attributes
1592
or arguments.
1593

            
1594
=over 4
1595

            
1596
=item *
1597

            
1598
C<filter> argument of C<insert()>, C<update()>,
1599
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1600
methods
1601

            
1602
=item *
1603

            
1604
C<execute()> method
1605

            
1606
=item *
1607

            
1608
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1609

            
1610
=item *
1611

            
1612
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1613

            
1614
=back
1615

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

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1618
    $dbi->register_tag(
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1619
        limit => sub {
1620
            ...;
1621
        }
1622
    );
1623

            
cleanup
Yuki Kimoto authored on 2011-01-25
1624
Register tag.
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1625

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

            
1628
    $dbi->rollback;
1629

            
1630
Rollback transaction.
1631
This is same as L<DBI>'s C<rollback>.
1632

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1633
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1634
    
select method column option ...
Yuki Kimoto authored on 2011-02-22
1635
    my $result = $dbi->select(
1636
        table     => $table,
1637
        column    => [@column],
1638
        where     => \%where,
1639
        append    => $append,
1640
        relation  => \%relation,
1641
        filter    => \%filter,
1642
        query     => 1,
1643
        selection => $selection
1644
    );
update document
yuki-kimoto authored on 2009-11-19
1645

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1646
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1647
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1648
C<relation> and C<filter> arguments.
1649
C<table> is a table name.
select method column option ...
Yuki Kimoto authored on 2011-02-22
1650
C<column> is column names. this is array reference or string.
cleanup
yuki-kimoto authored on 2010-08-09
1651
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1652
C<append> is a string added at the end of the SQL statement.
1653
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1654
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1655
default to 0. This is experimental.
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
1656
C<selection> is string of column name and tables. This is experimental
1657

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1661
First element is a string. it contains tags,
1662
such as "{= title} or {like author}".
1663
Second element is paramters.
1664

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1676
    $dbi->update(table  => $table, 
1677
                 param  => \%params,
1678
                 where  => \%where,
1679
                 append => $append,
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1680
                 filter => \@filter,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1681
                 query  => 1)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1682

            
cleanup
yuki-kimoto authored on 2010-10-17
1683
Execute update statement.
1684
C<update> method have C<table>, C<param>, C<where>, C<append>
1685
and C<filter> arguments.
1686
C<table> is a table name.
1687
C<param> is column-value pairs. this must be hash reference.
1688
C<where> is where clause. this must be hash reference.
1689
C<append> is a string added at the end of the SQL statement.
1690
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1691
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1692
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1693
This is overwrites C<default_bind_filter>.
1694
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1695

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1698
    $dbi->model('book')->method(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1699
        insert => sub { ... },
1700
        update => sub { ... }
1701
    );
1702
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1703
    my $model = $dbi->model('book');
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1704

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

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

            
1709
    $dbi->setup_model;
1710

            
1711
Setup all model objects.
1712
C<columns> and C<primary_key> is automatically set.
1713

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

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

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

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

            
1728
To update row by using primary key, use C<update_at()>
1729

            
1730
    $dbi->update_at(
1731
        table => 'book',
1732
        primary_key => ['id'],
1733
        where => ['123'],
1734
        param => {name => 'Ken'}
1735
    );
1736

            
1737
In this example, row which id column is 123 is updated.
1738
NOTE that you must pass array reference as C<where>.
1739
If C<param> contains primary key,
1740
the key and value is delete from C<param>.
1741

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

            
1744
    my $where = $dbi->where;
1745

            
1746
Create a new L<DBIx::Custom::Where> object.
1747

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

            
1750
    $dbi          = $dbi->cache_method(\&cache_method);
1751
    $cache_method = $dbi->cache_method
1752

            
1753
Method to set and get caches.
1754

            
cleanup
Yuki Kimoto authored on 2011-01-25
1755
=head1 Tags
1756

            
1757
The following tags is available.
1758

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

            
1761
Table tag
1762

            
1763
    {table TABLE}    ->    TABLE
1764

            
1765
This is used to teach what is applied table to C<execute()>.
1766

            
cleanup
Yuki Kimoto authored on 2011-01-25
1767
=head2 C<?>
1768

            
1769
Placeholder tag.
1770

            
1771
    {? NAME}    ->   ?
1772

            
1773
=head2 C<=>
1774

            
1775
Equal tag.
1776

            
1777
    {= NAME}    ->   NAME = ?
1778

            
1779
=head2 C<E<lt>E<gt>>
1780

            
1781
Not equal tag.
1782

            
1783
    {<> NAME}   ->   NAME <> ?
1784

            
1785
=head2 C<E<lt>>
1786

            
1787
Lower than tag
1788

            
1789
    {< NAME}    ->   NAME < ?
1790

            
1791
=head2 C<E<gt>>
1792

            
1793
Greater than tag
1794

            
1795
    {> NAME}    ->   NAME > ?
1796

            
1797
=head2 C<E<gt>=>
1798

            
1799
Greater than or equal tag
1800

            
1801
    {>= NAME}   ->   NAME >= ?
1802

            
1803
=head2 C<E<lt>=>
1804

            
1805
Lower than or equal tag
1806

            
1807
    {<= NAME}   ->   NAME <= ?
1808

            
1809
=head2 C<like>
1810

            
1811
Like tag
1812

            
1813
    {like NAME}   ->   NAME like ?
1814

            
1815
=head2 C<in>
1816

            
1817
In tag.
1818

            
1819
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
1820

            
1821
=head2 C<insert_param>
1822

            
1823
Insert parameter tag.
1824

            
1825
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
1826

            
1827
=head2 C<update_param>
1828

            
1829
Updata parameter tag.
1830

            
1831
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
1832

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

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

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

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

            
1842
C<< <kimoto.yuki at gmail.com> >>
1843

            
1844
L<http://github.com/yuki-kimoto/DBIx-Custom>
1845

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1846
=head1 AUTHOR
1847

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

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

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

            
1854
This program is free software; you can redistribute it and/or modify it
1855
under the same terms as Perl itself.
1856

            
1857
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1858

            
1859