Newer Older
364 lines | 8.584kb
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;
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/;
62
        
63
        $self->dbi->$method(@args, @_);
64
    };
65
    
66
    no strict 'refs';
67
    my $class = __PACKAGE__;
68
    *{"${class}::$method"} = $code;
69
}
70

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

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

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

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

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

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

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

            
143
=head1 NAME
144

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

            
147
=head1 SYNOPSIS
148

            
149
use DBIx::Custom::Table;
150

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

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

            
155
=head2 C<dbi>
156

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

            
160
L<DBIx::Custom> object.
161

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

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

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

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

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

            
174
Model name.
175

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
264
=head2 C<delete_all>
265

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

            
271
=head2 C<insert>
272

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

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

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

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

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

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

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

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

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

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

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

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

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

            
317
=head2 C<select>
318

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

            
324
=head2 C<update>
325

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

            
331
=head2 C<update_all>
332

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

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

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

            
345
=head2 C<select_at> DEPRECATED!
346

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

            
352
=head2 C<insert_at> DEPRECATED!
353

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

            
359
=head2 C<delete_at> DEPRECATED!
360

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