| OpenResty documentation | Contained in the OpenResty distribution. |
OpenResty::SQL::Insert - SQL generator for insert statements
OpenResty::SQL::Insert
ISA OpenResty::SQL::Statement
use OpenResty::SQL::Insert;
my $insert = OpenResty::SQL::Insert->new;
$insert->insert( 'models' )
->values( 'abc' => '"howdy"' );
print "$insert";
# produces: insert into models values (abc, "howdy");
$insert->cols('foo', 'bar');
print $insert->generate;
# produces: insert into models (foo, bar) values (abc, "howdy");
This class provides an OO interface for generating SQL insert statements without the pain of concatenating plain SQL strings.
new($table)values(@values)col(@column_names)Agent Zhang (agentzh) <agentzh@yahoo.cn>
| OpenResty documentation | Contained in the OpenResty distribution. |
package OpenResty::SQL::Insert; use strict; use warnings; use base 'OpenResty::SQL::Statement'; use overload '""' => sub { $_[0]->generate }; sub new { my $class = ref $_[0] ? ref shift : shift; bless { table => $_[0], values => [], cols => [], }, $class; } sub insert { $_[0]->{table} = $_[1]; $_[0] } sub cols { my $self = shift; push @{ $self->{cols} }, @_; $self; } sub values { my $self = shift; push @{ $self->{values} }, map { defined $_ ? $_ : 'NULL' } @_; $self; } sub generate { my $self = shift; my $sql; local $" = ', '; $sql .= "insert into $self->{table}"; my $cols = $self->{cols}; if ($cols and @$cols) { $sql .= " (@$cols)"; } $sql .= " values (@{ $self->{values} })"; return $sql . ";\n"; } 1; __END__