Newer Older
328 lines | 7.819kb
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 }
55
    elsif (index($method, 'select') != -1) { push @attrs, @select_attrs }
56
    
57
    for my $attr (@attrs) {
micro optimization
Yuki Kimoto authored on 2011-10-31
58
        $code .= "exists \$self->{$attr} ? ($attr => \$self->{$attr}) : (),";
micro optimization
Yuki Kimoto authored on 2011-10-31
59
    }
60
    
61
    $code .= qq/\@_);/ .
62
         qq/}/;
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
63
    
64
    no strict 'refs';
micro optimization
Yuki Kimoto authored on 2011-10-31
65
    *{__PACKAGE__ . "::$method"} = eval $code;
66
    croak $code if $@;
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
67
}
68

            
fixed update_or_insert metho...
Yuki Kimoto authored on 2011-10-31
69
sub update_or_insert {
70
    my ($self, $param, %opt) = @_;
71
    
72
    croak "update_or_insert method need primary_key and id option "
73
      unless (defined $opt{id} || defined $self->{id})
74
          && (defined $opt{primary_key} || defined $self->{primary_key});
75
    
76
    my $statement_opt = $opt{option} || {};
- id option work if id count...
Yuki Kimoto authored on 2011-11-03
77
    my $rows = $self->select(%opt, %{$statement_opt->{select} || {}})->all;
78
    if (@$rows == 0) {
79
        return $self->insert($param, %opt, %{$statement_opt->{insert} || {}});
80
    }
81
    elsif (@$rows == 1) {
82
        return $self->update($param, %opt, %{$statement_opt->{update} || {}});
83
    }
84
    else {
85
        croak "selected row must be one " . _subname;
86
    }
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 {
DBIx::Custom::Model execute ...
Yuki Kimoto authored on 2011-11-01
90
    warn "DBIx::Custom::Model execute method is DEPRECATED!";
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
91
    my $self = shift;
- removed EXPERIMENTAL the f...
Yuki Kimoto authored on 2011-09-12
92
    return $self->dbi->execute(
93
        shift,
94
        shift,
95
        table => $self->table,
96
        bind_type => $self->bind_type,
97
        primary_key => $self->primary_key,
98
        type => $self->type,
99
        @_
100
    );
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
101
}
102

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

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
105
sub helper {
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
106
    my $self = shift;
107
    
108
    # Merge
109
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
110
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
111
    
112
    return $self;
113
}
114

            
cleanup
Yuki Kimoto authored on 2011-03-21
115
sub mycolumn {
116
    my $self = shift;
117
    my $table = shift unless ref $_[0];
118
    my $columns = shift;
119
    
120
    $table ||= $self->table || '';
121
    $columns ||= $self->columns;
122
    
123
    return $self->dbi->mycolumn($table, $columns);
124
}
125

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
126
sub new {
127
    my $self = shift->SUPER::new(@_);
128
    
129
    # Check attribute names
130
    my @attrs = keys %$self;
cleanup
Yuki Kimoto authored on 2011-10-21
131
    for my $attr (@attrs) {
cleanup
Yuki Kimoto authored on 2011-04-25
132
        croak qq{"$attr" is invalid attribute name } . _subname
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
133
          unless $self->can($attr);
134
    }
135
    
micro optimization
Yuki Kimoto authored on 2011-10-31
136
    # Cache
137
    for my $attr (qw/dbi table created_at updated_at bind_type join primary_key/) {
138
        $self->$attr;
139
        $self->{$attr} = undef unless exists $self->{$attr};
140
    }
141
    $self->columns;
142
    
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
143
    return $self;
144
}
145

            
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
146
# DEPRECATED!
micro optimization
Yuki Kimoto authored on 2011-07-30
147
has 'filter';
cleanup
Yuki Kimoto authored on 2011-06-15
148
has 'name';
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-10-26
149
has 'type';
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
150

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

            
152
# DEPRECATED!
153
sub method {
154
    warn "method method is DEPRECATED! use helper instead";
155
    return shift->helper(@_);
156
}
157

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
158
1;
159

            
160
=head1 NAME
161

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

            
164
=head1 SYNOPSIS
165

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

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

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

            
172
=head2 C<dbi>
173

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

            
177
L<DBIx::Custom> object.
178

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

            
181
    my $created_at = $model->created_at;
182
    $model = $model->created_at('created_datatime');
183

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

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

            
188
    my $join = $model->join;
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
189
    $model = $model->join(
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
190
        ['left outer join company on book.company_id = company.id']
191
    );
192
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
193
Join clause, this value is passed to C<select> method.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
194

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

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

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

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

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

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

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

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

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

            
221
    my $updated_at = $model->updated_at;
222
    $model = $model->updated_at('updated_datatime');
223

            
224
Updated timestamp column, this is passed to C<update> method.
225

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

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

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

            
234
    my $count = $model->count;
235

            
236
Get rows count.
237

            
238
Options is same as C<select> method's ones.
239

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

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

            
247
=head2 C<delete_all>
248

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

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

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

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

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
263
    $model->helper(
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
264
        update_or_insert => sub {
265
            my $self = shift;
266
            
267
            # ...
268
        },
269
        find_or_create   => sub {
270
            my $self = shift;
271
            
272
            # ...
273
    );
274

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

            
277
    $model->update_or_insert;
278
    $model->find_or_create;
279

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

            
282
    my $column = $self->mycolumn;
283
    my $column = $self->mycolumn(book => ['author', 'title']);
284
    my $column = $self->mycolumn(['author', 'title']);
285

            
286
Create column clause for myself. The follwoing column clause is created.
287

            
288
    book.author as author,
289
    book.title as title
290

            
291
If table name is ommited, C<table> attribute of the model is used.
292
If column names is omitted, C<columns> attribute of the model is used.
293

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

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

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

            
300
=head2 C<select>
301

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

            
307
=head2 C<update>
308

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

            
314
=head2 C<update_all>
315

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

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

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

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