DB-Object
view release on metacpan or search on metacpan
lib/DB/Object/Query.pm view on Meta::CPAN
# Query string should lie within the object
# _cache_this sends back an object no matter what or undef() if an error occurs
my $sth = $tbl_o->_cache_this( $self );
# STOP! No need to go further
if( !defined( $sth ) )
{
return( $self->error( "Error while preparing query to select on table '", $tbl_o->name, "':\n$query\n", $tbl_o->error() ) );
}
# Routines such as as_string() expect an array on pupose so we do not have to commit the action
# but rather get the statement string. At the end, we write:
# $obj->select() to really select
# $obj->select->as_string() to ONLY get the formatted statement
# wantarray() returns the undefined value in void context, which is typical use of a real select command
# i.e. $obj->select();
# Straight forward declaration: $obj->select(); or $obj->select->execute() || die( $obj->error() );
if( !defined( wantarray() ) )
{
$sth->execute ||
return( $self->error( "Error while executing query to select:\n", $self->as_string(), "\n", $sth->errstr() ) );
}
return( $sth );
}
sub selected_fields { return( shift->_set_get( 'selected_fields', @_ ) ); }
sub sort
{
my $self = shift( @_ );
if( @_ )
{
$self->{reverse} = 0;
}
return( $self->{reverse} );
}
# The fields in their order of appearance in insert and update
# so that following ->exec( $hash ) would be able to allocate the bind values in the right order
sub sorted { return( shift->_set_get_array_as_object( 'sorted', @_ ) ); }
sub table_alias { return( shift->_set_get_scalar( 'table_alias', @_ ) ); }
sub table_object { return( shift->_set_get_object_without_init( 'table_object', 'DB::Object::Tables', @_ ) ); }
sub tie
{
my $self = shift( @_ );
if( @_ )
{
my $ref = '';
$ref = shift( @_ ) if( @_ && @_ % 2 );
my %hash = ( @_ );
$ref ||= \%hash;
$self->{tie} = $ref;
}
return( wantarray() ? %{$self->{tie}} : $self->{tie} );
}
sub unix_timestamp { return( shift->_set_get_array_as_object( 'unix_timestamp', @_ ) ); }
sub update
{
my $self = shift( @_ );
my $data = shift( @_ ) if( @_ == 1 && ref( $_[ 0 ] ) );
my @arg = @_;
if( !@arg && $data )
{
if( $self->_is_hash( $data => 'strict' ) )
{
@arg = %$data;
}
elsif( $self->_is_array( $data ) )
{
@arg = @$data;
}
}
my $constant = $self->constant;
if( scalar( keys( %$constant ) ) )
{
return( $constant->{sth} ) if( $constant->{sth} && $self->_is_object( $constant->{sth} ) && $constant->{sth}->isa( 'DB::Object::Statement' ) );
}
my $tbl_o = $self->table_object || return( $self->error( "No table object is set." ) );
my $table = $tbl_o->name ||
return( $self->error( "No table to update was provided." ) );
if( !scalar( @arg ) )
{
return( $self->error( "No data to update was provided." ) );
}
my $el = $self->elements;
my $elems = $self->format_update( \@arg ) || return( $self->pass_error );
$el->merge( $elems ) || return( $self->pass_error );
my $values = $elems->formats->join( ', ' );
my $clauses = $self->_query_components( 'update' );
my @query = ( "UPDATE $table SET $values" );
push( @query, @$clauses ) if( scalar( @$clauses ) );
my $query = $self->{query} = CORE::join( ' ', @query );
if( !$self->where && !$self->database_object->allow_bulk_update )
{
my( $p, $f, $l ) = caller();
my $call_sub = ( caller(1) )[3];
return( $self->error( "Refusing to do a bulk update. Called from package $p in file $f at line $l from sub $call_sub. Enable the allow_bulk_update database object property if you want to do so. Original query was: $query" ) );
}
$self->query_values( $values );
$self->_save_bind();
my $sth = $tbl_o->_cache_this( $self ) || return( $self->pass_error( $tbl_o->error ) );
# return( $self->error( "Error while preparing query to update table '$table':\n$query" ) );
# $obj->update() to really delete
# $obj->update->as_string() to ONLY get the formatted statement
# wantarray() returns the undefined value in void context, which is typical use of a real update command
# i.e. $obj->update();
if( !defined( wantarray() ) )
{
$sth->execute() ||
return( $self->error( "Error while executing query to update table '$table':\n$query\n", $sth->error ) );
# $sth->finish();
}
# wantarray returns false but not undefined when $obj->update->as_string();
return( $sth );
}
sub where { return( shift->_where_having( 'where', 'where', @_ ) ); }
( run in 1.087 second using v1.01-cache-2.11-cpan-39bf76dae61 )