Skip to content

Instantly share code, notes, and snippets.

@nekokak
Created November 10, 2011 02:28
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/1353917 to your computer and use it in GitHub Desktop.
Save nekokak/1353917 to your computer and use it in GitHub Desktop.
fast!
package Teng::Plugin::Retrieve;
use strict;
use warnings;
use utf8;
our @EXPORT = qw/retrieve/;
sub retrieve {
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 = join ' AND', map {"$_ = ?"} keys %$where;
my $sql = sprintf('SELECT %s FROM %s WHERE %s %s',
join(',', @{$table->{columns}}),
$table_name,
$cond,
$opt->{for_update} ? 'FOR UPDATE' : '',
);
my $sth = $self->_execute($sql, [values %$where]);
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,
}
);
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment