Created
November 9, 2011 11:24
-
-
Save nekokak/1351172 to your computer and use it in GitHub Desktop.
Teng#single
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub single { | |
my ($self, $table_name, $where, $opt) = @_; | |
my $table = $self->{schema}->get_table( $table_name ); | |
Carp::croak("No such table $table_name") unless $table; | |
my $cond_s = join ' AND', map {"$_ = ?"} keys %$where; | |
my $cond = $cond_s ? "WHERE $cond_s" : ''; | |
my $binds = [values %$where]; | |
my $sql = sprintf('SELECT %s FROM %s %s %s', | |
join(',', @{$table->{columns}}), | |
$table_name, | |
$cond, | |
$opt->{for_update} ? 'FOR UPDATE' : '', | |
); | |
my $sth = $self->_execute($sql, $binds); | |
my $row = $sth->fetchrow_hashref('NAME_lc'); | |
return unless $row; | |
return $row if $self->{suppress_row_objects}; | |
$table->{row_class}->new( | |
{ | |
sql => $sql, | |
row_data => $row, | |
teng => $self, | |
table => $table, | |
table_name => $table_name, | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment