Newer Older
334 lines | 7.882kb
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

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
3
use Object::Simple -base;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
4

            
5
use Carp 'croak';
cleanup
Yuki Kimoto authored on 2011-04-25
6
use DBIx::Custom::Util '_subname';
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
7

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

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
11
has [qw/dbi name table view/],
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
12
    table_alias => sub { {} },
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
13
    columns => sub { [] },
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
14
    filter => sub { [] },
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
15
    join => sub { [] },
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
16
    type => sub { [] },
updatedd pod
Yuki Kimoto authored on 2011-06-12
17
    primary_key => sub { [] };
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
18

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

            
21
sub AUTOLOAD {
22
    my $self = shift;
23

            
24
    # Method name
25
    my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
26

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

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

            
48
    my $code = sub {
49
        my $self = shift;
- fixed bug that model inser...
Yuki Kimoto authored on 2011-06-10
50

            
added tests
Yuki Kimoto authored on 2011-06-08
51
        my @args = (
52
            table => $self->table,
53
            type => $self->type,
54
            primary_key => $self->primary_key
55
        );
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
56
        push @args, (join => $self->join) if $method =~ /^select/;
- fixed bug that model inser...
Yuki Kimoto authored on 2011-06-10
57
        unshift @args, shift if @_ % 2;
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
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) {
cleanup
Yuki Kimoto authored on 2011-04-25
113
        croak qq{"$attr" is invalid attribute name } . _subname
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
114
          unless $self->can($attr);
115
    }
116
    
117
    return $self;
118
}
119

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

            
122
=head1 NAME
123

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

            
126
=head1 SYNOPSIS
127

            
128
use DBIx::Custom::Table;
129

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

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

            
134
=head2 C<dbi>
135

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

            
139
L<DBIx::Custom> object.
140

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

            
143
    my $dbi = $model->filter
added DBIx::Custom result_fi...
Yuki Kimoto authored on 2011-06-12
144
    $model  = $model->filter(
145
        title => {out => 'tp_to_date', in => 'date_to_tp'}
146
        author => {out => ..., in => ...},
147
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
148

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

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

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

            
156
Model name.
157

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

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

            
added DBIx::Custom result_fi...
Yuki Kimoto authored on 2011-06-12
167
=head2 C<primary_key>
168

            
169
    my $primary_key = $model->primary_key;
170
    $model          = $model->primary_key(['id', 'number']);
171

            
172
Foreign key, this is used as C<primary_key> of C<insert_at>,C<update_at()>,
173
C<delete_at()>,C<select_at()>.
174

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

            
177
    my $table = $model->table;
178
    $model    = $model->table('book');
179

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

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

            
185
    my $table_alias = $model->table_alias;
186
    $model = $model->table_alias(user1 => 'user', user2 => 'user');
187

            
188
Table alias. If you define table alias,
189
same filter as the table is avaliable
190
, and can write $dbi->column('user1') to get all columns.
191

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

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

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

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

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

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

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

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

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

            
added EXPERIMENTAL col metho...
Yuki Kimoto authored on 2011-06-08
220
Create column clause. The follwoing column clause is created.
221

            
222
    book.author as "book.author",
223
    book.title as "book.title"
224

            
cleanup
Yuki Kimoto authored on 2011-06-13
225
If Second argument is ommited, all columns set by C<columns> is used.
added EXPERIMENTAL col metho...
Yuki Kimoto authored on 2011-06-08
226

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

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

            
234
=head2 C<delete_all>
235

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

            
241
=head2 C<insert>
242

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

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

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

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

            
264
    $model->update_or_insert;
265
    $model->find_or_create;
266

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

            
269
    my $column = $self->mycolumn;
270
    my $column = $self->mycolumn(book => ['author', 'title']);
271
    my $column = $self->mycolumn(['author', 'title']);
272

            
273
Create column clause for myself. The follwoing column clause is created.
274

            
275
    book.author as author,
276
    book.title as title
277

            
278
If table name is ommited, C<table> attribute of the model is used.
279
If column names is omitted, C<columns> attribute of the model is used.
280

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

            
283
    my $table = DBIx::Custom::Table->new;
284

            
285
Create a L<DBIx::Custom::Table> object.
286

            
287
=head2 C<select>
288

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

            
294
=head2 C<update>
295

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

            
301
=head2 C<update_all>
302

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

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

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

            
315
=head2 C<select_at> DEPRECATED!
316

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

            
322
=head2 C<insert_at> DEPRECATED!
323

            
324
    $table->insert_at(...);
325
    
326
Same as C<insert_at()> of L<DBIx::Custom> except that
327
you don't have to specify C<table> and C<primary_key> option.
328

            
329
=head2 C<delete_at> DEPRECATED!
330

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