Newer Older
288 lines | 6.547kb
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;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
47
        my $args = [qw/table bind_type primary_key type/];
48
        push @$args, 'join' if $method =~ /^select/;
49
        $self->call_dbi($method, {args => $args}, @_);
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
50
    };
51
    
52
    no strict 'refs';
53
    my $class = __PACKAGE__;
54
    *{"${class}::$method"} = $code;
55
}
56

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
57
sub call_dbi {
58
    my $self = shift;
59
    my $method = shift;
60
    my $options = shift;
61
    my $arg_names = $options->{args};
62
    
63
    my @args;
64
    push @args, ($_ => $self->$_) for @$arg_names;
65
    unshift @args, shift if @_ % 2;
66
    
67
    return $self->dbi->$method(@args, @_);
68
}
69

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
70
sub execute {
71
    my $self = shift;
72
    push @_, ($_ => $self->$_) for qw/table bind_type primary_key type/;
73
    return $self->dbi->execute(@_);
74
}
75

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

            
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
78
sub method {
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

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
117
1;
118

            
119
=head1 NAME
120

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

            
123
=head1 SYNOPSIS
124

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

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

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

            
131
=head2 C<dbi>
132

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

            
136
L<DBIx::Custom> object.
137

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

            
140
    my $join = $model->join;
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
141
    $model = $model->join(
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
142
        ['left outer join company on book.company_id = company.id']
143
    );
144
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
145
Join clause, this value is passed to C<select> method.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
146

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

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

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

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

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

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

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

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
164
    my $type = $model->bind_type;
165
    $model = $model->bind_type(['image' => DBI::SQL_BLOB]);
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
166
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
167
Database data type, this is used as type optioon of C<insert>, 
168
C<update>, C<update_all>, C<delete>, C<delete_all>,
169
C<select>, and C<execute> method
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
170

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

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

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

            
179
    $model->call_dbi('insert',
180
      {args => ['table', 'primary_key' 'bind_type']}, @_)
181

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

            
185
Generally this method is used when you want to added dbi method to model.
186

            
187
    sub insert {
188
        shift->call_dbi('insert',
189
          {args => ['table', 'primary_key' 'bind_type']}, @_);
190
    }
191

            
192
=head2 C<count> EXPERIMENTAL
193

            
194
    my $count = $model->count;
195

            
196
Get rows count.
197

            
198
Options is same as C<select> method's ones.
199

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

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

            
207
=head2 C<delete_all>
208

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

            
214
=head2 C<execute EXPERIMENTAL>
215

            
216
    $model->execute(...);
217

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

            
221
=head2 C<insert>
222

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

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

            
230
    $model->method(
231
        update_or_insert => sub {
232
            my $self = shift;
233
            
234
            # ...
235
        },
236
        find_or_create   => sub {
237
            my $self = shift;
238
            
239
            # ...
240
    );
241

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

            
244
    $model->update_or_insert;
245
    $model->find_or_create;
246

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

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

            
253
Create column clause for myself. The follwoing column clause is created.
254

            
255
    book.author as author,
256
    book.title as title
257

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

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

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

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

            
267
=head2 C<select>
268

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
269
    $model->select(...);
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<select> 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>, C<primary_key> and C<jon> option.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
273

            
274
=head2 C<update>
275

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

            
281
=head2 C<update_all>
282

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

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