DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
3541 lines | 91.844kb
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

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

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

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

            
added helper method
yuki-kimoto authored on 2010-10-17
98
our $AUTOLOAD;
99
sub AUTOLOAD {
100
    my $self = shift;
101

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

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

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
119
sub assign_clause {
updated pod
Yuki Kimoto authored on 2011-09-02
120
    my ($self, $param, $opts) = @_;
121
    
122
    my $wrap = $opts->{wrap} || {};
micro optimization
Yuki Kimoto authored on 2011-10-23
123
    my $safety = $self->{safety_character} || $self->safety_character;
fixed some quoted bug
Yuki Kimoto authored on 2011-10-22
124
    my $qp = $self->_q('');
125
    my $q = substr($qp, 0, 1) || '';
126
    my $p = substr($qp, 1, 1) || '';
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
127
    
micro optimization
Yuki Kimoto authored on 2011-10-23
128
    # Check unsafety keys
129
    unless ((join('', keys %$param) || '') =~ /^[$safety\.]+$/) {
130
        for my $column (keys %$param) {
131
            croak qq{"$column" is not safety column name } . _subname
132
              unless $column =~ /^[$safety\.]+$/;
133
        }
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
134
    }
135
    
micro optimization
Yuki Kimoto authored on 2011-10-23
136
    # Assign clause (performance is important)
micro optimization
Yuki Kimoto authored on 2011-10-23
137
    join(
138
      ', ',
139
      map {
140
          ref $param->{$_} eq 'SCALAR' ? "$q$_$p = " . ${$param->{$_}}
141
          : $wrap->{$_} ? "$q$_$p = " . $wrap->{$_}->(":$_")
142
          : "$q$_$p = :$_";
143
      } sort keys %$param
144
    );
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
145
}
146

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-04-02
247
    # Delete statement
cleanup
Yuki Kimoto authored on 2011-10-21
248
    my $sql = "delete ";
cleanup
Yuki Kimoto authored on 2011-10-21
249
    $sql .= "$opt{prefix} " if defined $opt{prefix};
cleanup
Yuki Kimoto authored on 2011-10-25
250
    $sql .= "from " . $self->_q($opt{table}) . " $w->{clause} ";
packaging one directory
yuki-kimoto authored on 2009-11-16
251
    
252
    # Execute query
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
253
    $opt{statement} = 'delete';
cleanup
Yuki Kimoto authored on 2011-10-25
254
    $self->execute($sql, $w->{param}, %opt);
packaging one directory
yuki-kimoto authored on 2009-11-16
255
}
256

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

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

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

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

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

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

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

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

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

            
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
381
    # Return query
382
    return $query if $opt{query};
micro optimization
Yuki Kimoto authored on 2011-07-30
383
    
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
384
    # Merge query filter(DEPRECATED!)
DBIx::Custom::Query filter m...
Yuki Kimoto authored on 2011-07-30
385
    $filter ||= $query->{filter} || {};
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
386
    
cleanup
Yuki Kimoto authored on 2011-04-02
387
    # Tables
DBIx::Custom::Query tables a...
Yuki Kimoto authored on 2011-07-30
388
    unshift @$tables, @{$query->{tables} || []};
micro optimization
Yuki Kimoto authored on 2011-07-30
389
    my $main_table = @{$tables}[-1];
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
390

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

            
cleanup
yuki-kimoto authored on 2010-10-17
468
    # Execute
micro optimization
Yuki Kimoto authored on 2011-10-23
469
    my $sth = $query->{sth};
cleanup
yuki-kimoto authored on 2010-10-17
470
    my $affected;
cleanup
Yuki Kimoto authored on 2011-03-21
471
    eval {
micro optimization
Yuki Kimoto authored on 2011-10-23
472
        if ($opt{bind_type} || $opt{type}) {
473
            $sth->bind_param($_ + 1, $bind->[$_],
474
                $bind_types->[$_] ? $bind_types->[$_] : ())
475
              for (0 .. @$bind - 1);
476
            $affected = $sth->execute;
477
        }
478
        else {
479
            $affected = $sth->execute(@$bind);
480
        }
cleanup
Yuki Kimoto authored on 2011-03-21
481
    };
improved error messages
Yuki Kimoto authored on 2011-04-18
482
    
micro optimization
Yuki Kimoto authored on 2011-07-30
483
    $self->_croak($@, qq{. Following SQL is executed.\n}
484
      . qq{$query->{sql}\n} . _subname) if $@;
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
485

            
486
    # Remove id from parameter
487
    delete $param->{$_} for @cleanup;
cleanup
yuki-kimoto authored on 2010-10-17
488
    
improved debug message
Yuki Kimoto authored on 2011-05-23
489
    # DEBUG message
fixed DEBUG messsage bug
Yuki Kimoto authored on 2011-10-24
490
    if ($ENV{DBIX_CUSTOM_DEBUG}) {
491
        warn "SQL:\n" . $query->sql . "\n";
improved debug message
Yuki Kimoto authored on 2011-05-23
492
        my @output;
fixed DEBUG messsage bug
Yuki Kimoto authored on 2011-10-24
493
        for my $value (@$bind) {
improved debug message
Yuki Kimoto authored on 2011-05-23
494
            $value = 'undef' unless defined $value;
fixed DEBUG messsage bug
Yuki Kimoto authored on 2011-10-24
495
            $value = encode($ENV{DBIX_CUSTOM_DEBUG_ENCODING} || 'UTF-8', $value)
improved debug message
Yuki Kimoto authored on 2011-05-23
496
              if utf8::is_utf8($value);
497
            push @output, $value;
498
        }
fixed DEBUG messsage bug
Yuki Kimoto authored on 2011-10-24
499
        warn "Bind values: " . join(', ', @output) . "\n\n";
improved debug message
Yuki Kimoto authored on 2011-05-23
500
    }
added environment variable D...
Yuki Kimoto authored on 2011-04-02
501
    
micro optimization
Yuki Kimoto authored on 2011-10-23
502
    # Not select statement
503
    return $affected unless $sth->{NUM_OF_FIELDS};
504

            
505
    # Filter(DEPRECATED!)
506
    my $infilter = {};
507
    if ($self->{filter}{on}) {
508
        $infilter->{in}  = {};
509
        $infilter->{end} = {};
510
        push @$tables, $main_table if $main_table;
511
        for my $table (@$tables) {
512
            for my $way (qw/in end/) {
513
                $infilter->{$way} = {%{$infilter->{$way}},
514
                  %{$self->{filter}{$way}{$table} || {}}};
cleanup
Yuki Kimoto authored on 2011-04-02
515
            }
cleanup
Yuki Kimoto authored on 2011-01-12
516
        }
cleanup
yuki-kimoto authored on 2010-10-17
517
    }
micro optimization
Yuki Kimoto authored on 2011-10-23
518
    
519
    # Result
520
    my $result = $self->result_class->new(
521
        sth => $sth,
522
        dbi => $self,
523
        default_filter => $self->{default_in_filter},
524
        filter => $infilter->{in} || {},
525
        end_filter => $infilter->{end} || {},
526
        type_rule => {
527
            from1 => $self->type_rule->{from1},
528
            from2 => $self->type_rule->{from2}
529
        },
530
    );
531
    $result;
cleanup
yuki-kimoto authored on 2010-10-17
532
}
533

            
added test
Yuki Kimoto authored on 2011-08-16
534
sub get_table_info {
cleanup
Yuki Kimoto authored on 2011-10-21
535
    my ($self, %opt) = @_;
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
536
    
cleanup
Yuki Kimoto authored on 2011-10-21
537
    my $exclude = delete $opt{exclude};
538
    croak qq/"$_" is wrong option/ for keys %opt;
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
539
    
added test
Yuki Kimoto authored on 2011-08-16
540
    my $table_info = [];
541
    $self->each_table(
542
        sub { push @$table_info, {table => $_[1], info => $_[2] } },
543
        exclude => $exclude
544
    );
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
545
    
cleanup test
Yuki Kimoto authored on 2011-08-16
546
    return [sort {$a->{table} cmp $b->{table} } @$table_info];
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
547
}
548

            
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
549
sub get_column_info {
cleanup
Yuki Kimoto authored on 2011-10-21
550
    my ($self, %opt) = @_;
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
551
    
cleanup
Yuki Kimoto authored on 2011-10-21
552
    my $exclude_table = delete $opt{exclude_table};
553
    croak qq/"$_" is wrong option/ for keys %opt;
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
554
    
555
    my $column_info = [];
556
    $self->each_column(
557
        sub { push @$column_info, {table => $_[1], column => $_[2], info => $_[3] } },
558
        exclude_table => $exclude_table
559
    );
560
    
561
    return [
562
      sort {$a->{table} cmp $b->{table} || $a->{column} cmp $b->{column} }
cleanup
Yuki Kimoto authored on 2011-08-16
563
        @$column_info];
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
564
}
565

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
566
sub helper {
567
    my $self = shift;
568
    
569
    # Register method
570
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
571
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
572
    
573
    return $self;
574
}
575

            
cleanup
yuki-kimoto authored on 2010-10-17
576
sub insert {
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
577
    my $self = shift;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
578
    
cleanup
Yuki Kimoto authored on 2011-10-21
579
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
580
    my $param = @_ % 2 ? shift : undef;
cleanup
Yuki Kimoto authored on 2011-10-21
581
    my %opt = @_;
micro optimization
Yuki Kimoto authored on 2011-10-23
582
    warn "insert method param option is DEPRECATED!" if $opt{param};
cleanup
Yuki Kimoto authored on 2011-10-21
583
    $param ||= delete $opt{param} || {};
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
584
    
585
    # Timestamp
cleanup
Yuki Kimoto authored on 2011-10-21
586
    if ($opt{timestamp} && (my $insert_timestamp = $self->insert_timestamp)) {
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
587
        my $columns = $insert_timestamp->[0];
588
        $columns = [$columns] unless ref $columns eq 'ARRAY';
589
        my $value = $insert_timestamp->[1];
590
        $value = $value->() if ref $value eq 'CODE';
591
        $param->{$_} = $value for @$columns;
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
592
    }
cleanup
Yuki Kimoto authored on 2011-10-21
593
    
594
    # Merge id to parameter
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
595
    my @cleanup;
micro optimization
Yuki Kimoto authored on 2011-10-23
596
    if (defined $opt{id}) {
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
597
        croak "insert id option must be specified with primary_key option"
micro optimization
Yuki Kimoto authored on 2011-10-23
598
          unless $opt{primary_key};
599
        $opt{primary_key} = [$opt{primary_key}] unless ref $opt{primary_key};
600
        $opt{id} = [$opt{id}] unless ref $opt{id};
601
        for (my $i = 0; $i < @{$opt{primary_key}}; $i++) {
602
           my $key = $opt{primary_key}->[$i];
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
603
           next if exists $param->{$key};
micro optimization
Yuki Kimoto authored on 2011-10-23
604
           $param->{$key} = $opt{id}->[$i];
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
605
           push @cleanup, $key;
micro optimization
Yuki Kimoto authored on 2011-10-23
606
        }
607
    }
cleanup
Yuki Kimoto authored on 2011-10-21
608
    
cleanup
Yuki Kimoto authored on 2011-04-02
609
    # Insert statement
cleanup
Yuki Kimoto authored on 2011-10-21
610
    my $sql = "insert ";
cleanup
Yuki Kimoto authored on 2011-10-21
611
    $sql .= "$opt{prefix} " if defined $opt{prefix};
612
    $sql .= "into " . $self->_q($opt{table}) . " "
613
      . $self->values_clause($param, {wrap => $opt{wrap}}) . " ";
micro optimization
Yuki Kimoto authored on 2011-10-23
614

            
615
    # Remove id from parameter
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
616
    delete $param->{$_} for @cleanup;
packaging one directory
yuki-kimoto authored on 2009-11-16
617
    
618
    # Execute query
micro optimization
Yuki Kimoto authored on 2011-10-23
619
    $opt{statement} = 'insert';
micro optimization
Yuki Kimoto authored on 2011-10-23
620
    $self->execute($sql, $param, %opt);
packaging one directory
yuki-kimoto authored on 2009-11-16
621
}
622

            
micro optimization
Yuki Kimoto authored on 2011-10-23
623
sub _merge_id_to_param {
624
    my ($self, $id, $primary_keys, $param) = @_;
625
    
626
    # Create parameter
627
    $id = [$id] unless ref $id;
628
    
629
    return $param;
630
}
631

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
632
sub insert_timestamp {
633
    my $self = shift;
634
    
635
    if (@_) {
636
        $self->{insert_timestamp} = [@_];
637
        
638
        return $self;
639
    }
640
    return $self->{insert_timestamp};
641
}
642

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
643
sub include_model {
644
    my ($self, $name_space, $model_infos) = @_;
645
    
cleanup
Yuki Kimoto authored on 2011-04-02
646
    # Name space
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
647
    $name_space ||= '';
cleanup
Yuki Kimoto authored on 2011-04-02
648
    
649
    # Get Model infomations
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
650
    unless ($model_infos) {
cleanup
Yuki Kimoto authored on 2011-04-02
651

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
652
        # Load name space module
cleanup
Yuki Kimoto authored on 2011-04-25
653
        croak qq{"$name_space" is invalid class name } . _subname
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
654
          if $name_space =~ /[^\w:]/;
655
        eval "use $name_space";
cleanup
Yuki Kimoto authored on 2011-04-25
656
        croak qq{Name space module "$name_space.pm" is needed. $@ }
657
            . _subname
improved error messages
Yuki Kimoto authored on 2011-04-18
658
          if $@;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
659
        
660
        # Search model modules
661
        my $path = $INC{"$name_space.pm"};
662
        $path =~ s/\.pm$//;
663
        opendir my $dh, $path
cleanup
Yuki Kimoto authored on 2011-04-25
664
          or croak qq{Can't open directory "$path": $! } . _subname
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
665
        $model_infos = [];
666
        while (my $module = readdir $dh) {
667
            push @$model_infos, $module
668
              if $module =~ s/\.pm$//;
669
        }
670
        close $dh;
671
    }
672
    
cleanup
Yuki Kimoto authored on 2011-04-02
673
    # Include models
cleanup
Yuki Kimoto authored on 2011-10-21
674
    for my $model_info (@$model_infos) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
675
        
cleanup
Yuki Kimoto authored on 2011-04-02
676
        # Load model
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
677
        my $model_class;
678
        my $model_name;
679
        my $model_table;
680
        if (ref $model_info eq 'HASH') {
681
            $model_class = $model_info->{class};
682
            $model_name  = $model_info->{name};
683
            $model_table = $model_info->{table};
684
            
685
            $model_name  ||= $model_class;
686
            $model_table ||= $model_name;
687
        }
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
688
        else { $model_class = $model_name = $model_table = $model_info }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
689
        my $mclass = "${name_space}::$model_class";
cleanup
Yuki Kimoto authored on 2011-04-25
690
        croak qq{"$mclass" is invalid class name } . _subname
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
691
          if $mclass =~ /[^\w:]/;
692
        unless ($mclass->can('isa')) {
693
            eval "use $mclass";
cleanup
Yuki Kimoto authored on 2011-04-25
694
            croak "$@ " . _subname if $@;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
695
        }
696
        
cleanup
Yuki Kimoto authored on 2011-04-02
697
        # Create model
cleanup
Yuki Kimoto authored on 2011-10-21
698
        my $opt = {};
699
        $opt->{model_class} = $mclass if $mclass;
700
        $opt->{name}        = $model_name if $model_name;
701
        $opt->{table}       = $model_table if $model_table;
702
        $self->create_model($opt);
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
703
    }
704
    
705
    return $self;
706
}
707

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

            
added DBIx::Custom::Mapper
Yuki Kimoto authored on 2011-08-26
710
sub mapper {
711
    my $self = shift;
712
    return DBIx::Custom::Mapper->new(@_);
713
}
714

            
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
715
sub merge_param {
716
    my ($self, @params) = @_;
717
    
cleanup
Yuki Kimoto authored on 2011-04-02
718
    # Merge parameters
fixed merge_param bug
Yuki Kimoto authored on 2011-05-23
719
    my $merge = {};
cleanup
Yuki Kimoto authored on 2011-10-21
720
    for my $param (@params) {
721
        for my $column (keys %$param) {
fixed merge_param bug
Yuki Kimoto authored on 2011-05-23
722
            my $param_is_array = ref $param->{$column} eq 'ARRAY' ? 1 : 0;
723
            
724
            if (exists $merge->{$column}) {
725
                $merge->{$column} = [$merge->{$column}]
726
                  unless ref $merge->{$column} eq 'ARRAY';
727
                push @{$merge->{$column}},
728
                  ref $param->{$column} ? @{$param->{$column}} : $param->{$column};
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
729
            }
730
            else {
fixed merge_param bug
Yuki Kimoto authored on 2011-05-23
731
                $merge->{$column} = $param->{$column};
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
732
            }
733
        }
734
    }
735
    
fixed merge_param bug
Yuki Kimoto authored on 2011-05-23
736
    return $merge;
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
737
}
738

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
739
sub model {
740
    my ($self, $name, $model) = @_;
741
    
cleanup
Yuki Kimoto authored on 2011-04-02
742
    # Set model
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
743
    if ($model) {
744
        $self->models->{$name} = $model;
745
        return $self;
746
    }
747
    
748
    # Check model existance
cleanup
Yuki Kimoto authored on 2011-04-25
749
    croak qq{Model "$name" is not included } . _subname
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
750
      unless $self->models->{$name};
751
    
cleanup
Yuki Kimoto authored on 2011-04-02
752
    # Get model
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
753
    return $self->models->{$name};
754
}
755

            
cleanup
Yuki Kimoto authored on 2011-03-21
756
sub mycolumn {
757
    my ($self, $table, $columns) = @_;
758
    
cleanup
Yuki Kimoto authored on 2011-04-02
759
    # Create column clause
760
    my @column;
cleanup
Yuki Kimoto authored on 2011-03-21
761
    $columns ||= [];
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
762
    push @column, $self->_q($table) . "." . $self->_q($_) .
763
      " as " . $self->_q($_)
764
      for @$columns;
cleanup
Yuki Kimoto authored on 2011-03-21
765
    
766
    return join (', ', @column);
767
}
768

            
added dbi_options attribute
kimoto authored on 2010-12-20
769
sub new {
770
    my $self = shift->SUPER::new(@_);
771
    
cleanup
Yuki Kimoto authored on 2011-04-02
772
    # Check attributes
added dbi_options attribute
kimoto authored on 2010-12-20
773
    my @attrs = keys %$self;
cleanup
Yuki Kimoto authored on 2011-10-21
774
    for my $attr (@attrs) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
775
        croak qq{Invalid attribute: "$attr" } . _subname
added dbi_options attribute
kimoto authored on 2010-12-20
776
          unless $self->can($attr);
777
    }
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
778

            
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
779
    # DEPRECATED
cleanup
Yuki Kimoto authored on 2011-08-13
780
    $self->{_tags} = {
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
781
        '?'     => \&DBIx::Custom::Tag::placeholder,
782
        '='     => \&DBIx::Custom::Tag::equal,
783
        '<>'    => \&DBIx::Custom::Tag::not_equal,
784
        '>'     => \&DBIx::Custom::Tag::greater_than,
785
        '<'     => \&DBIx::Custom::Tag::lower_than,
786
        '>='    => \&DBIx::Custom::Tag::greater_than_equal,
787
        '<='    => \&DBIx::Custom::Tag::lower_than_equal,
788
        'like'  => \&DBIx::Custom::Tag::like,
789
        'in'    => \&DBIx::Custom::Tag::in,
790
        'insert_param' => \&DBIx::Custom::Tag::insert_param,
791
        'update_param' => \&DBIx::Custom::Tag::update_param
cleanup
Yuki Kimoto authored on 2011-08-13
792
    };
793
    
794
    return $self;
795
}
796

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

            
799
sub order {
800
    my $self = shift;
801
    return DBIx::Custom::Order->new(dbi => $self, @_);
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
802
}
803

            
cleanup
yuki-kimoto authored on 2010-10-17
804
sub register_filter {
cleanup
Yuki Kimoto authored on 2011-04-02
805
    my $self = shift;
cleanup
yuki-kimoto authored on 2010-10-17
806
    
807
    # Register filter
808
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
cleanup
Yuki Kimoto authored on 2011-04-02
809
    $self->filters({%{$self->filters}, %$filters});
cleanup
yuki-kimoto authored on 2010-10-17
810
    
cleanup
Yuki Kimoto authored on 2011-04-02
811
    return $self;
cleanup
yuki-kimoto authored on 2010-10-17
812
}
packaging one directory
yuki-kimoto authored on 2009-11-16
813

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

            
cleanup
Yuki Kimoto authored on 2011-10-21
817
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
818
    my $tables = ref $opt{table} eq 'ARRAY' ? $opt{table}
819
               : defined $opt{table} ? [$opt{table}]
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
820
               : [];
cleanup
Yuki Kimoto authored on 2011-10-21
821
    $opt{table} = $tables;
cleanup
Yuki Kimoto authored on 2011-10-21
822
    my $where_param = $opt{where_param} || delete $opt{param} || {};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
823
    
cleanup
Yuki Kimoto authored on 2011-03-09
824
    # Add relation tables(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-10-21
825
    if ($opt{relation}) {
826
        warn "select() relation option is DEPRECATED!";
827
        $self->_add_relation_table($tables, $opt{relation});
828
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
829
    
cleanup
Yuki Kimoto authored on 2011-04-02
830
    # Select statement
micro optimization
Yuki Kimoto authored on 2011-09-30
831
    my $sql = 'select ';
packaging one directory
yuki-kimoto authored on 2009-11-16
832
    
added EXPERIMENTAL select pr...
Yuki Kimoto authored on 2011-06-13
833
    # Prefix
cleanup
Yuki Kimoto authored on 2011-10-21
834
    $sql .= "$opt{prefix} " if defined $opt{prefix};
added EXPERIMENTAL select pr...
Yuki Kimoto authored on 2011-06-13
835
    
cleanup
Yuki Kimoto authored on 2011-10-21
836
    # Column
cleanup
Yuki Kimoto authored on 2011-10-21
837
    if (defined $opt{column}) {
838
        my $columns
839
          = ref $opt{column} eq 'ARRAY' ? $opt{column} : [$opt{column}];
cleanup
Yuki Kimoto authored on 2011-10-21
840
        for my $column (@$columns) {
- select() column option can...
Yuki Kimoto authored on 2011-06-08
841
            if (ref $column eq 'HASH') {
EXPERIMTANL column method th...
Yuki Kimoto authored on 2011-06-13
842
                $column = $self->column(%$column) if ref $column eq 'HASH';
- select() column option can...
Yuki Kimoto authored on 2011-06-08
843
            }
844
            elsif (ref $column eq 'ARRAY') {
- select method column optio...
Yuki Kimoto authored on 2011-07-11
845
                if (@$column == 3 && $column->[1] eq 'as') {
846
                    warn "[COLUMN, as => ALIAS] is DEPRECATED! use [COLUMN => ALIAS]";
847
                    splice @$column, 1, 1;
848
                }
849
                
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
850
                $column = join(' ', $column->[0], 'as', $self->_q($column->[1]));
- select() column option can...
Yuki Kimoto authored on 2011-06-08
851
            }
cleanup
Yuki Kimoto authored on 2011-04-02
852
            unshift @$tables, @{$self->_search_tables($column)};
micro optimization
Yuki Kimoto authored on 2011-09-30
853
            $sql .= "$column, ";
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
854
        }
micro optimization
Yuki Kimoto authored on 2011-09-30
855
        $sql =~ s/, $/ /;
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
856
    }
micro optimization
Yuki Kimoto authored on 2011-09-30
857
    else { $sql .= '* ' }
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
858
    
859
    # Table
micro optimization
Yuki Kimoto authored on 2011-09-30
860
    $sql .= 'from ';
cleanup
Yuki Kimoto authored on 2011-10-21
861
    if ($opt{relation}) {
cleanup
Yuki Kimoto authored on 2011-03-30
862
        my $found = {};
cleanup
Yuki Kimoto authored on 2011-10-21
863
        for my $table (@$tables) {
micro optimization
Yuki Kimoto authored on 2011-09-30
864
            $sql .= $self->_q($table) . ', ' unless $found->{$table};
cleanup
Yuki Kimoto authored on 2011-03-30
865
            $found->{$table} = 1;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
866
        }
packaging one directory
yuki-kimoto authored on 2009-11-16
867
    }
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
868
    else { $sql .= $self->_q($tables->[-1] || '') . ' ' }
micro optimization
Yuki Kimoto authored on 2011-09-30
869
    $sql =~ s/, $/ /;
cleanup
Yuki Kimoto authored on 2011-10-21
870
    croak "select method table option must be specified " . _subname
improved error messages
Yuki Kimoto authored on 2011-04-18
871
      unless $tables->[-1];
cleanup
Yuki Kimoto authored on 2011-04-01
872

            
cleanup
Yuki Kimoto authored on 2011-04-02
873
    # Add tables in parameter
DEPRECATED select() param op...
Yuki Kimoto authored on 2011-04-25
874
    unshift @$tables,
875
            @{$self->_search_tables(join(' ', keys %$where_param) || '')};
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
876
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
877
    # Where
cleanup
Yuki Kimoto authored on 2011-10-25
878
    my $w = $self->_where_clause_and_param($opt{where}, $where_param,
879
      delete $opt{id}, $opt{primary_key}, $tables->[-1]);
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
880
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
881
    # Add table names in where clause
cleanup
Yuki Kimoto authored on 2011-10-21
882
    unshift @$tables, @{$self->_search_tables($w->{clause})};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
883
    
cleanup
Yuki Kimoto authored on 2011-10-21
884
    # Join statement
cleanup
Yuki Kimoto authored on 2011-10-21
885
    $self->_push_join(\$sql, $opt{join}, $tables) if defined $opt{join};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
886
    
cleanup
Yuki Kimoto authored on 2011-03-09
887
    # Add where clause
cleanup
Yuki Kimoto authored on 2011-10-21
888
    $sql .= "$w->{clause} ";
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
889
    
cleanup
Yuki Kimoto authored on 2011-03-08
890
    # Relation(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-10-21
891
    $self->_push_relation(\$sql, $tables, $opt{relation}, $w->{clause} eq '' ? 1 : 0)
cleanup
Yuki Kimoto authored on 2011-10-21
892
      if $opt{relation};
cleanup
Yuki Kimoto authored on 2011-03-08
893
    
packaging one directory
yuki-kimoto authored on 2009-11-16
894
    # Execute query
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
895
    $opt{statement} = 'select';
cleanup
Yuki Kimoto authored on 2011-10-21
896
    my $result = $self->execute($sql, $w->{param}, %opt);
packaging one directory
yuki-kimoto authored on 2009-11-16
897
    
micro optimization
Yuki Kimoto authored on 2011-10-23
898
    $result;
packaging one directory
yuki-kimoto authored on 2009-11-16
899
}
900

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
901
sub setup_model {
902
    my $self = shift;
903
    
cleanup
Yuki Kimoto authored on 2011-04-02
904
    # Setup model
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
905
    $self->each_column(
906
        sub {
907
            my ($self, $table, $column, $column_info) = @_;
908
            if (my $model = $self->models->{$table}) {
909
                push @{$model->columns}, $column;
910
            }
911
        }
912
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
913
    return $self;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
914
}
915

            
update pod
Yuki Kimoto authored on 2011-08-10
916
sub show_datatype {
917
    my ($self, $table) = @_;
918
    croak "Table name must be specified" unless defined $table;
919
    print "$table\n";
920
    
921
    my $result = $self->select(table => $table, where => "'0' <> '0'");
922
    my $sth = $result->sth;
923

            
924
    my $columns = $sth->{NAME};
925
    my $data_types = $sth->{TYPE};
926
    
927
    for (my $i = 0; $i < @$columns; $i++) {
928
        my $column = $columns->[$i];
929
        my $data_type = $data_types->[$i];
930
        print "$column: $data_type\n";
931
    }
932
}
933

            
934
sub show_typename {
935
    my ($self, $t) = @_;
936
    croak "Table name must be specified" unless defined $t;
937
    print "$t\n";
938
    
939
    $self->each_column(sub {
940
        my ($self, $table, $column, $infos) = @_;
941
        return unless $table eq $t;
942
        my $typename = $infos->{TYPE_NAME};
943
        print "$column: $typename\n";
944
    });
945
    
946
    return $self;
947
}
948

            
test cleanup
Yuki Kimoto authored on 2011-08-15
949
sub show_tables {
950
    my $self = shift;
951
    
952
    my %tables;
953
    $self->each_table(sub { $tables{$_[1]}++ });
954
    print join("\n", sort keys %tables) . "\n";
955
    return $self;
956
}
957

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

            
961
    $self->{_type_rule_is_called} = 1;
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
962
    
963
    if (@_) {
changed type_rule arguments ...
Yuki Kimoto authored on 2011-06-12
964
        my $type_rule = ref $_[0] eq 'HASH' ? $_[0] : {@_};
fixed bug that type_rule fro...
Yuki Kimoto authored on 2011-06-13
965
        
966
        # Into
cleanup
Yuki Kimoto authored on 2011-10-21
967
        for my $i (1 .. 2) {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
968
            my $into = "into$i";
cleanup
Yuki Kimoto authored on 2011-08-16
969
            my $exists_into = exists $type_rule->{$into};
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
970
            $type_rule->{$into} = _array_to_hash($type_rule->{$into});
971
            $self->{type_rule} = $type_rule;
972
            $self->{"_$into"} = {};
cleanup
Yuki Kimoto authored on 2011-10-21
973
            for my $type_name (keys %{$type_rule->{$into} || {}}) {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
974
                croak qq{type name of $into section must be lower case}
975
                  if $type_name =~ /[A-Z]/;
976
            }
cleanup
Yuki Kimoto authored on 2011-08-16
977
            
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
978
            $self->each_column(sub {
979
                my ($dbi, $table, $column, $column_info) = @_;
980
                
981
                my $type_name = lc $column_info->{TYPE_NAME};
982
                if ($type_rule->{$into} &&
983
                    (my $filter = $type_rule->{$into}->{$type_name}))
type_rule can receive filter...
Yuki Kimoto authored on 2011-06-12
984
                {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
985
                    return unless exists $type_rule->{$into}->{$type_name};
986
                    if  (defined $filter && ref $filter ne 'CODE') 
987
                    {
988
                        my $fname = $filter;
989
                        croak qq{Filter "$fname" is not registered" } . _subname
990
                          unless exists $self->filters->{$fname};
991
                        
992
                        $filter = $self->filters->{$fname};
993
                    }
994

            
micro optimization
Yuki Kimoto authored on 2011-07-30
995
                    $self->{"_$into"}{key}{$table}{$column} = $filter;
996
                    $self->{"_$into"}{dot}{"$table.$column"} = $filter;
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
997
                }
998
            });
999
        }
1000

            
1001
        # From
cleanup
Yuki Kimoto authored on 2011-10-21
1002
        for my $i (1 .. 2) {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1003
            $type_rule->{"from$i"} = _array_to_hash($type_rule->{"from$i"});
cleanup
Yuki Kimoto authored on 2011-10-21
1004
            for my $data_type (keys %{$type_rule->{"from$i"} || {}}) {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1005
                croak qq{data type of from$i section must be lower case or number}
1006
                  if $data_type =~ /[A-Z]/;
1007
                my $fname = $type_rule->{"from$i"}{$data_type};
1008
                if (defined $fname && ref $fname ne 'CODE') {
type_rule can receive filter...
Yuki Kimoto authored on 2011-06-12
1009
                    croak qq{Filter "$fname" is not registered" } . _subname
1010
                      unless exists $self->filters->{$fname};
1011
                    
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1012
                    $type_rule->{"from$i"}{$data_type} = $self->filters->{$fname};
type_rule can receive filter...
Yuki Kimoto authored on 2011-06-12
1013
                }
fixed bug that type_rule fro...
Yuki Kimoto authored on 2011-06-13
1014
            }
1015
        }
1016
        
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
1017
        return $self;
1018
    }
1019
    
1020
    return $self->{type_rule} || {};
1021
}
1022

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

            
cleanup
Yuki Kimoto authored on 2011-10-21
1026
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
1027
    my $param = @_ % 2 ? shift : undef;
cleanup
Yuki Kimoto authored on 2011-10-21
1028
    my %opt = @_;
cleanup
Yuki Kimoto authored on 2011-10-21
1029
    warn "update param option is DEPRECATED!" if $opt{param};
cleanup
Yuki Kimoto authored on 2011-10-21
1030
    warn "update method where_param option is DEPRECATED!"
1031
      if $opt{where_param};
cleanup
Yuki Kimoto authored on 2011-10-21
1032
    $param ||= $opt{param} || {};
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
1033
    
cleanup
Yuki Kimoto authored on 2011-10-21
1034
    # Don't allow update all rows
1035
    croak qq{update method where option must be specified } . _subname
1036
      if !$opt{where} && !defined $opt{id} && !$opt{allow_update_all};
1037
    
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
1038
    # Timestamp
cleanup
Yuki Kimoto authored on 2011-10-21
1039
    if ($opt{timestamp} && (my $update_timestamp = $self->update_timestamp)) {
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
1040
        my $columns = $update_timestamp->[0];
1041
        $columns = [$columns] unless ref $columns eq 'ARRAY';
1042
        my $value = $update_timestamp->[1];
1043
        $value = $value->() if ref $value eq 'CODE';
1044
        $param->{$_} = $value for @$columns;
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
1045
    }
1046

            
cleanup
Yuki Kimoto authored on 2011-10-21
1047
    # Assign clause
cleanup
Yuki Kimoto authored on 2011-10-21
1048
    my $assign_clause = $self->assign_clause($param, {wrap => $opt{wrap}});
cleanup
Yuki Kimoto authored on 2011-10-21
1049
    
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
1050
    # Where
cleanup
Yuki Kimoto authored on 2011-10-25
1051
    my $w = $self->_where_clause_and_param($opt{where}, $opt{where_param},
1052
      delete $opt{id}, $opt{primary_key}, $opt{table});
removed reconnect method
yuki-kimoto authored on 2010-05-28
1053
    
cleanup
Yuki Kimoto authored on 2011-10-21
1054
    # Merge where parameter to parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1055
    $param = $self->merge_param($param, $w->{param}) if keys %{$w->{param}};
DEPRECATED select() param op...
Yuki Kimoto authored on 2011-04-25
1056
    
cleanup
Yuki Kimoto authored on 2011-04-02
1057
    # Update statement
cleanup
Yuki Kimoto authored on 2011-10-21
1058
    my $sql = "update ";
1059
    $sql .= "$opt{prefix} " if defined $opt{prefix};
cleanup
Yuki Kimoto authored on 2011-10-21
1060
    $sql .= $self->_q($opt{table}) . " set $assign_clause $w->{clause} ";
cleanup
Yuki Kimoto authored on 2011-01-27
1061
    
cleanup
yuki-kimoto authored on 2010-10-17
1062
    # Execute query
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
1063
    $opt{statement} = 'update';
micro optimization
Yuki Kimoto authored on 2011-10-23
1064
    $self->execute($sql, $param, %opt);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1065
}
1066

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

            
cleanup
Yuki Kimoto authored on 2011-10-21
1069
sub update_or_insert {
1070
    my $self = shift;
1071

            
cleanup
Yuki Kimoto authored on 2011-10-21
1072
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
1073
    my $param  = shift;
1074
    my %opt = @_;
1075
    my $id = $opt{id};
1076
    my $primary_key = $opt{primary_key};
1077
    $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY';
1078
    croak "update_or_insert method need primary_key option " .
1079
          "when id is specified" . _subname
1080
      if defined $id && !defined $primary_key;
1081
    my $table  = $opt{table};
1082
    croak qq{"table" option must be specified } . _subname
1083
      unless defined $table;
1084
    my $select_option = $opt{select_option};
1085
    
1086
    my $rows = $self->select(table => $table, id => $id,
1087
        primary_key => $primary_key, %$select_option)->all;
1088
    
1089
    croak "selected row count must be one or zero" . _subname
1090
      if @$rows > 1;
1091
    
1092
    my $row = $rows->[0];
1093
    my @opt = (table => $table);
1094
    push @opt, id => $id, primary_key => $primary_key if defined $id;
1095
    push @opt, %opt;
1096
    
1097
    if ($row) {
1098
        return $self->update($param, @opt);
1099
    }
1100
    else {
1101
        return $self->insert($param, @opt);
1102
    }
1103
}
1104

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
1105
sub update_timestamp {
1106
    my $self = shift;
1107
    
1108
    if (@_) {
1109
        $self->{update_timestamp} = [@_];
1110
        
1111
        return $self;
1112
    }
1113
    return $self->{update_timestamp};
1114
}
1115

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1116
sub values_clause {
1117
    my ($self, $param, $opts) = @_;
1118
    
1119
    my $wrap = $opts->{wrap} || {};
1120
    
1121
    # Create insert parameter tag
micro optimization
Yuki Kimoto authored on 2011-10-23
1122
    my $safety = $self->{safety_character} || $self->safety_character;
improved performance
Yuki Kimoto authored on 2011-10-22
1123
    my $qp = $self->_q('');
fixed some quoted bug
Yuki Kimoto authored on 2011-10-22
1124
    my $q = substr($qp, 0, 1) || '';
1125
    my $p = substr($qp, 1, 1) || '';
improved performance
Yuki Kimoto authored on 2011-10-22
1126
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1127
    # Check unsafety keys
1128
    unless ((join('', keys %$param) || '') =~ /^[$safety\.]+$/) {
1129
        for my $column (keys %$param) {
1130
            croak qq{"$column" is not safety column name } . _subname
1131
              unless $column =~ /^[$safety\.]+$/;
1132
        }
1133
    }
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1134
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1135
    # values clause(performance is important)
micro optimization
Yuki Kimoto authored on 2011-10-23
1136
    '(' .
1137
    join(
1138
      ', ',
1139
      map { "$q$_$p" } sort keys %$param
1140
    ) .
1141
    ') values (' .
1142
    join(
1143
      ', ',
1144
      map {
1145
          ref $param->{$_} eq 'SCALAR' ? ${$param->{$_}} :
1146
          $wrap->{$_} ? $wrap->{$_}->(":$_") :
1147
          ":$_";
1148
      } sort keys %$param
1149
    ) .
1150
    ')'
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1151
}
1152

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

            
updated pod
Yuki Kimoto authored on 2011-06-21
1155
sub _create_query {
cleanup
Yuki Kimoto authored on 2011-06-13
1156
    
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
1157
    my ($self, $source, $after_build_sql) = @_;
cleanup
Yuki Kimoto authored on 2011-06-13
1158
    
updated pod
Yuki Kimoto authored on 2011-06-21
1159
    # Cache
1160
    my $cache = $self->cache;
1161
    
1162
    # Query
1163
    my $query;
1164
    
1165
    # Get cached query
1166
    if ($cache) {
cleanup
Yuki Kimoto authored on 2011-06-13
1167
        
updated pod
Yuki Kimoto authored on 2011-06-21
1168
        # Get query
1169
        my $q = $self->cache_method->($self, $source);
cleanup
Yuki Kimoto authored on 2011-06-13
1170
        
updated pod
Yuki Kimoto authored on 2011-06-21
1171
        # Create query
1172
        if ($q) {
1173
            $query = DBIx::Custom::Query->new($q);
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
1174
            $query->{filters} = $self->filters;
cleanup
Yuki Kimoto authored on 2011-06-13
1175
        }
updated pod
Yuki Kimoto authored on 2011-06-21
1176
    }
1177
    
1178
    # Create query
1179
    unless ($query) {
1180

            
1181
        # Create query
1182
        my $builder = $self->query_builder;
1183
        $query = $builder->build_query($source);
1184

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

            
1191
        # Save query to cache
1192
        $self->cache_method->(
1193
            $self, $source,
1194
            {
1195
                sql     => $query->sql, 
1196
                columns => $query->columns,
DBIx::Custom::Query tables a...
Yuki Kimoto authored on 2011-07-30
1197
                tables  => $query->{tables} || []
updated pod
Yuki Kimoto authored on 2011-06-21
1198
            }
1199
        ) if $cache;
cleanup
Yuki Kimoto authored on 2011-06-13
1200
    }
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
1201

            
1202
    # Filter SQL
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
1203
    if ($after_build_sql) {
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
1204
        my $sql = $query->sql;
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
1205
        $sql = $after_build_sql->($sql);
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
1206
        $query->sql($sql);
1207
    }
1208
        
added EXPERIMENTAL last_sql ...
Yuki Kimoto authored on 2011-07-11
1209
    # Save sql
1210
    $self->last_sql($query->sql);
1211
    
updated pod
Yuki Kimoto authored on 2011-06-21
1212
    # Prepare statement handle
1213
    my $sth;
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
1214
    eval { $sth = $self->dbh->prepare($query->{sql}) };
updated pod
Yuki Kimoto authored on 2011-06-21
1215
    
1216
    if ($@) {
1217
        $self->_croak($@, qq{. Following SQL is executed.\n}
1218
                        . qq{$query->{sql}\n} . _subname);
1219
    }
1220
    
1221
    # Set statement handle
1222
    $query->sth($sth);
1223
    
1224
    # Set filters
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
1225
    $query->{filters} = $self->filters;
updated pod
Yuki Kimoto authored on 2011-06-21
1226
    
1227
    return $query;
cleanup
Yuki Kimoto authored on 2011-06-13
1228
}
1229

            
cleanup
Yuki Kimoto authored on 2011-04-02
1230
sub _create_bind_values {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1231
    my ($self, $params, $columns, $filter, $type_filters, $bind_type) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1232
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1233
    $bind_type = _array_to_hash($bind_type) if ref $bind_type eq 'ARRAY';
1234
    
cleanup
Yuki Kimoto authored on 2011-04-02
1235
    # Create bind values
micro optimization
Yuki Kimoto authored on 2011-10-23
1236
    my @bind;
1237
    my @types;
1238
    my %count;
1239
    my %not_exists;
cleanup
Yuki Kimoto authored on 2011-10-21
1240
    for my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1241
        
micro optimization
Yuki Kimoto authored on 2011-10-23
1242
        # Bind value
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1243
        if(ref $params->{$column} eq 'ARRAY') {
micro optimization
Yuki Kimoto authored on 2011-10-23
1244
            my $i = $count{$column} || 0;
1245
            $i += $not_exists{$column} || 0;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1246
            my $found;
1247
            for (my $k = $i; $i < @{$params->{$column}}; $k++) {
1248
                if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
micro optimization
Yuki Kimoto authored on 2011-10-23
1249
                    $not_exists{$column}++;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1250
                }
1251
                else  {
micro optimization
Yuki Kimoto authored on 2011-10-23
1252
                    push @bind, $params->{$column}->[$k];
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1253
                    $found = 1;
1254
                    last
1255
                }
1256
            }
1257
            next unless $found;
1258
        }
micro optimization
Yuki Kimoto authored on 2011-10-23
1259
        else { push @bind, $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1260
        
cleanup
Yuki Kimoto authored on 2011-01-12
1261
        # Filter
micro optimization
Yuki Kimoto authored on 2011-10-23
1262
        if (my $f = $filter->{$column} || $self->{default_out_filter} || '') {
micro optimization
Yuki Kimoto authored on 2011-10-23
1263
            $bind[-1] = $f->($bind[-1]);
micro optimization
Yuki Kimoto authored on 2011-10-23
1264
        }
separate DBIx::Custom type_r...
Yuki Kimoto authored on 2011-06-15
1265
        
1266
        # Type rule
micro optimization
Yuki Kimoto authored on 2011-10-23
1267
        if ($self->{_type_rule_is_called}) {
1268
            my $tf1 = $self->{"_into1"}->{dot}->{$column}
1269
              || $type_filters->{1}->{$column};
micro optimization
Yuki Kimoto authored on 2011-10-23
1270
            $bind[-1] = $tf1->($bind[-1]) if $tf1;
micro optimization
Yuki Kimoto authored on 2011-10-23
1271
            my $tf2 = $self->{"_into2"}->{dot}->{$column}
1272
              || $type_filters->{2}->{$column};
micro optimization
Yuki Kimoto authored on 2011-10-23
1273
            $bind[-1] = $tf2->($bind[-1]) if $tf2;
micro optimization
Yuki Kimoto authored on 2011-10-23
1274
        }
micro optimization
Yuki Kimoto authored on 2011-10-22
1275
       
micro optimization
Yuki Kimoto authored on 2011-10-23
1276
        # Bind types
micro optimization
Yuki Kimoto authored on 2011-10-23
1277
        push @types, $bind_type->{$column};
removed reconnect method
yuki-kimoto authored on 2010-05-28
1278
        
1279
        # Count up 
micro optimization
Yuki Kimoto authored on 2011-10-23
1280
        $count{$column}++;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1281
    }
1282
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1283
    return (\@bind, \@types);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1284
}
1285

            
cleanup
Yuki Kimoto authored on 2011-10-21
1286
sub _id_to_param {
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
1287
    my ($self, $id, $primary_keys, $table) = @_;
cleanup
Yuki Kimoto authored on 2011-10-21
1288
    
1289
    # Check primary key
cleanup
Yuki Kimoto authored on 2011-10-21
1290
    croak "primary_key option " .
1291
          "must be specified with id option " . _subname
1292
      unless defined $primary_keys;
1293
    $primary_keys = [$primary_keys] unless ref $primary_keys eq 'ARRAY';
improved error messages
Yuki Kimoto authored on 2011-04-18
1294
    
cleanup
Yuki Kimoto authored on 2011-06-08
1295
    # Create parameter
1296
    my $param = {};
fixed small insert, update, ...
Yuki Kimoto authored on 2011-06-21
1297
    if (defined $id) {
cleanup
Yuki Kimoto authored on 2011-06-08
1298
        $id = [$id] unless ref $id;
1299
        croak qq{"id" must be constant value or array reference}
improved error messages
Yuki Kimoto authored on 2011-04-18
1300
            . " (" . (caller 1)[3] . ")"
cleanup
Yuki Kimoto authored on 2011-06-08
1301
          unless !ref $id || ref $id eq 'ARRAY';
1302
        croak qq{"id" must contain values same count as primary key}
improved error messages
Yuki Kimoto authored on 2011-04-18
1303
            . " (" . (caller 1)[3] . ")"
cleanup
Yuki Kimoto authored on 2011-06-08
1304
          unless @$primary_keys eq @$id;
improved error messages
Yuki Kimoto authored on 2011-04-18
1305
        for(my $i = 0; $i < @$primary_keys; $i ++) {
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
1306
           my $key = $primary_keys->[$i];
1307
           $key = "$table." . $key if $table;
1308
           $param->{$key} = $id->[$i];
improved error messages
Yuki Kimoto authored on 2011-04-18
1309
        }
1310
    }
1311
    
cleanup
Yuki Kimoto authored on 2011-06-08
1312
    return $param;
improved error messages
Yuki Kimoto authored on 2011-04-18
1313
}
1314

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1315
sub _connect {
1316
    my $self = shift;
1317
    
1318
    # Attributes
added warnings
Yuki Kimoto authored on 2011-06-07
1319
    my $dsn = $self->data_source;
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
1320
    warn "data_source is DEPRECATED!\n"
fixed bug that data_source D...
Yuki Kimoto authored on 2011-06-13
1321
      if $dsn;
added warnings
Yuki Kimoto authored on 2011-06-07
1322
    $dsn ||= $self->dsn;
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
1323
    croak qq{"dsn" must be specified } . _subname
1324
      unless $dsn;
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1325
    my $user        = $self->user;
1326
    my $password    = $self->password;
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1327
    my $option = $self->_option;
1328
    $option = {%{$self->default_option}, %$option};
cleanup
Yuki Kimoto authored on 2011-08-16
1329
    
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1330
    # Connect
cleanup
Yuki Kimoto authored on 2011-08-16
1331
    my $dbh;
1332
    eval {
1333
        $dbh = DBI->connect(
1334
            $dsn,
1335
            $user,
1336
            $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1337
            $option
cleanup
Yuki Kimoto authored on 2011-08-16
1338
        );
1339
    };
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1340
    
1341
    # Connect error
cleanup
Yuki Kimoto authored on 2011-04-25
1342
    croak "$@ " . _subname if $@;
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1343
    
1344
    return $dbh;
1345
}
1346

            
cleanup
yuki-kimoto authored on 2010-10-17
1347
sub _croak {
1348
    my ($self, $error, $append) = @_;
cleanup
Yuki Kimoto authored on 2011-04-02
1349
    
1350
    # Append
cleanup
yuki-kimoto authored on 2010-10-17
1351
    $append ||= "";
1352
    
1353
    # Verbose
1354
    if ($Carp::Verbose) { croak $error }
1355
    
1356
    # Not verbose
1357
    else {
1358
        
1359
        # Remove line and module infromation
1360
        my $at_pos = rindex($error, ' at ');
1361
        $error = substr($error, 0, $at_pos);
1362
        $error =~ s/\s+$//;
1363
        croak "$error$append";
1364
    }
1365
}
1366

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

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1369
sub _need_tables {
1370
    my ($self, $tree, $need_tables, $tables) = @_;
1371
    
cleanup
Yuki Kimoto authored on 2011-04-02
1372
    # Get needed tables
cleanup
Yuki Kimoto authored on 2011-10-21
1373
    for my $table (@$tables) {
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1374
        if ($tree->{$table}) {
1375
            $need_tables->{$table} = 1;
1376
            $self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}])
1377
        }
1378
    }
1379
}
1380

            
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1381
sub _option {
1382
    my $self = shift;
1383
    my $option = {%{$self->dbi_options}, %{$self->dbi_option}, %{$self->option}};
1384
    warn "dbi_options is DEPRECATED! use option instead\n"
1385
      if keys %{$self->dbi_options};
1386
    warn "dbi_option is DEPRECATED! use option instead\n"
1387
      if keys %{$self->dbi_option};
1388
    return $option;
1389
}
1390

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1391
sub _push_join {
1392
    my ($self, $sql, $join, $join_tables) = @_;
1393
    
cleanup
Yuki Kimoto authored on 2011-10-21
1394
    $join = [$join] unless ref $join eq 'ARRAY';
cleanup
Yuki Kimoto authored on 2011-10-21
1395
    
cleanup
Yuki Kimoto authored on 2011-04-02
1396
    # No join
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1397
    return unless @$join;
1398
    
cleanup
Yuki Kimoto authored on 2011-04-02
1399
    # Push join clause
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1400
    my $tree = {};
1401
    for (my $i = 0; $i < @$join; $i++) {
1402
        
cleanup
Yuki Kimoto authored on 2011-07-28
1403
        # Arrange
added join new syntax
Yuki Kimoto authored on 2011-07-28
1404
        my $join_clause;;
1405
        my $option;
1406
        if (ref $join->[$i] eq 'HASH') {
1407
            $join_clause = $join->[$i]->{clause};
1408
            $option = {table => $join->[$i]->{table}};
1409
        }
1410
        else {
1411
            $join_clause = $join->[$i];
1412
            $option = {};
1413
        };
cleanup
Yuki Kimoto authored on 2011-07-28
1414

            
1415
        # Find tables in join clause
added join new syntax
Yuki Kimoto authored on 2011-07-28
1416
        my $table1;
1417
        my $table2;
1418
        if (my $table = $option->{table}) {
1419
            $table1 = $table->[0];
1420
            $table2 = $table->[1];
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1421
        }
cleanup
Yuki Kimoto authored on 2011-07-28
1422
        else {
1423
            my $q = $self->_quote;
1424
            my $j_clause = (split /\s+on\s+/, $join_clause)[-1];
1425
            $j_clause =~ s/'.+?'//g;
1426
            my $q_re = quotemeta($q);
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
1427
            $j_clause =~ s/[$q_re]//g;
improved join clause parsing
Yuki Kimoto authored on 2011-09-27
1428
            
1429
            my @j_clauses = reverse split /\s(and|on)\s/, $j_clause;
cleanup
Yuki Kimoto authored on 2011-07-28
1430
            my $c = $self->safety_character;
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1431
            my $join_re = qr/($c+)\.$c+[^$c].*?($c+)\.$c+/sm;
improved join clause parsing
Yuki Kimoto authored on 2011-09-27
1432
            for my $clause (@j_clauses) {
1433
                if ($clause =~ $join_re) {
1434
                    $table1 = $1;
1435
                    $table2 = $2;
1436
                    last;
1437
                }                
cleanup
Yuki Kimoto authored on 2011-07-28
1438
            }
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1439
        }
added join new syntax
Yuki Kimoto authored on 2011-07-28
1440
        croak qq{join clause must have two table name after "on" keyword. } .
1441
              qq{"$join_clause" is passed }  . _subname
1442
          unless defined $table1 && defined $table2;
1443
        croak qq{right side table of "$join_clause" must be unique }
1444
            . _subname
1445
          if exists $tree->{$table2};
1446
        croak qq{Same table "$table1" is specified} . _subname
1447
          if $table1 eq $table2;
1448
        $tree->{$table2}
1449
          = {position => $i, parent => $table1, join => $join_clause};
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1450
    }
1451
    
cleanup
Yuki Kimoto authored on 2011-04-02
1452
    # Search need tables
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1453
    my $need_tables = {};
1454
    $self->_need_tables($tree, $need_tables, $join_tables);
cleanup
Yuki Kimoto authored on 2011-10-21
1455
    my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} }
1456
      keys %$need_tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1457
    
1458
    # Add join clause
cleanup
Yuki Kimoto authored on 2011-10-21
1459
    $$sql .= $tree->{$_}{join} . ' ' for @need_tables;
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1460
}
cleanup
Yuki Kimoto authored on 2011-03-08
1461

            
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1462
sub _quote {
1463
    my $self = shift;
micro optimization
Yuki Kimoto authored on 2011-10-22
1464
    return $self->{reserved_word_quote} || $self->quote || '';
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1465
}
1466

            
cleanup
Yuki Kimoto authored on 2011-07-29
1467
sub _q {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
1468
    my ($self, $value, $quotemeta) = @_;
cleanup
Yuki Kimoto authored on 2011-07-29
1469
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1470
    my $quote = $self->{reserved_word_quote}
1471
      || $self->{quote} || $self->quote || '';
1472
    return "$quote$value$quote"
1473
      if !$quotemeta && ($quote eq '`' || $quote eq '"');
1474
    
cleanup
Yuki Kimoto authored on 2011-07-29
1475
    my $q = substr($quote, 0, 1) || '';
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
1476
    my $p;
1477
    if (defined $quote && length $quote > 1) {
1478
        $p = substr($quote, 1, 1);
1479
    }
1480
    else { $p = $q }
cleanup
Yuki Kimoto authored on 2011-07-29
1481
    
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
1482
    if ($quotemeta) {
1483
        $q = quotemeta($q);
1484
        $p = quotemeta($p);
1485
    }
1486
    
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
1487
    return "$q$value$p";
cleanup
Yuki Kimoto authored on 2011-07-29
1488
}
1489

            
cleanup
Yuki Kimoto authored on 2011-04-02
1490
sub _remove_duplicate_table {
1491
    my ($self, $tables, $main_table) = @_;
1492
    
1493
    # Remove duplicate table
1494
    my %tables = map {defined $_ ? ($_ => 1) : ()} @$tables;
1495
    delete $tables{$main_table} if $main_table;
1496
    
micro optimization
Yuki Kimoto authored on 2011-07-30
1497
    my $new_tables = [keys %tables, $main_table ? $main_table : ()];
1498
    if (my $q = $self->_quote) {
1499
        $q = quotemeta($q);
1500
        $_ =~ s/[$q]//g for @$new_tables;
1501
    }
1502

            
1503
    return $new_tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1504
}
1505

            
cleanup
Yuki Kimoto authored on 2011-04-02
1506
sub _search_tables {
cleanup
Yuki Kimoto authored on 2011-04-02
1507
    my ($self, $source) = @_;
1508
    
cleanup
Yuki Kimoto authored on 2011-04-02
1509
    # Search tables
cleanup
Yuki Kimoto authored on 2011-04-02
1510
    my $tables = [];
1511
    my $safety_character = $self->safety_character;
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1512
    my $q = $self->_quote;
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
1513
    my $quoted_safety_character_re = $self->_q("?([$safety_character]+)", 1);
1514
    my $table_re = $q ? qr/(?:^|[^$safety_character])${quoted_safety_character_re}?\./
improved table search in col...
Yuki Kimoto authored on 2011-04-12
1515
                      : qr/(?:^|[^$safety_character])([$safety_character]+)\./;
cleanup
Yuki Kimoto authored on 2011-04-02
1516
    while ($source =~ /$table_re/g) {
1517
        push @$tables, $1;
1518
    }
1519
    
1520
    return $tables;
1521
}
1522

            
cleanup
Yuki Kimoto authored on 2011-10-21
1523
sub _where_clause_and_param {
cleanup
Yuki Kimoto authored on 2011-10-25
1524
    my ($self, $where, $where_param, $id, $primary_key, $table) = @_;
cleanup
Yuki Kimoto authored on 2011-10-25
1525

            
cleanup
Yuki Kimoto authored on 2011-10-21
1526
    $where ||= {};
cleanup
Yuki Kimoto authored on 2011-10-25
1527
    $where = $self->_id_to_param($id, $primary_key, $table) if defined $id;
cleanup
Yuki Kimoto authored on 2011-10-25
1528
    $where_param ||= {};
cleanup
Yuki Kimoto authored on 2011-10-21
1529
    my $w = {};
1530
    my $where_clause = '';
cleanup
Yuki Kimoto authored on 2011-10-25
1531

            
cleanup
Yuki Kimoto authored on 2011-10-25
1532
    my $obj;
1533
    
cleanup
Yuki Kimoto authored on 2011-10-21
1534
    if (ref $where eq 'ARRAY' && !ref $where->[0]) {
1535
        $w->{clause} = "where " . $where->[0];
1536
        $w->{param} = $where->[1];
1537
    }
1538
    elsif (ref $where) {
cleanup
Yuki Kimoto authored on 2011-10-25
1539

            
1540
        # Hash
1541
        if (ref $where eq 'HASH') {
1542
            my $clause = ['and'];
1543
            my $q = $self->_quote;
1544
            for my $column (keys %$where) {
1545
                my $table;
1546
                my $c;
1547
                if ($column =~ /(?:(.*?)\.)?(.*)/) {
1548
                    $table = $1;
1549
                    $c = $2;
1550
                }
1551
                
1552
                my $table_quote;
1553
                $table_quote = $self->_q($table) if defined $table;
1554
                my $column_quote = $self->_q($c);
1555
                $column_quote = $table_quote . '.' . $column_quote
1556
                  if defined $table_quote;
1557
                push @$clause, "$column_quote = :$column" for keys %$where;
1558
            }
1559
            $obj = $self->where(clause => $clause, param => $where);
1560
        }
1561
        
1562
        # DBIx::Custom::Where object
1563
        elsif (ref $where eq 'DBIx::Custom::Where') {
1564
            $obj = $where;
1565
        }
1566
        
1567
        # Array
1568
        elsif (ref $where eq 'ARRAY') {
1569
            $obj = $self->where(
1570
                clause => $where->[0],
1571
                param  => $where->[1]
1572
            );
1573
        }
1574
        
1575
        # Check where argument
1576
        croak qq{"where" must be hash reference or DBIx::Custom::Where object}
1577
            . qq{or array reference, which contains where clause and parameter}
1578
            . _subname
1579
          unless ref $obj eq 'DBIx::Custom::Where';
1580

            
1581

            
1582
        $w->{param} = keys %$where_param
1583
                    ? $self->merge_param($where_param, $obj->param)
1584
                    : $obj->param;
1585
        $w->{clause} = $obj->to_string;
cleanup
Yuki Kimoto authored on 2011-10-21
1586
    }
1587
    elsif ($where) {
1588
        $w->{clause} = "where $where";
cleanup
Yuki Kimoto authored on 2011-10-25
1589
        $w->{param} = $where_param;
cleanup
Yuki Kimoto authored on 2011-10-21
1590
    }
1591
    
1592
    return $w;
1593
}
1594

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

            
1598
    # Initialize filters
1599
    $self->{filter} ||= {};
micro optimization
Yuki Kimoto authored on 2011-07-30
1600
    $self->{filter}{on} = 1;
updated pod
Yuki Kimoto authored on 2011-06-21
1601
    $self->{filter}{out} ||= {};
1602
    $self->{filter}{in} ||= {};
1603
    $self->{filter}{end} ||= {};
1604
    
1605
    # Usage
1606
    my $usage = "Usage: \$dbi->apply_filter(" .
1607
                "TABLE, COLUMN1, {in => INFILTER1, out => OUTFILTER1, end => ENDFILTER1}, " .
1608
                "COLUMN2, {in => INFILTER2, out => OUTFILTER2, end => ENDFILTER2}, ...)";
1609
    
1610
    # Apply filter
1611
    for (my $i = 0; $i < @cinfos; $i += 2) {
1612
        
1613
        # Column
1614
        my $column = $cinfos[$i];
1615
        if (ref $column eq 'ARRAY') {
cleanup
Yuki Kimoto authored on 2011-10-21
1616
            for my $c (@$column) {
updated pod
Yuki Kimoto authored on 2011-06-21
1617
                push @cinfos, $c, $cinfos[$i + 1];
1618
            }
1619
            next;
1620
        }
1621
        
1622
        # Filter infomation
1623
        my $finfo = $cinfos[$i + 1] || {};
1624
        croak "$usage (table: $table) " . _subname
1625
          unless  ref $finfo eq 'HASH';
cleanup
Yuki Kimoto authored on 2011-10-21
1626
        for my $ftype (keys %$finfo) {
updated pod
Yuki Kimoto authored on 2011-06-21
1627
            croak "$usage (table: $table) " . _subname
1628
              unless $ftype eq 'in' || $ftype eq 'out' || $ftype eq 'end'; 
1629
        }
1630
        
1631
        # Set filters
cleanup
Yuki Kimoto authored on 2011-10-21
1632
        for my $way (qw/in out end/) {
updated pod
Yuki Kimoto authored on 2011-06-21
1633
        
1634
            # Filter
1635
            my $filter = $finfo->{$way};
1636
            
1637
            # Filter state
1638
            my $state = !exists $finfo->{$way} ? 'not_exists'
1639
                      : !defined $filter        ? 'not_defined'
1640
                      : ref $filter eq 'CODE'   ? 'code'
1641
                      : 'name';
1642
            
1643
            # Filter is not exists
1644
            next if $state eq 'not_exists';
1645
            
1646
            # Check filter name
1647
            croak qq{Filter "$filter" is not registered } . _subname
1648
              if  $state eq 'name'
1649
               && ! exists $self->filters->{$filter};
1650
            
1651
            # Set filter
1652
            my $f = $state eq 'not_defined' ? undef
1653
                  : $state eq 'code'        ? $filter
1654
                  : $self->filters->{$filter};
1655
            $self->{filter}{$way}{$table}{$column} = $f;
1656
            $self->{filter}{$way}{$table}{"$table.$column"} = $f;
1657
            $self->{filter}{$way}{$table}{"${table}__$column"} = $f;
1658
            $self->{filter}{$way}{$table}{"${table}-$column"} = $f;
1659
        }
1660
    }
1661
    
1662
    return $self;
1663
}
1664

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1665
# DEPRECATED!
1666
has 'data_source';
1667
has dbi_options => sub { {} };
1668
has filter_check  => 1;
1669
has 'reserved_word_quote';
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1670
has dbi_option => sub { {} };
1671
has default_dbi_option => sub {
1672
    warn "default_dbi_option is DEPRECATED! use default_option instead";
1673
    return shift->default_option;
1674
};
1675

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1676
# DEPRECATED!
1677
sub method {
1678
    warn "method is DEPRECATED! use helper instead";
1679
    return shift->helper(@_);
1680
}
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1681

            
1682
# DEPRECATED!
1683
sub assign_param {
1684
    my $self = shift;
1685
    warn "assing_param is DEPRECATED! use assign_clause instead";
1686
    return $self->assign_clause(@_);
1687
}
1688

            
1689
# DEPRECATED
1690
sub update_param {
1691
    my ($self, $param, $opts) = @_;
1692
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1693
    warn "update_param is DEPRECATED! use assign_clause instead.";
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1694
    
1695
    # Create update parameter tag
1696
    my $tag = $self->assign_clause($param, $opts);
1697
    $tag = "set $tag" unless $opts->{no_set};
1698

            
1699
    return $tag;
1700
}
1701

            
updated pod
Yuki Kimoto authored on 2011-06-21
1702
# DEPRECATED!
1703
sub create_query {
1704
    warn "create_query is DEPRECATED! use query option of each method";
1705
    shift->_create_query(@_);
1706
}
1707

            
cleanup
Yuki Kimoto authored on 2011-06-13
1708
# DEPRECATED!
1709
sub apply_filter {
1710
    my $self = shift;
1711
    
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
1712
    warn "apply_filter is DEPRECATED!";
cleanup
Yuki Kimoto authored on 2011-06-13
1713
    return $self->_apply_filter(@_);
1714
}
1715

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

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

            
cleanup
Yuki Kimoto authored on 2011-10-21
1722
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
1723
    my $primary_keys = delete $opt{primary_key};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1724
    $primary_keys = [$primary_keys] unless ref $primary_keys;
cleanup
Yuki Kimoto authored on 2011-10-21
1725
    my $where = delete $opt{where};
1726
    my $param = delete $opt{param};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1727
    
1728
    # Table
1729
    croak qq{"table" option must be specified } . _subname
cleanup
Yuki Kimoto authored on 2011-10-21
1730
      unless $opt{table};
1731
    my $table = ref $opt{table} ? $opt{table}->[-1] : $opt{table};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1732
    
1733
    # Create where parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1734
    my $where_param = $self->_id_to_param($where, $primary_keys);
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1735
    
cleanup
Yuki Kimoto authored on 2011-10-21
1736
    return $self->select(where => $where_param, %opt);
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1737
}
1738

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

            
cleanup
Yuki Kimoto authored on 2011-10-21
1743
    warn "delete_at is DEPRECATED! use delete method id option instead";
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1744
    
cleanup
Yuki Kimoto authored on 2011-10-21
1745
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
1746
    my $primary_keys = delete $opt{primary_key};
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1747
    $primary_keys = [$primary_keys] unless ref $primary_keys;
cleanup
Yuki Kimoto authored on 2011-10-21
1748
    my $where = delete $opt{where};
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1749
    
1750
    # Create where parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1751
    my $where_param = $self->_id_to_param($where, $primary_keys);
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1752
    
cleanup
Yuki Kimoto authored on 2011-10-21
1753
    return $self->delete(where => $where_param, %opt);
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1754
}
1755

            
cleanup
Yuki Kimoto authored on 2011-06-08
1756
# DEPRECATED!
1757
sub update_at {
1758
    my $self = shift;
1759

            
cleanup
Yuki Kimoto authored on 2011-10-21
1760
    warn "update_at is DEPRECATED! use update method id option instead";
cleanup
Yuki Kimoto authored on 2011-06-08
1761
    
cleanup
Yuki Kimoto authored on 2011-10-21
1762
    # Options
cleanup
Yuki Kimoto authored on 2011-06-08
1763
    my $param;
1764
    $param = shift if @_ % 2;
cleanup
Yuki Kimoto authored on 2011-10-21
1765
    my %opt = @_;
1766
    my $primary_keys = delete $opt{primary_key};
cleanup
Yuki Kimoto authored on 2011-06-08
1767
    $primary_keys = [$primary_keys] unless ref $primary_keys;
cleanup
Yuki Kimoto authored on 2011-10-21
1768
    my $where = delete $opt{where};
1769
    my $p = delete $opt{param} || {};
cleanup
Yuki Kimoto authored on 2011-06-08
1770
    $param  ||= $p;
1771
    
1772
    # Create where parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1773
    my $where_param = $self->_id_to_param($where, $primary_keys);
cleanup
Yuki Kimoto authored on 2011-06-08
1774
    
cleanup
Yuki Kimoto authored on 2011-10-21
1775
    return $self->update(where => $where_param, param => $param, %opt);
cleanup
Yuki Kimoto authored on 2011-06-08
1776
}
1777

            
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1778
# DEPRECATED!
1779
sub insert_at {
1780
    my $self = shift;
1781
    
cleanup
Yuki Kimoto authored on 2011-10-21
1782
    warn "insert_at is DEPRECATED! use insert method id option instead";
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1783
    
cleanup
Yuki Kimoto authored on 2011-10-21
1784
    # Options
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1785
    my $param;
1786
    $param = shift if @_ % 2;
cleanup
Yuki Kimoto authored on 2011-10-21
1787
    my %opt = @_;
1788
    my $primary_key = delete $opt{primary_key};
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1789
    $primary_key = [$primary_key] unless ref $primary_key;
cleanup
Yuki Kimoto authored on 2011-10-21
1790
    my $where = delete $opt{where};
1791
    my $p = delete $opt{param} || {};
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1792
    $param  ||= $p;
1793
    
1794
    # Create where parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1795
    my $where_param = $self->_id_to_param($where, $primary_key);
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1796
    $param = $self->merge_param($where_param, $param);
1797
    
cleanup
Yuki Kimoto authored on 2011-10-21
1798
    return $self->insert(param => $param, %opt);
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1799
}
1800

            
added warnings
Yuki Kimoto authored on 2011-06-07
1801
# DEPRECATED!
1802
sub register_tag {
test cleanup
Yuki Kimoto authored on 2011-08-10
1803
    my $self = shift;
1804
    
added warnings
Yuki Kimoto authored on 2011-06-07
1805
    warn "register_tag is DEPRECATED!";
test cleanup
Yuki Kimoto authored on 2011-08-10
1806
    
1807
    # Merge tag
1808
    my $tags = ref $_[0] eq 'HASH' ? $_[0] : {@_};
1809
    $self->{_tags} = {%{$self->{_tags} || {}}, %$tags};
1810
    
1811
    return $self;
1812
}
1813

            
1814
# DEPRECATED!
1815
sub register_tag_processor {
1816
    my $self = shift;
1817
    warn "register_tag_processor is DEPRECATED!";
1818
    # Merge tag
1819
    my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_};
1820
    $self->{_tags} = {%{$self->{_tags} || {}}, %{$tag_processors}};
1821
    return $self;
added warnings
Yuki Kimoto authored on 2011-06-07
1822
}
1823

            
cleanup
Yuki Kimoto authored on 2011-01-25
1824
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1825
sub default_bind_filter {
1826
    my $self = shift;
1827
    
cleanup
Yuki Kimoto authored on 2011-06-13
1828
    warn "default_bind_filter is DEPRECATED!";
added warnings
Yuki Kimoto authored on 2011-06-07
1829
    
cleanup
Yuki Kimoto authored on 2011-01-12
1830
    if (@_) {
1831
        my $fname = $_[0];
1832
        
1833
        if (@_ && !$fname) {
1834
            $self->{default_out_filter} = undef;
1835
        }
1836
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1837
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1838
              unless exists $self->filters->{$fname};
1839
        
1840
            $self->{default_out_filter} = $self->filters->{$fname};
1841
        }
1842
        return $self;
1843
    }
1844
    
1845
    return $self->{default_out_filter};
1846
}
1847

            
cleanup
Yuki Kimoto authored on 2011-01-25
1848
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1849
sub default_fetch_filter {
1850
    my $self = shift;
added warnings
Yuki Kimoto authored on 2011-06-07
1851

            
cleanup
Yuki Kimoto authored on 2011-06-13
1852
    warn "default_fetch_filter is DEPRECATED!";
cleanup
Yuki Kimoto authored on 2011-01-12
1853
    
1854
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
1855
        my $fname = $_[0];
1856

            
cleanup
Yuki Kimoto authored on 2011-01-12
1857
        if (@_ && !$fname) {
1858
            $self->{default_in_filter} = undef;
1859
        }
1860
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1861
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1862
              unless exists $self->filters->{$fname};
1863
        
1864
            $self->{default_in_filter} = $self->filters->{$fname};
1865
        }
1866
        
1867
        return $self;
1868
    }
1869
    
many changed
Yuki Kimoto authored on 2011-01-23
1870
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
1871
}
1872

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1873
# DEPRECATED!
1874
sub insert_param {
1875
    my $self = shift;
1876
    warn "insert_param is DEPRECATED! use values_clause instead";
1877
    return $self->values_clause(@_);
1878
}
1879

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1880
# DEPRECATED!
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1881
sub insert_param_tag {
1882
    warn "insert_param_tag is DEPRECATED! " .
1883
         "use insert_param instead!";
1884
    return shift->insert_param(@_);
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1885
}
1886

            
1887
# DEPRECATED!
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1888
sub update_param_tag {
fixed DEPRECATED message bug
Yuki Kimoto authored on 2011-06-10
1889
    warn "update_param_tag is DEPRECATED! " .
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1890
         "use update_param instead";
1891
    return shift->update_param(@_);
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1892
}
cleanup
Yuki Kimoto authored on 2011-03-08
1893
# DEPRECATED!
1894
sub _push_relation {
1895
    my ($self, $sql, $tables, $relation, $need_where) = @_;
1896
    
1897
    if (keys %{$relation || {}}) {
micro optimization
Yuki Kimoto authored on 2011-09-30
1898
        $$sql .= $need_where ? 'where ' : 'and ';
cleanup
Yuki Kimoto authored on 2011-10-21
1899
        for my $rcolumn (keys %$relation) {
cleanup
Yuki Kimoto authored on 2011-03-08
1900
            my $table1 = (split (/\./, $rcolumn))[0];
1901
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1902
            push @$tables, ($table1, $table2);
micro optimization
Yuki Kimoto authored on 2011-09-30
1903
            $$sql .= "$rcolumn = " . $relation->{$rcolumn} .  'and ';
cleanup
Yuki Kimoto authored on 2011-03-08
1904
        }
1905
    }
micro optimization
Yuki Kimoto authored on 2011-09-30
1906
    $$sql =~ s/and $/ /;
cleanup
Yuki Kimoto authored on 2011-03-08
1907
}
1908

            
1909
# DEPRECATED!
1910
sub _add_relation_table {
cleanup
Yuki Kimoto authored on 2011-03-09
1911
    my ($self, $tables, $relation) = @_;
cleanup
Yuki Kimoto authored on 2011-03-08
1912
    
1913
    if (keys %{$relation || {}}) {
cleanup
Yuki Kimoto authored on 2011-10-21
1914
        for my $rcolumn (keys %$relation) {
cleanup
Yuki Kimoto authored on 2011-03-08
1915
            my $table1 = (split (/\./, $rcolumn))[0];
1916
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1917
            my $table1_exists;
1918
            my $table2_exists;
cleanup
Yuki Kimoto authored on 2011-10-21
1919
            for my $table (@$tables) {
cleanup
Yuki Kimoto authored on 2011-03-08
1920
                $table1_exists = 1 if $table eq $table1;
1921
                $table2_exists = 1 if $table eq $table2;
1922
            }
1923
            unshift @$tables, $table1 unless $table1_exists;
1924
            unshift @$tables, $table2 unless $table2_exists;
1925
        }
1926
    }
1927
}
1928

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1931
=head1 NAME
1932

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

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1937
    use DBIx::Custom;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1938
    
1939
    # Connect
1940
    my $dbi = DBIx::Custom->connect(
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
1941
        dsn => "dbi:mysql:database=dbname",
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1942
        user => 'ken',
1943
        password => '!LFKD%$&',
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1944
        option => {mysql_enable_utf8 => 1}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1945
    );
cleanup
yuki-kimoto authored on 2010-08-05
1946

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1947
    # Insert 
updated pod
Yuki Kimoto authored on 2011-06-21
1948
    $dbi->insert({title => 'Perl', author => 'Ken'}, table  => 'book');
removed reconnect method
yuki-kimoto authored on 2010-05-28
1949
    
1950
    # Update 
updated pod
Yuki Kimoto authored on 2011-06-21
1951
    $dbi->update({title => 'Perl', author => 'Ken'}, table  => 'book',
1952
      where  => {id => 5});
removed reconnect method
yuki-kimoto authored on 2010-05-28
1953
    
1954
    # Delete
updated pod
Yuki Kimoto authored on 2011-06-21
1955
    $dbi->delete(table  => 'book', where => {author => 'Ken'});
cleanup
yuki-kimoto authored on 2010-08-05
1956

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1961
    # Select, more complex
1962
    my $result = $dbi->select(
1963
        table  => 'book',
1964
        column => [
cleanup
Yuki Kimoto authored on 2011-06-13
1965
            {book => [qw/title author/]},
1966
            {company => ['name']}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1967
        ],
1968
        where  => {'book.author' => 'Ken'},
1969
        join => ['left outer join company on book.company_id = company.id'],
1970
        append => 'order by id limit 5'
removed reconnect method
yuki-kimoto authored on 2010-05-28
1971
    );
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1972
    
removed register_format()
yuki-kimoto authored on 2010-05-26
1973
    # Fetch
1974
    while (my $row = $result->fetch) {
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1975
        
removed register_format()
yuki-kimoto authored on 2010-05-26
1976
    }
1977
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1978
    # Fetch as hash
removed register_format()
yuki-kimoto authored on 2010-05-26
1979
    while (my $row = $result->fetch_hash) {
1980
        
1981
    }
1982
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1983
    # Execute SQL with parameter.
1984
    $dbi->execute(
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1985
        "select id from book where author = :author and title like :title",
updated pod
Yuki Kimoto authored on 2011-06-21
1986
        {author => 'ken', title => '%Perl%'}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1987
    );
1988
    
fix heading typos
Terrence Brannon authored on 2011-08-17
1989
=head1 DESCRIPTION
removed reconnect method
yuki-kimoto authored on 2010-05-28
1990

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

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

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

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

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

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

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

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2006
Named place holder support
2007

            
2008
=item *
2009

            
cleanup
Yuki Kimoto authored on 2011-07-29
2010
Model support
2011

            
2012
=item *
2013

            
2014
Connection manager support
2015

            
2016
=item *
pod fix
Yuki Kimoto authored on 2011-01-21
2017

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

            
2022
=item *
2023

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

            
2026
=item *
2027

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

            
2030
=back
pod fix
Yuki Kimoto authored on 2011-01-21
2031

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

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

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

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2039
Module documentations - 
cleanup
Yuki Kimoto authored on 2011-07-29
2040
L<DBIx::Custom::Result>,
2041
L<DBIx::Custom::Query>,
2042
L<DBIx::Custom::Where>,
2043
L<DBIx::Custom::Model>,
2044
L<DBIx::Custom::Order>
updated document
yuki-kimoto authored on 2010-08-08
2045

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

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

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

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

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

            
2059
    my $connector = DBIx::Connector->new(
cleanup
Yuki Kimoto authored on 2011-08-16
2060
        "dbi:mysql:database=$database",
2061
        $user,
2062
        $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
2063
        DBIx::Custom->new->default_option
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2064
    );
2065
    
updated pod
Yuki Kimoto authored on 2011-06-21
2066
    my $dbi = DBIx::Custom->connect(connector => $connector);
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2067

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

            
2071
    my $dbi = DBIx::Custom->connect(
2072
      dsn => $dsn, user => $user, password => $password, connector => 1);
2073
    
2074
    my $connector = $dbi->connector; # DBIx::Connector
2075

            
2076
Note that L<DBIx::Connector> must be installed.
2077

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

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

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

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

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2093
    {
2094
        RaiseError => 1,
2095
        PrintError => 0,
2096
        AutoCommit => 1,
2097
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
2098

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

            
2101
    my $exclude_table = $dbi->exclude_table;
2102
    $dbi = $dbi->exclude_table(qr/pg_/);
2103

            
2104
Excluded table regex.
2105
C<each_column>, C<each_table>, C<type_rule>,
2106
and C<setup_model> methods ignore matching tables.
2107

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

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

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

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

            
2117
    my $last_sql = $dbi->last_sql;
2118
    $dbi = $dbi->last_sql($last_sql);
2119

            
2120
Get last successed SQL executed by C<execute> method.
2121

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

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

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

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

            
2131
    my $option = $dbi->option;
2132
    $dbi = $dbi->option($option);
2133

            
2134
L<DBI> option, used when C<connect> method is executed.
2135
Each value in option override the value of C<default_option>.
2136

            
cleanup
yuki-kimoto authored on 2010-10-17
2137
=head2 C<password>
2138

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

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-07-30
2159
You can set quote pair.
2160

            
2161
    $dbi->quote('[]');
2162

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

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

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

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

            
fixed pod
Yuki Kimoto authored on 2011-09-27
2172
    my $safety_character = $dbi->safety_character;
2173
    $dbi = $dbi->safety_character($character);
update pod
Yuki Kimoto authored on 2011-01-27
2174

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

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

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

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

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

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

            
2191
    my $tag_parse = $dbi->tag_parse(0);
2192
    $dbi = $dbi->tag_parse;
2193

            
2194
Enable DEPRECATED tag parsing functionality, default to 1.
2195
If you want to disable tag parsing functionality, set to 0.
2196

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

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

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

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

            
2206
    my $user_column_info = $dbi->user_column_info;
2207
    $dbi = $dbi->user_column_info($user_column_info);
2208

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

            
2211
    [
2212
        {table => 'book', column => 'title', info => {...}},
2213
        {table => 'author', column => 'name', info => {...}}
2214
    ]
2215

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

            
2218
    my $user_column_info
2219
      = $dbi->get_column_info(exclude_table => qr/^system/);
2220
    $dbi->user_column_info($user_column_info);
2221

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

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

            
2227
    my $user_table_info = $dbi->user_table_info;
2228
    $dbi = $dbi->user_table_info($user_table_info);
2229

            
2230
You can set the following data.
2231

            
2232
    [
2233
        {table => 'book', info => {...}},
2234
        {table => 'author', info => {...}}
2235
    ]
2236

            
2237
Usually, you can set return value of C<get_table_info>.
2238

            
2239
    my $user_table_info = $dbi->get_table_info(exclude => qr/^system/);
2240
    $dbi->user_table_info($user_table_info);
2241

            
2242
If C<user_table_info> is set, C<each_table> use C<user_table_info>
2243
to find table info.
2244

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2281
Create column clause. The follwoing column clause is created.
2282

            
2283
    book.author as "book.author",
2284
    book.title as "book.title"
2285

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

            
cleanup
Yuki Kimoto authored on 2011-06-13
2288
    # Separator is hyphen
2289
    $dbi->separator('-');
2290
    
2291
    book.author as "book-author",
2292
    book.title as "book-title"
2293
    
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
2294
=head2 C<connect>
packaging one directory
yuki-kimoto authored on 2009-11-16
2295

            
update pod
Yuki Kimoto authored on 2011-03-13
2296
    my $dbi = DBIx::Custom->connect(
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
2297
        dsn => "dbi:mysql:database=dbname",
update pod
Yuki Kimoto authored on 2011-03-13
2298
        user => 'ken',
2299
        password => '!LFKD%$&',
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
2300
        option => {mysql_enable_utf8 => 1}
update pod
Yuki Kimoto authored on 2011-03-13
2301
    );
2302

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

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

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

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

            
2313
Get rows count.
2314

            
2315
Options is same as C<select> method's ones.
2316

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

            
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
2319
    my $model = $dbi->create_model(
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2320
        table => 'book',
2321
        primary_key => 'id',
2322
        join => [
2323
            'inner join company on book.comparny_id = company.id'
2324
        ],
2325
    );
2326

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

            
2330
   $dbi->model('book')->select(...);
2331

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

            
2334
    my $dbh = $dbi->dbh;
2335

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

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

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

            
2343
Execute delete statement.
2344

            
2345
The following opitons are available.
2346

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

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

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

            
2354
=item C<id>
2355

            
2356
    id => 4
2357
    id => [4, 5]
2358

            
2359
ID corresponding to C<primary_key>.
2360
You can delete rows by C<id> and C<primary_key>.
2361

            
2362
    $dbi->delete(
fixed pod
Yuki Kimoto authored on 2011-10-20
2363
        primary_key => ['id1', 'id2'],
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2364
        id => [4, 5],
2365
        table => 'book',
2366
    );
2367

            
2368
The above is same as the followin one.
2369

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

            
2372
=item C<prefix>
2373

            
2374
    prefix => 'some'
2375

            
2376
prefix before table name section.
2377

            
2378
    delete some from book
2379

            
2380
=item C<table>
2381

            
2382
    table => 'book'
2383

            
2384
Table name.
2385

            
2386
=item C<where>
2387

            
2388
Same as C<select> method's C<where> option.
2389

            
2390
=back
2391

            
2392
=head2 C<delete_all>
2393

            
2394
    $dbi->delete_all(table => $table);
2395

            
2396
Execute delete statement for all rows.
2397
Options is same as C<delete>.
2398

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

            
2401
    $dbi->each_column(
2402
        sub {
2403
            my ($dbi, $table, $column, $column_info) = @_;
2404
            
2405
            my $type = $column_info->{TYPE_NAME};
2406
            
2407
            if ($type eq 'DATE') {
2408
                # ...
2409
            }
2410
        }
2411
    );
2412

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

            
2418
If C<user_column_info> is set, C<each_column> method use C<user_column_info>
2419
infromation, you can improve the performance of C<each_column> in
2420
the following way.
2421

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

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

            
2428
    $dbi->each_table(
2429
        sub {
2430
            my ($dbi, $table, $table_info) = @_;
2431
            
2432
            my $table_name = $table_info->{TABLE_NAME};
2433
        }
2434
    );
2435

            
improved pod
Yuki Kimoto authored on 2011-10-14
2436
Iterate all table informationsfrom in database.
2437
Argument is callback which is executed when one table is found.
2438
Callback receive three arguments, C<DBIx::Custom object>, C<table name>,
2439
C<table information>.
2440

            
2441
If C<user_table_info> is set, C<each_table> method use C<user_table_info>
2442
infromation, you can improve the performance of C<each_table> in
2443
the following way.
2444

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

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

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

            
2456
    my $result = $dbi->execute(
2457
      "select * from book where title = :book.title and author like :book.author",
2458
      {'book.title' => 'Perl', 'book.author' => '%Ken%'}
update pod
Yuki Kimoto authored on 2011-03-13
2459
    );
2460

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

            
I call :title named placehol...
Yuki Kimoto authored on 2011-07-30
2467
Named placeholder such as C<:title> is replaced by placeholder C<?>.
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2468
    
micro optimization
Yuki Kimoto authored on 2011-07-30
2469
    # Original
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2470
    select * from book where title = :title and author like :author
2471
    
micro optimization
Yuki Kimoto authored on 2011-07-30
2472
    # Replaced
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2473
    select * from where title = ? and author like ?;
update pod
Yuki Kimoto authored on 2011-03-13
2474

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

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2478
    # Original
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2479
    select * from book where :title{=} and :author{like}
2480
    
micro optimization
Yuki Kimoto authored on 2011-07-30
2481
    # Replaced
update pod
Yuki Kimoto authored on 2011-03-13
2482
    select * from where title = ? and author like ?;
2483

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

            
2488
    select * from where title = "aa\\:bb";
2489

            
cleanup
Yuki Kimoto authored on 2011-10-20
2490
B<OPTIONS>
2491

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

            
2494
=over 4
2495

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

            
2498
You can filter sql after the sql is build.
2499

            
2500
    after_build_sql => $code_ref
2501

            
2502
The following one is one example.
2503

            
2504
    $dbi->select(
2505
        table => 'book',
2506
        column => 'distinct(name)',
2507
        after_build_sql => sub {
2508
            "select count(*) from ($_[0]) as t1"
2509
        }
2510
    );
2511

            
2512
The following SQL is executed.
2513

            
2514
    select count(*) from (select distinct(name) from book) as t1;
2515

            
cleanup
Yuki Kimoto authored on 2011-10-20
2516
=item C<append>
2517

            
2518
    append => 'order by name'
2519

            
2520
Append some statement after SQL.
2521

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

            
2524
Specify database bind data type.
2525

            
2526
    bind_type => [image => DBI::SQL_BLOB]
2527
    bind_type => [[qw/image audio/] => DBI::SQL_BLOB]
2528

            
2529
This is used to bind parameter by C<bind_param> of statment handle.
2530

            
2531
    $sth->bind_param($pos, $value, DBI::SQL_BLOB);
2532

            
update pod
Yuki Kimoto authored on 2011-03-13
2533
=item C<filter>
updated pod
Yuki Kimoto authored on 2011-06-09
2534
    
2535
    filter => {
2536
        title  => sub { uc $_[0] }
2537
        author => sub { uc $_[0] }
2538
    }
update pod
Yuki Kimoto authored on 2011-03-13
2539

            
updated pod
Yuki Kimoto authored on 2011-06-09
2540
    # Filter name
2541
    filter => {
2542
        title  => 'upper_case',
2543
        author => 'upper_case'
2544
    }
2545
        
2546
    # At once
2547
    filter => [
2548
        [qw/title author/]  => sub { uc $_[0] }
2549
    ]
2550

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

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

            
2558
    id => 4
2559
    id => [4, 5]
2560

            
2561
ID corresponding to C<primary_key>.
2562
You can delete rows by C<id> and C<primary_key>.
2563

            
2564
    $dbi->execute(
2565
        "select * from book where id1 = :id1 and id2 = :id2",
2566
        {},
fixed pod
Yuki Kimoto authored on 2011-10-20
2567
        primary_key => ['id1', 'id2'],
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
2568
        id => [4, 5],
2569
    );
2570

            
2571
The above is same as the followin one.
2572

            
2573
    $dbi->execute(
2574
        "select * from book where id1 = :id1 and id2 = :id2",
2575
        {id1 => 4, id2 => 5}
2576
    );
2577

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

            
2580
    query => 1
2581

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

            
2585
    my $sql = $query->sql;
cleanup
Yuki Kimoto authored on 2011-07-30
2586
    my $sth = $query->sth;
Added execute method's query...
Yuki Kimoto authored on 2011-07-30
2587
    my $columns = $query->columns;
2588
    
2589
If you want to execute SQL fast, you can do the following way.
2590

            
2591
    my $query;
cleanup
Yuki Kimoto authored on 2011-10-21
2592
    for my $row (@$rows) {
Added execute method's query...
Yuki Kimoto authored on 2011-07-30
2593
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
cleanup
Yuki Kimoto authored on 2011-10-20
2594
      $dbi->execute($query, $row);
Added execute method's query...
Yuki Kimoto authored on 2011-07-30
2595
    }
2596

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

            
2600
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
2601
You can do the following way.
cleanup
Yuki Kimoto authored on 2011-07-30
2602
    
2603
    my $query;
2604
    my $sth;
cleanup
Yuki Kimoto authored on 2011-10-21
2605
    for my $row (@$rows) {
cleanup
Yuki Kimoto authored on 2011-07-30
2606
      $query ||= $dbi->insert($row, table => 'book', query => 1);
2607
      $sth ||= $query->sth;
2608
      $sth->execute(map { $row->{$_} } sort keys %$row);
2609
    }
2610

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

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

            
cleanup
Yuki Kimoto authored on 2011-10-20
2617
    primary_key => 'id'
2618
    primary_key => ['id1', 'id2']
2619

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

            
updated pod
Yuki Kimoto authored on 2011-06-09
2622
=item C<table>
2623
    
2624
    table => 'author'
2625

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

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

            
updated pod
Yuki Kimoto authored on 2011-06-21
2633
    # Same
2634
    $dbi->execute(
2635
      "select * from book where title = :book.title and author = :book.author",
2636
      {title => 'Perl', author => 'Ken');
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2637

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

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

            
2642
Table alias. Key is real table name, value is alias table name.
2643
If you set C<table_alias>, you can enable C<into1> and C<into2> type rule
2644
on alias table name.
2645

            
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
2646
=item C<reuse EXPERIMENTAL>
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2647
    
micro optimization
Yuki Kimoto authored on 2011-10-23
2648
    reuse => $has_ref
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2649

            
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
2650
Reuse query object if the hash reference variable is set.
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2651
    
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
2652
    my $queries = {};
2653
    $dbi->execute($sql, $param, reuse => $queries);
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2654

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

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

            
2660
    type_rule_off => 1
2661

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

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

            
2666
    type_rule1_off => 1
2667

            
2668
Turn C<into1> type rule off.
2669

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

            
2672
    type_rule2_off => 1
2673

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

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

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

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

            
2682
get column infomation except for one which match C<exclude_table> pattern.
2683

            
2684
    [
2685
        {table => 'book', column => 'title', info => {...}},
2686
        {table => 'author', column => 'name' info => {...}}
2687
    ]
2688

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

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

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

            
2695
    [
2696
        {table => 'book', info => {...}},
2697
        {table => 'author', info => {...}}
2698
    ]
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2699

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

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

            
2704
    $dbi->helper(
2705
        update_or_insert => sub {
2706
            my $self = shift;
2707
            
2708
            # Process
2709
        },
2710
        find_or_create   => sub {
2711
            my $self = shift;
2712
            
2713
            # Process
2714
        }
2715
    );
2716

            
2717
Register helper. These helper is called directly from L<DBIx::Custom> object.
2718

            
2719
    $dbi->update_or_insert;
2720
    $dbi->find_or_create;
2721

            
cleanup
yuki-kimoto authored on 2010-10-17
2722
=head2 C<insert>
2723

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

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

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

            
2732
    {date => \"NOW()"}
2733

            
cleanup
Yuki Kimoto authored on 2011-10-20
2734
B<options>
2735

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

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

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

            
updated document
Yuki Kimoto authored on 2011-06-09
2743
    id => 4
2744
    id => [4, 5]
update pod
Yuki Kimoto authored on 2011-03-13
2745

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2749
    $dbi->insert(
updated document
Yuki Kimoto authored on 2011-06-09
2750
        {title => 'Perl', author => 'Ken'}
fixed pod
Yuki Kimoto authored on 2011-10-20
2751
        primary_key => ['id1', 'id2'],
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2752
        id => [4, 5],
updated document
Yuki Kimoto authored on 2011-06-09
2753
        table => 'book'
update pod
Yuki Kimoto authored on 2011-03-13
2754
    );
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2755

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

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

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

            
2765
    prefix => 'or replace'
2766

            
2767
prefix before table name section
2768

            
2769
    insert or replace into book
2770

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

            
2773
    table => 'book'
2774

            
2775
Table name.
2776

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

            
2779
    timestamp => 1
2780

            
2781
If this value is set to 1,
2782
automatically created timestamp column is set based on
2783
C<timestamp> attribute's C<insert> value.
2784

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

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

            
2789
placeholder wrapped string.
2790

            
2791
If the following statement
2792

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

            
2796
is executed, the following SQL is executed.
2797

            
2798
    insert into book price values ( ? + 5 );
2799

            
update pod
Yuki Kimoto authored on 2011-03-13
2800
=back
2801

            
2802
=over 4
2803

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

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2811
    lib / MyModel.pm
2812
        / MyModel / book.pm
2813
                  / company.pm
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2814

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

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

            
2819
    package MyModel;
updated pod
Yuki Kimoto authored on 2011-06-21
2820
    use DBIx::Custom::Model -base;
update pod
Yuki Kimoto authored on 2011-03-13
2821
    
2822
    1;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2823

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2828
    package MyModel::book;
updated pod
Yuki Kimoto authored on 2011-06-21
2829
    use MyModel -base;
update pod
Yuki Kimoto authored on 2011-03-13
2830
    
2831
    1;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2832

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2835
    package MyModel::company;
updated pod
Yuki Kimoto authored on 2011-06-21
2836
    use MyModel -base;
update pod
Yuki Kimoto authored on 2011-03-13
2837
    
2838
    1;
2839
    
updated pod
Yuki Kimoto authored on 2011-06-21
2840
MyModel::book and MyModel::company is included by C<include_model>.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2841

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

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

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

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

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

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

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

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

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

            
2867
    my $like_value = $dbi->like_value
2868

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

            
2871
    sub { "%$_[0]%" }
2872

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

            
2875
    my $mapper = $dbi->mapper(param => $param);
2876

            
2877
Create a new L<DBIx::Custom::Mapper> object.
2878

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

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

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

            
2885
    {key1 => [1, 1], key2 => 2}
2886

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

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

            
2891
    my $model = $dbi->model('book');
2892

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

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

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

            
2900
Create column clause for myself. The follwoing column clause is created.
2901

            
2902
    book.author as author,
2903
    book.title as title
2904

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

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

            
2914
Create a new L<DBIx::Custom> object.
2915

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

            
2918
    my $not_exists = $dbi->not_exists;
2919

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

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

            
2925
    my $order = $dbi->order;
2926

            
2927
Create a new L<DBIx::Custom::Order> object.
2928

            
cleanup
yuki-kimoto authored on 2010-10-17
2929
=head2 C<register_filter>
2930

            
update pod
Yuki Kimoto authored on 2011-03-13
2931
    $dbi->register_filter(
2932
        # Time::Piece object to database DATE format
2933
        tp_to_date => sub {
2934
            my $tp = shift;
2935
            return $tp->strftime('%Y-%m-%d');
2936
        },
2937
        # database DATE format to Time::Piece object
2938
        date_to_tp => sub {
2939
           my $date = shift;
2940
           return Time::Piece->strptime($date, '%Y-%m-%d');
2941
        }
2942
    );
cleanup
yuki-kimoto authored on 2010-10-17
2943
    
update pod
Yuki Kimoto authored on 2011-03-13
2944
Register filters, used by C<filter> option of many methods.
cleanup
yuki-kimoto authored on 2010-10-17
2945

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

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
2948
    my $result = $dbi->select(
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2949
        table  => 'book',
2950
        column => ['author', 'title'],
2951
        where  => {author => 'Ken'},
select method column option ...
Yuki Kimoto authored on 2011-02-22
2952
    );
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2953
    
updated document
Yuki Kimoto authored on 2011-06-09
2954
Execute select statement.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2955

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

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

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

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2963
=item C<column>
2964
    
updated document
Yuki Kimoto authored on 2011-06-09
2965
    column => 'author'
2966
    column => ['author', 'title']
2967

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

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

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

            
updated document
Yuki Kimoto authored on 2011-06-09
2976
    column => [
updated pod
Yuki Kimoto authored on 2011-06-07
2977
        {book => [qw/author title/]},
2978
        {person => [qw/name age/]}
updated document
Yuki Kimoto authored on 2011-06-09
2979
    ]
updated pod
Yuki Kimoto authored on 2011-06-07
2980

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

            
2983
    book.author as "book.author",
2984
    book.title as "book.title",
2985
    person.name as "person.name",
2986
    person.age as "person.age"
2987

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

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

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

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

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

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

            
3003
=item C<id>
3004

            
3005
    id => 4
3006
    id => [4, 5]
3007

            
3008
ID corresponding to C<primary_key>.
3009
You can select rows by C<id> and C<primary_key>.
3010

            
3011
    $dbi->select(
fixed pod
Yuki Kimoto authored on 2011-10-20
3012
        primary_key => ['id1', 'id2'],
updated document
Yuki Kimoto authored on 2011-06-09
3013
        id => [4, 5],
3014
        table => 'book'
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3015
    );
3016

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

            
updated pod
Yuki Kimoto authored on 2011-04-25
3019
    $dbi->select(
updated document
Yuki Kimoto authored on 2011-06-09
3020
        where => {id1 => 4, id2 => 5},
3021
        table => 'book'
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
3022
    );
3023
    
cleanup
Yuki Kimoto authored on 2011-10-20
3024
=item C<param>
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3025

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

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

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

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

            
3038
    prefix => 'SQL_CALC_FOUND_ROWS'
3039

            
3040
Prefix of column cluase
3041

            
3042
    select SQL_CALC_FOUND_ROWS title, author from book;
3043

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

            
3046
    join => [
3047
        'left outer join company on book.company_id = company_id',
3048
        'left outer join location on company.location_id = location.id'
3049
    ]
3050
        
3051
Join clause. If column cluase or where clause contain table name like "company.name",
3052
join clausees needed when SQL is created is used automatically.
update pod
Yuki Kimoto authored on 2011-03-12
3053

            
3054
    $dbi->select(
3055
        table => 'book',
cleanup
Yuki Kimoto authored on 2011-06-13
3056
        column => ['company.location_id as location_id'],
update pod
Yuki Kimoto authored on 2011-03-12
3057
        where => {'company.name' => 'Orange'},
3058
        join => [
3059
            'left outer join company on book.company_id = company.id',
3060
            'left outer join location on company.location_id = location.id'
3061
        ]
3062
    );
3063

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

            
cleanup
Yuki Kimoto authored on 2011-06-13
3067
    select company.location_id as location_id
update pod
Yuki Kimoto authored on 2011-03-12
3068
    from book
3069
      left outer join company on book.company_id = company.id
cleanup
Yuki Kimoto authored on 2011-06-13
3070
    where company.name = ?;
update pod
Yuki Kimoto authored on 2011-03-12
3071

            
added join new syntax
Yuki Kimoto authored on 2011-07-28
3072
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
3073
the join clause correctly.
added join new syntax
Yuki Kimoto authored on 2011-07-28
3074

            
3075
    $dbi->select(
3076
        table => 'book',
3077
        column => ['company.location_id as location_id'],
3078
        where => {'company.name' => 'Orange'},
3079
        join => [
3080
            {
3081
                clause => 'left outer join location on company.location_id = location.id',
3082
                table => ['company', 'location']
3083
            }
3084
        ]
3085
    );
3086

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

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

            
updated document
Yuki Kimoto authored on 2011-06-09
3091
Table name.
updated pod
Yuki Kimoto authored on 2011-06-08
3092

            
updated document
Yuki Kimoto authored on 2011-06-09
3093
=item C<where>
3094
    
3095
    # Hash refrence
3096
    where => {author => 'Ken', 'title' => 'Perl'}
3097
    
3098
    # DBIx::Custom::Where object
3099
    where => $dbi->where(
3100
        clause => ['and', 'author = :author', 'title like :title'],
3101
        param  => {author => 'Ken', title => '%Perl%'}
3102
    );
updated pod
Yuki Kimoto authored on 2011-06-21
3103
    
3104
    # Array reference 1 (array reference, hash referenc). same as above
3105
    where => [
3106
        ['and', 'author = :author', 'title like :title'],
3107
        {author => 'Ken', title => '%Perl%'}
3108
    ];    
3109
    
3110
    # Array reference 2 (String, hash reference)
3111
    where => [
3112
        'title like :title',
3113
        {title => '%Perl%'}
3114
    ]
3115
    
3116
    # String
3117
    where => 'title is null'
update pod
Yuki Kimoto authored on 2011-03-12
3118

            
cleanup
Yuki Kimoto authored on 2011-10-20
3119
Where clause. See L<DBIx::Custom::Where>.
updated document
Yuki Kimoto authored on 2011-06-09
3120
    
update pod
Yuki Kimoto authored on 2011-03-12
3121
=back
cleanup
Yuki Kimoto authored on 2011-03-08
3122

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

            
3125
    $dbi->setup_model;
3126

            
3127
Setup all model objects.
3128
C<columns> of model object is automatically set, parsing database information.
3129

            
3130
=head2 C<type_rule>
3131

            
3132
    $dbi->type_rule(
3133
        into1 => {
3134
            date => sub { ... },
3135
            datetime => sub { ... }
3136
        },
3137
        into2 => {
3138
            date => sub { ... },
3139
            datetime => sub { ... }
3140
        },
3141
        from1 => {
3142
            # DATE
3143
            9 => sub { ... },
3144
            # DATETIME or TIMESTAMP
3145
            11 => sub { ... },
3146
        }
3147
        from2 => {
3148
            # DATE
3149
            9 => sub { ... },
3150
            # DATETIME or TIMESTAMP
3151
            11 => sub { ... },
3152
        }
3153
    );
3154

            
3155
Filtering rule when data is send into and get from database.
3156
This has a little complex problem.
3157

            
3158
In C<into1> and C<into2> you can specify
3159
type name as same as type name defined
3160
by create table, such as C<DATETIME> or C<DATE>.
3161

            
3162
Note that type name and data type don't contain upper case.
3163
If these contain upper case charactor, you convert it to lower case.
3164

            
3165
C<into2> is executed after C<into1>.
3166

            
3167
Type rule of C<into1> and C<into2> is enabled on the following
3168
column name.
3169

            
3170
=over 4
3171

            
3172
=item 1. column name
3173

            
3174
    issue_date
3175
    issue_datetime
3176

            
3177
This need C<table> option in each method.
3178

            
3179
=item 2. table name and column name, separator is dot
3180

            
3181
    book.issue_date
3182
    book.issue_datetime
3183

            
3184
=back
3185

            
3186
You get all type name used in database by C<available_typename>.
3187

            
3188
    print $dbi->available_typename;
3189

            
3190
In C<from1> and C<from2> you specify data type, not type name.
3191
C<from2> is executed after C<from1>.
3192
You get all data type by C<available_datatype>.
3193

            
3194
    print $dbi->available_datatype;
3195

            
3196
You can also specify multiple types at once.
3197

            
3198
    $dbi->type_rule(
3199
        into1 => [
3200
            [qw/DATE DATETIME/] => sub { ... },
3201
        ],
3202
    );
3203

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

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

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

            
3210
If you want to set constant value to row data, use scalar reference
3211
as parameter value.
3212

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

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

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

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

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

            
updated document
Yuki Kimoto authored on 2011-06-09
3224
    id => 4
3225
    id => [4, 5]
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
3226

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

            
DEPRECATED select() param op...
Yuki Kimoto authored on 2011-04-25
3230
    $dbi->update(
updated document
Yuki Kimoto authored on 2011-06-09
3231
        {title => 'Perl', author => 'Ken'}
fixed pod
Yuki Kimoto authored on 2011-10-20
3232
        primary_key => ['id1', 'id2'],
updated document
Yuki Kimoto authored on 2011-06-09
3233
        id => [4, 5],
3234
        table => 'book'
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
3235
    );
update pod
Yuki Kimoto authored on 2011-03-13
3236

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

            
updated document
Yuki Kimoto authored on 2011-06-09
3239
    $dbi->update(
3240
        {title => 'Perl', author => 'Ken'}
3241
        where => {id1 => 4, id2 => 5},
3242
        table => 'book'
3243
    );
update pod
Yuki Kimoto authored on 2011-03-13
3244

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

            
3247
    prefix => 'or replace'
3248

            
3249
prefix before table name section
3250

            
3251
    update or replace book
3252

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

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

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

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

            
3261
    timestamp => 1
3262

            
3263
If this value is set to 1,
3264
automatically updated timestamp column is set based on
3265
C<timestamp> attribute's C<update> value.
3266

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

            
3269
Same as C<select> method's C<where> option.
3270

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

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

            
3275
placeholder wrapped string.
3276

            
3277
If the following statement
3278

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

            
3282
is executed, the following SQL is executed.
3283

            
3284
    update book set price =  ? + 5;
3285

            
updated pod
Yuki Kimoto authored on 2011-06-08
3286
=back
update pod
Yuki Kimoto authored on 2011-03-13
3287

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

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

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

            
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
3295
=head2 C<update_or_insert EXPERIMENTAL>
3296
    
3297
    # Where
3298
    $dbi->update_or_insert(
3299
        {id => 1, title => 'Perl'},
3300
        table => 'book',
3301
        where => {id => 1},
3302
        select_option => {append => 'for update'}
3303
    );
3304
    
3305
    # ID
3306
    $dbi->update_or_insert(
3307
        {title => 'Perl'},
3308
        table => 'book',
3309
        id => 1,
3310
        primary_key => 'id',
3311
        select_option => {append => 'for update'}
3312
    );
3313
    
3314
Update or insert.
3315

            
3316
In both examples, the following SQL is executed.
3317

            
3318
    # In case insert
3319
    insert into book (id, title) values (?, ?)
3320
    
3321
    # In case update
3322
    update book set (id = ?, title = ?) where book.id = ?
3323

            
3324
The following opitons are available adding to C<update> option.
3325

            
3326
=over 4
3327

            
3328
=item C<select_option>
3329

            
3330
    select_option => {append => 'for update'}
3331

            
3332
select method option,
3333
select method is used to check the row is already exists.
3334

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

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

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

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

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

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

            
3355
    $dbi->show_datatype($table);
3356

            
3357
Show data type of the columns of specified table.
3358

            
3359
    book
3360
    title: 5
3361
    issue_date: 91
3362

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

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

            
3367
    $dbi->show_tables;
3368

            
3369
Show tables.
3370

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

            
3373
    $dbi->show_typename($table);
3374

            
3375
Show type name of the columns of specified table.
3376

            
3377
    book
3378
    title: varchar
3379
    issue_date: date
3380

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

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

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

            
3387
Create values clause.
3388

            
3389
    (title, author) values (title = :title, age = :age);
3390

            
3391
You can use this in insert statement.
3392

            
3393
    my $insert_sql = "insert into book $values_clause";
3394

            
3395
=head2 C<where>
3396

            
3397
    my $where = $dbi->where(
3398
        clause => ['and', 'title = :title', 'author = :author'],
3399
        param => {title => 'Perl', author => 'Ken'}
3400
    );
3401

            
3402
Create a new L<DBIx::Custom::Where> object.
3403

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

            
3406
=head2 C<DBIX_CUSTOM_DEBUG>
3407

            
3408
If environment variable C<DBIX_CUSTOM_DEBUG> is set to true,
3409
executed SQL and bind values are printed to STDERR.
3410

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

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

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

            
3417
L<DBIx::Custom>
3418

            
3419
    # Attribute methods
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
3420
    default_dbi_option # will be removed 2017/1/1
3421
    dbi_option # will be removed 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3422
    data_source # will be removed at 2017/1/1
3423
    dbi_options # will be removed at 2017/1/1
3424
    filter_check # will be removed at 2017/1/1
3425
    reserved_word_quote # will be removed at 2017/1/1
3426
    cache_method # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3427
    
3428
    # Methods
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
3429
    method # will be removed at 2017/1/1
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
3430
    assign_param # will be removed at 2017/1/1
3431
    update_param # will be removed at 2017/1/1
3432
    insert_param # will be removed at 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3433
    create_query # will be removed at 2017/1/1
3434
    apply_filter # will be removed at 2017/1/1
3435
    select_at # will be removed at 2017/1/1
3436
    delete_at # will be removed at 2017/1/1
3437
    update_at # will be removed at 2017/1/1
3438
    insert_at # will be removed at 2017/1/1
3439
    register_tag # will be removed at 2017/1/1
3440
    default_bind_filter # will be removed at 2017/1/1
3441
    default_fetch_filter # will be removed at 2017/1/1
3442
    insert_param_tag # will be removed at 2017/1/1
update pod
Yuki Kimoto authored on 2011-08-10
3443
    register_tag # will be removed at 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3444
    register_tag_processor # will be removed at 2017/1/1
3445
    update_param_tag # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3446
    
3447
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
3448
    select method where_param option # will be removed 2017/1/1
cleanup
Yuki Kimoto authored on 2011-10-21
3449
    delete method where_param option # will be removed 2017/1/1
cleanup
Yuki Kimoto authored on 2011-10-21
3450
    update method where_param option # will be removed 2017/1/1
cleanup
Yuki Kimoto authored on 2011-10-21
3451
    insert method param option # will be removed at 2017/1/1
insert method's id option is...
Yuki Kimoto authored on 2011-10-10
3452
    insert method id option # will be removed at 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3453
    select method relation option # will be removed at 2017/1/1
3454
    select method column option [COLUMN, as => ALIAS] format
3455
      # will be removed at 2017/1/1
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
3456
    execute method's sqlfilter option # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3457
    
3458
    # Others
cleanup
Yuki Kimoto authored on 2011-07-28
3459
    execute("select * from {= title}"); # execute method's
3460
                                        # tag parsing functionality
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3461
                                        # will be removed at 2017/1/1
3462
    Query caching # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3463

            
3464
L<DBIx::Custom::Model>
3465

            
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3466
    # Attribute methods
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
3467
    method # will be removed at 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3468
    filter # will be removed at 2017/1/1
3469
    name # will be removed at 2017/1/1
3470
    type # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3471

            
3472
L<DBIx::Custom::Query>
3473
    
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3474
    # Attribute methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3475
    default_filter # will be removed at 2017/1/1
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3476
    table # will be removed at 2017/1/1
3477
    filters # will be removed at 2017/1/1
3478
    
3479
    # Methods
3480
    filter # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3481

            
3482
L<DBIx::Custom::QueryBuilder>
3483
    
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3484
    # Attribute methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3485
    tags # will be removed at 2017/1/1
3486
    tag_processors # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3487
    
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3488
    # Methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3489
    register_tag # will be removed at 2017/1/1
3490
    register_tag_processor # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3491
    
3492
    # Others
3493
    build_query("select * from {= title}"); # tag parsing functionality
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3494
                                            # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3495

            
3496
L<DBIx::Custom::Result>
3497
    
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3498
    # Attribute methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3499
    filter_check # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3500
    
3501
    # Methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3502
    end_filter # will be removed at 2017/1/1
3503
    remove_end_filter # will be removed at 2017/1/1
3504
    remove_filter # will be removed at 2017/1/1
3505
    default_filter # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3506

            
3507
L<DBIx::Custom::Tag>
3508

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

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

            
3513
If a functionality is DEPRECATED, you can know it by DEPRECATED warnings
3514
except for attribute method.
3515
You can check all DEPRECATED functionalities by document.
3516
DEPRECATED functionality is removed after five years,
3517
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
3518
I extend one year each time he tell me it.
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3519

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

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

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

            
3526
C<< <kimoto.yuki at gmail.com> >>
3527

            
3528
L<http://github.com/yuki-kimoto/DBIx-Custom>
3529

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
3530
=head1 AUTHOR
3531

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

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

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

            
3538
This program is free software; you can redistribute it and/or modify it
3539
under the same terms as Perl itself.
3540

            
3541
=cut