Newer Older
247 lines | 5.451kb
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
42
                 delete delete_at delete_all select select_at/;
43
foreach my $method (@methods) {
44

            
45
    my $code = sub {
46
        my $self = shift;
- fixed bug that model inser...
Yuki Kimoto authored on 2011-06-10
47

            
added tests
Yuki Kimoto authored on 2011-06-08
48
        my @args = (
49
            table => $self->table,
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
50
            bind_type => $self->bind_type,
51
            primary_key => $self->primary_key,
52
            type => $self->type, # DEPRECATED!
added tests
Yuki Kimoto authored on 2011-06-08
53
        );
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
54
        push @args, (join => $self->join) if $method =~ /^select/;
- fixed bug that model inser...
Yuki Kimoto authored on 2011-06-10
55
        unshift @args, shift if @_ % 2;
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
56
        
57
        $self->dbi->$method(@args, @_);
58
    };
59
    
60
    no strict 'refs';
61
    my $class = __PACKAGE__;
62
    *{"${class}::$method"} = $code;
63
}
64

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

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

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

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

            
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
101
# DEPRECATED!
micro optimization
Yuki Kimoto authored on 2011-07-30
102
has 'filter';
cleanup
Yuki Kimoto authored on 2011-06-15
103
has 'name';
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
104
has type => sub { [] };
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
105

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
106
1;
107

            
108
=head1 NAME
109

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

            
112
=head1 SYNOPSIS
113

            
114
use DBIx::Custom::Table;
115

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

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

            
120
=head2 C<dbi>
121

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

            
125
L<DBIx::Custom> object.
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;
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
130
    $model = $model->join(
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
131
        ['left outer join company on book.company_id = company.id']
132
    );
133
    
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
134
Join clause, this value is passed to C<select> method.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
135

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

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

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

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

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

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

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

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

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

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

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

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

            
173
=head2 C<delete_all>
174

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

            
180
=head2 C<insert>
181

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

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

            
189
    $model->method(
190
        update_or_insert => sub {
191
            my $self = shift;
192
            
193
            # ...
194
        },
195
        find_or_create   => sub {
196
            my $self = shift;
197
            
198
            # ...
199
    );
200

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

            
203
    $model->update_or_insert;
204
    $model->find_or_create;
205

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

            
208
    my $column = $self->mycolumn;
209
    my $column = $self->mycolumn(book => ['author', 'title']);
210
    my $column = $self->mycolumn(['author', 'title']);
211

            
212
Create column clause for myself. The follwoing column clause is created.
213

            
214
    book.author as author,
215
    book.title as title
216

            
217
If table name is ommited, C<table> attribute of the model is used.
218
If column names is omitted, C<columns> attribute of the model is used.
219

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

            
222
    my $table = DBIx::Custom::Table->new;
223

            
224
Create a L<DBIx::Custom::Table> object.
225

            
226
=head2 C<select>
227

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

            
233
=head2 C<update>
234

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

            
240
=head2 C<update_all>
241

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

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