Newer Older
172 lines | 3.404kb
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'],
15
    primary_key => sub { [] }
16
);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
17

            
18
our $AUTOLOAD;
19

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

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

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

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

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

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

            
71
1;
72

            
73
=head1 NAME
74

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

            
77
=head1 SYNOPSIS
78

            
79
use DBIx::Custom::Table;
80

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

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

            
85
=head2 C<dbi>
86

            
87
    my $dbi = $model->dbi;
88
    $model  = $model->dbi($dbi);
89

            
90
L<DBIx::Custom> object.
91

            
92
=head2 C<table>
93

            
94
    my $table = $model->table;
95
    $model    = $model->table('book');
96

            
97
Table name.
98
    
99
=head2 C<primary_key>
100

            
101
    my $primary_key = $model->primary_key;
102
    $model          = $model->primary_key(['id', 'number']);
103

            
104
Foreign key. This is used by C<update_at()>, C<delete_at()>,
105
C<select_at()>.
106

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

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

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

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

            
120
=head2 C<delete_all>
121

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

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

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

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

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

            
147
=head2 C<new>
148

            
149
    my $table = DBIx::Custom::Table->new;
150

            
151
Create a L<DBIx::Custom::Table> object.
152

            
153
=head2 C<select>
154

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

            
160
=head2 C<update>
161

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

            
167
=head2 C<update_all>
168

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