Newer Older
340 lines | 8.302kb
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1
package DBIx::Custom::Model;
updatedd pod
Yuki Kimoto authored on 2011-06-12
2
use Object::Simple -base;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
3

            
4
use Carp 'croak';
cleanup
Yuki Kimoto authored on 2011-04-25
5
use DBIx::Custom::Util '_subname';
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
6

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
7
# Carp trust relationship
8
push @DBIx::Custom::CARP_NOT, __PACKAGE__;
9

            
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-10-26
10
has [qw/dbi table created_at updated_at bind_type join primary_key/],
11
    columns => sub { [] };
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
12

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
13
our $AUTOLOAD;
14

            
15
sub AUTOLOAD {
16
    my $self = shift;
17

            
18
    # Method name
19
    my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
20

            
21
    # Method
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
22
    $self->{_methods} ||= {};
23
    if (my $method = $self->{_methods}->{$mname}) {
24
        return $self->$method(@_)
25
    }
26
    elsif (my $dbi_method = $self->dbi->can($mname)) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
27
        $self->dbi->$dbi_method(@_);
28
    }
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
29
    elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
30
        $self->dbi->dbh->$dbh_method(@_);
31
    }
32
    else {
cleanup
Yuki Kimoto authored on 2011-04-25
33
        croak qq{Can't locate object method "$mname" via "$package" }
34
            . _subname;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
35
    }
36
}
37

            
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
38
my @methods = qw/insert insert_at update update_at update_all
fixed update_or_insert metho...
Yuki Kimoto authored on 2011-10-31
39
  delete delete_at delete_all select select_at count/;
cleanup
Yuki Kimoto authored on 2011-10-21
40
for my $method (@methods) {
micro optimization
Yuki Kimoto authored on 2011-10-31
41
    
42
    my $code =
43
         qq/sub {/ .
44
         qq/my \$self = shift;/ .
45
         qq/\$self->dbi->$method(/ .
46
             qq/\@_ % 2 ? shift : (),/;
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
47

            
micro optimization
Yuki Kimoto authored on 2011-10-31
48
    
49
    my @attrs = qw/table type primary_key bind_type/;
50
    my @insert_attrs = qw/created_at updated_at/;
51
    my @update_attrs = qw/updated_at/;
52
    my @select_attrs = qw/join/;
53
    if ($method eq 'insert') { push @attrs, @insert_attrs }
54
    elsif ($method eq 'update') { push @attrs, @update_attrs }
- fixed bug that DBIx::Custo...
Yuki Kimoto authored on 2011-11-25
55
    elsif (index($method, 'select') != -1 || $method eq 'count') {
56
        push @attrs, @select_attrs
57
    }
micro optimization
Yuki Kimoto authored on 2011-10-31
58
    
59
    for my $attr (@attrs) {
micro optimization
Yuki Kimoto authored on 2011-10-31
60
        $code .= "exists \$self->{$attr} ? ($attr => \$self->{$attr}) : (),";
micro optimization
Yuki Kimoto authored on 2011-10-31
61
    }
62
    
63
    $code .= qq/\@_);/ .
64
         qq/}/;
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
65
    
66
    no strict 'refs';
micro optimization
Yuki Kimoto authored on 2011-10-31
67
    *{__PACKAGE__ . "::$method"} = eval $code;
68
    croak $code if $@;
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
69
}
70

            
fixed update_or_insert metho...
Yuki Kimoto authored on 2011-10-31
71
sub update_or_insert {
72
    my ($self, $param, %opt) = @_;
73
    
74
    croak "update_or_insert method need primary_key and id option "
75
      unless (defined $opt{id} || defined $self->{id})
76
          && (defined $opt{primary_key} || defined $self->{primary_key});
77
    
78
    my $statement_opt = $opt{option} || {};
- id option work if id count...
Yuki Kimoto authored on 2011-11-03
79
    my $rows = $self->select(%opt, %{$statement_opt->{select} || {}})->all;
80
    if (@$rows == 0) {
81
        return $self->insert($param, %opt, %{$statement_opt->{insert} || {}});
82
    }
83
    elsif (@$rows == 1) {
84
        return $self->update($param, %opt, %{$statement_opt->{update} || {}});
85
    }
86
    else {
87
        croak "selected row must be one " . _subname;
88
    }
fixed update_or_insert metho...
Yuki Kimoto authored on 2011-10-31
89
}
90

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
91
sub execute {
92
    my $self = shift;
- DBIx::Custom::QueryBuilder...
Yuki Kimoto authored on 2011-11-04
93
    
94
    if ($ENV{DBIX_CUSTOM_DISABLE_MODEL_EXECUTE}) {
95
        $self->dbi->execute(@_);
96
    }
97
    else {
98
        warn "DBIx::Custom::Model execute method is DEPRECATED! " .
99
             "use DBIx::Custom execute method. " .
100
             "If you want to call DBIx::Custom execute method directory from model, " .
101
             "set \$ENV{DBIX_CUSTOM_DISABLE_MODEL_EXECUTE} to 1 " .
102
             "until DBIx::Custom::Model execute method is removed in the future." ;
103
        return $self->dbi->execute(
104
            shift,
105
            shift,
106
            table => $self->table,
107
            bind_type => $self->bind_type,
108
            primary_key => $self->primary_key,
109
            type => $self->type,
110
            @_
111
        );    
112
    }
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
113
}
114

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
115
sub DESTROY { }
116

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
117
sub helper {
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
118
    my $self = shift;
119
    
120
    # Merge
121
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
122
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
123
    
124
    return $self;
125
}
126

            
cleanup
Yuki Kimoto authored on 2011-03-21
127
sub mycolumn {
128
    my $self = shift;
129
    my $table = shift unless ref $_[0];
130
    my $columns = shift;
131
    
132
    $table ||= $self->table || '';
133
    $columns ||= $self->columns;
134
    
135
    return $self->dbi->mycolumn($table, $columns);
136
}
137

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
138
sub new {
139
    my $self = shift->SUPER::new(@_);
140
    
141
    # Check attribute names
142
    my @attrs = keys %$self;
cleanup
Yuki Kimoto authored on 2011-10-21
143
    for my $attr (@attrs) {
cleanup
Yuki Kimoto authored on 2011-04-25
144
        croak qq{"$attr" is invalid attribute name } . _subname
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
145
          unless $self->can($attr);
146
    }
147
    
micro optimization
Yuki Kimoto authored on 2011-10-31
148
    # Cache
149
    for my $attr (qw/dbi table created_at updated_at bind_type join primary_key/) {
150
        $self->$attr;
151
        $self->{$attr} = undef unless exists $self->{$attr};
152
    }
153
    $self->columns;
154
    
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
155
    return $self;
156
}
157

            
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
158
# DEPRECATED!
micro optimization
Yuki Kimoto authored on 2011-07-30
159
has 'filter';
cleanup
Yuki Kimoto authored on 2011-06-15
160
has 'name';
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-10-26
161
has 'type';
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
162

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
163

            
164
# DEPRECATED!
165
sub method {
166
    warn "method method is DEPRECATED! use helper instead";
167
    return shift->helper(@_);
168
}
169

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
170
1;
171

            
172
=head1 NAME
173

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
174
DBIx::Custom::Model - Model
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
175

            
176
=head1 SYNOPSIS
177

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
178
use DBIx::Custom::Model;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
179

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
180
my $model = DBIx::Custom::Model->new(table => 'books');
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
181

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
182
=head1 ATTRIBUTES
183

            
184
=head2 C<dbi>
185

            
186
    my $dbi = $model->dbi;
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
187
    $model = $model->dbi($dbi);
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
188

            
189
L<DBIx::Custom> object.
190

            
update_or_insert method's re...
Yuki Kimoto authored on 2011-10-27
191
=head2 C<created_at EXPERIMENTAL>
192

            
193
    my $created_at = $model->created_at;
194
    $model = $model->created_at('created_datatime');
195

            
196
Create timestamp column, this is passed to C<insert> or C<update> method.
197

            
update pod
Yuki Kimoto authored on 2011-03-13
198
=head2 C<join>
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
199

            
200
    my $join = $model->join;
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
201
    $model = $model->join(
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
202
        ['left outer join company on book.company_id = company.id']
203
    );
204
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
205
Join clause, this value is passed to C<select> method.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
206

            
added DBIx::Custom result_fi...
Yuki Kimoto authored on 2011-06-12
207
=head2 C<primary_key>
208

            
209
    my $primary_key = $model->primary_key;
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
210
    $model = $model->primary_key(['id', 'number']);
added DBIx::Custom result_fi...
Yuki Kimoto authored on 2011-06-12
211

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
212
Primary key,this is passed to C<insert>, C<update>,
213
C<delete>, and C<select> method.
added DBIx::Custom result_fi...
Yuki Kimoto authored on 2011-06-12
214

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
215
=head2 C<table>
216

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
217
    my $model = $model->table;
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
218
    $model = $model->table('book');
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
219

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
220
Table name, this is passed to C<select> method.
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
221

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
222
=head2 C<bind_type>
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
223

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
224
    my $type = $model->bind_type;
225
    $model = $model->bind_type(['image' => DBI::SQL_BLOB]);
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
226
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
227
Database data type, this is used as type optioon of C<insert>, 
228
C<update>, C<update_all>, C<delete>, C<delete_all>,
cleanup
Yuki Kimoto authored on 2011-11-01
229
and C<select> method
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
230

            
update_or_insert method's re...
Yuki Kimoto authored on 2011-10-27
231
=head2 C<updated_at EXPERIMENTAL>
232

            
233
    my $updated_at = $model->updated_at;
234
    $model = $model->updated_at('updated_datatime');
235

            
236
Updated timestamp column, this is passed to C<update> method.
237

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
238
=head1 METHODS
239

            
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
240
L<DBIx::Custom::Model> inherits all methods from L<Object::Simple>,
241
and you can use all methods of L<DBIx::Custom> and L<DBI>
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
242
and implements the following new ones.
243

            
- removed EXPERIMENTAL the f...
Yuki Kimoto authored on 2011-09-12
244
=head2 C<count>
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
245

            
246
    my $count = $model->count;
247

            
248
Get rows count.
249

            
250
Options is same as C<select> method's ones.
251

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
252
=head2 C<delete>
253

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
254
    $model->delete(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
255
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
256
Same as C<delete> of L<DBIx::Custom> except that
micro optimization
Yuki Kimoto authored on 2011-10-31
257
you don't have to specify options if you set attribute in model.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
258

            
259
=head2 C<delete_all>
260

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
261
    $model->delete_all(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
262
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
263
Same as C<delete_all> of L<DBIx::Custom> except that
micro optimization
Yuki Kimoto authored on 2011-10-31
264
you don't have to specify options if you set attribute in model.
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
265

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
266
=head2 C<insert>
267

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
268
    $model->insert(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
269
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
270
Same as C<insert> of L<DBIx::Custom> except that
micro optimization
Yuki Kimoto authored on 2011-10-31
271
you don't have to specify options if you set attribute in model.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
272

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
273
=head2 C<helper>
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
274

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
275
    $model->helper(
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
276
        update_or_insert => sub {
277
            my $self = shift;
278
            
279
            # ...
280
        },
281
        find_or_create   => sub {
282
            my $self = shift;
283
            
284
            # ...
285
    );
286

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
287
Register helper. These helper is called directly from L<DBIx::Custom::Model> object.
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
288

            
289
    $model->update_or_insert;
290
    $model->find_or_create;
291

            
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
292
=head2 C<mycolumn>
cleanup
Yuki Kimoto authored on 2011-03-21
293

            
294
    my $column = $self->mycolumn;
295
    my $column = $self->mycolumn(book => ['author', 'title']);
296
    my $column = $self->mycolumn(['author', 'title']);
297

            
298
Create column clause for myself. The follwoing column clause is created.
299

            
300
    book.author as author,
301
    book.title as title
302

            
303
If table name is ommited, C<table> attribute of the model is used.
304
If column names is omitted, C<columns> attribute of the model is used.
305

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
306
=head2 C<new>
307

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
308
    my $model = DBIx::Custom::Model->new;
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
309

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
310
Create a L<DBIx::Custom::Model> object.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
311

            
312
=head2 C<select>
313

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
314
    $model->select(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
315
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
316
Same as C<select> of L<DBIx::Custom> except that
micro optimization
Yuki Kimoto authored on 2011-10-31
317
you don't have to specify options if you set attribute in model.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
318

            
319
=head2 C<update>
320

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
321
    $model->update(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
322
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
323
Same as C<update> of L<DBIx::Custom> except that
micro optimization
Yuki Kimoto authored on 2011-10-31
324
you don't have to specify options if you set attribute in model.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
325

            
326
=head2 C<update_all>
327

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
328
    $model->update_all(param => \%param);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
329
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
330
Same as C<update_all> of L<DBIx::Custom> except that
micro optimization
Yuki Kimoto authored on 2011-10-31
331
you don't have to specify options if you set attribute in model.
332

            
- id option work if id count...
Yuki Kimoto authored on 2011-11-03
333
=head2 C<update_or_insert>
micro optimization
Yuki Kimoto authored on 2011-10-31
334

            
335
    $model->update_or_insert(...);
336
    
337
Same as C<update> of L<DBIx::Custom> except that
338
you don't have to specify options if you set attribute in model.
update pod
Yuki Kimoto authored on 2011-02-28
339

            
update pod
Yuki Kimoto authored on 2011-06-15
340
=cut