Newer Older
302 lines | 6.349kb
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

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
6
use base 'DBIx::Custom';
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
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(
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
14
    ['dbi', 'name', 'table', 'view'],
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 { [] },
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
17
    join => sub { [] },
18
    primary_key => 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

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
21
sub column_clause {
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
22
    my $self = shift;
23
    
select method column option ...
Yuki Kimoto authored on 2011-02-22
24
    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
25
    
select method column option ...
Yuki Kimoto authored on 2011-02-22
26
    my $table   = $self->table;
27
    my $columns = $self->columns;
28
    my $add     = $args->{add} || [];
29
    my $remove  = $args->{remove} || [];
30
    my %remove  = map {$_ => 1} @$remove;
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-11
31
    my $prefix  = $args->{prefix} || '';
select method column option ...
Yuki Kimoto authored on 2011-02-22
32
    
33
    my @column;
34
    foreach my $column (@$columns) {
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-11
35
        push @column, "$table.$column as $prefix$column"
select method column option ...
Yuki Kimoto authored on 2011-02-22
36
          unless $remove{$column};
37
    }
38
    
39
    foreach my $column (@$add) {
40
        push @column, $column;
41
    }
42
    
43
    return join (', ', @column);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
44
}
45

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
46
sub delete {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
47
    my $self = shift;
select method column option ...
Yuki Kimoto authored on 2011-02-22
48
    $self->dbi->delete(table => $self->table, @_);
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
49
}
50

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
51
sub delete_all {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
52
    my $self = shift;
select method column option ...
Yuki Kimoto authored on 2011-02-22
53
    $self->dbi->delete_all(table => $self->table, @_);
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
54
}
55

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
56
sub delete_at {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
57
    my $self = shift;
select method column option ...
Yuki Kimoto authored on 2011-02-22
58
    
59
    return $self->dbi->delete_at(
60
        table => $self->table,
61
        primary_key => $self->primary_key,
62
        @_
63
    );
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
64
}
65

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

            
68
sub insert {
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->insert(table => $self->table, @_);
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
71
}
72

            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
73
sub insert_at {
74
    my $self = shift;
75
    
76
    return $self->dbi->insert_at(
77
        table => $self->table,
78
        primary_key => $self->primary_key,
79
        @_
80
    );
81
}
82

            
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
83
sub select {
84
    my $self = shift;
85
    $self->dbi->select(
86
        table => $self->table,
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
87
        join => $self->join,
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
88
        @_
89
    );
90
}
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
91

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
92
sub select_at {
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
93
    my $self = shift;
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
94
    
select method column option ...
Yuki Kimoto authored on 2011-02-22
95
    return $self->dbi->select_at(
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
96
        table => $self->table,
97
        primary_key => $self->primary_key,
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-11
98
        join => $self->join,
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
99
        @_
100
    );
101
}
102

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
103
sub update {
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
104
    my $self = shift;
select method column option ...
Yuki Kimoto authored on 2011-02-22
105
    $self->dbi->update(table => $self->table, @_)
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
106
}
107

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
108
sub update_all {
109
    my $self = shift;
110
    $self->dbi->update_all(table => $self->table, @_);
111
}
112

            
113

            
114
sub update_at {
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
115
    my $self = shift;
many changed
Yuki Kimoto authored on 2011-01-23
116
    
select method column option ...
Yuki Kimoto authored on 2011-02-22
117
    return $self->dbi->update_at(
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
118
        table => $self->table,
119
        primary_key => $self->primary_key,
120
        @_
121
    );
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
122
}
123

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

            
126
=head1 NAME
127

            
update pod
Yuki Kimoto authored on 2011-03-13
128
DBIx::Custom::Model - Model EXPERIMENTAL
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
129

            
130
=head1 SYNOPSIS
131

            
132
use DBIx::Custom::Table;
133

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
134
my $table = 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;
141
    $model  = $model->dbi($dbi);
142

            
143
L<DBIx::Custom> object.
144

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

            
147
    my $dbi = $model->filter
148
    $model  = $model->filter({out => 'tp_to_date', in => 'date_to_tp'});
149

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

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

            
154
    my $name = $model->name;
155
    $model   = $model->name('book');
156

            
157
Model name.
158

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

            
161
    my $join = $model->join;
162
    $model   = $model->join(
163
        ['left outer join company on book.company_id = company.id']
164
    );
165
    
166
Default join clause. This is used by C<select()>.
167

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

            
170
    my $table = $model->table;
171
    $model    = $model->table('book');
172

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

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

            
178
    my $primary_key = $model->primary_key;
179
    $model          = $model->primary_key(['id', 'number']);
180

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

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
184
=head2 C<view>
185

            
186
    my $view = $model->view;
187
    $model   = $model->view('select id, DATE(issue_datetime) as date from book');
188

            
189
View. This view is registered by C<view()> of L<DBIx::Custom> when
190
model is included by C<include_model>.
191

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

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

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

            
200
To create column clause automatically, use C<column_clause()>.
201
Valude of C<table> and C<columns> is used.
202

            
203
    my $column_clause = $model->column_clause;
204

            
205
If C<table> is 'book'�AC<column> is ['id', 'name'],
206
the following clause is created.
207

            
208
    book.id as id, book.name as name
209

            
210
These column name is for removing column name ambiguities.
211

            
212
If you remove some columns, use C<remove> option.
213

            
214
    my $column_clause = $model->column_clause(remove => ['id']);
215

            
216
If you add some column, use C<add> option.
217

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

            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-11
220
If you add column name prefix, use C<prefix> option
221

            
222
    my $column_clause = $model->column_clause(prefix => 'book__');
223

            
224
The following clause is created.
225

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

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

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

            
235
=head2 C<delete_all>
236

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

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

            
244
    $table->delete_at(...);
245
    
246
Same as C<delete()> of L<DBIx::Custom> except that
247
you don't have to specify C<table> and C<primary_key> option.
248

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

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
251
    $table->insert(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
252
    
253
Same as C<insert()> 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

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

            
258
    $table->insert_at(...);
259
    
260
Same as C<insert_at()> of L<DBIx::Custom> except that
261
you don't have to specify C<table> and C<primary_key> option.
262

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

            
265
    my $table = DBIx::Custom::Table->new;
266

            
267
Create a L<DBIx::Custom::Table> object.
268

            
269
=head2 C<select>
270

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

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

            
278
    $table->select_at(...);
279
    
280
Same as C<select_at()> of L<DBIx::Custom> except that
281
you don't have to specify C<table> and C<primary_key> option.
282

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

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
285
    $table->update(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
286
    
287
Same as C<update()> 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

            
290
=head2 C<update_all>
291

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

            
297
=head2 C<update_at>
298

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