Newer Older
335 lines | 7.786kb
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

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

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

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

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

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
124
1;
125

            
126
=head1 NAME
127

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

            
130
=head1 SYNOPSIS
131

            
132
use DBIx::Custom::Table;
133

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

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

            
138
=head2 C<dbi>
139

            
140
    my $dbi = $model->dbi;
141
    $model  = $model->dbi($dbi);
142

            
143
L<DBIx::Custom> object.
144

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

            
147
    my $dbi = $model->filter
148
    $model  = $model->filter({out => 'tp_to_date', in => 'date_to_tp'});
149

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

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

            
154
    my $name = $model->name;
155
    $model   = $model->name('book');
156

            
157
Model name.
158

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

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

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

            
170
    my $table = $model->table;
171
    $model    = $model->table('book');
172

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

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

            
178
    my $table_alias = $model->table_alias;
179
    $model = $model->table_alias(user1 => 'user', user2 => 'user');
180

            
181
Table alias. If you define table alias,
182
same filter as the table is avaliable
183
, and can write $dbi->column('user1') to get all columns.
184

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

            
187
    my $primary_key = $model->primary_key;
188
    $model          = $model->primary_key(['id', 'number']);
189

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

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

            
195
    my $type = $model->type;
196
    $model   = $model->type(['image' => DBI::SQL_BLOB]);
197
    
198
Database data type, this is used as type optioon of C<insert()>, C<insert_at()>,
199
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
200
C<select()>, C<select_at()>
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
201

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

            
204
    my $view = $model->view;
205
    $model   = $model->view('select id, DATE(issue_datetime) as date from book');
206

            
207
View. This view is registered by C<view()> of L<DBIx::Custom> when
208
model is included by C<include_model>.
209

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

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

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

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

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

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

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

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

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

            
235
=head2 C<delete_all>
236

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

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

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

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

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

            
258
    $table->insert_at(...);
259
    
260
Same as C<insert_at()> of L<DBIx::Custom> except that
261
you don't have to specify C<table> and C<primary_key> option.
262

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

            
265
    $model->method(
266
        update_or_insert => sub {
267
            my $self = shift;
268
            
269
            # ...
270
        },
271
        find_or_create   => sub {
272
            my $self = shift;
273
            
274
            # ...
275
    );
276

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

            
279
    $model->update_or_insert;
280
    $model->find_or_create;
281

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

            
284
    my $column = $self->mycolumn;
285
    my $column = $self->mycolumn(book => ['author', 'title']);
286
    my $column = $self->mycolumn(['author', 'title']);
287

            
288
Create column clause for myself. The follwoing column clause is created.
289

            
290
    book.author as author,
291
    book.title as title
292

            
293
If table name is ommited, C<table> attribute of the model is used.
294
If column names is omitted, C<columns> attribute of the model is used.
295

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

            
298
    my $table = DBIx::Custom::Table->new;
299

            
300
Create a L<DBIx::Custom::Table> object.
301

            
302
=head2 C<select>
303

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

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

            
311
    $table->select_at(...);
312
    
313
Same as C<select_at()> of L<DBIx::Custom> except that
314
you don't have to specify C<table> and C<primary_key> option.
315

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

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

            
323
=head2 C<update_all>
324

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

            
330
=head2 C<update_at>
331

            
332
    $table->update_at(...);
333
    
334
Same as C<update_at()> of L<DBIx::Custom> except that
335
you don't have to specify C<table> and C<primary_key> option.