Newer Older
338 lines | 7.812kb
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/],
cleanup
Yuki Kimoto authored on 2012-01-20
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 {
cleanup
Yuki Kimoto authored on 2012-01-20
16
  my $self = shift;
17

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

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

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

            
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 }
55
  elsif (index($method, 'select') != -1 || $method eq 'count') {
56
    push @attrs, @select_attrs
57
  }
58
  
59
  for my $attr (@attrs) {
60
    $code .= "exists \$self->{$attr} ? ($attr => \$self->{$attr}) : (),";
61
  }
62
  
63
  $code .= qq/\@_);/ .
64
       qq/}/;
65
  
66
  no strict 'refs';
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 {
cleanup
Yuki Kimoto authored on 2012-01-20
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} || {};
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 { croak "selected row must be one " . _subname }
fixed update_or_insert metho...
Yuki Kimoto authored on 2011-10-31
87
}
88

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

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

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

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

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

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

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
161
# DEPRECATED!
162
sub method {
cleanup
Yuki Kimoto authored on 2012-01-20
163
  warn "method method is DEPRECATED! use helper instead";
164
  return shift->helper(@_);
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
165
}
166

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
167
1;
168

            
169
=head1 NAME
170

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

            
173
=head1 SYNOPSIS
174

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

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

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

            
181
=head2 C<dbi>
182

            
cleanup
Yuki Kimoto authored on 2012-01-20
183
  my $dbi = $model->dbi;
184
  $model = $model->dbi($dbi);
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
185

            
186
L<DBIx::Custom> object.
187

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
188
=head2 C<created_at>
update_or_insert method's re...
Yuki Kimoto authored on 2011-10-27
189

            
cleanup
Yuki Kimoto authored on 2012-01-20
190
  my $created_at = $model->created_at;
191
  $model = $model->created_at('created_datatime');
update_or_insert method's re...
Yuki Kimoto authored on 2011-10-27
192

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
206
  my $primary_key = $model->primary_key;
207
  $model = $model->primary_key(['id', 'number']);
added DBIx::Custom result_fi...
Yuki Kimoto authored on 2011-06-12
208

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
214
  my $model = $model->table;
215
  $model = $model->table('book');
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
216

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

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

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

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
228
=head2 C<updated_at>
update_or_insert method's re...
Yuki Kimoto authored on 2011-10-27
229

            
cleanup
Yuki Kimoto authored on 2012-01-20
230
  my $updated_at = $model->updated_at;
231
  $model = $model->updated_at('updated_datatime');
update_or_insert method's re...
Yuki Kimoto authored on 2011-10-27
232

            
233
Updated timestamp column, this is passed to C<update> method.
234

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
243
  my $count = $model->count;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
244

            
245
Get rows count.
246

            
247
Options is same as C<select> method's ones.
248

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
251
  $model->delete(...);
252
  
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
253
Same as C<delete> of L<DBIx::Custom> except that
micro optimization
Yuki Kimoto authored on 2011-10-31
254
you don't have to specify options if you set attribute in model.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
255

            
256
=head2 C<delete_all>
257

            
cleanup
Yuki Kimoto authored on 2012-01-20
258
  $model->delete_all(...);
259
  
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
260
Same as C<delete_all> of L<DBIx::Custom> except that
micro optimization
Yuki Kimoto authored on 2011-10-31
261
you don't have to specify options if you set attribute in model.
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
262

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
265
  $model->insert(...);
266
  
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
267
Same as C<insert> of L<DBIx::Custom> except that
micro optimization
Yuki Kimoto authored on 2011-10-31
268
you don't have to specify options if you set attribute in model.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
269

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
272
  $model->helper(
273
    update_or_insert => sub {
274
      my $self = shift;
275
      
276
      # ...
277
    },
278
    find_or_create   => sub {
279
      my $self = shift;
280
      
281
      # ...
282
    }
283
  );
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
284

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
287
  $model->update_or_insert;
288
  $model->find_or_create;
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
289

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
292
  my $column = $self->mycolumn;
293
  my $column = $self->mycolumn(book => ['author', 'title']);
294
  my $column = $self->mycolumn(['author', 'title']);
cleanup
Yuki Kimoto authored on 2011-03-21
295

            
296
Create column clause for myself. The follwoing column clause is created.
297

            
cleanup
Yuki Kimoto authored on 2012-01-20
298
  book.author as author,
299
  book.title as title
cleanup
Yuki Kimoto authored on 2011-03-21
300

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
306
  my $model = DBIx::Custom::Model->new;
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
307

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

            
310
=head2 C<select>
311

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

            
317
=head2 C<update>
318

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

            
324
=head2 C<update_all>
325

            
cleanup
Yuki Kimoto authored on 2012-01-20
326
  $model->update_all(param => \%param);
327
  
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
328
Same as C<update_all> of L<DBIx::Custom> except that
micro optimization
Yuki Kimoto authored on 2011-10-31
329
you don't have to specify options if you set attribute in model.
330

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
333
  $model->update_or_insert(...);
334
  
micro optimization
Yuki Kimoto authored on 2011-10-31
335
Same as C<update> of L<DBIx::Custom> except that
336
you don't have to specify options if you set attribute in model.
update pod
Yuki Kimoto authored on 2011-02-28
337

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