Alzabo
view release on metacpan or search on metacpan
lib/Alzabo/SQLMaker.pm view on Meta::CPAN
return $self;
}
sub values
{
my $self = shift;
$self->_assert_last_op( qw( into ) );
validate_pos( @_, ( { type => UNDEF | SCALAR | OBJECT } ) x @_ );
if ( ref $_[0] && $_[0]->isa('Alzabo::SQLMaker') )
{
$self->{sql} = $_[0]->sql;
push @{ $self->{bind} }, $_[0]->bind;
}
else
{
my @vals = @_;
Alzabo::Exception::Params->throw
( error => "'values' method expects key/value pairs of column objects and values'" )
if !@vals || @vals % 2;
my %vals = map { ref $_ && $_->can('table') ? $_->name : $_ } @vals;
foreach my $c ( @vals[ map { $_ * 2 } 0 .. int($#vals/2) ] )
{
Alzabo::Exception::SQL->throw
( error => $c->name . " column was not specified in the into method call" )
unless grep { $c eq $_ } @{ $self->{columns} };
}
foreach my $c ( @{ $self->{columns } } )
{
Alzabo::Exception::SQL->throw
( error => $c->name . " was specified in the into method call but no value was provided" )
unless exists $vals{ $c->name };
}
$self->{sql} .= 'VALUES (';
$self->{sql} .=
join ', ', ( map { $self->_bind_val_for_insert( $_, $vals{ $_->name } ) }
@{ $self->{columns} }
);
$self->{sql} .= ')';
}
if ( @{ $self->{placeholders} } && @{ $self->{bind} } )
{
Alzabo::Exception::SQL->throw
( error => "Cannot mix actual bound values and placeholders in call to values()" );
}
$self->{last_op} = 'values';
return $self;
}
use constant UPDATE_SPEC => { can => 'alias_name' };
sub update
{
my $self = shift;
validate_pos( @_, UPDATE_SPEC );
my $table = shift;
$self->{sql} = 'UPDATE ';
$self->{sql} .= ( $self->{quote_identifiers} ?
$self->{driver}->quote_identifier( $table->name ) :
$table->name );
$self->{tables} = { $table => 1 };
$self->{type} = 'update';
$self->{last_op} = 'update';
return $self;
}
sub set
{
my $self = shift;
my @vals = @_;
$self->_assert_last_op('update');
Alzabo::Exception::Params->throw
( error => "'set' method expects key/value pairs of column objects and values'" )
if !@vals || @vals % 2;
validate_pos( @_, ( { can => 'table' },
{ type => UNDEF | SCALAR | OBJECT } ) x (@vals / 2) );
$self->{sql} .= ' SET ';
my @set;
my $table = ( keys %{ $self->{tables} } )[0];
while ( my ($col, $val) = splice @vals, 0, 2 )
{
unless ( $table eq $col->table )
{
my $err = 'Cannot set column (';
$err .= join '.', $col->table->name, $col->name;
$err .= ') unless its table is included in the UPDATE clause';
Alzabo::Exception::SQL->throw( error => $err );
}
push @set,
( $self->{quote_identifiers} ?
$self->{driver}->quote_identifier( $col->name ) :
$col->name ) .
' = ' . $self->_bind_val($val);
}
$self->{sql} .= join ', ', @set;
$self->{last_op} = 'set';
return $self;
( run in 1.313 second using v1.01-cache-2.11-cpan-98e64b0badf )