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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
145
=head1 NAME
146

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

            
149
=head1 SYNOPSIS
150

            
151
use DBIx::Custom::Table;
152

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

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

            
157
=head2 C<dbi>
158

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

            
162
L<DBIx::Custom> object.
163

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

            
166
    my $dbi = $model->filter
added DBIx::Custom result_fi...
Yuki Kimoto authored on 2011-06-12
167
    $model  = $model->filter(
168
        title => {out => 'tp_to_date', in => 'date_to_tp'}
169
        author => {out => ..., in => ...},
170
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
171

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

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

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

            
179
Model name.
180

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

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

            
added DBIx::Custom result_fi...
Yuki Kimoto authored on 2011-06-12
190
=head2 C<primary_key>
191

            
192
    my $primary_key = $model->primary_key;
193
    $model          = $model->primary_key(['id', 'number']);
194

            
195
Foreign key, this is used as C<primary_key> of C<insert_at>,C<update_at()>,
196
C<delete_at()>,C<select_at()>.
197

            
198
=head2 C<result_filter>
199

            
200
    my $dbi = $model->result_filter
201
    $model  = $model->result_filter(
202
        title => sub { ... },
203
        author => sub { ... }
204
    );
205

            
206
This filter is applied when L<DBIx::Custom>'s C<include_model()> or C<create_model> is called.
207

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

            
210
    my $table = $model->table;
211
    $model    = $model->table('book');
212

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

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

            
218
    my $table_alias = $model->table_alias;
219
    $model = $model->table_alias(user1 => 'user', user2 => 'user');
220

            
221
Table alias. If you define table alias,
222
same filter as the table is avaliable
223
, and can write $dbi->column('user1') to get all columns.
224

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

            
227
    my $type = $model->type;
228
    $model   = $model->type(['image' => DBI::SQL_BLOB]);
229
    
230
Database data type, this is used as type optioon of C<insert()>, C<insert_at()>,
231
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
232
C<select()>, C<select_at()>
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
233

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

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

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

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

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

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

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

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

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

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

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

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

            
265
Create column clause. The follwoing column clause is created.
266

            
267
    book.author as "book.author",
268
    book.title as "book.title"
269

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

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

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

            
279
=head2 C<delete_all>
280

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

            
286
=head2 C<insert>
287

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

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

            
295
    $model->method(
296
        update_or_insert => sub {
297
            my $self = shift;
298
            
299
            # ...
300
        },
301
        find_or_create   => sub {
302
            my $self = shift;
303
            
304
            # ...
305
    );
306

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

            
309
    $model->update_or_insert;
310
    $model->find_or_create;
311

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

            
314
    my $column = $self->mycolumn;
315
    my $column = $self->mycolumn(book => ['author', 'title']);
316
    my $column = $self->mycolumn(['author', 'title']);
317

            
318
Create column clause for myself. The follwoing column clause is created.
319

            
320
    book.author as author,
321
    book.title as title
322

            
323
If table name is ommited, C<table> attribute of the model is used.
324
If column names is omitted, C<columns> attribute of the model is used.
325

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

            
328
    my $table = DBIx::Custom::Table->new;
329

            
330
Create a L<DBIx::Custom::Table> object.
331

            
332
=head2 C<select>
333

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

            
339
=head2 C<update>
340

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

            
346
=head2 C<update_all>
347

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

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

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

            
360
=head2 C<select_at> DEPRECATED!
361

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

            
367
=head2 C<insert_at> DEPRECATED!
368

            
369
    $table->insert_at(...);
370
    
371
Same as C<insert_at()> of L<DBIx::Custom> except that
372
you don't have to specify C<table> and C<primary_key> option.
373

            
374
=head2 C<delete_at> DEPRECATED!
375

            
376
    $table->delete_at(...);
377
    
378
Same as C<delete()> of L<DBIx::Custom> except that
379
you don't have to specify C<table> and C<primary_key> option.