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

            
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
3
our $VERSION = '0.1647';
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
4

            
5
use 5.008001;
cleanup
yuki-kimoto authored on 2009-12-22
6
use strict;
7
use warnings;
8

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
9
use base 'Object::Simple';
many change
yuki-kimoto authored on 2010-02-11
10

            
packaging one directory
yuki-kimoto authored on 2009-11-16
11
use Carp 'croak';
12
use DBI;
13
use DBIx::Custom::Result;
cleanup
yuki-kimoto authored on 2010-02-11
14
use DBIx::Custom::Query;
cleanup
yuki-kimoto authored on 2010-08-05
15
use DBIx::Custom::QueryBuilder;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
16
use DBIx::Custom::Where;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
17
use DBIx::Custom::Model;
cleanup
Yuki Kimoto authored on 2011-01-25
18
use DBIx::Custom::Tag;
update document
yuki-kimoto authored on 2010-05-27
19
use Encode qw/encode_utf8 decode_utf8/;
packaging one directory
yuki-kimoto authored on 2009-11-16
20

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
992
=head1 GUIDE
993

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

            
996
=head1 EXAMPLES
997

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1036

            
1037
=head2 C<default_dbi_option>
1038

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

            
1042
DBI default options.
1043

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1057
=head2 C<password>
1058

            
1059
    my $password = $dbi->password;
1060
    $dbi         = $dbi->password('lkj&le`@s');
1061

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

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

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

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

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

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

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

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

            
1084
    my $safety_column_name = $self->safety_column_name;
1085
    $dbi                   = $self->safety_column_name($name);
1086

            
1087
Safety column name regex.
1088
Default is qr/^[\w\.]*$/
1089

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1092
    my $user = $dbi->user;
1093
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1094

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

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

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

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

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1106
    $dbi->apply_filter(
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1107
        $table,
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1108
        $column1 => {in => $infilter1, out => $outfilter1, end => $endfilter1}
1109
        $column2 => {in => $infilter2, out => $outfilter2, end =. $endfilter2}
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1110
        ...,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1111
    );
1112

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

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

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

            
1126
You can use three name as column name.
1127

            
1128
    1. column        : author
1129
    2. table.column  : book.author
1130
    3. table__column : book__author
1131

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1167
    $dbi->delete(table  => $table,
1168
                 where  => \%where,
1169
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1170
                 filter => \%filter,
1171
                 query  => 1);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1172

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1192
=head2 C<insert>
1193

            
1194
    $dbi->insert(table  => $table, 
1195
                 param  => \%param,
1196
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1197
                 filter => \%filter,
1198
                 query  => 1);
cleanup
yuki-kimoto authored on 2010-10-17
1199

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

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

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

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1233
    $dbi->include_model(
1234
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1235
            'book', 'person', 'company'
1236
        ]
1237
    );
1238

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

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

            
1245
    $dbi->include_model('MyModel');
1246

            
1247
Note that in this case name spece module is needed.
1248

            
1249
    # MyModel.pm
1250
    package MyModel;
1251
    
1252
    use base 'DBIx::Custom::Model';
1253

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1256
    MyModel::book
1257
    MyModel::person
1258
    MyModel::company
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1259

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

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

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1267
    $dbi->include_model(
1268
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1269
            {'book' => 'Book'},
1270
            {'person' => 'Person'}
1271
        ]
1272
    );
1273

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

            
1276
    $dbi->method(
1277
        update_or_insert => sub {
1278
            my $self = shift;
1279
            # do something
1280
        },
1281
        find_or_create   => sub {
1282
            my $self = shift;
1283
            # do something
1284
        }
1285
    );
1286

            
1287
Register method. These method is called from L<DBIx::Custom> object directory.
1288

            
1289
    $dbi->update_or_insert;
1290
    $dbi->find_or_create;
1291

            
1292
=head2 C<new>
1293

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

            
1297
Create a new L<DBIx::Custom> object.
1298

            
1299
=head2 C<(experimental) not_exists>
1300

            
1301
    my $not_exists = $dbi->not_exists;
1302

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1305
=head2 C<register_filter>
1306

            
1307
    $dbi->register_filter(%filters);
1308
    $dbi->register_filter(\%filters);
1309
    
1310
Register filter. Registered filters is available in the following attributes
1311
or arguments.
1312

            
1313
=over 4
1314

            
1315
=item *
1316

            
1317
C<filter> argument of C<insert()>, C<update()>,
1318
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1319
methods
1320

            
1321
=item *
1322

            
1323
C<execute()> method
1324

            
1325
=item *
1326

            
1327
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1328

            
1329
=item *
1330

            
1331
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1332

            
1333
=back
1334

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

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1337
    $dbi->register_tag(
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1338
        limit => sub {
1339
            ...;
1340
        }
1341
    );
1342

            
cleanup
Yuki Kimoto authored on 2011-01-25
1343
Register tag.
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1344

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

            
1347
    $dbi->rollback;
1348

            
1349
Rollback transaction.
1350
This is same as L<DBI>'s C<rollback>.
1351

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1352
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1353
    
cleanup
yuki-kimoto authored on 2010-08-05
1354
    my $result = $dbi->select(table    => $table,
1355
                              column   => [@column],
1356
                              where    => \%where,
1357
                              append   => $append,
1358
                              relation => \%relation,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1359
                              filter   => \%filter,
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
1360
                              query    => 1,
1361
                              selection => $selection);
update document
yuki-kimoto authored on 2009-11-19
1362

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

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1377
First element is a string. it contains tags,
1378
such as "{= title} or {like author}".
1379
Second element is paramters.
1380

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1383
    $dbi->update(table  => $table, 
1384
                 param  => \%params,
1385
                 where  => \%where,
1386
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1387
                 filter => \%filter,
1388
                 query  => 1)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1389

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

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1405
    $dbi->model('book')->method(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1406
        insert => sub { ... },
1407
        update => sub { ... }
1408
    );
1409
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1410
    my $model = $dbi->model('book');
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1411

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1416
    $dbi->update_all(table  => $table, 
1417
                     param  => \%params,
1418
                     filter => \%filter,
1419
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1420

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

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

            
1428
    my $where = $dbi->where;
1429

            
1430
Create a new L<DBIx::Custom::Where> object.
1431

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

            
1434
    $dbi          = $dbi->cache_method(\&cache_method);
1435
    $cache_method = $dbi->cache_method
1436

            
1437
Method to set and get caches.
1438

            
cleanup
Yuki Kimoto authored on 2011-01-25
1439
=head1 Tags
1440

            
1441
The following tags is available.
1442

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

            
1445
Table tag
1446

            
1447
    {table TABLE}    ->    TABLE
1448

            
1449
This is used to teach what is applied table to C<execute()>.
1450

            
cleanup
Yuki Kimoto authored on 2011-01-25
1451
=head2 C<?>
1452

            
1453
Placeholder tag.
1454

            
1455
    {? NAME}    ->   ?
1456

            
1457
=head2 C<=>
1458

            
1459
Equal tag.
1460

            
1461
    {= NAME}    ->   NAME = ?
1462

            
1463
=head2 C<E<lt>E<gt>>
1464

            
1465
Not equal tag.
1466

            
1467
    {<> NAME}   ->   NAME <> ?
1468

            
1469
=head2 C<E<lt>>
1470

            
1471
Lower than tag
1472

            
1473
    {< NAME}    ->   NAME < ?
1474

            
1475
=head2 C<E<gt>>
1476

            
1477
Greater than tag
1478

            
1479
    {> NAME}    ->   NAME > ?
1480

            
1481
=head2 C<E<gt>=>
1482

            
1483
Greater than or equal tag
1484

            
1485
    {>= NAME}   ->   NAME >= ?
1486

            
1487
=head2 C<E<lt>=>
1488

            
1489
Lower than or equal tag
1490

            
1491
    {<= NAME}   ->   NAME <= ?
1492

            
1493
=head2 C<like>
1494

            
1495
Like tag
1496

            
1497
    {like NAME}   ->   NAME like ?
1498

            
1499
=head2 C<in>
1500

            
1501
In tag.
1502

            
1503
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
1504

            
1505
=head2 C<insert_param>
1506

            
1507
Insert parameter tag.
1508

            
1509
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
1510

            
1511
=head2 C<update_param>
1512

            
1513
Updata parameter tag.
1514

            
1515
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
1516

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

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

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

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

            
1526
C<< <kimoto.yuki at gmail.com> >>
1527

            
1528
L<http://github.com/yuki-kimoto/DBIx-Custom>
1529

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1530
=head1 AUTHOR
1531

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

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

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

            
1538
This program is free software; you can redistribute it and/or modify it
1539
under the same terms as Perl itself.
1540

            
1541
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1542

            
1543