Newer Older
281 lines | 6.408kb
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;
65
    push @_, ($_ => $self->$_) for qw/table bind_type primary_key type/;
66
    return $self->dbi->execute(@_);
67
}
68

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

            
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
71
sub method {
72
    my $self = shift;
73
    
74
    # Merge
75
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
76
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
77
    
78
    return $self;
79
}
80

            
cleanup
Yuki Kimoto authored on 2011-03-21
81
sub mycolumn {
82
    my $self = shift;
83
    my $table = shift unless ref $_[0];
84
    my $columns = shift;
85
    
86
    $table ||= $self->table || '';
87
    $columns ||= $self->columns;
88
    
89
    return $self->dbi->mycolumn($table, $columns);
90
}
91

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

            
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
105
# DEPRECATED!
micro optimization
Yuki Kimoto authored on 2011-07-30
106
has 'filter';
cleanup
Yuki Kimoto authored on 2011-06-15
107
has 'name';
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
108
has type => sub { [] };
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
109

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
110
1;
111

            
112
=head1 NAME
113

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

            
116
=head1 SYNOPSIS
117

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

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

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

            
124
=head2 C<dbi>
125

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

            
129
L<DBIx::Custom> object.
130

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

            
133
    my $join = $model->join;
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
134
    $model = $model->join(
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
135
        ['left outer join company on book.company_id = company.id']
136
    );
137
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
138
Join clause, this value is passed to C<select> method.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
139

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

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

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

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

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

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

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

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
157
    my $type = $model->bind_type;
158
    $model = $model->bind_type(['image' => DBI::SQL_BLOB]);
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
159
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
160
Database data type, this is used as type optioon of C<insert>, 
161
C<update>, C<update_all>, C<delete>, C<delete_all>,
162
C<select>, and C<execute> method
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
163

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

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

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
170
=head2 C<call_dbi> EXPERIMENTAL
171

            
172
    $model->call_dbi('insert',
173
      {args => ['table', 'primary_key' 'bind_type']}, @_)
174

            
175
Call L<DBIx::Custom>(or subclass) method. you can add
176
attribute values of model to arguments by C<args> option.
177

            
178
Generally this method is used when you want to added dbi method to model.
179

            
180
    sub insert {
181
        shift->call_dbi('insert',
182
          {args => ['table', 'primary_key' 'bind_type']}, @_);
183
    }
184

            
185
=head2 C<count> EXPERIMENTAL
186

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

            
189
Get rows count.
190

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

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

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

            
200
=head2 C<delete_all>
201

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

            
207
=head2 C<execute EXPERIMENTAL>
208

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

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

            
214
=head2 C<insert>
215

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

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
221
=head2 C<method>
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
222

            
223
    $model->method(
224
        update_or_insert => sub {
225
            my $self = shift;
226
            
227
            # ...
228
        },
229
        find_or_create   => sub {
230
            my $self = shift;
231
            
232
            # ...
233
    );
234

            
235
Register method. These method is called directly from L<DBIx::Custom::Model> object.
236

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

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

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

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

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

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

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

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

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

            
260
=head2 C<select>
261

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

            
267
=head2 C<update>
268

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

            
274
=head2 C<update_all>
275

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

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