add feture. all model class ...
|
1 |
package DBIx::Custom::Model; |
added experimental DBIx::Cus...
|
2 | |
3 |
use strict; |
|
4 |
use warnings; |
|
5 | ||
6 |
use base 'Object::Simple'; |
|
7 | ||
8 |
use Carp 'croak'; |
|
9 | ||
added experimental DBIx::Cus...
|
10 |
# Carp trust relationship |
11 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
|
12 | ||
add DBIx::Custom::Model fore...
|
13 |
__PACKAGE__->attr( |
14 |
['dbi', 'table'], |
|
add DBIx::Custom::Model colu...
|
15 |
columns => sub { [] }, |
add DBIx::Custom::Model fore...
|
16 |
primary_key => sub { [] } |
17 |
); |
|
added experimental DBIx::Cus...
|
18 | |
19 |
our $AUTOLOAD; |
|
20 | ||
21 |
sub AUTOLOAD { |
|
22 |
my $self = shift; |
|
23 | ||
renamed experimental DBIx::C...
|
24 |
# Method name |
25 |
my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
added experimental DBIx::Cus...
|
26 | |
renamed experimental DBIx::C...
|
27 |
# Method |
28 |
$self->{_methods} ||= {}; |
|
table object call dbi object...
|
29 |
if (my $method = $self->{_methods}->{$mname}) { |
30 |
return $self->$method(@_) |
|
31 |
} |
|
add feture. all model class ...
|
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...
|
41 |
} |
42 | ||
renamed experimental DBIx::C...
|
43 |
sub method { |
added experimental DBIx::Cus...
|
44 |
my $self = shift; |
45 |
|
|
46 |
# Merge |
|
renamed experimental DBIx::C...
|
47 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
48 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
added experimental DBIx::Cus...
|
49 |
|
50 |
return $self; |
|
51 |
} |
|
52 | ||
added insert, update, update...
|
53 |
sub new { |
54 |
my $self = shift->SUPER::new(@_); |
|
55 |
|
|
many changed
|
56 |
# Methods |
added insert, update, update...
|
57 |
my @methods = qw/insert update update_all delete delete_all select/; |
58 |
foreach my $method (@methods) { |
|
renamed experimental DBIx::C...
|
59 |
$self->method( |
added insert, update, update...
|
60 |
$method => sub { |
61 |
my $self = shift; |
|
add feture. all model class ...
|
62 |
return $self->dbi->$method(table => $self->table, @_); |
added insert, update, update...
|
63 |
} |
64 |
); |
|
65 |
} |
|
many changed
|
66 |
|
added insert, update, update...
|
67 |
return $self; |
68 |
} |
|
69 | ||
added experimental DBIx::Cus...
|
70 |
sub DESTROY { } |
71 | ||
72 |
1; |
|
73 | ||
74 |
=head1 NAME |
|
75 | ||
add DBIx::Custom::Model fore...
|
76 |
DBIx::Custom::Model - Model (experimental) |
added experimental DBIx::Cus...
|
77 | |
78 |
=head1 SYNOPSIS |
|
79 | ||
80 |
use DBIx::Custom::Table; |
|
81 | ||
add feture. all model class ...
|
82 |
my $table = DBIx::Custom::Model->new(table => 'books'); |
added experimental DBIx::Cus...
|
83 | |
add DBIx::Custom::Model fore...
|
84 |
=head1 ATTRIBUTES |
85 | ||
add DBIx::Custom::Model colu...
|
86 |
=head2 C<(experimental) columns> |
87 | ||
88 |
my $columns = $model->columns; |
|
89 |
$model = $model->columns(['id', 'number']); |
|
90 | ||
add DBIx::Custom::Model fore...
|
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...
|
113 |
=head1 METHODS |
114 | ||
table object call dbi object...
|
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...
|
117 |
and implements the following new ones. |
118 | ||
added insert, update, update...
|
119 |
=head2 C<delete> |
120 | ||
table object call dbi object...
|
121 |
$table->delete(...); |
added insert, update, update...
|
122 |
|
123 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
124 |
you don't have to specify C<table> option. |
added insert, update, update...
|
125 | |
126 |
=head2 C<delete_all> |
|
127 | ||
table object call dbi object...
|
128 |
$table->delete_all(...); |
added insert, update, update...
|
129 |
|
130 |
Same as C<delete_all()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
131 |
you don't have to specify C<table> option. |
added insert, update, update...
|
132 | |
renamed experimental DBIx::C...
|
133 |
=head2 C<method> |
added experimental DBIx::Cus...
|
134 | |
table object call dbi object...
|
135 |
$table->method( |
136 |
count => sub { |
|
137 |
my $self = shift; |
|
simplified DBIx::Custom::Mod...
|
138 |
|
table object call dbi object...
|
139 |
return $self->select(column => 'count(*)', @_) |
140 |
->fetch_first->[0]; |
|
141 |
} |
|
142 |
); |
|
added experimental DBIx::Cus...
|
143 |
|
renamed experimental DBIx::C...
|
144 |
Add method to a L<DBIx::Custom::Table> object. |
added experimental DBIx::Cus...
|
145 | |
added insert, update, update...
|
146 |
=head2 C<insert> |
147 | ||
table object call dbi object...
|
148 |
$table->insert(...); |
added insert, update, update...
|
149 |
|
150 |
Same as C<insert()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
151 |
you don't have to specify C<table> option. |
added insert, update, update...
|
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...
|
161 |
$table->select(...); |
added insert, update, update...
|
162 |
|
163 |
Same as C<select()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
164 |
you don't have to specify C<table> option. |
added insert, update, update...
|
165 | |
166 |
=head2 C<update> |
|
167 | ||
table object call dbi object...
|
168 |
$table->update(...); |
added insert, update, update...
|
169 |
|
170 |
Same as C<update()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
171 |
you don't have to specify C<table> option. |
added insert, update, update...
|
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. |