Newer Older
354 lines | 7.592kb
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-15
6
use base 'Object::Simple';
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'],
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
15
    table_alias => sub { {} },
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
16
    columns => sub { [] },
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
17
    filter => sub { [] },
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
18
    join => sub { [] },
19
    primary_key => sub { [] }
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
20
);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
21

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
22
our $AUTOLOAD;
23

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

            
27
    # Method name
28
    my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
29

            
30
    # Method
31
    if (my $dbi_method = $self->dbi->can($mname)) {
32
        $self->dbi->$dbi_method(@_);
33
    }
34
    elsif (my $dbh_method = $self->dbi->dbh->can($mname)) {
35
        $self->dbi->dbh->$dbh_method(@_);
36
    }
37
    else {
38
        croak qq/Can't locate object method "$mname" via "$package"/
39
    }
40
}
41

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

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
67
sub mycolumn {
68
    my ($self, $columns) = @_;
69
    
70
    my $table = $self->table || '';
71
    $columns ||= $self->columns;
72
    
73
    my @column;
74
    push @column, "$table.$_ as $_" for @$columns;
75
    
76
    return join (', ', @column);
77
}
78

            
79
sub column {
80
    my ($self, $table, $columns) = @_;
81
    
82
    $self->{_table_alias} ||= {};
83
    my $dist;
84
    $dist = $self->dbi->{_table_alias}{$table}
85
          ? $self->dbi->{_table_alias}{$table}
86
          : $table;
87
    
88
    $self->dbi->{_model_from} ||= {};
89
    my $model = $self->dbi->{_model_from}->{$dist};
90
    
91
    $columns ||= $self->model($model)->columns;
92
    
93
    my @column;
94
    push @column, "$table.$_ as ${table}__$_" for @$columns;
95
    
96
    return join (', ', @column);
97
}
98

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
99
sub delete {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
100
    my $self = shift;
select method column option ...
Yuki Kimoto authored on 2011-02-22
101
    $self->dbi->delete(table => $self->table, @_);
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
102
}
103

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
104
sub delete_all {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
105
    my $self = shift;
select method column option ...
Yuki Kimoto authored on 2011-02-22
106
    $self->dbi->delete_all(table => $self->table, @_);
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
107
}
108

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
109
sub delete_at {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
110
    my $self = shift;
select method column option ...
Yuki Kimoto authored on 2011-02-22
111
    
112
    return $self->dbi->delete_at(
113
        table => $self->table,
114
        primary_key => $self->primary_key,
115
        @_
116
    );
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
117
}
118

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

            
121
sub insert {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
122
    my $self = shift;
select method column option ...
Yuki Kimoto authored on 2011-02-22
123
    $self->dbi->insert(table => $self->table, @_);
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
124
}
125

            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
126
sub insert_at {
127
    my $self = shift;
128
    
129
    return $self->dbi->insert_at(
130
        table => $self->table,
131
        primary_key => $self->primary_key,
132
        @_
133
    );
134
}
135

            
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
136
sub select {
137
    my $self = shift;
138
    $self->dbi->select(
139
        table => $self->table,
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
140
        join => $self->join,
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
141
        @_
142
    );
143
}
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
144

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

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
156
sub update {
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
157
    my $self = shift;
select method column option ...
Yuki Kimoto authored on 2011-02-22
158
    $self->dbi->update(table => $self->table, @_)
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
159
}
160

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
161
sub update_all {
162
    my $self = shift;
163
    $self->dbi->update_all(table => $self->table, @_);
164
}
165

            
166
sub update_at {
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
167
    my $self = shift;
many changed
Yuki Kimoto authored on 2011-01-23
168
    
select method column option ...
Yuki Kimoto authored on 2011-02-22
169
    return $self->dbi->update_at(
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
170
        table => $self->table,
171
        primary_key => $self->primary_key,
172
        @_
173
    );
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
174
}
175

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
176
1;
177

            
178
=head1 NAME
179

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

            
182
=head1 SYNOPSIS
183

            
184
use DBIx::Custom::Table;
185

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

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

            
190
=head2 C<dbi>
191

            
192
    my $dbi = $model->dbi;
193
    $model  = $model->dbi($dbi);
194

            
195
L<DBIx::Custom> object.
196

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

            
199
    my $dbi = $model->filter
200
    $model  = $model->filter({out => 'tp_to_date', in => 'date_to_tp'});
201

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
202
This filter is applied when L<DBIx::Custom>'s C<include_model()> is called.
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
203

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

            
206
    my $name = $model->name;
207
    $model   = $model->name('book');
208

            
209
Model name.
210

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

            
213
    my $join = $model->join;
214
    $model   = $model->join(
215
        ['left outer join company on book.company_id = company.id']
216
    );
217
    
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
218
Join clause, this is used as C<select()>'s C<join> option.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
219

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

            
222
    my $table = $model->table;
223
    $model    = $model->table('book');
224

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
225
Table name, this is used as C<select()> C<table> option.
226
Generally, this is automatically set from class name.
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
227

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

            
230
    my $primary_key = $model->primary_key;
231
    $model          = $model->primary_key(['id', 'number']);
232

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
233
Foreign key, this is used as C<primary_key> of C<insert_at>,C<update_at()>,
update pod
Yuki Kimoto authored on 2011-02-28
234
C<delete_at()>,C<select_at()>.
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
235

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

            
238
    my $view = $model->view;
239
    $model   = $model->view('select id, DATE(issue_datetime) as date from book');
240

            
241
View. This view is registered by C<view()> of L<DBIx::Custom> when
242
model is included by C<include_model>.
243

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

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

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

            
252
To create column clause automatically, use C<column_clause()>.
253
Valude of C<table> and C<columns> is used.
254

            
255
    my $column_clause = $model->column_clause;
256

            
257
If C<table> is 'book'�AC<column> is ['id', 'name'],
258
the following clause is created.
259

            
260
    book.id as id, book.name as name
261

            
262
These column name is for removing column name ambiguities.
263

            
264
If you remove some columns, use C<remove> option.
265

            
266
    my $column_clause = $model->column_clause(remove => ['id']);
267

            
268
If you add some column, use C<add> option.
269

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

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

            
274
    my $column_clause = $model->column_clause(prefix => 'book__');
275

            
276
The following clause is created.
277

            
278
    book.id as book__id, book.name as book__name
279

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

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

            
287
=head2 C<delete_all>
288

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

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

            
296
    $table->delete_at(...);
297
    
298
Same as C<delete()> of L<DBIx::Custom> except that
299
you don't have to specify C<table> and C<primary_key> option.
300

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

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

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

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

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

            
317
    my $table = DBIx::Custom::Table->new;
318

            
319
Create a L<DBIx::Custom::Table> object.
320

            
321
=head2 C<select>
322

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

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

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

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

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

            
342
=head2 C<update_all>
343

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

            
349
=head2 C<update_at>
350

            
351
    $table->update_at(...);
352
    
353
Same as C<update_at()> of L<DBIx::Custom> except that
354
you don't have to specify C<table> and C<primary_key> option.