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

            
cleanup
Yuki Kimoto authored on 2011-04-02
3
our $VERSION = '0.1671';
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
4

            
5
use 5.008001;
cleanup
yuki-kimoto authored on 2009-12-22
6
use strict;
7
use warnings;
8

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
9
use base 'Object::Simple';
many change
yuki-kimoto authored on 2010-02-11
10

            
packaging one directory
yuki-kimoto authored on 2009-11-16
11
use Carp 'croak';
12
use DBI;
13
use DBIx::Custom::Result;
cleanup
yuki-kimoto authored on 2010-02-11
14
use DBIx::Custom::Query;
cleanup
yuki-kimoto authored on 2010-08-05
15
use DBIx::Custom::QueryBuilder;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
16
use DBIx::Custom::Where;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
17
use DBIx::Custom::Model;
cleanup
Yuki Kimoto authored on 2011-01-25
18
use DBIx::Custom::Tag;
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
19
use DBIx::Custom::Util;
update document
yuki-kimoto authored on 2010-05-27
20
use Encode qw/encode_utf8 decode_utf8/;
packaging one directory
yuki-kimoto authored on 2009-11-16
21

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

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

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

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

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

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

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
164
sub connect {
cleanup
Yuki Kimoto authored on 2011-01-25
165
    my $self = ref $_[0] ? shift : shift->new(@_);;
removed register_format()
yuki-kimoto authored on 2010-05-26
166
    
cleanup
Yuki Kimoto authored on 2011-04-02
167
    # Connect and get database handle
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
168
    my $dbh = $self->_connect;
packaging one directory
yuki-kimoto authored on 2009-11-16
169
    
cleanup
Yuki Kimoto authored on 2011-04-02
170
    # Set database handle
packaging one directory
yuki-kimoto authored on 2009-11-16
171
    $self->dbh($dbh);
update document
yuki-kimoto authored on 2010-01-30
172
    
cleanup
Yuki Kimoto authored on 2011-04-02
173
    # Set process ID
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
174
    $self->pid($$);
175
    
packaging one directory
yuki-kimoto authored on 2009-11-16
176
    return $self;
177
}
178

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

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

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
238
sub dbh {
239
    my $self = shift;
cleanup
Yuki Kimoto authored on 2011-04-02
240
    
241
    # Set
update pod
Yuki Kimoto authored on 2011-03-13
242
    if (@_) {
243
        $self->{dbh} = $_[0];
244
        return $self;
245
    }
cleanup
Yuki Kimoto authored on 2011-04-02
246
    
247
    # Get
update pod
Yuki Kimoto authored on 2011-03-13
248
    else {
249
        my $pid = $$;
cleanup
Yuki Kimoto authored on 2011-04-02
250
        
251
        # Get database handle
update pod
Yuki Kimoto authored on 2011-03-13
252
        if ($self->pid eq $pid) {
253
            return $self->{dbh};
254
        }
cleanup
Yuki Kimoto authored on 2011-04-02
255
        
256
        # Create new database handle in child process
update pod
Yuki Kimoto authored on 2011-03-13
257
        else {
258
            croak "Process is forked in transaction"
259
              unless $self->{dbh}->{AutoCommit};
260
            $self->pid($pid);
261
            $self->{dbh}->{InactiveDestroy} = 1;
262
            return $self->{dbh} = $self->_connect;
263
        }
264
    }
265
}
266

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

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

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

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

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

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

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

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

            
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
336
    
cleanup
Yuki Kimoto authored on 2011-04-02
337
    return $self->delete(where => $where_param, %args);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
338
}
339

            
added helper method
yuki-kimoto authored on 2010-10-17
340
sub DESTROY { }
341

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
added EXPERIMENTAL replace()...
Yuki Kimoto authored on 2011-04-01
792
sub replace {
793
    my ($self, $join, $search, $replace) = @_;
794
    
cleanup
Yuki Kimoto authored on 2011-04-02
795
    # Replace
added EXPERIMENTAL replace()...
Yuki Kimoto authored on 2011-04-01
796
    my @replace_join;
797
    my $is_replaced;
798
    foreach my $j (@$join) {
799
        if ($search eq $j) {
800
            push @replace_join, $replace;
801
            $is_replaced = 1;
802
        }
803
        else {
804
            push @replace_join, $j;
805
        }
806
    }
807
    croak qq{Can't replace "$search" with "$replace"} unless $is_replaced;
808
    
809
    return @replace_join;
810
}
811

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

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

            
refactoring select
yuki-kimoto authored on 2010-04-28
818
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
819
    my $table = delete $args{table};
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
820
    my $tables = ref $table eq 'ARRAY' ? $table
821
               : defined $table ? [$table]
822
               : [];
cleanup
Yuki Kimoto authored on 2011-03-21
823
    my $columns   = delete $args{column};
824
    my $where     = delete $args{where} || {};
825
    my $append    = delete $args{append};
826
    my $join      = delete $args{join} || [];
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
827
    croak qq{"join" must be array reference}
828
      unless ref $join eq 'ARRAY';
cleanup
Yuki Kimoto authored on 2011-03-21
829
    my $relation = delete $args{relation};
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
830
    my $param = delete $args{param} || {};
cleanup
Yuki Kimoto authored on 2011-04-02
831
    my $query_return = $args{query};
832

            
833
    # Check arguments
834
    foreach my $name (keys %args) {
835
        croak qq{Argument "$name" is wrong name}
836
          unless $SELECT_ARGS{$name};
837
    }
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
838
    
cleanup
Yuki Kimoto authored on 2011-03-09
839
    # Add relation tables(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-03-21
840
    $self->_add_relation_table($tables, $relation);
packaging one directory
yuki-kimoto authored on 2009-11-16
841
    
cleanup
Yuki Kimoto authored on 2011-04-02
842
    # Select statement
cleanup
Yuki Kimoto authored on 2011-01-27
843
    my @sql;
844
    push @sql, 'select';
packaging one directory
yuki-kimoto authored on 2009-11-16
845
    
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
846
    # Column clause
cleanup
Yuki Kimoto authored on 2011-03-30
847
    if ($columns) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
848
        $columns = [$columns] if ! ref $columns;
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
849
        foreach my $column (@$columns) {
cleanup
Yuki Kimoto authored on 2011-04-02
850
            unshift @$tables, @{$self->_search_tables($column)};
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
851
            push @sql, ($column, ',');
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
852
        }
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
853
        pop @sql if $sql[-1] eq ',';
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
854
    }
855
    else { push @sql, '*' }
856
    
857
    # Table
cleanup
Yuki Kimoto authored on 2011-03-30
858
    push @sql, 'from';
cleanup
Yuki Kimoto authored on 2011-04-02
859
    my $q = $self->reserved_word_quote;
cleanup
Yuki Kimoto authored on 2011-03-30
860
    if ($relation) {
861
        my $found = {};
862
        foreach my $table (@$tables) {
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
863
            push @sql, ("$q$table$q", ',') unless $found->{$table};
cleanup
Yuki Kimoto authored on 2011-03-30
864
            $found->{$table} = 1;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
865
        }
packaging one directory
yuki-kimoto authored on 2009-11-16
866
    }
cleanup
Yuki Kimoto authored on 2011-03-30
867
    else {
868
        my $main_table = $tables->[-1] || '';
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
869
        push @sql, "$q$main_table$q";
cleanup
Yuki Kimoto authored on 2011-03-30
870
    }
871
    pop @sql if ($sql[-1] || '') eq ',';
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
872
    croak "Not found table name" unless $tables->[-1];
cleanup
Yuki Kimoto authored on 2011-04-01
873

            
cleanup
Yuki Kimoto authored on 2011-04-02
874
    # Add tables in parameter
875
    unshift @$tables, @{$self->_search_tables(join(' ', keys %$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-04-02
878
    $where = $self->_where_to_obj($where);
879
    $param = keys %$param ? $self->merge_param($param, $where->param)
880
                          : $where->param;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
881
    
882
    # String where
cleanup
Yuki Kimoto authored on 2011-04-02
883
    my $where_clause = $where->to_string;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
884
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
885
    # Add table names in where clause
cleanup
Yuki Kimoto authored on 2011-04-02
886
    unshift @$tables, @{$self->_search_tables($where_clause)};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
887
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
888
    # Push join
889
    $self->_push_join(\@sql, $join, $tables);
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
890
    
cleanup
Yuki Kimoto authored on 2011-03-09
891
    # Add where clause
cleanup
Yuki Kimoto authored on 2011-04-02
892
    push @sql, $where_clause;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
893
    
cleanup
Yuki Kimoto authored on 2011-03-08
894
    # Relation(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-04-02
895
    $self->_push_relation(\@sql, $tables, $relation, $where_clause eq '' ? 1 : 0);
cleanup
Yuki Kimoto authored on 2011-03-08
896
    
cleanup
Yuki Kimoto authored on 2011-04-02
897
    # Append
cleanup
Yuki Kimoto authored on 2011-01-27
898
    push @sql, $append if $append;
899
    
900
    # SQL
901
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
902
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
903
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
904
    my $query = $self->create_query($sql);
cleanup
Yuki Kimoto authored on 2011-04-02
905
    return $query if $query_return;
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
906
    
packaging one directory
yuki-kimoto authored on 2009-11-16
907
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
908
    my $result = $self->execute(
cleanup
Yuki Kimoto authored on 2011-03-21
909
        $query,
cleanup
Yuki Kimoto authored on 2011-04-02
910
        param => $param, 
cleanup
Yuki Kimoto authored on 2011-03-21
911
        table => $tables,
912
        %args
913
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
914
    
915
    return $result;
916
}
917

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

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

            
923
    # Arguments
924
    my $primary_keys = delete $args{primary_key};
925
    $primary_keys = [$primary_keys] unless ref $primary_keys;
926
    my $where = delete $args{where};
927
    my $param = delete $args{param};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
928
    
cleanup
Yuki Kimoto authored on 2011-04-02
929
    # Check arguments
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
930
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-04-02
931
        croak qq{Argument "$name" is wrong name}
cleanup
Yuki Kimoto authored on 2011-03-21
932
          unless $SELECT_AT_ARGS{$name};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
933
    }
934
    
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
935
    # Table
936
    croak qq{"table" option must be specified} unless $args{table};
937
    my $table = ref $args{table} ? $args{table}->[-1] : $args{table};
938
    
cleanup
Yuki Kimoto authored on 2011-04-02
939
    # Create where parameter
940
    my $where_param = $self->_create_where_param($where, $primary_keys);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
941
    
cleanup
Yuki Kimoto authored on 2011-04-02
942
    return $self->select(where => $where_param, %args);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
943
}
944

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
945
sub setup_model {
946
    my $self = shift;
947
    
cleanup
Yuki Kimoto authored on 2011-04-02
948
    # Setup model
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
949
    $self->each_column(
950
        sub {
951
            my ($self, $table, $column, $column_info) = @_;
952
            if (my $model = $self->models->{$table}) {
953
                push @{$model->columns}, $column;
954
            }
955
        }
956
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
957
    return $self;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
958
}
959

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
966
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
967
    my $table = delete $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
968
    croak qq{"table" option must be specified} unless $table;
cleanup
Yuki Kimoto authored on 2011-03-21
969
    my $param            = delete $args{param} || {};
970
    my $where            = delete $args{where} || {};
971
    my $append           = delete $args{append} || '';
972
    my $allow_update_all = delete $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
973
    
cleanup
Yuki Kimoto authored on 2011-04-02
974
    # Check argument names
975
    foreach my $name (keys %args) {
976
        croak qq{Argument "$name" is wrong name}
977
          unless $UPDATE_ARGS{$name};
978
    }
979
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
980
    # Columns
981
    my @columns;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
982
    my $safety = $self->safety_character;
cleanup
Yuki Kimoto authored on 2011-04-02
983
    my $q = $self->reserved_word_quote;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
984
    foreach my $column (keys %$param) {
985
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
986
          unless $column =~ /^[$safety\.]+$/;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
987
          $column = "$q$column$q";
988
          $column =~ s/\./$q.$q/;
989
        push @columns, "$column";
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
990
    }
991
        
cleanup
yuki-kimoto authored on 2010-10-17
992
    # Update clause
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
993
    my $update_clause = '{update_param ' . join(' ', @columns) . '}';
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
994

            
995
    # Where
cleanup
Yuki Kimoto authored on 2011-04-02
996
    $where = $self->_where_to_obj($where);
997
    my $where_clause = $where->to_string;
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
998
    croak qq{"where" must be specified}
cleanup
Yuki Kimoto authored on 2011-04-02
999
      if "$where_clause" eq '' && !$allow_update_all;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1000
    
cleanup
Yuki Kimoto authored on 2011-04-02
1001
    # Update statement
cleanup
Yuki Kimoto authored on 2011-01-27
1002
    my @sql;
cleanup
Yuki Kimoto authored on 2011-04-02
1003
    push @sql, "update $q$table$q $update_clause $where_clause";
cleanup
Yuki Kimoto authored on 2011-01-27
1004
    push @sql, $append if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1005
    
cleanup
Yuki Kimoto authored on 2011-04-02
1006
    # Merge parameters
1007
    $param = $self->merge_param($param, $where->param);
cleanup
yuki-kimoto authored on 2010-10-17
1008
    
cleanup
Yuki Kimoto authored on 2011-01-27
1009
    # SQL
1010
    my $sql = join(' ', @sql);
1011
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1012
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
1013
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1014
    return $query if $args{query};
1015
    
cleanup
yuki-kimoto authored on 2010-10-17
1016
    # Execute query
cleanup
Yuki Kimoto authored on 2011-03-21
1017
    my $ret_val = $self->execute(
1018
        $query,
1019
        param  => $param, 
1020
        table => $table,
1021
        %args
1022
    );
cleanup
yuki-kimoto authored on 2010-10-17
1023
    
1024
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1025
}
1026

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

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

            
1031
sub update_at {
1032
    my ($self, %args) = @_;
1033
    
cleanup
Yuki Kimoto authored on 2011-04-02
1034
    # Arguments
1035
    my $primary_keys = delete $args{primary_key};
1036
    $primary_keys = [$primary_keys] unless ref $primary_keys;
1037
    my $where = delete $args{where};
1038
    
1039

            
1040
    # Check arguments
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1041
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-04-02
1042
        croak qq{Argument "$name" is wrong name}
cleanup
Yuki Kimoto authored on 2011-03-21
1043
          unless $UPDATE_AT_ARGS{$name};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1044
    }
1045
    
cleanup
Yuki Kimoto authored on 2011-04-02
1046
    # Create where parameter
1047
    my $where_param = $self->_create_where_param($where, $primary_keys);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1048
    
cleanup
Yuki Kimoto authored on 2011-04-02
1049
    return $self->update(where => $where_param, %args);
1050
}
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1051

            
cleanup
Yuki Kimoto authored on 2011-04-02
1052
sub _create_where_param {
1053
    my ($self, $where, $primary_keys) = @_;
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1054
    
cleanup
Yuki Kimoto authored on 2011-04-02
1055
    # Create where parameter
1056
    my $where_param = {};
1057
    if ($where) {
1058
        $where = [$where] unless ref $where;
1059
        croak qq{"where" must be constant value or array reference}
1060
          unless !ref $where || ref $where eq 'ARRAY';
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1061
        for(my $i = 0; $i < @$primary_keys; $i ++) {
cleanup
Yuki Kimoto authored on 2011-04-02
1062
           $where_param->{$primary_keys->[$i]} = $where->[$i];
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1063
        }
1064
    }
1065
    
cleanup
Yuki Kimoto authored on 2011-04-02
1066
    return $where_param;
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1067
}
1068

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1069
sub update_param_tag {
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
1070
    my ($self, $param, $opt) = @_;
1071
    
cleanup
Yuki Kimoto authored on 2011-04-02
1072
    # Create update parameter tag
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
1073
    my @params;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1074
    my $safety = $self->safety_character;
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
1075
    my $q = $self->reserved_word_quote;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1076
    foreach my $column (keys %$param) {
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1077
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1078
          unless $column =~ /^[$safety\.]+$/;
cleanup
Yuki Kimoto authored on 2011-04-02
1079
        my $column = "$q$column$q";
1080
        $column =~ s/\./$q.$q/;
1081
        push @params, "$column = {? $column}";
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1082
    }
cleanup
Yuki Kimoto authored on 2011-04-02
1083
    my $tag;
1084
    $tag .= 'set ' unless $opt->{no_set};
1085
    $tag .= join(', ', @params);
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1086
    
cleanup
Yuki Kimoto authored on 2011-04-02
1087
    return $tag;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1088
}
1089

            
cleanup
Yuki Kimoto authored on 2011-01-25
1090
sub where {
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1091
    my $self = shift;
cleanup
Yuki Kimoto authored on 2011-04-02
1092
    
1093
    # Create where
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1094
    return DBIx::Custom::Where->new(
1095
        query_builder => $self->query_builder,
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1096
        safety_character => $self->safety_character,
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1097
        reserved_word_quote => $self->reserved_word_quote,
cleanup
Yuki Kimoto authored on 2011-03-09
1098
        @_
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1099
    );
cleanup
Yuki Kimoto authored on 2011-01-25
1100
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1101

            
cleanup
Yuki Kimoto authored on 2011-04-02
1102
sub _create_bind_values {
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1103
    my ($self, $params, $columns, $filter, $type) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1104
    
cleanup
Yuki Kimoto authored on 2011-04-02
1105
    # Create bind values
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1106
    my $bind = [];
removed reconnect method
yuki-kimoto authored on 2010-05-28
1107
    my $count = {};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1108
    my $not_exists = {};
cleanup
Yuki Kimoto authored on 2011-01-12
1109
    foreach my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1110
        
1111
        # Value
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1112
        my $value;
1113
        if(ref $params->{$column} eq 'ARRAY') {
1114
            my $i = $count->{$column} || 0;
1115
            $i += $not_exists->{$column} || 0;
1116
            my $found;
1117
            for (my $k = $i; $i < @{$params->{$column}}; $k++) {
1118
                if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
1119
                    $not_exists->{$column}++;
1120
                }
1121
                else  {
1122
                    $value = $params->{$column}->[$k];
1123
                    $found = 1;
1124
                    last
1125
                }
1126
            }
1127
            next unless $found;
1128
        }
1129
        else { $value = $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1130
        
cleanup
Yuki Kimoto authored on 2011-01-12
1131
        # Filter
1132
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
1133
        
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1134
        # Type
1135
        push @$bind, {
1136
            value => $f ? $f->($value) : $value,
1137
            type => $type->{$column}
1138
        };
removed reconnect method
yuki-kimoto authored on 2010-05-28
1139
        
1140
        # Count up 
1141
        $count->{$column}++;
1142
    }
1143
    
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1144
    return $bind;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1145
}
1146

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1147
sub _connect {
1148
    my $self = shift;
1149
    
1150
    # Attributes
1151
    my $data_source = $self->data_source;
1152
    croak qq{"data_source" must be specified to connect()"}
1153
      unless $data_source;
1154
    my $user        = $self->user;
1155
    my $password    = $self->password;
1156
    my $dbi_option = {%{$self->dbi_options}, %{$self->dbi_option}};
1157
    
1158
    # Connect
1159
    my $dbh = eval {DBI->connect(
1160
        $data_source,
1161
        $user,
1162
        $password,
1163
        {
1164
            %{$self->default_dbi_option},
1165
            %$dbi_option
1166
        }
1167
    )};
1168
    
1169
    # Connect error
1170
    croak $@ if $@;
1171
    
1172
    return $dbh;
1173
}
1174

            
cleanup
yuki-kimoto authored on 2010-10-17
1175
sub _croak {
1176
    my ($self, $error, $append) = @_;
cleanup
Yuki Kimoto authored on 2011-04-02
1177
    
1178
    # Append
cleanup
yuki-kimoto authored on 2010-10-17
1179
    $append ||= "";
1180
    
1181
    # Verbose
1182
    if ($Carp::Verbose) { croak $error }
1183
    
1184
    # Not verbose
1185
    else {
1186
        
1187
        # Remove line and module infromation
1188
        my $at_pos = rindex($error, ' at ');
1189
        $error = substr($error, 0, $at_pos);
1190
        $error =~ s/\s+$//;
1191
        croak "$error$append";
1192
    }
1193
}
1194

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1195
sub _need_tables {
1196
    my ($self, $tree, $need_tables, $tables) = @_;
1197
    
cleanup
Yuki Kimoto authored on 2011-04-02
1198
    # Get needed tables
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1199
    foreach my $table (@$tables) {
1200
        if ($tree->{$table}) {
1201
            $need_tables->{$table} = 1;
1202
            $self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}])
1203
        }
1204
    }
1205
}
1206

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1207
sub _push_join {
1208
    my ($self, $sql, $join, $join_tables) = @_;
1209
    
cleanup
Yuki Kimoto authored on 2011-04-02
1210
    # No join
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1211
    return unless @$join;
1212
    
cleanup
Yuki Kimoto authored on 2011-04-02
1213
    # Push join clause
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1214
    my $tree = {};
cleanup
Yuki Kimoto authored on 2011-04-02
1215
    my $q = $self->reserved_word_quote;
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1216
    for (my $i = 0; $i < @$join; $i++) {
1217
        
cleanup
Yuki Kimoto authored on 2011-04-02
1218
        # Search table in join clause
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1219
        my $join_clause = $join->[$i];
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1220
        my $q_re = quotemeta($q);
cleanup
Yuki Kimoto authored on 2011-04-01
1221
        my $join_re = $q ? qr/\s$q_re?([^\.\s$q_re]+?)$q_re?\..+?\s$q_re?([^\.\s$q_re]+?)$q_re?\..+?$/
1222
                         : qr/\s([^\.\s]+?)\..+?\s([^\.\s]+?)\..+?$/;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1223
        if ($join_clause =~ $join_re) {
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1224
            my $table1 = $1;
1225
            my $table2 = $2;
1226
            croak qq{right side table of "$join_clause" must be uniq}
1227
              if exists $tree->{$table2};
1228
            $tree->{$table2}
1229
              = {position => $i, parent => $table1, join => $join_clause};
1230
        }
1231
        else {
1232
            croak qq{join "$join_clause" must be two table name};
1233
        }
1234
    }
1235
    
cleanup
Yuki Kimoto authored on 2011-04-02
1236
    # Search need tables
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1237
    my $need_tables = {};
1238
    $self->_need_tables($tree, $need_tables, $join_tables);
1239
    my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} } keys %$need_tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1240
    
1241
    # Add join clause
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1242
    foreach my $need_table (@need_tables) {
1243
        push @$sql, $tree->{$need_table}{join};
1244
    }
1245
}
cleanup
Yuki Kimoto authored on 2011-03-08
1246

            
cleanup
Yuki Kimoto authored on 2011-04-02
1247
sub _remove_duplicate_table {
1248
    my ($self, $tables, $main_table) = @_;
1249
    
1250
    # Remove duplicate table
1251
    my %tables = map {defined $_ ? ($_ => 1) : ()} @$tables;
1252
    delete $tables{$main_table} if $main_table;
1253
    
1254
    return [keys %tables, $main_table ? $main_table : ()];
1255
}
1256

            
cleanup
Yuki Kimoto authored on 2011-04-02
1257
sub _search_tables {
cleanup
Yuki Kimoto authored on 2011-04-02
1258
    my ($self, $source) = @_;
1259
    
cleanup
Yuki Kimoto authored on 2011-04-02
1260
    # Search tables
cleanup
Yuki Kimoto authored on 2011-04-02
1261
    my $tables = [];
1262
    my $safety_character = $self->safety_character;
1263
    my $q = $self->reserved_word_quote;
1264
    my $q_re = quotemeta($q);
1265
    my $table_re = $q ? qr/\b$q_re?([$safety_character]+)$q_re?\./
1266
                      : qr/\b([$safety_character]+)\./;
1267
    while ($source =~ /$table_re/g) {
1268
        push @$tables, $1;
1269
    }
1270
    
1271
    return $tables;
1272
}
1273

            
cleanup
Yuki Kimoto authored on 2011-04-02
1274
sub _where_to_obj {
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1275
    my ($self, $where) = @_;
1276
    
cleanup
Yuki Kimoto authored on 2011-04-02
1277
    my $obj;
1278
    
1279
    # Hash
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1280
    if (ref $where eq 'HASH') {
1281
        my $clause = ['and'];
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1282
        my $q = $self->reserved_word_quote;
1283
        foreach my $column (keys %$where) {
1284
            $column = "$q$column$q";
1285
            $column =~ s/\./$q.$q/;
1286
            push @$clause, "{= $column}" for keys %$where;
1287
        }
cleanup
Yuki Kimoto authored on 2011-04-02
1288
        $obj = $self->where(clause => $clause, param => $where);
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1289
    }
cleanup
Yuki Kimoto authored on 2011-04-02
1290
    
1291
    # DBIx::Custom::Where object
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1292
    elsif (ref $where eq 'DBIx::Custom::Where') {
cleanup
Yuki Kimoto authored on 2011-04-02
1293
        $obj = $where;
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1294
    }
cleanup
Yuki Kimoto authored on 2011-04-02
1295
    
1296
    # Array(DEPRECATED!)
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1297
    elsif (ref $where eq 'ARRAY') {
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
1298
        warn "\$dbi->select(where => [CLAUSE, PARAMETER]) is DEPRECATED." .
1299
             "use \$dbi->select(where => \$dbi->where(clause => " .
1300
             "CLAUSE, param => PARAMETER));";
cleanup
Yuki Kimoto authored on 2011-04-02
1301
        $obj = $self->where(
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1302
            clause => $where->[0],
1303
            param  => $where->[1]
1304
        );
1305
    }
1306
    
cleanup
Yuki Kimoto authored on 2011-04-02
1307
    # Check where argument
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1308
    croak qq{"where" must be hash reference or DBIx::Custom::Where object} .
1309
          qq{or array reference, which contains where clause and paramter}
cleanup
Yuki Kimoto authored on 2011-04-02
1310
      unless ref $obj eq 'DBIx::Custom::Where';
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1311
    
cleanup
Yuki Kimoto authored on 2011-04-02
1312
    return $obj;
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1313
}
1314

            
cleanup
Yuki Kimoto authored on 2011-01-25
1315
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-23
1316
__PACKAGE__->attr(
1317
    dbi_options => sub { {} },
1318
    filter_check  => 1
1319
);
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1320

            
cleanup
Yuki Kimoto authored on 2011-01-25
1321
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1322
sub default_bind_filter {
1323
    my $self = shift;
1324
    
1325
    if (@_) {
1326
        my $fname = $_[0];
1327
        
1328
        if (@_ && !$fname) {
1329
            $self->{default_out_filter} = undef;
1330
        }
1331
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1332
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1333
              unless exists $self->filters->{$fname};
1334
        
1335
            $self->{default_out_filter} = $self->filters->{$fname};
1336
        }
1337
        return $self;
1338
    }
1339
    
1340
    return $self->{default_out_filter};
1341
}
1342

            
cleanup
Yuki Kimoto authored on 2011-01-25
1343
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1344
sub default_fetch_filter {
1345
    my $self = shift;
1346
    
1347
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
1348
        my $fname = $_[0];
1349

            
cleanup
Yuki Kimoto authored on 2011-01-12
1350
        if (@_ && !$fname) {
1351
            $self->{default_in_filter} = undef;
1352
        }
1353
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1354
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1355
              unless exists $self->filters->{$fname};
1356
        
1357
            $self->{default_in_filter} = $self->filters->{$fname};
1358
        }
1359
        
1360
        return $self;
1361
    }
1362
    
many changed
Yuki Kimoto authored on 2011-01-23
1363
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
1364
}
1365

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1366
# DEPRECATED!
1367
sub insert_param {
1368
    warn "insert_param is renamed to insert_param_tag."
1369
       . " insert_param is DEPRECATED!";
1370
    return shift->insert_param_tag(@_);
1371
}
1372

            
cleanup
Yuki Kimoto authored on 2011-01-25
1373
# DEPRECATED!
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1374
sub register_tag_processor {
1375
    return shift->query_builder->register_tag_processor(@_);
1376
}
1377

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1378
# DEPRECATED!
1379
sub update_param {
1380
    warn "update_param is renamed to update_param_tag."
1381
       . " update_param is DEPRECATED!";
1382
    return shift->update_param_tag(@_);
1383
}
cleanup
Yuki Kimoto authored on 2011-03-08
1384
# DEPRECATED!
1385
sub _push_relation {
1386
    my ($self, $sql, $tables, $relation, $need_where) = @_;
1387
    
1388
    if (keys %{$relation || {}}) {
1389
        push @$sql, $need_where ? 'where' : 'and';
1390
        foreach my $rcolumn (keys %$relation) {
1391
            my $table1 = (split (/\./, $rcolumn))[0];
1392
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1393
            push @$tables, ($table1, $table2);
1394
            push @$sql, ("$rcolumn = " . $relation->{$rcolumn},  'and');
1395
        }
1396
    }
1397
    pop @$sql if $sql->[-1] eq 'and';    
1398
}
1399

            
1400
# DEPRECATED!
1401
sub _add_relation_table {
cleanup
Yuki Kimoto authored on 2011-03-09
1402
    my ($self, $tables, $relation) = @_;
cleanup
Yuki Kimoto authored on 2011-03-08
1403
    
1404
    if (keys %{$relation || {}}) {
1405
        foreach my $rcolumn (keys %$relation) {
1406
            my $table1 = (split (/\./, $rcolumn))[0];
1407
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1408
            my $table1_exists;
1409
            my $table2_exists;
1410
            foreach my $table (@$tables) {
1411
                $table1_exists = 1 if $table eq $table1;
1412
                $table2_exists = 1 if $table eq $table2;
1413
            }
1414
            unshift @$tables, $table1 unless $table1_exists;
1415
            unshift @$tables, $table2 unless $table2_exists;
1416
        }
1417
    }
1418
}
1419

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1422
=head1 NAME
1423

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

            
1426
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
1427

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1428
    use DBIx::Custom;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1429
    
1430
    # Connect
1431
    my $dbi = DBIx::Custom->connect(
1432
        data_source => "dbi:mysql:database=dbname",
1433
        user => 'ken',
1434
        password => '!LFKD%$&',
1435
        dbi_option => {mysql_enable_utf8 => 1}
1436
    );
cleanup
yuki-kimoto authored on 2010-08-05
1437

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1438
    # Insert 
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1439
    $dbi->insert(
1440
        table  => 'book',
1441
        param  => {title => 'Perl', author => 'Ken'}
1442
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1443
    
1444
    # Update 
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1445
    $dbi->update(
1446
        table  => 'book', 
1447
        param  => {title => 'Perl', author => 'Ken'}, 
1448
        where  => {id => 5},
1449
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1450
    
1451
    # Delete
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1452
    $dbi->delete(
1453
        table  => 'book',
1454
        where  => {author => 'Ken'},
1455
    );
cleanup
yuki-kimoto authored on 2010-08-05
1456

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1463
    # Select, more complex
1464
    my $result = $dbi->select(
1465
        table  => 'book',
1466
        column => [
1467
            'book.author as book__author',
1468
            'company.name as company__name'
1469
        ],
1470
        where  => {'book.author' => 'Ken'},
1471
        join => ['left outer join company on book.company_id = company.id'],
1472
        append => 'order by id limit 5'
removed reconnect method
yuki-kimoto authored on 2010-05-28
1473
    );
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1474
    
removed register_format()
yuki-kimoto authored on 2010-05-26
1475
    # Fetch
1476
    while (my $row = $result->fetch) {
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1477
        
removed register_format()
yuki-kimoto authored on 2010-05-26
1478
    }
1479
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1480
    # Fetch as hash
removed register_format()
yuki-kimoto authored on 2010-05-26
1481
    while (my $row = $result->fetch_hash) {
1482
        
1483
    }
1484
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1485
    # Execute SQL with parameter.
1486
    $dbi->execute(
1487
        "select id from book where {= author} and {like title}",
1488
        param  => {author => 'ken', title => '%Perl%'}
1489
    );
1490
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1491
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
1492

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

            
1495
=head1 FEATURES
removed reconnect method
yuki-kimoto authored on 2010-05-28
1496

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1501
There are many basic methods to execute various queries.
1502
C<insert()>, C<update()>, C<update_all()>,C<delete()>,
1503
C<delete_all()>, C<select()>,
1504
C<insert_at()>, C<update_at()>, 
1505
C<delete_at()>, C<select_at()>, C<execute()>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1506

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1507
=item *
1508

            
1509
Filter when data is send or receive.
1510

            
1511
=item *
1512

            
1513
Data filtering system
1514

            
1515
=item *
1516

            
1517
Model support.
1518

            
1519
=item *
1520

            
1521
Generate where clause dinamically.
1522

            
1523
=item *
1524

            
1525
Generate join clause dinamically.
1526

            
1527
=back
pod fix
Yuki Kimoto authored on 2011-01-21
1528

            
1529
=head1 GUIDE
1530

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

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

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

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

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

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

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

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

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

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

            
1554
=head2 C<default_dbi_option>
1555

            
1556
    my $default_dbi_option = $dbi->default_dbi_option;
1557
    $dbi            = $dbi->default_dbi_option($default_dbi_option);
1558

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

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

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

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

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1578
=head2 C<models> EXPERIMENTAL
add models() attribute
Yuki Kimoto authored on 2011-02-21
1579

            
1580
    my $models = $dbi->models;
1581
    $dbi       = $dbi->models(\%models);
1582

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1585
=head2 C<password>
1586

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

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

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

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

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

            
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1599
=head2 C<reserved_word_quote> EXPERIMENTAL
1600

            
1601
     my reserved_word_quote = $dbi->reserved_word_quote;
1602
     $dbi                   = $dbi->reserved_word_quote('"');
1603

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

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

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

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

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

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

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

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

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

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

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1634
=head2 C<apply_filter> EXPERIMENTAL
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1635

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

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

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

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

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

            
1665
You can set multiple filters at once.
1666

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

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

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

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

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

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

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

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

            
1711
   $dbi->model('book')->select(...);
1712

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

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

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

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

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

            
1729
    my $dbh = $dbi->dbh;
1730
    $dbi    = $dbi->dbh($dbh);
1731

            
1732
Get and set database handle of L<DBI>.
1733

            
update pod
Yuki Kimoto authored on 2011-03-13
1734
If process is spawn by forking, new connection is created automatically.
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1735

            
1736
=head2 C<each_column>
1737

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

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

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

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

            
1762
Execute SQL, containing tags.
1763
Return value is L<DBIx::Custom::Result> in select statement, or
1764
the count of affected rows in insert, update, delete statement.
1765

            
1766
Tag is turned into the statement containing place holder
1767
before SQL is executed.
1768

            
1769
    select * from where title = ? and author like ?;
1770

            
1771
See also L<Tags/Tags>.
1772

            
1773
The following opitons are currently available.
1774

            
1775
=over 4
1776

            
1777
=item C<filter>
1778

            
1779
Filter, executed before data is send to database. This is array reference.
1780
Filter value is code reference or
1781
filter name registerd by C<register_filter()>.
1782

            
1783
    # Basic
1784
    $dbi->execute(
1785
        $sql,
1786
        filter => [
1787
            title  => sub { uc $_[0] }
1788
            author => sub { uc $_[0] }
1789
        ]
1790
    );
1791
    
1792
    # At once
1793
    $dbi->execute(
1794
        $sql,
1795
        filter => [
1796
            [qw/title author/]  => sub { uc $_[0] }
1797
        ]
1798
    );
1799
    
1800
    # Filter name
1801
    $dbi->execute(
1802
        $sql,
1803
        filter => [
1804
            title  => 'upper_case',
1805
            author => 'upper_case'
1806
        ]
1807
    );
1808

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

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

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

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

            
1817
Delete statement.
1818

            
1819
The following opitons are currently available.
1820

            
update pod
Yuki Kimoto authored on 2011-03-13
1821
=over 4
1822

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

            
1825
Table name.
1826

            
1827
    $dbi->delete(table => 'book');
1828

            
1829
=item C<where>
1830

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1831
Where clause. This is hash reference or L<DBIx::Custom::Where> object
1832
or array refrence, which contains where clause and paramter.
update pod
Yuki Kimoto authored on 2011-03-13
1833
    
1834
    # Hash reference
1835
    $dbi->delete(where => {title => 'Perl'});
1836
    
1837
    # DBIx::Custom::Where object
1838
    my $where = $dbi->where(
1839
        clause => ['and', '{= author}', '{like title}'],
1840
        param  => {author => 'Ken', title => '%Perl%'}
1841
    );
1842
    $dbi->delete(where => $where);
1843

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1844
    # Array refrendce (where clause and parameter)
1845
    $dbi->delete(where =>
1846
        [
1847
            ['and', '{= author}', '{like title}'],
1848
            {author => 'Ken', title => '%Perl%'}
1849
        ]
1850
    );
1851
    
update pod
Yuki Kimoto authored on 2011-03-13
1852
=item C<append>
1853

            
1854
Append statement to last of SQL. This is string.
1855

            
1856
    $dbi->delete(append => 'order by title');
1857

            
1858
=item C<filter>
1859

            
1860
Filter, executed before data is send to database. This is array reference.
1861
Filter value is code reference or
1862
filter name registerd by C<register_filter()>.
1863

            
1864
    # Basic
1865
    $dbi->delete(
1866
        filter => [
1867
            title  => sub { uc $_[0] }
1868
            author => sub { uc $_[0] }
1869
        ]
1870
    );
1871
    
1872
    # At once
1873
    $dbi->delete(
1874
        filter => [
1875
            [qw/title author/]  => sub { uc $_[0] }
1876
        ]
1877
    );
1878
    
1879
    # Filter name
1880
    $dbi->delete(
1881
        filter => [
1882
            title  => 'upper_case',
1883
            author => 'upper_case'
1884
        ]
1885
    );
1886

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
1889
=head2 C<column> EXPERIMENTAL
1890

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

            
1893
Create column clause. The follwoing column clause is created.
1894

            
1895
    book.author as book__author,
1896
    book.title as book__title
1897

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

            
1900
Get L<DBIx::Custom::Query> object instead of executing SQL.
1901
This is true or false value.
1902

            
1903
    my $query = $dbi->delete(query => 1);
1904

            
1905
You can check SQL.
1906

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1909
=back
1910

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

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

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

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

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

            
1922
    $dbi->delete_at(
1923
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
1924
        primary_key => 'id',
1925
        where => '5'
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1926
    );
1927

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1932
=over 4
1933

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1936
Primary key. This is constant value or array reference.
1937
    
1938
    # Constant value
1939
    $dbi->delete(primary_key => 'id');
1940

            
1941
    # Array reference
1942
    $dbi->delete(primary_key => ['id1', 'id2' ]);
1943

            
1944
This is used to create where clause.
1945

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

            
1948
Where clause, created from primary key information.
1949
This is constant value or array reference.
1950

            
1951
    # Constant value
1952
    $dbi->delete(where => 5);
1953

            
1954
    # Array reference
1955
    $dbi->delete(where => [3, 5]);
1956

            
1957
In first examle, the following SQL is created.
1958

            
1959
    delete from book where id = ?;
1960

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1963
=back
1964

            
cleanup
yuki-kimoto authored on 2010-10-17
1965
=head2 C<insert>
1966

            
update pod
Yuki Kimoto authored on 2011-03-13
1967
    $dbi->insert(
1968
        table  => 'book', 
1969
        param  => {title => 'Perl', author => 'Ken'}
1970
    );
1971

            
1972
Insert statement.
1973

            
1974
The following opitons are currently available.
1975

            
update pod
Yuki Kimoto authored on 2011-03-13
1976
=over 4
1977

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

            
1980
Table name.
1981

            
1982
    $dbi->insert(table => 'book');
1983

            
1984
=item C<param>
1985

            
1986
Insert data. This is hash reference.
1987

            
1988
    $dbi->insert(param => {title => 'Perl'});
1989

            
1990
=item C<append>
1991

            
1992
Append statement to last of SQL. This is string.
1993

            
1994
    $dbi->insert(append => 'order by title');
1995

            
1996
=item C<filter>
1997

            
1998
Filter, executed before data is send to database. This is array reference.
1999
Filter value is code reference or
2000
filter name registerd by C<register_filter()>.
2001

            
2002
    # Basic
2003
    $dbi->insert(
2004
        filter => [
2005
            title  => sub { uc $_[0] }
2006
            author => sub { uc $_[0] }
2007
        ]
2008
    );
2009
    
2010
    # At once
2011
    $dbi->insert(
2012
        filter => [
2013
            [qw/title author/]  => sub { uc $_[0] }
2014
        ]
2015
    );
2016
    
2017
    # Filter name
2018
    $dbi->insert(
2019
        filter => [
2020
            title  => 'upper_case',
2021
            author => 'upper_case'
2022
        ]
2023
    );
2024

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

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

            
2029
Get L<DBIx::Custom::Query> object instead of executing SQL.
2030
This is true or false value.
2031

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

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2038
=back
2039

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

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

            
2044
    $dbi->insert_at(
2045
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
2046
        primary_key => 'id',
2047
        where => '5',
2048
        param => {title => 'Perl'}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
2049
    );
2050

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2055
=over 4
2056

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

            
2059
Primary key. This is constant value or array reference.
2060
    
2061
    # Constant value
2062
    $dbi->insert(primary_key => 'id');
2063

            
2064
    # Array reference
2065
    $dbi->insert(primary_key => ['id1', 'id2' ]);
2066

            
2067
This is used to create parts of insert data.
2068

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

            
2071
Parts of Insert data, create from primary key information.
2072
This is constant value or array reference.
2073

            
2074
    # Constant value
2075
    $dbi->insert(where => 5);
2076

            
2077
    # Array reference
2078
    $dbi->insert(where => [3, 5]);
2079

            
2080
In first examle, the following SQL is created.
2081

            
2082
    insert into book (id, title) values (?, ?);
2083

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2086
=back
2087

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

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

            
2092
Create insert parameter tag.
2093

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2096
=head2 C<include_model> EXPERIMENTAL
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2097

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2103
    lib / MyModel.pm
2104
        / MyModel / book.pm
2105
                  / company.pm
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2106

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

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

            
2111
    package MyModel;
2112
    
2113
    use base 'DBIx::Custom::Model';
update pod
Yuki Kimoto authored on 2011-03-13
2114
    
2115
    1;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2116

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2121
    package MyModel::book;
2122
    
2123
    use base 'MyModel';
2124
    
2125
    1;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2126

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2129
    package MyModel::company;
2130
    
2131
    use base 'MyModel';
2132
    
2133
    1;
2134
    
2135
MyModel::book and MyModel::company is included by C<include_model()>.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2136

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

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

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

            
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
2144
=head2 C<merge_param> EXPERIMENTAL
2145

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

            
2148
Merge paramters.
2149

            
2150
$param:
2151

            
2152
    {key1 => [1, 1], key2 => 2}
2153

            
update pod
Yuki Kimoto authored on 2011-03-13
2154
=head2 C<method> EXPERIMENTAL
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2155

            
2156
    $dbi->method(
2157
        update_or_insert => sub {
2158
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-03-13
2159
            
2160
            # Process
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2161
        },
2162
        find_or_create   => sub {
2163
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-03-13
2164
            
2165
            # Process
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2166
        }
2167
    );
2168

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

            
2171
    $dbi->update_or_insert;
2172
    $dbi->find_or_create;
2173

            
update pod
Yuki Kimoto authored on 2011-03-13
2174
=head2 C<model> EXPERIMENTAL
2175

            
2176
    $dbi->model('book')->method(
2177
        insert => sub { ... },
2178
        update => sub { ... }
2179
    );
2180
    
2181
    my $model = $dbi->model('book');
2182

            
2183
Set and get a L<DBIx::Custom::Model> object,
2184

            
cleanup
Yuki Kimoto authored on 2011-03-21
2185
=head2 C<mycolumn> EXPERIMENTAL
2186

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

            
2189
Create column clause for myself. The follwoing column clause is created.
2190

            
2191
    book.author as author,
2192
    book.title as title
2193

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2196
    my $dbi = DBIx::Custom->new(
2197
        data_source => "dbi:mysql:database=dbname",
2198
        user => 'ken',
2199
        password => '!LFKD%$&',
2200
        dbi_option => {mysql_enable_utf8 => 1}
2201
    );
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2202

            
2203
Create a new L<DBIx::Custom> object.
2204

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

            
2207
    my $not_exists = $dbi->not_exists;
2208

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

            
cleanup
yuki-kimoto authored on 2010-10-17
2212
=head2 C<register_filter>
2213

            
update pod
Yuki Kimoto authored on 2011-03-13
2214
    $dbi->register_filter(
2215
        # Time::Piece object to database DATE format
2216
        tp_to_date => sub {
2217
            my $tp = shift;
2218
            return $tp->strftime('%Y-%m-%d');
2219
        },
2220
        # database DATE format to Time::Piece object
2221
        date_to_tp => sub {
2222
           my $date = shift;
2223
           return Time::Piece->strptime($date, '%Y-%m-%d');
2224
        }
2225
    );
cleanup
yuki-kimoto authored on 2010-10-17
2226
    
update pod
Yuki Kimoto authored on 2011-03-13
2227
Register filters, used by C<filter> option of many methods.
cleanup
yuki-kimoto authored on 2010-10-17
2228

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2231
    $dbi->register_tag(
2232
        update => sub {
2233
            my @columns = @_;
2234
            
2235
            # Update parameters
2236
            my $s = 'set ';
2237
            $s .= "$_ = ?, " for @columns;
2238
            $s =~ s/, $//;
2239
            
2240
            return [$s, \@columns];
2241
        }
2242
    );
cleanup
yuki-kimoto authored on 2010-10-17
2243

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

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

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

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

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

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

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

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

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

            
added EXPERIMENTAL replace()...
Yuki Kimoto authored on 2011-04-01
2265
=head2 C<replace> EXPERIMENTAL
2266
    
2267
    my $join = [
2268
        'left outer join table2 on table1.key1 = table2.key1',
2269
        'left outer join table3 on table2.key3 = table3.key3'
2270
    ];
2271
    $join = $dbi->replace(
2272
        $join,
2273
        'left outer join table2 on table1.key1 = table2.key1',
2274
        'left outer join (select * from table2 where {= table2.key1}) ' . 
2275
          'as table2 on table1.key1 = table2.key1'
2276
    );
2277

            
2278
Replace join clauses if match the expression.
2279

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

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

            
2290
The following opitons are currently available.
2291

            
2292
=over 4
2293

            
2294
=item C<table>
2295

            
2296
Table name.
2297

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

            
2300
=item C<column>
2301

            
2302
Column clause. This is array reference or constant value.
2303

            
2304
    # Hash refernce
2305
    $dbi->select(column => ['author', 'title']);
2306
    
2307
    # Constant value
2308
    $dbi->select(column => 'author');
2309

            
2310
Default is '*' unless C<column> is specified.
2311

            
2312
    # Default
2313
    $dbi->select(column => '*');
2314

            
2315
=item C<where>
2316

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

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2330
    # Array refrendce (where clause and parameter)
2331
    $dbi->select(where =>
2332
        [
2333
            ['and', '{= author}', '{like title}'],
2334
            {author => 'Ken', title => '%Perl%'}
2335
        ]
2336
    );
2337
    
update pod
Yuki Kimoto authored on 2011-03-13
2338
=item C<join> EXPERIMENTAL
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2339

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

            
2342
    $dbi->select(join =>
2343
        [
2344
            'left outer join company on book.company_id = company_id',
2345
            'left outer join location on company.location_id = location.id'
2346
        ]
2347
    );
2348

            
2349
If column cluase or where clause contain table name like "company.name",
2350
needed join clause is used automatically.
2351

            
2352
    $dbi->select(
2353
        table => 'book',
2354
        column => ['company.location_id as company__location_id'],
2355
        where => {'company.name' => 'Orange'},
2356
        join => [
2357
            'left outer join company on book.company_id = company.id',
2358
            'left outer join location on company.location_id = location.id'
2359
        ]
2360
    );
2361

            
2362
In above select, the following SQL is created.
2363

            
2364
    select company.location_id as company__location_id
2365
    from book
2366
      left outer join company on book.company_id = company.id
2367
    where company.name = Orange
2368

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

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

            
2382
For example, if you want to contain tag in join clause, 
2383
you can pass parameter by C<param> option.
2384

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

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

            
2389
    $dbi->select(append => 'order by title');
2390

            
2391
=item C<filter>
2392

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

            
2397
    # Basic
2398
    $dbi->select(
2399
        filter => [
2400
            title  => sub { uc $_[0] }
2401
            author => sub { uc $_[0] }
2402
        ]
2403
    );
2404
    
2405
    # At once
2406
    $dbi->select(
2407
        filter => [
2408
            [qw/title author/]  => sub { uc $_[0] }
2409
        ]
2410
    );
2411
    
2412
    # Filter name
2413
    $dbi->select(
2414
        filter => [
2415
            title  => 'upper_case',
2416
            author => 'upper_case'
2417
        ]
2418
    );
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
2419

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

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

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

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

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

            
2431
    my $sql = $query->sql;
2432

            
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
2433
=item C<type> EXPERIMENTAL
2434

            
2435
Specify database data type.
2436

            
2437
    $dbi->select(type => [image => DBI::SQL_BLOB]);
2438
    $dbi->select(type => [[qw/image audio/] => DBI::SQL_BLOB]);
2439

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

            
2442
    $sth->bind_param($pos, $value, DBI::SQL_BLOB);
2443

            
update pod
Yuki Kimoto authored on 2011-03-12
2444
=back
cleanup
Yuki Kimoto authored on 2011-03-08
2445

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

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

            
2450
    $dbi->select_at(
2451
        table => 'book',
2452
        primary_key => 'id',
2453
        where => '5'
2454
    );
2455

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2460
=over 4
2461

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

            
update pod
Yuki Kimoto authored on 2011-03-12
2464
Primary key. This is constant value or array reference.
2465
    
2466
    # Constant value
2467
    $dbi->select(primary_key => 'id');
2468

            
2469
    # Array reference
2470
    $dbi->select(primary_key => ['id1', 'id2' ]);
2471

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

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

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

            
2479
    # Constant value
2480
    $dbi->select(where => 5);
2481

            
2482
    # Array reference
2483
    $dbi->select(where => [3, 5]);
2484

            
2485
In first examle, the following SQL is created.
2486

            
2487
    select * from book where id = ?
2488

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2491
=back
2492

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2495
    $dbi->update(
2496
        table  => 'book',
2497
        param  => {title => 'Perl'},
2498
        where  => {id => 4}
2499
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
2500

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2505
=over 4
2506

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2509
Table name.
2510

            
2511
    $dbi->update(table => 'book');
2512

            
2513
=item C<param>
2514

            
2515
Update data. This is hash reference.
2516

            
2517
    $dbi->update(param => {title => 'Perl'});
2518

            
2519
=item C<where>
2520

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

            
2542
=item C<append>
2543

            
2544
Append statement to last of SQL. This is string.
2545

            
2546
    $dbi->update(append => 'order by title');
2547

            
2548
=item C<filter>
2549

            
2550
Filter, executed before data is send to database. This is array reference.
2551
Filter value is code reference or
2552
filter name registerd by C<register_filter()>.
2553

            
2554
    # Basic
2555
    $dbi->update(
2556
        filter => [
2557
            title  => sub { uc $_[0] }
2558
            author => sub { uc $_[0] }
2559
        ]
2560
    );
2561
    
2562
    # At once
2563
    $dbi->update(
2564
        filter => [
2565
            [qw/title author/]  => sub { uc $_[0] }
2566
        ]
2567
    );
2568
    
2569
    # Filter name
2570
    $dbi->update(
2571
        filter => [
2572
            title  => 'upper_case',
2573
            author => 'upper_case'
2574
        ]
2575
    );
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2576

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

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

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

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

            
2586
You can check SQL.
2587

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2590
=back
2591

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

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

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

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

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

            
2603
    $dbi->update_at(
2604
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
2605
        primary_key => 'id',
2606
        where => '5',
2607
        param => {title => 'Perl'}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2608
    );
2609

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2614
=over 4
2615

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

            
2618
Primary key. This is constant value or array reference.
2619
    
2620
    # Constant value
2621
    $dbi->update(primary_key => 'id');
2622

            
2623
    # Array reference
2624
    $dbi->update(primary_key => ['id1', 'id2' ]);
2625

            
2626
This is used to create where clause.
2627

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

            
2630
Where clause, created from primary key information.
2631
This is constant value or array reference.
2632

            
2633
    # Constant value
2634
    $dbi->update(where => 5);
2635

            
2636
    # Array reference
2637
    $dbi->update(where => [3, 5]);
2638

            
2639
In first examle, the following SQL is created.
2640

            
2641
    update book set title = ? where id = ?
2642

            
2643
Place holders are set to 'Perl' and 5.
2644

            
update pod
Yuki Kimoto authored on 2011-03-13
2645
=back
2646

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

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

            
2651
Create update parameter tag.
2652

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

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2655
You can create tag without 'set '
2656
by C<no_set> option. This option is EXPERIMENTAL.
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
2657

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2658
    my $update_param_tag = $dbi->update_param_tag(
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
2659
        {title => 'a', age => 2}
2660
        {no_set => 1}
2661
    );
2662

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

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

            
cleanup
Yuki Kimoto authored on 2011-03-09
2667
    my $where = $dbi->where(
2668
        clause => ['and', '{= title}', '{= author}'],
2669
        param => {title => 'Perl', author => 'Ken'}
2670
    );
fix tests
Yuki Kimoto authored on 2011-01-18
2671

            
2672
Create a new L<DBIx::Custom::Where> object.
2673

            
update pod
Yuki Kimoto authored on 2011-03-13
2674
=head2 C<setup_model> EXPERIMENTAL
cleanup
Yuki Kimoto authored on 2011-01-12
2675

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

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
2681
=head1 Tags
2682

            
2683
The following tags is available.
2684

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2685
=head2 C<table>
add table tag
Yuki Kimoto authored on 2011-02-09
2686

            
2687
Table tag
2688

            
2689
    {table TABLE}    ->    TABLE
2690

            
update pod
Yuki Kimoto authored on 2011-03-13
2691
This is used to tell C<execute()> what table is needed .
add table tag
Yuki Kimoto authored on 2011-02-09
2692

            
cleanup
Yuki Kimoto authored on 2011-01-25
2693
=head2 C<?>
2694

            
2695
Placeholder tag.
2696

            
2697
    {? NAME}    ->   ?
2698

            
2699
=head2 C<=>
2700

            
2701
Equal tag.
2702

            
2703
    {= NAME}    ->   NAME = ?
2704

            
2705
=head2 C<E<lt>E<gt>>
2706

            
2707
Not equal tag.
2708

            
2709
    {<> NAME}   ->   NAME <> ?
2710

            
2711
=head2 C<E<lt>>
2712

            
2713
Lower than tag
2714

            
2715
    {< NAME}    ->   NAME < ?
2716

            
2717
=head2 C<E<gt>>
2718

            
2719
Greater than tag
2720

            
2721
    {> NAME}    ->   NAME > ?
2722

            
2723
=head2 C<E<gt>=>
2724

            
2725
Greater than or equal tag
2726

            
2727
    {>= NAME}   ->   NAME >= ?
2728

            
2729
=head2 C<E<lt>=>
2730

            
2731
Lower than or equal tag
2732

            
2733
    {<= NAME}   ->   NAME <= ?
2734

            
2735
=head2 C<like>
2736

            
2737
Like tag
2738

            
2739
    {like NAME}   ->   NAME like ?
2740

            
2741
=head2 C<in>
2742

            
2743
In tag.
2744

            
2745
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
2746

            
2747
=head2 C<insert_param>
2748

            
2749
Insert parameter tag.
2750

            
2751
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
2752

            
2753
=head2 C<update_param>
2754

            
2755
Updata parameter tag.
2756

            
2757
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
2758

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

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

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

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

            
2768
C<< <kimoto.yuki at gmail.com> >>
2769

            
2770
L<http://github.com/yuki-kimoto/DBIx-Custom>
2771

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
2772
=head1 AUTHOR
2773

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

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

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

            
2780
This program is free software; you can redistribute it and/or modify it
2781
under the same terms as Perl itself.
2782

            
2783
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
2784

            
2785