Newer Older
321 lines | 7.452kb
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
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
32
    $self->{_methods} ||= {};
33
    if (my $method = $self->{_methods}->{$mname}) {
34
        return $self->$method(@_)
35
    }
36
    elsif (my $dbi_method = $self->dbi->can($mname)) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
37
        $self->dbi->$dbi_method(@_);
38
    }
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
39
    elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
40
        $self->dbi->dbh->$dbh_method(@_);
41
    }
42
    else {
43
        croak qq/Can't locate object method "$mname" via "$package"/
44
    }
45
}
46

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

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

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

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

            
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
85
sub method {
86
    my $self = shift;
87
    
88
    # Merge
89
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
90
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
91
    
92
    return $self;
93
}
94

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

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
106
sub new {
107
    my $self = shift->SUPER::new(@_);
108
    
109
    # Check attribute names
110
    my @attrs = keys %$self;
111
    foreach my $attr (@attrs) {
112
        croak qq{"$attr" is invalid attribute name}
113
          unless $self->can($attr);
114
    }
115
    
116
    return $self;
117
}
118

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
119
1;
120

            
121
=head1 NAME
122

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

            
125
=head1 SYNOPSIS
126

            
127
use DBIx::Custom::Table;
128

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

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

            
133
=head2 C<dbi>
134

            
135
    my $dbi = $model->dbi;
136
    $model  = $model->dbi($dbi);
137

            
138
L<DBIx::Custom> object.
139

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

            
142
    my $dbi = $model->filter
143
    $model  = $model->filter({out => 'tp_to_date', in => 'date_to_tp'});
144

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

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

            
149
    my $name = $model->name;
150
    $model   = $model->name('book');
151

            
152
Model name.
153

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

            
156
    my $join = $model->join;
157
    $model   = $model->join(
158
        ['left outer join company on book.company_id = company.id']
159
    );
160
    
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
161
Join clause, this is used as C<select()>'s C<join> option.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
162

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

            
165
    my $table = $model->table;
166
    $model    = $model->table('book');
167

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

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

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

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

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

            
181
    my $type = $model->type;
182
    $model   = $model->type(['image' => DBI::SQL_BLOB]);
183
    
184
Database data type, this is used as type optioon of C<insert()>, C<insert_at()>,
185
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
186
C<select()>, C<select_at()>
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
187

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

            
190
    my $view = $model->view;
191
    $model   = $model->view('select id, DATE(issue_datetime) as date from book');
192

            
193
View. This view is registered by C<view()> of L<DBIx::Custom> when
194
model is included by C<include_model>.
195

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

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

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

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

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

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

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

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

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

            
221
=head2 C<delete_all>
222

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

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

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

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

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

            
244
    $table->insert_at(...);
245
    
246
Same as C<insert_at()> of L<DBIx::Custom> except that
247
you don't have to specify C<table> and C<primary_key> option.
248

            
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
249
=head2 C<method> EXPERIMENTAL
250

            
251
    $model->method(
252
        update_or_insert => sub {
253
            my $self = shift;
254
            
255
            # ...
256
        },
257
        find_or_create   => sub {
258
            my $self = shift;
259
            
260
            # ...
261
    );
262

            
263
Register method. These method is called directly from L<DBIx::Custom::Model> object.
264

            
265
    $model->update_or_insert;
266
    $model->find_or_create;
267

            
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
268
=head2 C<mycolumn>
cleanup
Yuki Kimoto authored on 2011-03-21
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.