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'], |
|
15 |
primary_key => sub { [] } |
|
16 |
); |
|
added experimental DBIx::Cus...
|
17 | |
18 |
our $AUTOLOAD; |
|
19 | ||
20 |
sub AUTOLOAD { |
|
21 |
my $self = shift; |
|
22 | ||
renamed experimental DBIx::C...
|
23 |
# Method name |
24 |
my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
added experimental DBIx::Cus...
|
25 | |
renamed experimental DBIx::C...
|
26 |
# Method |
27 |
$self->{_methods} ||= {}; |
|
table object call dbi object...
|
28 |
if (my $method = $self->{_methods}->{$mname}) { |
29 |
return $self->$method(@_) |
|
30 |
} |
|
add feture. all model class ...
|
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...
|
40 |
} |
41 | ||
renamed experimental DBIx::C...
|
42 |
sub method { |
added experimental DBIx::Cus...
|
43 |
my $self = shift; |
44 |
|
|
45 |
# Merge |
|
renamed experimental DBIx::C...
|
46 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
47 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
added experimental DBIx::Cus...
|
48 |
|
49 |
return $self; |
|
50 |
} |
|
51 | ||
added insert, update, update...
|
52 |
sub new { |
53 |
my $self = shift->SUPER::new(@_); |
|
54 |
|
|
many changed
|
55 |
# Methods |
added insert, update, update...
|
56 |
my @methods = qw/insert update update_all delete delete_all select/; |
57 |
foreach my $method (@methods) { |
|
renamed experimental DBIx::C...
|
58 |
$self->method( |
added insert, update, update...
|
59 |
$method => sub { |
60 |
my $self = shift; |
|
add feture. all model class ...
|
61 |
return $self->dbi->$method(table => $self->table, @_); |
added insert, update, update...
|
62 |
} |
63 |
); |
|
64 |
} |
|
many changed
|
65 |
|
added insert, update, update...
|
66 |
return $self; |
67 |
} |
|
68 | ||
added experimental DBIx::Cus...
|
69 |
sub DESTROY { } |
70 | ||
71 |
1; |
|
72 | ||
73 |
=head1 NAME |
|
74 | ||
add DBIx::Custom::Model fore...
|
75 |
DBIx::Custom::Model - Model (experimental) |
added experimental DBIx::Cus...
|
76 | |
77 |
=head1 SYNOPSIS |
|
78 | ||
79 |
use DBIx::Custom::Table; |
|
80 | ||
add feture. all model class ...
|
81 |
my $table = DBIx::Custom::Model->new(table => 'books'); |
added experimental DBIx::Cus...
|
82 | |
add DBIx::Custom::Model fore...
|
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...
|
107 |
=head1 METHODS |
108 | ||
table object call dbi object...
|
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...
|
111 |
and implements the following new ones. |
112 | ||
added insert, update, update...
|
113 |
=head2 C<delete> |
114 | ||
table object call dbi object...
|
115 |
$table->delete(...); |
added insert, update, update...
|
116 |
|
117 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
118 |
you don't have to specify C<table> option. |
added insert, update, update...
|
119 | |
120 |
=head2 C<delete_all> |
|
121 | ||
table object call dbi object...
|
122 |
$table->delete_all(...); |
added insert, update, update...
|
123 |
|
124 |
Same as C<delete_all()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
125 |
you don't have to specify C<table> option. |
added insert, update, update...
|
126 | |
renamed experimental DBIx::C...
|
127 |
=head2 C<method> |
added experimental DBIx::Cus...
|
128 | |
table object call dbi object...
|
129 |
$table->method( |
130 |
count => sub { |
|
131 |
my $self = shift; |
|
simplified DBIx::Custom::Mod...
|
132 |
|
table object call dbi object...
|
133 |
return $self->select(column => 'count(*)', @_) |
134 |
->fetch_first->[0]; |
|
135 |
} |
|
136 |
); |
|
added experimental DBIx::Cus...
|
137 |
|
renamed experimental DBIx::C...
|
138 |
Add method to a L<DBIx::Custom::Table> object. |
added experimental DBIx::Cus...
|
139 | |
added insert, update, update...
|
140 |
=head2 C<insert> |
141 | ||
table object call dbi object...
|
142 |
$table->insert(...); |
added insert, update, update...
|
143 |
|
144 |
Same as C<insert()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
145 |
you don't have to specify C<table> option. |
added insert, update, update...
|
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...
|
155 |
$table->select(...); |
added insert, update, update...
|
156 |
|
157 |
Same as C<select()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
158 |
you don't have to specify C<table> option. |
added insert, update, update...
|
159 | |
160 |
=head2 C<update> |
|
161 | ||
table object call dbi object...
|
162 |
$table->update(...); |
added insert, update, update...
|
163 |
|
164 |
Same as C<update()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
165 |
you don't have to specify C<table> option. |
added insert, update, update...
|
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. |