Skip to content

Instantly share code, notes, and snippets.

@nekokak
Created November 9, 2011 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nekokak/1351172 to your computer and use it in GitHub Desktop.
Save nekokak/1351172 to your computer and use it in GitHub Desktop.
Teng#single
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