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

            
add models() attribute
Yuki Kimoto authored on 2011-02-21
3
our $VERSION = '0.1648';
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;
update document
yuki-kimoto authored on 2010-05-27
19
use Encode qw/encode_utf8 decode_utf8/;
packaging one directory
yuki-kimoto authored on 2009-11-16
20

            
fix tests
Yuki Kimoto authored on 2011-01-13
21
__PACKAGE__->attr(
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
22
    [qw/data_source dbh password user/],
fix tests
Yuki Kimoto authored on 2011-01-13
23
    cache => 1,
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
24
    dbi_option => sub { {} },
25
    default_dbi_option => sub {{
26
        RaiseError => 1,
27
        PrintError => 0,
28
        AutoCommit => 1
29
    }},
add models() attribute
Yuki Kimoto authored on 2011-02-21
30
    models => sub { {} },
cleanup
Yuki Kimoto authored on 2011-01-23
31
    query_builder => sub { DBIx::Custom::QueryBuilder->new },
many changed
Yuki Kimoto authored on 2011-01-23
32
    result_class  => 'DBIx::Custom::Result',
update pod
Yuki Kimoto authored on 2011-02-07
33
    safety_column_name => sub { qr/^[\w\.]*$/ },
34
    stash => sub { {} }
many changed
Yuki Kimoto authored on 2011-01-23
35
);
36

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
179
sub create_query {
180
    my ($self, $source) = @_;
update document
yuki-kimoto authored on 2010-01-30
181
    
cleanup
yuki-kimoto authored on 2010-10-17
182
    # Cache
183
    my $cache = $self->cache;
update document
yuki-kimoto authored on 2010-01-30
184
    
cleanup
yuki-kimoto authored on 2010-10-17
185
    # Create query
186
    my $query;
187
    if ($cache) {
188
        
189
        # Get query
190
        my $q = $self->cache_method->($self, $source);
191
        
192
        # Create query
add table tag
Yuki Kimoto authored on 2011-02-09
193
        if ($q) {
194
            $query = DBIx::Custom::Query->new($q);
195
            $query->filters($self->filters);
196
        }
cleanup
yuki-kimoto authored on 2010-10-17
197
    }
198
    
199
    unless ($query) {
cleanup insert
yuki-kimoto authored on 2010-04-28
200

            
cleanup
yuki-kimoto authored on 2010-10-17
201
        # Create SQL object
202
        my $builder = $self->query_builder;
203
        
204
        # Create query
205
        $query = $builder->build_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
206

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
232
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
233
    my ($self, %args) = @_;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
234
    
235
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
236
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
237
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
238
          unless $VALID_DELETE_ARGS{$name};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
239
    }
240
    
241
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
242
    my $table            = $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
243
    croak qq{"table" option must be specified} unless $table;
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
244
    my $where            = $args{where} || {};
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
245
    my $append           = $args{append};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
246
    my $filter           = $args{filter};
cleanup
yuki-kimoto authored on 2010-10-17
247
    my $allow_delete_all = $args{allow_delete_all};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
248

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

            
cleanup
Yuki Kimoto authored on 2011-01-27
271
    # SQL stack
272
    my @sql;
273

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

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

            
added helper method
yuki-kimoto authored on 2010-10-17
294
sub DESTROY { }
295

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

            
cleanup
yuki-kimoto authored on 2010-10-17
298
sub execute{
299
    my ($self, $query, %args)  = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
300
    
301
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
302
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
303
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
304
          unless $VALID_EXECUTE_ARGS{$name};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
305
    }
306
    
cleanup
yuki-kimoto authored on 2010-10-17
307
    my $params = $args{param} || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
308
    
cleanup
yuki-kimoto authored on 2010-10-17
309
    # First argument is the soruce of SQL
310
    $query = $self->create_query($query)
311
      unless ref $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
312
    
add table tag
Yuki Kimoto authored on 2011-02-09
313
    # Applied filter
cleanup
Yuki Kimoto authored on 2011-01-12
314
    my $filter = {};
add table tag
Yuki Kimoto authored on 2011-02-09
315
    my $tables = $query->tables;
316
    my $arg_tables = $args{table} || [];
317
    $arg_tables = [$arg_tables]
318
      unless ref $arg_tables eq 'ARRAY';
319
    push @$tables, @$arg_tables;
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
320
    foreach my $table (@$tables) {
cleanup
Yuki Kimoto authored on 2011-01-12
321
        $filter = {
322
            %$filter,
323
            %{$self->{filter}{out}->{$table} || {}}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
324
        }
325
    }
326
    
cleanup
Yuki Kimoto authored on 2011-01-12
327
    # Filter argument
328
    my $f = $args{filter} || $query->filter || {};
329
    foreach my $column (keys %$f) {
330
        my $fname = $f->{$column};
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
331
        if (!defined $fname) {
cleanup
Yuki Kimoto authored on 2011-01-12
332
            $f->{$column} = undef;
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
333
        }
334
        elsif (ref $fname ne 'CODE') {
many changed
Yuki Kimoto authored on 2011-01-23
335
          croak qq{Filter "$fname" is not registered"}
cleanup
Yuki Kimoto authored on 2010-12-21
336
            unless exists $self->filters->{$fname};
337
          
cleanup
Yuki Kimoto authored on 2011-01-12
338
          $f->{$column} = $self->filters->{$fname};
cleanup
Yuki Kimoto authored on 2010-12-21
339
        }
340
    }
cleanup
Yuki Kimoto authored on 2011-01-12
341
    $filter = {%$filter, %$f};
packaging one directory
yuki-kimoto authored on 2009-11-16
342
    
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
343
    # Bind
344
    my $bind = $self->_bind($params, $query->columns, $filter);
cleanup
yuki-kimoto authored on 2010-10-17
345
    
346
    # Execute
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
347
    my $sth = $query->sth;
cleanup
yuki-kimoto authored on 2010-10-17
348
    my $affected;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
349
    eval {$affected = $sth->execute(@$bind)};
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
350
    $self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@;
cleanup
yuki-kimoto authored on 2010-10-17
351
    
352
    # Return resultset if select statement is executed
353
    if ($sth->{NUM_OF_FIELDS}) {
354
        
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
355
        # Result in and end filter
356
        my $in_filter  = {};
357
        my $end_filter = {};
cleanup
Yuki Kimoto authored on 2011-01-12
358
        foreach my $table (@$tables) {
359
            $in_filter = {
360
                %$in_filter,
361
                %{$self->{filter}{in}{$table} || {}}
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
362
            };
363
            $end_filter = {
364
                %$end_filter,
365
                %{$self->{filter}{end}{$table} || {}}
366
            };
cleanup
Yuki Kimoto authored on 2011-01-12
367
        }
368
        
369
        # Result
370
        my $result = $self->result_class->new(
cleanup
Yuki Kimoto authored on 2010-12-22
371
            sth            => $sth,
372
            filters        => $self->filters,
373
            filter_check   => $self->filter_check,
cleanup
Yuki Kimoto authored on 2011-01-12
374
            default_filter => $self->{default_in_filter},
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
375
            filter         => $in_filter || {},
376
            end_filter     => $end_filter || {}
cleanup
yuki-kimoto authored on 2010-10-17
377
        );
378

            
379
        return $result;
380
    }
381
    return $affected;
382
}
383

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

            
389
    # Check arguments
390
    foreach my $name (keys %args) {
391
        croak qq{"$name" is invalid argument}
392
          unless $VALID_INSERT_ARGS{$name};
packaging one directory
yuki-kimoto authored on 2009-11-16
393
    }
394
    
cleanup
yuki-kimoto authored on 2010-10-17
395
    # Arguments
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
396
    my $table  = $args{table};
397
    croak qq{"table" option must be specified} unless $table;
cleanup
yuki-kimoto authored on 2010-10-17
398
    my $param  = $args{param} || {};
399
    my $append = $args{append} || '';
400
    my $filter = $args{filter};
401
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
402
    # Columns
403
    my @columns;
404
    my $safety = $self->safety_column_name;
405
    foreach my $column (keys %$param) {
406
        croak qq{"$column" is not safety column name}
407
          unless $column =~ /$safety/;
408
        push @columns, $column;
409
    }
cleanup
yuki-kimoto authored on 2010-10-17
410
    
cleanup
Yuki Kimoto authored on 2011-01-27
411
    # SQL stack
412
    my @sql;
413
    
414
    # Insert
415
    push @sql, "insert into $table {insert_param ". join(' ', @columns) . '}';
416
    push @sql, $append if $append;
417
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
418
    # SQL
cleanup
Yuki Kimoto authored on 2011-01-27
419
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
420
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
421
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
422
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
423
    return $query if $args{query};
424
    
packaging one directory
yuki-kimoto authored on 2009-11-16
425
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
426
    my $ret_val = $self->execute(
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
427
        $query,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
428
        param  => $param,
429
        filter => $filter,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
430
        table => $table
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
431
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
432
    
433
    return $ret_val;
434
}
435

            
pod fix
Yuki Kimoto authored on 2011-01-21
436
sub each_column {
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
437
    my ($self, $cb) = @_;
438
    
439
    # Iterate all tables
440
    my $sth_tables = $self->dbh->table_info;
441
    while (my $table_info = $sth_tables->fetchrow_hashref) {
442
        
443
        # Table
444
        my $table = $table_info->{TABLE_NAME};
445
        
446
        # Iterate all columns
447
        my $sth_columns = $self->dbh->column_info(undef, undef, $table, '%');
448
        while (my $column_info = $sth_columns->fetchrow_hashref) {
449
            my $column = $column_info->{COLUMN_NAME};
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
450
            $self->$cb($table, $column, $column_info);
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
451
        }
452
    }
453
}
454

            
added dbi_options attribute
kimoto authored on 2010-12-20
455
sub new {
456
    my $self = shift->SUPER::new(@_);
457
    
458
    # Check attribute names
459
    my @attrs = keys %$self;
460
    foreach my $attr (@attrs) {
461
        croak qq{"$attr" is invalid attribute name}
462
          unless $self->can($attr);
463
    }
cleanup
Yuki Kimoto authored on 2011-01-25
464

            
465
    $self->register_tag(
466
        '?'     => \&DBIx::Custom::Tag::placeholder,
467
        '='     => \&DBIx::Custom::Tag::equal,
468
        '<>'    => \&DBIx::Custom::Tag::not_equal,
469
        '>'     => \&DBIx::Custom::Tag::greater_than,
470
        '<'     => \&DBIx::Custom::Tag::lower_than,
471
        '>='    => \&DBIx::Custom::Tag::greater_than_equal,
472
        '<='    => \&DBIx::Custom::Tag::lower_than_equal,
473
        'like'  => \&DBIx::Custom::Tag::like,
474
        'in'    => \&DBIx::Custom::Tag::in,
475
        'insert_param' => \&DBIx::Custom::Tag::insert_param,
476
        'update_param' => \&DBIx::Custom::Tag::update_param
477
    );
added dbi_options attribute
kimoto authored on 2010-12-20
478
    
479
    return $self;
480
}
481

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

            
cleanup
yuki-kimoto authored on 2010-10-17
484
sub register_filter {
485
    my $invocant = shift;
486
    
487
    # Register filter
488
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
489
    $invocant->filters({%{$invocant->filters}, %$filters});
490
    
491
    return $invocant;
492
}
packaging one directory
yuki-kimoto authored on 2009-11-16
493

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
499
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
500
    my ($self, %args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
501
    
refactoring select
yuki-kimoto authored on 2010-04-28
502
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
503
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
504
        croak qq{"$name" is invalid argument}
refactoring select
yuki-kimoto authored on 2010-04-28
505
          unless $VALID_SELECT_ARGS{$name};
506
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
507
    
refactoring select
yuki-kimoto authored on 2010-04-28
508
    # Arguments
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
509
    my $table = $args{table};
510
    my $tables = ref $table eq 'ARRAY' ? $table
511
               : defined $table ? [$table]
512
               : [];
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
513
    my $columns   = $args{column} || [];
514
    my $selection = $args{selection} || '';
515
    my $where     = $args{where} || {};
516
    my $relation  = $args{relation};
517
    my $append    = $args{append};
518
    my $filter    = $args{filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
519
    
cleanup
Yuki Kimoto authored on 2011-01-27
520
    # SQL stack
521
    my @sql;
522
    
523
    push @sql, 'select';
packaging one directory
yuki-kimoto authored on 2009-11-16
524
    
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
525
    if ($selection) {
526
        croak qq{Can't contain "where" clause in selection}
527
          if $selection =~ /\swhere\s/;
528
        push @sql, $selection;
529
    }
530
    else {
531
        # Column clause
532
        if (@$columns) {
533
            foreach my $column (@$columns) {
534
                push @sql, ($column, ',');
535
            }
536
            pop @sql if $sql[-1] eq ',';
537
        }
538
        else { push @sql, '*' }
539
        
540
        # Table
541
        croak qq{"table" option must be specified} unless @$tables;
542
        push @sql, 'from';
543
        foreach my $table (@$tables) {
544
            push @sql, ($table, ',');
packaging one directory
yuki-kimoto authored on 2009-11-16
545
        }
cleanup
Yuki Kimoto authored on 2011-01-27
546
        pop @sql if $sql[-1] eq ',';
packaging one directory
yuki-kimoto authored on 2009-11-16
547
    }
548
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
549
    # Where
550
    my $w;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
551
    if (ref $where eq 'HASH') {
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
552
        my $clause = ['and'];
553
        push @$clause, "{= $_}" for keys %$where;
554
        $w = $self->where;
555
        $w->clause($clause);
556
        $w->param($where);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
557
    }
558
    elsif (ref $where eq 'DBIx::Custom::Where') {
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
559
        $w = $where;
560
        $where = $w->param;
packaging one directory
yuki-kimoto authored on 2009-11-16
561
    }
562
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
563
    croak qq{"where" must be hash reference or DBIx::Custom::Where object}
564
      unless ref $w eq 'DBIx::Custom::Where';
565
    
566
    # String where
567
    my $swhere = "$w";
cleanup
Yuki Kimoto authored on 2011-01-27
568
    push @sql, $swhere;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
569
    
added commit method
yuki-kimoto authored on 2010-05-27
570
    # Relation
571
    if ($relation) {
cleanup
Yuki Kimoto authored on 2011-01-27
572
        push @sql, $swhere eq '' ? 'where' : 'and';
573
        foreach my $rcolumn (keys %$relation) {
574
            push @sql, ("$rcolumn = " . $relation->{$rcolumn},  'and');
packaging one directory
yuki-kimoto authored on 2009-11-16
575
        }
576
    }
cleanup
Yuki Kimoto authored on 2011-01-27
577
    pop @sql if $sql[-1] eq 'and';
added commit method
yuki-kimoto authored on 2010-05-27
578
    
cleanup
Yuki Kimoto authored on 2011-01-27
579
    # Append statement
580
    push @sql, $append if $append;
581
    
582
    # SQL
583
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
584
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
585
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
586
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
587
    return $query if $args{query};
588
    
packaging one directory
yuki-kimoto authored on 2009-11-16
589
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
590
    my $result = $self->execute(
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
591
        $query, param  => $where, filter => $filter,
592
        table => $tables);
packaging one directory
yuki-kimoto authored on 2009-11-16
593
    
594
    return $result;
595
}
596

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
597
sub model {
598
    my ($self, $name, $model) = @_;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
599
    
600
    # Set
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
601
    if ($model) {
add models() attribute
Yuki Kimoto authored on 2011-02-21
602
        $self->models->{$name} = $model;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
603
        return $self;
604
    }
605
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
606
    # Check model existance
607
    croak qq{Model "$name" is not included}
add models() attribute
Yuki Kimoto authored on 2011-02-21
608
      unless $self->models->{$name};
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
609
    
610
    # Get
add models() attribute
Yuki Kimoto authored on 2011-02-21
611
    return $self->models->{$name};
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
612
}
613

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
614
sub include_model {
615
    my ($self, $name_space, $model_infos) = @_;
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
616
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
617
    $name_space ||= '';
618
    unless ($model_infos) {
619
        # Load name space module
620
        croak qq{"$name_space" is invalid class name}
621
          if $name_space =~ /[^\w:]/;
622
        eval "use $name_space";
623
        croak qq{Name space module "$name_space.pm" is needed. $@} if $@;
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
624
        
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
625
        # Search model modules
626
        my $path = $INC{"$name_space.pm"};
627
        $path =~ s/\.pm$//;
628
        opendir my $dh, $path
629
          or croak qq{Can't open directory "$path": $!};
630
        $model_infos = [];
631
        while (my $module = readdir $dh) {
632
            push @$model_infos, $module
633
              if $module =~ s/\.pm$//;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
634
        }
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
635
        
636
        close $dh;
637
    }
638
    
639
    foreach my $model_info (@$model_infos) {
640
        
641
        # Model name and class
642
        my $model_name;
643
        my $model_class;
644
        if (ref $model_info eq 'HASH') {
645
            $model_name = (keys %$model_info)[0];
646
            $model_class = $model_info->{$model_name};
647
        }
648
        else { $model_name = $model_class = $model_info }
649
        my $mclass = "${name_space}::$model_class";
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
650
        
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
651
        # Load
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
652
        croak qq{"$mclass" is invalid class name}
653
          if $mclass =~ /[^\w:]/;
654
        unless ($mclass->can('isa')) {
655
            eval "use $mclass";
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
656
            croak $@ if $@;
657
        }
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
658
        
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
659
        # Instantiate
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
660
        my $model = $mclass->new(dbi => $self);
661
        $model->table($model_name) unless $model->table;
removed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-28
662
        
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
663
        # Set
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
664
        $self->model($model_name, $model);
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
665
    }
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
666
    return $self;
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
667
}
668

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

            
673
sub update {
674
    my ($self, %args) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
675
    
cleanup
yuki-kimoto authored on 2010-10-17
676
    # Check arguments
677
    foreach my $name (keys %args) {
678
        croak qq{"$name" is invalid argument}
679
          unless $VALID_UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
680
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
681
    
cleanup
yuki-kimoto authored on 2010-10-17
682
    # Arguments
683
    my $table            = $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
684
    croak qq{"table" option must be specified} unless $table;
cleanup
yuki-kimoto authored on 2010-10-17
685
    my $param            = $args{param} || {};
686
    my $where            = $args{where} || {};
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
687
    my $append           = $args{append} || '';
cleanup
yuki-kimoto authored on 2010-10-17
688
    my $filter           = $args{filter};
689
    my $allow_update_all = $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
690
    
cleanup
yuki-kimoto authored on 2010-10-17
691
    # Update keys
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
692
    my @clumns = keys %$param;
693

            
694
    # Columns
695
    my @columns;
696
    my $safety = $self->safety_column_name;
697
    foreach my $column (keys %$param) {
698
        croak qq{"$column" is not safety column name}
699
          unless $column =~ /$safety/;
700
        push @columns, $column;
701
    }
702
        
cleanup
yuki-kimoto authored on 2010-10-17
703
    # Update clause
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
704
    my $update_clause = '{update_param ' . join(' ', @clumns) . '}';
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
705

            
706
    # Where
707
    my $w;
708
    if (ref $where eq 'HASH') {
709
        my $clause = ['and'];
710
        push @$clause, "{= $_}" for keys %$where;
711
        $w = $self->where;
712
        $w->clause($clause);
713
        $w->param($where);
714
    }
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
715
    elsif (ref $where eq 'DBIx::Custom::Where') {
716
        $w = $where;
717
        $where = $w->param;
718
    }  
removed experimental registe...
yuki-kimoto authored on 2010-08-24
719
    
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
720
    croak qq{"where" must be hash refernce or DBIx::Custom::Where object}
721
      unless ref $w eq 'DBIx::Custom::Where';
removed reconnect method
yuki-kimoto authored on 2010-05-28
722
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
723
    # String where
724
    my $swhere = "$w";
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
725
    
726
    croak qq{"where" must be specified}
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
727
      if "$swhere" eq '' && !$allow_update_all;
removed reconnect method
yuki-kimoto authored on 2010-05-28
728
    
cleanup
Yuki Kimoto authored on 2011-01-27
729
    # SQL stack
730
    my @sql;
731
    
732
    # Update
733
    push @sql, "update $table $update_clause $swhere";
734
    push @sql, $append if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
735
    
cleanup
yuki-kimoto authored on 2010-10-17
736
    # Rearrange parameters
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
737
    foreach my $wkey (keys %$where) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
738
        
cleanup
yuki-kimoto authored on 2010-10-17
739
        if (exists $param->{$wkey}) {
740
            $param->{$wkey} = [$param->{$wkey}]
741
              unless ref $param->{$wkey} eq 'ARRAY';
742
            
743
            push @{$param->{$wkey}}, $where->{$wkey};
744
        }
745
        else {
746
            $param->{$wkey} = $where->{$wkey};
747
        }
removed reconnect method
yuki-kimoto authored on 2010-05-28
748
    }
cleanup
yuki-kimoto authored on 2010-10-17
749
    
cleanup
Yuki Kimoto authored on 2011-01-27
750
    # SQL
751
    my $sql = join(' ', @sql);
752
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
753
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
754
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
755
    return $query if $args{query};
756
    
cleanup
yuki-kimoto authored on 2010-10-17
757
    # Execute query
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
758
    my $ret_val = $self->execute($query, param  => $param, 
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
759
                                 filter => $filter,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
760
                                 table => $table);
cleanup
yuki-kimoto authored on 2010-10-17
761
    
762
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
763
}
764

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

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

            
770
    return DBIx::Custom::Where->new(
771
        query_builder => $self->query_builder,
772
        safety_column_name => $self->safety_column_name
773
    );
cleanup
Yuki Kimoto authored on 2011-01-25
774
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
775

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
776
sub _bind {
cleanup
Yuki Kimoto authored on 2011-01-12
777
    my ($self, $params, $columns, $filter) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
778
    
cleanup
Yuki Kimoto authored on 2011-01-12
779
    # bind values
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
780
    my @bind;
add tests
yuki-kimoto authored on 2010-08-08
781
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
782
    # Build bind values
783
    my $count = {};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
784
    my $not_exists = {};
cleanup
Yuki Kimoto authored on 2011-01-12
785
    foreach my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
786
        
787
        # Value
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
788
        my $value;
789
        if(ref $params->{$column} eq 'ARRAY') {
790
            my $i = $count->{$column} || 0;
791
            $i += $not_exists->{$column} || 0;
792
            my $found;
793
            for (my $k = $i; $i < @{$params->{$column}}; $k++) {
794
                if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
795
                    $not_exists->{$column}++;
796
                }
797
                else  {
798
                    $value = $params->{$column}->[$k];
799
                    $found = 1;
800
                    last
801
                }
802
            }
803
            next unless $found;
804
        }
805
        else { $value = $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
806
        
cleanup
Yuki Kimoto authored on 2011-01-12
807
        # Filter
808
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
809
        
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
810
        push @bind, $f ? $f->($value) : $value;
removed reconnect method
yuki-kimoto authored on 2010-05-28
811
        
812
        # Count up 
813
        $count->{$column}++;
814
    }
815
    
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
816
    return \@bind;
removed reconnect method
yuki-kimoto authored on 2010-05-28
817
}
818

            
cleanup
yuki-kimoto authored on 2010-10-17
819
sub _croak {
820
    my ($self, $error, $append) = @_;
821
    $append ||= "";
822
    
823
    # Verbose
824
    if ($Carp::Verbose) { croak $error }
825
    
826
    # Not verbose
827
    else {
828
        
829
        # Remove line and module infromation
830
        my $at_pos = rindex($error, ' at ');
831
        $error = substr($error, 0, $at_pos);
832
        $error =~ s/\s+$//;
833
        
834
        croak "$error$append";
835
    }
836
}
837

            
cleanup
Yuki Kimoto authored on 2011-01-25
838
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-23
839
__PACKAGE__->attr(
840
    dbi_options => sub { {} },
841
    filter_check  => 1
842
);
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
843

            
cleanup
Yuki Kimoto authored on 2011-01-25
844
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
845
sub default_bind_filter {
846
    my $self = shift;
847
    
848
    if (@_) {
849
        my $fname = $_[0];
850
        
851
        if (@_ && !$fname) {
852
            $self->{default_out_filter} = undef;
853
        }
854
        else {
many changed
Yuki Kimoto authored on 2011-01-23
855
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
856
              unless exists $self->filters->{$fname};
857
        
858
            $self->{default_out_filter} = $self->filters->{$fname};
859
        }
860
        return $self;
861
    }
862
    
863
    return $self->{default_out_filter};
864
}
865

            
cleanup
Yuki Kimoto authored on 2011-01-25
866
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
867
sub default_fetch_filter {
868
    my $self = shift;
869
    
870
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
871
        my $fname = $_[0];
872

            
cleanup
Yuki Kimoto authored on 2011-01-12
873
        if (@_ && !$fname) {
874
            $self->{default_in_filter} = undef;
875
        }
876
        else {
many changed
Yuki Kimoto authored on 2011-01-23
877
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
878
              unless exists $self->filters->{$fname};
879
        
880
            $self->{default_in_filter} = $self->filters->{$fname};
881
        }
882
        
883
        return $self;
884
    }
885
    
many changed
Yuki Kimoto authored on 2011-01-23
886
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
887
}
888

            
cleanup
Yuki Kimoto authored on 2011-01-25
889
# DEPRECATED!
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
890
sub register_tag_processor {
891
    return shift->query_builder->register_tag_processor(@_);
892
}
893

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
896
=head1 NAME
897

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

            
900
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
901

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
907
    # Insert 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
908
    $dbi->insert(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
909
                 param  => {title => 'Perl', author => 'Ken'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
910
                 filter => {title => 'to_something'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
911
    
912
    # Update 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
913
    $dbi->update(table  => 'book', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
914
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
915
                 where  => {id => 5},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
916
                 filter => {title => 'to_something'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
917
    
918
    # Update all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
919
    $dbi->update_all(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
920
                     param  => {title => 'Perl'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
921
                     filter => {title => 'to_something'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
922
    
923
    # Delete
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
924
    $dbi->delete(table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
925
                 where  => {author => 'Ken'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
926
                 filter => {title => 'to_something'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
927
    
928
    # Delete all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
929
    $dbi->delete_all(table => 'book');
cleanup
yuki-kimoto authored on 2010-08-05
930

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
931
    # Select
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
932
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
933
        table  => 'book',
update document
yuki-kimoto authored on 2010-05-27
934
        column => [qw/author title/],
935
        where  => {author => 'Ken'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
936
        relation => {'book.id' => 'rental.book_id'},
updated document
yuki-kimoto authored on 2010-08-08
937
        append => 'order by id limit 5',
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
938
        filter => {title => 'to_something'}
added commit method
yuki-kimoto authored on 2010-05-27
939
    );
cleanup
yuki-kimoto authored on 2010-08-05
940

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
941
    # Execute SQL
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
942
    $dbi->execute("select title from book");
removed register_format()
yuki-kimoto authored on 2010-05-26
943
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
944
    # Execute SQL with hash binding and filtering
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
945
    $dbi->execute("select id from book where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
946
                  param  => {author => 'ken', title => '%Perl%'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
947
                  filter => {title => 'to_something'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
948

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

            
955
    # Get DBI object
956
    my $dbh = $dbi->dbh;
957

            
removed register_format()
yuki-kimoto authored on 2010-05-26
958
    # Fetch
959
    while (my $row = $result->fetch) {
960
        # ...
961
    }
962
    
963
    # Fetch hash
964
    while (my $row = $result->fetch_hash) {
965
        
966
    }
967
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
968
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
969

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

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

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

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

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

            
992
=head1 GUIDE
993

            
994
L<DBIx::Custom::Guide> - L<DBIx::Custom> complete guide
995

            
996
=head1 EXAMPLES
997

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1030
DBI options.
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1031

            
1032
Each option specified can ovewrite C<default_dbi_option>.
1033

            
1034
C<connect()> method use this value to connect the database.
1035

            
1036

            
1037
=head2 C<default_dbi_option>
1038

            
1039
    my $default_dbi_option = $dbi->default_dbi_option;
1040
    $dbi            = $dbi->default_dbi_option($default_dbi_option);
1041

            
1042
DBI default options.
1043

            
1044
    RaiseError => 1,
1045
    PrintError => 0,
1046
    AutoCommit => 1,
1047

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

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

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

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

            
add models() attribute
Yuki Kimoto authored on 2011-02-21
1057
Filters
1058

            
1059
=head2 C<(experimental) models>
1060

            
1061
    my $models = $dbi->models;
1062
    $dbi       = $dbi->models(\%models);
1063

            
1064
Models
1065

            
cleanup
yuki-kimoto authored on 2010-10-17
1066
=head2 C<password>
1067

            
1068
    my $password = $dbi->password;
1069
    $dbi         = $dbi->password('lkj&le`@s');
1070

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

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

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

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

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

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

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

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

            
1093
    my $safety_column_name = $self->safety_column_name;
1094
    $dbi                   = $self->safety_column_name($name);
1095

            
1096
Safety column name regex.
1097
Default is qr/^[\w\.]*$/
1098

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1101
    my $user = $dbi->user;
1102
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1103

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

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

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

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

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1115
    $dbi->apply_filter(
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1116
        $table,
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1117
        $column1 => {in => $infilter1, out => $outfilter1, end => $endfilter1}
1118
        $column2 => {in => $infilter2, out => $outfilter2, end =. $endfilter2}
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1119
        ...,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1120
    );
1121

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

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

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

            
1135
You can use three name as column name.
1136

            
1137
    1. column        : author
1138
    2. table.column  : book.author
1139
    3. table__column : book__author
1140

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1166
    my $result = $dbi->execute($query,  param => $params, filter => \%filter);
1167
    my $result = $dbi->execute($source, param => $params, filter => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1168

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1176
    $dbi->delete(table  => $table,
1177
                 where  => \%where,
1178
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1179
                 filter => \%filter,
1180
                 query  => 1);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1181

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1201
=head2 C<insert>
1202

            
1203
    $dbi->insert(table  => $table, 
1204
                 param  => \%param,
1205
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1206
                 filter => \%filter,
1207
                 query  => 1);
cleanup
yuki-kimoto authored on 2010-10-17
1208

            
1209
Execute insert statement.
1210
C<insert> method have C<table>, C<param>, C<append>
1211
and C<filter> arguments.
1212
C<table> is a table name.
1213
C<param> is the pairs of column name value. this must be hash reference.
1214
C<append> is a string added at the end of the SQL statement.
1215
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1216
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1217
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1218
This is overwrites C<default_bind_filter>.
1219
Return value of C<insert()> is the count of affected rows.
1220

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

            
pod fix
Yuki Kimoto authored on 2011-01-21
1223
    $dbi->each_column(
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1224
        sub {
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1225
            my ($self, $table, $column, $info) = @_;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1226
            
pod fix
Yuki Kimoto authored on 2011-01-21
1227
            my $type = $info->{TYPE_NAME};
1228
            
1229
            if ($type eq 'DATE') {
1230
                # ...
1231
            }
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1232
        }
1233
    );
pod fix
Yuki Kimoto authored on 2011-01-21
1234
Get column informations from database.
1235
Argument is callback.
1236
You can do anything in callback.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1237
Callback receive four arguments, dbi object, table name,
1238
column name and columninformation.
1239

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1242
    $dbi->include_model(
1243
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1244
            'book', 'person', 'company'
1245
        ]
1246
    );
1247

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

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

            
1254
    $dbi->include_model('MyModel');
1255

            
1256
Note that in this case name spece module is needed.
1257

            
1258
    # MyModel.pm
1259
    package MyModel;
1260
    
1261
    use base 'DBIx::Custom::Model';
1262

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1265
    MyModel::book
1266
    MyModel::person
1267
    MyModel::company
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1268

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

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

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1276
    $dbi->include_model(
1277
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1278
            {'book' => 'Book'},
1279
            {'person' => 'Person'}
1280
        ]
1281
    );
1282

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

            
1285
    $dbi->method(
1286
        update_or_insert => sub {
1287
            my $self = shift;
1288
            # do something
1289
        },
1290
        find_or_create   => sub {
1291
            my $self = shift;
1292
            # do something
1293
        }
1294
    );
1295

            
1296
Register method. These method is called from L<DBIx::Custom> object directory.
1297

            
1298
    $dbi->update_or_insert;
1299
    $dbi->find_or_create;
1300

            
1301
=head2 C<new>
1302

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

            
1306
Create a new L<DBIx::Custom> object.
1307

            
1308
=head2 C<(experimental) not_exists>
1309

            
1310
    my $not_exists = $dbi->not_exists;
1311

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1314
=head2 C<register_filter>
1315

            
1316
    $dbi->register_filter(%filters);
1317
    $dbi->register_filter(\%filters);
1318
    
1319
Register filter. Registered filters is available in the following attributes
1320
or arguments.
1321

            
1322
=over 4
1323

            
1324
=item *
1325

            
1326
C<filter> argument of C<insert()>, C<update()>,
1327
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1328
methods
1329

            
1330
=item *
1331

            
1332
C<execute()> method
1333

            
1334
=item *
1335

            
1336
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1337

            
1338
=item *
1339

            
1340
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1341

            
1342
=back
1343

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

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1346
    $dbi->register_tag(
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1347
        limit => sub {
1348
            ...;
1349
        }
1350
    );
1351

            
cleanup
Yuki Kimoto authored on 2011-01-25
1352
Register tag.
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1353

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

            
1356
    $dbi->rollback;
1357

            
1358
Rollback transaction.
1359
This is same as L<DBI>'s C<rollback>.
1360

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1361
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1362
    
cleanup
yuki-kimoto authored on 2010-08-05
1363
    my $result = $dbi->select(table    => $table,
1364
                              column   => [@column],
1365
                              where    => \%where,
1366
                              append   => $append,
1367
                              relation => \%relation,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1368
                              filter   => \%filter,
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
1369
                              query    => 1,
1370
                              selection => $selection);
update document
yuki-kimoto authored on 2009-11-19
1371

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1372
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1373
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1374
C<relation> and C<filter> arguments.
1375
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1376
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1377
C<append> is a string added at the end of the SQL statement.
1378
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1379
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1380
default to 0. This is experimental.
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
1381
C<selection> is string of column name and tables. This is experimental
1382

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1386
First element is a string. it contains tags,
1387
such as "{= title} or {like author}".
1388
Second element is paramters.
1389

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1392
    $dbi->update(table  => $table, 
1393
                 param  => \%params,
1394
                 where  => \%where,
1395
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1396
                 filter => \%filter,
1397
                 query  => 1)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1398

            
cleanup
yuki-kimoto authored on 2010-10-17
1399
Execute update statement.
1400
C<update> method have C<table>, C<param>, C<where>, C<append>
1401
and C<filter> arguments.
1402
C<table> is a table name.
1403
C<param> is column-value pairs. this must be hash reference.
1404
C<where> is where clause. this must be hash reference.
1405
C<append> is a string added at the end of the SQL statement.
1406
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1407
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1408
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1409
This is overwrites C<default_bind_filter>.
1410
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1411

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1414
    $dbi->model('book')->method(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1415
        insert => sub { ... },
1416
        update => sub { ... }
1417
    );
1418
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1419
    my $model = $dbi->model('book');
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1420

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1425
    $dbi->update_all(table  => $table, 
1426
                     param  => \%params,
1427
                     filter => \%filter,
1428
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1429

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

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

            
1437
    my $where = $dbi->where;
1438

            
1439
Create a new L<DBIx::Custom::Where> object.
1440

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

            
1443
    $dbi          = $dbi->cache_method(\&cache_method);
1444
    $cache_method = $dbi->cache_method
1445

            
1446
Method to set and get caches.
1447

            
cleanup
Yuki Kimoto authored on 2011-01-25
1448
=head1 Tags
1449

            
1450
The following tags is available.
1451

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

            
1454
Table tag
1455

            
1456
    {table TABLE}    ->    TABLE
1457

            
1458
This is used to teach what is applied table to C<execute()>.
1459

            
cleanup
Yuki Kimoto authored on 2011-01-25
1460
=head2 C<?>
1461

            
1462
Placeholder tag.
1463

            
1464
    {? NAME}    ->   ?
1465

            
1466
=head2 C<=>
1467

            
1468
Equal tag.
1469

            
1470
    {= NAME}    ->   NAME = ?
1471

            
1472
=head2 C<E<lt>E<gt>>
1473

            
1474
Not equal tag.
1475

            
1476
    {<> NAME}   ->   NAME <> ?
1477

            
1478
=head2 C<E<lt>>
1479

            
1480
Lower than tag
1481

            
1482
    {< NAME}    ->   NAME < ?
1483

            
1484
=head2 C<E<gt>>
1485

            
1486
Greater than tag
1487

            
1488
    {> NAME}    ->   NAME > ?
1489

            
1490
=head2 C<E<gt>=>
1491

            
1492
Greater than or equal tag
1493

            
1494
    {>= NAME}   ->   NAME >= ?
1495

            
1496
=head2 C<E<lt>=>
1497

            
1498
Lower than or equal tag
1499

            
1500
    {<= NAME}   ->   NAME <= ?
1501

            
1502
=head2 C<like>
1503

            
1504
Like tag
1505

            
1506
    {like NAME}   ->   NAME like ?
1507

            
1508
=head2 C<in>
1509

            
1510
In tag.
1511

            
1512
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
1513

            
1514
=head2 C<insert_param>
1515

            
1516
Insert parameter tag.
1517

            
1518
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
1519

            
1520
=head2 C<update_param>
1521

            
1522
Updata parameter tag.
1523

            
1524
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
1525

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

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

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

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

            
1535
C<< <kimoto.yuki at gmail.com> >>
1536

            
1537
L<http://github.com/yuki-kimoto/DBIx-Custom>
1538

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1539
=head1 AUTHOR
1540

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

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

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

            
1547
This program is free software; you can redistribute it and/or modify it
1548
under the same terms as Perl itself.
1549

            
1550
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1551

            
1552