Newer Older
275 lines | 6.449kb
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
32
    if (my $dbi_method = $self->dbi->can($mname)) {
33
        $self->dbi->$dbi_method(@_);
34
    }
35
    elsif (my $dbh_method = $self->dbi->dbh->can($mname)) {
36
        $self->dbi->dbh->$dbh_method(@_);
37
    }
38
    else {
39
        croak qq/Can't locate object method "$mname" via "$package"/
40
    }
41
}
42

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
81
sub mycolumn {
82
    my $self = shift;
83
    my $table = shift unless ref $_[0];
84
    my $columns = shift;
85
    
86
    $table ||= $self->table || '';
87
    $columns ||= $self->columns;
88
    
89
    return $self->dbi->mycolumn($table, $columns);
90
}
91

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
92
1;
93

            
94
=head1 NAME
95

            
update pod
Yuki Kimoto authored on 2011-03-13
96
DBIx::Custom::Model - Model EXPERIMENTAL
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
97

            
98
=head1 SYNOPSIS
99

            
100
use DBIx::Custom::Table;
101

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

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

            
106
=head2 C<dbi>
107

            
108
    my $dbi = $model->dbi;
109
    $model  = $model->dbi($dbi);
110

            
111
L<DBIx::Custom> object.
112

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

            
115
    my $dbi = $model->filter
116
    $model  = $model->filter({out => 'tp_to_date', in => 'date_to_tp'});
117

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

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

            
122
    my $name = $model->name;
123
    $model   = $model->name('book');
124

            
125
Model name.
126

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

            
129
    my $join = $model->join;
130
    $model   = $model->join(
131
        ['left outer join company on book.company_id = company.id']
132
    );
133
    
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
134
Join clause, this is used as C<select()>'s C<join> option.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
135

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

            
138
    my $table = $model->table;
139
    $model    = $model->table('book');
140

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

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

            
146
    my $primary_key = $model->primary_key;
147
    $model          = $model->primary_key(['id', 'number']);
148

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

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

            
154
    my $type = $model->type;
155
    $model   = $model->type(['image' => DBI::SQL_BLOB]);
156
    
157
Database data type, this is used as type optioon of C<insert()>, C<insert_at()>,
158
C<update()>, C<update_at()>, C<update_all>, C<delete()>, C<delete_all()>,
159
C<select(), C<select_at()>
160

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

            
163
    my $view = $model->view;
164
    $model   = $model->view('select id, DATE(issue_datetime) as date from book');
165

            
166
View. This view is registered by C<view()> of L<DBIx::Custom> when
167
model is included by C<include_model>.
168

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

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

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

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

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

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

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

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

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

            
194
=head2 C<delete_all>
195

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

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

            
203
    $table->delete_at(...);
204
    
205
Same as C<delete()> of L<DBIx::Custom> except that
206
you don't have to specify C<table> and C<primary_key> option.
207

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

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

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

            
217
    $table->insert_at(...);
218
    
219
Same as C<insert_at()> of L<DBIx::Custom> except that
220
you don't have to specify C<table> and C<primary_key> option.
221

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

            
224
    my $column = $self->mycolumn;
225
    my $column = $self->mycolumn(book => ['author', 'title']);
226
    my $column = $self->mycolumn(['author', 'title']);
227

            
228
Create column clause for myself. The follwoing column clause is created.
229

            
230
    book.author as author,
231
    book.title as title
232

            
233
If table name is ommited, C<table> attribute of the model is used.
234
If column names is omitted, C<columns> attribute of the model is used.
235

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

            
238
    my $table = DBIx::Custom::Table->new;
239

            
240
Create a L<DBIx::Custom::Table> object.
241

            
242
=head2 C<select>
243

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

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

            
251
    $table->select_at(...);
252
    
253
Same as C<select_at()> of L<DBIx::Custom> except that
254
you don't have to specify C<table> and C<primary_key> option.
255

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

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

            
263
=head2 C<update_all>
264

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

            
270
=head2 C<update_at>
271

            
272
    $table->update_at(...);
273
    
274
Same as C<update_at()> of L<DBIx::Custom> except that
275
you don't have to specify C<table> and C<primary_key> option.