Newer Older
328 lines | 6.774kb
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 { [] },
18
    relation => 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,
120
        relation => $self->relation,
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,
select method column option ...
Yuki Kimoto authored on 2011-02-22
131
        relation => $self->relation,
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
132
        @_
133
    );
134
}
135

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

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

            
146

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

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

            
159
=head1 NAME
160

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

            
163
=head1 SYNOPSIS
164

            
165
use DBIx::Custom::Table;
166

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

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

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

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

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

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

            
181
L<DBIx::Custom> object.
182

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

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

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

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

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

            
195
Model name.
196

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

            
199
    my $table = $model->table;
200
    $model    = $model->table('book');
201

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

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

            
207
    my $primary_key = $model->primary_key;
208
    $model          = $model->primary_key(['id', 'number']);
209

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

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

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

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

            
221
To create column clause automatically, use C<column_clause()>.
222
Valude of C<table> and C<columns> is used.
223

            
224
    my $column_clause = $model->column_clause;
225

            
226
If C<table> is 'book'�AC<column> is ['id', 'name'],
227
the following clause is created.
228

            
229
    book.id as id, book.name as name
230

            
231
These column name is for removing column name ambiguities.
232

            
233
If you remove some columns, use C<remove> option.
234

            
235
    my $column_clause = $model->column_clause(remove => ['id']);
236

            
237
If you add some column, use C<add> option.
238

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

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

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

            
248
=head2 C<delete_all>
249

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

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

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

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

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

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

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

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

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

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

            
291
    my $table = DBIx::Custom::Table->new;
292

            
293
Create a L<DBIx::Custom::Table> object.
294

            
295
=head2 C<select>
296

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

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

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

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

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

            
316
=head2 C<update_all>
317

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

            
323
=head2 C<update_at>
324

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