Newer Older
286 lines | 6.737kb
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 pod
Yuki Kimoto authored on 2011-03-13
151
=head2 C<join>
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
152

            
153
    my $join = $model->join;
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
154
    $model = $model->join(
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
155
        ['left outer join company on book.company_id = company.id']
156
    );
157
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
158
Join clause, this value is passed to C<select> method.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
159

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

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

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

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

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

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

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

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
177
    my $type = $model->bind_type;
178
    $model = $model->bind_type(['image' => DBI::SQL_BLOB]);
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
179
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
180
Database data type, this is used as type optioon of C<insert>, 
181
C<update>, C<update_all>, C<delete>, C<delete_all>,
182
C<select>, and C<execute> method
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
183

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

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

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

            
192
    my $count = $model->count;
193

            
194
Get rows count.
195

            
196
Options is same as C<select> method's ones.
197

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

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

            
205
=head2 C<delete_all>
206

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

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

            
214
    $model->execute(...);
215

            
216
Same as C<execute> of L<DBIx::Custom> except that
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<insert>
220

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
221
    $model->insert(...);
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<insert> 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.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
225

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

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
228
    $model->helper(
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
229
        update_or_insert => sub {
230
            my $self = shift;
231
            
232
            # ...
233
        },
234
        find_or_create   => sub {
235
            my $self = shift;
236
            
237
            # ...
238
    );
239

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

            
242
    $model->update_or_insert;
243
    $model->find_or_create;
244

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

            
247
    my $column = $self->mycolumn;
248
    my $column = $self->mycolumn(book => ['author', 'title']);
249
    my $column = $self->mycolumn(['author', 'title']);
250

            
251
Create column clause for myself. The follwoing column clause is created.
252

            
253
    book.author as author,
254
    book.title as title
255

            
256
If table name is ommited, C<table> attribute of the model is used.
257
If column names is omitted, C<columns> attribute of the model is used.
258

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

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

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

            
265
=head2 C<select>
266

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
267
    $model->select(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
268
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
269
Same as C<select> of L<DBIx::Custom> except that
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
270
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
271

            
272
=head2 C<update>
273

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

            
279
=head2 C<update_all>
280

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
281
    $model->update_all(param => \%param);
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<update_all> 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> and C<primary_key> option.
update pod
Yuki Kimoto authored on 2011-02-28
285

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