Newer Older
323 lines | 7.525kb
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';
9

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

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

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

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

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

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

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

            
52
    my $code = sub {
53
        my $self = shift;
54
        
55
        my @args = (table => $self->table, type => $self->type);
56
        push @args, (primary_key => $self->primary_key) if $method =~ /_at$/;
57
        push @args, (join => $self->join) if $method =~ /^select/;
58
        
59
        $self->dbi->$method(@args, @_);
60
    };
61
    
62
    no strict 'refs';
63
    my $class = __PACKAGE__;
64
    *{"${class}::$method"} = $code;
65
}
66

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

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

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

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

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
107
sub new {
108
    my $self = shift->SUPER::new(@_);
109
    
110
    # Check attribute names
111
    my @attrs = keys %$self;
112
    foreach my $attr (@attrs) {
113
        croak qq{"$attr" is invalid attribute name}
improved error messages
Yuki Kimoto authored on 2011-04-18
114
            . qq{ (DBIx::Custom::Model::new) }
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

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

            
175
    my $primary_key = $model->primary_key;
176
    $model          = $model->primary_key(['id', 'number']);
177

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

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

            
183
    my $type = $model->type;
184
    $model   = $model->type(['image' => DBI::SQL_BLOB]);
185
    
186
Database data type, this is used as type optioon of C<insert()>, C<insert_at()>,
187
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
188
C<select()>, C<select_at()>
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
189

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

            
192
    my $view = $model->view;
193
    $model   = $model->view('select id, DATE(issue_datetime) as date from book');
194

            
195
View. This view is registered by C<view()> of L<DBIx::Custom> when
196
model is included by C<include_model>.
197

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

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

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

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

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

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

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

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

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

            
223
=head2 C<delete_all>
224

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

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

            
232
    $table->delete_at(...);
233
    
234
Same as C<delete()> of L<DBIx::Custom> except that
235
you don't have to specify C<table> and C<primary_key> option.
236

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

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

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

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

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

            
253
    $model->method(
254
        update_or_insert => sub {
255
            my $self = shift;
256
            
257
            # ...
258
        },
259
        find_or_create   => sub {
260
            my $self = shift;
261
            
262
            # ...
263
    );
264

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

            
267
    $model->update_or_insert;
268
    $model->find_or_create;
269

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

            
272
    my $column = $self->mycolumn;
273
    my $column = $self->mycolumn(book => ['author', 'title']);
274
    my $column = $self->mycolumn(['author', 'title']);
275

            
276
Create column clause for myself. The follwoing column clause is created.
277

            
278
    book.author as author,
279
    book.title as title
280

            
281
If table name is ommited, C<table> attribute of the model is used.
282
If column names is omitted, C<columns> attribute of the model is used.
283

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

            
286
    my $table = DBIx::Custom::Table->new;
287

            
288
Create a L<DBIx::Custom::Table> object.
289

            
290
=head2 C<select>
291

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

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

            
299
    $table->select_at(...);
300
    
301
Same as C<select_at()> of L<DBIx::Custom> except that
302
you don't have to specify C<table> and C<primary_key> option.
303

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

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

            
311
=head2 C<update_all>
312

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

            
318
=head2 C<update_at>
319

            
320
    $table->update_at(...);
321
    
322
Same as C<update_at()> of L<DBIx::Custom> except that
323
you don't have to specify C<table> and C<primary_key> option.