Newer Older
325 lines | 6.673kb
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1
package DBIx::Custom::Model;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
2

            
3
use strict;
4
use warnings;
5

            
6
use base 'Object::Simple';
7

            
8
use Carp 'croak';
9

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
10
# Carp trust relationship
11
push @DBIx::Custom::CARP_NOT, __PACKAGE__;
12

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
13
__PACKAGE__->attr(
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
14
    ['dbi', 'name', 'table'],
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
15
    columns => sub { [] },
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
16
    filter => sub { [] },
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
17
    primary_key => sub { [] },
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
18
);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
19

            
20
our $AUTOLOAD;
21

            
22
sub AUTOLOAD {
23
    my $self = shift;
24

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
25
    # Method name
26
    my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
27

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
28
    # Method
29
    $self->{_methods} ||= {};
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
30
    if (my $method = $self->{_methods}->{$mname}) {
31
        return $self->$method(@_)
32
    }
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
33
    elsif ($self->dbi->can($mname)) {
34
        $self->dbi->$mname(@_);
35
    }
36
    elsif ($self->dbi->dbh->can($mname)) {
37
        $self->dbi->dbh->$mname(@_);
38
    }
39
    else {
40
        croak qq/Can't locate object method "$mname" via "$package"/
41
    }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
42
}
43

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
44
sub column_clause {
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
45
    my $self = shift;
46
    
select method column option ...
Yuki Kimoto authored on 2011-02-22
47
    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
48
    
select method column option ...
Yuki Kimoto authored on 2011-02-22
49
    my $table   = $self->table;
50
    my $columns = $self->columns;
51
    my $add     = $args->{add} || [];
52
    my $remove  = $args->{remove} || [];
53
    my %remove  = map {$_ => 1} @$remove;
54
    
55
    my @column;
56
    foreach my $column (@$columns) {
57
        push @column, "$table.$column as $column"
58
          unless $remove{$column};
59
    }
60
    
61
    foreach my $column (@$add) {
62
        push @column, $column;
63
    }
64
    
65
    return join (', ', @column);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
66
}
67

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
68
sub delete {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
69
    my $self = shift;
select method column option ...
Yuki Kimoto authored on 2011-02-22
70
    $self->dbi->delete(table => $self->table, @_);
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
71
}
72

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
73
sub delete_all {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
74
    my $self = shift;
select method column option ...
Yuki Kimoto authored on 2011-02-22
75
    $self->dbi->delete_all(table => $self->table, @_);
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
76
}
77

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
78
sub delete_at {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
79
    my $self = shift;
select method column option ...
Yuki Kimoto authored on 2011-02-22
80
    
81
    return $self->dbi->delete_at(
82
        table => $self->table,
83
        primary_key => $self->primary_key,
84
        @_
85
    );
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
86
}
87

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

            
90
sub insert {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
91
    my $self = shift;
select method column option ...
Yuki Kimoto authored on 2011-02-22
92
    $self->dbi->insert(table => $self->table, @_);
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
93
}
94

            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
95
sub insert_at {
96
    my $self = shift;
97
    
98
    return $self->dbi->insert_at(
99
        table => $self->table,
100
        primary_key => $self->primary_key,
101
        @_
102
    );
103
}
104

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
105
sub method {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
106
    my $self = shift;
select method column option ...
Yuki Kimoto authored on 2011-02-22
107
    
108
    # Merge
109
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
110
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
111
    
112
    return $self;
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
113
}
114

            
115
sub select {
116
    my $self = shift;
117
    $self->dbi->select(
118
        table => $self->table,
119
        @_
120
    );
121
}
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
122

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
123
sub select_at {
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
124
    my $self = shift;
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
125
    
select method column option ...
Yuki Kimoto authored on 2011-02-22
126
    return $self->dbi->select_at(
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
127
        table => $self->table,
128
        primary_key => $self->primary_key,
129
        @_
130
    );
131
}
132

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
133
sub update {
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
134
    my $self = shift;
select method column option ...
Yuki Kimoto authored on 2011-02-22
135
    $self->dbi->update(table => $self->table, @_)
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
136
}
137

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
138
sub update_all {
139
    my $self = shift;
140
    $self->dbi->update_all(table => $self->table, @_);
141
}
142

            
143

            
144
sub update_at {
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
145
    my $self = shift;
many changed
Yuki Kimoto authored on 2011-01-23
146
    
select method column option ...
Yuki Kimoto authored on 2011-02-22
147
    return $self->dbi->update_at(
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
148
        table => $self->table,
149
        primary_key => $self->primary_key,
150
        @_
151
    );
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
152
}
153

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
154
1;
155

            
156
=head1 NAME
157

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
158
DBIx::Custom::Model - Model (experimental)
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
159

            
160
=head1 SYNOPSIS
161

            
162
use DBIx::Custom::Table;
163

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
164
my $table = DBIx::Custom::Model->new(table => 'books');
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
165

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

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
168
=head2 C<columns>
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
169

            
170
    my $columns = $model->columns;
171
    $model      = $model->columns(['id', 'number']);
172

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
173
=head2 C<dbi>
174

            
175
    my $dbi = $model->dbi;
176
    $model  = $model->dbi($dbi);
177

            
178
L<DBIx::Custom> object.
179

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
180
=head2 C<filter>
181

            
182
    my $dbi = $model->filter
183
    $model  = $model->filter({out => 'tp_to_date', in => 'date_to_tp'});
184

            
185
This filter is applied when L<DBIx::Custom> C<include_model()> is called.
186

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
187
=head2 C<name>
188

            
189
    my $name = $model->name;
190
    $model   = $model->name('book');
191

            
192
Model name.
193

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

            
196
    my $table = $model->table;
197
    $model    = $model->table('book');
198

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
199
Table name. Model name and table name is different.
200
Table name is real table name in database.
201

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
202
=head2 C<primary_key>
203

            
204
    my $primary_key = $model->primary_key;
205
    $model          = $model->primary_key(['id', 'number']);
206

            
update pod
Yuki Kimoto authored on 2011-02-28
207
Foreign key. This is used by C<insert_at>,C<update_at()>,
208
C<delete_at()>,C<select_at()>.
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
209

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

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
212
L<DBIx::Custom> inherits all methods from L<Object::Simple>,
213
and you can use all methods of the object set to C<dbi>.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
214
and implements the following new ones.
215

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
216
=head2 C<column_clause()>
217

            
218
To create column clause automatically, use C<column_clause()>.
219
Valude of C<table> and C<columns> is used.
220

            
221
    my $column_clause = $model->column_clause;
222

            
223
If C<table> is 'book'�AC<column> is ['id', 'name'],
224
the following clause is created.
225

            
226
    book.id as id, book.name as name
227

            
228
These column name is for removing column name ambiguities.
229

            
230
If you remove some columns, use C<remove> option.
231

            
232
    my $column_clause = $model->column_clause(remove => ['id']);
233

            
234
If you add some column, use C<add> option.
235

            
236
    my $column_clause = $model->column_clause(add => ['company.id as company__id']);
237

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

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
240
    $table->delete(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
241
    
242
Same as C<delete()> of L<DBIx::Custom> except that
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
243
you don't have to specify C<table> option.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
244

            
245
=head2 C<delete_all>
246

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
247
    $table->delete_all(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
248
    
249
Same as C<delete_all()> of L<DBIx::Custom> except that
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
250
you don't have to specify C<table> option.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
251

            
update pod
Yuki Kimoto authored on 2011-02-28
252
=head2 C<delete_at>
253

            
254
    $table->delete_at(...);
255
    
256
Same as C<delete()> of L<DBIx::Custom> except that
257
you don't have to specify C<table> and C<primary_key> option.
258

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
259
=head2 C<method>
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
260

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
261
    $table->method(
262
        count => sub {
263
            my $self = shift;
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
264
        
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
265
            return $self->select(column => 'count(*)', @_)
266
                        ->fetch_first->[0];
267
        }
268
    );
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
269
    
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
270
Add method to a L<DBIx::Custom::Table> object.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
271

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

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
274
    $table->insert(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
275
    
276
Same as C<insert()> of L<DBIx::Custom> except that
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
277
you don't have to specify C<table> option.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
278

            
update pod
Yuki Kimoto authored on 2011-02-28
279
=head2 C<insert>
280

            
281
    $table->insert_at(...);
282
    
283
Same as C<insert_at()> of L<DBIx::Custom> except that
284
you don't have to specify C<table> and C<primary_key> option.
285

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

            
288
    my $table = DBIx::Custom::Table->new;
289

            
290
Create a L<DBIx::Custom::Table> object.
291

            
292
=head2 C<select>
293

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
294
    $table->select(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
295
    
296
Same as C<select()> of L<DBIx::Custom> except that
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
297
you don't have to specify C<table> option.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
298

            
update pod
Yuki Kimoto authored on 2011-02-28
299
=head2 C<select_at>
300

            
301
    $table->select_at(...);
302
    
303
Same as C<select_at()> of L<DBIx::Custom> except that
304
you don't have to specify C<table> and C<primary_key> option.
305

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
306
=head2 C<update>
307

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
308
    $table->update(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
309
    
310
Same as C<update()> of L<DBIx::Custom> except that
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
311
you don't have to specify C<table> option.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
312

            
313
=head2 C<update_all>
314

            
315
    $table->update_all(param => \%param);
316
    
317
Same as C<update_all()> of L<DBIx::Custom> except that
318
you don't have to specify table name.
update pod
Yuki Kimoto authored on 2011-02-28
319

            
320
=head2 C<update_at>
321

            
322
    $table->update_at(...);
323
    
324
Same as C<update_at()> of L<DBIx::Custom> except that
325
you don't have to specify C<table> and C<primary_key> option.