DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
3509 lines | 90.767kb
cleanup
yuki-kimoto authored on 2009-12-22
1
package DBIx::Custom;
added EXPERIMENTAL insert, u...
Yuki Kimoto authored on 2011-06-21
2
use Object::Simple -base;
cleanup
yuki-kimoto authored on 2009-12-22
3

            
micro optimization
Yuki Kimoto authored on 2011-10-23
4
our $VERSION = '0.1735';
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
5
use 5.008001;
cleanup
yuki-kimoto authored on 2009-12-22
6

            
packaging one directory
yuki-kimoto authored on 2009-11-16
7
use Carp 'croak';
8
use DBI;
9
use DBIx::Custom::Result;
cleanup
yuki-kimoto authored on 2010-02-11
10
use DBIx::Custom::Query;
cleanup
yuki-kimoto authored on 2010-08-05
11
use DBIx::Custom::QueryBuilder;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
12
use DBIx::Custom::Where;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
13
use DBIx::Custom::Model;
cleanup
Yuki Kimoto authored on 2011-01-25
14
use DBIx::Custom::Tag;
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
15
use DBIx::Custom::Order;
cleanup
Yuki Kimoto authored on 2011-04-25
16
use DBIx::Custom::Util qw/_array_to_hash _subname/;
added tests
Yuki Kimoto authored on 2011-08-26
17
use DBIx::Custom::Mapper;
- added EXPERIMENTAL pass at...
Yuki Kimoto authored on 2011-09-02
18
use DBIx::Custom::NotExists;
improved debug message
Yuki Kimoto authored on 2011-05-23
19
use Encode qw/encode encode_utf8 decode_utf8/;
cleanup
Yuki Kimoto authored on 2011-08-13
20
use Scalar::Util qw/weaken/;
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;
improved debug message
Yuki Kimoto authored on 2011-05-23
23
use constant DEBUG_ENCODING => $ENV{DBIX_CUSTOM_DEBUG_ENCODING} || 'UTF-8';
added environment variable D...
Yuki Kimoto authored on 2011-04-02
24

            
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
25
has [qw/connector dsn password quote user exclude_table user_table_info
26
        user_column_info/],
removed from cache() and cac...
Yuki Kimoto authored on 2011-03-29
27
    cache => 0,
many changed
Yuki Kimoto authored on 2011-01-23
28
    cache_method => sub {
29
        sub {
30
            my $self = shift;
31
            
32
            $self->{_cached} ||= {};
33
            
34
            if (@_ > 1) {
update pod
Yuki Kimoto authored on 2011-03-13
35
                $self->{_cached}{$_[0]} = $_[1];
many changed
Yuki Kimoto authored on 2011-01-23
36
            }
37
            else {
update pod
Yuki Kimoto authored on 2011-03-13
38
                return $self->{_cached}{$_[0]};
many changed
Yuki Kimoto authored on 2011-01-23
39
            }
40
        }
update pod
Yuki Kimoto authored on 2011-03-13
41
    },
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
42
    option => sub { {} },
43
    default_option => sub {
update pod
Yuki Kimoto authored on 2011-03-13
44
        {
45
            RaiseError => 1,
46
            PrintError => 0,
47
            AutoCommit => 1
48
        }
49
    },
fix tests
Yuki Kimoto authored on 2011-01-13
50
    filters => sub {
51
        {
52
            encode_utf8 => sub { encode_utf8($_[0]) },
53
            decode_utf8 => sub { decode_utf8($_[0]) }
54
        }
update pod
Yuki Kimoto authored on 2011-03-13
55
    },
added EXPERIMENTAL last_sql ...
Yuki Kimoto authored on 2011-07-11
56
    last_sql => '',
update pod
Yuki Kimoto authored on 2011-03-13
57
    models => sub { {} },
cleanup
Yuki Kimoto authored on 2011-08-13
58
    query_builder => sub {
59
        my $self = shift;
60
        my $builder = DBIx::Custom::QueryBuilder->new(dbi => $self);
61
        weaken $builder->{dbi};
62
        return $builder;
63
    },
update pod
Yuki Kimoto authored on 2011-03-13
64
    result_class  => 'DBIx::Custom::Result',
65
    safety_character => '\w',
cleanup test
Yuki Kimoto authored on 2011-08-10
66
    separator => '.',
added tag_parse attribute
Yuki Kimoto authored on 2011-06-28
67
    stash => sub { {} },
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
68
    tag_parse => 1;
cleanup
yuki-kimoto authored on 2010-10-17
69

            
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
70
sub available_datatype {
test cleanup
Yuki Kimoto authored on 2011-08-10
71
    my $self = shift;
72
    
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
73
    my $data_types = '';
cleanup
Yuki Kimoto authored on 2011-10-21
74
    for my $i (-1000 .. 1000) {
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
75
         my $type_info = $self->dbh->type_info($i);
76
         my $data_type = $type_info->{DATA_TYPE};
77
         my $type_name = $type_info->{TYPE_NAME};
78
         $data_types .= "$data_type ($type_name)\n"
79
           if defined $data_type;
80
    }
81
    return "Data Type maybe equal to Type Name" unless $data_types;
82
    $data_types = "Data Type (Type name)\n" . $data_types;
83
    return $data_types;
84
}
85

            
86
sub available_typename {
87
    my $self = shift;
88
    
89
    # Type Names
90
    my $type_names = {};
91
    $self->each_column(sub {
92
        my ($self, $table, $column, $column_info) = @_;
93
        $type_names->{$column_info->{TYPE_NAME}} = 1
94
          if $column_info->{TYPE_NAME};
95
    });
96
    my @output = sort keys %$type_names;
97
    unshift @output, "Type Name";
98
    return join "\n", @output;
test cleanup
Yuki Kimoto authored on 2011-08-10
99
}
100

            
added helper method
yuki-kimoto authored on 2010-10-17
101
our $AUTOLOAD;
102
sub AUTOLOAD {
103
    my $self = shift;
104

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

            
cleanup
Yuki Kimoto authored on 2011-04-02
108
    # Call method
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
109
    $self->{_methods} ||= {};
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
110
    if (my $method = $self->{_methods}->{$mname}) {
111
        return $self->$method(@_)
112
    }
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
113
    elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
114
        $self->dbh->$dbh_method(@_);
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
115
    }
116
    else {
cleanup
Yuki Kimoto authored on 2011-04-25
117
        croak qq{Can't locate object method "$mname" via "$package" }
118
            . _subname;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
119
    }
added helper method
yuki-kimoto authored on 2010-10-17
120
}
121

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
122
sub assign_clause {
updated pod
Yuki Kimoto authored on 2011-09-02
123
    my ($self, $param, $opts) = @_;
124
    
125
    my $wrap = $opts->{wrap} || {};
fixed some quoted bug
Yuki Kimoto authored on 2011-10-22
126

            
127
    my $qp = $self->_q('');
128
    my $q = substr($qp, 0, 1) || '';
129
    my $p = substr($qp, 1, 1) || '';
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
130
    
131
    # Create set tag
132
    my @params;
133
    my $safety = $self->safety_character;
cleanup
Yuki Kimoto authored on 2011-10-21
134
    for my $column (sort keys %$param) {
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
135
        croak qq{"$column" is not safety column name } . _subname
136
          unless $column =~ /^[$safety\.]+$/;
fixed some quoted bug
Yuki Kimoto authored on 2011-10-22
137
        my $column_quote = "$q$column$p";
138
        $column_quote =~ s/\./$p.$q/;
updated pod
Yuki Kimoto authored on 2011-09-02
139
        my $func = $wrap->{$column} || sub { $_[0] };
140
        push @params,
141
          ref $param->{$column} eq 'SCALAR' ? "$column_quote = " . ${$param->{$column}}
142
        : "$column_quote = " . $func->(":$column");
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
143
    }
144
    my $tag = join(', ', @params);
145
    
146
    return $tag;
147
}
148

            
cleanup
Yuki Kimoto authored on 2011-03-21
149
sub column {
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
150
    my $self = shift;
151
    my $option = pop if ref $_[-1] eq 'HASH';
152
    my $real_table = shift;
153
    my $columns = shift;
154
    my $table = $option->{alias} || $real_table;
155
    
156
    # Columns
157
    unless ($columns) {
158
        $columns ||= $self->model($real_table)->columns;
159
    }
added helper method
yuki-kimoto authored on 2010-10-17
160
    
EXPERIMTANL column method th...
Yuki Kimoto authored on 2011-06-13
161
    # Separator
162
    my $separator = $self->separator;
163
    
cleanup
Yuki Kimoto authored on 2011-04-02
164
    # Column clause
cleanup
Yuki Kimoto authored on 2011-03-21
165
    my @column;
cleanup
Yuki Kimoto authored on 2011-04-02
166
    $columns ||= [];
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
167
    push @column, $self->_q($table) . "." . $self->_q($_) .
168
      " as " . $self->_q("${table}${separator}$_")
EXPERIMTANL column method th...
Yuki Kimoto authored on 2011-06-13
169
      for @$columns;
cleanup
Yuki Kimoto authored on 2011-03-21
170
    
171
    return join (', ', @column);
added helper method
yuki-kimoto authored on 2010-10-17
172
}
173

            
packaging one directory
yuki-kimoto authored on 2009-11-16
174
sub connect {
cleanup
Yuki Kimoto authored on 2011-08-16
175
    my $self = ref $_[0] ? shift : shift->new(@_);
176
    
177
    my $connector = $self->connector;
178
    
179
    if (!ref $connector && $connector) {
180
        require DBIx::Connector;
181
        
182
        my $dsn = $self->dsn;
183
        my $user = $self->user;
184
        my $password = $self->password;
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
185
        my $option = $self->_option;
cleanup
Yuki Kimoto authored on 2011-08-16
186
        my $connector = DBIx::Connector->new($dsn, $user, $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
187
          {%{$self->default_option} , %$option});
cleanup
Yuki Kimoto authored on 2011-08-16
188
        $self->connector($connector);
189
    }
removed register_format()
yuki-kimoto authored on 2010-05-26
190
    
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
191
    # Connect
192
    $self->dbh;
update document
yuki-kimoto authored on 2010-01-30
193
    
packaging one directory
yuki-kimoto authored on 2009-11-16
194
    return $self;
195
}
196

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
197
sub count { shift->select(column => 'count(*)', @_)->fetch_first->[0] }
198

            
update pod
Yuki Kimoto authored on 2011-03-13
199
sub dbh {
200
    my $self = shift;
cleanup
Yuki Kimoto authored on 2011-04-02
201
    
fixed dbh() method bug:wq
Yuki Kimoto authored on 2011-04-05
202
    # Set
203
    if (@_) {
204
        $self->{dbh} = $_[0];
205
        
206
        return $self;
207
    }
208
    
209
    # Get
210
    else {
211
        # From Connction manager
212
        if (my $connector = $self->connector) {
cleanup
Yuki Kimoto authored on 2011-04-25
213
            croak "connector must have dbh() method " . _subname
fixed dbh() method bug:wq
Yuki Kimoto authored on 2011-04-05
214
              unless ref $connector && $connector->can('dbh');
215
              
set reserved_word_quote auto...
Yuki Kimoto authored on 2011-06-08
216
            $self->{dbh} = $connector->dbh;
fixed dbh() method bug:wq
Yuki Kimoto authored on 2011-04-05
217
        }
218
        
set reserved_word_quote auto...
Yuki Kimoto authored on 2011-06-08
219
        # Connect
220
        $self->{dbh} ||= $self->_connect;
221
        
222
        # Quote
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
223
        if (!defined $self->reserved_word_quote && !defined $self->quote) {
prepare oracle test
Yuki Kimoto authored on 2011-08-15
224
            my $driver = $self->_driver;
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
225
            my $quote =  $driver eq 'odbc' ? '[]'
226
                       : $driver eq 'ado' ? '[]'
227
                       : $driver eq 'mysql' ? '`'
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
228
                       : '"';
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
229
            $self->quote($quote);
set reserved_word_quote auto...
Yuki Kimoto authored on 2011-06-08
230
        }
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
231
        
set reserved_word_quote auto...
Yuki Kimoto authored on 2011-06-08
232
        return $self->{dbh};
update pod
Yuki Kimoto authored on 2011-03-13
233
    }
234
}
235

            
cleanup
Yuki Kimoto authored on 2011-10-21
236
sub delete {
237
    my ($self, %opt) = @_;
238
    warn "delete method where_param option is DEPRECATED!"
239
      if $opt{where_param};
240
    
cleanup
Yuki Kimoto authored on 2011-10-21
241
    # Don't allow delete all rows
cleanup
Yuki Kimoto authored on 2011-10-21
242
    croak qq{delete method where or id option must be specified } . _subname
243
      if !$opt{where} && !defined $opt{id} && !$opt{allow_delete_all};
244
    
245
    # Where
246
    my $where = defined $opt{id}
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
247
           ? $self->_id_to_param(delete $opt{id}, $opt{primary_key}, $opt{table})
cleanup
Yuki Kimoto authored on 2011-10-21
248
           : $opt{where};
249
    my $w = $self->_where_clause_and_param($where, $opt{where_param});
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
250

            
cleanup
Yuki Kimoto authored on 2011-04-02
251
    # Delete statement
cleanup
Yuki Kimoto authored on 2011-10-21
252
    my $sql = "delete ";
cleanup
Yuki Kimoto authored on 2011-10-21
253
    $sql .= "$opt{prefix} " if defined $opt{prefix};
cleanup
Yuki Kimoto authored on 2011-10-21
254
    $sql .= "from " . $self->_q($opt{table}) . " $w->{clause} ";
packaging one directory
yuki-kimoto authored on 2009-11-16
255
    
256
    # Execute query
cleanup
Yuki Kimoto authored on 2011-10-21
257
    return $self->execute($sql, $w->{param}, %opt);
packaging one directory
yuki-kimoto authored on 2009-11-16
258
}
259

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

            
added memory leak check test
Yuki Kimoto authored on 2011-08-15
262
sub DESTROY {}
added helper method
yuki-kimoto authored on 2010-10-17
263

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
264
sub create_model {
265
    my $self = shift;
266
    
cleanup
Yuki Kimoto authored on 2011-10-21
267
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
268
    my $opt = ref $_[0] eq 'HASH' ? $_[0] : {@_};
269
    $opt->{dbi} = $self;
270
    my $model_class = delete $opt->{model_class} || 'DBIx::Custom::Model';
271
    my $model_name  = delete $opt->{name};
272
    my $model_table = delete $opt->{table};
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
273
    $model_name ||= $model_table;
274
    
cleanup
Yuki Kimoto authored on 2011-04-02
275
    # Create model
cleanup
Yuki Kimoto authored on 2011-10-21
276
    my $model = $model_class->new($opt);
cleanup
Yuki Kimoto authored on 2011-08-13
277
    weaken $model->{dbi};
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
278
    $model->name($model_name) unless $model->name;
279
    $model->table($model_table) unless $model->table;
280
    
micro optimization
Yuki Kimoto authored on 2011-07-30
281
    # Apply filter(DEPRECATED logic)
282
    if ($model->{filter}) {
283
        my $filter = ref $model->filter eq 'HASH'
284
                   ? [%{$model->filter}]
285
                   : $model->filter;
286
        $filter ||= [];
287
        warn "DBIx::Custom::Model filter method is DEPRECATED!"
288
          if @$filter;
289
        $self->_apply_filter($model->table, @$filter);
290
    }
291
    
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
292
    # Set model
293
    $self->model($model->name, $model);
294
    
create_model() return model
Yuki Kimoto authored on 2011-03-29
295
    return $self->model($model->name);
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
296
}
297

            
298
sub each_column {
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
299
    my ($self, $cb, %options) = @_;
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
300

            
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
301
    my $user_column_info = $self->user_column_info;
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
302
    
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
303
    if ($user_column_info) {
304
        $self->$cb($_->{table}, $_->{column}, $_->{info}) for @$user_column_info;
305
    }
306
    else {
307
    
308
        my $re = $self->exclude_table || $options{exclude_table};
309
        # Tables
310
        my %tables;
311
        $self->each_table(sub { $tables{$_[1]}++ });
added SQL Server test
Yuki Kimoto authored on 2011-08-14
312

            
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
313
        # Iterate all tables
314
        my @tables = sort keys %tables;
315
        for (my $i = 0; $i < @tables; $i++) {
316
            my $table = $tables[$i];
317
            
318
            # Iterate all columns
319
            my $sth_columns;
320
            eval {$sth_columns = $self->dbh->column_info(undef, undef, $table, '%')};
321
            next if $@;
322
            while (my $column_info = $sth_columns->fetchrow_hashref) {
323
                my $column = $column_info->{COLUMN_NAME};
324
                $self->$cb($table, $column, $column_info);
325
            }
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
326
        }
327
    }
328
}
329

            
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
330
sub each_table {
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
331
    my ($self, $cb, %option) = @_;
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
332
    
cleanup test
Yuki Kimoto authored on 2011-08-16
333
    my $user_table_infos = $self->user_table_info;
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
334
    
added test
Yuki Kimoto authored on 2011-08-16
335
    # Iterate tables
336
    if ($user_table_infos) {
337
        $self->$cb($_->{table}, $_->{info}) for @$user_table_infos;
338
    }
339
    else {
340
        my $re = $self->exclude_table || $option{exclude};
341
        my $sth_tables = $self->dbh->table_info;
342
        while (my $table_info = $sth_tables->fetchrow_hashref) {
343
            
344
            # Table
345
            my $table = $table_info->{TABLE_NAME};
346
            next if defined $re && $table =~ /$re/;
347
            $self->$cb($table, $table_info);
348
        }
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
349
    }
350
}
351

            
cleanup
Yuki Kimoto authored on 2011-04-02
352
sub execute {
execute method can second ar...
Yuki Kimoto authored on 2011-06-09
353
    my $self = shift;
cleanup
Yuki Kimoto authored on 2011-10-20
354
    my $sql = shift;
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
355

            
356
    # Options
execute method can second ar...
Yuki Kimoto authored on 2011-06-09
357
    my $param;
358
    $param = shift if @_ % 2;
cleanup
Yuki Kimoto authored on 2011-10-21
359
    my %opt = @_;
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
360
    warn "sqlfilter option is DEPRECATED" if $opt{sqlfilter};
361
    $param ||= $opt{param} || {};
cleanup
Yuki Kimoto authored on 2011-10-21
362
    my $tables = $opt{table} || [];
cleanup
Yuki Kimoto authored on 2011-04-02
363
    $tables = [$tables] unless ref $tables eq 'ARRAY';
micro optimization
Yuki Kimoto authored on 2011-10-23
364
    my $filter = ref $opt{filter} eq 'ARRAY' ?
365
      _array_to_hash($opt{filter}) : $opt{filter};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
366
    
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
367
    # Append
368
    $sql .= $opt{append} if defined $opt{append} && !ref $sql;
369
    
370
    # Query
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
371
    my $query;
372
    if (ref $sql) { $query = $sql }
373
    else {
374
        $query = $opt{reuse}->{$sql} if $opt{reuse};
375
        $query = $self->_create_query($sql,$opt{after_build_sql} || $opt{sqlfilter})
376
          unless $query;
377
        $opt{reuse}->{$sql} = $query if $opt{reuse};
378
    }
379
        
added EXPERIMENTAL last_sql ...
Yuki Kimoto authored on 2011-07-11
380
    # Save query
micro optimization
Yuki Kimoto authored on 2011-10-23
381
    $self->{last_sql} = $query->{sql};
added EXPERIMENTAL last_sql ...
Yuki Kimoto authored on 2011-07-11
382

            
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
383
    # Return query
384
    return $query if $opt{query};
micro optimization
Yuki Kimoto authored on 2011-07-30
385
    
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
386
    # Merge query filter(DEPRECATED!)
DBIx::Custom::Query filter m...
Yuki Kimoto authored on 2011-07-30
387
    $filter ||= $query->{filter} || {};
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
388
    
cleanup
Yuki Kimoto authored on 2011-04-02
389
    # Tables
DBIx::Custom::Query tables a...
Yuki Kimoto authored on 2011-07-30
390
    unshift @$tables, @{$query->{tables} || []};
micro optimization
Yuki Kimoto authored on 2011-07-30
391
    my $main_table = @{$tables}[-1];
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
392
    
393
    # Convert id to parameter
394
    if (defined $opt{id}) {
395
        my $id_param = $self->_id_to_param($opt{id}, $opt{primary_key}, $main_table);
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
396
        $param = $self->merge_param($id_param, $param);
397
    }
micro optimization
Yuki Kimoto authored on 2011-07-30
398
    
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
399
    # Cleanup tables(DEPRECATED!)
micro optimization
Yuki Kimoto authored on 2011-07-30
400
    $tables = $self->_remove_duplicate_table($tables, $main_table)
401
      if @$tables > 1;
cleanup
Yuki Kimoto authored on 2011-04-02
402
    
added type_rule into logic
Yuki Kimoto authored on 2011-06-09
403
    # Type rule
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
404
    my $type_filters = {};
micro optimization
Yuki Kimoto authored on 2011-10-23
405
    if ($self->{_type_rule_is_called}) {
406
        unless ($opt{type_rule_off}) {
407
            my $type_rule_off_parts = {
408
                1 => $opt{type_rule1_off},
409
                2 => $opt{type_rule2_off}
410
            };
411
            for my $i (1, 2) {
412
                unless ($type_rule_off_parts->{$i}) {
413
                    $type_filters->{$i} = {};
414
                    my $table_alias = $opt{table_alias} || {};
415
                    for my $alias (keys %$table_alias) {
416
                        my $table = $table_alias->{$alias};
417
                        
418
                        for my $column (keys %{$self->{"_into$i"}{key}{$table} || {}}) {
419
                            $type_filters->{$i}->{"$alias.$column"} = $self->{"_into$i"}{key}{$table}{$column};
420
                        }
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
421
                    }
micro optimization
Yuki Kimoto authored on 2011-10-23
422
                    $type_filters->{$i} = {%{$type_filters->{$i}}, %{$self->{"_into$i"}{key}{$main_table} || {}}}
423
                      if $main_table;
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
424
                }
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
425
            }
added type_rule into logic
Yuki Kimoto authored on 2011-06-09
426
        }
427
    }
cleanup
Yuki Kimoto authored on 2011-04-02
428
    
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
429
    # Applied filter(DEPRECATED!)
micro optimization
Yuki Kimoto authored on 2011-07-30
430
    if ($self->{filter}{on}) {
431
        my $applied_filter = {};
cleanup
Yuki Kimoto authored on 2011-10-21
432
        for my $table (@$tables) {
micro optimization
Yuki Kimoto authored on 2011-07-30
433
            $applied_filter = {
434
                %$applied_filter,
435
                %{$self->{filter}{out}->{$table} || {}}
436
            }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
437
        }
micro optimization
Yuki Kimoto authored on 2011-07-30
438
        $filter = {%$applied_filter, %$filter};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
439
    }
440
    
cleanup
Yuki Kimoto authored on 2011-04-02
441
    # Replace filter name to code
cleanup
Yuki Kimoto authored on 2011-10-21
442
    for my $column (keys %$filter) {
cleanup
Yuki Kimoto authored on 2011-04-02
443
        my $name = $filter->{$column};
444
        if (!defined $name) {
445
            $filter->{$column} = undef;
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
446
        }
cleanup
Yuki Kimoto authored on 2011-04-02
447
        elsif (ref $name ne 'CODE') {
cleanup
Yuki Kimoto authored on 2011-04-25
448
          croak qq{Filter "$name" is not registered" } . _subname
cleanup
Yuki Kimoto authored on 2011-04-02
449
            unless exists $self->filters->{$name};
450
          $filter->{$column} = $self->filters->{$name};
cleanup
Yuki Kimoto authored on 2010-12-21
451
        }
452
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
453
    
cleanup
Yuki Kimoto authored on 2011-04-02
454
    # Create bind values
micro optimization
Yuki Kimoto authored on 2011-10-23
455
    my ($bind, $bind_types) = $self->_create_bind_values($param, $query->columns,
456
      $filter, $type_filters, $opt{bind_type} || $opt{type} || {});
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
457

            
cleanup
yuki-kimoto authored on 2010-10-17
458
    # Execute
micro optimization
Yuki Kimoto authored on 2011-10-23
459
    my $sth = $query->{sth};
cleanup
yuki-kimoto authored on 2010-10-17
460
    my $affected;
cleanup
Yuki Kimoto authored on 2011-03-21
461
    eval {
micro optimization
Yuki Kimoto authored on 2011-10-23
462
        if ($opt{bind_type} || $opt{type}) {
463
            $sth->bind_param($_ + 1, $bind->[$_],
464
                $bind_types->[$_] ? $bind_types->[$_] : ())
465
              for (0 .. @$bind - 1);
466
            $affected = $sth->execute;
467
        }
468
        else {
469
            $affected = $sth->execute(@$bind);
470
        }
cleanup
Yuki Kimoto authored on 2011-03-21
471
    };
improved error messages
Yuki Kimoto authored on 2011-04-18
472
    
micro optimization
Yuki Kimoto authored on 2011-07-30
473
    $self->_croak($@, qq{. Following SQL is executed.\n}
474
      . qq{$query->{sql}\n} . _subname) if $@;
cleanup
yuki-kimoto authored on 2010-10-17
475
    
improved debug message
Yuki Kimoto authored on 2011-05-23
476
    # DEBUG message
477
    if (DEBUG) {
478
        print STDERR "SQL:\n" . $query->sql . "\n";
479
        my @output;
cleanup
Yuki Kimoto authored on 2011-10-21
480
        for my $b (@$bind) {
improved debug message
Yuki Kimoto authored on 2011-05-23
481
            my $value = $b->{value};
482
            $value = 'undef' unless defined $value;
483
            $value = encode(DEBUG_ENCODING(), $value)
484
              if utf8::is_utf8($value);
485
            push @output, $value;
486
        }
487
        print STDERR "Bind values: " . join(', ', @output) . "\n\n";
488
    }
added environment variable D...
Yuki Kimoto authored on 2011-04-02
489
    
micro optimization
Yuki Kimoto authored on 2011-10-23
490
    # Not select statement
491
    return $affected unless $sth->{NUM_OF_FIELDS};
492

            
493
    # Filter(DEPRECATED!)
494
    my $infilter = {};
495
    if ($self->{filter}{on}) {
496
        $infilter->{in}  = {};
497
        $infilter->{end} = {};
498
        push @$tables, $main_table if $main_table;
499
        for my $table (@$tables) {
500
            for my $way (qw/in end/) {
501
                $infilter->{$way} = {%{$infilter->{$way}},
502
                  %{$self->{filter}{$way}{$table} || {}}};
cleanup
Yuki Kimoto authored on 2011-04-02
503
            }
cleanup
Yuki Kimoto authored on 2011-01-12
504
        }
cleanup
yuki-kimoto authored on 2010-10-17
505
    }
micro optimization
Yuki Kimoto authored on 2011-10-23
506
    
507
    # Result
508
    my $result = $self->result_class->new(
509
        sth => $sth,
510
        dbi => $self,
511
        default_filter => $self->{default_in_filter},
512
        filter => $infilter->{in} || {},
513
        end_filter => $infilter->{end} || {},
514
        type_rule => {
515
            from1 => $self->type_rule->{from1},
516
            from2 => $self->type_rule->{from2}
517
        },
518
    );
519
    $result;
cleanup
yuki-kimoto authored on 2010-10-17
520
}
521

            
added test
Yuki Kimoto authored on 2011-08-16
522
sub get_table_info {
cleanup
Yuki Kimoto authored on 2011-10-21
523
    my ($self, %opt) = @_;
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
524
    
cleanup
Yuki Kimoto authored on 2011-10-21
525
    my $exclude = delete $opt{exclude};
526
    croak qq/"$_" is wrong option/ for keys %opt;
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
527
    
added test
Yuki Kimoto authored on 2011-08-16
528
    my $table_info = [];
529
    $self->each_table(
530
        sub { push @$table_info, {table => $_[1], info => $_[2] } },
531
        exclude => $exclude
532
    );
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
533
    
cleanup test
Yuki Kimoto authored on 2011-08-16
534
    return [sort {$a->{table} cmp $b->{table} } @$table_info];
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
535
}
536

            
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
537
sub get_column_info {
cleanup
Yuki Kimoto authored on 2011-10-21
538
    my ($self, %opt) = @_;
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
539
    
cleanup
Yuki Kimoto authored on 2011-10-21
540
    my $exclude_table = delete $opt{exclude_table};
541
    croak qq/"$_" is wrong option/ for keys %opt;
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
542
    
543
    my $column_info = [];
544
    $self->each_column(
545
        sub { push @$column_info, {table => $_[1], column => $_[2], info => $_[3] } },
546
        exclude_table => $exclude_table
547
    );
548
    
549
    return [
550
      sort {$a->{table} cmp $b->{table} || $a->{column} cmp $b->{column} }
cleanup
Yuki Kimoto authored on 2011-08-16
551
        @$column_info];
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
552
}
553

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
554
sub helper {
555
    my $self = shift;
556
    
557
    # Register method
558
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
559
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
560
    
561
    return $self;
562
}
563

            
cleanup
yuki-kimoto authored on 2010-10-17
564
sub insert {
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
565
    my $self = shift;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
566
    
cleanup
Yuki Kimoto authored on 2011-10-21
567
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
568
    my $param = @_ % 2 ? shift : undef;
cleanup
Yuki Kimoto authored on 2011-10-21
569
    my %opt = @_;
570
    warn "insert method param option is DEPRECATED" if $opt{param};
571
    $param ||= delete $opt{param} || {};
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
572
    
573
    # Timestamp
cleanup
Yuki Kimoto authored on 2011-10-21
574
    if ($opt{timestamp} && (my $insert_timestamp = $self->insert_timestamp)) {
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
575
        my $columns = $insert_timestamp->[0];
576
        $columns = [$columns] unless ref $columns eq 'ARRAY';
577
        my $value = $insert_timestamp->[1];
578
        $value = $value->() if ref $value eq 'CODE';
579
        $param->{$_} = $value for @$columns;
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
580
    }
cleanup
Yuki Kimoto authored on 2011-10-21
581
    
582
    # Merge id to parameter
583
    $param = $self->merge_param(
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
584
        $self->_id_to_param(delete $opt{id}, $opt{primary_key}), $param)
cleanup
Yuki Kimoto authored on 2011-10-21
585
      if defined $opt{id};
cleanup
Yuki Kimoto authored on 2011-10-21
586
    
cleanup
Yuki Kimoto authored on 2011-04-02
587
    # Insert statement
cleanup
Yuki Kimoto authored on 2011-10-21
588
    my $sql = "insert ";
cleanup
Yuki Kimoto authored on 2011-10-21
589
    $sql .= "$opt{prefix} " if defined $opt{prefix};
590
    $sql .= "into " . $self->_q($opt{table}) . " "
591
      . $self->values_clause($param, {wrap => $opt{wrap}}) . " ";
packaging one directory
yuki-kimoto authored on 2009-11-16
592
    
593
    # Execute query
cleanup
Yuki Kimoto authored on 2011-10-21
594
    return $self->execute($sql, $param, %opt);
packaging one directory
yuki-kimoto authored on 2009-11-16
595
}
596

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
597
sub insert_timestamp {
598
    my $self = shift;
599
    
600
    if (@_) {
601
        $self->{insert_timestamp} = [@_];
602
        
603
        return $self;
604
    }
605
    return $self->{insert_timestamp};
606
}
607

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
608
sub include_model {
609
    my ($self, $name_space, $model_infos) = @_;
610
    
cleanup
Yuki Kimoto authored on 2011-04-02
611
    # Name space
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
612
    $name_space ||= '';
cleanup
Yuki Kimoto authored on 2011-04-02
613
    
614
    # Get Model infomations
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
615
    unless ($model_infos) {
cleanup
Yuki Kimoto authored on 2011-04-02
616

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
617
        # Load name space module
cleanup
Yuki Kimoto authored on 2011-04-25
618
        croak qq{"$name_space" is invalid class name } . _subname
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
619
          if $name_space =~ /[^\w:]/;
620
        eval "use $name_space";
cleanup
Yuki Kimoto authored on 2011-04-25
621
        croak qq{Name space module "$name_space.pm" is needed. $@ }
622
            . _subname
improved error messages
Yuki Kimoto authored on 2011-04-18
623
          if $@;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
624
        
625
        # Search model modules
626
        my $path = $INC{"$name_space.pm"};
627
        $path =~ s/\.pm$//;
628
        opendir my $dh, $path
cleanup
Yuki Kimoto authored on 2011-04-25
629
          or croak qq{Can't open directory "$path": $! } . _subname
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
630
        $model_infos = [];
631
        while (my $module = readdir $dh) {
632
            push @$model_infos, $module
633
              if $module =~ s/\.pm$//;
634
        }
635
        close $dh;
636
    }
637
    
cleanup
Yuki Kimoto authored on 2011-04-02
638
    # Include models
cleanup
Yuki Kimoto authored on 2011-10-21
639
    for my $model_info (@$model_infos) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
640
        
cleanup
Yuki Kimoto authored on 2011-04-02
641
        # Load model
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
642
        my $model_class;
643
        my $model_name;
644
        my $model_table;
645
        if (ref $model_info eq 'HASH') {
646
            $model_class = $model_info->{class};
647
            $model_name  = $model_info->{name};
648
            $model_table = $model_info->{table};
649
            
650
            $model_name  ||= $model_class;
651
            $model_table ||= $model_name;
652
        }
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
653
        else { $model_class = $model_name = $model_table = $model_info }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
654
        my $mclass = "${name_space}::$model_class";
cleanup
Yuki Kimoto authored on 2011-04-25
655
        croak qq{"$mclass" is invalid class name } . _subname
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
656
          if $mclass =~ /[^\w:]/;
657
        unless ($mclass->can('isa')) {
658
            eval "use $mclass";
cleanup
Yuki Kimoto authored on 2011-04-25
659
            croak "$@ " . _subname if $@;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
660
        }
661
        
cleanup
Yuki Kimoto authored on 2011-04-02
662
        # Create model
cleanup
Yuki Kimoto authored on 2011-10-21
663
        my $opt = {};
664
        $opt->{model_class} = $mclass if $mclass;
665
        $opt->{name}        = $model_name if $model_name;
666
        $opt->{table}       = $model_table if $model_table;
667
        $self->create_model($opt);
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
668
    }
669
    
670
    return $self;
671
}
672

            
added EXPERIMENTAL like_valu...
Yuki Kimoto authored on 2011-09-16
673
sub like_value { sub { "%$_[0]%" } }
674

            
added DBIx::Custom::Mapper
Yuki Kimoto authored on 2011-08-26
675
sub mapper {
676
    my $self = shift;
677
    return DBIx::Custom::Mapper->new(@_);
678
}
679

            
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
680
sub merge_param {
681
    my ($self, @params) = @_;
682
    
cleanup
Yuki Kimoto authored on 2011-04-02
683
    # Merge parameters
fixed merge_param bug
Yuki Kimoto authored on 2011-05-23
684
    my $merge = {};
cleanup
Yuki Kimoto authored on 2011-10-21
685
    for my $param (@params) {
686
        for my $column (keys %$param) {
fixed merge_param bug
Yuki Kimoto authored on 2011-05-23
687
            my $param_is_array = ref $param->{$column} eq 'ARRAY' ? 1 : 0;
688
            
689
            if (exists $merge->{$column}) {
690
                $merge->{$column} = [$merge->{$column}]
691
                  unless ref $merge->{$column} eq 'ARRAY';
692
                push @{$merge->{$column}},
693
                  ref $param->{$column} ? @{$param->{$column}} : $param->{$column};
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
694
            }
695
            else {
fixed merge_param bug
Yuki Kimoto authored on 2011-05-23
696
                $merge->{$column} = $param->{$column};
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
697
            }
698
        }
699
    }
700
    
fixed merge_param bug
Yuki Kimoto authored on 2011-05-23
701
    return $merge;
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
702
}
703

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
704
sub model {
705
    my ($self, $name, $model) = @_;
706
    
cleanup
Yuki Kimoto authored on 2011-04-02
707
    # Set model
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
708
    if ($model) {
709
        $self->models->{$name} = $model;
710
        return $self;
711
    }
712
    
713
    # Check model existance
cleanup
Yuki Kimoto authored on 2011-04-25
714
    croak qq{Model "$name" is not included } . _subname
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
715
      unless $self->models->{$name};
716
    
cleanup
Yuki Kimoto authored on 2011-04-02
717
    # Get model
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
718
    return $self->models->{$name};
719
}
720

            
cleanup
Yuki Kimoto authored on 2011-03-21
721
sub mycolumn {
722
    my ($self, $table, $columns) = @_;
723
    
cleanup
Yuki Kimoto authored on 2011-04-02
724
    # Create column clause
725
    my @column;
cleanup
Yuki Kimoto authored on 2011-03-21
726
    $columns ||= [];
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
727
    push @column, $self->_q($table) . "." . $self->_q($_) .
728
      " as " . $self->_q($_)
729
      for @$columns;
cleanup
Yuki Kimoto authored on 2011-03-21
730
    
731
    return join (', ', @column);
732
}
733

            
added dbi_options attribute
kimoto authored on 2010-12-20
734
sub new {
735
    my $self = shift->SUPER::new(@_);
736
    
cleanup
Yuki Kimoto authored on 2011-04-02
737
    # Check attributes
added dbi_options attribute
kimoto authored on 2010-12-20
738
    my @attrs = keys %$self;
cleanup
Yuki Kimoto authored on 2011-10-21
739
    for my $attr (@attrs) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
740
        croak qq{Invalid attribute: "$attr" } . _subname
added dbi_options attribute
kimoto authored on 2010-12-20
741
          unless $self->can($attr);
742
    }
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
743

            
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
744
    # DEPRECATED
cleanup
Yuki Kimoto authored on 2011-08-13
745
    $self->{_tags} = {
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
746
        '?'     => \&DBIx::Custom::Tag::placeholder,
747
        '='     => \&DBIx::Custom::Tag::equal,
748
        '<>'    => \&DBIx::Custom::Tag::not_equal,
749
        '>'     => \&DBIx::Custom::Tag::greater_than,
750
        '<'     => \&DBIx::Custom::Tag::lower_than,
751
        '>='    => \&DBIx::Custom::Tag::greater_than_equal,
752
        '<='    => \&DBIx::Custom::Tag::lower_than_equal,
753
        'like'  => \&DBIx::Custom::Tag::like,
754
        'in'    => \&DBIx::Custom::Tag::in,
755
        'insert_param' => \&DBIx::Custom::Tag::insert_param,
756
        'update_param' => \&DBIx::Custom::Tag::update_param
cleanup
Yuki Kimoto authored on 2011-08-13
757
    };
758
    
759
    return $self;
760
}
761

            
- added EXPERIMENTAL pass at...
Yuki Kimoto authored on 2011-09-02
762
sub not_exists { DBIx::Custom::NotExists->singleton }
cleanup
Yuki Kimoto authored on 2011-08-13
763

            
764
sub order {
765
    my $self = shift;
766
    return DBIx::Custom::Order->new(dbi => $self, @_);
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
767
}
768

            
cleanup
yuki-kimoto authored on 2010-10-17
769
sub register_filter {
cleanup
Yuki Kimoto authored on 2011-04-02
770
    my $self = shift;
cleanup
yuki-kimoto authored on 2010-10-17
771
    
772
    # Register filter
773
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
cleanup
Yuki Kimoto authored on 2011-04-02
774
    $self->filters({%{$self->filters}, %$filters});
cleanup
yuki-kimoto authored on 2010-10-17
775
    
cleanup
Yuki Kimoto authored on 2011-04-02
776
    return $self;
cleanup
yuki-kimoto authored on 2010-10-17
777
}
packaging one directory
yuki-kimoto authored on 2009-11-16
778

            
779
sub select {
cleanup
Yuki Kimoto authored on 2011-10-21
780
    my ($self, %opt) = @_;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
781

            
cleanup
Yuki Kimoto authored on 2011-10-21
782
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
783
    my $tables = ref $opt{table} eq 'ARRAY' ? $opt{table}
784
               : defined $opt{table} ? [$opt{table}]
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
785
               : [];
cleanup
Yuki Kimoto authored on 2011-10-21
786
    $opt{table} = $tables;
cleanup
Yuki Kimoto authored on 2011-10-21
787
    my $where_param = $opt{where_param} || delete $opt{param} || {};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
788
    
cleanup
Yuki Kimoto authored on 2011-03-09
789
    # Add relation tables(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-10-21
790
    if ($opt{relation}) {
791
        warn "select() relation option is DEPRECATED!";
792
        $self->_add_relation_table($tables, $opt{relation});
793
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
794
    
cleanup
Yuki Kimoto authored on 2011-04-02
795
    # Select statement
micro optimization
Yuki Kimoto authored on 2011-09-30
796
    my $sql = 'select ';
packaging one directory
yuki-kimoto authored on 2009-11-16
797
    
added EXPERIMENTAL select pr...
Yuki Kimoto authored on 2011-06-13
798
    # Prefix
cleanup
Yuki Kimoto authored on 2011-10-21
799
    $sql .= "$opt{prefix} " if defined $opt{prefix};
added EXPERIMENTAL select pr...
Yuki Kimoto authored on 2011-06-13
800
    
cleanup
Yuki Kimoto authored on 2011-10-21
801
    # Column
cleanup
Yuki Kimoto authored on 2011-10-21
802
    if (defined $opt{column}) {
803
        my $columns
804
          = ref $opt{column} eq 'ARRAY' ? $opt{column} : [$opt{column}];
cleanup
Yuki Kimoto authored on 2011-10-21
805
        for my $column (@$columns) {
- select() column option can...
Yuki Kimoto authored on 2011-06-08
806
            if (ref $column eq 'HASH') {
EXPERIMTANL column method th...
Yuki Kimoto authored on 2011-06-13
807
                $column = $self->column(%$column) if ref $column eq 'HASH';
- select() column option can...
Yuki Kimoto authored on 2011-06-08
808
            }
809
            elsif (ref $column eq 'ARRAY') {
- select method column optio...
Yuki Kimoto authored on 2011-07-11
810
                if (@$column == 3 && $column->[1] eq 'as') {
811
                    warn "[COLUMN, as => ALIAS] is DEPRECATED! use [COLUMN => ALIAS]";
812
                    splice @$column, 1, 1;
813
                }
814
                
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
815
                $column = join(' ', $column->[0], 'as', $self->_q($column->[1]));
- select() column option can...
Yuki Kimoto authored on 2011-06-08
816
            }
cleanup
Yuki Kimoto authored on 2011-04-02
817
            unshift @$tables, @{$self->_search_tables($column)};
micro optimization
Yuki Kimoto authored on 2011-09-30
818
            $sql .= "$column, ";
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
819
        }
micro optimization
Yuki Kimoto authored on 2011-09-30
820
        $sql =~ s/, $/ /;
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
821
    }
micro optimization
Yuki Kimoto authored on 2011-09-30
822
    else { $sql .= '* ' }
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
823
    
824
    # Table
micro optimization
Yuki Kimoto authored on 2011-09-30
825
    $sql .= 'from ';
cleanup
Yuki Kimoto authored on 2011-10-21
826
    if ($opt{relation}) {
cleanup
Yuki Kimoto authored on 2011-03-30
827
        my $found = {};
cleanup
Yuki Kimoto authored on 2011-10-21
828
        for my $table (@$tables) {
micro optimization
Yuki Kimoto authored on 2011-09-30
829
            $sql .= $self->_q($table) . ', ' unless $found->{$table};
cleanup
Yuki Kimoto authored on 2011-03-30
830
            $found->{$table} = 1;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
831
        }
packaging one directory
yuki-kimoto authored on 2009-11-16
832
    }
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
833
    else { $sql .= $self->_q($tables->[-1] || '') . ' ' }
micro optimization
Yuki Kimoto authored on 2011-09-30
834
    $sql =~ s/, $/ /;
cleanup
Yuki Kimoto authored on 2011-10-21
835
    croak "select method table option must be specified " . _subname
improved error messages
Yuki Kimoto authored on 2011-04-18
836
      unless $tables->[-1];
cleanup
Yuki Kimoto authored on 2011-04-01
837

            
cleanup
Yuki Kimoto authored on 2011-04-02
838
    # Add tables in parameter
DEPRECATED select() param op...
Yuki Kimoto authored on 2011-04-25
839
    unshift @$tables,
840
            @{$self->_search_tables(join(' ', keys %$where_param) || '')};
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
841
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
842
    # Where
cleanup
Yuki Kimoto authored on 2011-10-21
843
    my $where = defined $opt{id}
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
844
              ? $self->_id_to_param(delete $opt{id}, $opt{primary_key}, $tables->[-1])
cleanup
Yuki Kimoto authored on 2011-10-21
845
              : $opt{where};
846
    my $w = $self->_where_clause_and_param($where, $where_param);
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
847
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
848
    # Add table names in where clause
cleanup
Yuki Kimoto authored on 2011-10-21
849
    unshift @$tables, @{$self->_search_tables($w->{clause})};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
850
    
cleanup
Yuki Kimoto authored on 2011-10-21
851
    # Join statement
cleanup
Yuki Kimoto authored on 2011-10-21
852
    $self->_push_join(\$sql, $opt{join}, $tables) if defined $opt{join};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
853
    
cleanup
Yuki Kimoto authored on 2011-03-09
854
    # Add where clause
cleanup
Yuki Kimoto authored on 2011-10-21
855
    $sql .= "$w->{clause} ";
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
856
    
cleanup
Yuki Kimoto authored on 2011-03-08
857
    # Relation(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-10-21
858
    $self->_push_relation(\$sql, $tables, $opt{relation}, $w->{clause} eq '' ? 1 : 0)
cleanup
Yuki Kimoto authored on 2011-10-21
859
      if $opt{relation};
cleanup
Yuki Kimoto authored on 2011-03-08
860
    
packaging one directory
yuki-kimoto authored on 2009-11-16
861
    # Execute query
cleanup
Yuki Kimoto authored on 2011-10-21
862
    my $result = $self->execute($sql, $w->{param}, %opt);
packaging one directory
yuki-kimoto authored on 2009-11-16
863
    
864
    return $result;
865
}
866

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
867
sub setup_model {
868
    my $self = shift;
869
    
cleanup
Yuki Kimoto authored on 2011-04-02
870
    # Setup model
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
871
    $self->each_column(
872
        sub {
873
            my ($self, $table, $column, $column_info) = @_;
874
            if (my $model = $self->models->{$table}) {
875
                push @{$model->columns}, $column;
876
            }
877
        }
878
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
879
    return $self;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
880
}
881

            
update pod
Yuki Kimoto authored on 2011-08-10
882
sub show_datatype {
883
    my ($self, $table) = @_;
884
    croak "Table name must be specified" unless defined $table;
885
    print "$table\n";
886
    
887
    my $result = $self->select(table => $table, where => "'0' <> '0'");
888
    my $sth = $result->sth;
889

            
890
    my $columns = $sth->{NAME};
891
    my $data_types = $sth->{TYPE};
892
    
893
    for (my $i = 0; $i < @$columns; $i++) {
894
        my $column = $columns->[$i];
895
        my $data_type = $data_types->[$i];
896
        print "$column: $data_type\n";
897
    }
898
}
899

            
900
sub show_typename {
901
    my ($self, $t) = @_;
902
    croak "Table name must be specified" unless defined $t;
903
    print "$t\n";
904
    
905
    $self->each_column(sub {
906
        my ($self, $table, $column, $infos) = @_;
907
        return unless $table eq $t;
908
        my $typename = $infos->{TYPE_NAME};
909
        print "$column: $typename\n";
910
    });
911
    
912
    return $self;
913
}
914

            
test cleanup
Yuki Kimoto authored on 2011-08-15
915
sub show_tables {
916
    my $self = shift;
917
    
918
    my %tables;
919
    $self->each_table(sub { $tables{$_[1]}++ });
920
    print join("\n", sort keys %tables) . "\n";
921
    return $self;
922
}
923

            
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
924
sub type_rule {
925
    my $self = shift;
micro optimization
Yuki Kimoto authored on 2011-10-23
926

            
927
    $self->{_type_rule_is_called} = 1;
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
928
    
929
    if (@_) {
changed type_rule arguments ...
Yuki Kimoto authored on 2011-06-12
930
        my $type_rule = ref $_[0] eq 'HASH' ? $_[0] : {@_};
fixed bug that type_rule fro...
Yuki Kimoto authored on 2011-06-13
931
        
932
        # Into
cleanup
Yuki Kimoto authored on 2011-10-21
933
        for my $i (1 .. 2) {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
934
            my $into = "into$i";
cleanup
Yuki Kimoto authored on 2011-08-16
935
            my $exists_into = exists $type_rule->{$into};
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
936
            $type_rule->{$into} = _array_to_hash($type_rule->{$into});
937
            $self->{type_rule} = $type_rule;
938
            $self->{"_$into"} = {};
cleanup
Yuki Kimoto authored on 2011-10-21
939
            for my $type_name (keys %{$type_rule->{$into} || {}}) {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
940
                croak qq{type name of $into section must be lower case}
941
                  if $type_name =~ /[A-Z]/;
942
            }
cleanup
Yuki Kimoto authored on 2011-08-16
943
            
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
944
            $self->each_column(sub {
945
                my ($dbi, $table, $column, $column_info) = @_;
946
                
947
                my $type_name = lc $column_info->{TYPE_NAME};
948
                if ($type_rule->{$into} &&
949
                    (my $filter = $type_rule->{$into}->{$type_name}))
type_rule can receive filter...
Yuki Kimoto authored on 2011-06-12
950
                {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
951
                    return unless exists $type_rule->{$into}->{$type_name};
952
                    if  (defined $filter && ref $filter ne 'CODE') 
953
                    {
954
                        my $fname = $filter;
955
                        croak qq{Filter "$fname" is not registered" } . _subname
956
                          unless exists $self->filters->{$fname};
957
                        
958
                        $filter = $self->filters->{$fname};
959
                    }
960

            
micro optimization
Yuki Kimoto authored on 2011-07-30
961
                    $self->{"_$into"}{key}{$table}{$column} = $filter;
962
                    $self->{"_$into"}{dot}{"$table.$column"} = $filter;
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
963
                }
964
            });
965
        }
966

            
967
        # From
cleanup
Yuki Kimoto authored on 2011-10-21
968
        for my $i (1 .. 2) {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
969
            $type_rule->{"from$i"} = _array_to_hash($type_rule->{"from$i"});
cleanup
Yuki Kimoto authored on 2011-10-21
970
            for my $data_type (keys %{$type_rule->{"from$i"} || {}}) {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
971
                croak qq{data type of from$i section must be lower case or number}
972
                  if $data_type =~ /[A-Z]/;
973
                my $fname = $type_rule->{"from$i"}{$data_type};
974
                if (defined $fname && ref $fname ne 'CODE') {
type_rule can receive filter...
Yuki Kimoto authored on 2011-06-12
975
                    croak qq{Filter "$fname" is not registered" } . _subname
976
                      unless exists $self->filters->{$fname};
977
                    
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
978
                    $type_rule->{"from$i"}{$data_type} = $self->filters->{$fname};
type_rule can receive filter...
Yuki Kimoto authored on 2011-06-12
979
                }
fixed bug that type_rule fro...
Yuki Kimoto authored on 2011-06-13
980
            }
981
        }
982
        
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
983
        return $self;
984
    }
985
    
986
    return $self->{type_rule} || {};
987
}
988

            
cleanup
yuki-kimoto authored on 2010-10-17
989
sub update {
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
990
    my $self = shift;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
991

            
cleanup
Yuki Kimoto authored on 2011-10-21
992
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
993
    my $param = @_ % 2 ? shift : undef;
cleanup
Yuki Kimoto authored on 2011-10-21
994
    my %opt = @_;
cleanup
Yuki Kimoto authored on 2011-10-21
995
    warn "update param option is DEPRECATED!" if $opt{param};
cleanup
Yuki Kimoto authored on 2011-10-21
996
    warn "update method where_param option is DEPRECATED!"
997
      if $opt{where_param};
cleanup
Yuki Kimoto authored on 2011-10-21
998
    $param ||= $opt{param} || {};
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
999
    
cleanup
Yuki Kimoto authored on 2011-10-21
1000
    # Don't allow update all rows
1001
    croak qq{update method where option must be specified } . _subname
1002
      if !$opt{where} && !defined $opt{id} && !$opt{allow_update_all};
1003
    
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
1004
    # Timestamp
cleanup
Yuki Kimoto authored on 2011-10-21
1005
    if ($opt{timestamp} && (my $update_timestamp = $self->update_timestamp)) {
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
1006
        my $columns = $update_timestamp->[0];
1007
        $columns = [$columns] unless ref $columns eq 'ARRAY';
1008
        my $value = $update_timestamp->[1];
1009
        $value = $value->() if ref $value eq 'CODE';
1010
        $param->{$_} = $value for @$columns;
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
1011
    }
1012

            
cleanup
Yuki Kimoto authored on 2011-10-21
1013
    # Assign clause
cleanup
Yuki Kimoto authored on 2011-10-21
1014
    my $assign_clause = $self->assign_clause($param, {wrap => $opt{wrap}});
cleanup
Yuki Kimoto authored on 2011-10-21
1015
    
1016
    # Convert id to where parameter
1017
    my $where = defined $opt{id}
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
1018
      ? $self->_id_to_param(delete $opt{id}, $opt{primary_key}, $opt{table})
cleanup
Yuki Kimoto authored on 2011-10-21
1019
      : $opt{where};
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
1020

            
1021
    # Where
cleanup
Yuki Kimoto authored on 2011-10-21
1022
    my $w = $self->_where_clause_and_param($where, $opt{where_param});
removed reconnect method
yuki-kimoto authored on 2010-05-28
1023
    
cleanup
Yuki Kimoto authored on 2011-10-21
1024
    # Merge where parameter to parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1025
    $param = $self->merge_param($param, $w->{param}) if keys %{$w->{param}};
DEPRECATED select() param op...
Yuki Kimoto authored on 2011-04-25
1026
    
cleanup
Yuki Kimoto authored on 2011-04-02
1027
    # Update statement
cleanup
Yuki Kimoto authored on 2011-10-21
1028
    my $sql = "update ";
1029
    $sql .= "$opt{prefix} " if defined $opt{prefix};
cleanup
Yuki Kimoto authored on 2011-10-21
1030
    $sql .= $self->_q($opt{table}) . " set $assign_clause $w->{clause} ";
cleanup
Yuki Kimoto authored on 2011-01-27
1031
    
cleanup
yuki-kimoto authored on 2010-10-17
1032
    # Execute query
cleanup
Yuki Kimoto authored on 2011-10-21
1033
    return $self->execute($sql, $param, %opt);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1034
}
1035

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

            
cleanup
Yuki Kimoto authored on 2011-10-21
1038
sub update_or_insert {
1039
    my $self = shift;
1040

            
cleanup
Yuki Kimoto authored on 2011-10-21
1041
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
1042
    my $param  = shift;
1043
    my %opt = @_;
1044
    my $id = $opt{id};
1045
    my $primary_key = $opt{primary_key};
1046
    $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY';
1047
    croak "update_or_insert method need primary_key option " .
1048
          "when id is specified" . _subname
1049
      if defined $id && !defined $primary_key;
1050
    my $table  = $opt{table};
1051
    croak qq{"table" option must be specified } . _subname
1052
      unless defined $table;
1053
    my $select_option = $opt{select_option};
1054
    
1055
    my $rows = $self->select(table => $table, id => $id,
1056
        primary_key => $primary_key, %$select_option)->all;
1057
    
1058
    croak "selected row count must be one or zero" . _subname
1059
      if @$rows > 1;
1060
    
1061
    my $row = $rows->[0];
1062
    my @opt = (table => $table);
1063
    push @opt, id => $id, primary_key => $primary_key if defined $id;
1064
    push @opt, %opt;
1065
    
1066
    if ($row) {
1067
        return $self->update($param, @opt);
1068
    }
1069
    else {
1070
        return $self->insert($param, @opt);
1071
    }
1072
}
1073

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
1074
sub update_timestamp {
1075
    my $self = shift;
1076
    
1077
    if (@_) {
1078
        $self->{update_timestamp} = [@_];
1079
        
1080
        return $self;
1081
    }
1082
    return $self->{update_timestamp};
1083
}
1084

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1085
sub values_clause {
1086
    my ($self, $param, $opts) = @_;
1087
    
1088
    my $wrap = $opts->{wrap} || {};
1089
    
1090
    # Create insert parameter tag
micro optimization
Yuki Kimoto authored on 2011-10-23
1091
    my $safety = $self->{safety_character} || $self->safety_character;
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1092
    my @columns;
1093
    my @placeholders;
improved performance
Yuki Kimoto authored on 2011-10-22
1094
    my $qp = $self->_q('');
fixed some quoted bug
Yuki Kimoto authored on 2011-10-22
1095
    my $q = substr($qp, 0, 1) || '';
1096
    my $p = substr($qp, 1, 1) || '';
improved performance
Yuki Kimoto authored on 2011-10-22
1097
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1098
    # Check unsafety keys
1099
    unless ((join('', keys %$param) || '') =~ /^[$safety\.]+$/) {
1100
        for my $column (keys %$param) {
1101
            croak qq{"$column" is not safety column name } . _subname
1102
              unless $column =~ /^[$safety\.]+$/;
1103
        }
1104
    }
1105

            
cleanup
Yuki Kimoto authored on 2011-10-21
1106
    for my $column (sort keys %$param) {
micro optimization
Yuki Kimoto authored on 2011-10-23
1107
        push @columns, "$q$column$p";
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1108
        push @placeholders,
micro optimization
Yuki Kimoto authored on 2011-10-23
1109
          ref $param->{$column} eq 'SCALAR' ? ${$param->{$column}} :
1110
          $wrap->{$column} ? $wrap->{$column}->(":$column") :
1111
          ":$column"
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1112
    }
1113
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1114
    '(' . join(', ', @columns) . ') ' . 'values ' .
1115
      '(' . join(', ', @placeholders) . ')'
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1116
}
1117

            
sub module use DBIx::Custom ...
Yuki Kimoto authored on 2011-08-02
1118
sub where { DBIx::Custom::Where->new(dbi => shift, @_) }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1119

            
updated pod
Yuki Kimoto authored on 2011-06-21
1120
sub _create_query {
cleanup
Yuki Kimoto authored on 2011-06-13
1121
    
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
1122
    my ($self, $source, $after_build_sql) = @_;
cleanup
Yuki Kimoto authored on 2011-06-13
1123
    
updated pod
Yuki Kimoto authored on 2011-06-21
1124
    # Cache
1125
    my $cache = $self->cache;
1126
    
1127
    # Query
1128
    my $query;
1129
    
1130
    # Get cached query
1131
    if ($cache) {
cleanup
Yuki Kimoto authored on 2011-06-13
1132
        
updated pod
Yuki Kimoto authored on 2011-06-21
1133
        # Get query
1134
        my $q = $self->cache_method->($self, $source);
cleanup
Yuki Kimoto authored on 2011-06-13
1135
        
updated pod
Yuki Kimoto authored on 2011-06-21
1136
        # Create query
1137
        if ($q) {
1138
            $query = DBIx::Custom::Query->new($q);
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
1139
            $query->{filters} = $self->filters;
cleanup
Yuki Kimoto authored on 2011-06-13
1140
        }
updated pod
Yuki Kimoto authored on 2011-06-21
1141
    }
1142
    
1143
    # Create query
1144
    unless ($query) {
1145

            
1146
        # Create query
1147
        my $builder = $self->query_builder;
1148
        $query = $builder->build_query($source);
1149

            
1150
        # Remove reserved word quote
1151
        if (my $q = $self->_quote) {
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
1152
            $q = quotemeta($q);
1153
            $_ =~ s/[$q]//g for @{$query->columns}
cleanup
Yuki Kimoto authored on 2011-06-13
1154
        }
updated pod
Yuki Kimoto authored on 2011-06-21
1155

            
1156
        # Save query to cache
1157
        $self->cache_method->(
1158
            $self, $source,
1159
            {
1160
                sql     => $query->sql, 
1161
                columns => $query->columns,
DBIx::Custom::Query tables a...
Yuki Kimoto authored on 2011-07-30
1162
                tables  => $query->{tables} || []
updated pod
Yuki Kimoto authored on 2011-06-21
1163
            }
1164
        ) if $cache;
cleanup
Yuki Kimoto authored on 2011-06-13
1165
    }
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
1166

            
1167
    # Filter SQL
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
1168
    if ($after_build_sql) {
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
1169
        my $sql = $query->sql;
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
1170
        $sql = $after_build_sql->($sql);
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
1171
        $query->sql($sql);
1172
    }
1173
        
added EXPERIMENTAL last_sql ...
Yuki Kimoto authored on 2011-07-11
1174
    # Save sql
1175
    $self->last_sql($query->sql);
1176
    
updated pod
Yuki Kimoto authored on 2011-06-21
1177
    # Prepare statement handle
1178
    my $sth;
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
1179
    eval { $sth = $self->dbh->prepare($query->{sql}) };
updated pod
Yuki Kimoto authored on 2011-06-21
1180
    
1181
    if ($@) {
1182
        $self->_croak($@, qq{. Following SQL is executed.\n}
1183
                        . qq{$query->{sql}\n} . _subname);
1184
    }
1185
    
1186
    # Set statement handle
1187
    $query->sth($sth);
1188
    
1189
    # Set filters
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
1190
    $query->{filters} = $self->filters;
updated pod
Yuki Kimoto authored on 2011-06-21
1191
    
1192
    return $query;
cleanup
Yuki Kimoto authored on 2011-06-13
1193
}
1194

            
cleanup
Yuki Kimoto authored on 2011-04-02
1195
sub _create_bind_values {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1196
    my ($self, $params, $columns, $filter, $type_filters, $bind_type) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1197
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1198
    $bind_type = _array_to_hash($bind_type) if ref $bind_type eq 'ARRAY';
1199
    
cleanup
Yuki Kimoto authored on 2011-04-02
1200
    # Create bind values
micro optimization
Yuki Kimoto authored on 2011-10-23
1201
    my @bind;
1202
    my @types;
1203
    my %count;
1204
    my %not_exists;
cleanup
Yuki Kimoto authored on 2011-10-21
1205
    for my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1206
        
micro optimization
Yuki Kimoto authored on 2011-10-23
1207
        # Bind value
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1208
        if(ref $params->{$column} eq 'ARRAY') {
micro optimization
Yuki Kimoto authored on 2011-10-23
1209
            my $i = $count{$column} || 0;
1210
            $i += $not_exists{$column} || 0;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1211
            my $found;
1212
            for (my $k = $i; $i < @{$params->{$column}}; $k++) {
1213
                if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
micro optimization
Yuki Kimoto authored on 2011-10-23
1214
                    $not_exists{$column}++;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1215
                }
1216
                else  {
micro optimization
Yuki Kimoto authored on 2011-10-23
1217
                    push @bind, $params->{$column}->[$k];
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1218
                    $found = 1;
1219
                    last
1220
                }
1221
            }
1222
            next unless $found;
1223
        }
micro optimization
Yuki Kimoto authored on 2011-10-23
1224
        else { push @bind, $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1225
        
cleanup
Yuki Kimoto authored on 2011-01-12
1226
        # Filter
micro optimization
Yuki Kimoto authored on 2011-10-23
1227
        if (my $f = $filter->{$column} || $self->{default_out_filter} || '') {
micro optimization
Yuki Kimoto authored on 2011-10-23
1228
            $bind[-1] = $f->($bind[-1]);
micro optimization
Yuki Kimoto authored on 2011-10-23
1229
        }
separate DBIx::Custom type_r...
Yuki Kimoto authored on 2011-06-15
1230
        
1231
        # Type rule
micro optimization
Yuki Kimoto authored on 2011-10-23
1232
        if ($self->{_type_rule_is_called}) {
1233
            my $tf1 = $self->{"_into1"}->{dot}->{$column}
1234
              || $type_filters->{1}->{$column};
micro optimization
Yuki Kimoto authored on 2011-10-23
1235
            $bind[-1] = $tf1->($bind[-1]) if $tf1;
micro optimization
Yuki Kimoto authored on 2011-10-23
1236
            my $tf2 = $self->{"_into2"}->{dot}->{$column}
1237
              || $type_filters->{2}->{$column};
micro optimization
Yuki Kimoto authored on 2011-10-23
1238
            $bind[-1] = $tf2->($bind[-1]) if $tf2;
micro optimization
Yuki Kimoto authored on 2011-10-23
1239
        }
micro optimization
Yuki Kimoto authored on 2011-10-22
1240
       
micro optimization
Yuki Kimoto authored on 2011-10-23
1241
        # Bind types
micro optimization
Yuki Kimoto authored on 2011-10-23
1242
        push @types, $bind_type->{$column};
removed reconnect method
yuki-kimoto authored on 2010-05-28
1243
        
1244
        # Count up 
micro optimization
Yuki Kimoto authored on 2011-10-23
1245
        $count{$column}++;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1246
    }
1247
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1248
    return (\@bind, \@types);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1249
}
1250

            
cleanup
Yuki Kimoto authored on 2011-10-21
1251
sub _id_to_param {
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
1252
    my ($self, $id, $primary_keys, $table) = @_;
cleanup
Yuki Kimoto authored on 2011-10-21
1253
    
1254
    # Check primary key
cleanup
Yuki Kimoto authored on 2011-10-21
1255
    croak "primary_key option " .
1256
          "must be specified with id option " . _subname
1257
      unless defined $primary_keys;
1258
    $primary_keys = [$primary_keys] unless ref $primary_keys eq 'ARRAY';
improved error messages
Yuki Kimoto authored on 2011-04-18
1259
    
cleanup
Yuki Kimoto authored on 2011-06-08
1260
    # Create parameter
1261
    my $param = {};
fixed small insert, update, ...
Yuki Kimoto authored on 2011-06-21
1262
    if (defined $id) {
cleanup
Yuki Kimoto authored on 2011-06-08
1263
        $id = [$id] unless ref $id;
1264
        croak qq{"id" must be constant value or array reference}
improved error messages
Yuki Kimoto authored on 2011-04-18
1265
            . " (" . (caller 1)[3] . ")"
cleanup
Yuki Kimoto authored on 2011-06-08
1266
          unless !ref $id || ref $id eq 'ARRAY';
1267
        croak qq{"id" must contain values same count as primary key}
improved error messages
Yuki Kimoto authored on 2011-04-18
1268
            . " (" . (caller 1)[3] . ")"
cleanup
Yuki Kimoto authored on 2011-06-08
1269
          unless @$primary_keys eq @$id;
improved error messages
Yuki Kimoto authored on 2011-04-18
1270
        for(my $i = 0; $i < @$primary_keys; $i ++) {
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
1271
           my $key = $primary_keys->[$i];
1272
           $key = "$table." . $key if $table;
1273
           $param->{$key} = $id->[$i];
improved error messages
Yuki Kimoto authored on 2011-04-18
1274
        }
1275
    }
1276
    
cleanup
Yuki Kimoto authored on 2011-06-08
1277
    return $param;
improved error messages
Yuki Kimoto authored on 2011-04-18
1278
}
1279

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1280
sub _connect {
1281
    my $self = shift;
1282
    
1283
    # Attributes
added warnings
Yuki Kimoto authored on 2011-06-07
1284
    my $dsn = $self->data_source;
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
1285
    warn "data_source is DEPRECATED!\n"
fixed bug that data_source D...
Yuki Kimoto authored on 2011-06-13
1286
      if $dsn;
added warnings
Yuki Kimoto authored on 2011-06-07
1287
    $dsn ||= $self->dsn;
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
1288
    croak qq{"dsn" must be specified } . _subname
1289
      unless $dsn;
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1290
    my $user        = $self->user;
1291
    my $password    = $self->password;
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1292
    my $option = $self->_option;
1293
    $option = {%{$self->default_option}, %$option};
cleanup
Yuki Kimoto authored on 2011-08-16
1294
    
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1295
    # Connect
cleanup
Yuki Kimoto authored on 2011-08-16
1296
    my $dbh;
1297
    eval {
1298
        $dbh = DBI->connect(
1299
            $dsn,
1300
            $user,
1301
            $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1302
            $option
cleanup
Yuki Kimoto authored on 2011-08-16
1303
        );
1304
    };
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1305
    
1306
    # Connect error
cleanup
Yuki Kimoto authored on 2011-04-25
1307
    croak "$@ " . _subname if $@;
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1308
    
1309
    return $dbh;
1310
}
1311

            
cleanup
yuki-kimoto authored on 2010-10-17
1312
sub _croak {
1313
    my ($self, $error, $append) = @_;
cleanup
Yuki Kimoto authored on 2011-04-02
1314
    
1315
    # Append
cleanup
yuki-kimoto authored on 2010-10-17
1316
    $append ||= "";
1317
    
1318
    # Verbose
1319
    if ($Carp::Verbose) { croak $error }
1320
    
1321
    # Not verbose
1322
    else {
1323
        
1324
        # Remove line and module infromation
1325
        my $at_pos = rindex($error, ' at ');
1326
        $error = substr($error, 0, $at_pos);
1327
        $error =~ s/\s+$//;
1328
        croak "$error$append";
1329
    }
1330
}
1331

            
prepare oracle test
Yuki Kimoto authored on 2011-08-15
1332
sub _driver { lc shift->{dbh}->{Driver}->{Name} }
1333

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1334
sub _need_tables {
1335
    my ($self, $tree, $need_tables, $tables) = @_;
1336
    
cleanup
Yuki Kimoto authored on 2011-04-02
1337
    # Get needed tables
cleanup
Yuki Kimoto authored on 2011-10-21
1338
    for my $table (@$tables) {
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1339
        if ($tree->{$table}) {
1340
            $need_tables->{$table} = 1;
1341
            $self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}])
1342
        }
1343
    }
1344
}
1345

            
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1346
sub _option {
1347
    my $self = shift;
1348
    my $option = {%{$self->dbi_options}, %{$self->dbi_option}, %{$self->option}};
1349
    warn "dbi_options is DEPRECATED! use option instead\n"
1350
      if keys %{$self->dbi_options};
1351
    warn "dbi_option is DEPRECATED! use option instead\n"
1352
      if keys %{$self->dbi_option};
1353
    return $option;
1354
}
1355

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1356
sub _push_join {
1357
    my ($self, $sql, $join, $join_tables) = @_;
1358
    
cleanup
Yuki Kimoto authored on 2011-10-21
1359
    $join = [$join] unless ref $join eq 'ARRAY';
cleanup
Yuki Kimoto authored on 2011-10-21
1360
    
cleanup
Yuki Kimoto authored on 2011-04-02
1361
    # No join
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1362
    return unless @$join;
1363
    
cleanup
Yuki Kimoto authored on 2011-04-02
1364
    # Push join clause
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1365
    my $tree = {};
1366
    for (my $i = 0; $i < @$join; $i++) {
1367
        
cleanup
Yuki Kimoto authored on 2011-07-28
1368
        # Arrange
added join new syntax
Yuki Kimoto authored on 2011-07-28
1369
        my $join_clause;;
1370
        my $option;
1371
        if (ref $join->[$i] eq 'HASH') {
1372
            $join_clause = $join->[$i]->{clause};
1373
            $option = {table => $join->[$i]->{table}};
1374
        }
1375
        else {
1376
            $join_clause = $join->[$i];
1377
            $option = {};
1378
        };
cleanup
Yuki Kimoto authored on 2011-07-28
1379

            
1380
        # Find tables in join clause
added join new syntax
Yuki Kimoto authored on 2011-07-28
1381
        my $table1;
1382
        my $table2;
1383
        if (my $table = $option->{table}) {
1384
            $table1 = $table->[0];
1385
            $table2 = $table->[1];
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1386
        }
cleanup
Yuki Kimoto authored on 2011-07-28
1387
        else {
1388
            my $q = $self->_quote;
1389
            my $j_clause = (split /\s+on\s+/, $join_clause)[-1];
1390
            $j_clause =~ s/'.+?'//g;
1391
            my $q_re = quotemeta($q);
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
1392
            $j_clause =~ s/[$q_re]//g;
improved join clause parsing
Yuki Kimoto authored on 2011-09-27
1393
            
1394
            my @j_clauses = reverse split /\s(and|on)\s/, $j_clause;
cleanup
Yuki Kimoto authored on 2011-07-28
1395
            my $c = $self->safety_character;
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1396
            my $join_re = qr/($c+)\.$c+[^$c].*?($c+)\.$c+/sm;
improved join clause parsing
Yuki Kimoto authored on 2011-09-27
1397
            for my $clause (@j_clauses) {
1398
                if ($clause =~ $join_re) {
1399
                    $table1 = $1;
1400
                    $table2 = $2;
1401
                    last;
1402
                }                
cleanup
Yuki Kimoto authored on 2011-07-28
1403
            }
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1404
        }
added join new syntax
Yuki Kimoto authored on 2011-07-28
1405
        croak qq{join clause must have two table name after "on" keyword. } .
1406
              qq{"$join_clause" is passed }  . _subname
1407
          unless defined $table1 && defined $table2;
1408
        croak qq{right side table of "$join_clause" must be unique }
1409
            . _subname
1410
          if exists $tree->{$table2};
1411
        croak qq{Same table "$table1" is specified} . _subname
1412
          if $table1 eq $table2;
1413
        $tree->{$table2}
1414
          = {position => $i, parent => $table1, join => $join_clause};
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1415
    }
1416
    
cleanup
Yuki Kimoto authored on 2011-04-02
1417
    # Search need tables
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1418
    my $need_tables = {};
1419
    $self->_need_tables($tree, $need_tables, $join_tables);
cleanup
Yuki Kimoto authored on 2011-10-21
1420
    my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} }
1421
      keys %$need_tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1422
    
1423
    # Add join clause
cleanup
Yuki Kimoto authored on 2011-10-21
1424
    $$sql .= $tree->{$_}{join} . ' ' for @need_tables;
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1425
}
cleanup
Yuki Kimoto authored on 2011-03-08
1426

            
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1427
sub _quote {
1428
    my $self = shift;
micro optimization
Yuki Kimoto authored on 2011-10-22
1429
    return $self->{reserved_word_quote} || $self->quote || '';
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1430
}
1431

            
cleanup
Yuki Kimoto authored on 2011-07-29
1432
sub _q {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
1433
    my ($self, $value, $quotemeta) = @_;
cleanup
Yuki Kimoto authored on 2011-07-29
1434
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1435
    my $quote = $self->{reserved_word_quote}
1436
      || $self->{quote} || $self->quote || '';
1437
    return "$quote$value$quote"
1438
      if !$quotemeta && ($quote eq '`' || $quote eq '"');
1439
    
cleanup
Yuki Kimoto authored on 2011-07-29
1440
    my $q = substr($quote, 0, 1) || '';
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
1441
    my $p;
1442
    if (defined $quote && length $quote > 1) {
1443
        $p = substr($quote, 1, 1);
1444
    }
1445
    else { $p = $q }
cleanup
Yuki Kimoto authored on 2011-07-29
1446
    
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
1447
    if ($quotemeta) {
1448
        $q = quotemeta($q);
1449
        $p = quotemeta($p);
1450
    }
1451
    
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
1452
    return "$q$value$p";
cleanup
Yuki Kimoto authored on 2011-07-29
1453
}
1454

            
cleanup
Yuki Kimoto authored on 2011-04-02
1455
sub _remove_duplicate_table {
1456
    my ($self, $tables, $main_table) = @_;
1457
    
1458
    # Remove duplicate table
1459
    my %tables = map {defined $_ ? ($_ => 1) : ()} @$tables;
1460
    delete $tables{$main_table} if $main_table;
1461
    
micro optimization
Yuki Kimoto authored on 2011-07-30
1462
    my $new_tables = [keys %tables, $main_table ? $main_table : ()];
1463
    if (my $q = $self->_quote) {
1464
        $q = quotemeta($q);
1465
        $_ =~ s/[$q]//g for @$new_tables;
1466
    }
1467

            
1468
    return $new_tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1469
}
1470

            
cleanup
Yuki Kimoto authored on 2011-04-02
1471
sub _search_tables {
cleanup
Yuki Kimoto authored on 2011-04-02
1472
    my ($self, $source) = @_;
1473
    
cleanup
Yuki Kimoto authored on 2011-04-02
1474
    # Search tables
cleanup
Yuki Kimoto authored on 2011-04-02
1475
    my $tables = [];
1476
    my $safety_character = $self->safety_character;
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1477
    my $q = $self->_quote;
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
1478
    my $quoted_safety_character_re = $self->_q("?([$safety_character]+)", 1);
1479
    my $table_re = $q ? qr/(?:^|[^$safety_character])${quoted_safety_character_re}?\./
improved table search in col...
Yuki Kimoto authored on 2011-04-12
1480
                      : qr/(?:^|[^$safety_character])([$safety_character]+)\./;
cleanup
Yuki Kimoto authored on 2011-04-02
1481
    while ($source =~ /$table_re/g) {
1482
        push @$tables, $1;
1483
    }
1484
    
1485
    return $tables;
1486
}
1487

            
cleanup
Yuki Kimoto authored on 2011-04-02
1488
sub _where_to_obj {
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1489
    my ($self, $where) = @_;
1490
    
cleanup
Yuki Kimoto authored on 2011-04-02
1491
    my $obj;
1492
    
1493
    # Hash
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1494
    if (ref $where eq 'HASH') {
1495
        my $clause = ['and'];
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1496
        my $q = $self->_quote;
cleanup
Yuki Kimoto authored on 2011-10-21
1497
        for my $column (keys %$where) {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
1498
            my $table;
1499
            my $c;
1500
            if ($column =~ /(?:(.*?)\.)?(.*)/) {
1501
                $table = $1;
1502
                $c = $2;
1503
            }
1504
            
1505
            my $table_quote;
1506
            $table_quote = $self->_q($table) if defined $table;
1507
            my $column_quote = $self->_q($c);
1508
            $column_quote = $table_quote . '.' . $column_quote
1509
              if defined $table_quote;
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1510
            push @$clause, "$column_quote = :$column" for keys %$where;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1511
        }
cleanup
Yuki Kimoto authored on 2011-04-02
1512
        $obj = $self->where(clause => $clause, param => $where);
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1513
    }
cleanup
Yuki Kimoto authored on 2011-04-02
1514
    
1515
    # DBIx::Custom::Where object
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1516
    elsif (ref $where eq 'DBIx::Custom::Where') {
cleanup
Yuki Kimoto authored on 2011-04-02
1517
        $obj = $where;
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1518
    }
cleanup
Yuki Kimoto authored on 2011-04-02
1519
    
updated pod
Yuki Kimoto authored on 2011-06-21
1520
    # Array
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1521
    elsif (ref $where eq 'ARRAY') {
cleanup
Yuki Kimoto authored on 2011-04-02
1522
        $obj = $self->where(
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1523
            clause => $where->[0],
1524
            param  => $where->[1]
1525
        );
1526
    }
1527
    
cleanup
Yuki Kimoto authored on 2011-04-02
1528
    # Check where argument
improved error messages
Yuki Kimoto authored on 2011-04-18
1529
    croak qq{"where" must be hash reference or DBIx::Custom::Where object}
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
1530
        . qq{or array reference, which contains where clause and parameter}
cleanup
Yuki Kimoto authored on 2011-04-25
1531
        . _subname
cleanup
Yuki Kimoto authored on 2011-04-02
1532
      unless ref $obj eq 'DBIx::Custom::Where';
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1533
    
cleanup
Yuki Kimoto authored on 2011-04-02
1534
    return $obj;
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1535
}
1536

            
cleanup
Yuki Kimoto authored on 2011-10-21
1537
sub _where_clause_and_param {
1538
    my ($self, $where, $param) = @_;
1539
 
1540
    $where ||= {};
1541
    $param ||= {};
1542
    my $w = {};
1543
    my $where_clause = '';
1544
    if (ref $where eq 'ARRAY' && !ref $where->[0]) {
1545
        $w->{clause} = "where " . $where->[0];
1546
        $w->{param} = $where->[1];
1547
    }
1548
    elsif (ref $where) {
1549
        $where = $self->_where_to_obj($where);
1550
        $w->{param} = keys %$param
1551
                    ? $self->merge_param($param, $where->param)
1552
                    : $where->param;
1553
        $w->{clause} = $where->to_string;
1554
    }
1555
    elsif ($where) {
1556
        $w->{clause} = "where $where";
1557
        $w->{param} = $param;
1558
    }
1559
    
1560
    return $w;
1561
}
1562

            
updated pod
Yuki Kimoto authored on 2011-06-21
1563
sub _apply_filter {
1564
    my ($self, $table, @cinfos) = @_;
1565

            
1566
    # Initialize filters
1567
    $self->{filter} ||= {};
micro optimization
Yuki Kimoto authored on 2011-07-30
1568
    $self->{filter}{on} = 1;
updated pod
Yuki Kimoto authored on 2011-06-21
1569
    $self->{filter}{out} ||= {};
1570
    $self->{filter}{in} ||= {};
1571
    $self->{filter}{end} ||= {};
1572
    
1573
    # Usage
1574
    my $usage = "Usage: \$dbi->apply_filter(" .
1575
                "TABLE, COLUMN1, {in => INFILTER1, out => OUTFILTER1, end => ENDFILTER1}, " .
1576
                "COLUMN2, {in => INFILTER2, out => OUTFILTER2, end => ENDFILTER2}, ...)";
1577
    
1578
    # Apply filter
1579
    for (my $i = 0; $i < @cinfos; $i += 2) {
1580
        
1581
        # Column
1582
        my $column = $cinfos[$i];
1583
        if (ref $column eq 'ARRAY') {
cleanup
Yuki Kimoto authored on 2011-10-21
1584
            for my $c (@$column) {
updated pod
Yuki Kimoto authored on 2011-06-21
1585
                push @cinfos, $c, $cinfos[$i + 1];
1586
            }
1587
            next;
1588
        }
1589
        
1590
        # Filter infomation
1591
        my $finfo = $cinfos[$i + 1] || {};
1592
        croak "$usage (table: $table) " . _subname
1593
          unless  ref $finfo eq 'HASH';
cleanup
Yuki Kimoto authored on 2011-10-21
1594
        for my $ftype (keys %$finfo) {
updated pod
Yuki Kimoto authored on 2011-06-21
1595
            croak "$usage (table: $table) " . _subname
1596
              unless $ftype eq 'in' || $ftype eq 'out' || $ftype eq 'end'; 
1597
        }
1598
        
1599
        # Set filters
cleanup
Yuki Kimoto authored on 2011-10-21
1600
        for my $way (qw/in out end/) {
updated pod
Yuki Kimoto authored on 2011-06-21
1601
        
1602
            # Filter
1603
            my $filter = $finfo->{$way};
1604
            
1605
            # Filter state
1606
            my $state = !exists $finfo->{$way} ? 'not_exists'
1607
                      : !defined $filter        ? 'not_defined'
1608
                      : ref $filter eq 'CODE'   ? 'code'
1609
                      : 'name';
1610
            
1611
            # Filter is not exists
1612
            next if $state eq 'not_exists';
1613
            
1614
            # Check filter name
1615
            croak qq{Filter "$filter" is not registered } . _subname
1616
              if  $state eq 'name'
1617
               && ! exists $self->filters->{$filter};
1618
            
1619
            # Set filter
1620
            my $f = $state eq 'not_defined' ? undef
1621
                  : $state eq 'code'        ? $filter
1622
                  : $self->filters->{$filter};
1623
            $self->{filter}{$way}{$table}{$column} = $f;
1624
            $self->{filter}{$way}{$table}{"$table.$column"} = $f;
1625
            $self->{filter}{$way}{$table}{"${table}__$column"} = $f;
1626
            $self->{filter}{$way}{$table}{"${table}-$column"} = $f;
1627
        }
1628
    }
1629
    
1630
    return $self;
1631
}
1632

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1633
# DEPRECATED!
1634
has 'data_source';
1635
has dbi_options => sub { {} };
1636
has filter_check  => 1;
1637
has 'reserved_word_quote';
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1638
has dbi_option => sub { {} };
1639
has default_dbi_option => sub {
1640
    warn "default_dbi_option is DEPRECATED! use default_option instead";
1641
    return shift->default_option;
1642
};
1643

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1644
# DEPRECATED!
1645
sub method {
1646
    warn "method is DEPRECATED! use helper instead";
1647
    return shift->helper(@_);
1648
}
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1649

            
1650
# DEPRECATED!
1651
sub assign_param {
1652
    my $self = shift;
1653
    warn "assing_param is DEPRECATED! use assign_clause instead";
1654
    return $self->assign_clause(@_);
1655
}
1656

            
1657
# DEPRECATED
1658
sub update_param {
1659
    my ($self, $param, $opts) = @_;
1660
    
1661
    warn "update_param is DEPRECATED! use assing_clause instead.";
1662
    
1663
    # Create update parameter tag
1664
    my $tag = $self->assign_clause($param, $opts);
1665
    $tag = "set $tag" unless $opts->{no_set};
1666

            
1667
    return $tag;
1668
}
1669

            
updated pod
Yuki Kimoto authored on 2011-06-21
1670
# DEPRECATED!
1671
sub create_query {
1672
    warn "create_query is DEPRECATED! use query option of each method";
1673
    shift->_create_query(@_);
1674
}
1675

            
cleanup
Yuki Kimoto authored on 2011-06-13
1676
# DEPRECATED!
1677
sub apply_filter {
1678
    my $self = shift;
1679
    
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
1680
    warn "apply_filter is DEPRECATED!";
cleanup
Yuki Kimoto authored on 2011-06-13
1681
    return $self->_apply_filter(@_);
1682
}
1683

            
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1684
# DEPRECATED!
1685
sub select_at {
cleanup
Yuki Kimoto authored on 2011-10-21
1686
    my ($self, %opt) = @_;
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1687

            
cleanup
Yuki Kimoto authored on 2011-10-21
1688
    warn "select_at is DEPRECATED! use select method id option instead";
updated pod
Yuki Kimoto authored on 2011-06-08
1689

            
cleanup
Yuki Kimoto authored on 2011-10-21
1690
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
1691
    my $primary_keys = delete $opt{primary_key};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1692
    $primary_keys = [$primary_keys] unless ref $primary_keys;
cleanup
Yuki Kimoto authored on 2011-10-21
1693
    my $where = delete $opt{where};
1694
    my $param = delete $opt{param};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1695
    
1696
    # Table
1697
    croak qq{"table" option must be specified } . _subname
cleanup
Yuki Kimoto authored on 2011-10-21
1698
      unless $opt{table};
1699
    my $table = ref $opt{table} ? $opt{table}->[-1] : $opt{table};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1700
    
1701
    # Create where parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1702
    my $where_param = $self->_id_to_param($where, $primary_keys);
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1703
    
cleanup
Yuki Kimoto authored on 2011-10-21
1704
    return $self->select(where => $where_param, %opt);
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1705
}
1706

            
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1707
# DEPRECATED!
1708
sub delete_at {
cleanup
Yuki Kimoto authored on 2011-10-21
1709
    my ($self, %opt) = @_;
updated pod
Yuki Kimoto authored on 2011-06-08
1710

            
cleanup
Yuki Kimoto authored on 2011-10-21
1711
    warn "delete_at is DEPRECATED! use delete method id option instead";
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1712
    
cleanup
Yuki Kimoto authored on 2011-10-21
1713
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
1714
    my $primary_keys = delete $opt{primary_key};
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1715
    $primary_keys = [$primary_keys] unless ref $primary_keys;
cleanup
Yuki Kimoto authored on 2011-10-21
1716
    my $where = delete $opt{where};
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1717
    
1718
    # Create where parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1719
    my $where_param = $self->_id_to_param($where, $primary_keys);
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1720
    
cleanup
Yuki Kimoto authored on 2011-10-21
1721
    return $self->delete(where => $where_param, %opt);
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1722
}
1723

            
cleanup
Yuki Kimoto authored on 2011-06-08
1724
# DEPRECATED!
1725
sub update_at {
1726
    my $self = shift;
1727

            
cleanup
Yuki Kimoto authored on 2011-10-21
1728
    warn "update_at is DEPRECATED! use update method id option instead";
cleanup
Yuki Kimoto authored on 2011-06-08
1729
    
cleanup
Yuki Kimoto authored on 2011-10-21
1730
    # Options
cleanup
Yuki Kimoto authored on 2011-06-08
1731
    my $param;
1732
    $param = shift if @_ % 2;
cleanup
Yuki Kimoto authored on 2011-10-21
1733
    my %opt = @_;
1734
    my $primary_keys = delete $opt{primary_key};
cleanup
Yuki Kimoto authored on 2011-06-08
1735
    $primary_keys = [$primary_keys] unless ref $primary_keys;
cleanup
Yuki Kimoto authored on 2011-10-21
1736
    my $where = delete $opt{where};
1737
    my $p = delete $opt{param} || {};
cleanup
Yuki Kimoto authored on 2011-06-08
1738
    $param  ||= $p;
1739
    
1740
    # Create where parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1741
    my $where_param = $self->_id_to_param($where, $primary_keys);
cleanup
Yuki Kimoto authored on 2011-06-08
1742
    
cleanup
Yuki Kimoto authored on 2011-10-21
1743
    return $self->update(where => $where_param, param => $param, %opt);
cleanup
Yuki Kimoto authored on 2011-06-08
1744
}
1745

            
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1746
# DEPRECATED!
1747
sub insert_at {
1748
    my $self = shift;
1749
    
cleanup
Yuki Kimoto authored on 2011-10-21
1750
    warn "insert_at is DEPRECATED! use insert method id option instead";
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1751
    
cleanup
Yuki Kimoto authored on 2011-10-21
1752
    # Options
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1753
    my $param;
1754
    $param = shift if @_ % 2;
cleanup
Yuki Kimoto authored on 2011-10-21
1755
    my %opt = @_;
1756
    my $primary_key = delete $opt{primary_key};
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1757
    $primary_key = [$primary_key] unless ref $primary_key;
cleanup
Yuki Kimoto authored on 2011-10-21
1758
    my $where = delete $opt{where};
1759
    my $p = delete $opt{param} || {};
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1760
    $param  ||= $p;
1761
    
1762
    # Create where parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1763
    my $where_param = $self->_id_to_param($where, $primary_key);
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1764
    $param = $self->merge_param($where_param, $param);
1765
    
cleanup
Yuki Kimoto authored on 2011-10-21
1766
    return $self->insert(param => $param, %opt);
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1767
}
1768

            
added warnings
Yuki Kimoto authored on 2011-06-07
1769
# DEPRECATED!
1770
sub register_tag {
test cleanup
Yuki Kimoto authored on 2011-08-10
1771
    my $self = shift;
1772
    
added warnings
Yuki Kimoto authored on 2011-06-07
1773
    warn "register_tag is DEPRECATED!";
test cleanup
Yuki Kimoto authored on 2011-08-10
1774
    
1775
    # Merge tag
1776
    my $tags = ref $_[0] eq 'HASH' ? $_[0] : {@_};
1777
    $self->{_tags} = {%{$self->{_tags} || {}}, %$tags};
1778
    
1779
    return $self;
1780
}
1781

            
1782
# DEPRECATED!
1783
sub register_tag_processor {
1784
    my $self = shift;
1785
    warn "register_tag_processor is DEPRECATED!";
1786
    # Merge tag
1787
    my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_};
1788
    $self->{_tags} = {%{$self->{_tags} || {}}, %{$tag_processors}};
1789
    return $self;
added warnings
Yuki Kimoto authored on 2011-06-07
1790
}
1791

            
cleanup
Yuki Kimoto authored on 2011-01-25
1792
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1793
sub default_bind_filter {
1794
    my $self = shift;
1795
    
cleanup
Yuki Kimoto authored on 2011-06-13
1796
    warn "default_bind_filter is DEPRECATED!";
added warnings
Yuki Kimoto authored on 2011-06-07
1797
    
cleanup
Yuki Kimoto authored on 2011-01-12
1798
    if (@_) {
1799
        my $fname = $_[0];
1800
        
1801
        if (@_ && !$fname) {
1802
            $self->{default_out_filter} = undef;
1803
        }
1804
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1805
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1806
              unless exists $self->filters->{$fname};
1807
        
1808
            $self->{default_out_filter} = $self->filters->{$fname};
1809
        }
1810
        return $self;
1811
    }
1812
    
1813
    return $self->{default_out_filter};
1814
}
1815

            
cleanup
Yuki Kimoto authored on 2011-01-25
1816
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1817
sub default_fetch_filter {
1818
    my $self = shift;
added warnings
Yuki Kimoto authored on 2011-06-07
1819

            
cleanup
Yuki Kimoto authored on 2011-06-13
1820
    warn "default_fetch_filter is DEPRECATED!";
cleanup
Yuki Kimoto authored on 2011-01-12
1821
    
1822
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
1823
        my $fname = $_[0];
1824

            
cleanup
Yuki Kimoto authored on 2011-01-12
1825
        if (@_ && !$fname) {
1826
            $self->{default_in_filter} = undef;
1827
        }
1828
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1829
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1830
              unless exists $self->filters->{$fname};
1831
        
1832
            $self->{default_in_filter} = $self->filters->{$fname};
1833
        }
1834
        
1835
        return $self;
1836
    }
1837
    
many changed
Yuki Kimoto authored on 2011-01-23
1838
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
1839
}
1840

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1841
# DEPRECATED!
1842
sub insert_param {
1843
    my $self = shift;
1844
    warn "insert_param is DEPRECATED! use values_clause instead";
1845
    return $self->values_clause(@_);
1846
}
1847

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1848
# DEPRECATED!
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1849
sub insert_param_tag {
1850
    warn "insert_param_tag is DEPRECATED! " .
1851
         "use insert_param instead!";
1852
    return shift->insert_param(@_);
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1853
}
1854

            
1855
# DEPRECATED!
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1856
sub update_param_tag {
fixed DEPRECATED message bug
Yuki Kimoto authored on 2011-06-10
1857
    warn "update_param_tag is DEPRECATED! " .
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1858
         "use update_param instead";
1859
    return shift->update_param(@_);
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1860
}
cleanup
Yuki Kimoto authored on 2011-03-08
1861
# DEPRECATED!
1862
sub _push_relation {
1863
    my ($self, $sql, $tables, $relation, $need_where) = @_;
1864
    
1865
    if (keys %{$relation || {}}) {
micro optimization
Yuki Kimoto authored on 2011-09-30
1866
        $$sql .= $need_where ? 'where ' : 'and ';
cleanup
Yuki Kimoto authored on 2011-10-21
1867
        for my $rcolumn (keys %$relation) {
cleanup
Yuki Kimoto authored on 2011-03-08
1868
            my $table1 = (split (/\./, $rcolumn))[0];
1869
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1870
            push @$tables, ($table1, $table2);
micro optimization
Yuki Kimoto authored on 2011-09-30
1871
            $$sql .= "$rcolumn = " . $relation->{$rcolumn} .  'and ';
cleanup
Yuki Kimoto authored on 2011-03-08
1872
        }
1873
    }
micro optimization
Yuki Kimoto authored on 2011-09-30
1874
    $$sql =~ s/and $/ /;
cleanup
Yuki Kimoto authored on 2011-03-08
1875
}
1876

            
1877
# DEPRECATED!
1878
sub _add_relation_table {
cleanup
Yuki Kimoto authored on 2011-03-09
1879
    my ($self, $tables, $relation) = @_;
cleanup
Yuki Kimoto authored on 2011-03-08
1880
    
1881
    if (keys %{$relation || {}}) {
cleanup
Yuki Kimoto authored on 2011-10-21
1882
        for my $rcolumn (keys %$relation) {
cleanup
Yuki Kimoto authored on 2011-03-08
1883
            my $table1 = (split (/\./, $rcolumn))[0];
1884
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1885
            my $table1_exists;
1886
            my $table2_exists;
cleanup
Yuki Kimoto authored on 2011-10-21
1887
            for my $table (@$tables) {
cleanup
Yuki Kimoto authored on 2011-03-08
1888
                $table1_exists = 1 if $table eq $table1;
1889
                $table2_exists = 1 if $table eq $table2;
1890
            }
1891
            unshift @$tables, $table1 unless $table1_exists;
1892
            unshift @$tables, $table2 unless $table2_exists;
1893
        }
1894
    }
1895
}
1896

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1899
=head1 NAME
1900

            
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
1901
DBIx::Custom - DBI extension to execute insert, update, delete, and select easily
removed reconnect method
yuki-kimoto authored on 2010-05-28
1902

            
fix heading typos
Terrence Brannon authored on 2011-08-17
1903
=head1 SYNOPSIS
cleanup
yuki-kimoto authored on 2010-08-05
1904

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1905
    use DBIx::Custom;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1906
    
1907
    # Connect
1908
    my $dbi = DBIx::Custom->connect(
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
1909
        dsn => "dbi:mysql:database=dbname",
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1910
        user => 'ken',
1911
        password => '!LFKD%$&',
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1912
        option => {mysql_enable_utf8 => 1}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1913
    );
cleanup
yuki-kimoto authored on 2010-08-05
1914

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1915
    # Insert 
updated pod
Yuki Kimoto authored on 2011-06-21
1916
    $dbi->insert({title => 'Perl', author => 'Ken'}, table  => 'book');
removed reconnect method
yuki-kimoto authored on 2010-05-28
1917
    
1918
    # Update 
updated pod
Yuki Kimoto authored on 2011-06-21
1919
    $dbi->update({title => 'Perl', author => 'Ken'}, table  => 'book',
1920
      where  => {id => 5});
removed reconnect method
yuki-kimoto authored on 2010-05-28
1921
    
1922
    # Delete
updated pod
Yuki Kimoto authored on 2011-06-21
1923
    $dbi->delete(table  => 'book', where => {author => 'Ken'});
cleanup
yuki-kimoto authored on 2010-08-05
1924

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1925
    # Select
updated pod
Yuki Kimoto authored on 2011-06-21
1926
    my $result = $dbi->select(table  => 'book',
1927
      column => ['title', 'author'], where  => {author => 'Ken'});
cleanup
yuki-kimoto authored on 2010-08-05
1928

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1929
    # Select, more complex
1930
    my $result = $dbi->select(
1931
        table  => 'book',
1932
        column => [
cleanup
Yuki Kimoto authored on 2011-06-13
1933
            {book => [qw/title author/]},
1934
            {company => ['name']}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1935
        ],
1936
        where  => {'book.author' => 'Ken'},
1937
        join => ['left outer join company on book.company_id = company.id'],
1938
        append => 'order by id limit 5'
removed reconnect method
yuki-kimoto authored on 2010-05-28
1939
    );
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1940
    
removed register_format()
yuki-kimoto authored on 2010-05-26
1941
    # Fetch
1942
    while (my $row = $result->fetch) {
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1943
        
removed register_format()
yuki-kimoto authored on 2010-05-26
1944
    }
1945
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1946
    # Fetch as hash
removed register_format()
yuki-kimoto authored on 2010-05-26
1947
    while (my $row = $result->fetch_hash) {
1948
        
1949
    }
1950
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1951
    # Execute SQL with parameter.
1952
    $dbi->execute(
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1953
        "select id from book where author = :author and title like :title",
updated pod
Yuki Kimoto authored on 2011-06-21
1954
        {author => 'ken', title => '%Perl%'}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1955
    );
1956
    
fix heading typos
Terrence Brannon authored on 2011-08-17
1957
=head1 DESCRIPTION
removed reconnect method
yuki-kimoto authored on 2010-05-28
1958

            
cleanup
Yuki Kimoto authored on 2011-07-29
1959
L<DBIx::Custom> is L<DBI> wrapper module to execute SQL easily.
updated pod
Yuki Kimoto authored on 2011-06-21
1960
This module have the following features.
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1961

            
updated pod
Yuki Kimoto authored on 2011-06-21
1962
=over 4
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1963

            
cleanup
Yuki Kimoto authored on 2011-07-29
1964
=item *
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1965

            
cleanup
Yuki Kimoto authored on 2011-07-29
1966
Execute C<insert>, C<update>, C<delete>, or C<select> statement easily
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1967

            
cleanup
Yuki Kimoto authored on 2011-07-29
1968
=item *
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1969

            
cleanup
Yuki Kimoto authored on 2011-07-29
1970
Create C<where> clause flexibly
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1971

            
cleanup
Yuki Kimoto authored on 2011-07-29
1972
=item *
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1973

            
micro optimization
Yuki Kimoto authored on 2011-07-30
1974
Named place holder support
1975

            
1976
=item *
1977

            
cleanup
Yuki Kimoto authored on 2011-07-29
1978
Model support
1979

            
1980
=item *
1981

            
1982
Connection manager support
1983

            
1984
=item *
pod fix
Yuki Kimoto authored on 2011-01-21
1985

            
cleanup
Yuki Kimoto authored on 2011-07-30
1986
Choice your favorite relational database management system,
cleanup
Yuki Kimoto authored on 2011-07-29
1987
C<MySQL>, C<SQLite>, C<PostgreSQL>, C<Oracle>,
1988
C<Microsoft SQL Server>, C<Microsoft Access>, C<DB2> or anything, 
1989

            
1990
=item *
1991

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
1992
Filtering by data type or column name
cleanup
Yuki Kimoto authored on 2011-07-29
1993

            
1994
=item *
1995

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
1996
Create C<order by> clause flexibly
cleanup
Yuki Kimoto authored on 2011-07-29
1997

            
1998
=back
pod fix
Yuki Kimoto authored on 2011-01-21
1999

            
fix heading typos
Terrence Brannon authored on 2011-08-17
2000
=head1 DOCUMENTATION
pod fix
Yuki Kimoto authored on 2011-01-21
2001

            
cleanup
Yuki Kimoto authored on 2011-07-29
2002
L<DBIx::Custom::Guide> - How to use L<DBIx::Custom>
pod fix
Yuki Kimoto authored on 2011-01-21
2003

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2004
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki>
cleanup
Yuki Kimoto authored on 2011-07-29
2005
- Theare are various examples.
2006

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2007
Module documentations - 
cleanup
Yuki Kimoto authored on 2011-07-29
2008
L<DBIx::Custom::Result>,
2009
L<DBIx::Custom::Query>,
2010
L<DBIx::Custom::Where>,
2011
L<DBIx::Custom::Model>,
2012
L<DBIx::Custom::Order>
updated document
yuki-kimoto authored on 2010-08-08
2013

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

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

            
2018
    my $connector = $dbi->connector;
micro optimization
Yuki Kimoto authored on 2011-07-30
2019
    $dbi = $dbi->connector($connector);
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2020

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2021
Connection manager object. if C<connector> is set, you can get C<dbh>
2022
through connection manager. Conection manager object must have C<dbh> mehtod.
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2023

            
2024
This is L<DBIx::Connector> example. Please pass
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
2025
C<default_option> to L<DBIx::Connector> C<new> method.
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2026

            
2027
    my $connector = DBIx::Connector->new(
cleanup
Yuki Kimoto authored on 2011-08-16
2028
        "dbi:mysql:database=$database",
2029
        $user,
2030
        $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
2031
        DBIx::Custom->new->default_option
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2032
    );
2033
    
updated pod
Yuki Kimoto authored on 2011-06-21
2034
    my $dbi = DBIx::Custom->connect(connector => $connector);
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2035

            
cleanup
Yuki Kimoto authored on 2011-08-16
2036
If C<connector> is set to 1 when connect method is called,
2037
L<DBIx::Connector> is automatically set to C<connector>
2038

            
2039
    my $dbi = DBIx::Custom->connect(
2040
      dsn => $dsn, user => $user, password => $password, connector => 1);
2041
    
2042
    my $connector = $dbi->connector; # DBIx::Connector
2043

            
2044
Note that L<DBIx::Connector> must be installed.
2045

            
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
2046
=head2 C<dsn>
2047

            
2048
    my $dsn = $dbi->dsn;
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
2049
    $dbi = $dbi->dsn("DBI:mysql:database=dbname");
packaging one directory
yuki-kimoto authored on 2009-11-16
2050

            
updated pod
Yuki Kimoto authored on 2011-06-21
2051
Data source name, used when C<connect> method is executed.
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
2052

            
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
2053
=head2 C<default_option>
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
2054

            
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
2055
    my $default_option = $dbi->default_option;
2056
    $dbi = $dbi->default_option($default_option);
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
2057

            
updated pod
Yuki Kimoto authored on 2011-06-21
2058
L<DBI> default option, used when C<connect> method is executed,
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2059
default to the following values.
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
2060

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2061
    {
2062
        RaiseError => 1,
2063
        PrintError => 0,
2064
        AutoCommit => 1,
2065
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
2066

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
2067
=head2 C<exclude_table>
2068

            
2069
    my $exclude_table = $dbi->exclude_table;
2070
    $dbi = $dbi->exclude_table(qr/pg_/);
2071

            
2072
Excluded table regex.
2073
C<each_column>, C<each_table>, C<type_rule>,
2074
and C<setup_model> methods ignore matching tables.
2075

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

            
cleanup
yuki-kimoto authored on 2010-10-17
2078
    my $filters = $dbi->filters;
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
2079
    $dbi = $dbi->filters(\%filters);
packaging one directory
yuki-kimoto authored on 2009-11-16
2080

            
updated pod
Yuki Kimoto authored on 2011-06-21
2081
Filters, registered by C<register_filter> method.
add models() attribute
Yuki Kimoto authored on 2011-02-21
2082

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2011-07-26
2083
=head2 C<last_sql>
added EXPERIMENTAL last_sql ...
Yuki Kimoto authored on 2011-07-11
2084

            
2085
    my $last_sql = $dbi->last_sql;
2086
    $dbi = $dbi->last_sql($last_sql);
2087

            
2088
Get last successed SQL executed by C<execute> method.
2089

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

            
2092
    my $models = $dbi->models;
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
2093
    $dbi = $dbi->models(\%models);
add models() attribute
Yuki Kimoto authored on 2011-02-21
2094

            
updated pod
Yuki Kimoto authored on 2011-06-21
2095
Models, included by C<include_model> method.
add models() attribute
Yuki Kimoto authored on 2011-02-21
2096

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
2097
=head2 C<option>
2098

            
2099
    my $option = $dbi->option;
2100
    $dbi = $dbi->option($option);
2101

            
2102
L<DBI> option, used when C<connect> method is executed.
2103
Each value in option override the value of C<default_option>.
2104

            
cleanup
yuki-kimoto authored on 2010-10-17
2105
=head2 C<password>
2106

            
2107
    my $password = $dbi->password;
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
2108
    $dbi = $dbi->password('lkj&le`@s');
cleanup
yuki-kimoto authored on 2010-10-17
2109

            
updated pod
Yuki Kimoto authored on 2011-06-21
2110
Password, used when C<connect> method is executed.
update document
yuki-kimoto authored on 2010-01-30
2111

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2114
    my $builder = $dbi->query_builder;
added commit method
yuki-kimoto authored on 2010-05-27
2115

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2116
Creat query builder. This is L<DBIx::Custom::QueryBuilder>.
cleanup
yuki-kimoto authored on 2010-08-05
2117

            
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
2118
=head2 C<quote>
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
2119

            
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
2120
     my quote = $dbi->quote;
2121
     $dbi = $dbi->quote('"');
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
2122

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
2123
Reserved word quote.
2124
Default to double quote '"' except for mysql.
2125
In mysql, default to back quote '`'
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
2126

            
cleanup
Yuki Kimoto authored on 2011-07-30
2127
You can set quote pair.
2128

            
2129
    $dbi->quote('[]');
2130

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

            
cleanup
yuki-kimoto authored on 2010-10-17
2133
    my $result_class = $dbi->result_class;
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
2134
    $dbi = $dbi->result_class('DBIx::Custom::Result');
cleanup
yuki-kimoto authored on 2010-08-05
2135

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

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

            
fixed pod
Yuki Kimoto authored on 2011-09-27
2140
    my $safety_character = $dbi->safety_character;
2141
    $dbi = $dbi->safety_character($character);
update pod
Yuki Kimoto authored on 2011-01-27
2142

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2146
=head2 C<separator>
2147

            
fixed pod
Yuki Kimoto authored on 2011-09-27
2148
    my $separator = $dbi->separator;
2149
    $dbi = $dbi->separator('-');
improved join clause parsing
Yuki Kimoto authored on 2011-09-27
2150

            
2151
Separator which join table name and column name.
2152
This have effect to C<column> and C<mycolumn> method,
2153
and C<select> method's column option.
cleanup test
Yuki Kimoto authored on 2011-08-10
2154

            
improved join clause parsing
Yuki Kimoto authored on 2011-09-27
2155
Default to C<.>.
cleanup test
Yuki Kimoto authored on 2011-08-10
2156

            
added tag_parse attribute
Yuki Kimoto authored on 2011-06-28
2157
=head2 C<tag_parse>
2158

            
2159
    my $tag_parse = $dbi->tag_parse(0);
2160
    $dbi = $dbi->tag_parse;
2161

            
2162
Enable DEPRECATED tag parsing functionality, default to 1.
2163
If you want to disable tag parsing functionality, set to 0.
2164

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

            
cleanup
yuki-kimoto authored on 2010-10-17
2167
    my $user = $dbi->user;
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
2168
    $dbi = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
2169

            
updated pod
Yuki Kimoto authored on 2011-06-21
2170
User name, used when C<connect> method is executed.
update pod
Yuki Kimoto authored on 2011-01-27
2171

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2172
=head2 C<user_column_info>
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2173

            
2174
    my $user_column_info = $dbi->user_column_info;
2175
    $dbi = $dbi->user_column_info($user_column_info);
2176

            
improved join clause parsing
Yuki Kimoto authored on 2011-09-30
2177
You can set the date like the following one.
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2178

            
2179
    [
2180
        {table => 'book', column => 'title', info => {...}},
2181
        {table => 'author', column => 'name', info => {...}}
2182
    ]
2183

            
improved join clause parsing
Yuki Kimoto authored on 2011-09-30
2184
Usually, you set return value of C<get_column_info>.
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2185

            
2186
    my $user_column_info
2187
      = $dbi->get_column_info(exclude_table => qr/^system/);
2188
    $dbi->user_column_info($user_column_info);
2189

            
2190
If C<user_column_info> is set, C<each_column> use C<user_column_info>
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2191
to find column info. this is very fast.
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2192

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2193
=head2 C<user_table_info>
added test
Yuki Kimoto authored on 2011-08-16
2194

            
2195
    my $user_table_info = $dbi->user_table_info;
2196
    $dbi = $dbi->user_table_info($user_table_info);
2197

            
2198
You can set the following data.
2199

            
2200
    [
2201
        {table => 'book', info => {...}},
2202
        {table => 'author', info => {...}}
2203
    ]
2204

            
2205
Usually, you can set return value of C<get_table_info>.
2206

            
2207
    my $user_table_info = $dbi->get_table_info(exclude => qr/^system/);
2208
    $dbi->user_table_info($user_table_info);
2209

            
2210
If C<user_table_info> is set, C<each_table> use C<user_table_info>
2211
to find table info.
2212

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

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

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2219
=head2 C<available_datatype>
changed type_rule arguments ...
Yuki Kimoto authored on 2011-06-12
2220

            
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
2221
    print $dbi->available_datatype;
changed type_rule arguments ...
Yuki Kimoto authored on 2011-06-12
2222

            
added EXPERIMENTAL available...
Yuki Kimoto authored on 2011-06-14
2223
Get available data types. You can use these data types
updated pod
Yuki Kimoto authored on 2011-06-21
2224
in C<type rule>'s C<from1> and C<from2> section.
added EXPERIMENTAL available...
Yuki Kimoto authored on 2011-06-14
2225

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2226
=head2 C<available_typename>
added EXPERIMENTAL available...
Yuki Kimoto authored on 2011-06-14
2227

            
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
2228
    print $dbi->available_typename;
added EXPERIMENTAL available...
Yuki Kimoto authored on 2011-06-14
2229

            
2230
Get available type names. You can use these type names in
updated pod
Yuki Kimoto authored on 2011-06-21
2231
C<type_rule>'s C<into1> and C<into2> section.
changed type_rule arguments ...
Yuki Kimoto authored on 2011-06-12
2232

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
2233
=head2 C<assign_clause>
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
2234

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
2235
    my $assign_clause = $dbi->assign_clause({title => 'a', age => 2});
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
2236

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
2237
Create assign clause
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
2238

            
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
2239
    title = :title, author = :author
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
2240

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
2241
This is used to create update clause.
2242

            
2243
    "update book set " . $dbi->assign_clause({title => 'a', age => 2});
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
2244

            
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2245
=head2 C<column>
- select() EXPERIMETNAL colu...
Yuki Kimoto authored on 2011-06-08
2246

            
cleanup
Yuki Kimoto authored on 2011-06-13
2247
    my $column = $dbi->column(book => ['author', 'title']);
- select() EXPERIMETNAL colu...
Yuki Kimoto authored on 2011-06-08
2248

            
2249
Create column clause. The follwoing column clause is created.
2250

            
2251
    book.author as "book.author",
2252
    book.title as "book.title"
2253

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2254
You can change separator by C<separator> attribute.
- select() EXPERIMETNAL colu...
Yuki Kimoto authored on 2011-06-08
2255

            
cleanup
Yuki Kimoto authored on 2011-06-13
2256
    # Separator is hyphen
2257
    $dbi->separator('-');
2258
    
2259
    book.author as "book-author",
2260
    book.title as "book-title"
2261
    
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
2262
=head2 C<connect>
packaging one directory
yuki-kimoto authored on 2009-11-16
2263

            
update pod
Yuki Kimoto authored on 2011-03-13
2264
    my $dbi = DBIx::Custom->connect(
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
2265
        dsn => "dbi:mysql:database=dbname",
update pod
Yuki Kimoto authored on 2011-03-13
2266
        user => 'ken',
2267
        password => '!LFKD%$&',
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
2268
        option => {mysql_enable_utf8 => 1}
update pod
Yuki Kimoto authored on 2011-03-13
2269
    );
2270

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

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

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2277
=head2 C<count>
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
2278

            
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
2279
    my $count = $dbi->count(table => 'book');
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
2280

            
2281
Get rows count.
2282

            
2283
Options is same as C<select> method's ones.
2284

            
2285
=head2 C<create_model>
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2286

            
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
2287
    my $model = $dbi->create_model(
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2288
        table => 'book',
2289
        primary_key => 'id',
2290
        join => [
2291
            'inner join company on book.comparny_id = company.id'
2292
        ],
2293
    );
2294

            
2295
Create L<DBIx::Custom::Model> object and initialize model.
updated pod
Yuki Kimoto authored on 2011-06-21
2296
the module is also used from C<model> method.
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2297

            
2298
   $dbi->model('book')->select(...);
2299

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

            
2302
    my $dbh = $dbi->dbh;
2303

            
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2304
Get L<DBI> database handle. if C<connector> is set, you can get
updated pod
Yuki Kimoto authored on 2011-06-21
2305
database handle through C<connector> object.
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2306

            
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2307
=head2 C<delete>
2308

            
2309
    $dbi->delete(table => 'book', where => {title => 'Perl'});
2310

            
2311
Execute delete statement.
2312

            
2313
The following opitons are available.
2314

            
cleanup
Yuki Kimoto authored on 2011-10-20
2315
B<OPTIONS>
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2316

            
cleanup
Yuki Kimoto authored on 2011-10-20
2317
C<delete> method use all of C<execute> method's options,
2318
and use the following new ones.
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2319

            
cleanup
Yuki Kimoto authored on 2011-10-20
2320
=over 4
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2321

            
2322
=item C<id>
2323

            
2324
    id => 4
2325
    id => [4, 5]
2326

            
2327
ID corresponding to C<primary_key>.
2328
You can delete rows by C<id> and C<primary_key>.
2329

            
2330
    $dbi->delete(
fixed pod
Yuki Kimoto authored on 2011-10-20
2331
        primary_key => ['id1', 'id2'],
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2332
        id => [4, 5],
2333
        table => 'book',
2334
    );
2335

            
2336
The above is same as the followin one.
2337

            
2338
    $dbi->delete(where => {id1 => 4, id2 => 5}, table => 'book');
2339

            
2340
=item C<prefix>
2341

            
2342
    prefix => 'some'
2343

            
2344
prefix before table name section.
2345

            
2346
    delete some from book
2347

            
2348
=item C<table>
2349

            
2350
    table => 'book'
2351

            
2352
Table name.
2353

            
2354
=item C<where>
2355

            
2356
Same as C<select> method's C<where> option.
2357

            
2358
=back
2359

            
2360
=head2 C<delete_all>
2361

            
2362
    $dbi->delete_all(table => $table);
2363

            
2364
Execute delete statement for all rows.
2365
Options is same as C<delete>.
2366

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2367
=head2 C<each_column>
2368

            
2369
    $dbi->each_column(
2370
        sub {
2371
            my ($dbi, $table, $column, $column_info) = @_;
2372
            
2373
            my $type = $column_info->{TYPE_NAME};
2374
            
2375
            if ($type eq 'DATE') {
2376
                # ...
2377
            }
2378
        }
2379
    );
2380

            
improved pod
Yuki Kimoto authored on 2011-10-14
2381
Iterate all column informations in database.
2382
Argument is callback which is executed when one column is found.
2383
Callback receive four arguments. C<DBIx::Custom object>, C<table name>,
2384
C<column name>, and C<column information>.
2385

            
2386
If C<user_column_info> is set, C<each_column> method use C<user_column_info>
2387
infromation, you can improve the performance of C<each_column> in
2388
the following way.
2389

            
2390
    my $column_infos = $dbi->get_column_info(exclude_table => qr/^system_/);
2391
    $dbi->user_column_info($column_info);
2392
    $dbi->each_column(sub { ... });
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
2393

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2011-07-26
2394
=head2 C<each_table>
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
2395

            
2396
    $dbi->each_table(
2397
        sub {
2398
            my ($dbi, $table, $table_info) = @_;
2399
            
2400
            my $table_name = $table_info->{TABLE_NAME};
2401
        }
2402
    );
2403

            
improved pod
Yuki Kimoto authored on 2011-10-14
2404
Iterate all table informationsfrom in database.
2405
Argument is callback which is executed when one table is found.
2406
Callback receive three arguments, C<DBIx::Custom object>, C<table name>,
2407
C<table information>.
2408

            
2409
If C<user_table_info> is set, C<each_table> method use C<user_table_info>
2410
infromation, you can improve the performance of C<each_table> in
2411
the following way.
2412

            
2413
    my $table_infos = $dbi->get_table_info(exclude => qr/^system_/);
2414
    $dbi->user_table_info($table_info);
2415
    $dbi->each_table(sub { ... });
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
2416

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2419
    my $result = $dbi->execute(
updated pod
Yuki Kimoto authored on 2011-06-21
2420
      "select * from book where title = :title and author like :author",
2421
      {title => 'Perl', author => '%Ken%'}
2422
    );
2423

            
2424
    my $result = $dbi->execute(
2425
      "select * from book where title = :book.title and author like :book.author",
2426
      {'book.title' => 'Perl', 'book.author' => '%Ken%'}
update pod
Yuki Kimoto authored on 2011-03-13
2427
    );
2428

            
updated pod
Yuki Kimoto authored on 2011-06-21
2429
Execute SQL. SQL can contain column parameter such as :author and :title.
2430
You can append table name to column name such as :book.title and :book.author.
2431
Second argunet is data, embedded into column parameter.
2432
Return value is L<DBIx::Custom::Result> object when select statement is executed,
2433
or the count of affected rows when insert, update, delete statement is executed.
update pod
Yuki Kimoto authored on 2011-03-13
2434

            
I call :title named placehol...
Yuki Kimoto authored on 2011-07-30
2435
Named placeholder such as C<:title> is replaced by placeholder C<?>.
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2436
    
micro optimization
Yuki Kimoto authored on 2011-07-30
2437
    # Original
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2438
    select * from book where title = :title and author like :author
2439
    
micro optimization
Yuki Kimoto authored on 2011-07-30
2440
    # Replaced
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2441
    select * from where title = ? and author like ?;
update pod
Yuki Kimoto authored on 2011-03-13
2442

            
I call :title named placehol...
Yuki Kimoto authored on 2011-07-30
2443
You can specify operator with named placeholder
fixed pod
Yuki Kimoto authored on 2011-10-20
2444
by C<name{operator}> syntax.
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2445

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2446
    # Original
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2447
    select * from book where :title{=} and :author{like}
2448
    
micro optimization
Yuki Kimoto authored on 2011-07-30
2449
    # Replaced
update pod
Yuki Kimoto authored on 2011-03-13
2450
    select * from where title = ? and author like ?;
2451

            
fixed named placeholder bug ...
Yuki Kimoto authored on 2011-08-01
2452
Note that colons in time format such as 12:13:15 is exeption,
2453
it is not parsed as named placeholder.
2454
If you want to use colon generally, you must escape it by C<\\>
2455

            
2456
    select * from where title = "aa\\:bb";
2457

            
cleanup
Yuki Kimoto authored on 2011-10-20
2458
B<OPTIONS>
2459

            
updated pod
Yuki Kimoto authored on 2011-06-09
2460
The following opitons are available.
update pod
Yuki Kimoto authored on 2011-03-13
2461

            
2462
=over 4
2463

            
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2464
=item C<after_build_sql> 
2465

            
2466
You can filter sql after the sql is build.
2467

            
2468
    after_build_sql => $code_ref
2469

            
2470
The following one is one example.
2471

            
2472
    $dbi->select(
2473
        table => 'book',
2474
        column => 'distinct(name)',
2475
        after_build_sql => sub {
2476
            "select count(*) from ($_[0]) as t1"
2477
        }
2478
    );
2479

            
2480
The following SQL is executed.
2481

            
2482
    select count(*) from (select distinct(name) from book) as t1;
2483

            
cleanup
Yuki Kimoto authored on 2011-10-20
2484
=item C<append>
2485

            
2486
    append => 'order by name'
2487

            
2488
Append some statement after SQL.
2489

            
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
2490
=item C<bind_type>
2491

            
2492
Specify database bind data type.
2493

            
2494
    bind_type => [image => DBI::SQL_BLOB]
2495
    bind_type => [[qw/image audio/] => DBI::SQL_BLOB]
2496

            
2497
This is used to bind parameter by C<bind_param> of statment handle.
2498

            
2499
    $sth->bind_param($pos, $value, DBI::SQL_BLOB);
2500

            
update pod
Yuki Kimoto authored on 2011-03-13
2501
=item C<filter>
updated pod
Yuki Kimoto authored on 2011-06-09
2502
    
2503
    filter => {
2504
        title  => sub { uc $_[0] }
2505
        author => sub { uc $_[0] }
2506
    }
update pod
Yuki Kimoto authored on 2011-03-13
2507

            
updated pod
Yuki Kimoto authored on 2011-06-09
2508
    # Filter name
2509
    filter => {
2510
        title  => 'upper_case',
2511
        author => 'upper_case'
2512
    }
2513
        
2514
    # At once
2515
    filter => [
2516
        [qw/title author/]  => sub { uc $_[0] }
2517
    ]
2518

            
separate DBIx::Custom type_r...
Yuki Kimoto authored on 2011-06-15
2519
Filter. You can set subroutine or filter name
updated pod
Yuki Kimoto authored on 2011-06-21
2520
registered by by C<register_filter>.
separate DBIx::Custom type_r...
Yuki Kimoto authored on 2011-06-15
2521
This filter is executed before data is saved into database.
2522
and before type rule filter is executed.
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2523

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
2524
=item C<id>
2525

            
2526
    id => 4
2527
    id => [4, 5]
2528

            
2529
ID corresponding to C<primary_key>.
2530
You can delete rows by C<id> and C<primary_key>.
2531

            
2532
    $dbi->execute(
2533
        "select * from book where id1 = :id1 and id2 = :id2",
2534
        {},
fixed pod
Yuki Kimoto authored on 2011-10-20
2535
        primary_key => ['id1', 'id2'],
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
2536
        id => [4, 5],
2537
    );
2538

            
2539
The above is same as the followin one.
2540

            
2541
    $dbi->execute(
2542
        "select * from book where id1 = :id1 and id2 = :id2",
2543
        {id1 => 4, id2 => 5}
2544
    );
2545

            
updated document
Yuki Kimoto authored on 2011-06-09
2546
=item C<query>
2547

            
2548
    query => 1
2549

            
2550
C<execute> method return L<DBIx::Custom::Query> object, not executing SQL.
cleanup
Yuki Kimoto authored on 2011-07-30
2551
You can check SQL or get statment handle.
updated pod
Yuki Kimoto authored on 2011-06-21
2552

            
2553
    my $sql = $query->sql;
cleanup
Yuki Kimoto authored on 2011-07-30
2554
    my $sth = $query->sth;
Added execute method's query...
Yuki Kimoto authored on 2011-07-30
2555
    my $columns = $query->columns;
2556
    
2557
If you want to execute SQL fast, you can do the following way.
2558

            
2559
    my $query;
cleanup
Yuki Kimoto authored on 2011-10-21
2560
    for my $row (@$rows) {
Added execute method's query...
Yuki Kimoto authored on 2011-07-30
2561
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
cleanup
Yuki Kimoto authored on 2011-10-20
2562
      $dbi->execute($query, $row);
Added execute method's query...
Yuki Kimoto authored on 2011-07-30
2563
    }
2564

            
2565
Statement handle is reused and SQL parsing is finished,
2566
so you can get more performance than normal way.
cleanup
Yuki Kimoto authored on 2011-07-30
2567

            
2568
If you want to execute SQL as possible as fast and don't need filtering.
Added execute method's query...
Yuki Kimoto authored on 2011-07-30
2569
You can do the following way.
cleanup
Yuki Kimoto authored on 2011-07-30
2570
    
2571
    my $query;
2572
    my $sth;
cleanup
Yuki Kimoto authored on 2011-10-21
2573
    for my $row (@$rows) {
cleanup
Yuki Kimoto authored on 2011-07-30
2574
      $query ||= $dbi->insert($row, table => 'book', query => 1);
2575
      $sth ||= $query->sth;
2576
      $sth->execute(map { $row->{$_} } sort keys %$row);
2577
    }
2578

            
2579
Note that $row must be simple hash reference, such as
2580
{title => 'Perl', author => 'Ken'}.
2581
and don't forget to sort $row values by $row key asc order.
updated document
Yuki Kimoto authored on 2011-06-09
2582

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
2583
=item C<primary_key>
2584

            
cleanup
Yuki Kimoto authored on 2011-10-20
2585
    primary_key => 'id'
2586
    primary_key => ['id1', 'id2']
2587

            
2588
Priamry key. This is used when C<id> option find primary key.
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
2589

            
updated pod
Yuki Kimoto authored on 2011-06-09
2590
=item C<table>
2591
    
2592
    table => 'author'
2593

            
updated pod
Yuki Kimoto authored on 2011-06-21
2594
If you want to omit table name in column name
2595
and enable C<into1> and C<into2> type filter,
2596
You must set C<table> option.
updated pod
Yuki Kimoto authored on 2011-06-09
2597

            
updated pod
Yuki Kimoto authored on 2011-06-21
2598
    $dbi->execute("select * from book where title = :title and author = :author",
2599
        {title => 'Perl', author => 'Ken', table => 'book');
updated pod
Yuki Kimoto authored on 2011-06-09
2600

            
updated pod
Yuki Kimoto authored on 2011-06-21
2601
    # Same
2602
    $dbi->execute(
2603
      "select * from book where title = :book.title and author = :book.author",
2604
      {title => 'Perl', author => 'Ken');
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2605

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2606
=item C<table_alias>
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-06-27
2607

            
cleanup
Yuki Kimoto authored on 2011-10-20
2608
    table_alias => {user => 'worker'}
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-06-27
2609

            
2610
Table alias. Key is real table name, value is alias table name.
2611
If you set C<table_alias>, you can enable C<into1> and C<into2> type rule
2612
on alias table name.
2613

            
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
2614
=item C<reuse EXPERIMENTAL>
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2615
    
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
2616
    reuse_query => $has_ref
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2617

            
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
2618
Reuse query object if the hash reference variable is set.
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2619
    
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
2620
    my $queries = {};
2621
    $dbi->execute($sql, $param, reuse => $queries);
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2622

            
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
2623
This will improved performance when you want to execute same query repeatedly
2624
because generally creating query object is slow.
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2625

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2626
=item C<type_rule_off>
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2627

            
2628
    type_rule_off => 1
2629

            
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
2630
Turn C<into1> and C<into2> type rule off.
2631

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2632
=item C<type_rule1_off>
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
2633

            
2634
    type_rule1_off => 1
2635

            
2636
Turn C<into1> type rule off.
2637

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2638
=item C<type_rule2_off>
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
2639

            
2640
    type_rule2_off => 1
2641

            
2642
Turn C<into2> type rule off.
update document
yuki-kimoto authored on 2009-11-19
2643

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

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2646
=head2 C<get_column_info>
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2647

            
improved pod
Yuki Kimoto authored on 2011-10-14
2648
    my $column_infos = $dbi->get_column_info(exclude_table => qr/^system_/);
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2649

            
2650
get column infomation except for one which match C<exclude_table> pattern.
2651

            
2652
    [
2653
        {table => 'book', column => 'title', info => {...}},
2654
        {table => 'author', column => 'name' info => {...}}
2655
    ]
2656

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2657
=head2 C<get_table_info>
added test
Yuki Kimoto authored on 2011-08-16
2658

            
improved pod
Yuki Kimoto authored on 2011-10-14
2659
    my $table_infos = $dbi->get_table_info(exclude => qr/^system_/);
update pod
Yuki Kimoto authored on 2011-03-13
2660

            
added test
Yuki Kimoto authored on 2011-08-16
2661
get table infomation except for one which match C<exclude> pattern.
2662

            
2663
    [
2664
        {table => 'book', info => {...}},
2665
        {table => 'author', info => {...}}
2666
    ]
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2667

            
added test
Yuki Kimoto authored on 2011-08-16
2668
You can set this value to C<user_table_info>.
update pod
Yuki Kimoto authored on 2011-03-13
2669

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
2670
=head2 C<helper>
2671

            
2672
    $dbi->helper(
2673
        update_or_insert => sub {
2674
            my $self = shift;
2675
            
2676
            # Process
2677
        },
2678
        find_or_create   => sub {
2679
            my $self = shift;
2680
            
2681
            # Process
2682
        }
2683
    );
2684

            
2685
Register helper. These helper is called directly from L<DBIx::Custom> object.
2686

            
2687
    $dbi->update_or_insert;
2688
    $dbi->find_or_create;
2689

            
cleanup
yuki-kimoto authored on 2010-10-17
2690
=head2 C<insert>
2691

            
cleanup
Yuki Kimoto authored on 2011-06-09
2692
    $dbi->insert({title => 'Perl', author => 'Ken'}, table  => 'book');
update pod
Yuki Kimoto authored on 2011-03-13
2693

            
updated pod
Yuki Kimoto authored on 2011-06-21
2694
Execute insert statement. First argument is row data. Return value is
2695
affected row count.
update pod
Yuki Kimoto authored on 2011-03-13
2696

            
insert and update method's p...
Yuki Kimoto authored on 2011-07-29
2697
If you want to set constant value to row data, use scalar reference
2698
as parameter value.
2699

            
2700
    {date => \"NOW()"}
2701

            
cleanup
Yuki Kimoto authored on 2011-10-20
2702
B<options>
2703

            
cleanup
Yuki Kimoto authored on 2011-10-20
2704
C<insert> method use all of C<execute> method's options,
cleanup
Yuki Kimoto authored on 2011-10-20
2705
and use the following new ones.
update pod
Yuki Kimoto authored on 2011-03-13
2706

            
cleanup
Yuki Kimoto authored on 2011-06-09
2707
=over 4
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
2708

            
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2709
=item C<id>
2710

            
updated document
Yuki Kimoto authored on 2011-06-09
2711
    id => 4
2712
    id => [4, 5]
update pod
Yuki Kimoto authored on 2011-03-13
2713

            
updated document
Yuki Kimoto authored on 2011-06-09
2714
ID corresponding to C<primary_key>.
2715
You can insert a row by C<id> and C<primary_key>.
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2716

            
update pod
Yuki Kimoto authored on 2011-03-13
2717
    $dbi->insert(
updated document
Yuki Kimoto authored on 2011-06-09
2718
        {title => 'Perl', author => 'Ken'}
fixed pod
Yuki Kimoto authored on 2011-10-20
2719
        primary_key => ['id1', 'id2'],
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2720
        id => [4, 5],
updated document
Yuki Kimoto authored on 2011-06-09
2721
        table => 'book'
update pod
Yuki Kimoto authored on 2011-03-13
2722
    );
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2723

            
updated document
Yuki Kimoto authored on 2011-06-09
2724
The above is same as the followin one.
update pod
Yuki Kimoto authored on 2011-03-13
2725

            
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2726
    $dbi->insert(
updated document
Yuki Kimoto authored on 2011-06-09
2727
        {id1 => 4, id2 => 5, title => 'Perl', author => 'Ken'},
2728
        table => 'book'
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2729
    );
update pod
Yuki Kimoto authored on 2011-03-13
2730

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2011-07-26
2731
=item C<prefix>
added EXPERIMENTAL insert, u...
Yuki Kimoto authored on 2011-06-21
2732

            
2733
    prefix => 'or replace'
2734

            
2735
prefix before table name section
2736

            
2737
    insert or replace into book
2738

            
updated document
Yuki Kimoto authored on 2011-06-09
2739
=item C<table>
2740

            
2741
    table => 'book'
2742

            
2743
Table name.
2744

            
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
2745
=item C<timestamp>
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
2746

            
2747
    timestamp => 1
2748

            
2749
If this value is set to 1,
2750
automatically created timestamp column is set based on
2751
C<timestamp> attribute's C<insert> value.
2752

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2753
=item C<wrap>
updated pod
Yuki Kimoto authored on 2011-09-02
2754

            
2755
    wrap => {price => sub { "max($_[0])" }}
2756

            
2757
placeholder wrapped string.
2758

            
2759
If the following statement
2760

            
2761
    $dbi->insert({price => 100}, table => 'book',
2762
      {price => sub { "$_[0] + 5" }});
2763

            
2764
is executed, the following SQL is executed.
2765

            
2766
    insert into book price values ( ? + 5 );
2767

            
update pod
Yuki Kimoto authored on 2011-03-13
2768
=back
2769

            
2770
=over 4
2771

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

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2779
    lib / MyModel.pm
2780
        / MyModel / book.pm
2781
                  / company.pm
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2782

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

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

            
2787
    package MyModel;
updated pod
Yuki Kimoto authored on 2011-06-21
2788
    use DBIx::Custom::Model -base;
update pod
Yuki Kimoto authored on 2011-03-13
2789
    
2790
    1;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2791

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2796
    package MyModel::book;
updated pod
Yuki Kimoto authored on 2011-06-21
2797
    use MyModel -base;
update pod
Yuki Kimoto authored on 2011-03-13
2798
    
2799
    1;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2800

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2803
    package MyModel::company;
updated pod
Yuki Kimoto authored on 2011-06-21
2804
    use MyModel -base;
update pod
Yuki Kimoto authored on 2011-03-13
2805
    
2806
    1;
2807
    
updated pod
Yuki Kimoto authored on 2011-06-21
2808
MyModel::book and MyModel::company is included by C<include_model>.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2809

            
updated pod
Yuki Kimoto authored on 2011-06-21
2810
You can get model object by C<model>.
update pod
Yuki Kimoto authored on 2011-03-13
2811

            
updated pod
Yuki Kimoto authored on 2011-06-21
2812
    my $book_model = $dbi->model('book');
update pod
Yuki Kimoto authored on 2011-03-13
2813
    my $company_model = $dbi->model('company');
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2814

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

            
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
2817
=head2 C<insert_timestamp>
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
2818

            
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
2819
    $dbi->insert_timestamp(
2820
      [qw/created_at updated_at/]
2821
        => sub { Time::Piece->localtime->strftime("%Y-%m-%d %H:%M:%S") }
2822
    );
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
2823

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
2824
Timestamp value when C<insert> method is executed
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
2825
with C<timestamp> option.
2826

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
2827
If C<insert_timestamp> is set and C<insert> method is executed
2828
with C<timestamp> option, column C<created_at> and C<update_at>
2829
is automatically set to the value like "2010-10-11 10:12:54".
2830

            
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
2831
    $dbi->insert($param, table => 'book', timestamp => 1);
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
2832

            
cleanup
Yuki Kimoto authored on 2011-10-20
2833
=head2 C<like_value>
added EXPERIMENTAL like_valu...
Yuki Kimoto authored on 2011-09-16
2834

            
2835
    my $like_value = $dbi->like_value
2836

            
cleanup
Yuki Kimoto authored on 2011-10-20
2837
Code reference which return a value for the like value.
added EXPERIMENTAL like_valu...
Yuki Kimoto authored on 2011-09-16
2838

            
2839
    sub { "%$_[0]%" }
2840

            
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
2841
=head2 C<mapper>
- added EXPERIMENTAL pass at...
Yuki Kimoto authored on 2011-09-02
2842

            
2843
    my $mapper = $dbi->mapper(param => $param);
2844

            
2845
Create a new L<DBIx::Custom::Mapper> object.
2846

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

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

            
cleanup
Yuki Kimoto authored on 2011-10-20
2851
Merge parameters. The following new parameter is created.
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
2852

            
2853
    {key1 => [1, 1], key2 => 2}
2854

            
cleanup
Yuki Kimoto authored on 2011-10-20
2855
If same keys contains, the value is converted to array reference.
2856

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

            
2859
    my $model = $dbi->model('book');
2860

            
cleanup
Yuki Kimoto authored on 2011-10-20
2861
Get a L<DBIx::Custom::Model> object
2862
create by C<create_model> or C<include_model>
update pod
Yuki Kimoto authored on 2011-03-13
2863

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

            
fixed pod
Yuki Kimoto authored on 2011-09-27
2866
    my $column = $dbi->mycolumn(book => ['author', 'title']);
cleanup
Yuki Kimoto authored on 2011-03-21
2867

            
2868
Create column clause for myself. The follwoing column clause is created.
2869

            
2870
    book.author as author,
2871
    book.title as title
2872

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2875
    my $dbi = DBIx::Custom->new(
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
2876
        dsn => "dbi:mysql:database=dbname",
update pod
Yuki Kimoto authored on 2011-03-13
2877
        user => 'ken',
2878
        password => '!LFKD%$&',
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
2879
        option => {mysql_enable_utf8 => 1}
update pod
Yuki Kimoto authored on 2011-03-13
2880
    );
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2881

            
2882
Create a new L<DBIx::Custom> object.
2883

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

            
2886
    my $not_exists = $dbi->not_exists;
2887

            
update pod
Yuki Kimoto authored on 2011-03-13
2888
DBIx::Custom::NotExists object, indicating the column is not exists.
cleanup
Yuki Kimoto authored on 2011-10-20
2889
This is used in C<param> of L<DBIx::Custom::Where> .
experimental extended select...
Yuki Kimoto authored on 2011-01-17
2890

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2891
=head2 C<order>
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
2892

            
2893
    my $order = $dbi->order;
2894

            
2895
Create a new L<DBIx::Custom::Order> object.
2896

            
cleanup
yuki-kimoto authored on 2010-10-17
2897
=head2 C<register_filter>
2898

            
update pod
Yuki Kimoto authored on 2011-03-13
2899
    $dbi->register_filter(
2900
        # Time::Piece object to database DATE format
2901
        tp_to_date => sub {
2902
            my $tp = shift;
2903
            return $tp->strftime('%Y-%m-%d');
2904
        },
2905
        # database DATE format to Time::Piece object
2906
        date_to_tp => sub {
2907
           my $date = shift;
2908
           return Time::Piece->strptime($date, '%Y-%m-%d');
2909
        }
2910
    );
cleanup
yuki-kimoto authored on 2010-10-17
2911
    
update pod
Yuki Kimoto authored on 2011-03-13
2912
Register filters, used by C<filter> option of many methods.
cleanup
yuki-kimoto authored on 2010-10-17
2913

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

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
2916
    my $result = $dbi->select(
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2917
        table  => 'book',
2918
        column => ['author', 'title'],
2919
        where  => {author => 'Ken'},
select method column option ...
Yuki Kimoto authored on 2011-02-22
2920
    );
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2921
    
updated document
Yuki Kimoto authored on 2011-06-09
2922
Execute select statement.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2923

            
cleanup
Yuki Kimoto authored on 2011-10-20
2924
B<OPTIONS>
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2925

            
cleanup
Yuki Kimoto authored on 2011-10-20
2926
C<select> method use all of C<execute> method's options,
2927
and use the following new ones.
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
2928

            
cleanup
Yuki Kimoto authored on 2011-10-20
2929
=over 4
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
2930

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2931
=item C<column>
2932
    
updated document
Yuki Kimoto authored on 2011-06-09
2933
    column => 'author'
2934
    column => ['author', 'title']
2935

            
2936
Column clause.
updated pod
Yuki Kimoto authored on 2011-06-07
2937
    
updated document
Yuki Kimoto authored on 2011-06-09
2938
if C<column> is not specified, '*' is set.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2939

            
updated document
Yuki Kimoto authored on 2011-06-09
2940
    column => '*'
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2941

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2011-07-26
2942
You can specify hash of array reference.
updated pod
Yuki Kimoto authored on 2011-06-07
2943

            
updated document
Yuki Kimoto authored on 2011-06-09
2944
    column => [
updated pod
Yuki Kimoto authored on 2011-06-07
2945
        {book => [qw/author title/]},
2946
        {person => [qw/name age/]}
updated document
Yuki Kimoto authored on 2011-06-09
2947
    ]
updated pod
Yuki Kimoto authored on 2011-06-07
2948

            
updated pod
Yuki Kimoto authored on 2011-06-21
2949
This is expanded to the following one by using C<colomn> method.
- select() column option can...
Yuki Kimoto authored on 2011-06-08
2950

            
2951
    book.author as "book.author",
2952
    book.title as "book.title",
2953
    person.name as "person.name",
2954
    person.age as "person.age"
2955

            
- select method column optio...
Yuki Kimoto authored on 2011-07-11
2956
You can specify array of array reference, first argument is
2957
column name, second argument is alias.
- select() column option can...
Yuki Kimoto authored on 2011-06-08
2958

            
updated document
Yuki Kimoto authored on 2011-06-09
2959
    column => [
- select method column optio...
Yuki Kimoto authored on 2011-07-11
2960
        ['date(book.register_datetime)' => 'book.register_date']
updated document
Yuki Kimoto authored on 2011-06-09
2961
    ];
- select() column option can...
Yuki Kimoto authored on 2011-06-08
2962

            
- select method column optio...
Yuki Kimoto authored on 2011-07-11
2963
Alias is quoted properly and joined.
- select() column option can...
Yuki Kimoto authored on 2011-06-08
2964

            
2965
    date(book.register_datetime) as "book.register_date"
updated pod
Yuki Kimoto authored on 2011-06-07
2966

            
updated document
Yuki Kimoto authored on 2011-06-09
2967
=item C<filter>
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2968

            
updated document
Yuki Kimoto authored on 2011-06-09
2969
Same as C<execute> method's C<filter> option.
2970

            
2971
=item C<id>
2972

            
2973
    id => 4
2974
    id => [4, 5]
2975

            
2976
ID corresponding to C<primary_key>.
2977
You can select rows by C<id> and C<primary_key>.
2978

            
2979
    $dbi->select(
fixed pod
Yuki Kimoto authored on 2011-10-20
2980
        primary_key => ['id1', 'id2'],
updated document
Yuki Kimoto authored on 2011-06-09
2981
        id => [4, 5],
2982
        table => 'book'
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2983
    );
2984

            
updated document
Yuki Kimoto authored on 2011-06-09
2985
The above is same as the followin one.
2986

            
updated pod
Yuki Kimoto authored on 2011-04-25
2987
    $dbi->select(
updated document
Yuki Kimoto authored on 2011-06-09
2988
        where => {id1 => 4, id2 => 5},
2989
        table => 'book'
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2990
    );
2991
    
cleanup
Yuki Kimoto authored on 2011-10-20
2992
=item C<param>
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2993

            
updated document
Yuki Kimoto authored on 2011-06-09
2994
    param => {'table2.key3' => 5}
update pod
Yuki Kimoto authored on 2011-03-12
2995

            
updated document
Yuki Kimoto authored on 2011-06-09
2996
Parameter shown before where clause.
2997
    
2998
For example, if you want to contain tag in join clause, 
2999
you can pass parameter by C<param> option.
update pod
Yuki Kimoto authored on 2011-03-12
3000

            
updated document
Yuki Kimoto authored on 2011-06-09
3001
    join  => ['inner join (select * from table2 where table2.key3 = :table2.key3)' . 
3002
              ' as table2 on table1.key1 = table2.key1']
3003

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2011-07-26
3004
=itme C<prefix>
added EXPERIMENTAL select pr...
Yuki Kimoto authored on 2011-06-13
3005

            
3006
    prefix => 'SQL_CALC_FOUND_ROWS'
3007

            
3008
Prefix of column cluase
3009

            
3010
    select SQL_CALC_FOUND_ROWS title, author from book;
3011

            
updated document
Yuki Kimoto authored on 2011-06-09
3012
=item C<join>
3013

            
3014
    join => [
3015
        'left outer join company on book.company_id = company_id',
3016
        'left outer join location on company.location_id = location.id'
3017
    ]
3018
        
3019
Join clause. If column cluase or where clause contain table name like "company.name",
3020
join clausees needed when SQL is created is used automatically.
update pod
Yuki Kimoto authored on 2011-03-12
3021

            
3022
    $dbi->select(
3023
        table => 'book',
cleanup
Yuki Kimoto authored on 2011-06-13
3024
        column => ['company.location_id as location_id'],
update pod
Yuki Kimoto authored on 2011-03-12
3025
        where => {'company.name' => 'Orange'},
3026
        join => [
3027
            'left outer join company on book.company_id = company.id',
3028
            'left outer join location on company.location_id = location.id'
3029
        ]
3030
    );
3031

            
updated document
Yuki Kimoto authored on 2011-06-09
3032
In above select, column and where clause contain "company" table,
3033
the following SQL is created
update pod
Yuki Kimoto authored on 2011-03-12
3034

            
cleanup
Yuki Kimoto authored on 2011-06-13
3035
    select company.location_id as location_id
update pod
Yuki Kimoto authored on 2011-03-12
3036
    from book
3037
      left outer join company on book.company_id = company.id
cleanup
Yuki Kimoto authored on 2011-06-13
3038
    where company.name = ?;
update pod
Yuki Kimoto authored on 2011-03-12
3039

            
added join new syntax
Yuki Kimoto authored on 2011-07-28
3040
You can specify two table by yourself. This is useful when join parser can't parse
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
3041
the join clause correctly.
added join new syntax
Yuki Kimoto authored on 2011-07-28
3042

            
3043
    $dbi->select(
3044
        table => 'book',
3045
        column => ['company.location_id as location_id'],
3046
        where => {'company.name' => 'Orange'},
3047
        join => [
3048
            {
3049
                clause => 'left outer join location on company.location_id = location.id',
3050
                table => ['company', 'location']
3051
            }
3052
        ]
3053
    );
3054

            
updated document
Yuki Kimoto authored on 2011-06-09
3055
=item C<table>
updated pod
Yuki Kimoto authored on 2011-06-08
3056

            
updated document
Yuki Kimoto authored on 2011-06-09
3057
    table => 'book'
updated pod
Yuki Kimoto authored on 2011-06-08
3058

            
updated document
Yuki Kimoto authored on 2011-06-09
3059
Table name.
updated pod
Yuki Kimoto authored on 2011-06-08
3060

            
updated document
Yuki Kimoto authored on 2011-06-09
3061
=item C<where>
3062
    
3063
    # Hash refrence
3064
    where => {author => 'Ken', 'title' => 'Perl'}
3065
    
3066
    # DBIx::Custom::Where object
3067
    where => $dbi->where(
3068
        clause => ['and', 'author = :author', 'title like :title'],
3069
        param  => {author => 'Ken', title => '%Perl%'}
3070
    );
updated pod
Yuki Kimoto authored on 2011-06-21
3071
    
3072
    # Array reference 1 (array reference, hash referenc). same as above
3073
    where => [
3074
        ['and', 'author = :author', 'title like :title'],
3075
        {author => 'Ken', title => '%Perl%'}
3076
    ];    
3077
    
3078
    # Array reference 2 (String, hash reference)
3079
    where => [
3080
        'title like :title',
3081
        {title => '%Perl%'}
3082
    ]
3083
    
3084
    # String
3085
    where => 'title is null'
update pod
Yuki Kimoto authored on 2011-03-12
3086

            
cleanup
Yuki Kimoto authored on 2011-10-20
3087
Where clause. See L<DBIx::Custom::Where>.
updated document
Yuki Kimoto authored on 2011-06-09
3088
    
update pod
Yuki Kimoto authored on 2011-03-12
3089
=back
cleanup
Yuki Kimoto authored on 2011-03-08
3090

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3091
=head2 C<setup_model>
3092

            
3093
    $dbi->setup_model;
3094

            
3095
Setup all model objects.
3096
C<columns> of model object is automatically set, parsing database information.
3097

            
3098
=head2 C<type_rule>
3099

            
3100
    $dbi->type_rule(
3101
        into1 => {
3102
            date => sub { ... },
3103
            datetime => sub { ... }
3104
        },
3105
        into2 => {
3106
            date => sub { ... },
3107
            datetime => sub { ... }
3108
        },
3109
        from1 => {
3110
            # DATE
3111
            9 => sub { ... },
3112
            # DATETIME or TIMESTAMP
3113
            11 => sub { ... },
3114
        }
3115
        from2 => {
3116
            # DATE
3117
            9 => sub { ... },
3118
            # DATETIME or TIMESTAMP
3119
            11 => sub { ... },
3120
        }
3121
    );
3122

            
3123
Filtering rule when data is send into and get from database.
3124
This has a little complex problem.
3125

            
3126
In C<into1> and C<into2> you can specify
3127
type name as same as type name defined
3128
by create table, such as C<DATETIME> or C<DATE>.
3129

            
3130
Note that type name and data type don't contain upper case.
3131
If these contain upper case charactor, you convert it to lower case.
3132

            
3133
C<into2> is executed after C<into1>.
3134

            
3135
Type rule of C<into1> and C<into2> is enabled on the following
3136
column name.
3137

            
3138
=over 4
3139

            
3140
=item 1. column name
3141

            
3142
    issue_date
3143
    issue_datetime
3144

            
3145
This need C<table> option in each method.
3146

            
3147
=item 2. table name and column name, separator is dot
3148

            
3149
    book.issue_date
3150
    book.issue_datetime
3151

            
3152
=back
3153

            
3154
You get all type name used in database by C<available_typename>.
3155

            
3156
    print $dbi->available_typename;
3157

            
3158
In C<from1> and C<from2> you specify data type, not type name.
3159
C<from2> is executed after C<from1>.
3160
You get all data type by C<available_datatype>.
3161

            
3162
    print $dbi->available_datatype;
3163

            
3164
You can also specify multiple types at once.
3165

            
3166
    $dbi->type_rule(
3167
        into1 => [
3168
            [qw/DATE DATETIME/] => sub { ... },
3169
        ],
3170
    );
3171

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

            
updated document
Yuki Kimoto authored on 2011-06-09
3174
    $dbi->update({title => 'Perl'}, table  => 'book', where  => {id => 4});
removed reconnect method
yuki-kimoto authored on 2010-05-28
3175

            
insert and update method's p...
Yuki Kimoto authored on 2011-07-29
3176
Execute update statement. First argument is update row data.
3177

            
3178
If you want to set constant value to row data, use scalar reference
3179
as parameter value.
3180

            
3181
    {date => \"NOW()"}
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
3182

            
cleanup
Yuki Kimoto authored on 2011-10-20
3183
B<OPTIONS>
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
3184

            
cleanup
Yuki Kimoto authored on 2011-10-20
3185
C<update> method use all of C<execute> method's options,
3186
and use the following new ones.
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
3187

            
cleanup
Yuki Kimoto authored on 2011-10-20
3188
=over 4
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
3189

            
updated document
Yuki Kimoto authored on 2011-06-09
3190
=item C<id>
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
3191

            
updated document
Yuki Kimoto authored on 2011-06-09
3192
    id => 4
3193
    id => [4, 5]
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
3194

            
updated document
Yuki Kimoto authored on 2011-06-09
3195
ID corresponding to C<primary_key>.
3196
You can update rows by C<id> and C<primary_key>.
update pod
Yuki Kimoto authored on 2011-03-13
3197

            
DEPRECATED select() param op...
Yuki Kimoto authored on 2011-04-25
3198
    $dbi->update(
updated document
Yuki Kimoto authored on 2011-06-09
3199
        {title => 'Perl', author => 'Ken'}
fixed pod
Yuki Kimoto authored on 2011-10-20
3200
        primary_key => ['id1', 'id2'],
updated document
Yuki Kimoto authored on 2011-06-09
3201
        id => [4, 5],
3202
        table => 'book'
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
3203
    );
update pod
Yuki Kimoto authored on 2011-03-13
3204

            
updated document
Yuki Kimoto authored on 2011-06-09
3205
The above is same as the followin one.
update pod
Yuki Kimoto authored on 2011-03-13
3206

            
updated document
Yuki Kimoto authored on 2011-06-09
3207
    $dbi->update(
3208
        {title => 'Perl', author => 'Ken'}
3209
        where => {id1 => 4, id2 => 5},
3210
        table => 'book'
3211
    );
update pod
Yuki Kimoto authored on 2011-03-13
3212

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2011-07-26
3213
=item C<prefix>
added EXPERIMENTAL insert, u...
Yuki Kimoto authored on 2011-06-21
3214

            
3215
    prefix => 'or replace'
3216

            
3217
prefix before table name section
3218

            
3219
    update or replace book
3220

            
updated document
Yuki Kimoto authored on 2011-06-09
3221
=item C<table>
update pod
Yuki Kimoto authored on 2011-03-13
3222

            
updated document
Yuki Kimoto authored on 2011-06-09
3223
    table => 'book'
update pod
Yuki Kimoto authored on 2011-03-13
3224

            
updated document
Yuki Kimoto authored on 2011-06-09
3225
Table name.
update pod
Yuki Kimoto authored on 2011-03-13
3226

            
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
3227
=item C<timestamp>
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
3228

            
3229
    timestamp => 1
3230

            
3231
If this value is set to 1,
3232
automatically updated timestamp column is set based on
3233
C<timestamp> attribute's C<update> value.
3234

            
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
3235
=item C<where>
3236

            
3237
Same as C<select> method's C<where> option.
3238

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
3239
=item C<wrap>
updated pod
Yuki Kimoto authored on 2011-09-02
3240

            
3241
    wrap => {price => sub { "max($_[0])" }}
3242

            
3243
placeholder wrapped string.
3244

            
3245
If the following statement
3246

            
3247
    $dbi->update({price => 100}, table => 'book',
3248
      {price => sub { "$_[0] + 5" }});
3249

            
3250
is executed, the following SQL is executed.
3251

            
3252
    update book set price =  ? + 5;
3253

            
updated pod
Yuki Kimoto authored on 2011-06-08
3254
=back
update pod
Yuki Kimoto authored on 2011-03-13
3255

            
updated pod
Yuki Kimoto authored on 2011-06-08
3256
=head2 C<update_all>
update pod
Yuki Kimoto authored on 2011-03-13
3257

            
updated pod
Yuki Kimoto authored on 2011-06-21
3258
    $dbi->update_all({title => 'Perl'}, table => 'book', );
update pod
Yuki Kimoto authored on 2011-03-13
3259

            
updated document
Yuki Kimoto authored on 2011-06-09
3260
Execute update statement for all rows.
updated pod
Yuki Kimoto authored on 2011-06-21
3261
Options is same as C<update> method.
update pod
Yuki Kimoto authored on 2011-03-13
3262

            
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
3263
=head2 C<update_or_insert EXPERIMENTAL>
3264
    
3265
    # Where
3266
    $dbi->update_or_insert(
3267
        {id => 1, title => 'Perl'},
3268
        table => 'book',
3269
        where => {id => 1},
3270
        select_option => {append => 'for update'}
3271
    );
3272
    
3273
    # ID
3274
    $dbi->update_or_insert(
3275
        {title => 'Perl'},
3276
        table => 'book',
3277
        id => 1,
3278
        primary_key => 'id',
3279
        select_option => {append => 'for update'}
3280
    );
3281
    
3282
Update or insert.
3283

            
3284
In both examples, the following SQL is executed.
3285

            
3286
    # In case insert
3287
    insert into book (id, title) values (?, ?)
3288
    
3289
    # In case update
3290
    update book set (id = ?, title = ?) where book.id = ?
3291

            
3292
The following opitons are available adding to C<update> option.
3293

            
3294
=over 4
3295

            
3296
=item C<select_option>
3297

            
3298
    select_option => {append => 'for update'}
3299

            
3300
select method option,
3301
select method is used to check the row is already exists.
3302

            
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
3303
=head2 C<update_timestamp>
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
3304

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3305
    $dbi->update_timestamp(
3306
      updated_at
3307
        => sub { Time::Piece->localtime->strftime("%Y-%m-%d %H:%M:%S") }
cleanup
Yuki Kimoto authored on 2011-03-09
3308
    );
fix tests
Yuki Kimoto authored on 2011-01-18
3309

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3310
Timestamp value when C<update> method is executed
3311
with C<timestamp> option.
cleanup
Yuki Kimoto authored on 2011-01-12
3312

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3313
If C<insert_timestamp> is set and C<insert> method is executed
3314
with C<timestamp> option, column C<update_at>
3315
is automatically set to the value like "2010-10-11 10:12:54".
cleanup
Yuki Kimoto authored on 2011-01-12
3316

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3317
>|perl|
3318
$dbi->update($param, table => 'book', timestamp => 1);
3319
||<
cleanup
Yuki Kimoto authored on 2011-01-12
3320

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
3321
=head2 C<show_datatype>
update pod
Yuki Kimoto authored on 2011-08-10
3322

            
3323
    $dbi->show_datatype($table);
3324

            
3325
Show data type of the columns of specified table.
3326

            
3327
    book
3328
    title: 5
3329
    issue_date: 91
3330

            
3331
This data type is used in C<type_rule>'s C<from1> and C<from2>.
3332

            
- removed EXPERIMENTAL the f...
Yuki Kimoto authored on 2011-09-12
3333
=head2 C<show_tables>
test cleanup
Yuki Kimoto authored on 2011-08-15
3334

            
3335
    $dbi->show_tables;
3336

            
3337
Show tables.
3338

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
3339
=head2 C<show_typename>
update pod
Yuki Kimoto authored on 2011-08-10
3340

            
3341
    $dbi->show_typename($table);
3342

            
3343
Show type name of the columns of specified table.
3344

            
3345
    book
3346
    title: varchar
3347
    issue_date: date
3348

            
3349
This type name is used in C<type_rule>'s C<into1> and C<into2>.
3350

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3351
=head2 C<values_clause>
3352

            
3353
    my $values_clause = $dbi->values_clause({title => 'a', age => 2});
3354

            
3355
Create values clause.
3356

            
3357
    (title, author) values (title = :title, age = :age);
3358

            
3359
You can use this in insert statement.
3360

            
3361
    my $insert_sql = "insert into book $values_clause";
3362

            
3363
=head2 C<where>
3364

            
3365
    my $where = $dbi->where(
3366
        clause => ['and', 'title = :title', 'author = :author'],
3367
        param => {title => 'Perl', author => 'Ken'}
3368
    );
3369

            
3370
Create a new L<DBIx::Custom::Where> object.
3371

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
3372
=head1 ENVIRONMENTAL VARIABLES
3373

            
3374
=head2 C<DBIX_CUSTOM_DEBUG>
3375

            
3376
If environment variable C<DBIX_CUSTOM_DEBUG> is set to true,
3377
executed SQL and bind values are printed to STDERR.
3378

            
improved debug message
Yuki Kimoto authored on 2011-05-23
3379
=head2 C<DBIX_CUSTOM_DEBUG_ENCODING>
3380

            
3381
DEBUG output encoding. Default to UTF-8.
added environment variable D...
Yuki Kimoto authored on 2011-04-02
3382

            
fix heading typos
Terrence Brannon authored on 2011-08-17
3383
=head1 DEPRECATED FUNCTIONALITY
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3384

            
3385
L<DBIx::Custom>
3386

            
3387
    # Attribute methods
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
3388
    default_dbi_option # will be removed 2017/1/1
3389
    dbi_option # will be removed 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3390
    data_source # will be removed at 2017/1/1
3391
    dbi_options # will be removed at 2017/1/1
3392
    filter_check # will be removed at 2017/1/1
3393
    reserved_word_quote # will be removed at 2017/1/1
3394
    cache_method # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3395
    
3396
    # Methods
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
3397
    method # will be removed at 2017/1/1
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
3398
    assign_param # will be removed at 2017/1/1
3399
    update_param # will be removed at 2017/1/1
3400
    insert_param # will be removed at 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3401
    create_query # will be removed at 2017/1/1
3402
    apply_filter # will be removed at 2017/1/1
3403
    select_at # will be removed at 2017/1/1
3404
    delete_at # will be removed at 2017/1/1
3405
    update_at # will be removed at 2017/1/1
3406
    insert_at # will be removed at 2017/1/1
3407
    register_tag # will be removed at 2017/1/1
3408
    default_bind_filter # will be removed at 2017/1/1
3409
    default_fetch_filter # will be removed at 2017/1/1
3410
    insert_param_tag # will be removed at 2017/1/1
update pod
Yuki Kimoto authored on 2011-08-10
3411
    register_tag # will be removed at 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3412
    register_tag_processor # will be removed at 2017/1/1
3413
    update_param_tag # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3414
    
3415
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
3416
    select method where_param option # will be removed 2017/1/1
cleanup
Yuki Kimoto authored on 2011-10-21
3417
    delete method where_param option # will be removed 2017/1/1
cleanup
Yuki Kimoto authored on 2011-10-21
3418
    update method where_param option # will be removed 2017/1/1
cleanup
Yuki Kimoto authored on 2011-10-21
3419
    insert method param option # will be removed at 2017/1/1
insert method's id option is...
Yuki Kimoto authored on 2011-10-10
3420
    insert method id option # will be removed at 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3421
    select method relation option # will be removed at 2017/1/1
3422
    select method column option [COLUMN, as => ALIAS] format
3423
      # will be removed at 2017/1/1
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
3424
    execute method's sqlfilter option # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3425
    
3426
    # Others
cleanup
Yuki Kimoto authored on 2011-07-28
3427
    execute("select * from {= title}"); # execute method's
3428
                                        # tag parsing functionality
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3429
                                        # will be removed at 2017/1/1
3430
    Query caching # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3431

            
3432
L<DBIx::Custom::Model>
3433

            
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3434
    # Attribute methods
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
3435
    method # will be removed at 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3436
    filter # will be removed at 2017/1/1
3437
    name # will be removed at 2017/1/1
3438
    type # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3439

            
3440
L<DBIx::Custom::Query>
3441
    
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3442
    # Attribute methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3443
    default_filter # will be removed at 2017/1/1
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3444
    table # will be removed at 2017/1/1
3445
    filters # will be removed at 2017/1/1
3446
    
3447
    # Methods
3448
    filter # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3449

            
3450
L<DBIx::Custom::QueryBuilder>
3451
    
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3452
    # Attribute methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3453
    tags # will be removed at 2017/1/1
3454
    tag_processors # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3455
    
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3456
    # Methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3457
    register_tag # will be removed at 2017/1/1
3458
    register_tag_processor # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3459
    
3460
    # Others
3461
    build_query("select * from {= title}"); # tag parsing functionality
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3462
                                            # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3463

            
3464
L<DBIx::Custom::Result>
3465
    
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3466
    # Attribute methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3467
    filter_check # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3468
    
3469
    # Methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3470
    end_filter # will be removed at 2017/1/1
3471
    remove_end_filter # will be removed at 2017/1/1
3472
    remove_filter # will be removed at 2017/1/1
3473
    default_filter # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3474

            
3475
L<DBIx::Custom::Tag>
3476

            
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3477
    This module is DEPRECATED! # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3478

            
fix heading typos
Terrence Brannon authored on 2011-08-17
3479
=head1 BACKWARDS COMPATIBILITY POLICY
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3480

            
3481
If a functionality is DEPRECATED, you can know it by DEPRECATED warnings
3482
except for attribute method.
3483
You can check all DEPRECATED functionalities by document.
3484
DEPRECATED functionality is removed after five years,
3485
but if at least one person use the functionality and tell me that thing
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3486
I extend one year each time he tell me it.
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3487

            
3488
EXPERIMENTAL functionality will be changed without warnings.
DBIx::Custom is now stable
yuki-kimoto authored on 2010-09-07
3489

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

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

            
3494
C<< <kimoto.yuki at gmail.com> >>
3495

            
3496
L<http://github.com/yuki-kimoto/DBIx-Custom>
3497

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
3498
=head1 AUTHOR
3499

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

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

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

            
3506
This program is free software; you can redistribute it and/or modify it
3507
under the same terms as Perl itself.
3508

            
3509
=cut