Newer Older
311 lines | 7.238kb
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) {
62
        $code .= "$attr => exists \$self->{$attr} ? \$self->{$attr} : \$self->$attr,";
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
    
119
    return $self;
120
}
121

            
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
122
# DEPRECATED!
micro optimization
Yuki Kimoto authored on 2011-07-30
123
has 'filter';
cleanup
Yuki Kimoto authored on 2011-06-15
124
has 'name';
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-10-26
125
has 'type';
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
126

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

            
128
# DEPRECATED!
129
sub method {
130
    warn "method method is DEPRECATED! use helper instead";
131
    return shift->helper(@_);
132
}
133

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
134
1;
135

            
136
=head1 NAME
137

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

            
140
=head1 SYNOPSIS
141

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

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

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

            
148
=head2 C<dbi>
149

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

            
153
L<DBIx::Custom> object.
154

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

            
157
    my $created_at = $model->created_at;
158
    $model = $model->created_at('created_datatime');
159

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

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

            
164
    my $join = $model->join;
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
165
    $model = $model->join(
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
166
        ['left outer join company on book.company_id = company.id']
167
    );
168
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
169
Join clause, this value is passed to C<select> method.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
170

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

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

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

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

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

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

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

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
188
    my $type = $model->bind_type;
189
    $model = $model->bind_type(['image' => DBI::SQL_BLOB]);
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
190
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
191
Database data type, this is used as type optioon of C<insert>, 
192
C<update>, C<update_all>, C<delete>, C<delete_all>,
193
C<select>, and C<execute> method
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
194

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

            
197
    my $updated_at = $model->updated_at;
198
    $model = $model->updated_at('updated_datatime');
199

            
200
Updated timestamp column, this is passed to C<update> method.
201

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

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

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

            
210
    my $count = $model->count;
211

            
212
Get rows count.
213

            
214
Options is same as C<select> method's ones.
215

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

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

            
223
=head2 C<delete_all>
224

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
225
    $model->delete_all(...);
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_all> 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.
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
229

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

            
232
    $model->execute(...);
233

            
234
Same as C<execute> 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.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
236

            
237
=head2 C<insert>
238

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
239
    $model->insert(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
240
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
241
Same as C<insert> 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

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

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
246
    $model->helper(
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
247
        update_or_insert => sub {
248
            my $self = shift;
249
            
250
            # ...
251
        },
252
        find_or_create   => sub {
253
            my $self = shift;
254
            
255
            # ...
256
    );
257

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

            
260
    $model->update_or_insert;
261
    $model->find_or_create;
262

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

            
265
    my $column = $self->mycolumn;
266
    my $column = $self->mycolumn(book => ['author', 'title']);
267
    my $column = $self->mycolumn(['author', 'title']);
268

            
269
Create column clause for myself. The follwoing column clause is created.
270

            
271
    book.author as author,
272
    book.title as title
273

            
274
If table name is ommited, C<table> attribute of the model is used.
275
If column names is omitted, C<columns> attribute of the model is used.
276

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

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

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

            
283
=head2 C<select>
284

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

            
290
=head2 C<update>
291

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
292
    $model->update(...);
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<update> 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_all>
298

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
299
    $model->update_all(param => \%param);
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_all> 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.
303

            
304
=head2 C<update_or_insert EXPERIMENTAL>
305

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

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