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

            
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
3
our $VERSION = '0.1678';
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;
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
19
use DBIx::Custom::Util;
update document
yuki-kimoto authored on 2010-05-27
20
use Encode qw/encode_utf8 decode_utf8/;
packaging one directory
yuki-kimoto authored on 2009-11-16
21

            
added environment variable D...
Yuki Kimoto authored on 2011-04-02
22
use constant DEBUG => $ENV{DBIX_CUSTOM_DEBUG} || 0;
23

            
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
24
our @COMMON_ARGS = qw/table query filter type/;
cleanup
Yuki Kimoto authored on 2011-03-21
25

            
fix tests
Yuki Kimoto authored on 2011-01-13
26
__PACKAGE__->attr(
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
27
    [qw/connector data_source password pid user/],
removed from cache() and cac...
Yuki Kimoto authored on 2011-03-29
28
    cache => 0,
many changed
Yuki Kimoto authored on 2011-01-23
29
    cache_method => sub {
30
        sub {
31
            my $self = shift;
32
            
33
            $self->{_cached} ||= {};
34
            
35
            if (@_ > 1) {
update pod
Yuki Kimoto authored on 2011-03-13
36
                $self->{_cached}{$_[0]} = $_[1];
many changed
Yuki Kimoto authored on 2011-01-23
37
            }
38
            else {
update pod
Yuki Kimoto authored on 2011-03-13
39
                return $self->{_cached}{$_[0]};
many changed
Yuki Kimoto authored on 2011-01-23
40
            }
41
        }
update pod
Yuki Kimoto authored on 2011-03-13
42
    },
43
    dbi_option => sub { {} },
44
    default_dbi_option => sub {
45
        {
46
            RaiseError => 1,
47
            PrintError => 0,
48
            AutoCommit => 1
49
        }
50
    },
fix tests
Yuki Kimoto authored on 2011-01-13
51
    filters => sub {
52
        {
53
            encode_utf8 => sub { encode_utf8($_[0]) },
54
            decode_utf8 => sub { decode_utf8($_[0]) }
55
        }
update pod
Yuki Kimoto authored on 2011-03-13
56
    },
57
    models => sub { {} },
58
    query_builder => sub { DBIx::Custom::QueryBuilder->new },
59
    result_class  => 'DBIx::Custom::Result',
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
60
    reserved_word_quote => '',
update pod
Yuki Kimoto authored on 2011-03-13
61
    safety_character => '\w',
62
    stash => sub { {} }
fix tests
Yuki Kimoto authored on 2011-01-13
63
);
cleanup
yuki-kimoto authored on 2010-10-17
64

            
added helper method
yuki-kimoto authored on 2010-10-17
65
our $AUTOLOAD;
66
sub AUTOLOAD {
67
    my $self = shift;
68

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

            
cleanup
Yuki Kimoto authored on 2011-04-02
72
    # Call method
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
73
    $self->{_methods} ||= {};
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
74
    if (my $method = $self->{_methods}->{$mname}) {
75
        return $self->$method(@_)
76
    }
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
77
    elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
78
        $self->dbh->$dbh_method(@_);
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
79
    }
80
    else {
81
        croak qq/Can't locate object method "$mname" via "$package"/
82
    }
added helper method
yuki-kimoto authored on 2010-10-17
83
}
84

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

            
88
    # Initialize filters
cleanup
Yuki Kimoto authored on 2011-01-12
89
    $self->{filter} ||= {};
many changed
Yuki Kimoto authored on 2011-01-23
90
    $self->{filter}{out} ||= {};
91
    $self->{filter}{in} ||= {};
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
92
    $self->{filter}{end} ||= {};
cleanup
Yuki Kimoto authored on 2010-12-22
93
    
cleanup
Yuki Kimoto authored on 2011-04-02
94
    # Usage
many changed
Yuki Kimoto authored on 2011-01-23
95
    my $usage = "Usage: \$dbi->apply_filter(" .
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
96
                "TABLE, COLUMN1, {in => INFILTER1, out => OUTFILTER1, end => ENDFILTER1}, " .
97
                "COLUMN2, {in => INFILTER2, out => OUTFILTER2, end => ENDFILTER2}, ...)";
cleanup
Yuki Kimoto authored on 2011-04-02
98
    
99
    # Apply filter
many changed
Yuki Kimoto authored on 2011-01-23
100
    for (my $i = 0; $i < @cinfos; $i += 2) {
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
101
        
many changed
Yuki Kimoto authored on 2011-01-23
102
        # Column
103
        my $column = $cinfos[$i];
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
104
        if (ref $column eq 'ARRAY') {
105
            foreach my $c (@$column) {
106
                push @cinfos, $c, $cinfos[$i + 1];
107
            }
108
            next;
109
        }
110
        
cleanup
Yuki Kimoto authored on 2011-04-02
111
        # Filter infomation
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
112
        my $finfo = $cinfos[$i + 1] || {};
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
113
        croak "$usage (table: $table)" unless  ref $finfo eq 'HASH';
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
114
        foreach my $ftype (keys %$finfo) {
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
115
            croak "$usage (table: $table 2)" unless $ftype eq 'in' || $ftype eq 'out'
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
116
                             || $ftype eq 'end'; 
many changed
Yuki Kimoto authored on 2011-01-23
117
        }
118
        
cleanup
Yuki Kimoto authored on 2011-04-02
119
        # Set filters
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
120
        foreach my $way (qw/in out end/) {
cleanup
Yuki Kimoto authored on 2011-04-02
121
        
122
            # Filter
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
123
            my $filter = $finfo->{$way};
cleanup
Yuki Kimoto authored on 2010-12-22
124
            
cleanup
Yuki Kimoto authored on 2011-04-02
125
            # Filter state
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
126
            my $state = !exists $finfo->{$way} ? 'not_exists'
127
                      : !defined $filter        ? 'not_defined'
128
                      : ref $filter eq 'CODE'   ? 'code'
129
                      : 'name';
130
            
cleanup
Yuki Kimoto authored on 2011-04-02
131
            # Filter is not exists
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
132
            next if $state eq 'not_exists';
133
            
cleanup
Yuki Kimoto authored on 2011-04-02
134
            # Check filter name
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
135
            croak qq{Filter "$filter" is not registered}
136
              if  $state eq 'name'
137
               && ! exists $self->filters->{$filter};
138
            
cleanup
Yuki Kimoto authored on 2011-04-02
139
            # Set filter
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
140
            my $f = $state eq 'not_defined' ? undef
141
                  : $state eq 'code'        ? $filter
142
                  : $self->filters->{$filter};
143
            $self->{filter}{$way}{$table}{$column} = $f;
144
            $self->{filter}{$way}{$table}{"$table.$column"} = $f;
145
            $self->{filter}{$way}{$table}{"${table}__$column"} = $f;
many changed
Yuki Kimoto authored on 2011-01-23
146
        }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
147
    }
148
    
many changed
Yuki Kimoto authored on 2011-01-23
149
    return $self;
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
150
}
151

            
cleanup
Yuki Kimoto authored on 2011-03-21
152
sub column {
153
    my ($self, $table, $columns) = @_;
added helper method
yuki-kimoto authored on 2010-10-17
154
    
cleanup
Yuki Kimoto authored on 2011-04-02
155
    # Reserved word quote
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
156
    my $q = $self->reserved_word_quote;
157
    
cleanup
Yuki Kimoto authored on 2011-04-02
158
    # Column clause
cleanup
Yuki Kimoto authored on 2011-03-21
159
    my @column;
cleanup
Yuki Kimoto authored on 2011-04-02
160
    $columns ||= [];
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
161
    push @column, "$q$table$q.$q$_$q as $q${table}${q}__$q$_$q" for @$columns;
cleanup
Yuki Kimoto authored on 2011-03-21
162
    
163
    return join (', ', @column);
added helper method
yuki-kimoto authored on 2010-10-17
164
}
165

            
packaging one directory
yuki-kimoto authored on 2009-11-16
166
sub connect {
cleanup
Yuki Kimoto authored on 2011-01-25
167
    my $self = ref $_[0] ? shift : shift->new(@_);;
removed register_format()
yuki-kimoto authored on 2010-05-26
168
    
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
169
    # Connect
170
    $self->dbh;
update document
yuki-kimoto authored on 2010-01-30
171
    
cleanup
Yuki Kimoto authored on 2011-04-02
172
    # Set process ID
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
173
    $self->pid($$);
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 2011-04-02
184
    # Query
cleanup
yuki-kimoto authored on 2010-10-17
185
    my $query;
cleanup
Yuki Kimoto authored on 2011-04-02
186
    
187
    # Get cached query
cleanup
yuki-kimoto authored on 2010-10-17
188
    if ($cache) {
189
        
190
        # Get query
191
        my $q = $self->cache_method->($self, $source);
192
        
193
        # Create query
add table tag
Yuki Kimoto authored on 2011-02-09
194
        if ($q) {
195
            $query = DBIx::Custom::Query->new($q);
196
            $query->filters($self->filters);
197
        }
cleanup
yuki-kimoto authored on 2010-10-17
198
    }
199
    
cleanup
Yuki Kimoto authored on 2011-04-02
200
    # Create query
cleanup
yuki-kimoto authored on 2010-10-17
201
    unless ($query) {
cleanup insert
yuki-kimoto authored on 2010-04-28
202

            
cleanup
yuki-kimoto authored on 2010-10-17
203
        # Create query
cleanup
Yuki Kimoto authored on 2011-04-02
204
        my $builder = $self->query_builder;
cleanup
yuki-kimoto authored on 2010-10-17
205
        $query = $builder->build_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
206

            
cleanup
Yuki Kimoto authored on 2011-04-02
207
        # Remove reserved word quote
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
208
        if (my $q = $self->reserved_word_quote) {
cleanup
Yuki Kimoto authored on 2011-04-02
209
            $_ =~ s/$q//g for @{$query->columns}
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
210
        }
211

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

            
update pod
Yuki Kimoto authored on 2011-03-13
237
sub dbh {
238
    my $self = shift;
cleanup
Yuki Kimoto authored on 2011-04-02
239
    
fixed dbh() method bug:wq
Yuki Kimoto authored on 2011-04-05
240
    # Set
241
    if (@_) {
242
        $self->{dbh} = $_[0];
243
        
244
        return $self;
245
    }
246
    
247
    # Get
248
    else {
249
        # From Connction manager
250
        if (my $connector = $self->connector) {
251
            croak "connector must have dbh() method"
252
              unless ref $connector && $connector->can('dbh');
253
              
254
            return $self->{dbh} = $connector->dbh;
255
        }
256
        
257
        return $self->{dbh} ||= $self->_connect;
update pod
Yuki Kimoto authored on 2011-03-13
258
    }
259
}
260

            
cleanup
Yuki Kimoto authored on 2011-03-21
261
our %DELETE_ARGS
cleanup
Yuki Kimoto authored on 2011-03-21
262
  = map { $_ => 1 } @COMMON_ARGS, qw/where append allow_delete_all/;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
263

            
cleanup
yuki-kimoto authored on 2010-10-17
264
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
265
    my ($self, %args) = @_;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
266

            
cleanup
Yuki Kimoto authored on 2011-04-02
267
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
268
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-04-02
269
        croak qq{Argument "$name" is wrong name}
cleanup
Yuki Kimoto authored on 2011-03-21
270
          unless $DELETE_ARGS{$name};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
271
    }
272
    
273
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
274
    my $table = $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
275
    croak qq{"table" option must be specified} unless $table;
cleanup
Yuki Kimoto authored on 2011-03-21
276
    my $where            = delete $args{where} || {};
277
    my $append           = delete $args{append};
278
    my $allow_delete_all = delete $args{allow_delete_all};
cleanup
Yuki Kimoto authored on 2011-04-02
279
    my $query_return     = delete $args{query};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
280

            
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
281
    # Where
cleanup
Yuki Kimoto authored on 2011-04-02
282
    $where = $self->_where_to_obj($where);
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
283
    
cleanup
Yuki Kimoto authored on 2011-04-02
284
    # Where clause
285
    my $where_clause = $where->to_string;
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
286
    croak qq{"where" must be specified}
cleanup
Yuki Kimoto authored on 2011-04-02
287
      if $where_clause eq '' && !$allow_delete_all;
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
288

            
cleanup
Yuki Kimoto authored on 2011-04-02
289
    # Delete statement
cleanup
Yuki Kimoto authored on 2011-01-27
290
    my @sql;
cleanup
Yuki Kimoto authored on 2011-04-02
291
    my $q = $self->reserved_word_quote;
292
    push @sql, "delete from $q$table$q $where_clause";
cleanup
Yuki Kimoto authored on 2011-01-27
293
    push @sql, $append if $append;
294
    my $sql = join(' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
295
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
296
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
297
    my $query = $self->create_query($sql);
cleanup
Yuki Kimoto authored on 2011-04-02
298
    return $query if $query_return;
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
299
    
packaging one directory
yuki-kimoto authored on 2009-11-16
300
    # Execute query
cleanup
Yuki Kimoto authored on 2011-04-02
301
    return $self->execute(
cleanup
Yuki Kimoto authored on 2011-03-21
302
        $query,
cleanup
Yuki Kimoto authored on 2011-04-02
303
        param => $where->param,
cleanup
Yuki Kimoto authored on 2011-03-21
304
        table => $table,
305
        %args
306
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
307
}
308

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
311
our %DELETE_AT_ARGS = (%DELETE_ARGS, where => 1, primary_key => 1);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
312

            
313
sub delete_at {
314
    my ($self, %args) = @_;
315
    
cleanup
Yuki Kimoto authored on 2011-04-02
316
    # Arguments
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
317
    my $primary_keys = delete $args{primary_key};
318
    $primary_keys = [$primary_keys] unless ref $primary_keys;
cleanup
Yuki Kimoto authored on 2011-04-02
319
    my $where = delete $args{where};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
320
    
cleanup
Yuki Kimoto authored on 2011-04-02
321
    # Check arguments
322
    foreach my $name (keys %args) {
323
        croak qq{Argument "$name" is wrong name}
324
          unless $DELETE_AT_ARGS{$name};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
325
    }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
326
    
cleanup
Yuki Kimoto authored on 2011-04-02
327
    # Create where parameter
328
    my $where_param = $self->_create_where_param($where, $primary_keys);
329

            
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
330
    
cleanup
Yuki Kimoto authored on 2011-04-02
331
    return $self->delete(where => $where_param, %args);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
332
}
333

            
added helper method
yuki-kimoto authored on 2010-10-17
334
sub DESTROY { }
335

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
336
sub create_model {
337
    my $self = shift;
338
    
cleanup
Yuki Kimoto authored on 2011-04-02
339
    # Arguments
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
340
    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
341
    $args->{dbi} = $self;
342
    my $model_class = delete $args->{model_class} || 'DBIx::Custom::Model';
343
    my $model_name  = delete $args->{name};
344
    my $model_table = delete $args->{table};
345
    $model_name ||= $model_table;
346
    
cleanup
Yuki Kimoto authored on 2011-04-02
347
    # Create model
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
348
    my $model = $model_class->new($args);
349
    $model->name($model_name) unless $model->name;
350
    $model->table($model_table) unless $model->table;
351
    
352
    # Apply filter
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
353
    my $filter = ref $model->filter eq 'HASH'
354
               ? [%{$model->filter}]
355
               : $model->filter;
356
    $self->apply_filter($model->table, @$filter);
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
357
    
cleanup
Yuki Kimoto authored on 2011-04-02
358
    # Associate table with model
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
359
    croak "Table name is duplicated"
360
      if exists $self->{_model_from}->{$model->table};
361
    $self->{_model_from}->{$model->table} = $model->name;
362

            
363
    # Table alias
364
    $self->{_table_alias} ||= {};
365
    $self->{_table_alias} = {%{$self->{_table_alias}}, %{$model->table_alias}};
366
    
367
    # Set model
368
    $self->model($model->name, $model);
369
    
create_model() return model
Yuki Kimoto authored on 2011-03-29
370
    return $self->model($model->name);
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
371
}
372

            
373
sub each_column {
374
    my ($self, $cb) = @_;
375
    
376
    # Iterate all tables
377
    my $sth_tables = $self->dbh->table_info;
378
    while (my $table_info = $sth_tables->fetchrow_hashref) {
379
        
380
        # Table
381
        my $table = $table_info->{TABLE_NAME};
382
        
383
        # Iterate all columns
384
        my $sth_columns = $self->dbh->column_info(undef, undef, $table, '%');
385
        while (my $column_info = $sth_columns->fetchrow_hashref) {
386
            my $column = $column_info->{COLUMN_NAME};
387
            $self->$cb($table, $column, $column_info);
388
        }
389
    }
390
}
391

            
cleanup
Yuki Kimoto authored on 2011-04-02
392
our %EXECUTE_ARGS = map { $_ => 1 } @COMMON_ARGS, 'param';
393

            
394
sub execute {
cleanup
yuki-kimoto authored on 2010-10-17
395
    my ($self, $query, %args)  = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
396
    
cleanup
Yuki Kimoto authored on 2011-04-02
397
    # Arguments
cleanup
Yuki Kimoto authored on 2011-04-02
398
    my $param  = delete $args{param} || {};
cleanup
Yuki Kimoto authored on 2011-04-02
399
    my $tables = delete $args{table} || [];
400
    $tables = [$tables] unless ref $tables eq 'ARRAY';
cleanup
Yuki Kimoto authored on 2011-04-02
401
    my $filter = delete $args{filter};
402
    $filter = DBIx::Custom::Util::array_to_hash($filter);
403
    my $type = delete $args{type};
404
    $type = DBIx::Custom::Util::array_to_hash($type);
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
405
    
cleanup
Yuki Kimoto authored on 2011-03-09
406
    # Check argument names
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
407
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-04-02
408
        croak qq{Argument "$name" is wrong name}
cleanup
Yuki Kimoto authored on 2011-03-21
409
          unless $EXECUTE_ARGS{$name};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
410
    }
411
    
cleanup
Yuki Kimoto authored on 2011-04-02
412
    # Create query
413
    $query = $self->create_query($query) unless ref $query;
cleanup
Yuki Kimoto authored on 2011-04-02
414
    $filter ||= $query->filter;
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
415
    
cleanup
Yuki Kimoto authored on 2011-04-02
416
    # Tables
417
    unshift @$tables, @{$query->tables};
cleanup
Yuki Kimoto authored on 2011-03-09
418
    my $main_table = pop @$tables;
cleanup
Yuki Kimoto authored on 2011-04-02
419
    $tables = $self->_remove_duplicate_table($tables, $main_table);
420
    if (my $q = $self->reserved_word_quote) {
421
        $_ =~ s/$q//g for @$tables;
422
    }
cleanup
Yuki Kimoto authored on 2011-04-02
423
    
424
    # Table alias
cleanup
Yuki Kimoto authored on 2011-04-02
425
    foreach my $table (@$tables) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
426
        
cleanup
Yuki Kimoto authored on 2011-04-02
427
        # No need
428
        next unless my $alias = $self->{_table_alias}->{$table};
429
        $self->{filter} ||= {};
430
        next if $self->{filter}{out}{$table};
431
        
432
        # Filter
433
        $self->{filter}{out} ||= {};
434
        $self->{filter}{in}  ||= {};
435
        $self->{filter}{end} ||= {};
436
        
437
        # Create alias filter
438
        foreach my $type (qw/out in end/) {
439
            my @filter_names = keys %{$self->{filter}{$type}{$alias} || {}};
440
            foreach my $filter_name (@filter_names) {
441
                my $filter_name_alias = $filter_name;
442
                $filter_name_alias =~ s/^$alias\./$table\./;
443
                $filter_name_alias =~ s/^${alias}__/${table}__/; 
444
                $self->{filter}{$type}{$table}{$filter_name_alias}
445
                  = $self->{filter}{$type}{$alias}{$filter_name}
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
446
            }
447
        }
448
    }
cleanup
Yuki Kimoto authored on 2011-04-02
449
    
450
    # Applied filter
451
    my $applied_filter = {};
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
452
    foreach my $table (@$tables) {
cleanup
Yuki Kimoto authored on 2011-04-02
453
        $applied_filter = {
454
            %$applied_filter,
cleanup
Yuki Kimoto authored on 2011-01-12
455
            %{$self->{filter}{out}->{$table} || {}}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
456
        }
457
    }
cleanup
Yuki Kimoto authored on 2011-04-02
458
    $filter = {%$applied_filter, %$filter};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
459
    
cleanup
Yuki Kimoto authored on 2011-04-02
460
    # Replace filter name to code
461
    foreach my $column (keys %$filter) {
462
        my $name = $filter->{$column};
463
        if (!defined $name) {
464
            $filter->{$column} = undef;
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
465
        }
cleanup
Yuki Kimoto authored on 2011-04-02
466
        elsif (ref $name ne 'CODE') {
467
          croak qq{Filter "$name" is not registered"}
468
            unless exists $self->filters->{$name};
469
          $filter->{$column} = $self->filters->{$name};
cleanup
Yuki Kimoto authored on 2010-12-21
470
        }
471
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
472
    
cleanup
Yuki Kimoto authored on 2011-04-02
473
    # Create bind values
474
    my $bind = $self->_create_bind_values(
475
        $param,
476
        $query->columns,
477
        $filter,
478
        $type
479
    );
cleanup
yuki-kimoto authored on 2010-10-17
480
    
481
    # Execute
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
482
    my $sth = $query->sth;
cleanup
yuki-kimoto authored on 2010-10-17
483
    my $affected;
cleanup
Yuki Kimoto authored on 2011-03-21
484
    eval {
485
        for (my $i = 0; $i < @$bind; $i++) {
cleanup
Yuki Kimoto authored on 2011-04-02
486
            my $type = $bind->[$i]->{type};
487
            $sth->bind_param($i + 1, $bind->[$i]->{value}, $type ? $type : ());
cleanup
Yuki Kimoto authored on 2011-03-21
488
        }
489
        $affected = $sth->execute;
490
    };
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
491
    $self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@;
cleanup
yuki-kimoto authored on 2010-10-17
492
    
added environment variable D...
Yuki Kimoto authored on 2011-04-02
493
    # Output SQL for debug
494
    warn $query->sql . "\n" if DEBUG;
495
    
cleanup
Yuki Kimoto authored on 2011-04-02
496
    # Select statement
cleanup
yuki-kimoto authored on 2010-10-17
497
    if ($sth->{NUM_OF_FIELDS}) {
498
        
cleanup
Yuki Kimoto authored on 2011-04-02
499
        # Filter
500
        my $filter = {};
501
        $filter->{in}  = {};
502
        $filter->{end} = {};
cleanup
Yuki Kimoto authored on 2011-01-12
503
        foreach my $table (@$tables) {
cleanup
Yuki Kimoto authored on 2011-04-02
504
            foreach my $way (qw/in end/) {
505
                $filter->{$way} = {
506
                    %{$filter->{$way}},
507
                    %{$self->{filter}{$way}{$table} || {}}
508
                };
509
            }
cleanup
Yuki Kimoto authored on 2011-01-12
510
        }
511
        
512
        # Result
513
        my $result = $self->result_class->new(
cleanup
Yuki Kimoto authored on 2010-12-22
514
            sth            => $sth,
515
            filters        => $self->filters,
516
            filter_check   => $self->filter_check,
cleanup
Yuki Kimoto authored on 2011-01-12
517
            default_filter => $self->{default_in_filter},
cleanup
Yuki Kimoto authored on 2011-04-02
518
            filter         => $filter->{in} || {},
519
            end_filter     => $filter->{end} || {}
cleanup
yuki-kimoto authored on 2010-10-17
520
        );
521

            
522
        return $result;
523
    }
cleanup
Yuki Kimoto authored on 2011-04-02
524
    
525
    # Not select statement
526
    else { return $affected }
cleanup
yuki-kimoto authored on 2010-10-17
527
}
528

            
cleanup
Yuki Kimoto authored on 2011-03-21
529
our %INSERT_ARGS = map { $_ => 1 } @COMMON_ARGS, qw/param append/;
update pod
Yuki Kimoto authored on 2011-03-13
530

            
cleanup
yuki-kimoto authored on 2010-10-17
531
sub insert {
532
    my ($self, %args) = @_;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
533
    
cleanup
yuki-kimoto authored on 2010-10-17
534
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
535
    my $table  = delete $args{table};
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
536
    croak qq{"table" option must be specified} unless $table;
cleanup
Yuki Kimoto authored on 2011-03-21
537
    my $param  = delete $args{param} || {};
538
    my $append = delete $args{append} || '';
cleanup
Yuki Kimoto authored on 2011-04-02
539
    my $query_return  = delete $args{query};
540

            
541
    # Check arguments
542
    foreach my $name (keys %args) {
543
        croak qq{Argument "$name" is wrong name}
544
          unless $INSERT_ARGS{$name};
545
    }
546

            
547
    # Reserved word quote
548
    my $q = $self->reserved_word_quote;
cleanup
yuki-kimoto authored on 2010-10-17
549
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
550
    # Columns
551
    my @columns;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
552
    my $safety = $self->safety_character;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
553
    foreach my $column (keys %$param) {
554
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
555
          unless $column =~ /^[$safety\.]+$/;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
556
          $column = "$q$column$q";
557
          $column =~ s/\./$q.$q/;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
558
        push @columns, $column;
559
    }
cleanup
yuki-kimoto authored on 2010-10-17
560
    
cleanup
Yuki Kimoto authored on 2011-04-02
561
    # Insert statement
cleanup
Yuki Kimoto authored on 2011-01-27
562
    my @sql;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
563
    push @sql, "insert into $q$table$q {insert_param ". join(' ', @columns) . '}';
cleanup
Yuki Kimoto authored on 2011-01-27
564
    push @sql, $append if $append;
565
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
566
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
567
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
568
    my $query = $self->create_query($sql);
cleanup
Yuki Kimoto authored on 2011-04-02
569
    return $query if $query_return;
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
570
    
packaging one directory
yuki-kimoto authored on 2009-11-16
571
    # Execute query
cleanup
Yuki Kimoto authored on 2011-04-02
572
    return $self->execute(
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
573
        $query,
cleanup
Yuki Kimoto authored on 2011-04-02
574
        param => $param,
cleanup
Yuki Kimoto authored on 2011-03-21
575
        table => $table,
576
        %args
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
577
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
578
}
579

            
cleanup
Yuki Kimoto authored on 2011-03-21
580
our %INSERT_AT_ARGS = (%INSERT_ARGS, where => 1, primary_key => 1);
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
581

            
582
sub insert_at {
583
    my ($self, %args) = @_;
cleanup
Yuki Kimoto authored on 2011-04-02
584

            
585
    # Arguments
586
    my $primary_keys = delete $args{primary_key};
587
    $primary_keys = [$primary_keys] unless ref $primary_keys;
588
    my $where = delete $args{where};
589
    my $param = delete $args{param};
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
590
    
cleanup
Yuki Kimoto authored on 2011-04-02
591
    # Check arguments
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
592
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-04-02
593
        croak qq{Argument "$name" is wrong name}
cleanup
Yuki Kimoto authored on 2011-03-21
594
          unless $INSERT_AT_ARGS{$name};
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
595
    }
596
    
cleanup
Yuki Kimoto authored on 2011-04-02
597
    # Create where parameter
598
    my $where_param = $self->_create_where_param($where, $primary_keys);
cleanup
Yuki Kimoto authored on 2011-04-02
599
    $param = $self->merge_param($where_param, $param);
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
600
    
601
    return $self->insert(param => $param, %args);
602
}
603

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
604
sub insert_param_tag {
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
605
    my ($self, $param) = @_;
606
    
cleanup
Yuki Kimoto authored on 2011-04-02
607
    # Create insert parameter tag
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
608
    my $safety = $self->safety_character;
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
609
    my $q = $self->reserved_word_quote;
cleanup
Yuki Kimoto authored on 2011-04-02
610
    my @columns;
611
    my @placeholders;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
612
    foreach my $column (keys %$param) {
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
613
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
614
          unless $column =~ /^[$safety\.]+$/;
cleanup
Yuki Kimoto authored on 2011-04-02
615
        $column = "$q$column$q";
616
        $column =~ s/\./$q.$q/;
617
        push @columns, $column;
618
        push @placeholders, "{? $column}";
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
619
    }
620
    
cleanup
Yuki Kimoto authored on 2011-04-02
621
    return '(' . join(', ', @columns) . ') ' . 'values ' .
622
           '(' . join(', ', @placeholders) . ')'
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
623
}
624

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
625
sub include_model {
626
    my ($self, $name_space, $model_infos) = @_;
627
    
cleanup
Yuki Kimoto authored on 2011-04-02
628
    # Name space
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
629
    $name_space ||= '';
cleanup
Yuki Kimoto authored on 2011-04-02
630
    
631
    # Get Model infomations
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
632
    unless ($model_infos) {
cleanup
Yuki Kimoto authored on 2011-04-02
633

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
634
        # Load name space module
635
        croak qq{"$name_space" is invalid class name}
636
          if $name_space =~ /[^\w:]/;
637
        eval "use $name_space";
638
        croak qq{Name space module "$name_space.pm" is needed. $@} if $@;
639
        
640
        # Search model modules
641
        my $path = $INC{"$name_space.pm"};
642
        $path =~ s/\.pm$//;
643
        opendir my $dh, $path
644
          or croak qq{Can't open directory "$path": $!};
645
        $model_infos = [];
646
        while (my $module = readdir $dh) {
647
            push @$model_infos, $module
648
              if $module =~ s/\.pm$//;
649
        }
650
        close $dh;
651
    }
652
    
cleanup
Yuki Kimoto authored on 2011-04-02
653
    # Include models
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
654
    foreach my $model_info (@$model_infos) {
655
        
cleanup
Yuki Kimoto authored on 2011-04-02
656
        # Load model
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
657
        my $model_class;
658
        my $model_name;
659
        my $model_table;
660
        if (ref $model_info eq 'HASH') {
661
            $model_class = $model_info->{class};
662
            $model_name  = $model_info->{name};
663
            $model_table = $model_info->{table};
664
            
665
            $model_name  ||= $model_class;
666
            $model_table ||= $model_name;
667
        }
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
668
        else { $model_class = $model_name = $model_table = $model_info }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
669
        my $mclass = "${name_space}::$model_class";
670
        croak qq{"$mclass" is invalid class name}
671
          if $mclass =~ /[^\w:]/;
672
        unless ($mclass->can('isa')) {
673
            eval "use $mclass";
674
            croak $@ if $@;
675
        }
676
        
cleanup
Yuki Kimoto authored on 2011-04-02
677
        # Create model
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
678
        my $args = {};
679
        $args->{model_class} = $mclass if $mclass;
680
        $args->{name}        = $model_name if $model_name;
681
        $args->{table}       = $model_table if $model_table;
682
        $self->create_model($args);
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
683
    }
684
    
685
    return $self;
686
}
687

            
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
688
sub merge_param {
689
    my ($self, @params) = @_;
690
    
cleanup
Yuki Kimoto authored on 2011-04-02
691
    # Merge parameters
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
692
    my $param = {};
693
    foreach my $p (@params) {
694
        foreach my $column (keys %$p) {
695
            if (exists $param->{$column}) {
696
                $param->{$column} = [$param->{$column}]
697
                  unless ref $param->{$column} eq 'ARRAY';
698
                push @{$param->{$column}}, $p->{$column};
699
            }
700
            else {
701
                $param->{$column} = $p->{$column};
702
            }
703
        }
704
    }
705
    
706
    return $param;
707
}
708

            
cleanup
Yuki Kimoto authored on 2011-03-21
709
sub method {
710
    my $self = shift;
711
    
cleanup
Yuki Kimoto authored on 2011-04-02
712
    # Register method
cleanup
Yuki Kimoto authored on 2011-03-21
713
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
714
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
715
    
716
    return $self;
717
}
718

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
719
sub model {
720
    my ($self, $name, $model) = @_;
721
    
cleanup
Yuki Kimoto authored on 2011-04-02
722
    # Set model
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
723
    if ($model) {
724
        $self->models->{$name} = $model;
725
        return $self;
726
    }
727
    
728
    # Check model existance
729
    croak qq{Model "$name" is not included}
730
      unless $self->models->{$name};
731
    
cleanup
Yuki Kimoto authored on 2011-04-02
732
    # Get model
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
733
    return $self->models->{$name};
734
}
735

            
cleanup
Yuki Kimoto authored on 2011-03-21
736
sub mycolumn {
737
    my ($self, $table, $columns) = @_;
738
    
cleanup
Yuki Kimoto authored on 2011-04-02
739
    # Create column clause
740
    my @column;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
741
    my $q = $self->reserved_word_quote;
cleanup
Yuki Kimoto authored on 2011-03-21
742
    $columns ||= [];
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
743
    push @column, "$q$table$q.$q$_$q as $q$_$q" for @$columns;
cleanup
Yuki Kimoto authored on 2011-03-21
744
    
745
    return join (', ', @column);
746
}
747

            
added dbi_options attribute
kimoto authored on 2010-12-20
748
sub new {
749
    my $self = shift->SUPER::new(@_);
750
    
cleanup
Yuki Kimoto authored on 2011-04-02
751
    # Check attributes
added dbi_options attribute
kimoto authored on 2010-12-20
752
    my @attrs = keys %$self;
753
    foreach my $attr (@attrs) {
cleanup
Yuki Kimoto authored on 2011-04-02
754
        croak qq{"$attr" is wrong name}
added dbi_options attribute
kimoto authored on 2010-12-20
755
          unless $self->can($attr);
756
    }
cleanup
Yuki Kimoto authored on 2011-04-02
757
    
758
    # Register tag
cleanup
Yuki Kimoto authored on 2011-01-25
759
    $self->register_tag(
760
        '?'     => \&DBIx::Custom::Tag::placeholder,
761
        '='     => \&DBIx::Custom::Tag::equal,
762
        '<>'    => \&DBIx::Custom::Tag::not_equal,
763
        '>'     => \&DBIx::Custom::Tag::greater_than,
764
        '<'     => \&DBIx::Custom::Tag::lower_than,
765
        '>='    => \&DBIx::Custom::Tag::greater_than_equal,
766
        '<='    => \&DBIx::Custom::Tag::lower_than_equal,
767
        'like'  => \&DBIx::Custom::Tag::like,
768
        'in'    => \&DBIx::Custom::Tag::in,
769
        'insert_param' => \&DBIx::Custom::Tag::insert_param,
770
        'update_param' => \&DBIx::Custom::Tag::update_param
771
    );
added dbi_options attribute
kimoto authored on 2010-12-20
772
    
773
    return $self;
774
}
775

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

            
cleanup
yuki-kimoto authored on 2010-10-17
778
sub register_filter {
cleanup
Yuki Kimoto authored on 2011-04-02
779
    my $self = shift;
cleanup
yuki-kimoto authored on 2010-10-17
780
    
781
    # Register filter
782
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
cleanup
Yuki Kimoto authored on 2011-04-02
783
    $self->filters({%{$self->filters}, %$filters});
cleanup
yuki-kimoto authored on 2010-10-17
784
    
cleanup
Yuki Kimoto authored on 2011-04-02
785
    return $self;
cleanup
yuki-kimoto authored on 2010-10-17
786
}
packaging one directory
yuki-kimoto authored on 2009-11-16
787

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
790
our %SELECT_ARGS
cleanup
Yuki Kimoto authored on 2011-04-01
791
  = map { $_ => 1 } @COMMON_ARGS, qw/column where append relation join param/;
refactoring select
yuki-kimoto authored on 2010-04-28
792

            
packaging one directory
yuki-kimoto authored on 2009-11-16
793
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
794
    my ($self, %args) = @_;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
795

            
refactoring select
yuki-kimoto authored on 2010-04-28
796
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
797
    my $table = delete $args{table};
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
798
    my $tables = ref $table eq 'ARRAY' ? $table
799
               : defined $table ? [$table]
800
               : [];
cleanup
Yuki Kimoto authored on 2011-03-21
801
    my $columns   = delete $args{column};
802
    my $where     = delete $args{where} || {};
803
    my $append    = delete $args{append};
804
    my $join      = delete $args{join} || [];
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
805
    croak qq{"join" must be array reference}
806
      unless ref $join eq 'ARRAY';
cleanup
Yuki Kimoto authored on 2011-03-21
807
    my $relation = delete $args{relation};
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
808
    my $param = delete $args{param} || {};
cleanup
Yuki Kimoto authored on 2011-04-02
809
    my $query_return = $args{query};
810

            
811
    # Check arguments
812
    foreach my $name (keys %args) {
813
        croak qq{Argument "$name" is wrong name}
814
          unless $SELECT_ARGS{$name};
815
    }
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
816
    
cleanup
Yuki Kimoto authored on 2011-03-09
817
    # Add relation tables(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-03-21
818
    $self->_add_relation_table($tables, $relation);
packaging one directory
yuki-kimoto authored on 2009-11-16
819
    
cleanup
Yuki Kimoto authored on 2011-04-02
820
    # Select statement
cleanup
Yuki Kimoto authored on 2011-01-27
821
    my @sql;
822
    push @sql, 'select';
packaging one directory
yuki-kimoto authored on 2009-11-16
823
    
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
824
    # Column clause
cleanup
Yuki Kimoto authored on 2011-03-30
825
    if ($columns) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
826
        $columns = [$columns] if ! ref $columns;
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
827
        foreach my $column (@$columns) {
cleanup
Yuki Kimoto authored on 2011-04-02
828
            unshift @$tables, @{$self->_search_tables($column)};
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
829
            push @sql, ($column, ',');
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
830
        }
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
831
        pop @sql if $sql[-1] eq ',';
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
832
    }
833
    else { push @sql, '*' }
834
    
835
    # Table
cleanup
Yuki Kimoto authored on 2011-03-30
836
    push @sql, 'from';
cleanup
Yuki Kimoto authored on 2011-04-02
837
    my $q = $self->reserved_word_quote;
cleanup
Yuki Kimoto authored on 2011-03-30
838
    if ($relation) {
839
        my $found = {};
840
        foreach my $table (@$tables) {
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
841
            push @sql, ("$q$table$q", ',') unless $found->{$table};
cleanup
Yuki Kimoto authored on 2011-03-30
842
            $found->{$table} = 1;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
843
        }
packaging one directory
yuki-kimoto authored on 2009-11-16
844
    }
cleanup
Yuki Kimoto authored on 2011-03-30
845
    else {
846
        my $main_table = $tables->[-1] || '';
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
847
        push @sql, "$q$main_table$q";
cleanup
Yuki Kimoto authored on 2011-03-30
848
    }
849
    pop @sql if ($sql[-1] || '') eq ',';
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
850
    croak "Not found table name" unless $tables->[-1];
cleanup
Yuki Kimoto authored on 2011-04-01
851

            
cleanup
Yuki Kimoto authored on 2011-04-02
852
    # Add tables in parameter
853
    unshift @$tables, @{$self->_search_tables(join(' ', keys %$param) || '')};
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
854
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
855
    # Where
cleanup
Yuki Kimoto authored on 2011-04-02
856
    $where = $self->_where_to_obj($where);
857
    $param = keys %$param ? $self->merge_param($param, $where->param)
858
                          : $where->param;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
859
    
860
    # String where
cleanup
Yuki Kimoto authored on 2011-04-02
861
    my $where_clause = $where->to_string;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
862
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
863
    # Add table names in where clause
cleanup
Yuki Kimoto authored on 2011-04-02
864
    unshift @$tables, @{$self->_search_tables($where_clause)};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
865
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
866
    # Push join
867
    $self->_push_join(\@sql, $join, $tables);
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
868
    
cleanup
Yuki Kimoto authored on 2011-03-09
869
    # Add where clause
cleanup
Yuki Kimoto authored on 2011-04-02
870
    push @sql, $where_clause;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
871
    
cleanup
Yuki Kimoto authored on 2011-03-08
872
    # Relation(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-04-02
873
    $self->_push_relation(\@sql, $tables, $relation, $where_clause eq '' ? 1 : 0);
cleanup
Yuki Kimoto authored on 2011-03-08
874
    
cleanup
Yuki Kimoto authored on 2011-04-02
875
    # Append
cleanup
Yuki Kimoto authored on 2011-01-27
876
    push @sql, $append if $append;
877
    
878
    # SQL
879
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
880
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
881
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
882
    my $query = $self->create_query($sql);
cleanup
Yuki Kimoto authored on 2011-04-02
883
    return $query if $query_return;
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
884
    
packaging one directory
yuki-kimoto authored on 2009-11-16
885
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
886
    my $result = $self->execute(
cleanup
Yuki Kimoto authored on 2011-03-21
887
        $query,
cleanup
Yuki Kimoto authored on 2011-04-02
888
        param => $param, 
cleanup
Yuki Kimoto authored on 2011-03-21
889
        table => $tables,
890
        %args
891
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
892
    
893
    return $result;
894
}
895

            
cleanup
Yuki Kimoto authored on 2011-03-21
896
our %SELECT_AT_ARGS = (%SELECT_ARGS, where => 1, primary_key => 1);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
897

            
898
sub select_at {
899
    my ($self, %args) = @_;
cleanup
Yuki Kimoto authored on 2011-04-02
900

            
901
    # Arguments
902
    my $primary_keys = delete $args{primary_key};
903
    $primary_keys = [$primary_keys] unless ref $primary_keys;
904
    my $where = delete $args{where};
905
    my $param = delete $args{param};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
906
    
cleanup
Yuki Kimoto authored on 2011-04-02
907
    # Check arguments
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
908
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-04-02
909
        croak qq{Argument "$name" is wrong name}
cleanup
Yuki Kimoto authored on 2011-03-21
910
          unless $SELECT_AT_ARGS{$name};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
911
    }
912
    
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
913
    # Table
914
    croak qq{"table" option must be specified} unless $args{table};
915
    my $table = ref $args{table} ? $args{table}->[-1] : $args{table};
916
    
cleanup
Yuki Kimoto authored on 2011-04-02
917
    # Create where parameter
918
    my $where_param = $self->_create_where_param($where, $primary_keys);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
919
    
cleanup
Yuki Kimoto authored on 2011-04-02
920
    return $self->select(where => $where_param, %args);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
921
}
922

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
923
sub setup_model {
924
    my $self = shift;
925
    
cleanup
Yuki Kimoto authored on 2011-04-02
926
    # Setup model
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
927
    $self->each_column(
928
        sub {
929
            my ($self, $table, $column, $column_info) = @_;
930
            if (my $model = $self->models->{$table}) {
931
                push @{$model->columns}, $column;
932
            }
933
        }
934
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
935
    return $self;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
936
}
937

            
cleanup
Yuki Kimoto authored on 2011-03-21
938
our %UPDATE_ARGS
939
  = map { $_ => 1 } @COMMON_ARGS, qw/param where append allow_update_all/;
cleanup
yuki-kimoto authored on 2010-10-17
940

            
941
sub update {
942
    my ($self, %args) = @_;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
943

            
cleanup
yuki-kimoto authored on 2010-10-17
944
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
945
    my $table = delete $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
946
    croak qq{"table" option must be specified} unless $table;
cleanup
Yuki Kimoto authored on 2011-03-21
947
    my $param            = delete $args{param} || {};
948
    my $where            = delete $args{where} || {};
949
    my $append           = delete $args{append} || '';
950
    my $allow_update_all = delete $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
951
    
cleanup
Yuki Kimoto authored on 2011-04-02
952
    # Check argument names
953
    foreach my $name (keys %args) {
954
        croak qq{Argument "$name" is wrong name}
955
          unless $UPDATE_ARGS{$name};
956
    }
957
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
958
    # Columns
959
    my @columns;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
960
    my $safety = $self->safety_character;
cleanup
Yuki Kimoto authored on 2011-04-02
961
    my $q = $self->reserved_word_quote;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
962
    foreach my $column (keys %$param) {
963
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
964
          unless $column =~ /^[$safety\.]+$/;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
965
          $column = "$q$column$q";
966
          $column =~ s/\./$q.$q/;
967
        push @columns, "$column";
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
968
    }
969
        
cleanup
yuki-kimoto authored on 2010-10-17
970
    # Update clause
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
971
    my $update_clause = '{update_param ' . join(' ', @columns) . '}';
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
972

            
973
    # Where
cleanup
Yuki Kimoto authored on 2011-04-02
974
    $where = $self->_where_to_obj($where);
975
    my $where_clause = $where->to_string;
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
976
    croak qq{"where" must be specified}
cleanup
Yuki Kimoto authored on 2011-04-02
977
      if "$where_clause" eq '' && !$allow_update_all;
removed reconnect method
yuki-kimoto authored on 2010-05-28
978
    
cleanup
Yuki Kimoto authored on 2011-04-02
979
    # Update statement
cleanup
Yuki Kimoto authored on 2011-01-27
980
    my @sql;
cleanup
Yuki Kimoto authored on 2011-04-02
981
    push @sql, "update $q$table$q $update_clause $where_clause";
cleanup
Yuki Kimoto authored on 2011-01-27
982
    push @sql, $append if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
983
    
cleanup
Yuki Kimoto authored on 2011-04-02
984
    # Merge parameters
985
    $param = $self->merge_param($param, $where->param);
cleanup
yuki-kimoto authored on 2010-10-17
986
    
cleanup
Yuki Kimoto authored on 2011-01-27
987
    # SQL
988
    my $sql = join(' ', @sql);
989
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
990
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
991
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
992
    return $query if $args{query};
993
    
cleanup
yuki-kimoto authored on 2010-10-17
994
    # Execute query
cleanup
Yuki Kimoto authored on 2011-03-21
995
    my $ret_val = $self->execute(
996
        $query,
997
        param  => $param, 
998
        table => $table,
999
        %args
1000
    );
cleanup
yuki-kimoto authored on 2010-10-17
1001
    
1002
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1003
}
1004

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
1007
our %UPDATE_AT_ARGS = (%UPDATE_ARGS, where => 1, primary_key => 1);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1008

            
1009
sub update_at {
1010
    my ($self, %args) = @_;
1011
    
cleanup
Yuki Kimoto authored on 2011-04-02
1012
    # Arguments
1013
    my $primary_keys = delete $args{primary_key};
1014
    $primary_keys = [$primary_keys] unless ref $primary_keys;
1015
    my $where = delete $args{where};
1016
    
1017

            
1018
    # Check arguments
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1019
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-04-02
1020
        croak qq{Argument "$name" is wrong name}
cleanup
Yuki Kimoto authored on 2011-03-21
1021
          unless $UPDATE_AT_ARGS{$name};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1022
    }
1023
    
cleanup
Yuki Kimoto authored on 2011-04-02
1024
    # Create where parameter
1025
    my $where_param = $self->_create_where_param($where, $primary_keys);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1026
    
cleanup
Yuki Kimoto authored on 2011-04-02
1027
    return $self->update(where => $where_param, %args);
1028
}
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1029

            
cleanup
Yuki Kimoto authored on 2011-04-02
1030
sub _create_where_param {
1031
    my ($self, $where, $primary_keys) = @_;
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1032
    
cleanup
Yuki Kimoto authored on 2011-04-02
1033
    # Create where parameter
1034
    my $where_param = {};
1035
    if ($where) {
1036
        $where = [$where] unless ref $where;
1037
        croak qq{"where" must be constant value or array reference}
1038
          unless !ref $where || ref $where eq 'ARRAY';
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1039
        for(my $i = 0; $i < @$primary_keys; $i ++) {
cleanup
Yuki Kimoto authored on 2011-04-02
1040
           $where_param->{$primary_keys->[$i]} = $where->[$i];
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1041
        }
1042
    }
1043
    
cleanup
Yuki Kimoto authored on 2011-04-02
1044
    return $where_param;
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1045
}
1046

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1047
sub update_param_tag {
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
1048
    my ($self, $param, $opt) = @_;
1049
    
cleanup
Yuki Kimoto authored on 2011-04-02
1050
    # Create update parameter tag
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
1051
    my @params;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1052
    my $safety = $self->safety_character;
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
1053
    my $q = $self->reserved_word_quote;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1054
    foreach my $column (keys %$param) {
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1055
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1056
          unless $column =~ /^[$safety\.]+$/;
cleanup
Yuki Kimoto authored on 2011-04-02
1057
        my $column = "$q$column$q";
1058
        $column =~ s/\./$q.$q/;
1059
        push @params, "$column = {? $column}";
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1060
    }
cleanup
Yuki Kimoto authored on 2011-04-02
1061
    my $tag;
1062
    $tag .= 'set ' unless $opt->{no_set};
1063
    $tag .= join(', ', @params);
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1064
    
cleanup
Yuki Kimoto authored on 2011-04-02
1065
    return $tag;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1066
}
1067

            
cleanup
Yuki Kimoto authored on 2011-01-25
1068
sub where {
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1069
    my $self = shift;
cleanup
Yuki Kimoto authored on 2011-04-02
1070
    
1071
    # Create where
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1072
    return DBIx::Custom::Where->new(
1073
        query_builder => $self->query_builder,
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1074
        safety_character => $self->safety_character,
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1075
        reserved_word_quote => $self->reserved_word_quote,
cleanup
Yuki Kimoto authored on 2011-03-09
1076
        @_
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1077
    );
cleanup
Yuki Kimoto authored on 2011-01-25
1078
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1079

            
cleanup
Yuki Kimoto authored on 2011-04-02
1080
sub _create_bind_values {
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1081
    my ($self, $params, $columns, $filter, $type) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1082
    
cleanup
Yuki Kimoto authored on 2011-04-02
1083
    # Create bind values
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1084
    my $bind = [];
removed reconnect method
yuki-kimoto authored on 2010-05-28
1085
    my $count = {};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1086
    my $not_exists = {};
cleanup
Yuki Kimoto authored on 2011-01-12
1087
    foreach my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1088
        
1089
        # Value
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1090
        my $value;
1091
        if(ref $params->{$column} eq 'ARRAY') {
1092
            my $i = $count->{$column} || 0;
1093
            $i += $not_exists->{$column} || 0;
1094
            my $found;
1095
            for (my $k = $i; $i < @{$params->{$column}}; $k++) {
1096
                if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
1097
                    $not_exists->{$column}++;
1098
                }
1099
                else  {
1100
                    $value = $params->{$column}->[$k];
1101
                    $found = 1;
1102
                    last
1103
                }
1104
            }
1105
            next unless $found;
1106
        }
1107
        else { $value = $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1108
        
cleanup
Yuki Kimoto authored on 2011-01-12
1109
        # Filter
1110
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
1111
        
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1112
        # Type
1113
        push @$bind, {
1114
            value => $f ? $f->($value) : $value,
1115
            type => $type->{$column}
1116
        };
removed reconnect method
yuki-kimoto authored on 2010-05-28
1117
        
1118
        # Count up 
1119
        $count->{$column}++;
1120
    }
1121
    
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1122
    return $bind;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1123
}
1124

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1125
sub _connect {
1126
    my $self = shift;
1127
    
1128
    # Attributes
1129
    my $data_source = $self->data_source;
1130
    croak qq{"data_source" must be specified to connect()"}
1131
      unless $data_source;
1132
    my $user        = $self->user;
1133
    my $password    = $self->password;
1134
    my $dbi_option = {%{$self->dbi_options}, %{$self->dbi_option}};
1135
    
1136
    # Connect
1137
    my $dbh = eval {DBI->connect(
1138
        $data_source,
1139
        $user,
1140
        $password,
1141
        {
1142
            %{$self->default_dbi_option},
1143
            %$dbi_option
1144
        }
1145
    )};
1146
    
1147
    # Connect error
1148
    croak $@ if $@;
1149
    
1150
    return $dbh;
1151
}
1152

            
cleanup
yuki-kimoto authored on 2010-10-17
1153
sub _croak {
1154
    my ($self, $error, $append) = @_;
cleanup
Yuki Kimoto authored on 2011-04-02
1155
    
1156
    # Append
cleanup
yuki-kimoto authored on 2010-10-17
1157
    $append ||= "";
1158
    
1159
    # Verbose
1160
    if ($Carp::Verbose) { croak $error }
1161
    
1162
    # Not verbose
1163
    else {
1164
        
1165
        # Remove line and module infromation
1166
        my $at_pos = rindex($error, ' at ');
1167
        $error = substr($error, 0, $at_pos);
1168
        $error =~ s/\s+$//;
1169
        croak "$error$append";
1170
    }
1171
}
1172

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1173
sub _need_tables {
1174
    my ($self, $tree, $need_tables, $tables) = @_;
1175
    
cleanup
Yuki Kimoto authored on 2011-04-02
1176
    # Get needed tables
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1177
    foreach my $table (@$tables) {
1178
        if ($tree->{$table}) {
1179
            $need_tables->{$table} = 1;
1180
            $self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}])
1181
        }
1182
    }
1183
}
1184

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1185
sub _push_join {
1186
    my ($self, $sql, $join, $join_tables) = @_;
1187
    
cleanup
Yuki Kimoto authored on 2011-04-02
1188
    # No join
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1189
    return unless @$join;
1190
    
cleanup
Yuki Kimoto authored on 2011-04-02
1191
    # Push join clause
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1192
    my $tree = {};
cleanup
Yuki Kimoto authored on 2011-04-02
1193
    my $q = $self->reserved_word_quote;
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1194
    for (my $i = 0; $i < @$join; $i++) {
1195
        
cleanup
Yuki Kimoto authored on 2011-04-02
1196
        # Search table in join clause
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1197
        my $join_clause = $join->[$i];
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1198
        my $q_re = quotemeta($q);
cleanup
Yuki Kimoto authored on 2011-04-01
1199
        my $join_re = $q ? qr/\s$q_re?([^\.\s$q_re]+?)$q_re?\..+?\s$q_re?([^\.\s$q_re]+?)$q_re?\..+?$/
1200
                         : qr/\s([^\.\s]+?)\..+?\s([^\.\s]+?)\..+?$/;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1201
        if ($join_clause =~ $join_re) {
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1202
            my $table1 = $1;
1203
            my $table2 = $2;
1204
            croak qq{right side table of "$join_clause" must be uniq}
1205
              if exists $tree->{$table2};
1206
            $tree->{$table2}
1207
              = {position => $i, parent => $table1, join => $join_clause};
1208
        }
1209
        else {
1210
            croak qq{join "$join_clause" must be two table name};
1211
        }
1212
    }
1213
    
cleanup
Yuki Kimoto authored on 2011-04-02
1214
    # Search need tables
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1215
    my $need_tables = {};
1216
    $self->_need_tables($tree, $need_tables, $join_tables);
1217
    my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} } keys %$need_tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1218
    
1219
    # Add join clause
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1220
    foreach my $need_table (@need_tables) {
1221
        push @$sql, $tree->{$need_table}{join};
1222
    }
1223
}
cleanup
Yuki Kimoto authored on 2011-03-08
1224

            
cleanup
Yuki Kimoto authored on 2011-04-02
1225
sub _remove_duplicate_table {
1226
    my ($self, $tables, $main_table) = @_;
1227
    
1228
    # Remove duplicate table
1229
    my %tables = map {defined $_ ? ($_ => 1) : ()} @$tables;
1230
    delete $tables{$main_table} if $main_table;
1231
    
1232
    return [keys %tables, $main_table ? $main_table : ()];
1233
}
1234

            
cleanup
Yuki Kimoto authored on 2011-04-02
1235
sub _search_tables {
cleanup
Yuki Kimoto authored on 2011-04-02
1236
    my ($self, $source) = @_;
1237
    
cleanup
Yuki Kimoto authored on 2011-04-02
1238
    # Search tables
cleanup
Yuki Kimoto authored on 2011-04-02
1239
    my $tables = [];
1240
    my $safety_character = $self->safety_character;
1241
    my $q = $self->reserved_word_quote;
1242
    my $q_re = quotemeta($q);
improved table search in col...
Yuki Kimoto authored on 2011-04-12
1243
    my $table_re = $q ? qr/(?:^|[^$safety_character])$q_re?([$safety_character]+)$q_re?\./
1244
                      : qr/(?:^|[^$safety_character])([$safety_character]+)\./;
cleanup
Yuki Kimoto authored on 2011-04-02
1245
    while ($source =~ /$table_re/g) {
1246
        push @$tables, $1;
1247
    }
1248
    
1249
    return $tables;
1250
}
1251

            
cleanup
Yuki Kimoto authored on 2011-04-02
1252
sub _where_to_obj {
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1253
    my ($self, $where) = @_;
1254
    
cleanup
Yuki Kimoto authored on 2011-04-02
1255
    my $obj;
1256
    
1257
    # Hash
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1258
    if (ref $where eq 'HASH') {
1259
        my $clause = ['and'];
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1260
        my $q = $self->reserved_word_quote;
1261
        foreach my $column (keys %$where) {
1262
            $column = "$q$column$q";
1263
            $column =~ s/\./$q.$q/;
1264
            push @$clause, "{= $column}" for keys %$where;
1265
        }
cleanup
Yuki Kimoto authored on 2011-04-02
1266
        $obj = $self->where(clause => $clause, param => $where);
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1267
    }
cleanup
Yuki Kimoto authored on 2011-04-02
1268
    
1269
    # DBIx::Custom::Where object
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1270
    elsif (ref $where eq 'DBIx::Custom::Where') {
cleanup
Yuki Kimoto authored on 2011-04-02
1271
        $obj = $where;
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1272
    }
cleanup
Yuki Kimoto authored on 2011-04-02
1273
    
1274
    # Array(DEPRECATED!)
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1275
    elsif (ref $where eq 'ARRAY') {
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
1276
        warn "\$dbi->select(where => [CLAUSE, PARAMETER]) is DEPRECATED." .
1277
             "use \$dbi->select(where => \$dbi->where(clause => " .
1278
             "CLAUSE, param => PARAMETER));";
cleanup
Yuki Kimoto authored on 2011-04-02
1279
        $obj = $self->where(
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1280
            clause => $where->[0],
1281
            param  => $where->[1]
1282
        );
1283
    }
1284
    
cleanup
Yuki Kimoto authored on 2011-04-02
1285
    # Check where argument
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1286
    croak qq{"where" must be hash reference or DBIx::Custom::Where object} .
1287
          qq{or array reference, which contains where clause and paramter}
cleanup
Yuki Kimoto authored on 2011-04-02
1288
      unless ref $obj eq 'DBIx::Custom::Where';
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1289
    
cleanup
Yuki Kimoto authored on 2011-04-02
1290
    return $obj;
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1291
}
1292

            
cleanup
Yuki Kimoto authored on 2011-01-25
1293
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-23
1294
__PACKAGE__->attr(
1295
    dbi_options => sub { {} },
1296
    filter_check  => 1
1297
);
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1298

            
cleanup
Yuki Kimoto authored on 2011-01-25
1299
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1300
sub default_bind_filter {
1301
    my $self = shift;
1302
    
1303
    if (@_) {
1304
        my $fname = $_[0];
1305
        
1306
        if (@_ && !$fname) {
1307
            $self->{default_out_filter} = undef;
1308
        }
1309
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1310
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1311
              unless exists $self->filters->{$fname};
1312
        
1313
            $self->{default_out_filter} = $self->filters->{$fname};
1314
        }
1315
        return $self;
1316
    }
1317
    
1318
    return $self->{default_out_filter};
1319
}
1320

            
cleanup
Yuki Kimoto authored on 2011-01-25
1321
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1322
sub default_fetch_filter {
1323
    my $self = shift;
1324
    
1325
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
1326
        my $fname = $_[0];
1327

            
cleanup
Yuki Kimoto authored on 2011-01-12
1328
        if (@_ && !$fname) {
1329
            $self->{default_in_filter} = undef;
1330
        }
1331
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1332
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1333
              unless exists $self->filters->{$fname};
1334
        
1335
            $self->{default_in_filter} = $self->filters->{$fname};
1336
        }
1337
        
1338
        return $self;
1339
    }
1340
    
many changed
Yuki Kimoto authored on 2011-01-23
1341
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
1342
}
1343

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1344
# DEPRECATED!
1345
sub insert_param {
1346
    warn "insert_param is renamed to insert_param_tag."
1347
       . " insert_param is DEPRECATED!";
1348
    return shift->insert_param_tag(@_);
1349
}
1350

            
cleanup
Yuki Kimoto authored on 2011-01-25
1351
# DEPRECATED!
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1352
sub register_tag_processor {
1353
    return shift->query_builder->register_tag_processor(@_);
1354
}
1355

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1356
# DEPRECATED!
1357
sub update_param {
1358
    warn "update_param is renamed to update_param_tag."
1359
       . " update_param is DEPRECATED!";
1360
    return shift->update_param_tag(@_);
1361
}
cleanup
Yuki Kimoto authored on 2011-03-08
1362
# DEPRECATED!
1363
sub _push_relation {
1364
    my ($self, $sql, $tables, $relation, $need_where) = @_;
1365
    
1366
    if (keys %{$relation || {}}) {
1367
        push @$sql, $need_where ? 'where' : 'and';
1368
        foreach my $rcolumn (keys %$relation) {
1369
            my $table1 = (split (/\./, $rcolumn))[0];
1370
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1371
            push @$tables, ($table1, $table2);
1372
            push @$sql, ("$rcolumn = " . $relation->{$rcolumn},  'and');
1373
        }
1374
    }
1375
    pop @$sql if $sql->[-1] eq 'and';    
1376
}
1377

            
1378
# DEPRECATED!
1379
sub _add_relation_table {
cleanup
Yuki Kimoto authored on 2011-03-09
1380
    my ($self, $tables, $relation) = @_;
cleanup
Yuki Kimoto authored on 2011-03-08
1381
    
1382
    if (keys %{$relation || {}}) {
1383
        foreach my $rcolumn (keys %$relation) {
1384
            my $table1 = (split (/\./, $rcolumn))[0];
1385
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1386
            my $table1_exists;
1387
            my $table2_exists;
1388
            foreach my $table (@$tables) {
1389
                $table1_exists = 1 if $table eq $table1;
1390
                $table2_exists = 1 if $table eq $table2;
1391
            }
1392
            unshift @$tables, $table1 unless $table1_exists;
1393
            unshift @$tables, $table2 unless $table2_exists;
1394
        }
1395
    }
1396
}
1397

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1400
=head1 NAME
1401

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1402
DBIx::Custom - Useful database access, respecting SQL!
removed reconnect method
yuki-kimoto authored on 2010-05-28
1403

            
1404
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
1405

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1406
    use DBIx::Custom;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1407
    
1408
    # Connect
1409
    my $dbi = DBIx::Custom->connect(
1410
        data_source => "dbi:mysql:database=dbname",
1411
        user => 'ken',
1412
        password => '!LFKD%$&',
1413
        dbi_option => {mysql_enable_utf8 => 1}
1414
    );
cleanup
yuki-kimoto authored on 2010-08-05
1415

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1416
    # Insert 
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1417
    $dbi->insert(
1418
        table  => 'book',
1419
        param  => {title => 'Perl', author => 'Ken'}
1420
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1421
    
1422
    # Update 
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1423
    $dbi->update(
1424
        table  => 'book', 
1425
        param  => {title => 'Perl', author => 'Ken'}, 
1426
        where  => {id => 5},
1427
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1428
    
1429
    # Delete
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1430
    $dbi->delete(
1431
        table  => 'book',
1432
        where  => {author => 'Ken'},
1433
    );
cleanup
yuki-kimoto authored on 2010-08-05
1434

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1435
    # Select
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
1436
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1437
        table  => 'book',
update document
yuki-kimoto authored on 2010-05-27
1438
        where  => {author => 'Ken'},
added commit method
yuki-kimoto authored on 2010-05-27
1439
    );
cleanup
yuki-kimoto authored on 2010-08-05
1440

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1441
    # Select, more complex
1442
    my $result = $dbi->select(
1443
        table  => 'book',
1444
        column => [
1445
            'book.author as book__author',
1446
            'company.name as company__name'
1447
        ],
1448
        where  => {'book.author' => 'Ken'},
1449
        join => ['left outer join company on book.company_id = company.id'],
1450
        append => 'order by id limit 5'
removed reconnect method
yuki-kimoto authored on 2010-05-28
1451
    );
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1452
    
removed register_format()
yuki-kimoto authored on 2010-05-26
1453
    # Fetch
1454
    while (my $row = $result->fetch) {
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1455
        
removed register_format()
yuki-kimoto authored on 2010-05-26
1456
    }
1457
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1458
    # Fetch as hash
removed register_format()
yuki-kimoto authored on 2010-05-26
1459
    while (my $row = $result->fetch_hash) {
1460
        
1461
    }
1462
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1463
    # Execute SQL with parameter.
1464
    $dbi->execute(
1465
        "select id from book where {= author} and {like title}",
1466
        param  => {author => 'ken', title => '%Perl%'}
1467
    );
1468
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1469
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
1470

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1471
L<DBIx::Custom> is L<DBI> wrapper module.
1472

            
1473
=head1 FEATURES
removed reconnect method
yuki-kimoto authored on 2010-05-28
1474

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1475
=over 4
removed reconnect method
yuki-kimoto authored on 2010-05-28
1476

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1477
=item *
removed reconnect method
yuki-kimoto authored on 2010-05-28
1478

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1479
There are many basic methods to execute various queries.
1480
C<insert()>, C<update()>, C<update_all()>,C<delete()>,
1481
C<delete_all()>, C<select()>,
1482
C<insert_at()>, C<update_at()>, 
1483
C<delete_at()>, C<select_at()>, C<execute()>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1484

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1485
=item *
1486

            
1487
Filter when data is send or receive.
1488

            
1489
=item *
1490

            
1491
Data filtering system
1492

            
1493
=item *
1494

            
1495
Model support.
1496

            
1497
=item *
1498

            
1499
Generate where clause dinamically.
1500

            
1501
=item *
1502

            
1503
Generate join clause dinamically.
1504

            
1505
=back
pod fix
Yuki Kimoto authored on 2011-01-21
1506

            
1507
=head1 GUIDE
1508

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1509
L<DBIx::Custom::Guide> - L<DBIx::Custom> Guide
pod fix
Yuki Kimoto authored on 2011-01-21
1510

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1511
=head1 Wiki
pod fix
Yuki Kimoto authored on 2011-01-21
1512

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1513
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki>
updated document
yuki-kimoto authored on 2010-08-08
1514

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

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
1517
=head2 C<connector>
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
1518

            
1519
    my $connector = $dbi->connector;
1520
    $dbi          = $dbi->connector(DBIx::Connector->new(...));
1521

            
1522
Connection manager object. if connector is set, you can get C<dbh()>
1523
from connection manager. conection manager object must have dbh() mehtod.
1524

            
1525
This is L<DBIx::Connector> example. Please pass
1526
C<default_dbi_option> to L<DBIx::Connector>.
1527

            
1528
    my $connector = DBIx::Connector->new(
1529
        "dbi:mysql:database=$DATABASE",
1530
        $USER,
1531
        $PASSWORD,
1532
        DBIx::Custom->new->default_dbi_option
1533
    );
1534
    
1535
    my $dbi = DBIx::Custom->new(connector => $connector);
1536

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1542
Data source, used when C<connect()> is executed.
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1543

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

            
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1546
    my $dbi_option = $dbi->dbi_option;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1547
    $dbi           = $dbi->dbi_option($dbi_option);
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1548

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1549
L<DBI> option, used when C<connect()> is executed.
1550
Each value in option override the value of C<default_dbi_option>.
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1551

            
1552
=head2 C<default_dbi_option>
1553

            
1554
    my $default_dbi_option = $dbi->default_dbi_option;
1555
    $dbi            = $dbi->default_dbi_option($default_dbi_option);
1556

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1557
L<DBI> default option, used when C<connect()> is executed,
1558
default to the following values.
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1559

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1560
    {
1561
        RaiseError => 1,
1562
        PrintError => 0,
1563
        AutoCommit => 1,
1564
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
1565

            
update pod
Yuki Kimoto authored on 2011-03-13
1566
You should not change C<AutoCommit> value directly,
1567
the value is used to check if the process is in transaction.
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1568

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1574
Filters, registered by C<register_filter()>.
add models() attribute
Yuki Kimoto authored on 2011-02-21
1575

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
1576
=head2 C<models>
add models() attribute
Yuki Kimoto authored on 2011-02-21
1577

            
1578
    my $models = $dbi->models;
1579
    $dbi       = $dbi->models(\%models);
1580

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1581
Models, included by C<include_model()>.
add models() attribute
Yuki Kimoto authored on 2011-02-21
1582

            
cleanup
yuki-kimoto authored on 2010-10-17
1583
=head2 C<password>
1584

            
1585
    my $password = $dbi->password;
1586
    $dbi         = $dbi->password('lkj&le`@s');
1587

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1588
Password, used when C<connect()> is executed.
update document
yuki-kimoto authored on 2010-01-30
1589

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1595
Query builder, default to L<DBIx::Custom::QueryBuilder> object.
cleanup
yuki-kimoto authored on 2010-08-05
1596

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
1597
=head2 C<reserved_word_quote>
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1598

            
1599
     my reserved_word_quote = $dbi->reserved_word_quote;
1600
     $dbi                   = $dbi->reserved_word_quote('"');
1601

            
cleanup
Yuki Kimoto authored on 2011-04-02
1602
Reserved word quote, default to empty string.
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1603

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1609
Result class, default to L<DBIx::Custom::Result>.
cleanup
yuki-kimoto authored on 2010-08-05
1610

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1611
=head2 C<safety_character>
update pod
Yuki Kimoto authored on 2011-01-27
1612

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1613
    my $safety_character = $self->safety_character;
cleanup
Yuki Kimoto authored on 2011-03-10
1614
    $dbi                 = $self->safety_character($character);
update pod
Yuki Kimoto authored on 2011-01-27
1615

            
update pod
Yuki Kimoto authored on 2011-03-13
1616
Regex of safety character for table and column name, default to '\w'.
cleanup
Yuki Kimoto authored on 2011-03-10
1617
Note that you don't have to specify like '[\w]'.
update pod
Yuki Kimoto authored on 2011-01-27
1618

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1621
    my $user = $dbi->user;
1622
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1623

            
cleanup
Yuki Kimoto authored on 2011-03-10
1624
User name, used when C<connect()> is executed.
update pod
Yuki Kimoto authored on 2011-01-27
1625

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1628
L<DBIx::Custom> inherits all methods from L<Object::Simple>
cleanup
Yuki Kimoto authored on 2011-03-10
1629
and use all methods of L<DBI>
cleanup
yuki-kimoto authored on 2010-10-17
1630
and implements the following new ones.
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1631

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
1632
=head2 C<apply_filter>
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1633

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1634
    $dbi->apply_filter(
cleanup
Yuki Kimoto authored on 2011-03-10
1635
        'book',
update pod
Yuki Kimoto authored on 2011-03-13
1636
        'issue_date' => {
1637
            out => 'tp_to_date',
1638
            in  => 'date_to_tp',
1639
            end => 'tp_to_displaydate'
1640
        },
1641
        'write_date' => {
1642
            out => 'tp_to_date',
1643
            in  => 'date_to_tp',
1644
            end => 'tp_to_displaydate'
1645
        }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1646
    );
1647

            
update pod
Yuki Kimoto authored on 2011-03-13
1648
Apply filter to columns.
1649
C<out> filter is executed before data is send to database.
1650
C<in> filter is executed after a row is fetch.
1651
C<end> filter is execute after C<in> filter is executed.
1652

            
1653
Filter is applied to the follwoing tree column name pattern.
cleanup
Yuki Kimoto authored on 2010-12-21
1654

            
update pod
Yuki Kimoto authored on 2011-03-13
1655
       PETTERN         EXAMPLE
1656
    1. Column        : author
1657
    2. Table.Column  : book.author
1658
    3. Table__Column : book__author
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1659

            
update pod
Yuki Kimoto authored on 2011-03-13
1660
If column name is duplicate with other table,
1661
Main filter specified by C<table> option is used.
1662

            
1663
You can set multiple filters at once.
1664

            
1665
    $dbi->apply_filter(
1666
        'book',
1667
        [qw/issue_date write_date/] => {
1668
            out => 'tp_to_date',
1669
            in  => 'date_to_tp',
1670
            end => 'tp_to_displaydate'
1671
        }
1672
    );
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1673

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1676
    my $dbi = DBIx::Custom->connect(
1677
        data_source => "dbi:mysql:database=dbname",
1678
        user => 'ken',
1679
        password => '!LFKD%$&',
1680
        dbi_option => {mysql_enable_utf8 => 1}
1681
    );
1682

            
1683
Connect to the database and create a new L<DBIx::Custom> object.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1684

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1685
L<DBIx::Custom> is a wrapper of L<DBI>.
cleanup
yuki-kimoto authored on 2010-08-09
1686
C<AutoCommit> and C<RaiseError> options are true, 
update pod
Yuki Kimoto authored on 2011-03-13
1687
and C<PrintError> option is false by default.
packaging one directory
yuki-kimoto authored on 2009-11-16
1688

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1689
=head2 create_model
1690

            
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
1691
    my $model = $dbi->create_model(
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1692
        table => 'book',
1693
        primary_key => 'id',
1694
        join => [
1695
            'inner join company on book.comparny_id = company.id'
1696
        ],
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
1697
        filter => {
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1698
            publish_date => {
1699
                out => 'tp_to_date',
1700
                in => 'date_to_tp',
1701
                end => 'tp_to_displaydate'
1702
            }
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
1703
        }
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1704
    );
1705

            
1706
Create L<DBIx::Custom::Model> object and initialize model.
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
1707
the module is also used from model() method.
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1708

            
1709
   $dbi->model('book')->select(...);
1710

            
cleanup
yuki-kimoto authored on 2010-10-17
1711
=head2 C<create_query>
1712
    
1713
    my $query = $dbi->create_query(
update pod
Yuki Kimoto authored on 2011-03-13
1714
        "insert into book {insert_param title author};";
cleanup
yuki-kimoto authored on 2010-10-17
1715
    );
update document
yuki-kimoto authored on 2009-11-19
1716

            
update pod
Yuki Kimoto authored on 2011-03-13
1717
Create L<DBIx::Custom::Query> object.
1718

            
cleanup
yuki-kimoto authored on 2010-10-17
1719
If you want to get high performance,
update pod
Yuki Kimoto authored on 2011-03-13
1720
create L<DBIx::Custom::Query> object and execute the query by C<execute()>
1721
instead of other methods, such as C<insert>, C<update>.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1722

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

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1725
=head2 C<dbh>
1726

            
1727
    my $dbh = $dbi->dbh;
1728

            
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
1729
Get L<DBI> database handle. if C<connector> is set, you can get
1730
database handle from C<connector>.
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1731

            
1732
=head2 C<each_column>
1733

            
1734
    $dbi->each_column(
1735
        sub {
1736
            my ($dbi, $table, $column, $column_info) = @_;
1737
            
1738
            my $type = $column_info->{TYPE_NAME};
1739
            
1740
            if ($type eq 'DATE') {
1741
                # ...
1742
            }
1743
        }
1744
    );
1745

            
1746
Iterate all column informations of all table from database.
1747
Argument is callback when one column is found.
1748
Callback receive four arguments, dbi object, table name,
1749
column name and column information.
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1750

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1753
    my $result = $dbi->execute(
1754
        "select * from book where {= title} and {like author}",
1755
        param => {title => 'Perl', author => '%Ken%'}
1756
    );
1757

            
1758
Execute SQL, containing tags.
1759
Return value is L<DBIx::Custom::Result> in select statement, or
1760
the count of affected rows in insert, update, delete statement.
1761

            
1762
Tag is turned into the statement containing place holder
1763
before SQL is executed.
1764

            
1765
    select * from where title = ? and author like ?;
1766

            
1767
See also L<Tags/Tags>.
1768

            
1769
The following opitons are currently available.
1770

            
1771
=over 4
1772

            
improved table search in col...
Yuki Kimoto authored on 2011-04-12
1773
=item C<table>
1774

            
1775
Table names for filtering.
1776

            
1777
    $dbi->execute(table => ['author', 'book']);
1778

            
1779
C<execute()> is unlike C<insert()>, C<update()>, C<delete()>, C<select(),
1780
Filtering is off because we don't know what filter is applied.
1781

            
1782

            
1783

            
1784

            
1785

            
1786

            
update pod
Yuki Kimoto authored on 2011-03-13
1787
=item C<filter>
1788

            
1789
Filter, executed before data is send to database. This is array reference.
1790
Filter value is code reference or
1791
filter name registerd by C<register_filter()>.
1792

            
1793
    # Basic
1794
    $dbi->execute(
1795
        $sql,
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
1796
        filter => {
update pod
Yuki Kimoto authored on 2011-03-13
1797
            title  => sub { uc $_[0] }
1798
            author => sub { uc $_[0] }
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
1799
        }
update pod
Yuki Kimoto authored on 2011-03-13
1800
    );
1801
    
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
1802
    # At once (use array reference)
update pod
Yuki Kimoto authored on 2011-03-13
1803
    $dbi->execute(
1804
        $sql,
1805
        filter => [
1806
            [qw/title author/]  => sub { uc $_[0] }
1807
        ]
1808
    );
1809
    
1810
    # Filter name
1811
    $dbi->execute(
1812
        $sql,
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
1813
        filter => {
update pod
Yuki Kimoto authored on 2011-03-13
1814
            title  => 'upper_case',
1815
            author => 'upper_case'
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
1816
        }
update pod
Yuki Kimoto authored on 2011-03-13
1817
    );
1818

            
1819
These filters are added to the C<out> filters, set by C<apply_filter()>.
update document
yuki-kimoto authored on 2009-11-19
1820

            
update pod
Yuki Kimoto authored on 2011-03-13
1821
=back
version 0.0901
yuki-kimoto authored on 2009-12-17
1822

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1825
    $dbi->delete(table => 'book', where => {title => 'Perl'});
1826

            
1827
Delete statement.
1828

            
1829
The following opitons are currently available.
1830

            
update pod
Yuki Kimoto authored on 2011-03-13
1831
=over 4
1832

            
update pod
Yuki Kimoto authored on 2011-03-13
1833
=item C<table>
1834

            
1835
Table name.
1836

            
1837
    $dbi->delete(table => 'book');
1838

            
1839
=item C<where>
1840

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1841
Where clause. This is hash reference or L<DBIx::Custom::Where> object
1842
or array refrence, which contains where clause and paramter.
update pod
Yuki Kimoto authored on 2011-03-13
1843
    
1844
    # Hash reference
1845
    $dbi->delete(where => {title => 'Perl'});
1846
    
1847
    # DBIx::Custom::Where object
1848
    my $where = $dbi->where(
1849
        clause => ['and', '{= author}', '{like title}'],
1850
        param  => {author => 'Ken', title => '%Perl%'}
1851
    );
1852
    $dbi->delete(where => $where);
1853

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1854
    # Array refrendce (where clause and parameter)
1855
    $dbi->delete(where =>
1856
        [
1857
            ['and', '{= author}', '{like title}'],
1858
            {author => 'Ken', title => '%Perl%'}
1859
        ]
1860
    );
1861
    
update pod
Yuki Kimoto authored on 2011-03-13
1862
=item C<append>
1863

            
1864
Append statement to last of SQL. This is string.
1865

            
1866
    $dbi->delete(append => 'order by title');
1867

            
1868
=item C<filter>
1869

            
1870
Filter, executed before data is send to database. This is array reference.
1871
Filter value is code reference or
1872
filter name registerd by C<register_filter()>.
1873

            
1874
    # Basic
1875
    $dbi->delete(
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
1876
        filter => {
update pod
Yuki Kimoto authored on 2011-03-13
1877
            title  => sub { uc $_[0] }
1878
            author => sub { uc $_[0] }
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
1879
        }
update pod
Yuki Kimoto authored on 2011-03-13
1880
    );
1881
    
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
1882
    # At once (use array reference)
update pod
Yuki Kimoto authored on 2011-03-13
1883
    $dbi->delete(
1884
        filter => [
1885
            [qw/title author/]  => sub { uc $_[0] }
1886
        ]
1887
    );
1888
    
1889
    # Filter name
1890
    $dbi->delete(
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
1891
        filter => {
update pod
Yuki Kimoto authored on 2011-03-13
1892
            title  => 'upper_case',
1893
            author => 'upper_case'
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
1894
        }
update pod
Yuki Kimoto authored on 2011-03-13
1895
    );
1896

            
1897
These filters are added to the C<out> filters, set by C<apply_filter()>.
1898

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
1899
=head2 C<column>
cleanup
Yuki Kimoto authored on 2011-03-21
1900

            
1901
    my $column = $self->column(book => ['author', 'title']);
1902

            
1903
Create column clause. The follwoing column clause is created.
1904

            
1905
    book.author as book__author,
1906
    book.title as book__title
1907

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1908
=item C<query>
update pod
Yuki Kimoto authored on 2011-03-13
1909

            
1910
Get L<DBIx::Custom::Query> object instead of executing SQL.
1911
This is true or false value.
1912

            
1913
    my $query = $dbi->delete(query => 1);
1914

            
1915
You can check SQL.
1916

            
1917
    my $sql = $query->sql;
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1918

            
update pod
Yuki Kimoto authored on 2011-03-13
1919
=back
1920

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1925
Delete statement to delete all rows.
1926
Options is same as C<delete()>.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1927

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1928
=head2 C<delete_at()>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1929

            
update pod
Yuki Kimoto authored on 2011-03-13
1930
Delete statement, using primary key.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1931

            
1932
    $dbi->delete_at(
1933
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
1934
        primary_key => 'id',
1935
        where => '5'
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1936
    );
1937

            
update pod
Yuki Kimoto authored on 2011-03-13
1938
This method is same as C<delete()> exept that
1939
C<primary_key> is specified and C<where> is constant value or array refrence.
1940
all option of C<delete()> is available.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1941

            
update pod
Yuki Kimoto authored on 2011-03-13
1942
=over 4
1943

            
1944
=item C<primary_key>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1945

            
update pod
Yuki Kimoto authored on 2011-03-13
1946
Primary key. This is constant value or array reference.
1947
    
1948
    # Constant value
1949
    $dbi->delete(primary_key => 'id');
1950

            
1951
    # Array reference
1952
    $dbi->delete(primary_key => ['id1', 'id2' ]);
1953

            
1954
This is used to create where clause.
1955

            
update pod
Yuki Kimoto authored on 2011-03-13
1956
=item C<where>
update pod
Yuki Kimoto authored on 2011-03-13
1957

            
1958
Where clause, created from primary key information.
1959
This is constant value or array reference.
1960

            
1961
    # Constant value
1962
    $dbi->delete(where => 5);
1963

            
1964
    # Array reference
1965
    $dbi->delete(where => [3, 5]);
1966

            
1967
In first examle, the following SQL is created.
1968

            
1969
    delete from book where id = ?;
1970

            
1971
Place holder is set to 5.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1972

            
update pod
Yuki Kimoto authored on 2011-03-13
1973
=back
1974

            
cleanup
yuki-kimoto authored on 2010-10-17
1975
=head2 C<insert>
1976

            
update pod
Yuki Kimoto authored on 2011-03-13
1977
    $dbi->insert(
1978
        table  => 'book', 
1979
        param  => {title => 'Perl', author => 'Ken'}
1980
    );
1981

            
1982
Insert statement.
1983

            
1984
The following opitons are currently available.
1985

            
update pod
Yuki Kimoto authored on 2011-03-13
1986
=over 4
1987

            
update pod
Yuki Kimoto authored on 2011-03-13
1988
=item C<table>
1989

            
1990
Table name.
1991

            
1992
    $dbi->insert(table => 'book');
1993

            
1994
=item C<param>
1995

            
1996
Insert data. This is hash reference.
1997

            
1998
    $dbi->insert(param => {title => 'Perl'});
1999

            
2000
=item C<append>
2001

            
2002
Append statement to last of SQL. This is string.
2003

            
2004
    $dbi->insert(append => 'order by title');
2005

            
2006
=item C<filter>
2007

            
2008
Filter, executed before data is send to database. This is array reference.
2009
Filter value is code reference or
2010
filter name registerd by C<register_filter()>.
2011

            
2012
    # Basic
2013
    $dbi->insert(
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
2014
        filter => {
update pod
Yuki Kimoto authored on 2011-03-13
2015
            title  => sub { uc $_[0] }
2016
            author => sub { uc $_[0] }
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
2017
        }
update pod
Yuki Kimoto authored on 2011-03-13
2018
    );
2019
    
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
2020
    # At once (use array reference)
update pod
Yuki Kimoto authored on 2011-03-13
2021
    $dbi->insert(
2022
        filter => [
2023
            [qw/title author/]  => sub { uc $_[0] }
2024
        ]
2025
    );
2026
    
2027
    # Filter name
2028
    $dbi->insert(
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
2029
        filter => {
update pod
Yuki Kimoto authored on 2011-03-13
2030
            title  => 'upper_case',
2031
            author => 'upper_case'
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
2032
        }
update pod
Yuki Kimoto authored on 2011-03-13
2033
    );
2034

            
2035
These filters are added to the C<out> filters, set by C<apply_filter()>.
2036

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2037
=item C<query>
update pod
Yuki Kimoto authored on 2011-03-13
2038

            
2039
Get L<DBIx::Custom::Query> object instead of executing SQL.
2040
This is true or false value.
2041

            
2042
    my $query = $dbi->insert(query => 1);
cleanup
yuki-kimoto authored on 2010-10-17
2043

            
update pod
Yuki Kimoto authored on 2011-03-13
2044
You can check SQL.
cleanup
yuki-kimoto authored on 2010-10-17
2045

            
update pod
Yuki Kimoto authored on 2011-03-13
2046
    my $sql = $query->sql;
2047

            
update pod
Yuki Kimoto authored on 2011-03-13
2048
=back
2049

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2050
=head2 C<insert_at()>
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
2051

            
update pod
Yuki Kimoto authored on 2011-03-13
2052
Insert statement, using primary key.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
2053

            
2054
    $dbi->insert_at(
2055
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
2056
        primary_key => 'id',
2057
        where => '5',
2058
        param => {title => 'Perl'}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
2059
    );
2060

            
update pod
Yuki Kimoto authored on 2011-03-13
2061
This method is same as C<insert()> exept that
2062
C<primary_key> is specified and C<where> is constant value or array refrence.
2063
all option of C<insert()> is available.
2064

            
update pod
Yuki Kimoto authored on 2011-03-13
2065
=over 4
2066

            
2067
=item C<primary_key>
update pod
Yuki Kimoto authored on 2011-03-13
2068

            
2069
Primary key. This is constant value or array reference.
2070
    
2071
    # Constant value
2072
    $dbi->insert(primary_key => 'id');
2073

            
2074
    # Array reference
2075
    $dbi->insert(primary_key => ['id1', 'id2' ]);
2076

            
2077
This is used to create parts of insert data.
2078

            
update pod
Yuki Kimoto authored on 2011-03-13
2079
=item C<where>
update pod
Yuki Kimoto authored on 2011-03-13
2080

            
2081
Parts of Insert data, create from primary key information.
2082
This is constant value or array reference.
2083

            
2084
    # Constant value
2085
    $dbi->insert(where => 5);
2086

            
2087
    # Array reference
2088
    $dbi->insert(where => [3, 5]);
2089

            
2090
In first examle, the following SQL is created.
2091

            
2092
    insert into book (id, title) values (?, ?);
2093

            
2094
Place holders are set to 5 and 'Perl'.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
2095

            
update pod
Yuki Kimoto authored on 2011-03-13
2096
=back
2097

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2098
=head2 C<insert_param_tag>
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2099

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2100
    my $insert_param_tag = $dbi->insert_param_tag({title => 'a', age => 2});
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2101

            
2102
Create insert parameter tag.
2103

            
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
2104
    (title, author) values ({? title}, {? author});
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2105

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2106
=head2 C<include_model>
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2107

            
update pod
Yuki Kimoto authored on 2011-03-13
2108
    $dbi->include_model('MyModel');
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2109

            
update pod
Yuki Kimoto authored on 2011-03-13
2110
Include models from specified namespace,
2111
the following layout is needed to include models.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2112

            
update pod
Yuki Kimoto authored on 2011-03-13
2113
    lib / MyModel.pm
2114
        / MyModel / book.pm
2115
                  / company.pm
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2116

            
update pod
Yuki Kimoto authored on 2011-03-13
2117
Name space module, extending L<DBIx::Custom::Model>.
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2118

            
update pod
Yuki Kimoto authored on 2011-03-13
2119
B<MyModel.pm>
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2120

            
2121
    package MyModel;
2122
    
2123
    use base 'DBIx::Custom::Model';
update pod
Yuki Kimoto authored on 2011-03-13
2124
    
2125
    1;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2126

            
update pod
Yuki Kimoto authored on 2011-03-13
2127
Model modules, extending name space module.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2128

            
update pod
Yuki Kimoto authored on 2011-03-13
2129
B<MyModel/book.pm>
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2130

            
update pod
Yuki Kimoto authored on 2011-03-13
2131
    package MyModel::book;
2132
    
2133
    use base 'MyModel';
2134
    
2135
    1;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2136

            
update pod
Yuki Kimoto authored on 2011-03-13
2137
B<MyModel/company.pm>
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2138

            
update pod
Yuki Kimoto authored on 2011-03-13
2139
    package MyModel::company;
2140
    
2141
    use base 'MyModel';
2142
    
2143
    1;
2144
    
2145
MyModel::book and MyModel::company is included by C<include_model()>.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2146

            
update pod
Yuki Kimoto authored on 2011-03-13
2147
You can get model object by C<model()>.
2148

            
2149
    my $book_model    = $dbi->model('book');
2150
    my $company_model = $dbi->model('company');
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2151

            
update pod
Yuki Kimoto authored on 2011-03-13
2152
See L<DBIx::Custom::Model> to know model features.
2153

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2154
=head2 C<merge_param>
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
2155

            
2156
    my $param = $dbi->merge_param({key1 => 1}, {key1 => 1, key2 => 2});
2157

            
2158
Merge paramters.
2159

            
2160
$param:
2161

            
2162
    {key1 => [1, 1], key2 => 2}
2163

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2164
=head2 C<method>
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2165

            
2166
    $dbi->method(
2167
        update_or_insert => sub {
2168
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-03-13
2169
            
2170
            # Process
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2171
        },
2172
        find_or_create   => sub {
2173
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-03-13
2174
            
2175
            # Process
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2176
        }
2177
    );
2178

            
update pod
Yuki Kimoto authored on 2011-03-13
2179
Register method. These method is called directly from L<DBIx::Custom> object.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2180

            
2181
    $dbi->update_or_insert;
2182
    $dbi->find_or_create;
2183

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2184
=head2 C<model>
update pod
Yuki Kimoto authored on 2011-03-13
2185

            
2186
    $dbi->model('book')->method(
2187
        insert => sub { ... },
2188
        update => sub { ... }
2189
    );
2190
    
2191
    my $model = $dbi->model('book');
2192

            
2193
Set and get a L<DBIx::Custom::Model> object,
2194

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2195
=head2 C<mycolumn>
cleanup
Yuki Kimoto authored on 2011-03-21
2196

            
2197
    my $column = $self->mycolumn(book => ['author', 'title']);
2198

            
2199
Create column clause for myself. The follwoing column clause is created.
2200

            
2201
    book.author as author,
2202
    book.title as title
2203

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2204
=head2 C<new>
2205

            
update pod
Yuki Kimoto authored on 2011-03-13
2206
    my $dbi = DBIx::Custom->new(
2207
        data_source => "dbi:mysql:database=dbname",
2208
        user => 'ken',
2209
        password => '!LFKD%$&',
2210
        dbi_option => {mysql_enable_utf8 => 1}
2211
    );
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2212

            
2213
Create a new L<DBIx::Custom> object.
2214

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2215
=head2 C<not_exists>
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2216

            
2217
    my $not_exists = $dbi->not_exists;
2218

            
update pod
Yuki Kimoto authored on 2011-03-13
2219
DBIx::Custom::NotExists object, indicating the column is not exists.
2220
This is used by C<clause> of L<DBIx::Custom::Where> .
experimental extended select...
Yuki Kimoto authored on 2011-01-17
2221

            
cleanup
yuki-kimoto authored on 2010-10-17
2222
=head2 C<register_filter>
2223

            
update pod
Yuki Kimoto authored on 2011-03-13
2224
    $dbi->register_filter(
2225
        # Time::Piece object to database DATE format
2226
        tp_to_date => sub {
2227
            my $tp = shift;
2228
            return $tp->strftime('%Y-%m-%d');
2229
        },
2230
        # database DATE format to Time::Piece object
2231
        date_to_tp => sub {
2232
           my $date = shift;
2233
           return Time::Piece->strptime($date, '%Y-%m-%d');
2234
        }
2235
    );
cleanup
yuki-kimoto authored on 2010-10-17
2236
    
update pod
Yuki Kimoto authored on 2011-03-13
2237
Register filters, used by C<filter> option of many methods.
cleanup
yuki-kimoto authored on 2010-10-17
2238

            
update pod
Yuki Kimoto authored on 2011-03-13
2239
=head2 C<register_tag>
cleanup
yuki-kimoto authored on 2010-10-17
2240

            
update pod
Yuki Kimoto authored on 2011-03-13
2241
    $dbi->register_tag(
2242
        update => sub {
2243
            my @columns = @_;
2244
            
2245
            # Update parameters
2246
            my $s = 'set ';
2247
            $s .= "$_ = ?, " for @columns;
2248
            $s =~ s/, $//;
2249
            
2250
            return [$s, \@columns];
2251
        }
2252
    );
cleanup
yuki-kimoto authored on 2010-10-17
2253

            
update pod
Yuki Kimoto authored on 2011-03-13
2254
Register tag, used by C<execute()>.
cleanup
yuki-kimoto authored on 2010-10-17
2255

            
update pod
Yuki Kimoto authored on 2011-03-13
2256
See also L<Tags/Tags> about tag registered by default.
cleanup
yuki-kimoto authored on 2010-10-17
2257

            
update pod
Yuki Kimoto authored on 2011-03-13
2258
Tag parser receive arguments specified in tag.
2259
In the following tag, 'title' and 'author' is parser arguments
cleanup
yuki-kimoto authored on 2010-10-17
2260

            
update pod
Yuki Kimoto authored on 2011-03-13
2261
    {update_param title author} 
cleanup
yuki-kimoto authored on 2010-10-17
2262

            
update pod
Yuki Kimoto authored on 2011-03-13
2263
Tag parser must return array refrence,
2264
first element is the result statement, 
2265
second element is column names corresponding to place holders.
cleanup
yuki-kimoto authored on 2010-10-17
2266

            
update pod
Yuki Kimoto authored on 2011-03-13
2267
In this example, result statement is 
cleanup
yuki-kimoto authored on 2010-10-17
2268

            
update pod
Yuki Kimoto authored on 2011-03-13
2269
    set title = ?, author = ?
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
2270

            
update pod
Yuki Kimoto authored on 2011-03-13
2271
Column names is
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
2272

            
update pod
Yuki Kimoto authored on 2011-03-13
2273
    ['title', 'author']
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
2274

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
2275
=head2 C<select>
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2276

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
2277
    my $result = $dbi->select(
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2278
        table  => 'book',
2279
        column => ['author', 'title'],
2280
        where  => {author => 'Ken'},
select method column option ...
Yuki Kimoto authored on 2011-02-22
2281
    );
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2282
    
update pod
Yuki Kimoto authored on 2011-03-12
2283
Select statement.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2284

            
2285
The following opitons are currently available.
2286

            
2287
=over 4
2288

            
2289
=item C<table>
2290

            
2291
Table name.
2292

            
update pod
Yuki Kimoto authored on 2011-03-12
2293
    $dbi->select(table => 'book');
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2294

            
2295
=item C<column>
2296

            
2297
Column clause. This is array reference or constant value.
2298

            
2299
    # Hash refernce
2300
    $dbi->select(column => ['author', 'title']);
2301
    
2302
    # Constant value
2303
    $dbi->select(column => 'author');
2304

            
2305
Default is '*' unless C<column> is specified.
2306

            
2307
    # Default
2308
    $dbi->select(column => '*');
2309

            
2310
=item C<where>
2311

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2312
Where clause. This is hash reference or L<DBIx::Custom::Where> object,
2313
or array refrence, which contains where clause and paramter.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2314
    
2315
    # Hash reference
update pod
Yuki Kimoto authored on 2011-03-12
2316
    $dbi->select(where => {author => 'Ken', 'title' => 'Perl'});
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2317
    
update pod
Yuki Kimoto authored on 2011-03-12
2318
    # DBIx::Custom::Where object
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2319
    my $where = $dbi->where(
2320
        clause => ['and', '{= author}', '{like title}'],
2321
        param  => {author => 'Ken', title => '%Perl%'}
2322
    );
update pod
Yuki Kimoto authored on 2011-03-12
2323
    $dbi->select(where => $where);
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2324

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2325
    # Array refrendce (where clause and parameter)
2326
    $dbi->select(where =>
2327
        [
2328
            ['and', '{= author}', '{like title}'],
2329
            {author => 'Ken', title => '%Perl%'}
2330
        ]
2331
    );
2332
    
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2333
=item C<join>
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2334

            
update pod
Yuki Kimoto authored on 2011-03-12
2335
Join clause used in need. This is array reference.
2336

            
2337
    $dbi->select(join =>
2338
        [
2339
            'left outer join company on book.company_id = company_id',
2340
            'left outer join location on company.location_id = location.id'
2341
        ]
2342
    );
2343

            
2344
If column cluase or where clause contain table name like "company.name",
2345
needed join clause is used automatically.
2346

            
2347
    $dbi->select(
2348
        table => 'book',
2349
        column => ['company.location_id as company__location_id'],
2350
        where => {'company.name' => 'Orange'},
2351
        join => [
2352
            'left outer join company on book.company_id = company.id',
2353
            'left outer join location on company.location_id = location.id'
2354
        ]
2355
    );
2356

            
2357
In above select, the following SQL is created.
2358

            
2359
    select company.location_id as company__location_id
2360
    from book
2361
      left outer join company on book.company_id = company.id
2362
    where company.name = Orange
2363

            
added EXPERIMENTAL replace()...
Yuki Kimoto authored on 2011-04-01
2364
=item C<param> EXPERIMETNAL
2365

            
2366
Parameter shown before where clause.
2367
    
2368
    $dbi->select(
2369
        table => 'table1',
2370
        column => 'table1.key1 as table1_key1, key2, key3',
2371
        where   => {'table1.key2' => 3},
2372
        join  => ['inner join (select * from table2 where {= table2.key3})' . 
2373
                  ' as table2 on table1.key1 = table2.key1'],
2374
        param => {'table2.key3' => 5}
2375
    );
2376

            
2377
For example, if you want to contain tag in join clause, 
2378
you can pass parameter by C<param> option.
2379

            
update pod
Yuki Kimoto authored on 2011-03-12
2380
=item C<append>
2381

            
update pod
Yuki Kimoto authored on 2011-03-13
2382
Append statement to last of SQL. This is string.
update pod
Yuki Kimoto authored on 2011-03-12
2383

            
2384
    $dbi->select(append => 'order by title');
2385

            
2386
=item C<filter>
2387

            
update pod
Yuki Kimoto authored on 2011-03-13
2388
Filter, executed before data is send to database. This is array reference.
2389
Filter value is code reference or
update pod
Yuki Kimoto authored on 2011-03-12
2390
filter name registerd by C<register_filter()>.
2391

            
2392
    # Basic
2393
    $dbi->select(
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
2394
        filter => {
update pod
Yuki Kimoto authored on 2011-03-12
2395
            title  => sub { uc $_[0] }
2396
            author => sub { uc $_[0] }
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
2397
        }
update pod
Yuki Kimoto authored on 2011-03-12
2398
    );
2399
    
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
2400
    # At once (use array reference)
update pod
Yuki Kimoto authored on 2011-03-12
2401
    $dbi->select(
2402
        filter => [
2403
            [qw/title author/]  => sub { uc $_[0] }
2404
        ]
2405
    );
2406
    
2407
    # Filter name
2408
    $dbi->select(
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
2409
        filter => {
update pod
Yuki Kimoto authored on 2011-03-12
2410
            title  => 'upper_case',
2411
            author => 'upper_case'
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
2412
        }
update pod
Yuki Kimoto authored on 2011-03-12
2413
    );
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
2414

            
update pod
Yuki Kimoto authored on 2011-03-13
2415
These filters are added to the C<out> filters, set by C<apply_filter()>.
update document
yuki-kimoto authored on 2009-11-19
2416

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2417
=item C<query>
cleanup
yuki-kimoto authored on 2010-08-09
2418

            
update pod
Yuki Kimoto authored on 2011-03-12
2419
Get L<DBIx::Custom::Query> object instead of executing SQL.
2420
This is true or false value.
2421

            
update pod
Yuki Kimoto authored on 2011-03-13
2422
    my $query = $dbi->select(query => 1);
update pod
Yuki Kimoto authored on 2011-03-12
2423

            
update pod
Yuki Kimoto authored on 2011-03-13
2424
You can check SQL.
update pod
Yuki Kimoto authored on 2011-03-12
2425

            
2426
    my $sql = $query->sql;
2427

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2428
=item C<type>
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
2429

            
2430
Specify database data type.
2431

            
2432
    $dbi->select(type => [image => DBI::SQL_BLOB]);
2433
    $dbi->select(type => [[qw/image audio/] => DBI::SQL_BLOB]);
2434

            
2435
This is used to bind paramter by C<bind_param()> of statment handle.
2436

            
2437
    $sth->bind_param($pos, $value, DBI::SQL_BLOB);
2438

            
update pod
Yuki Kimoto authored on 2011-03-12
2439
=back
cleanup
Yuki Kimoto authored on 2011-03-08
2440

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2441
=head2 C<select_at()>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2442

            
update pod
Yuki Kimoto authored on 2011-03-12
2443
Select statement, using primary key.
2444

            
2445
    $dbi->select_at(
2446
        table => 'book',
2447
        primary_key => 'id',
2448
        where => '5'
2449
    );
2450

            
update pod
Yuki Kimoto authored on 2011-03-13
2451
This method is same as C<select()> exept that
2452
C<primary_key> is specified and C<where> is constant value or array refrence.
update pod
Yuki Kimoto authored on 2011-03-12
2453
all option of C<select()> is available.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2454

            
update pod
Yuki Kimoto authored on 2011-03-13
2455
=over 4
2456

            
2457
=item C<primary_key>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2458

            
update pod
Yuki Kimoto authored on 2011-03-12
2459
Primary key. This is constant value or array reference.
2460
    
2461
    # Constant value
2462
    $dbi->select(primary_key => 'id');
2463

            
2464
    # Array reference
2465
    $dbi->select(primary_key => ['id1', 'id2' ]);
2466

            
update pod
Yuki Kimoto authored on 2011-03-13
2467
This is used to create where clause.
2468

            
update pod
Yuki Kimoto authored on 2011-03-13
2469
=item C<where>
update pod
Yuki Kimoto authored on 2011-03-12
2470

            
update pod
Yuki Kimoto authored on 2011-03-13
2471
Where clause, created from primary key information.
update pod
Yuki Kimoto authored on 2011-03-12
2472
This is constant value or array reference.
2473

            
2474
    # Constant value
2475
    $dbi->select(where => 5);
2476

            
2477
    # Array reference
2478
    $dbi->select(where => [3, 5]);
2479

            
2480
In first examle, the following SQL is created.
2481

            
2482
    select * from book where id = ?
2483

            
2484
Place holder is set to 5.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2485

            
update pod
Yuki Kimoto authored on 2011-03-13
2486
=back
2487

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2490
    $dbi->update(
2491
        table  => 'book',
2492
        param  => {title => 'Perl'},
2493
        where  => {id => 4}
2494
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
2495

            
update pod
Yuki Kimoto authored on 2011-03-13
2496
Update statement.
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2497

            
update pod
Yuki Kimoto authored on 2011-03-13
2498
The following opitons are currently available.
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2499

            
update pod
Yuki Kimoto authored on 2011-03-13
2500
=over 4
2501

            
update pod
Yuki Kimoto authored on 2011-03-13
2502
=item C<table>
2503

            
update pod
Yuki Kimoto authored on 2011-03-13
2504
Table name.
2505

            
2506
    $dbi->update(table => 'book');
2507

            
2508
=item C<param>
2509

            
2510
Update data. This is hash reference.
2511

            
2512
    $dbi->update(param => {title => 'Perl'});
2513

            
2514
=item C<where>
2515

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2516
Where clause. This is hash reference or L<DBIx::Custom::Where> object
2517
or array refrence.
update pod
Yuki Kimoto authored on 2011-03-13
2518
    
2519
    # Hash reference
2520
    $dbi->update(where => {author => 'Ken', 'title' => 'Perl'});
2521
    
2522
    # DBIx::Custom::Where object
2523
    my $where = $dbi->where(
2524
        clause => ['and', '{= author}', '{like title}'],
2525
        param  => {author => 'Ken', title => '%Perl%'}
2526
    );
2527
    $dbi->update(where => $where);
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2528
    
2529
    # Array refrendce (where clause and parameter)
2530
    $dbi->update(where =>
2531
        [
2532
            ['and', '{= author}', '{like title}'],
2533
            {author => 'Ken', title => '%Perl%'}
2534
        ]
2535
    );
update pod
Yuki Kimoto authored on 2011-03-13
2536

            
2537
=item C<append>
2538

            
2539
Append statement to last of SQL. This is string.
2540

            
2541
    $dbi->update(append => 'order by title');
2542

            
2543
=item C<filter>
2544

            
2545
Filter, executed before data is send to database. This is array reference.
2546
Filter value is code reference or
2547
filter name registerd by C<register_filter()>.
2548

            
2549
    # Basic
2550
    $dbi->update(
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
2551
        filter => {
update pod
Yuki Kimoto authored on 2011-03-13
2552
            title  => sub { uc $_[0] }
2553
            author => sub { uc $_[0] }
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
2554
        }
update pod
Yuki Kimoto authored on 2011-03-13
2555
    );
2556
    
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
2557
    # At once (use array reference)
update pod
Yuki Kimoto authored on 2011-03-13
2558
    $dbi->update(
2559
        filter => [
2560
            [qw/title author/]  => sub { uc $_[0] }
2561
        ]
2562
    );
2563
    
2564
    # Filter name
2565
    $dbi->update(
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
2566
        filter => {
update pod
Yuki Kimoto authored on 2011-03-13
2567
            title  => 'upper_case',
2568
            author => 'upper_case'
DBIx::Custom::Model filter a...
Yuki Kimoto authored on 2011-04-18
2569
        }
update pod
Yuki Kimoto authored on 2011-03-13
2570
    );
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2571

            
update pod
Yuki Kimoto authored on 2011-03-13
2572
These filters are added to the C<out> filters, set by C<apply_filter()>.
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2573

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2574
=item C<query>
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
2575

            
update pod
Yuki Kimoto authored on 2011-03-13
2576
Get L<DBIx::Custom::Query> object instead of executing SQL.
2577
This is true or false value.
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
2578

            
update pod
Yuki Kimoto authored on 2011-03-13
2579
    my $query = $dbi->update(query => 1);
2580

            
2581
You can check SQL.
2582

            
2583
    my $sql = $query->sql;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
2584

            
update pod
Yuki Kimoto authored on 2011-03-13
2585
=back
2586

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2589
    $dbi->update_all(table => 'book', param => {title => 'Perl'});
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
2590

            
update pod
Yuki Kimoto authored on 2011-03-13
2591
Update statement to update all rows.
2592
Options is same as C<update()>.
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
2593

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2594
=head2 C<update_at()>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2595

            
update pod
Yuki Kimoto authored on 2011-03-13
2596
Update statement, using primary key.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2597

            
2598
    $dbi->update_at(
2599
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
2600
        primary_key => 'id',
2601
        where => '5',
2602
        param => {title => 'Perl'}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2603
    );
2604

            
update pod
Yuki Kimoto authored on 2011-03-13
2605
This method is same as C<update()> exept that
2606
C<primary_key> is specified and C<where> is constant value or array refrence.
2607
all option of C<update()> is available.
2608

            
update pod
Yuki Kimoto authored on 2011-03-13
2609
=over 4
2610

            
2611
=item C<primary_key>
update pod
Yuki Kimoto authored on 2011-03-13
2612

            
2613
Primary key. This is constant value or array reference.
2614
    
2615
    # Constant value
2616
    $dbi->update(primary_key => 'id');
2617

            
2618
    # Array reference
2619
    $dbi->update(primary_key => ['id1', 'id2' ]);
2620

            
2621
This is used to create where clause.
2622

            
update pod
Yuki Kimoto authored on 2011-03-13
2623
=item C<where>
update pod
Yuki Kimoto authored on 2011-03-13
2624

            
2625
Where clause, created from primary key information.
2626
This is constant value or array reference.
2627

            
2628
    # Constant value
2629
    $dbi->update(where => 5);
2630

            
2631
    # Array reference
2632
    $dbi->update(where => [3, 5]);
2633

            
2634
In first examle, the following SQL is created.
2635

            
2636
    update book set title = ? where id = ?
2637

            
2638
Place holders are set to 'Perl' and 5.
2639

            
update pod
Yuki Kimoto authored on 2011-03-13
2640
=back
2641

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2642
=head2 C<update_param_tag>
update pod
Yuki Kimoto authored on 2011-03-13
2643

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2644
    my $update_param_tag = $dbi->update_param_tag({title => 'a', age => 2});
update pod
Yuki Kimoto authored on 2011-03-13
2645

            
2646
Create update parameter tag.
2647

            
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
2648
    set title = {? title}, author = {? author}
2649

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2650
You can create tag without 'set '
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2651
by C<no_set> option.
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
2652

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2653
    my $update_param_tag = $dbi->update_param_tag(
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
2654
        {title => 'a', age => 2}
2655
        {no_set => 1}
2656
    );
2657

            
2658
    title = {? title}, author = {? author}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2659

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2660
=head2 C<where>
fix tests
Yuki Kimoto authored on 2011-01-18
2661

            
cleanup
Yuki Kimoto authored on 2011-03-09
2662
    my $where = $dbi->where(
2663
        clause => ['and', '{= title}', '{= author}'],
2664
        param => {title => 'Perl', author => 'Ken'}
2665
    );
fix tests
Yuki Kimoto authored on 2011-01-18
2666

            
2667
Create a new L<DBIx::Custom::Where> object.
2668

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2669
=head2 C<setup_model>
cleanup
Yuki Kimoto authored on 2011-01-12
2670

            
update pod
Yuki Kimoto authored on 2011-03-13
2671
    $dbi->setup_model;
cleanup
Yuki Kimoto authored on 2011-01-12
2672

            
update pod
Yuki Kimoto authored on 2011-03-13
2673
Setup all model objects.
update pod
Yuki Kimoto authored on 2011-03-13
2674
C<columns> of model object is automatically set, parsing database information.
cleanup
Yuki Kimoto authored on 2011-01-12
2675

            
cleanup
Yuki Kimoto authored on 2011-01-25
2676
=head1 Tags
2677

            
2678
The following tags is available.
2679

            
2680
=head2 C<?>
2681

            
2682
Placeholder tag.
2683

            
2684
    {? NAME}    ->   ?
2685

            
2686
=head2 C<=>
2687

            
2688
Equal tag.
2689

            
2690
    {= NAME}    ->   NAME = ?
2691

            
2692
=head2 C<E<lt>E<gt>>
2693

            
2694
Not equal tag.
2695

            
2696
    {<> NAME}   ->   NAME <> ?
2697

            
2698
=head2 C<E<lt>>
2699

            
2700
Lower than tag
2701

            
2702
    {< NAME}    ->   NAME < ?
2703

            
2704
=head2 C<E<gt>>
2705

            
2706
Greater than tag
2707

            
2708
    {> NAME}    ->   NAME > ?
2709

            
2710
=head2 C<E<gt>=>
2711

            
2712
Greater than or equal tag
2713

            
2714
    {>= NAME}   ->   NAME >= ?
2715

            
2716
=head2 C<E<lt>=>
2717

            
2718
Lower than or equal tag
2719

            
2720
    {<= NAME}   ->   NAME <= ?
2721

            
2722
=head2 C<like>
2723

            
2724
Like tag
2725

            
2726
    {like NAME}   ->   NAME like ?
2727

            
2728
=head2 C<in>
2729

            
2730
In tag.
2731

            
2732
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
2733

            
2734
=head2 C<insert_param>
2735

            
2736
Insert parameter tag.
2737

            
2738
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
2739

            
2740
=head2 C<update_param>
2741

            
2742
Updata parameter tag.
2743

            
2744
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
2745

            
added environment variable D...
Yuki Kimoto authored on 2011-04-02
2746
=head1 ENVIRONMENT VARIABLE
2747

            
2748
=head2 C<DBIX_CUSTOM_DEBUG>
2749

            
2750
If environment variable C<DBIX_CUSTOM_DEBUG> is set to true,
2751
executed SQL is printed to STDERR.
2752

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
2755
L<DBIx::Custom> is stable. APIs keep backword compatible
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2756
except EXPERIMENTAL one in the feature.
DBIx::Custom is now stable
yuki-kimoto authored on 2010-09-07
2757

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

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

            
2762
C<< <kimoto.yuki at gmail.com> >>
2763

            
2764
L<http://github.com/yuki-kimoto/DBIx-Custom>
2765

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
2766
=head1 AUTHOR
2767

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

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

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

            
2774
This program is free software; you can redistribute it and/or modify it
2775
under the same terms as Perl itself.
2776

            
2777
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
2778

            
2779