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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1133
=head1 NAME
1134

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

            
1137
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
1138

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

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

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

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

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

            
1192
    # Get DBI object
1193
    my $dbh = $dbi->dbh;
1194

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

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

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

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

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

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

            
1229
=head1 GUIDE
1230

            
1231
L<DBIx::Custom::Guide> - L<DBIx::Custom> complete guide
1232

            
1233
=head1 EXAMPLES
1234

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1267
DBI options.
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1268

            
1269
Each option specified can ovewrite C<default_dbi_option>.
1270

            
1271
C<connect()> method use this value to connect the database.
1272

            
1273

            
1274
=head2 C<default_dbi_option>
1275

            
1276
    my $default_dbi_option = $dbi->default_dbi_option;
1277
    $dbi            = $dbi->default_dbi_option($default_dbi_option);
1278

            
1279
DBI default options.
1280

            
1281
    RaiseError => 1,
1282
    PrintError => 0,
1283
    AutoCommit => 1,
1284

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

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

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

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

            
add models() attribute
Yuki Kimoto authored on 2011-02-21
1294
Filters
1295

            
1296
=head2 C<(experimental) models>
1297

            
1298
    my $models = $dbi->models;
1299
    $dbi       = $dbi->models(\%models);
1300

            
1301
Models
1302

            
cleanup
yuki-kimoto authored on 2010-10-17
1303
=head2 C<password>
1304

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

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

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

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

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

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

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

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

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

            
1330
    my $safety_column_name = $self->safety_column_name;
1331
    $dbi                   = $self->safety_column_name($name);
1332

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

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

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

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

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

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

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

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

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

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

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

            
1372
You can use three name as column name.
1373

            
1374
    1. column        : author
1375
    2. table.column  : book.author
1376
    3. table__column : book__author
1377

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1440
To delete row by using primary key, use C<delete_at()>
1441

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

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

            
1451
You can also write arguments like this.
1452

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1459
=head2 C<insert>
1460

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

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

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

            
1481
To insert row by using primary key, use C<insert_at()>
1482

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

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

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

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

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

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

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

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

            
1528
    $dbi->include_model('MyModel');
1529

            
1530
Note that in this case name spece module is needed.
1531

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

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1539
    MyModel::book
1540
    MyModel::person
1541
    MyModel::company
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
You can get these instance by C<model()>.
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
    my $book_model = $dbi->model('book');
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1546

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

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

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

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

            
1570
Register method. These method is called from L<DBIx::Custom> object directory.
1571

            
1572
    $dbi->update_or_insert;
1573
    $dbi->find_or_create;
1574

            
1575
=head2 C<new>
1576

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

            
1580
Create a new L<DBIx::Custom> object.
1581

            
1582
=head2 C<(experimental) not_exists>
1583

            
1584
    my $not_exists = $dbi->not_exists;
1585

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1588
=head2 C<register_filter>
1589

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

            
1596
=over 4
1597

            
1598
=item *
1599

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

            
1604
=item *
1605

            
1606
C<execute()> method
1607

            
1608
=item *
1609

            
1610
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1611

            
1612
=item *
1613

            
1614
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1615

            
1616
=back
1617

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

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
1626
Register tag.
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1627

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

            
1630
    $dbi->rollback;
1631

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1711
    $dbi->setup_model;
1712

            
1713
Setup all model objects.
1714
C<columns> and C<primary_key> is automatically set.
1715

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

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

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

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

            
1730
To update row by using primary key, use C<update_at()>
1731

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

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

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

            
1746
    my $where = $dbi->where;
1747

            
1748
Create a new L<DBIx::Custom::Where> object.
1749

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

            
1752
    $dbi          = $dbi->cache_method(\&cache_method);
1753
    $cache_method = $dbi->cache_method
1754

            
1755
Method to set and get caches.
1756

            
cleanup
Yuki Kimoto authored on 2011-01-25
1757
=head1 Tags
1758

            
1759
The following tags is available.
1760

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

            
1763
Table tag
1764

            
1765
    {table TABLE}    ->    TABLE
1766

            
1767
This is used to teach what is applied table to C<execute()>.
1768

            
cleanup
Yuki Kimoto authored on 2011-01-25
1769
=head2 C<?>
1770

            
1771
Placeholder tag.
1772

            
1773
    {? NAME}    ->   ?
1774

            
1775
=head2 C<=>
1776

            
1777
Equal tag.
1778

            
1779
    {= NAME}    ->   NAME = ?
1780

            
1781
=head2 C<E<lt>E<gt>>
1782

            
1783
Not equal tag.
1784

            
1785
    {<> NAME}   ->   NAME <> ?
1786

            
1787
=head2 C<E<lt>>
1788

            
1789
Lower than tag
1790

            
1791
    {< NAME}    ->   NAME < ?
1792

            
1793
=head2 C<E<gt>>
1794

            
1795
Greater than tag
1796

            
1797
    {> NAME}    ->   NAME > ?
1798

            
1799
=head2 C<E<gt>=>
1800

            
1801
Greater than or equal tag
1802

            
1803
    {>= NAME}   ->   NAME >= ?
1804

            
1805
=head2 C<E<lt>=>
1806

            
1807
Lower than or equal tag
1808

            
1809
    {<= NAME}   ->   NAME <= ?
1810

            
1811
=head2 C<like>
1812

            
1813
Like tag
1814

            
1815
    {like NAME}   ->   NAME like ?
1816

            
1817
=head2 C<in>
1818

            
1819
In tag.
1820

            
1821
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
1822

            
1823
=head2 C<insert_param>
1824

            
1825
Insert parameter tag.
1826

            
1827
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
1828

            
1829
=head2 C<update_param>
1830

            
1831
Updata parameter tag.
1832

            
1833
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
1834

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

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

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

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

            
1844
C<< <kimoto.yuki at gmail.com> >>
1845

            
1846
L<http://github.com/yuki-kimoto/DBIx-Custom>
1847

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1848
=head1 AUTHOR
1849

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

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

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

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

            
1859
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1860

            
1861