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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
3
our $VERSION = '0.1646';
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,
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',
update pod
Yuki Kimoto authored on 2011-02-07
27
    safety_column_name => sub { qr/^[\w\.]*$/ },
28
    stash => sub { {} }
many changed
Yuki Kimoto authored on 2011-01-23
29
);
30

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-01-27
267
    # SQL stack
268
    my @sql;
269

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

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

            
added helper method
yuki-kimoto authored on 2010-10-17
290
sub DESTROY { }
291

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

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

            
375
        return $result;
376
    }
377
    return $affected;
378
}
379

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
893
=head1 NAME
894

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

            
897
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
898

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

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

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

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

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

            
952
    # Get DBI object
953
    my $dbh = $dbi->dbh;
954

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

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

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

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

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

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

            
989
=head1 GUIDE
990

            
991
L<DBIx::Custom::Guide> - L<DBIx::Custom> complete guide
992

            
993
=head1 EXAMPLES
994

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1027
DBI options.
1028
C<connect()> method use this value to connect the database.
1029

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1037
=head2 C<password>
1038

            
1039
    my $password = $dbi->password;
1040
    $dbi         = $dbi->password('lkj&le`@s');
1041

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

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

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

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

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

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

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

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

            
1064
    my $safety_column_name = $self->safety_column_name;
1065
    $dbi                   = $self->safety_column_name($name);
1066

            
1067
Safety column name regex.
1068
Default is qr/^[\w\.]*$/
1069

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1072
    my $user = $dbi->user;
1073
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1074

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

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

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

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

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1086
    $dbi->apply_filter(
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1087
        $table,
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1088
        $column1 => {in => $infilter1, out => $outfilter1, end => $endfilter1}
1089
        $column2 => {in => $infilter2, out => $outfilter2, end =. $endfilter2}
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1090
        ...,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1091
    );
1092

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

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

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

            
1106
You can use three name as column name.
1107

            
1108
    1. column        : author
1109
    2. table.column  : book.author
1110
    3. table__column : book__author
1111

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1147
    $dbi->delete(table  => $table,
1148
                 where  => \%where,
1149
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1150
                 filter => \%filter,
1151
                 query  => 1);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1152

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1172
=head2 C<insert>
1173

            
1174
    $dbi->insert(table  => $table, 
1175
                 param  => \%param,
1176
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1177
                 filter => \%filter,
1178
                 query  => 1);
cleanup
yuki-kimoto authored on 2010-10-17
1179

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

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

            
pod fix
Yuki Kimoto authored on 2011-01-21
1194
    $dbi->each_column(
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1195
        sub {
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1196
            my ($self, $table, $column, $info) = @_;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1197
            
pod fix
Yuki Kimoto authored on 2011-01-21
1198
            my $type = $info->{TYPE_NAME};
1199
            
1200
            if ($type eq 'DATE') {
1201
                # ...
1202
            }
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1203
        }
1204
    );
pod fix
Yuki Kimoto authored on 2011-01-21
1205
Get column informations from database.
1206
Argument is callback.
1207
You can do anything in callback.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1208
Callback receive four arguments, dbi object, table name,
1209
column name and columninformation.
1210

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1213
    $dbi->include_model(
1214
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1215
            'book', 'person', 'company'
1216
        ]
1217
    );
1218

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

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

            
1225
    $dbi->include_model('MyModel');
1226

            
1227
Note that in this case name spece module is needed.
1228

            
1229
    # MyModel.pm
1230
    package MyModel;
1231
    
1232
    use base 'DBIx::Custom::Model';
1233

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1236
    MyModel::book
1237
    MyModel::person
1238
    MyModel::company
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1239

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

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

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1247
    $dbi->include_model(
1248
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1249
            {'book' => 'Book'},
1250
            {'person' => 'Person'}
1251
        ]
1252
    );
1253

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

            
1256
    $dbi->method(
1257
        update_or_insert => sub {
1258
            my $self = shift;
1259
            # do something
1260
        },
1261
        find_or_create   => sub {
1262
            my $self = shift;
1263
            # do something
1264
        }
1265
    );
1266

            
1267
Register method. These method is called from L<DBIx::Custom> object directory.
1268

            
1269
    $dbi->update_or_insert;
1270
    $dbi->find_or_create;
1271

            
1272
=head2 C<new>
1273

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

            
1277
Create a new L<DBIx::Custom> object.
1278

            
1279
=head2 C<(experimental) not_exists>
1280

            
1281
    my $not_exists = $dbi->not_exists;
1282

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1285
=head2 C<register_filter>
1286

            
1287
    $dbi->register_filter(%filters);
1288
    $dbi->register_filter(\%filters);
1289
    
1290
Register filter. Registered filters is available in the following attributes
1291
or arguments.
1292

            
1293
=over 4
1294

            
1295
=item *
1296

            
1297
C<filter> argument of C<insert()>, C<update()>,
1298
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1299
methods
1300

            
1301
=item *
1302

            
1303
C<execute()> method
1304

            
1305
=item *
1306

            
1307
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1308

            
1309
=item *
1310

            
1311
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1312

            
1313
=back
1314

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

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1317
    $dbi->register_tag(
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1318
        limit => sub {
1319
            ...;
1320
        }
1321
    );
1322

            
cleanup
Yuki Kimoto authored on 2011-01-25
1323
Register tag.
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1324

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

            
1327
    $dbi->rollback;
1328

            
1329
Rollback transaction.
1330
This is same as L<DBI>'s C<rollback>.
1331

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1332
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1333
    
cleanup
yuki-kimoto authored on 2010-08-05
1334
    my $result = $dbi->select(table    => $table,
1335
                              column   => [@column],
1336
                              where    => \%where,
1337
                              append   => $append,
1338
                              relation => \%relation,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1339
                              filter   => \%filter,
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
1340
                              query    => 1,
1341
                              selection => $selection);
update document
yuki-kimoto authored on 2009-11-19
1342

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1343
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1344
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1345
C<relation> and C<filter> arguments.
1346
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1347
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1348
C<append> is a string added at the end of the SQL statement.
1349
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1350
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1351
default to 0. This is experimental.
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
1352
C<selection> is string of column name and tables. This is experimental
1353

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1357
First element is a string. it contains tags,
1358
such as "{= title} or {like author}".
1359
Second element is paramters.
1360

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1363
    $dbi->update(table  => $table, 
1364
                 param  => \%params,
1365
                 where  => \%where,
1366
                 append => $append,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1367
                 filter => \%filter,
1368
                 query  => 1)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1369

            
cleanup
yuki-kimoto authored on 2010-10-17
1370
Execute update statement.
1371
C<update> method have C<table>, C<param>, C<where>, C<append>
1372
and C<filter> arguments.
1373
C<table> is a table name.
1374
C<param> is column-value pairs. this must be hash reference.
1375
C<where> is where clause. this must be hash reference.
1376
C<append> is a string added at the end of the SQL statement.
1377
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1378
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1379
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1380
This is overwrites C<default_bind_filter>.
1381
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1382

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1385
    $dbi->model('book')->method(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1386
        insert => sub { ... },
1387
        update => sub { ... }
1388
    );
1389
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1390
    my $model = $dbi->model('book');
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1391

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1396
    $dbi->update_all(table  => $table, 
1397
                     param  => \%params,
1398
                     filter => \%filter,
1399
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1400

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

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

            
1408
    my $where = $dbi->where;
1409

            
1410
Create a new L<DBIx::Custom::Where> object.
1411

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

            
1414
    $dbi          = $dbi->cache_method(\&cache_method);
1415
    $cache_method = $dbi->cache_method
1416

            
1417
Method to set and get caches.
1418

            
cleanup
Yuki Kimoto authored on 2011-01-25
1419
=head1 Tags
1420

            
1421
The following tags is available.
1422

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

            
1425
Table tag
1426

            
1427
    {table TABLE}    ->    TABLE
1428

            
1429
This is used to teach what is applied table to C<execute()>.
1430

            
cleanup
Yuki Kimoto authored on 2011-01-25
1431
=head2 C<?>
1432

            
1433
Placeholder tag.
1434

            
1435
    {? NAME}    ->   ?
1436

            
1437
=head2 C<=>
1438

            
1439
Equal tag.
1440

            
1441
    {= NAME}    ->   NAME = ?
1442

            
1443
=head2 C<E<lt>E<gt>>
1444

            
1445
Not equal tag.
1446

            
1447
    {<> NAME}   ->   NAME <> ?
1448

            
1449
=head2 C<E<lt>>
1450

            
1451
Lower than tag
1452

            
1453
    {< NAME}    ->   NAME < ?
1454

            
1455
=head2 C<E<gt>>
1456

            
1457
Greater than tag
1458

            
1459
    {> NAME}    ->   NAME > ?
1460

            
1461
=head2 C<E<gt>=>
1462

            
1463
Greater than or equal tag
1464

            
1465
    {>= NAME}   ->   NAME >= ?
1466

            
1467
=head2 C<E<lt>=>
1468

            
1469
Lower than or equal tag
1470

            
1471
    {<= NAME}   ->   NAME <= ?
1472

            
1473
=head2 C<like>
1474

            
1475
Like tag
1476

            
1477
    {like NAME}   ->   NAME like ?
1478

            
1479
=head2 C<in>
1480

            
1481
In tag.
1482

            
1483
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
1484

            
1485
=head2 C<insert_param>
1486

            
1487
Insert parameter tag.
1488

            
1489
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
1490

            
1491
=head2 C<update_param>
1492

            
1493
Updata parameter tag.
1494

            
1495
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
1496

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

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

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

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

            
1506
C<< <kimoto.yuki at gmail.com> >>
1507

            
1508
L<http://github.com/yuki-kimoto/DBIx-Custom>
1509

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1510
=head1 AUTHOR
1511

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

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

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

            
1518
This program is free software; you can redistribute it and/or modify it
1519
under the same terms as Perl itself.
1520

            
1521
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1522

            
1523