Permalink
Browse files

DEPRECATED SingleBySQL plugin, move to Teng core.

  • Loading branch information...
1 parent 976036f commit 3b626b4473c06a171ab46232f2a7132a0c5a4996 @nihen nihen committed Aug 15, 2012
Showing with 39 additions and 3 deletions.
  1. +37 −1 lib/Teng.pm
  2. +2 −1 lib/Teng/Plugin/SingleBySQL.pm
  3. +0 −1 t/001_basic/029_single_by_sql.t
View
@@ -542,6 +542,30 @@ sub search_by_sql {
return wantarray ? $itr->all : $itr;
}
+sub single_by_sql {
+ my ($self, $sql, $bind, $table_name) = @_;
+
+ $table_name ||= $self->_guess_table_name( $sql );
+ my $table = $self->{schema}->get_table( $table_name );
+ Carp::croak("No such table $table_name") unless $table;
+
+ my $sth = $self->_execute($sql, $bind);
+ my $row = $sth->fetchrow_hashref($self->{fields_case});
+
+ 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,
+ }
+ );
+}
+
sub _guess_table_name {
my ($class, $sql) = @_;
@@ -628,7 +652,7 @@ in your script.
);
$row->update({name => 'nekokak'});
- $row = $teng->search_by_sql(q{SELECT id, name FROM user WHERE id = ?}, [ 1 ]);
+ $row = $teng->single_by_sql(q{SELECT id, name FROM user WHERE id = ?}, [ 1 ]);
$row->delete();
=head1 ARCHITECTURE
@@ -884,6 +908,18 @@ execute your SQL
If $table is specified, it set table infomation to result iterator.
So, you can use table row class to search_by_sql result.
+=item $row = $teng->single_by_sql($sql, [\@bind_values, [$table_name]])
+
+get one record from your SQL.
+
+ my $row = $teng->single_by_sql(q{SELECT id,name FROM user WHERE id = ? LIMIT 1}, [1], 'user');
+
+This is a shortcut for
+
+ my $row = $teng->search_by_sql(q{SELECT id,name FROM user WHERE id = ? LIMIT 1}, [1], 'user')->next;
+
+But optimized implementation.
+
=item $teng->txn_scope
Creates a new transaction scope guard object.
@@ -5,6 +5,7 @@ use utf8;
our @EXPORT = qw/single_by_sql/;
+warn "IMPORTANT: Teng::Plugin::SingleBySQL is DEPRECATED AND *WILL* BE REMOVED. DO NOT USE.\n";
sub single_by_sql {
my ($self, $sql, $bind, $table_name) = @_;
@@ -35,7 +36,7 @@ __END__
=head1 NAME
-Teng::Plugin::SingleBySQL - single by sql
+Teng::Plugin::SingleBySQL - (DEPRECATED)single by sql
=head1 PROVIDED METHODS
@@ -5,7 +5,6 @@ use Test::More;
my $dbh = t::Utils->setup_dbh;
my $db = Mock::Basic->new({dbh => $dbh});
$db->setup_test_db;
-Mock::Basic->load_plugin('SingleBySQL');
$db->insert('mock_basic',{
id => 1,

0 comments on commit 3b626b4

Please sign in to comment.