Newer Older
290 lines | 5.912kb
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 { [] },
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
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

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
96
sub method {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
97
    my $self = shift;
select method column option ...
Yuki Kimoto authored on 2011-02-22
98
    
99
    # Merge
100
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
101
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
102
    
103
    return $self;
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
104
}
105

            
106
sub select {
107
    my $self = shift;
108
    $self->dbi->select(
109
        table => $self->table,
110
        relation => $self->relation,
111
        @_
112
    );
113
}
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
114

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

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
126
sub update {
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
127
    my $self = shift;
select method column option ...
Yuki Kimoto authored on 2011-02-22
128
    $self->dbi->update(table => $self->table, @_)
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
129
}
130

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
131
sub update_all {
132
    my $self = shift;
133
    $self->dbi->update_all(table => $self->table, @_);
134
}
135

            
136

            
137
sub update_at {
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
138
    my $self = shift;
many changed
Yuki Kimoto authored on 2011-01-23
139
    
select method column option ...
Yuki Kimoto authored on 2011-02-22
140
    return $self->dbi->update_at(
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
141
        table => $self->table,
142
        primary_key => $self->primary_key,
143
        @_
144
    );
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
145
}
146

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
147
1;
148

            
149
=head1 NAME
150

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

            
153
=head1 SYNOPSIS
154

            
155
use DBIx::Custom::Table;
156

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

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

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

            
163
    my $columns = $model->columns;
164
    $model      = $model->columns(['id', 'number']);
165

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

            
168
    my $dbi = $model->dbi;
169
    $model  = $model->dbi($dbi);
170

            
171
L<DBIx::Custom> object.
172

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

            
175
    my $dbi = $model->filter
176
    $model  = $model->filter({out => 'tp_to_date', in => 'date_to_tp'});
177

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

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

            
182
    my $name = $model->name;
183
    $model   = $model->name('book');
184

            
185
Model name.
186

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

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

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

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

            
197
    my $primary_key = $model->primary_key;
198
    $model          = $model->primary_key(['id', 'number']);
199

            
200
Foreign key. This is used by C<update_at()>, C<delete_at()>,
201
C<select_at()>.
202

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

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

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

            
211
To create column clause automatically, use C<column_clause()>.
212
Valude of C<table> and C<columns> is used.
213

            
214
    my $column_clause = $model->column_clause;
215

            
216
If C<table> is 'book'�AC<column> is ['id', 'name'],
217
the following clause is created.
218

            
219
    book.id as id, book.name as name
220

            
221
These column name is for removing column name ambiguities.
222

            
223
If you remove some columns, use C<remove> option.
224

            
225
    my $column_clause = $model->column_clause(remove => ['id']);
226

            
227
If you add some column, use C<add> option.
228

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

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

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

            
238
=head2 C<delete_all>
239

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

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

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
247
    $table->method(
248
        count => sub {
249
            my $self = shift;
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
250
        
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
251
            return $self->select(column => 'count(*)', @_)
252
                        ->fetch_first->[0];
253
        }
254
    );
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
255
    
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
256
Add method to a L<DBIx::Custom::Table> object.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
257

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

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

            
265
=head2 C<new>
266

            
267
    my $table = DBIx::Custom::Table->new;
268

            
269
Create a L<DBIx::Custom::Table> object.
270

            
271
=head2 C<select>
272

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

            
278
=head2 C<update>
279

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

            
285
=head2 C<update_all>
286

            
287
    $table->update_all(param => \%param);
288
    
289
Same as C<update_all()> of L<DBIx::Custom> except that
290
you don't have to specify table name.