DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
3508 lines | 90.721kb
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
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1201
    my $bind = [];
micro optimization
Yuki Kimoto authored on 2011-10-23
1202
    my $types = [];
removed reconnect method
yuki-kimoto authored on 2010-05-28
1203
    my $count = {};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
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
        
1207
        # Value
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1208
        my $value;
1209
        if(ref $params->{$column} eq 'ARRAY') {
1210
            my $i = $count->{$column} || 0;
1211
            $i += $not_exists->{$column} || 0;
1212
            my $found;
1213
            for (my $k = $i; $i < @{$params->{$column}}; $k++) {
1214
                if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
1215
                    $not_exists->{$column}++;
1216
                }
1217
                else  {
1218
                    $value = $params->{$column}->[$k];
1219
                    $found = 1;
1220
                    last
1221
                }
1222
            }
1223
            next unless $found;
1224
        }
1225
        else { $value = $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1226
        
cleanup
Yuki Kimoto authored on 2011-01-12
1227
        # Filter
1228
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
separate DBIx::Custom type_r...
Yuki Kimoto authored on 2011-06-15
1229
        $value = $f->($value) if $f;
1230
        
1231
        # Type rule
micro optimization
Yuki Kimoto authored on 2011-10-22
1232
        my $tf1 = $self->{"_into1"}->{dot}->{$column}
1233
          || $type_filters->{1}->{$column};
1234
        $value = $tf1->($value) if $tf1;
1235
        my $tf2 = $self->{"_into2"}->{dot}->{$column}
1236
          || $type_filters->{2}->{$column};
1237
        $value = $tf2->($value) if $tf2;
1238
       
separate DBIx::Custom type_r...
Yuki Kimoto authored on 2011-06-15
1239
        # Bind values
micro optimization
Yuki Kimoto authored on 2011-10-23
1240
        push @$bind, $value;
1241
        push @$types, $bind_type->{$column};
removed reconnect method
yuki-kimoto authored on 2010-05-28
1242
        
1243
        # Count up 
1244
        $count->{$column}++;
1245
    }
1246
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1247
    return ($bind, $types);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1248
}
1249

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1666
    return $tag;
1667
}
1668

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1975
=item *
1976

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

            
1979
=item *
1980

            
1981
Connection manager support
1982

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

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

            
1989
=item *
1990

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

            
1993
=item *
1994

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2189
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
2190
to find column info. this is very fast.
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2191

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

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

            
2197
You can set the following data.
2198

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2280
Get rows count.
2281

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

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

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

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

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

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

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

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

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

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

            
2310
Execute delete statement.
2311

            
2312
The following opitons are available.
2313

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

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

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

            
2321
=item C<id>
2322

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

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

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

            
2335
The above is same as the followin one.
2336

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

            
2339
=item C<prefix>
2340

            
2341
    prefix => 'some'
2342

            
2343
prefix before table name section.
2344

            
2345
    delete some from book
2346

            
2347
=item C<table>
2348

            
2349
    table => 'book'
2350

            
2351
Table name.
2352

            
2353
=item C<where>
2354

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

            
2357
=back
2358

            
2359
=head2 C<delete_all>
2360

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2461
=over 4
2462

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

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

            
2467
    after_build_sql => $code_ref
2468

            
2469
The following one is one example.
2470

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

            
2479
The following SQL is executed.
2480

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

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

            
2485
    append => 'order by name'
2486

            
2487
Append some statement after SQL.
2488

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

            
2491
Specify database bind data type.
2492

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

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

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

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

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

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

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

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

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

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

            
2538
The above is same as the followin one.
2539

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

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

            
2547
    query => 1
2548

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

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

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

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

            
2567
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
2568
You can do the following way.
cleanup
Yuki Kimoto authored on 2011-07-30
2569
    
2570
    my $query;
2571
    my $sth;
cleanup
Yuki Kimoto authored on 2011-10-21
2572
    for my $row (@$rows) {
cleanup
Yuki Kimoto authored on 2011-07-30
2573
      $query ||= $dbi->insert($row, table => 'book', query => 1);
2574
      $sth ||= $query->sth;
2575
      $sth->execute(map { $row->{$_} } sort keys %$row);
2576
    }
2577

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2627
    type_rule_off => 1
2628

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

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

            
2633
    type_rule1_off => 1
2634

            
2635
Turn C<into1> type rule off.
2636

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

            
2639
    type_rule2_off => 1
2640

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2732
    prefix => 'or replace'
2733

            
2734
prefix before table name section
2735

            
2736
    insert or replace into book
2737

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

            
2740
    table => 'book'
2741

            
2742
Table name.
2743

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

            
2746
    timestamp => 1
2747

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

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

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

            
2756
placeholder wrapped string.
2757

            
2758
If the following statement
2759

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

            
2763
is executed, the following SQL is executed.
2764

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

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

            
2769
=over 4
2770

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2834
    my $like_value = $dbi->like_value
2835

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2970
=item C<id>
2971

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

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

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

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

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

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

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

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

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

            
3005
    prefix => 'SQL_CALC_FOUND_ROWS'
3006

            
3007
Prefix of column cluase
3008

            
3009
    select SQL_CALC_FOUND_ROWS title, author from book;
3010

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

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

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

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

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

            
added join new syntax
Yuki Kimoto authored on 2011-07-28
3039
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
3040
the join clause correctly.
added join new syntax
Yuki Kimoto authored on 2011-07-28
3041

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

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

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

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

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

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

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

            
3092
    $dbi->setup_model;
3093

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

            
3097
=head2 C<type_rule>
3098

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

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

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

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

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

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

            
3137
=over 4
3138

            
3139
=item 1. column name
3140

            
3141
    issue_date
3142
    issue_datetime
3143

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

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

            
3148
    book.issue_date
3149
    book.issue_datetime
3150

            
3151
=back
3152

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

            
3155
    print $dbi->available_typename;
3156

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

            
3161
    print $dbi->available_datatype;
3162

            
3163
You can also specify multiple types at once.
3164

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
3214
    prefix => 'or replace'
3215

            
3216
prefix before table name section
3217

            
3218
    update or replace book
3219

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

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

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

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

            
3228
    timestamp => 1
3229

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

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

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

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

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

            
3242
placeholder wrapped string.
3243

            
3244
If the following statement
3245

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

            
3249
is executed, the following SQL is executed.
3250

            
3251
    update book set price =  ? + 5;
3252

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

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

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

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

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

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

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

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

            
3293
=over 4
3294

            
3295
=item C<select_option>
3296

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

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

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

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

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

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

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

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

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

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

            
3326
    book
3327
    title: 5
3328
    issue_date: 91
3329

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

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

            
3334
    $dbi->show_tables;
3335

            
3336
Show tables.
3337

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

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

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

            
3344
    book
3345
    title: varchar
3346
    issue_date: date
3347

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

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

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

            
3354
Create values clause.
3355

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

            
3358
You can use this in insert statement.
3359

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

            
3362
=head2 C<where>
3363

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

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

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

            
3373
=head2 C<DBIX_CUSTOM_DEBUG>
3374

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

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

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

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

            
3384
L<DBIx::Custom>
3385

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

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

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

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

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

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

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

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

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

            
3480
If a functionality is DEPRECATED, you can know it by DEPRECATED warnings
3481
except for attribute method.
3482
You can check all DEPRECATED functionalities by document.
3483
DEPRECATED functionality is removed after five years,
3484
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
3485
I extend one year each time he tell me it.
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3486

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

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

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

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

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

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

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

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

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

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

            
3508
=cut