Newer Older
280 lines | 6.184kb
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

            
cleanup
Yuki Kimoto authored on 2011-06-15
10
has [qw/dbi table/],
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
11
    bind_type => sub { [] },
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
12
    columns => sub { [] },
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
13
    join => sub { [] },
updatedd pod
Yuki Kimoto authored on 2011-06-12
14
    primary_key => sub { [] };
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
15

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
16
our $AUTOLOAD;
17

            
18
sub AUTOLOAD {
19
    my $self = shift;
20

            
21
    # Method name
22
    my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
23

            
24
    # Method
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
25
    $self->{_methods} ||= {};
26
    if (my $method = $self->{_methods}->{$mname}) {
27
        return $self->$method(@_)
28
    }
29
    elsif (my $dbi_method = $self->dbi->can($mname)) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
30
        $self->dbi->$dbi_method(@_);
31
    }
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
32
    elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
33
        $self->dbi->dbh->$dbh_method(@_);
34
    }
35
    else {
cleanup
Yuki Kimoto authored on 2011-04-25
36
        croak qq{Can't locate object method "$mname" via "$package" }
37
            . _subname;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
38
    }
39
}
40

            
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
41
my @methods = qw/insert insert_at update update_at update_all
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
42
                 delete delete_at delete_all select select_at count/;
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
43
foreach my $method (@methods) {
44

            
45
    my $code = sub {
46
        my $self = shift;
- removed EXPERIMENTAL call_...
Yuki Kimoto authored on 2011-09-12
47
        $self->dbi->$method(
48
            @_ % 2 ? shift : (),
49
            table => $self->table,
50
            bind_type => $self->bind_type,
51
            primary_key => $self->primary_key,
52
            type => $self->type,
53
            $method =~ /^select/ ? (join => $self->join) : (), 
54
            @_
55
        )
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
56
    };
57
    
58
    no strict 'refs';
59
    my $class = __PACKAGE__;
60
    *{"${class}::$method"} = $code;
61
}
62

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
63
sub execute {
64
    my $self = shift;
- removed EXPERIMENTAL the f...
Yuki Kimoto authored on 2011-09-12
65
    return $self->dbi->execute(
66
        shift,
67
        shift,
68
        table => $self->table,
69
        bind_type => $self->bind_type,
70
        primary_key => $self->primary_key,
71
        type => $self->type,
72
        @_
73
    );
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
74
}
75

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

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
88
sub mycolumn {
89
    my $self = shift;
90
    my $table = shift unless ref $_[0];
91
    my $columns = shift;
92
    
93
    $table ||= $self->table || '';
94
    $columns ||= $self->columns;
95
    
96
    return $self->dbi->mycolumn($table, $columns);
97
}
98

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
99
sub new {
100
    my $self = shift->SUPER::new(@_);
101
    
102
    # Check attribute names
103
    my @attrs = keys %$self;
104
    foreach my $attr (@attrs) {
cleanup
Yuki Kimoto authored on 2011-04-25
105
        croak qq{"$attr" is invalid attribute name } . _subname
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
106
          unless $self->can($attr);
107
    }
108
    
109
    return $self;
110
}
111

            
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
112
# DEPRECATED!
micro optimization
Yuki Kimoto authored on 2011-07-30
113
has 'filter';
cleanup
Yuki Kimoto authored on 2011-06-15
114
has 'name';
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
115
has type => sub { [] };
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
116

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

            
118
# DEPRECATED!
119
sub method {
120
    warn "method method is DEPRECATED! use helper instead";
121
    return shift->helper(@_);
122
}
123

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
124
1;
125

            
126
=head1 NAME
127

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

            
130
=head1 SYNOPSIS
131

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

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

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

            
138
=head2 C<dbi>
139

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

            
143
L<DBIx::Custom> object.
144

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

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

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

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

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

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

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

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

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

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

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

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

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

            
186
    my $count = $model->count;
187

            
188
Get rows count.
189

            
190
Options is same as C<select> method's ones.
191

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

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

            
199
=head2 C<delete_all>
200

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

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

            
208
    $model->execute(...);
209

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

            
213
=head2 C<insert>
214

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

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

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
222
    $model->helper(
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
223
        update_or_insert => sub {
224
            my $self = shift;
225
            
226
            # ...
227
        },
228
        find_or_create   => sub {
229
            my $self = shift;
230
            
231
            # ...
232
    );
233

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

            
236
    $model->update_or_insert;
237
    $model->find_or_create;
238

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

            
241
    my $column = $self->mycolumn;
242
    my $column = $self->mycolumn(book => ['author', 'title']);
243
    my $column = $self->mycolumn(['author', 'title']);
244

            
245
Create column clause for myself. The follwoing column clause is created.
246

            
247
    book.author as author,
248
    book.title as title
249

            
250
If table name is ommited, C<table> attribute of the model is used.
251
If column names is omitted, C<columns> attribute of the model is used.
252

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

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

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

            
259
=head2 C<select>
260

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

            
266
=head2 C<update>
267

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

            
273
=head2 C<update_all>
274

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

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