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