DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1411 lines | 36.126kb
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
186
        $query = DBIx::Custom::Query->new($q) if $q;
187
    }
188
    
189
    unless ($query) {
cleanup insert
yuki-kimoto authored on 2010-04-28
190

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-01-27
260
    # SQL stack
261
    my @sql;
262

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

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

            
added helper method
yuki-kimoto authored on 2010-10-17
283
sub DESTROY { }
284

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

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

            
366
        return $result;
367
    }
368
    return $affected;
369
}
370

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

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

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

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

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

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

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

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

            
refactoring select
yuki-kimoto authored on 2010-04-28
483
our %VALID_SELECT_ARGS
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
484
  = map { $_ => 1 } qw/table column where append relation filter query/;
refactoring select
yuki-kimoto authored on 2010-04-28
485

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

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
576
sub table {
577
    my $self = shift;
578
    my $name = shift;
579
    
580
    # Create table
581
    $self->{_tables} ||= {};
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
582
    unless (defined $self->{_tables}->{$name}) {
583
        # Base table
584
        my $base_table = $self->base_table;
585
        
586
        # Base methods
587
        my $bmethods = ref $base_table->{_methods} eq 'HASH'
588
                     ? $base_table->{_methods}
589
                     : {};
590
        
591
        # Copy Methods
592
        my $methods = {};
593
        $methods->{$_} = $bmethods->{$_} for keys %$bmethods;
removed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-28
594
        $methods->{"base_$_"} = $bmethods->{$_} for keys %$bmethods;
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
595
        
596
        # Create table
597
        my $table = $base_table->new(
598
            dbi      => $self,
599
            name     => $name,
600
        );
removed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-28
601
        $table->method($methods);
602
        
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
603
        $self->{_tables}->{$name} = $table;
604
    }
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
605
    
606
    return $self->{_tables}{$name};
607
}
608

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

            
613
sub update {
614
    my ($self, %args) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
615
    
cleanup
yuki-kimoto authored on 2010-10-17
616
    # Check arguments
617
    foreach my $name (keys %args) {
618
        croak qq{"$name" is invalid argument}
619
          unless $VALID_UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
620
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
621
    
cleanup
yuki-kimoto authored on 2010-10-17
622
    # Arguments
623
    my $table            = $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
624
    croak qq{"table" option must be specified} unless $table;
cleanup
yuki-kimoto authored on 2010-10-17
625
    my $param            = $args{param} || {};
626
    my $where            = $args{where} || {};
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
627
    my $append           = $args{append} || '';
cleanup
yuki-kimoto authored on 2010-10-17
628
    my $filter           = $args{filter};
629
    my $allow_update_all = $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
630
    
cleanup
yuki-kimoto authored on 2010-10-17
631
    # Update keys
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
632
    my @clumns = keys %$param;
633

            
634
    # Columns
635
    my @columns;
636
    my $safety = $self->safety_column_name;
637
    foreach my $column (keys %$param) {
638
        croak qq{"$column" is not safety column name}
639
          unless $column =~ /$safety/;
640
        push @columns, $column;
641
    }
642
        
cleanup
yuki-kimoto authored on 2010-10-17
643
    # Update clause
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
644
    my $update_clause = '{update_param ' . join(' ', @clumns) . '}';
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
645

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

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

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

            
710
    return DBIx::Custom::Where->new(
711
        query_builder => $self->query_builder,
712
        safety_column_name => $self->safety_column_name
713
    );
cleanup
Yuki Kimoto authored on 2011-01-25
714
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
715

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

            
cleanup
yuki-kimoto authored on 2010-10-17
759
sub _croak {
760
    my ($self, $error, $append) = @_;
761
    $append ||= "";
762
    
763
    # Verbose
764
    if ($Carp::Verbose) { croak $error }
765
    
766
    # Not verbose
767
    else {
768
        
769
        # Remove line and module infromation
770
        my $at_pos = rindex($error, ' at ');
771
        $error = substr($error, 0, $at_pos);
772
        $error =~ s/\s+$//;
773
        
774
        croak "$error$append";
775
    }
776
}
777

            
cleanup
Yuki Kimoto authored on 2011-01-25
778
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-23
779
__PACKAGE__->attr(
780
    dbi_options => sub { {} },
781
    filter_check  => 1
782
);
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
783

            
cleanup
Yuki Kimoto authored on 2011-01-25
784
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
785
sub default_bind_filter {
786
    my $self = shift;
787
    
788
    if (@_) {
789
        my $fname = $_[0];
790
        
791
        if (@_ && !$fname) {
792
            $self->{default_out_filter} = undef;
793
        }
794
        else {
many changed
Yuki Kimoto authored on 2011-01-23
795
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
796
              unless exists $self->filters->{$fname};
797
        
798
            $self->{default_out_filter} = $self->filters->{$fname};
799
        }
800
        return $self;
801
    }
802
    
803
    return $self->{default_out_filter};
804
}
805

            
cleanup
Yuki Kimoto authored on 2011-01-25
806
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
807
sub default_fetch_filter {
808
    my $self = shift;
809
    
810
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
811
        my $fname = $_[0];
812

            
cleanup
Yuki Kimoto authored on 2011-01-12
813
        if (@_ && !$fname) {
814
            $self->{default_in_filter} = undef;
815
        }
816
        else {
many changed
Yuki Kimoto authored on 2011-01-23
817
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
818
              unless exists $self->filters->{$fname};
819
        
820
            $self->{default_in_filter} = $self->filters->{$fname};
821
        }
822
        
823
        return $self;
824
    }
825
    
many changed
Yuki Kimoto authored on 2011-01-23
826
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
827
}
828

            
cleanup
Yuki Kimoto authored on 2011-01-25
829
# DEPRECATED!
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
830
sub register_tag_processor {
831
    return shift->query_builder->register_tag_processor(@_);
832
}
833

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
836
=head1 NAME
837

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

            
840
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
841

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
847
    # Insert 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
848
    $dbi->insert(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
849
                 param  => {title => 'Perl', author => 'Ken'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
850
                 filter => {title => 'to_something'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
851
    
852
    # Update 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
853
    $dbi->update(table  => 'book', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
854
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
855
                 where  => {id => 5},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
856
                 filter => {title => 'to_something'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
857
    
858
    # Update all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
859
    $dbi->update_all(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
860
                     param  => {title => 'Perl'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
861
                     filter => {title => 'to_something'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
862
    
863
    # Delete
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
864
    $dbi->delete(table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
865
                 where  => {author => 'Ken'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
866
                 filter => {title => 'to_something'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
867
    
868
    # Delete all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
869
    $dbi->delete_all(table => 'book');
cleanup
yuki-kimoto authored on 2010-08-05
870

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
871
    # Select
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
872
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
873
        table  => 'book',
update document
yuki-kimoto authored on 2010-05-27
874
        column => [qw/author title/],
875
        where  => {author => 'Ken'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
876
        relation => {'book.id' => 'rental.book_id'},
updated document
yuki-kimoto authored on 2010-08-08
877
        append => 'order by id limit 5',
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
878
        filter => {title => 'to_something'}
added commit method
yuki-kimoto authored on 2010-05-27
879
    );
cleanup
yuki-kimoto authored on 2010-08-05
880

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

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

            
895
    # Get DBI object
896
    my $dbh = $dbi->dbh;
897

            
removed register_format()
yuki-kimoto authored on 2010-05-26
898
    # Fetch
899
    while (my $row = $result->fetch) {
900
        # ...
901
    }
902
    
903
    # Fetch hash
904
    while (my $row = $result->fetch_hash) {
905
        
906
    }
907
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
908
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
909

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

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

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

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

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

            
932
=head1 GUIDE
933

            
934
L<DBIx::Custom::Guide> - L<DBIx::Custom> complete guide
935

            
936
=head1 EXAMPLES
937

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

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

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

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

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

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

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

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

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

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

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

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

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

            
970
DBI options.
971
C<connect()> method use this value to connect the database.
972

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
980
=head2 C<password>
981

            
982
    my $password = $dbi->password;
983
    $dbi         = $dbi->password('lkj&le`@s');
984

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

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

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

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

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

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

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

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

            
1007
    my $safety_column_name = $self->safety_column_name;
1008
    $dbi                   = $self->safety_column_name($name);
1009

            
1010
Safety column name regex.
1011
Default is qr/^[\w\.]*$/
1012

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1015
    my $user = $dbi->user;
1016
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1017

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

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

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

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

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1029
    $dbi->apply_filter(
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1030
        $table,
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1031
        $column1 => {in => $infilter1, out => $outfilter1, end => $endfilter1}
1032
        $column2 => {in => $infilter2, out => $outfilter2, end =. $endfilter2}
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1033
        ...,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1034
    );
1035

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

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

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

            
1049
You can use three name as column name.
1050

            
1051
    1. column        : author
1052
    2. table.column  : book.author
1053
    3. table__column : book__author
1054

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1090
    $dbi->delete(table  => $table,
1091
                 where  => \%where,
1092
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1093
                 filter => \%filter,
1094
                 query  => 1);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1095

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1115
=head2 C<insert>
1116

            
1117
    $dbi->insert(table  => $table, 
1118
                 param  => \%param,
1119
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1120
                 filter => \%filter,
1121
                 query  => 1);
cleanup
yuki-kimoto authored on 2010-10-17
1122

            
1123
Execute insert statement.
1124
C<insert> method have C<table>, C<param>, C<append>
1125
and C<filter> arguments.
1126
C<table> is a table name.
1127
C<param> is the pairs of column name value. this must be hash reference.
1128
C<append> is a string added at the end of the SQL statement.
1129
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1130
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1131
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1132
This is overwrites C<default_bind_filter>.
1133
Return value of C<insert()> is the count of affected rows.
1134

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

            
pod fix
Yuki Kimoto authored on 2011-01-21
1137
    $dbi->each_column(
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1138
        sub {
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1139
            my ($self, $table, $column, $info) = @_;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1140
            
pod fix
Yuki Kimoto authored on 2011-01-21
1141
            my $type = $info->{TYPE_NAME};
1142
            
1143
            if ($type eq 'DATE') {
1144
                # ...
1145
            }
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1146
        }
1147
    );
pod fix
Yuki Kimoto authored on 2011-01-21
1148
Get column informations from database.
1149
Argument is callback.
1150
You can do anything in callback.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1151
Callback receive four arguments, dbi object, table name,
1152
column name and columninformation.
1153

            
1154
=head2 C<(experimental) method>
1155

            
1156
    $dbi->method(
1157
        update_or_insert => sub {
1158
            my $self = shift;
1159
            # do something
1160
        },
1161
        find_or_create   => sub {
1162
            my $self = shift;
1163
            # do something
1164
        }
1165
    );
1166

            
1167
Register method. These method is called from L<DBIx::Custom> object directory.
1168

            
1169
    $dbi->update_or_insert;
1170
    $dbi->find_or_create;
1171

            
1172
=head2 C<new>
1173

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

            
1177
Create a new L<DBIx::Custom> object.
1178

            
1179
=head2 C<(experimental) not_exists>
1180

            
1181
    my $not_exists = $dbi->not_exists;
1182

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1185
=head2 C<register_filter>
1186

            
1187
    $dbi->register_filter(%filters);
1188
    $dbi->register_filter(\%filters);
1189
    
1190
Register filter. Registered filters is available in the following attributes
1191
or arguments.
1192

            
1193
=over 4
1194

            
1195
=item *
1196

            
1197
C<filter> argument of C<insert()>, C<update()>,
1198
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1199
methods
1200

            
1201
=item *
1202

            
1203
C<execute()> method
1204

            
1205
=item *
1206

            
1207
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1208

            
1209
=item *
1210

            
1211
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1212

            
1213
=back
1214

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

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1217
    $dbi->register_tag(
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1218
        limit => sub {
1219
            ...;
1220
        }
1221
    );
1222

            
cleanup
Yuki Kimoto authored on 2011-01-25
1223
Register tag.
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1224

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

            
1227
    $dbi->rollback;
1228

            
1229
Rollback transaction.
1230
This is same as L<DBI>'s C<rollback>.
1231

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1232
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1233
    
cleanup
yuki-kimoto authored on 2010-08-05
1234
    my $result = $dbi->select(table    => $table,
1235
                              column   => [@column],
1236
                              where    => \%where,
1237
                              append   => $append,
1238
                              relation => \%relation,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1239
                              filter   => \%filter,
1240
                              query    => 1);
update document
yuki-kimoto authored on 2009-11-19
1241

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1242
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1243
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1244
C<relation> and C<filter> arguments.
1245
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1246
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1247
C<append> is a string added at the end of the SQL statement.
1248
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1249
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1250
default to 0. This is experimental.
update document
yuki-kimoto authored on 2009-11-19
1251

            
cleanup
yuki-kimoto authored on 2010-08-09
1252
First element is a string. it contains tags,
1253
such as "{= title} or {like author}".
1254
Second element is paramters.
1255

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1258
    $dbi->update(table  => $table, 
1259
                 param  => \%params,
1260
                 where  => \%where,
1261
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1262
                 filter => \%filter,
1263
                 query  => 1)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1264

            
cleanup
yuki-kimoto authored on 2010-10-17
1265
Execute update statement.
1266
C<update> method have C<table>, C<param>, C<where>, C<append>
1267
and C<filter> arguments.
1268
C<table> is a table name.
1269
C<param> is column-value pairs. this must be hash reference.
1270
C<where> is where clause. this must be hash reference.
1271
C<append> is a string added at the end of the SQL statement.
1272
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1273
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1274
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1275
This is overwrites C<default_bind_filter>.
1276
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1277

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

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
1280
    $dbi->table('book')->method(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1281
        insert => sub { ... },
1282
        update => sub { ... }
1283
    );
1284
    
1285
    my $table = $dbi->table('book');
1286

            
1287
Create a L<DBIx::Custom::Table> object,
1288
or get a L<DBIx::Custom::Table> object.
1289

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1292
    $dbi->update_all(table  => $table, 
1293
                     param  => \%params,
1294
                     filter => \%filter,
1295
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1296

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

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

            
1304
    my $where = $dbi->where;
1305

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

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

            
1310
    $dbi          = $dbi->cache_method(\&cache_method);
1311
    $cache_method = $dbi->cache_method
1312

            
1313
Method to set and get caches.
1314

            
cleanup
Yuki Kimoto authored on 2011-01-25
1315
=head1 Tags
1316

            
1317
The following tags is available.
1318

            
1319
=head2 C<?>
1320

            
1321
Placeholder tag.
1322

            
1323
    {? NAME}    ->   ?
1324

            
1325
=head2 C<=>
1326

            
1327
Equal tag.
1328

            
1329
    {= NAME}    ->   NAME = ?
1330

            
1331
=head2 C<E<lt>E<gt>>
1332

            
1333
Not equal tag.
1334

            
1335
    {<> NAME}   ->   NAME <> ?
1336

            
1337
=head2 C<E<lt>>
1338

            
1339
Lower than tag
1340

            
1341
    {< NAME}    ->   NAME < ?
1342

            
1343
=head2 C<E<gt>>
1344

            
1345
Greater than tag
1346

            
1347
    {> NAME}    ->   NAME > ?
1348

            
1349
=head2 C<E<gt>=>
1350

            
1351
Greater than or equal tag
1352

            
1353
    {>= NAME}   ->   NAME >= ?
1354

            
1355
=head2 C<E<lt>=>
1356

            
1357
Lower than or equal tag
1358

            
1359
    {<= NAME}   ->   NAME <= ?
1360

            
1361
=head2 C<like>
1362

            
1363
Like tag
1364

            
1365
    {like NAME}   ->   NAME like ?
1366

            
1367
=head2 C<in>
1368

            
1369
In tag.
1370

            
1371
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
1372

            
1373
=head2 C<insert_param>
1374

            
1375
Insert parameter tag.
1376

            
1377
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
1378

            
1379
=head2 C<update_param>
1380

            
1381
Updata parameter tag.
1382

            
1383
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
1384

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

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

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

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

            
1394
C<< <kimoto.yuki at gmail.com> >>
1395

            
1396
L<http://github.com/yuki-kimoto/DBIx-Custom>
1397

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1398
=head1 AUTHOR
1399

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

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

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

            
1406
This program is free software; you can redistribute it and/or modify it
1407
under the same terms as Perl itself.
1408

            
1409
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1410

            
1411