Newer Older
300 lines | 7.135kb
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
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
39
                 delete delete_at delete_all select select_at count/;
cleanup
Yuki Kimoto authored on 2011-10-21
40
for my $method (@methods) {
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
41

            
42
    my $code = sub {
43
        my $self = shift;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-10-26
44
        
45
        unless ($self->{_attribute_cache}{$self}) {
46
            $self->$_ for qw/table bind_type primary_key
47
              type join created_at updated_at/;
48
            $self->{_attribute_cache}{$self} = 1;
49
        }
50

            
- removed EXPERIMENTAL call_...
Yuki Kimoto authored on 2011-09-12
51
        $self->dbi->$method(
52
            @_ % 2 ? shift : (),
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-10-26
53
            table => $self->{table},
54
            exists $self->{type} ? (type => $self->{type}) : (),
55
            exists $self->{created_at} && $method eq 'insert' ? (created_at => $self->{created_at}) : (),
56
            exists $self->{updated_at} && ($method eq 'insert' || $method eq 'update') ? (updated_at => $self->{updated_at}) : (),
57
            exists $self->{bind_type} ? (bind_type=> $self->{bind_type}) : (),
58
            exists $self->{primary_key} ? (primary_key => $self->{primary_key}) : (),
59
            exists $self->{join} && index($method, 'select') != -1 ? (join => $self->{join}) : (),
- removed EXPERIMENTAL call_...
Yuki Kimoto authored on 2011-09-12
60
            @_
61
        )
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
62
    };
63
    
64
    no strict 'refs';
65
    my $class = __PACKAGE__;
66
    *{"${class}::$method"} = $code;
67
}
68

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

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

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

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

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
105
sub new {
106
    my $self = shift->SUPER::new(@_);
107
    
108
    # Check attribute names
109
    my @attrs = keys %$self;
cleanup
Yuki Kimoto authored on 2011-10-21
110
    for my $attr (@attrs) {
cleanup
Yuki Kimoto authored on 2011-04-25
111
        croak qq{"$attr" is invalid attribute name } . _subname
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
112
          unless $self->can($attr);
113
    }
114
    
115
    return $self;
116
}
117

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

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

            
124
# DEPRECATED!
125
sub method {
126
    warn "method method is DEPRECATED! use helper instead";
127
    return shift->helper(@_);
128
}
129

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
130
1;
131

            
132
=head1 NAME
133

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

            
136
=head1 SYNOPSIS
137

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

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

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

            
144
=head2 C<dbi>
145

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

            
149
L<DBIx::Custom> object.
150

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

            
153
    my $created_at = $model->created_at;
154
    $model = $model->created_at('created_datatime');
155

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

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

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

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

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

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

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

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

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

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

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

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

            
193
    my $updated_at = $model->updated_at;
194
    $model = $model->updated_at('updated_datatime');
195

            
196
Updated timestamp column, this is passed to C<update> method.
197

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

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

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

            
206
    my $count = $model->count;
207

            
208
Get rows count.
209

            
210
Options is same as C<select> method's ones.
211

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

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
214
    $model->delete(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
215
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
216
Same as C<delete> of L<DBIx::Custom> except that
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
217
you don't have to specify C<table> and C<primary_key> option.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
218

            
219
=head2 C<delete_all>
220

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
221
    $model->delete_all(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
222
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
223
Same as C<delete_all> of L<DBIx::Custom> except that
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
224
you don't have to specify C<table> and C<primary_key> option.
225

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

            
228
    $model->execute(...);
229

            
230
Same as C<execute> of L<DBIx::Custom> except that
231
you don't have to specify C<table> and C<primary_key> option.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
232

            
233
=head2 C<insert>
234

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
235
    $model->insert(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
236
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
237
Same as C<insert> of L<DBIx::Custom> except that
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
238
you don't have to specify C<table> and C<primary_key> option.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
239

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

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

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

            
256
    $model->update_or_insert;
257
    $model->find_or_create;
258

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

            
261
    my $column = $self->mycolumn;
262
    my $column = $self->mycolumn(book => ['author', 'title']);
263
    my $column = $self->mycolumn(['author', 'title']);
264

            
265
Create column clause for myself. The follwoing column clause is created.
266

            
267
    book.author as author,
268
    book.title as title
269

            
270
If table name is ommited, C<table> attribute of the model is used.
271
If column names is omitted, C<columns> attribute of the model is used.
272

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

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

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

            
279
=head2 C<select>
280

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
281
    $model->select(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
282
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
283
Same as C<select> of L<DBIx::Custom> except that
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
284
you don't have to specify C<table>, C<primary_key> and C<jon> option.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
285

            
286
=head2 C<update>
287

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
288
    $model->update(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
289
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
290
Same as C<update> of L<DBIx::Custom> except that
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
291
you don't have to specify C<table> and C<primary_key> option.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
292

            
293
=head2 C<update_all>
294

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
295
    $model->update_all(param => \%param);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
296
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
297
Same as C<update_all> of L<DBIx::Custom> except that
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
298
you don't have to specify C<table> and C<primary_key> option.
update pod
Yuki Kimoto authored on 2011-02-28
299

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