Newer Older
288 lines | 6.712kb
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 { [] },
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
19
    type => sub { [] },
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
20
    primary_key => sub { [] }
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
21
);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
22

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

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

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

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

            
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
43
my @methods = qw/insert insert_at update update_at update_all
44
                 delete delete_at delete_all select select_at/;
45
foreach my $method (@methods) {
46

            
47
    my $code = sub {
48
        my $self = shift;
49
        
50
        my @args = (table => $self->table, type => $self->type);
51
        push @args, (primary_key => $self->primary_key) if $method =~ /_at$/;
52
        push @args, (join => $self->join) if $method =~ /^select/;
53
        
54
        $self->dbi->$method(@args, @_);
55
    };
56
    
57
    no strict 'refs';
58
    my $class = __PACKAGE__;
59
    *{"${class}::$method"} = $code;
60
}
61

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
62
sub column {
63
    my ($self, $table, $columns) = @_;
64
    
65
    $self->{_table_alias} ||= {};
66
    my $dist;
67
    $dist = $self->dbi->{_table_alias}{$table}
68
          ? $self->dbi->{_table_alias}{$table}
69
          : $table;
70
    
71
    $self->dbi->{_model_from} ||= {};
72
    my $model = $self->dbi->{_model_from}->{$dist};
73
    
74
    $columns ||= $self->model($model)->columns;
75
    
cleanup
Yuki Kimoto authored on 2011-03-21
76
    return $self->dbi->column($table, $columns);
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
77
}
78

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
81
sub mycolumn {
82
    my $self = shift;
83
    my $table = shift unless ref $_[0];
84
    my $columns = shift;
85
    
86
    $table ||= $self->table || '';
87
    $columns ||= $self->columns;
88
    
89
    return $self->dbi->mycolumn($table, $columns);
90
}
91

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
92
sub new {
93
    my $self = shift->SUPER::new(@_);
94
    
95
    # Check attribute names
96
    my @attrs = keys %$self;
97
    foreach my $attr (@attrs) {
98
        croak qq{"$attr" is invalid attribute name}
99
          unless $self->can($attr);
100
    }
101
    
102
    return $self;
103
}
104

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
105
1;
106

            
107
=head1 NAME
108

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

            
111
=head1 SYNOPSIS
112

            
113
use DBIx::Custom::Table;
114

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

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

            
119
=head2 C<dbi>
120

            
121
    my $dbi = $model->dbi;
122
    $model  = $model->dbi($dbi);
123

            
124
L<DBIx::Custom> object.
125

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

            
128
    my $dbi = $model->filter
129
    $model  = $model->filter({out => 'tp_to_date', in => 'date_to_tp'});
130

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

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

            
135
    my $name = $model->name;
136
    $model   = $model->name('book');
137

            
138
Model name.
139

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

            
142
    my $join = $model->join;
143
    $model   = $model->join(
144
        ['left outer join company on book.company_id = company.id']
145
    );
146
    
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
147
Join clause, this is used as C<select()>'s C<join> option.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
148

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

            
151
    my $table = $model->table;
152
    $model    = $model->table('book');
153

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

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

            
159
    my $primary_key = $model->primary_key;
160
    $model          = $model->primary_key(['id', 'number']);
161

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

            
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
165
=head2 C<type>
166

            
167
    my $type = $model->type;
168
    $model   = $model->type(['image' => DBI::SQL_BLOB]);
169
    
170
Database data type, this is used as type optioon of C<insert()>, C<insert_at()>,
171
C<update()>, C<update_at()>, C<update_all>, C<delete()>, C<delete_all()>,
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
172
C<select()>, C<select_at()>
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
173

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

            
176
    my $view = $model->view;
177
    $model   = $model->view('select id, DATE(issue_datetime) as date from book');
178

            
179
View. This view is registered by C<view()> of L<DBIx::Custom> when
180
model is included by C<include_model>.
181

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

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

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

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

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

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

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

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

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

            
207
=head2 C<delete_all>
208

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

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

            
216
    $table->delete_at(...);
217
    
218
Same as C<delete()> of L<DBIx::Custom> except that
219
you don't have to specify C<table> and C<primary_key> option.
220

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

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

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

            
230
    $table->insert_at(...);
231
    
232
Same as C<insert_at()> of L<DBIx::Custom> except that
233
you don't have to specify C<table> and C<primary_key> option.
234

            
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
235
=head2 C<mycolumn>
cleanup
Yuki Kimoto authored on 2011-03-21
236

            
237
    my $column = $self->mycolumn;
238
    my $column = $self->mycolumn(book => ['author', 'title']);
239
    my $column = $self->mycolumn(['author', 'title']);
240

            
241
Create column clause for myself. The follwoing column clause is created.
242

            
243
    book.author as author,
244
    book.title as title
245

            
246
If table name is ommited, C<table> attribute of the model is used.
247
If column names is omitted, C<columns> attribute of the model is used.
248

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

            
251
    my $table = DBIx::Custom::Table->new;
252

            
253
Create a L<DBIx::Custom::Table> object.
254

            
255
=head2 C<select>
256

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

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

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

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

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

            
276
=head2 C<update_all>
277

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

            
283
=head2 C<update_at>
284

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