| OpenResty documentation | Contained in the OpenResty distribution. |
OpenResty::SQL::Update - SQL generator for update statements
OpenResty::SQL::Update
ISA OpenResty::SQL::Statement
use OpenResty::SQL::Update;
my $update = OpenResty::SQL::Update->new;
$update->update( 'models' )
->set( 'abc' => '"howdy"' );
print $update->generate;
# produces:
# update models set abc = "howdy";
$update->where("table_name", '=', _Q('blah'))->set(foo => 'bar');
print "$update";
# produces:
# update models
# set abc = "howdy", foo = bar
# where table_name = 'blah';
$update->where("Foo", '>', 'bar');
print "$update";
# produces:
# update models
# set abc = "howdy", foo = bar
# where table_name = 'blah' and Foo > bar;
$update->reset( qw<abc> )
->set( 'foo' => 3 )->where(name => '"John"');
print "$update";
# produces:
# update abc
# set foo = 3
# where name = "John";
This class provides an OO interface for generating SQL update statements without the pain of concatenating plain SQL strings.
new($table)new()update($table)where($column => $value)reset()reset($table)generateAgent Zhang (agentzh) <agentzh@yahoo.cn>
| OpenResty documentation | Contained in the OpenResty distribution. |
package OpenResty::SQL::Update; use strict; use warnings; use base 'OpenResty::SQL::Statement'; sub new { my $class = ref $_[0] ? ref shift : shift; bless { update => $_[0], set => [], where => [] }, $class; } sub update { $_[0]->{update} = $_[1]; $_[0] } sub set { my $self = shift; push @{ $self->{set} }, "$_[0] = $_[1]"; $self; } sub op { $_[0]->{op} = lc($_[1]); $_[0]; } sub generate { my $self = shift; my $sql; local $" = ', '; $sql .= "update $self->{update} set @{ $self->{set} }"; my @where = @{ $self->{where} }; my $op = $self->{op} || 'and'; my $where = join ' '.$op.' ', map { join(' ', @$_) } @where; if ($where) { $sql .= "\nwhere $where" } return $sql . ";\n"; } 1; __END__