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

            
cleanup
Yuki Kimoto authored on 2011-02-09
3
our $VERSION = '0.1643';
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;
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
17
use DBIx::Custom::Table;
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,
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
24
    dbi_option    => sub { {} },
cleanup
Yuki Kimoto authored on 2011-01-23
25
    query_builder => sub { DBIx::Custom::QueryBuilder->new },
many changed
Yuki Kimoto authored on 2011-01-23
26
    result_class  => 'DBIx::Custom::Result',
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
27
    base_table    => sub { DBIx::Custom::Table->new(dbi => shift) },
update pod
Yuki Kimoto authored on 2011-02-07
28
    safety_column_name => sub { qr/^[\w\.]*$/ },
29
    stash => sub { {} }
many changed
Yuki Kimoto authored on 2011-01-23
30
);
31

            
32
__PACKAGE__->attr(
33
    cache_method => sub {
34
        sub {
35
            my $self = shift;
36
            
37
            $self->{_cached} ||= {};
38
            
39
            if (@_ > 1) {
40
                $self->{_cached}{$_[0]} = $_[1] 
41
            }
42
            else {
43
                return $self->{_cached}{$_[0]}
44
            }
45
        }
46
    }
47
);
48

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

            
added helper method
yuki-kimoto authored on 2010-10-17
58
our $AUTOLOAD;
59
sub AUTOLOAD {
60
    my $self = shift;
61

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

            
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
65
    # Method
66
    $self->{_methods} ||= {};
autoload DBI method
Yuki Kimoto authored on 2011-01-26
67
    my $method = $self->{_methods}->{$mname};
68
    return $self->$method(@_) if $method;
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
69
    
autoload DBI method
Yuki Kimoto authored on 2011-01-26
70
    # DBI method
71
    return $self->dbh->$mname(@_);
added helper method
yuki-kimoto authored on 2010-10-17
72
}
73

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

            
77
    # Initialize filters
cleanup
Yuki Kimoto authored on 2011-01-12
78
    $self->{filter} ||= {};
many changed
Yuki Kimoto authored on 2011-01-23
79
    $self->{filter}{out} ||= {};
80
    $self->{filter}{in} ||= {};
cleanup
Yuki Kimoto authored on 2010-12-22
81
    
many changed
Yuki Kimoto authored on 2011-01-23
82
    # Create filters
83
    my $usage = "Usage: \$dbi->apply_filter(" .
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
84
                "TABLE, COLUMN1, {in => INFILTER1, out => OUTFILTER1, end => ENDFILTER1}, " .
85
                "COLUMN2, {in => INFILTER2, out => OUTFILTER2, end => ENDFILTER2}, ...)";
many changed
Yuki Kimoto authored on 2011-01-23
86

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

            
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
129
sub method {
added helper method
yuki-kimoto authored on 2010-10-17
130
    my $self = shift;
131
    
132
    # Merge
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
133
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
134
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
added helper method
yuki-kimoto authored on 2010-10-17
135
    
136
    return $self;
137
}
138

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
194
        # Create SQL object
195
        my $builder = $self->query_builder;
196
        
197
        # Create query
198
        $query = $builder->build_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
199

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-01-27
264
    # SQL stack
265
    my @sql;
266

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

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

            
added helper method
yuki-kimoto authored on 2010-10-17
287
sub DESTROY { }
288

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

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

            
372
        return $result;
373
    }
374
    return $affected;
375
}
376

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

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

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

            
added dbi_options attribute
kimoto authored on 2010-12-20
448
sub new {
449
    my $self = shift->SUPER::new(@_);
450
    
451
    # Check attribute names
452
    my @attrs = keys %$self;
453
    foreach my $attr (@attrs) {
454
        croak qq{"$attr" is invalid attribute name}
455
          unless $self->can($attr);
456
    }
cleanup
Yuki Kimoto authored on 2011-01-25
457

            
458
    $self->register_tag(
459
        '?'     => \&DBIx::Custom::Tag::placeholder,
460
        '='     => \&DBIx::Custom::Tag::equal,
461
        '<>'    => \&DBIx::Custom::Tag::not_equal,
462
        '>'     => \&DBIx::Custom::Tag::greater_than,
463
        '<'     => \&DBIx::Custom::Tag::lower_than,
464
        '>='    => \&DBIx::Custom::Tag::greater_than_equal,
465
        '<='    => \&DBIx::Custom::Tag::lower_than_equal,
466
        'like'  => \&DBIx::Custom::Tag::like,
467
        'in'    => \&DBIx::Custom::Tag::in,
468
        'insert_param' => \&DBIx::Custom::Tag::insert_param,
469
        'update_param' => \&DBIx::Custom::Tag::update_param
470
    );
added dbi_options attribute
kimoto authored on 2010-12-20
471
    
472
    return $self;
473
}
474

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

            
cleanup
yuki-kimoto authored on 2010-10-17
477
sub register_filter {
478
    my $invocant = shift;
479
    
480
    # Register filter
481
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
482
    $invocant->filters({%{$invocant->filters}, %$filters});
483
    
484
    return $invocant;
485
}
packaging one directory
yuki-kimoto authored on 2009-11-16
486

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

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

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

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
590
sub table {
591
    my $self = shift;
592
    my $name = shift;
593
    
594
    # Create table
595
    $self->{_tables} ||= {};
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
596
    unless (defined $self->{_tables}->{$name}) {
597
        # Base table
598
        my $base_table = $self->base_table;
599
        
600
        # Base methods
601
        my $bmethods = ref $base_table->{_methods} eq 'HASH'
602
                     ? $base_table->{_methods}
603
                     : {};
604
        
605
        # Copy Methods
606
        my $methods = {};
607
        $methods->{$_} = $bmethods->{$_} for keys %$bmethods;
removed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-28
608
        $methods->{"base_$_"} = $bmethods->{$_} for keys %$bmethods;
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
609
        
610
        # Create table
611
        my $table = $base_table->new(
612
            dbi      => $self,
613
            name     => $name,
614
        );
removed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-28
615
        $table->method($methods);
616
        
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
617
        $self->{_tables}->{$name} = $table;
618
    }
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
619
    
620
    return $self->{_tables}{$name};
621
}
622

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

            
627
sub update {
628
    my ($self, %args) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
629
    
cleanup
yuki-kimoto authored on 2010-10-17
630
    # Check arguments
631
    foreach my $name (keys %args) {
632
        croak qq{"$name" is invalid argument}
633
          unless $VALID_UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
634
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
635
    
cleanup
yuki-kimoto authored on 2010-10-17
636
    # Arguments
637
    my $table            = $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
638
    croak qq{"table" option must be specified} unless $table;
cleanup
yuki-kimoto authored on 2010-10-17
639
    my $param            = $args{param} || {};
640
    my $where            = $args{where} || {};
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
641
    my $append           = $args{append} || '';
cleanup
yuki-kimoto authored on 2010-10-17
642
    my $filter           = $args{filter};
643
    my $allow_update_all = $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
644
    
cleanup
yuki-kimoto authored on 2010-10-17
645
    # Update keys
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
646
    my @clumns = keys %$param;
647

            
648
    # Columns
649
    my @columns;
650
    my $safety = $self->safety_column_name;
651
    foreach my $column (keys %$param) {
652
        croak qq{"$column" is not safety column name}
653
          unless $column =~ /$safety/;
654
        push @columns, $column;
655
    }
656
        
cleanup
yuki-kimoto authored on 2010-10-17
657
    # Update clause
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
658
    my $update_clause = '{update_param ' . join(' ', @clumns) . '}';
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
659

            
660
    # Where
661
    my $w;
662
    if (ref $where eq 'HASH') {
663
        my $clause = ['and'];
664
        push @$clause, "{= $_}" for keys %$where;
665
        $w = $self->where;
666
        $w->clause($clause);
667
        $w->param($where);
668
    }
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
669
    elsif (ref $where eq 'DBIx::Custom::Where') {
670
        $w = $where;
671
        $where = $w->param;
672
    }  
removed experimental registe...
yuki-kimoto authored on 2010-08-24
673
    
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
674
    croak qq{"where" must be hash refernce or DBIx::Custom::Where object}
675
      unless ref $w eq 'DBIx::Custom::Where';
removed reconnect method
yuki-kimoto authored on 2010-05-28
676
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
677
    # String where
678
    my $swhere = "$w";
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
679
    
680
    croak qq{"where" must be specified}
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
681
      if "$swhere" eq '' && !$allow_update_all;
removed reconnect method
yuki-kimoto authored on 2010-05-28
682
    
cleanup
Yuki Kimoto authored on 2011-01-27
683
    # SQL stack
684
    my @sql;
685
    
686
    # Update
687
    push @sql, "update $table $update_clause $swhere";
688
    push @sql, $append if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
689
    
cleanup
yuki-kimoto authored on 2010-10-17
690
    # Rearrange parameters
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
691
    foreach my $wkey (keys %$where) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
692
        
cleanup
yuki-kimoto authored on 2010-10-17
693
        if (exists $param->{$wkey}) {
694
            $param->{$wkey} = [$param->{$wkey}]
695
              unless ref $param->{$wkey} eq 'ARRAY';
696
            
697
            push @{$param->{$wkey}}, $where->{$wkey};
698
        }
699
        else {
700
            $param->{$wkey} = $where->{$wkey};
701
        }
removed reconnect method
yuki-kimoto authored on 2010-05-28
702
    }
cleanup
yuki-kimoto authored on 2010-10-17
703
    
cleanup
Yuki Kimoto authored on 2011-01-27
704
    # SQL
705
    my $sql = join(' ', @sql);
706
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
707
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
708
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
709
    return $query if $args{query};
710
    
cleanup
yuki-kimoto authored on 2010-10-17
711
    # Execute query
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
712
    my $ret_val = $self->execute($query, param  => $param, 
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
713
                                 filter => $filter,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
714
                                 table => $table);
cleanup
yuki-kimoto authored on 2010-10-17
715
    
716
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
717
}
718

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

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

            
724
    return DBIx::Custom::Where->new(
725
        query_builder => $self->query_builder,
726
        safety_column_name => $self->safety_column_name
727
    );
cleanup
Yuki Kimoto authored on 2011-01-25
728
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
729

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
730
sub _bind {
cleanup
Yuki Kimoto authored on 2011-01-12
731
    my ($self, $params, $columns, $filter) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
732
    
cleanup
Yuki Kimoto authored on 2011-01-12
733
    # bind values
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
734
    my @bind;
add tests
yuki-kimoto authored on 2010-08-08
735
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
736
    # Build bind values
737
    my $count = {};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
738
    my $not_exists = {};
cleanup
Yuki Kimoto authored on 2011-01-12
739
    foreach my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
740
        
741
        # Value
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
742
        my $value;
743
        if(ref $params->{$column} eq 'ARRAY') {
744
            my $i = $count->{$column} || 0;
745
            $i += $not_exists->{$column} || 0;
746
            my $found;
747
            for (my $k = $i; $i < @{$params->{$column}}; $k++) {
748
                if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
749
                    $not_exists->{$column}++;
750
                }
751
                else  {
752
                    $value = $params->{$column}->[$k];
753
                    $found = 1;
754
                    last
755
                }
756
            }
757
            next unless $found;
758
        }
759
        else { $value = $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
760
        
cleanup
Yuki Kimoto authored on 2011-01-12
761
        # Filter
762
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
763
        
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
764
        push @bind, $f ? $f->($value) : $value;
removed reconnect method
yuki-kimoto authored on 2010-05-28
765
        
766
        # Count up 
767
        $count->{$column}++;
768
    }
769
    
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
770
    return \@bind;
removed reconnect method
yuki-kimoto authored on 2010-05-28
771
}
772

            
cleanup
yuki-kimoto authored on 2010-10-17
773
sub _croak {
774
    my ($self, $error, $append) = @_;
775
    $append ||= "";
776
    
777
    # Verbose
778
    if ($Carp::Verbose) { croak $error }
779
    
780
    # Not verbose
781
    else {
782
        
783
        # Remove line and module infromation
784
        my $at_pos = rindex($error, ' at ');
785
        $error = substr($error, 0, $at_pos);
786
        $error =~ s/\s+$//;
787
        
788
        croak "$error$append";
789
    }
790
}
791

            
cleanup
Yuki Kimoto authored on 2011-01-25
792
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-23
793
__PACKAGE__->attr(
794
    dbi_options => sub { {} },
795
    filter_check  => 1
796
);
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
797

            
cleanup
Yuki Kimoto authored on 2011-01-25
798
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
799
sub default_bind_filter {
800
    my $self = shift;
801
    
802
    if (@_) {
803
        my $fname = $_[0];
804
        
805
        if (@_ && !$fname) {
806
            $self->{default_out_filter} = undef;
807
        }
808
        else {
many changed
Yuki Kimoto authored on 2011-01-23
809
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
810
              unless exists $self->filters->{$fname};
811
        
812
            $self->{default_out_filter} = $self->filters->{$fname};
813
        }
814
        return $self;
815
    }
816
    
817
    return $self->{default_out_filter};
818
}
819

            
cleanup
Yuki Kimoto authored on 2011-01-25
820
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
821
sub default_fetch_filter {
822
    my $self = shift;
823
    
824
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
825
        my $fname = $_[0];
826

            
cleanup
Yuki Kimoto authored on 2011-01-12
827
        if (@_ && !$fname) {
828
            $self->{default_in_filter} = undef;
829
        }
830
        else {
many changed
Yuki Kimoto authored on 2011-01-23
831
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
832
              unless exists $self->filters->{$fname};
833
        
834
            $self->{default_in_filter} = $self->filters->{$fname};
835
        }
836
        
837
        return $self;
838
    }
839
    
many changed
Yuki Kimoto authored on 2011-01-23
840
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
841
}
842

            
cleanup
Yuki Kimoto authored on 2011-01-25
843
# DEPRECATED!
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
844
sub register_tag_processor {
845
    return shift->query_builder->register_tag_processor(@_);
846
}
847

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
850
=head1 NAME
851

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

            
854
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
855

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
861
    # Insert 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
862
    $dbi->insert(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
863
                 param  => {title => 'Perl', author => 'Ken'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
864
                 filter => {title => 'to_something'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
865
    
866
    # Update 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
867
    $dbi->update(table  => 'book', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
868
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
869
                 where  => {id => 5},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
870
                 filter => {title => 'to_something'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
871
    
872
    # Update all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
873
    $dbi->update_all(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
874
                     param  => {title => 'Perl'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
875
                     filter => {title => 'to_something'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
876
    
877
    # Delete
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
878
    $dbi->delete(table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
879
                 where  => {author => 'Ken'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
880
                 filter => {title => 'to_something'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
881
    
882
    # Delete all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
883
    $dbi->delete_all(table => 'book');
cleanup
yuki-kimoto authored on 2010-08-05
884

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
885
    # Select
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
886
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
887
        table  => 'book',
update document
yuki-kimoto authored on 2010-05-27
888
        column => [qw/author title/],
889
        where  => {author => 'Ken'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
890
        relation => {'book.id' => 'rental.book_id'},
updated document
yuki-kimoto authored on 2010-08-08
891
        append => 'order by id limit 5',
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
892
        filter => {title => 'to_something'}
added commit method
yuki-kimoto authored on 2010-05-27
893
    );
cleanup
yuki-kimoto authored on 2010-08-05
894

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

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

            
909
    # Get DBI object
910
    my $dbh = $dbi->dbh;
911

            
removed register_format()
yuki-kimoto authored on 2010-05-26
912
    # Fetch
913
    while (my $row = $result->fetch) {
914
        # ...
915
    }
916
    
917
    # Fetch hash
918
    while (my $row = $result->fetch_hash) {
919
        
920
    }
921
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
922
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
923

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

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

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

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

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

            
946
=head1 GUIDE
947

            
948
L<DBIx::Custom::Guide> - L<DBIx::Custom> complete guide
949

            
950
=head1 EXAMPLES
951

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

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

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

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

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

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

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

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

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

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

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

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

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

            
984
DBI options.
985
C<connect()> method use this value to connect the database.
986

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
994
=head2 C<password>
995

            
996
    my $password = $dbi->password;
997
    $dbi         = $dbi->password('lkj&le`@s');
998

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

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

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

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

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

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

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

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

            
1021
    my $safety_column_name = $self->safety_column_name;
1022
    $dbi                   = $self->safety_column_name($name);
1023

            
1024
Safety column name regex.
1025
Default is qr/^[\w\.]*$/
1026

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1029
    my $user = $dbi->user;
1030
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1031

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

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

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

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

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1043
    $dbi->apply_filter(
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1044
        $table,
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1045
        $column1 => {in => $infilter1, out => $outfilter1, end => $endfilter1}
1046
        $column2 => {in => $infilter2, out => $outfilter2, end =. $endfilter2}
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1047
        ...,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1048
    );
1049

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

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

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

            
1063
You can use three name as column name.
1064

            
1065
    1. column        : author
1066
    2. table.column  : book.author
1067
    3. table__column : book__author
1068

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1104
    $dbi->delete(table  => $table,
1105
                 where  => \%where,
1106
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1107
                 filter => \%filter,
1108
                 query  => 1);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1109

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1129
=head2 C<insert>
1130

            
1131
    $dbi->insert(table  => $table, 
1132
                 param  => \%param,
1133
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1134
                 filter => \%filter,
1135
                 query  => 1);
cleanup
yuki-kimoto authored on 2010-10-17
1136

            
1137
Execute insert statement.
1138
C<insert> method have C<table>, C<param>, C<append>
1139
and C<filter> arguments.
1140
C<table> is a table name.
1141
C<param> is the pairs of column name value. this must be hash reference.
1142
C<append> is a string added at the end of the SQL statement.
1143
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1144
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1145
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1146
This is overwrites C<default_bind_filter>.
1147
Return value of C<insert()> is the count of affected rows.
1148

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

            
pod fix
Yuki Kimoto authored on 2011-01-21
1151
    $dbi->each_column(
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1152
        sub {
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1153
            my ($self, $table, $column, $info) = @_;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1154
            
pod fix
Yuki Kimoto authored on 2011-01-21
1155
            my $type = $info->{TYPE_NAME};
1156
            
1157
            if ($type eq 'DATE') {
1158
                # ...
1159
            }
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1160
        }
1161
    );
pod fix
Yuki Kimoto authored on 2011-01-21
1162
Get column informations from database.
1163
Argument is callback.
1164
You can do anything in callback.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1165
Callback receive four arguments, dbi object, table name,
1166
column name and columninformation.
1167

            
1168
=head2 C<(experimental) method>
1169

            
1170
    $dbi->method(
1171
        update_or_insert => sub {
1172
            my $self = shift;
1173
            # do something
1174
        },
1175
        find_or_create   => sub {
1176
            my $self = shift;
1177
            # do something
1178
        }
1179
    );
1180

            
1181
Register method. These method is called from L<DBIx::Custom> object directory.
1182

            
1183
    $dbi->update_or_insert;
1184
    $dbi->find_or_create;
1185

            
1186
=head2 C<new>
1187

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

            
1191
Create a new L<DBIx::Custom> object.
1192

            
1193
=head2 C<(experimental) not_exists>
1194

            
1195
    my $not_exists = $dbi->not_exists;
1196

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1199
=head2 C<register_filter>
1200

            
1201
    $dbi->register_filter(%filters);
1202
    $dbi->register_filter(\%filters);
1203
    
1204
Register filter. Registered filters is available in the following attributes
1205
or arguments.
1206

            
1207
=over 4
1208

            
1209
=item *
1210

            
1211
C<filter> argument of C<insert()>, C<update()>,
1212
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1213
methods
1214

            
1215
=item *
1216

            
1217
C<execute()> method
1218

            
1219
=item *
1220

            
1221
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1222

            
1223
=item *
1224

            
1225
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1226

            
1227
=back
1228

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

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1231
    $dbi->register_tag(
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1232
        limit => sub {
1233
            ...;
1234
        }
1235
    );
1236

            
cleanup
Yuki Kimoto authored on 2011-01-25
1237
Register tag.
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1238

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

            
1241
    $dbi->rollback;
1242

            
1243
Rollback transaction.
1244
This is same as L<DBI>'s C<rollback>.
1245

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1246
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1247
    
cleanup
yuki-kimoto authored on 2010-08-05
1248
    my $result = $dbi->select(table    => $table,
1249
                              column   => [@column],
1250
                              where    => \%where,
1251
                              append   => $append,
1252
                              relation => \%relation,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1253
                              filter   => \%filter,
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
1254
                              query    => 1,
1255
                              selection => $selection);
update document
yuki-kimoto authored on 2009-11-19
1256

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1257
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1258
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1259
C<relation> and C<filter> arguments.
1260
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1261
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1262
C<append> is a string added at the end of the SQL statement.
1263
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1264
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1265
default to 0. This is experimental.
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
1266
C<selection> is string of column name and tables. This is experimental
1267

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1271
First element is a string. it contains tags,
1272
such as "{= title} or {like author}".
1273
Second element is paramters.
1274

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1277
    $dbi->update(table  => $table, 
1278
                 param  => \%params,
1279
                 where  => \%where,
1280
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1281
                 filter => \%filter,
1282
                 query  => 1)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1283

            
cleanup
yuki-kimoto authored on 2010-10-17
1284
Execute update statement.
1285
C<update> method have C<table>, C<param>, C<where>, C<append>
1286
and C<filter> arguments.
1287
C<table> is a table name.
1288
C<param> is column-value pairs. this must be hash reference.
1289
C<where> is where clause. this must be hash reference.
1290
C<append> is a string added at the end of the SQL statement.
1291
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1292
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1293
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1294
This is overwrites C<default_bind_filter>.
1295
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1296

            
cleanup
Yuki Kimoto authored on 2011-01-12
1297
=head2 C<(experimental) table>
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1298

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
1299
    $dbi->table('book')->method(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1300
        insert => sub { ... },
1301
        update => sub { ... }
1302
    );
1303
    
1304
    my $table = $dbi->table('book');
1305

            
1306
Create a L<DBIx::Custom::Table> object,
1307
or get a L<DBIx::Custom::Table> object.
1308

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1311
    $dbi->update_all(table  => $table, 
1312
                     param  => \%params,
1313
                     filter => \%filter,
1314
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1315

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

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

            
1323
    my $where = $dbi->where;
1324

            
1325
Create a new L<DBIx::Custom::Where> object.
1326

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

            
1329
    $dbi          = $dbi->cache_method(\&cache_method);
1330
    $cache_method = $dbi->cache_method
1331

            
1332
Method to set and get caches.
1333

            
cleanup
Yuki Kimoto authored on 2011-01-25
1334
=head1 Tags
1335

            
1336
The following tags is available.
1337

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

            
1340
Table tag
1341

            
1342
    {table TABLE}    ->    TABLE
1343

            
1344
This is used to teach what is applied table to C<execute()>.
1345

            
cleanup
Yuki Kimoto authored on 2011-01-25
1346
=head2 C<?>
1347

            
1348
Placeholder tag.
1349

            
1350
    {? NAME}    ->   ?
1351

            
1352
=head2 C<=>
1353

            
1354
Equal tag.
1355

            
1356
    {= NAME}    ->   NAME = ?
1357

            
1358
=head2 C<E<lt>E<gt>>
1359

            
1360
Not equal tag.
1361

            
1362
    {<> NAME}   ->   NAME <> ?
1363

            
1364
=head2 C<E<lt>>
1365

            
1366
Lower than tag
1367

            
1368
    {< NAME}    ->   NAME < ?
1369

            
1370
=head2 C<E<gt>>
1371

            
1372
Greater than tag
1373

            
1374
    {> NAME}    ->   NAME > ?
1375

            
1376
=head2 C<E<gt>=>
1377

            
1378
Greater than or equal tag
1379

            
1380
    {>= NAME}   ->   NAME >= ?
1381

            
1382
=head2 C<E<lt>=>
1383

            
1384
Lower than or equal tag
1385

            
1386
    {<= NAME}   ->   NAME <= ?
1387

            
1388
=head2 C<like>
1389

            
1390
Like tag
1391

            
1392
    {like NAME}   ->   NAME like ?
1393

            
1394
=head2 C<in>
1395

            
1396
In tag.
1397

            
1398
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
1399

            
1400
=head2 C<insert_param>
1401

            
1402
Insert parameter tag.
1403

            
1404
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
1405

            
1406
=head2 C<update_param>
1407

            
1408
Updata parameter tag.
1409

            
1410
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
1411

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

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

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

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

            
1421
C<< <kimoto.yuki at gmail.com> >>
1422

            
1423
L<http://github.com/yuki-kimoto/DBIx-Custom>
1424

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1425
=head1 AUTHOR
1426

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

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

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

            
1433
This program is free software; you can redistribute it and/or modify it
1434
under the same terms as Perl itself.
1435

            
1436
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1437

            
1438