Newer Older
275 lines | 6.1kb
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1
package DBIx::Custom::Model;
updatedd pod
Yuki Kimoto authored on 2011-06-12
2
use Object::Simple -base;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
3

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

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

            
cleanup
Yuki Kimoto authored on 2011-06-15
10
has [qw/dbi table/],
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
11
    bind_type => sub { [] },
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
12
    columns => sub { [] },
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
13
    join => sub { [] },
updatedd pod
Yuki Kimoto authored on 2011-06-12
14
    primary_key => sub { [] };
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
15

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

            
18
sub AUTOLOAD {
19
    my $self = shift;
20

            
21
    # Method name
22
    my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
23

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

            
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
41
my @methods = qw/insert insert_at update update_at update_all
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
42
                 delete delete_at delete_all select select_at count/;
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
43
foreach my $method (@methods) {
44

            
45
    my $code = sub {
46
        my $self = shift;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
47
        my $args = [qw/table bind_type primary_key type/];
48
        push @$args, 'join' if $method =~ /^select/;
49
        $self->call_dbi($method, {args => $args}, @_);
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
50
    };
51
    
52
    no strict 'refs';
53
    my $class = __PACKAGE__;
54
    *{"${class}::$method"} = $code;
55
}
56

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
57
sub call_dbi {
58
    my $self = shift;
59
    my $method = shift;
60
    my $options = shift;
61
    my $arg_names = $options->{args};
62
    
63
    my @args;
64
    push @args, ($_ => $self->$_) for @$arg_names;
65
    unshift @args, shift if @_ % 2;
66
    
67
    return $self->dbi->$method(@args, @_);
68
}
69

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

            
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
72
sub method {
73
    my $self = shift;
74
    
75
    # Merge
76
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
77
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
78
    
79
    return $self;
80
}
81

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

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
93
sub new {
94
    my $self = shift->SUPER::new(@_);
95
    
96
    # Check attribute names
97
    my @attrs = keys %$self;
98
    foreach my $attr (@attrs) {
cleanup
Yuki Kimoto authored on 2011-04-25
99
        croak qq{"$attr" is invalid attribute name } . _subname
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
100
          unless $self->can($attr);
101
    }
102
    
103
    return $self;
104
}
105

            
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
106
# DEPRECATED!
micro optimization
Yuki Kimoto authored on 2011-07-30
107
has 'filter';
cleanup
Yuki Kimoto authored on 2011-06-15
108
has 'name';
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
109
has type => sub { [] };
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
110

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
111
1;
112

            
113
=head1 NAME
114

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

            
117
=head1 SYNOPSIS
118

            
119
use DBIx::Custom::Table;
120

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

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

            
125
=head2 C<dbi>
126

            
127
    my $dbi = $model->dbi;
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
128
    $model = $model->dbi($dbi);
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
129

            
130
L<DBIx::Custom> object.
131

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

            
134
    my $join = $model->join;
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
135
    $model = $model->join(
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
136
        ['left outer join company on book.company_id = company.id']
137
    );
138
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
139
Join clause, this value is passed to C<select> method.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
140

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

            
143
    my $primary_key = $model->primary_key;
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
144
    $model = $model->primary_key(['id', 'number']);
added DBIx::Custom result_fi...
Yuki Kimoto authored on 2011-06-12
145

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
146
Primary key,this is passed to C<insert>, C<update>,
147
C<delete>, and C<select> method.
added DBIx::Custom result_fi...
Yuki Kimoto authored on 2011-06-12
148

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

            
151
    my $table = $model->table;
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
152
    $model = $model->table('book');
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
153

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
154
Table name, this is passed to C<select> method.
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
155

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
156
=head2 C<bind_type>
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
157

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
158
    my $type = $model->bind_type;
159
    $model = $model->bind_type(['image' => DBI::SQL_BLOB]);
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
160
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
161
Database data type, this is used as type optioon of C<insert>, 
162
C<update>, C<update_all>, C<delete>, C<delete_all>,
163
C<select>, and C<execute> method
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
164

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

            
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
167
L<DBIx::Custom::Model> inherits all methods from L<Object::Simple>,
168
and you can use all methods of L<DBIx::Custom> and L<DBI>
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
169
and implements the following new ones.
170

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
171
=head2 C<call_dbi> EXPERIMENTAL
172

            
173
    $model->call_dbi('insert',
174
      {args => ['table', 'primary_key' 'bind_type']}, @_)
175

            
176
Call L<DBIx::Custom>(or subclass) method. you can add
177
attribute values of model to arguments by C<args> option.
178

            
179
Generally this method is used when you want to added dbi method to model.
180

            
181
    sub insert {
182
        shift->call_dbi('insert',
183
          {args => ['table', 'primary_key' 'bind_type']}, @_);
184
    }
185

            
186
=head2 C<count> EXPERIMENTAL
187

            
188
    my $count = $model->count;
189

            
190
Get rows count.
191

            
192
Options is same as C<select> method's ones.
193

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

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
196
    $table->delete(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
197
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
198
Same as C<delete> 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

            
201
=head2 C<delete_all>
202

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
203
    $table->delete_all(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
204
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
205
Same as C<delete_all> of L<DBIx::Custom> except that
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
206
you don't have to specify C<table> option.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
207

            
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
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
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

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

            
217
    $model->method(
218
        update_or_insert => sub {
219
            my $self = shift;
220
            
221
            # ...
222
        },
223
        find_or_create   => sub {
224
            my $self = shift;
225
            
226
            # ...
227
    );
228

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

            
231
    $model->update_or_insert;
232
    $model->find_or_create;
233

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

            
236
    my $column = $self->mycolumn;
237
    my $column = $self->mycolumn(book => ['author', 'title']);
238
    my $column = $self->mycolumn(['author', 'title']);
239

            
240
Create column clause for myself. The follwoing column clause is created.
241

            
242
    book.author as author,
243
    book.title as title
244

            
245
If table name is ommited, C<table> attribute of the model is used.
246
If column names is omitted, C<columns> attribute of the model is used.
247

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

            
250
    my $table = DBIx::Custom::Table->new;
251

            
252
Create a L<DBIx::Custom::Table> object.
253

            
254
=head2 C<select>
255

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

            
261
=head2 C<update>
262

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
263
    $table->update(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
264
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
265
Same as C<update> of L<DBIx::Custom> except that
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
266
you don't have to specify C<table> option.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
267

            
268
=head2 C<update_all>
269

            
270
    $table->update_all(param => \%param);
271
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
272
Same as C<update_all> of L<DBIx::Custom> except that
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
273
you don't have to specify table name.
update pod
Yuki Kimoto authored on 2011-02-28
274

            
update pod
Yuki Kimoto authored on 2011-06-15
275
=cut