Newer Older
321 lines | 6.889kb
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

            
42
sub column {
43
    my ($self, $table, $columns) = @_;
44
    
45
    $self->{_table_alias} ||= {};
46
    my $dist;
47
    $dist = $self->dbi->{_table_alias}{$table}
48
          ? $self->dbi->{_table_alias}{$table}
49
          : $table;
50
    
51
    $self->dbi->{_model_from} ||= {};
52
    my $model = $self->dbi->{_model_from}->{$dist};
53
    
54
    $columns ||= $self->model($model)->columns;
55
    
cleanup
Yuki Kimoto authored on 2011-03-21
56
    return $self->dbi->column($table, $columns);
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
57
}
58

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

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
64
sub delete_all {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
65
    my $self = shift;
select method column option ...
Yuki Kimoto authored on 2011-02-22
66
    $self->dbi->delete_all(table => $self->table, @_);
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
67
}
68

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
69
sub delete_at {
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
    
72
    return $self->dbi->delete_at(
73
        table => $self->table,
74
        primary_key => $self->primary_key,
75
        @_
76
    );
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 DESTROY { }
80

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

            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
86
sub insert_at {
87
    my $self = shift;
88
    
89
    return $self->dbi->insert_at(
90
        table => $self->table,
91
        primary_key => $self->primary_key,
92
        @_
93
    );
94
}
95

            
cleanup
Yuki Kimoto authored on 2011-03-21
96
sub mycolumn {
97
    my $self = shift;
98
    my $table = shift unless ref $_[0];
99
    my $columns = shift;
100
    
101
    $table ||= $self->table || '';
102
    $columns ||= $self->columns;
103
    
104
    return $self->dbi->mycolumn($table, $columns);
105
}
106

            
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
107
sub select {
108
    my $self = shift;
109
    $self->dbi->select(
110
        table => $self->table,
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
111
        join => $self->join,
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
112
        @_
113
    );
114
}
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
115

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

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

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
132
sub update_all {
133
    my $self = shift;
134
    $self->dbi->update_all(table => $self->table, @_);
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

            
update pod
Yuki Kimoto authored on 2011-03-13
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

            
161
=head2 C<dbi>
162

            
163
    my $dbi = $model->dbi;
164
    $model  = $model->dbi($dbi);
165

            
166
L<DBIx::Custom> object.
167

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

            
170
    my $dbi = $model->filter
171
    $model  = $model->filter({out => 'tp_to_date', in => 'date_to_tp'});
172

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
173
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
174

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

            
177
    my $name = $model->name;
178
    $model   = $model->name('book');
179

            
180
Model name.
181

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

            
184
    my $join = $model->join;
185
    $model   = $model->join(
186
        ['left outer join company on book.company_id = company.id']
187
    );
188
    
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
189
Join clause, this is used as C<select()>'s C<join> option.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
190

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

            
193
    my $table = $model->table;
194
    $model    = $model->table('book');
195

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

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

            
201
    my $primary_key = $model->primary_key;
202
    $model          = $model->primary_key(['id', 'number']);
203

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
204
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
205
C<delete_at()>,C<select_at()>.
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
206

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

            
209
    my $view = $model->view;
210
    $model   = $model->view('select id, DATE(issue_datetime) as date from book');
211

            
212
View. This view is registered by C<view()> of L<DBIx::Custom> when
213
model is included by C<include_model>.
214

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

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
221
=head2 C<column> EXPERIMETNAL
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
222

            
cleanup
Yuki Kimoto authored on 2011-03-21
223
    my $column = $self->column(book => ['author', 'title']);
224
    my $column = $self->column('book');
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
225

            
cleanup
Yuki Kimoto authored on 2011-03-21
226
Create column clause. The follwoing column clause is created.
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
227

            
cleanup
Yuki Kimoto authored on 2011-03-21
228
    book.author as book__author,
229
    book.title as book__title
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
230

            
cleanup
Yuki Kimoto authored on 2011-03-21
231
If column names is omitted, C<columns> attribute of the model is used.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-11
232

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

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

            
240
=head2 C<delete_all>
241

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

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

            
249
    $table->delete_at(...);
250
    
251
Same as C<delete()> of L<DBIx::Custom> except that
252
you don't have to specify C<table> and C<primary_key> option.
253

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
268
=head2 C<mycolumn> EXPERIMENTAL
269

            
270
    my $column = $self->mycolumn;
271
    my $column = $self->mycolumn(book => ['author', 'title']);
272
    my $column = $self->mycolumn(['author', 'title']);
273

            
274
Create column clause for myself. The follwoing column clause is created.
275

            
276
    book.author as author,
277
    book.title as title
278

            
279
If table name is ommited, C<table> attribute of the model is used.
280
If column names is omitted, C<columns> attribute of the model is used.
281

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

            
284
    my $table = DBIx::Custom::Table->new;
285

            
286
Create a L<DBIx::Custom::Table> object.
287

            
288
=head2 C<select>
289

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

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

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

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

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

            
309
=head2 C<update_all>
310

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

            
316
=head2 C<update_at>
317

            
318
    $table->update_at(...);
319
    
320
Same as C<update_at()> of L<DBIx::Custom> except that
321
you don't have to specify C<table> and C<primary_key> option.