DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1573 lines | 39.999kb
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

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
669
sub setup_model {
670
    my $self = shift;
671
    
672
    $self->each_column(
673
        sub {
674
            my ($self, $table, $column, $column_info) = @_;
675
            
676
            if (my $model = $self->models->{$table}) {
677
                push @{$model->columns}, $column;
678
            }
679
        }
680
    );
681
}
682

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

            
687
sub update {
688
    my ($self, %args) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
689
    
cleanup
yuki-kimoto authored on 2010-10-17
690
    # Check arguments
691
    foreach my $name (keys %args) {
692
        croak qq{"$name" is invalid argument}
693
          unless $VALID_UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
694
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
695
    
cleanup
yuki-kimoto authored on 2010-10-17
696
    # Arguments
697
    my $table            = $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
698
    croak qq{"table" option must be specified} unless $table;
cleanup
yuki-kimoto authored on 2010-10-17
699
    my $param            = $args{param} || {};
700
    my $where            = $args{where} || {};
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
701
    my $append           = $args{append} || '';
cleanup
yuki-kimoto authored on 2010-10-17
702
    my $filter           = $args{filter};
703
    my $allow_update_all = $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
704
    
cleanup
yuki-kimoto authored on 2010-10-17
705
    # Update keys
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
706
    my @clumns = keys %$param;
707

            
708
    # Columns
709
    my @columns;
710
    my $safety = $self->safety_column_name;
711
    foreach my $column (keys %$param) {
712
        croak qq{"$column" is not safety column name}
713
          unless $column =~ /$safety/;
714
        push @columns, $column;
715
    }
716
        
cleanup
yuki-kimoto authored on 2010-10-17
717
    # Update clause
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
718
    my $update_clause = '{update_param ' . join(' ', @clumns) . '}';
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
719

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

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

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

            
784
    return DBIx::Custom::Where->new(
785
        query_builder => $self->query_builder,
786
        safety_column_name => $self->safety_column_name
787
    );
cleanup
Yuki Kimoto authored on 2011-01-25
788
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
789

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

            
cleanup
yuki-kimoto authored on 2010-10-17
833
sub _croak {
834
    my ($self, $error, $append) = @_;
835
    $append ||= "";
836
    
837
    # Verbose
838
    if ($Carp::Verbose) { croak $error }
839
    
840
    # Not verbose
841
    else {
842
        
843
        # Remove line and module infromation
844
        my $at_pos = rindex($error, ' at ');
845
        $error = substr($error, 0, $at_pos);
846
        $error =~ s/\s+$//;
847
        
848
        croak "$error$append";
849
    }
850
}
851

            
cleanup
Yuki Kimoto authored on 2011-01-25
852
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-23
853
__PACKAGE__->attr(
854
    dbi_options => sub { {} },
855
    filter_check  => 1
856
);
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
857

            
cleanup
Yuki Kimoto authored on 2011-01-25
858
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
859
sub default_bind_filter {
860
    my $self = shift;
861
    
862
    if (@_) {
863
        my $fname = $_[0];
864
        
865
        if (@_ && !$fname) {
866
            $self->{default_out_filter} = undef;
867
        }
868
        else {
many changed
Yuki Kimoto authored on 2011-01-23
869
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
870
              unless exists $self->filters->{$fname};
871
        
872
            $self->{default_out_filter} = $self->filters->{$fname};
873
        }
874
        return $self;
875
    }
876
    
877
    return $self->{default_out_filter};
878
}
879

            
cleanup
Yuki Kimoto authored on 2011-01-25
880
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
881
sub default_fetch_filter {
882
    my $self = shift;
883
    
884
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
885
        my $fname = $_[0];
886

            
cleanup
Yuki Kimoto authored on 2011-01-12
887
        if (@_ && !$fname) {
888
            $self->{default_in_filter} = undef;
889
        }
890
        else {
many changed
Yuki Kimoto authored on 2011-01-23
891
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
892
              unless exists $self->filters->{$fname};
893
        
894
            $self->{default_in_filter} = $self->filters->{$fname};
895
        }
896
        
897
        return $self;
898
    }
899
    
many changed
Yuki Kimoto authored on 2011-01-23
900
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
901
}
902

            
cleanup
Yuki Kimoto authored on 2011-01-25
903
# DEPRECATED!
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
904
sub register_tag_processor {
905
    return shift->query_builder->register_tag_processor(@_);
906
}
907

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
910
=head1 NAME
911

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

            
914
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
915

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
921
    # Insert 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
922
    $dbi->insert(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
923
                 param  => {title => 'Perl', author => 'Ken'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
924
                 filter => {title => 'to_something'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
925
    
926
    # Update 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
927
    $dbi->update(table  => 'book', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
928
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
929
                 where  => {id => 5},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
930
                 filter => {title => 'to_something'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
931
    
932
    # Update all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
933
    $dbi->update_all(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
934
                     param  => {title => 'Perl'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
935
                     filter => {title => 'to_something'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
936
    
937
    # Delete
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
938
    $dbi->delete(table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
939
                 where  => {author => 'Ken'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
940
                 filter => {title => 'to_something'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
941
    
942
    # Delete all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
943
    $dbi->delete_all(table => 'book');
cleanup
yuki-kimoto authored on 2010-08-05
944

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
945
    # Select
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
946
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
947
        table  => 'book',
update document
yuki-kimoto authored on 2010-05-27
948
        column => [qw/author title/],
949
        where  => {author => 'Ken'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
950
        relation => {'book.id' => 'rental.book_id'},
updated document
yuki-kimoto authored on 2010-08-08
951
        append => 'order by id limit 5',
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
952
        filter => {title => 'to_something'}
added commit method
yuki-kimoto authored on 2010-05-27
953
    );
cleanup
yuki-kimoto authored on 2010-08-05
954

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

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

            
969
    # Get DBI object
970
    my $dbh = $dbi->dbh;
971

            
removed register_format()
yuki-kimoto authored on 2010-05-26
972
    # Fetch
973
    while (my $row = $result->fetch) {
974
        # ...
975
    }
976
    
977
    # Fetch hash
978
    while (my $row = $result->fetch_hash) {
979
        
980
    }
981
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
982
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
983

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

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

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

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

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

            
1006
=head1 GUIDE
1007

            
1008
L<DBIx::Custom::Guide> - L<DBIx::Custom> complete guide
1009

            
1010
=head1 EXAMPLES
1011

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1044
DBI options.
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1045

            
1046
Each option specified can ovewrite C<default_dbi_option>.
1047

            
1048
C<connect()> method use this value to connect the database.
1049

            
1050

            
1051
=head2 C<default_dbi_option>
1052

            
1053
    my $default_dbi_option = $dbi->default_dbi_option;
1054
    $dbi            = $dbi->default_dbi_option($default_dbi_option);
1055

            
1056
DBI default options.
1057

            
1058
    RaiseError => 1,
1059
    PrintError => 0,
1060
    AutoCommit => 1,
1061

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

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

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

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

            
add models() attribute
Yuki Kimoto authored on 2011-02-21
1071
Filters
1072

            
1073
=head2 C<(experimental) models>
1074

            
1075
    my $models = $dbi->models;
1076
    $dbi       = $dbi->models(\%models);
1077

            
1078
Models
1079

            
cleanup
yuki-kimoto authored on 2010-10-17
1080
=head2 C<password>
1081

            
1082
    my $password = $dbi->password;
1083
    $dbi         = $dbi->password('lkj&le`@s');
1084

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

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

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

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

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

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

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

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

            
1107
    my $safety_column_name = $self->safety_column_name;
1108
    $dbi                   = $self->safety_column_name($name);
1109

            
1110
Safety column name regex.
1111
Default is qr/^[\w\.]*$/
1112

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1115
    my $user = $dbi->user;
1116
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1117

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

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

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

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

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1129
    $dbi->apply_filter(
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1130
        $table,
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1131
        $column1 => {in => $infilter1, out => $outfilter1, end => $endfilter1}
1132
        $column2 => {in => $infilter2, out => $outfilter2, end =. $endfilter2}
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1133
        ...,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1134
    );
1135

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

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

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

            
1149
You can use three name as column name.
1150

            
1151
    1. column        : author
1152
    2. table.column  : book.author
1153
    3. table__column : book__author
1154

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1190
    $dbi->delete(table  => $table,
1191
                 where  => \%where,
1192
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1193
                 filter => \%filter,
1194
                 query  => 1);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1195

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1215
=head2 C<insert>
1216

            
1217
    $dbi->insert(table  => $table, 
1218
                 param  => \%param,
1219
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1220
                 filter => \%filter,
1221
                 query  => 1);
cleanup
yuki-kimoto authored on 2010-10-17
1222

            
1223
Execute insert statement.
1224
C<insert> method have C<table>, C<param>, C<append>
1225
and C<filter> arguments.
1226
C<table> is a table name.
1227
C<param> is the pairs of column name value. this must be hash reference.
1228
C<append> is a string added at the end of the SQL statement.
1229
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1230
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1231
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1232
This is overwrites C<default_bind_filter>.
1233
Return value of C<insert()> is the count of affected rows.
1234

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

            
pod fix
Yuki Kimoto authored on 2011-01-21
1237
    $dbi->each_column(
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1238
        sub {
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1239
            my ($self, $table, $column, $column_info) = @_;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1240
            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1241
            my $type = $column_info->{TYPE_NAME};
pod fix
Yuki Kimoto authored on 2011-01-21
1242
            
1243
            if ($type eq 'DATE') {
1244
                # ...
1245
            }
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1246
        }
1247
    );
pod fix
Yuki Kimoto authored on 2011-01-21
1248
Get column informations from database.
1249
Argument is callback.
1250
You can do anything in callback.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1251
Callback receive four arguments, dbi object, table name,
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1252
column name and column information.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1253

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1256
    $dbi->include_model(
1257
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1258
            'book', 'person', 'company'
1259
        ]
1260
    );
1261

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

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

            
1268
    $dbi->include_model('MyModel');
1269

            
1270
Note that in this case name spece module is needed.
1271

            
1272
    # MyModel.pm
1273
    package MyModel;
1274
    
1275
    use base 'DBIx::Custom::Model';
1276

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1279
    MyModel::book
1280
    MyModel::person
1281
    MyModel::company
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1282

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

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

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1290
    $dbi->include_model(
1291
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1292
            {'book' => 'Book'},
1293
            {'person' => 'Person'}
1294
        ]
1295
    );
1296

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

            
1299
    $dbi->method(
1300
        update_or_insert => sub {
1301
            my $self = shift;
1302
            # do something
1303
        },
1304
        find_or_create   => sub {
1305
            my $self = shift;
1306
            # do something
1307
        }
1308
    );
1309

            
1310
Register method. These method is called from L<DBIx::Custom> object directory.
1311

            
1312
    $dbi->update_or_insert;
1313
    $dbi->find_or_create;
1314

            
1315
=head2 C<new>
1316

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

            
1320
Create a new L<DBIx::Custom> object.
1321

            
1322
=head2 C<(experimental) not_exists>
1323

            
1324
    my $not_exists = $dbi->not_exists;
1325

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1328
=head2 C<register_filter>
1329

            
1330
    $dbi->register_filter(%filters);
1331
    $dbi->register_filter(\%filters);
1332
    
1333
Register filter. Registered filters is available in the following attributes
1334
or arguments.
1335

            
1336
=over 4
1337

            
1338
=item *
1339

            
1340
C<filter> argument of C<insert()>, C<update()>,
1341
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1342
methods
1343

            
1344
=item *
1345

            
1346
C<execute()> method
1347

            
1348
=item *
1349

            
1350
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1351

            
1352
=item *
1353

            
1354
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1355

            
1356
=back
1357

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

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1360
    $dbi->register_tag(
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1361
        limit => sub {
1362
            ...;
1363
        }
1364
    );
1365

            
cleanup
Yuki Kimoto authored on 2011-01-25
1366
Register tag.
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1367

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

            
1370
    $dbi->rollback;
1371

            
1372
Rollback transaction.
1373
This is same as L<DBI>'s C<rollback>.
1374

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1375
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1376
    
cleanup
yuki-kimoto authored on 2010-08-05
1377
    my $result = $dbi->select(table    => $table,
1378
                              column   => [@column],
1379
                              where    => \%where,
1380
                              append   => $append,
1381
                              relation => \%relation,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1382
                              filter   => \%filter,
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
1383
                              query    => 1,
1384
                              selection => $selection);
update document
yuki-kimoto authored on 2009-11-19
1385

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1386
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1387
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1388
C<relation> and C<filter> arguments.
1389
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1390
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1391
C<append> is a string added at the end of the SQL statement.
1392
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1393
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1394
default to 0. This is experimental.
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
1395
C<selection> is string of column name and tables. This is experimental
1396

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1400
First element is a string. it contains tags,
1401
such as "{= title} or {like author}".
1402
Second element is paramters.
1403

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1406
    $dbi->update(table  => $table, 
1407
                 param  => \%params,
1408
                 where  => \%where,
1409
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1410
                 filter => \%filter,
1411
                 query  => 1)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1412

            
cleanup
yuki-kimoto authored on 2010-10-17
1413
Execute update statement.
1414
C<update> method have C<table>, C<param>, C<where>, C<append>
1415
and C<filter> arguments.
1416
C<table> is a table name.
1417
C<param> is column-value pairs. this must be hash reference.
1418
C<where> is where clause. this must be hash reference.
1419
C<append> is a string added at the end of the SQL statement.
1420
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1421
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1422
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1423
This is overwrites C<default_bind_filter>.
1424
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1425

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1428
    $dbi->model('book')->method(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1429
        insert => sub { ... },
1430
        update => sub { ... }
1431
    );
1432
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1433
    my $model = $dbi->model('book');
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1434

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

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

            
1439
    $dbi->setup_model;
1440

            
1441
Setup all model objects.
1442
C<columns> and C<primary_key> is automatically set.
1443

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1446
    $dbi->update_all(table  => $table, 
1447
                     param  => \%params,
1448
                     filter => \%filter,
1449
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1450

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

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

            
1458
    my $where = $dbi->where;
1459

            
1460
Create a new L<DBIx::Custom::Where> object.
1461

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

            
1464
    $dbi          = $dbi->cache_method(\&cache_method);
1465
    $cache_method = $dbi->cache_method
1466

            
1467
Method to set and get caches.
1468

            
cleanup
Yuki Kimoto authored on 2011-01-25
1469
=head1 Tags
1470

            
1471
The following tags is available.
1472

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

            
1475
Table tag
1476

            
1477
    {table TABLE}    ->    TABLE
1478

            
1479
This is used to teach what is applied table to C<execute()>.
1480

            
cleanup
Yuki Kimoto authored on 2011-01-25
1481
=head2 C<?>
1482

            
1483
Placeholder tag.
1484

            
1485
    {? NAME}    ->   ?
1486

            
1487
=head2 C<=>
1488

            
1489
Equal tag.
1490

            
1491
    {= NAME}    ->   NAME = ?
1492

            
1493
=head2 C<E<lt>E<gt>>
1494

            
1495
Not equal tag.
1496

            
1497
    {<> NAME}   ->   NAME <> ?
1498

            
1499
=head2 C<E<lt>>
1500

            
1501
Lower than tag
1502

            
1503
    {< NAME}    ->   NAME < ?
1504

            
1505
=head2 C<E<gt>>
1506

            
1507
Greater than tag
1508

            
1509
    {> NAME}    ->   NAME > ?
1510

            
1511
=head2 C<E<gt>=>
1512

            
1513
Greater than or equal tag
1514

            
1515
    {>= NAME}   ->   NAME >= ?
1516

            
1517
=head2 C<E<lt>=>
1518

            
1519
Lower than or equal tag
1520

            
1521
    {<= NAME}   ->   NAME <= ?
1522

            
1523
=head2 C<like>
1524

            
1525
Like tag
1526

            
1527
    {like NAME}   ->   NAME like ?
1528

            
1529
=head2 C<in>
1530

            
1531
In tag.
1532

            
1533
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
1534

            
1535
=head2 C<insert_param>
1536

            
1537
Insert parameter tag.
1538

            
1539
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
1540

            
1541
=head2 C<update_param>
1542

            
1543
Updata parameter tag.
1544

            
1545
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
1546

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

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

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

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

            
1556
C<< <kimoto.yuki at gmail.com> >>
1557

            
1558
L<http://github.com/yuki-kimoto/DBIx-Custom>
1559

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1560
=head1 AUTHOR
1561

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

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

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

            
1568
This program is free software; you can redistribute it and/or modify it
1569
under the same terms as Perl itself.
1570

            
1571
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1572

            
1573