Newer Older
318 lines | 7.439kb
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
micro optimization
Yuki Kimoto authored on 2011-10-31
39
  delete delete_at delete_all select select_at count update_or_insert/;
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 @update_or_insert_attrs = qw/created_at updated_at/;
53
    my @select_attrs = qw/join/;
54
    if ($method eq 'insert') { push @attrs, @insert_attrs }
55
    elsif ($method eq 'update_or_insert') {
56
        push @attrs, @update_or_insert_attrs;
57
    }
58
    elsif ($method eq 'update') { push @attrs, @update_attrs }
59
    elsif (index($method, 'select') != -1) { push @attrs, @select_attrs }
60
    
61
    for my $attr (@attrs) {
micro optimization
Yuki Kimoto authored on 2011-10-31
62
        $code .= "exists \$self->{$attr} ? ($attr => \$self->{$attr}) : (),";
micro optimization
Yuki Kimoto authored on 2011-10-31
63
    }
64
    
65
    $code .= qq/\@_);/ .
66
         qq/}/;
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
67
    
68
    no strict 'refs';
micro optimization
Yuki Kimoto authored on 2011-10-31
69
    *{__PACKAGE__ . "::$method"} = eval $code;
70
    croak $code if $@;
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
71
}
72

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
73
sub execute {
74
    my $self = shift;
- removed EXPERIMENTAL the f...
Yuki Kimoto authored on 2011-09-12
75
    return $self->dbi->execute(
76
        shift,
77
        shift,
78
        table => $self->table,
79
        bind_type => $self->bind_type,
80
        primary_key => $self->primary_key,
81
        type => $self->type,
82
        @_
83
    );
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
84
}
85

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

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
88
sub helper {
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
89
    my $self = shift;
90
    
91
    # Merge
92
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
93
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
94
    
95
    return $self;
96
}
97

            
cleanup
Yuki Kimoto authored on 2011-03-21
98
sub mycolumn {
99
    my $self = shift;
100
    my $table = shift unless ref $_[0];
101
    my $columns = shift;
102
    
103
    $table ||= $self->table || '';
104
    $columns ||= $self->columns;
105
    
106
    return $self->dbi->mycolumn($table, $columns);
107
}
108

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
109
sub new {
110
    my $self = shift->SUPER::new(@_);
111
    
112
    # Check attribute names
113
    my @attrs = keys %$self;
cleanup
Yuki Kimoto authored on 2011-10-21
114
    for my $attr (@attrs) {
cleanup
Yuki Kimoto authored on 2011-04-25
115
        croak qq{"$attr" is invalid attribute name } . _subname
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
116
          unless $self->can($attr);
117
    }
118
    
micro optimization
Yuki Kimoto authored on 2011-10-31
119
    # Cache
120
    for my $attr (qw/dbi table created_at updated_at bind_type join primary_key/) {
121
        $self->$attr;
122
        $self->{$attr} = undef unless exists $self->{$attr};
123
    }
124
    $self->columns;
125
    
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
126
    return $self;
127
}
128

            
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
129
# DEPRECATED!
micro optimization
Yuki Kimoto authored on 2011-07-30
130
has 'filter';
cleanup
Yuki Kimoto authored on 2011-06-15
131
has 'name';
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-10-26
132
has 'type';
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
133

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

            
135
# DEPRECATED!
136
sub method {
137
    warn "method method is DEPRECATED! use helper instead";
138
    return shift->helper(@_);
139
}
140

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
141
1;
142

            
143
=head1 NAME
144

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

            
147
=head1 SYNOPSIS
148

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

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

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

            
155
=head2 C<dbi>
156

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

            
160
L<DBIx::Custom> object.
161

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

            
164
    my $created_at = $model->created_at;
165
    $model = $model->created_at('created_datatime');
166

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

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

            
171
    my $join = $model->join;
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
172
    $model = $model->join(
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
173
        ['left outer join company on book.company_id = company.id']
174
    );
175
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
176
Join clause, this value is passed to C<select> method.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
177

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

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

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

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

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

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

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

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
195
    my $type = $model->bind_type;
196
    $model = $model->bind_type(['image' => DBI::SQL_BLOB]);
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
197
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
198
Database data type, this is used as type optioon of C<insert>, 
199
C<update>, C<update_all>, C<delete>, C<delete_all>,
200
C<select>, and C<execute> method
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
201

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

            
204
    my $updated_at = $model->updated_at;
205
    $model = $model->updated_at('updated_datatime');
206

            
207
Updated timestamp column, this is passed to C<update> method.
208

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

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

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

            
217
    my $count = $model->count;
218

            
219
Get rows count.
220

            
221
Options is same as C<select> method's ones.
222

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

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

            
230
=head2 C<delete_all>
231

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

            
- removed EXPERIMENTAL the f...
Yuki Kimoto authored on 2011-09-12
237
=head2 C<execute>
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
238

            
239
    $model->execute(...);
240

            
241
Same as C<execute> of L<DBIx::Custom> except that
micro optimization
Yuki Kimoto authored on 2011-10-31
242
you don't have to specify options if you set attribute in model.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
243

            
244
=head2 C<insert>
245

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

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

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
253
    $model->helper(
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
254
        update_or_insert => sub {
255
            my $self = shift;
256
            
257
            # ...
258
        },
259
        find_or_create   => sub {
260
            my $self = shift;
261
            
262
            # ...
263
    );
264

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

            
267
    $model->update_or_insert;
268
    $model->find_or_create;
269

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

            
272
    my $column = $self->mycolumn;
273
    my $column = $self->mycolumn(book => ['author', 'title']);
274
    my $column = $self->mycolumn(['author', 'title']);
275

            
276
Create column clause for myself. The follwoing column clause is created.
277

            
278
    book.author as author,
279
    book.title as title
280

            
281
If table name is ommited, C<table> attribute of the model is used.
282
If column names is omitted, C<columns> attribute of the model is used.
283

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

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

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

            
290
=head2 C<select>
291

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

            
297
=head2 C<update>
298

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

            
304
=head2 C<update_all>
305

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

            
311
=head2 C<update_or_insert EXPERIMENTAL>
312

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

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