Newer Older
178 lines | 3.554kb
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

            
6
use base 'Object::Simple';
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(
14
    ['dbi', 'table'],
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
15
    columns => sub { [] },
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
16
    primary_key => sub { [] }
17
);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
18

            
19
our $AUTOLOAD;
20

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

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
24
    # Method name
25
    my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
26

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
27
    # Method
28
    $self->{_methods} ||= {};
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
29
    if (my $method = $self->{_methods}->{$mname}) {
30
        return $self->$method(@_)
31
    }
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
32
    elsif ($self->dbi->can($mname)) {
33
        $self->dbi->$mname(@_);
34
    }
35
    elsif ($self->dbi->dbh->can($mname)) {
36
        $self->dbi->dbh->$mname(@_);
37
    }
38
    else {
39
        croak qq/Can't locate object method "$mname" via "$package"/
40
    }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
41
}
42

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
43
sub method {
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
44
    my $self = shift;
45
    
46
    # Merge
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
47
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
48
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
49
    
50
    return $self;
51
}
52

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
53
sub new {
54
    my $self = shift->SUPER::new(@_);
55
    
many changed
Yuki Kimoto authored on 2011-01-23
56
    # Methods
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
57
    my @methods = qw/insert update update_all delete delete_all select/;
58
    foreach my $method (@methods) {
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
59
        $self->method(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
60
            $method => sub {
61
                my $self = shift;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
62
                return $self->dbi->$method(table => $self->table, @_);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
63
            }
64
        );
65
    }
many changed
Yuki Kimoto authored on 2011-01-23
66
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
67
    return $self;
68
}
69

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
70
sub DESTROY { }
71

            
72
1;
73

            
74
=head1 NAME
75

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
76
DBIx::Custom::Model - Model (experimental)
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
77

            
78
=head1 SYNOPSIS
79

            
80
use DBIx::Custom::Table;
81

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

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

            
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
86
=head2 C<(experimental) columns>
87

            
88
    my $columns = $model->columns;
89
    $model      = $model->columns(['id', 'number']);
90

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
91
=head2 C<dbi>
92

            
93
    my $dbi = $model->dbi;
94
    $model  = $model->dbi($dbi);
95

            
96
L<DBIx::Custom> object.
97

            
98
=head2 C<table>
99

            
100
    my $table = $model->table;
101
    $model    = $model->table('book');
102

            
103
Table name.
104
    
105
=head2 C<primary_key>
106

            
107
    my $primary_key = $model->primary_key;
108
    $model          = $model->primary_key(['id', 'number']);
109

            
110
Foreign key. This is used by C<update_at()>, C<delete_at()>,
111
C<select_at()>.
112

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

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

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

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

            
126
=head2 C<delete_all>
127

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

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
133
=head2 C<method>
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
134

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
135
    $table->method(
136
        count => sub {
137
            my $self = shift;
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
138
        
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
139
            return $self->select(column => 'count(*)', @_)
140
                        ->fetch_first->[0];
141
        }
142
    );
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
143
    
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
144
Add method to a L<DBIx::Custom::Table> object.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
145

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

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

            
153
=head2 C<new>
154

            
155
    my $table = DBIx::Custom::Table->new;
156

            
157
Create a L<DBIx::Custom::Table> object.
158

            
159
=head2 C<select>
160

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

            
166
=head2 C<update>
167

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
168
    $table->update(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
169
    
170
Same as C<update()> 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<update_all>
174

            
175
    $table->update_all(param => \%param);
176
    
177
Same as C<update_all()> of L<DBIx::Custom> except that
178
you don't have to specify table name.