Newer Older
336 lines | 6.945kb
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 { [] },
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
18
    join => sub { [] }
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
19
);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
20

            
21
our $AUTOLOAD;
22

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

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

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

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

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

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

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

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

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

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

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

            
116
sub select {
117
    my $self = shift;
118
    $self->dbi->select(
119
        table => $self->table,
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
120
        join => $self->join,
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
121
        @_
122
    );
123
}
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
124

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

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

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

            
145

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

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
156
1;
157

            
158
=head1 NAME
159

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

            
162
=head1 SYNOPSIS
163

            
164
use DBIx::Custom::Table;
165

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

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

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

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

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

            
177
    my $dbi = $model->dbi;
178
    $model  = $model->dbi($dbi);
179

            
180
L<DBIx::Custom> object.
181

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

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

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

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

            
191
    my $name = $model->name;
192
    $model   = $model->name('book');
193

            
194
Model name.
195

            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
196
=head2 C<(experimental) join>
197

            
198
    my $join = $model->join;
199
    $model   = $model->join(
200
        ['left outer join company on book.company_id = company.id']
201
    );
202
    
203
Default join clause. This is used by C<select()>.
204

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

            
207
    my $table = $model->table;
208
    $model    = $model->table('book');
209

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

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

            
215
    my $primary_key = $model->primary_key;
216
    $model          = $model->primary_key(['id', 'number']);
217

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

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

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

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

            
229
To create column clause automatically, use C<column_clause()>.
230
Valude of C<table> and C<columns> is used.
231

            
232
    my $column_clause = $model->column_clause;
233

            
234
If C<table> is 'book'�AC<column> is ['id', 'name'],
235
the following clause is created.
236

            
237
    book.id as id, book.name as name
238

            
239
These column name is for removing column name ambiguities.
240

            
241
If you remove some columns, use C<remove> option.
242

            
243
    my $column_clause = $model->column_clause(remove => ['id']);
244

            
245
If you add some column, use C<add> option.
246

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

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

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

            
256
=head2 C<delete_all>
257

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

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

            
265
    $table->delete_at(...);
266
    
267
Same as C<delete()> of L<DBIx::Custom> except that
268
you don't have to specify C<table> and C<primary_key> option.
269

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

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
272
    $table->method(
273
        count => sub {
274
            my $self = shift;
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
275
        
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
276
            return $self->select(column => 'count(*)', @_)
277
                        ->fetch_first->[0];
278
        }
279
    );
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
280
    
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
281
Add method to a L<DBIx::Custom::Table> object.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
282

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

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

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

            
292
    $table->insert_at(...);
293
    
294
Same as C<insert_at()> of L<DBIx::Custom> except that
295
you don't have to specify C<table> and C<primary_key> option.
296

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

            
299
    my $table = DBIx::Custom::Table->new;
300

            
301
Create a L<DBIx::Custom::Table> object.
302

            
303
=head2 C<select>
304

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

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

            
312
    $table->select_at(...);
313
    
314
Same as C<select_at()> of L<DBIx::Custom> except that
315
you don't have to specify C<table> and C<primary_key> option.
316

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

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

            
324
=head2 C<update_all>
325

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

            
331
=head2 C<update_at>
332

            
333
    $table->update_at(...);
334
    
335
Same as C<update_at()> of L<DBIx::Custom> except that
336
you don't have to specify C<table> and C<primary_key> option.