Newer Older
332 lines | 7.783kb
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
        
56
        my @args = (table => $self->table, type => $self->type);
57
        push @args, (primary_key => $self->primary_key) if $method =~ /_at$/;
58
        push @args, (join => $self->join) if $method =~ /^select/;
59
        
60
        $self->dbi->$method(@args, @_);
61
    };
62
    
63
    no strict 'refs';
64
    my $class = __PACKAGE__;
65
    *{"${class}::$method"} = $code;
66
}
67

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

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

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

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

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

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
121
1;
122

            
123
=head1 NAME
124

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

            
127
=head1 SYNOPSIS
128

            
129
use DBIx::Custom::Table;
130

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

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

            
135
=head2 C<dbi>
136

            
137
    my $dbi = $model->dbi;
138
    $model  = $model->dbi($dbi);
139

            
140
L<DBIx::Custom> object.
141

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

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

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

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

            
151
    my $name = $model->name;
152
    $model   = $model->name('book');
153

            
154
Model name.
155

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

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

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

            
167
    my $table = $model->table;
168
    $model    = $model->table('book');
169

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

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

            
175
    my $table_alias = $model->table_alias;
176
    $model = $model->table_alias(user1 => 'user', user2 => 'user');
177

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

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

            
184
    my $primary_key = $model->primary_key;
185
    $model          = $model->primary_key(['id', 'number']);
186

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

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

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

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

            
201
    my $view = $model->view;
202
    $model   = $model->view('select id, DATE(issue_datetime) as date from book');
203

            
204
View. This view is registered by C<view()> of L<DBIx::Custom> when
205
model is included by C<include_model>.
206

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

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

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

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

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

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

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

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

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

            
232
=head2 C<delete_all>
233

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

            
update pod
Yuki Kimoto authored on 2011-02-28
239
=head2 C<delete_at>
240

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

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

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

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

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

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

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

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

            
276
    $model->update_or_insert;
277
    $model->find_or_create;
278

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

            
281
    my $column = $self->mycolumn;
282
    my $column = $self->mycolumn(book => ['author', 'title']);
283
    my $column = $self->mycolumn(['author', 'title']);
284

            
285
Create column clause for myself. The follwoing column clause is created.
286

            
287
    book.author as author,
288
    book.title as title
289

            
290
If table name is ommited, C<table> attribute of the model is used.
291
If column names is omitted, C<columns> attribute of the model is used.
292

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

            
295
    my $table = DBIx::Custom::Table->new;
296

            
297
Create a L<DBIx::Custom::Table> object.
298

            
299
=head2 C<select>
300

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

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

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

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

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

            
320
=head2 C<update_all>
321

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

            
327
=head2 C<update_at>
328

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