/
Row.pm
254 lines (177 loc) · 5.3 KB
/
Row.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package Teng::Row;
use strict;
use warnings;
use Carp ();
our $AUTOLOAD;
sub new {
my ($class, $args) = @_;
my $self = bless {
_get_column_cached => {},
_dirty_columns => {},
%$args,
}, $class;
$self->{select_columns} ||= [keys %{$args->{row_data}}];
$self->{table} ||= $args->{teng}->schema->get_table($args->{table_name});
$self;
}
sub _lazy_get_data {
my ($x, $col) = @_;
return sub {
my $self = shift;
if ($self->{_untrusted_row_data}->{$col}) {
Carp::carp("${col}'s row data is untrusted. by your update query.");
}
my $cache = $self->{_get_column_cached};
my $data = $cache->{$col};
if (! $data) {
$data = $cache->{$col} = $self->{table} ? $self->{table}->call_inflate($col, $self->get_column($col)) : $self->get_column($col);
}
return $data;
};
}
sub handle { $_[0]->{teng} }
sub get_column {
my ($self, $col) = @_;
unless ( $col ) {
Carp::croak('please specify $col for first argument');
}
if ( exists $self->{row_data}->{$col} ) {
return $self->{row_data}->{$col};
} else {
Carp::croak("Specified colum '$col' not found in row (query: " . ( $self->{sql} || 'unknown' ) . ")" );
}
}
sub get_columns {
my $self = shift;
my %data;
for my $col ( @{$self->{select_columns}} ) {
$data{$col} = $self->get_column($col);
}
return \%data;
}
sub set_column {
my ($self, $col, $val) = @_;
if (ref($val) eq 'SCALAR') {
$self->{_untrusted_row_data}->{$col} = 1;
}
$self->{row_data}->{$col} = $val;
delete $self->{_get_column_cached}->{$col};
$self->{_dirty_columns}->{$col} = 1;
}
sub set_columns {
my ($self, $args) = @_;
for my $col (keys %$args) {
$self->set_column($col, $args->{$col});
}
}
sub get_dirty_columns {
my $self = shift;
my %rows = map {$_ => $self->get_column($_)}
keys %{$self->{_dirty_columns}};
return \%rows;
}
sub update {
my ($self, $upd) = @_;
if (ref($self) eq 'Teng::Row') {
Carp::croak q{can't update from basic Teng::Row class.};
}
my $table = $self->{table};
my $table_name = $self->{table_name};
if (! $table) {
Carp::croak( "Table definition for $table_name does not exist (Did you declare it in our schema?)" );
}
%$upd = (%{$self->get_dirty_columns}, %{$upd||+{}});
for my $col (keys %{$upd}) {
$upd->{$col} = $table->call_deflate($col, $upd->{$col});
}
my $where = $self->_where_cond;
$self->set_columns($upd);
$upd = $self->get_dirty_columns;
return 0 unless %$upd;
my $bind_args = $self->{teng}->_bind_sql_type_to_args($table, $upd);
my $result = $self->{teng}->_update($table_name, $bind_args, $where, 1);
$self->{_dirty_columns} = {};
$result;
}
sub delete {
my $self = shift;
if (ref($self) eq 'Teng::Row') {
Carp::croak q{can't delete from basic Teng::Row class.};
}
$self->{teng}->delete($self->{table_name}, $self->_where_cond);
}
sub refetch {
my $self = shift;
$self->{teng}->single($self->{table_name}, $self->_where_cond);
}
sub _where_cond {
my $self = shift;
my $table = $self->{table};
my $table_name = $self->{table_name};
unless ($table) {
Carp::croak("Unknown table: $table_name");
}
# get target table pk
my $pk = $table->primary_keys;
unless ($pk) {
Carp::croak("$table_name has no primary key.");
}
# multi primary keys
if ( ref $pk eq 'ARRAY' ) {
my %pks = map { $_ => 1 } @$pk;
unless ( ( grep { exists $pks{ $_ } } @{$self->{select_columns}} ) == @$pk ) {
Carp::croak "can't get primary columns in your query.";
}
return +{ map { $_ => $self->$_() } @$pk };
} else {
unless (grep { $pk eq $_ } @{$self->{select_columns}}) {
Carp::croak "can't get primary column in your query.";
}
return +{ $pk => $self->$pk };
}
}
sub AUTOLOAD{
my $self = shift;
my($method) = ($AUTOLOAD =~ /([^:']+$)/);
$self->_lazy_get_data($method)->($self);
}
### don't autoload this
sub DESTROY { 1 };
1;
__END__
=head1 NAME
Teng::Row - Teng's Row class
=head1 METHODS
=over
=item $row = Teng::Row->new
create new Teng::Row's instance
=item $row->get_column($column_name)
my $val = $row->get_column($column_name);
get a column value from a row object.
=item $row->get_columns
my $data = $row->get_columns;
Does C<get_column>, for all column values.
=item $row->set_columns(\%new_row_data)
$row->set_columns({$col => $val});
set columns data.
=item $row->set_column($col => $val)
$row->set_column($col => $val);
set column data.
=item $row->get_dirty_columns
returns those that have been changed.
=item $row->update([$arg])
update is executed for instance record.
It works by schema in which primary key exists.
$row->update({name => 'tokuhirom'});
# or
$row->set({name => 'tokuhirom'});
$row->update;
=item $row->delete
delete is executed for instance record.
It works by schema in which primary key exists.
=item my $refetched_row = $row->refetch;
refetch record from database. get new row object.
=item $row->handle
get teng object.
$row->handle->single('table', {id => 1});
=cut