Newer Older
365 lines | 8.616kb
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';
cleanup
Yuki Kimoto authored on 2011-04-25
9
use DBIx::Custom::Util '_subname';
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
10

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
11
# Carp trust relationship
12
push @DBIx::Custom::CARP_NOT, __PACKAGE__;
13

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
14
__PACKAGE__->attr(
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
15
    ['dbi', 'name', 'table', 'view'],
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
16
    table_alias => sub { {} },
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
17
    columns => sub { [] },
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
18
    filter => sub { [] },
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
19
    join => sub { [] },
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
20
    type => sub { [] },
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
21
    primary_key => sub { [] }
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
22
);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
23

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

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

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

            
32
    # Method
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
33
    $self->{_methods} ||= {};
34
    if (my $method = $self->{_methods}->{$mname}) {
35
        return $self->$method(@_)
36
    }
37
    elsif (my $dbi_method = $self->dbi->can($mname)) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
38
        $self->dbi->$dbi_method(@_);
39
    }
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
40
    elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
41
        $self->dbi->dbh->$dbh_method(@_);
42
    }
43
    else {
cleanup
Yuki Kimoto authored on 2011-04-25
44
        croak qq{Can't locate object method "$mname" via "$package" }
45
            . _subname;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
46
    }
47
}
48

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

            
53
    my $code = sub {
54
        my $self = shift;
- fixed bug that model inser...
Yuki Kimoto authored on 2011-06-10
55

            
added tests
Yuki Kimoto authored on 2011-06-08
56
        my @args = (
57
            table => $self->table,
58
            type => $self->type,
59
            primary_key => $self->primary_key
60
        );
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
61
        push @args, (join => $self->join) if $method =~ /^select/;
- fixed bug that model inser...
Yuki Kimoto authored on 2011-06-10
62
        unshift @args, shift if @_ % 2;
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
63
        
64
        $self->dbi->$method(@args, @_);
65
    };
66
    
67
    no strict 'refs';
68
    my $class = __PACKAGE__;
69
    *{"${class}::$method"} = $code;
70
}
71

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

            
added EXPERIMENTAL col metho...
Yuki Kimoto authored on 2011-06-08
89
sub col {
90
    my ($self, $table, $columns) = @_;
91
    
92
    $self->{_table_alias} ||= {};
93
    my $dist;
94
    $dist = $self->dbi->{_table_alias}{$table}
95
          ? $self->dbi->{_table_alias}{$table}
96
          : $table;
97
    
98
    $self->dbi->{_model_from} ||= {};
99
    my $model = $self->dbi->{_model_from}->{$dist};
100
    
101
    $columns ||= $self->model($model)->columns;
102
    
103
    return $self->dbi->col($table, $columns);
104
}
105

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

            
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
108
sub method {
109
    my $self = shift;
110
    
111
    # Merge
112
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
113
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
114
    
115
    return $self;
116
}
117

            
cleanup
Yuki Kimoto authored on 2011-03-21
118
sub mycolumn {
119
    my $self = shift;
120
    my $table = shift unless ref $_[0];
121
    my $columns = shift;
122
    
123
    $table ||= $self->table || '';
124
    $columns ||= $self->columns;
125
    
126
    return $self->dbi->mycolumn($table, $columns);
127
}
128

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
129
sub new {
130
    my $self = shift->SUPER::new(@_);
131
    
132
    # Check attribute names
133
    my @attrs = keys %$self;
134
    foreach my $attr (@attrs) {
cleanup
Yuki Kimoto authored on 2011-04-25
135
        croak qq{"$attr" is invalid attribute name } . _subname
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
136
          unless $self->can($attr);
137
    }
138
    
139
    return $self;
140
}
141

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
142
1;
143

            
144
=head1 NAME
145

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
146
DBIx::Custom::Model - Model
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
147

            
148
=head1 SYNOPSIS
149

            
150
use DBIx::Custom::Table;
151

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

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

            
156
=head2 C<dbi>
157

            
158
    my $dbi = $model->dbi;
159
    $model  = $model->dbi($dbi);
160

            
161
L<DBIx::Custom> object.
162

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

            
165
    my $dbi = $model->filter
166
    $model  = $model->filter({out => 'tp_to_date', in => 'date_to_tp'});
167

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

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

            
172
    my $name = $model->name;
173
    $model   = $model->name('book');
174

            
175
Model name.
176

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-04-25
194
=head2 C<table_alias> EXPERIMENTAL
195

            
196
    my $table_alias = $model->table_alias;
197
    $model = $model->table_alias(user1 => 'user', user2 => 'user');
198

            
199
Table alias. If you define table alias,
200
same filter as the table is avaliable
201
, and can write $dbi->column('user1') to get all columns.
202

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

            
205
    my $primary_key = $model->primary_key;
206
    $model          = $model->primary_key(['id', 'number']);
207

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

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

            
213
    my $type = $model->type;
214
    $model   = $model->type(['image' => DBI::SQL_BLOB]);
215
    
216
Database data type, this is used as type optioon of C<insert()>, C<insert_at()>,
217
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
218
C<select()>, C<select_at()>
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
219

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

            
222
    my $view = $model->view;
223
    $model   = $model->view('select id, DATE(issue_datetime) as date from book');
224

            
225
View. This view is registered by C<view()> of L<DBIx::Custom> when
226
model is included by C<include_model>.
227

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

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

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

            
- select() EXPERIMETNAL colu...
Yuki Kimoto authored on 2011-06-08
236
    my $column = $model->column(book => ['author', 'title']);
237
    my $column = $model->column('book');
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
238

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

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

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

            
added EXPERIMENTAL col metho...
Yuki Kimoto authored on 2011-06-08
246
=head2 C<col> EXPERIMETNAL
247

            
- select() EXPERIMETNAL colu...
Yuki Kimoto authored on 2011-06-08
248
    my $column = $model->col(book => ['author', 'title']);
249
    my $column = $model->col('book');
added EXPERIMENTAL col metho...
Yuki Kimoto authored on 2011-06-08
250

            
251
Create column clause. The follwoing column clause is created.
252

            
253
    book.author as "book.author",
254
    book.title as "book.title"
255

            
256
If column names is omitted, C<columns> attribute of the model is used.
257

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

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

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

            
272
=head2 C<insert>
273

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

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
279
=head2 C<method>
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
280

            
281
    $model->method(
282
        update_or_insert => sub {
283
            my $self = shift;
284
            
285
            # ...
286
        },
287
        find_or_create   => sub {
288
            my $self = shift;
289
            
290
            # ...
291
    );
292

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

            
295
    $model->update_or_insert;
296
    $model->find_or_create;
297

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

            
300
    my $column = $self->mycolumn;
301
    my $column = $self->mycolumn(book => ['author', 'title']);
302
    my $column = $self->mycolumn(['author', 'title']);
303

            
304
Create column clause for myself. The follwoing column clause is created.
305

            
306
    book.author as author,
307
    book.title as title
308

            
309
If table name is ommited, C<table> attribute of the model is used.
310
If column names is omitted, C<columns> attribute of the model is used.
311

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

            
314
    my $table = DBIx::Custom::Table->new;
315

            
316
Create a L<DBIx::Custom::Table> object.
317

            
318
=head2 C<select>
319

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

            
325
=head2 C<update>
326

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

            
332
=head2 C<update_all>
333

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

            
- select() EXPERIMETNAL colu...
Yuki Kimoto authored on 2011-06-08
339
=head2 C<update_at> DEPRECATED!
update pod
Yuki Kimoto authored on 2011-02-28
340

            
341
    $table->update_at(...);
342
    
343
Same as C<update_at()> of L<DBIx::Custom> except that
344
you don't have to specify C<table> and C<primary_key> option.
- select() EXPERIMETNAL colu...
Yuki Kimoto authored on 2011-06-08
345

            
346
=head2 C<select_at> DEPRECATED!
347

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

            
353
=head2 C<insert_at> DEPRECATED!
354

            
355
    $table->insert_at(...);
356
    
357
Same as C<insert_at()> of L<DBIx::Custom> except that
358
you don't have to specify C<table> and C<primary_key> option.
359

            
360
=head2 C<delete_at> DEPRECATED!
361

            
362
    $table->delete_at(...);
363
    
364
Same as C<delete()> of L<DBIx::Custom> except that
365
you don't have to specify C<table> and C<primary_key> option.