| 1 | package Teng::Plugin::SingleBySQL; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | |||
| 6 | our @EXPORT = qw/single_by_sql/; | ||
| 7 | |||
| 8 | |||
| 9 | sub single_by_sql { | ||
| 10 | my ($self, $sql, $bind, $table_name) = @_; | ||
| 11 | |||
| 12 | $table_name ||= $self->_guess_table_name( $sql ); | ||
| 13 | my $table = $self->{schema}->get_table( $table_name ); | ||
| 14 | Carp::croak("No such table $table_name") unless $table; | ||
| 15 | |||
| 16 | my $sth = $self->_execute($sql, $bind); | ||
| 17 | my $row = $sth->fetchrow_hashref($self->{fields_case}); | ||
| 18 | |||
| 19 | return unless $row; | ||
| 20 | return $row if $self->{suppress_row_objects}; | ||
| 21 | |||
| 22 | $table->{row_class}->new( | ||
| 23 | { | ||
| 24 | sql => $sql, | ||
| 25 | row_data => $row, | ||
| 26 | teng => $self, | ||
| 27 | table => $table, | ||
| 28 | table_name => $table_name, | ||
| 29 | } | ||
| 30 | ); | ||
| 31 | } | ||
| 32 | |||
| 33 | 1; | ||
| 34 | __END__ | ||
| 35 | |||
| 36 | =head1 NAME | ||
| 37 | |||
| 38 | Teng::Plugin::SingleBySQL - single by sql | ||
| 39 | |||
| 40 | =head1 PROVIDED METHODS | ||
| 41 | |||
| 42 | =over 4 | ||
| 43 | |||
| 44 | =item $row = $teng->single_by_sqle($sql, [\%bind_values, [$table_name]]) | ||
| 45 | |||
| 46 | get one record from your SQL. | ||
| 47 | |||
| 48 | my $row = $teng->single_by_sql(q{SELECT id,name FROM user WHERE id = ? LIMIT 1}, [1], 'user'); | ||
| 49 | |||
| 50 | =back | ||
| 51 |