Basset

 view release on metacpan or  search on metacpan

lib/Basset/DB/Table.pm  view on Meta::CPAN

$test->is($o->definition($def), $def, "Set definition");
$test->is($o->definition, $def, "Got definition");

my %bindable = map {$_, 1} $o->update_bindables();

$test->is($bindable{'able'}, 1, 'able is update bindable');
$test->is($bindable{'baker'}, 1, 'baker is update bindable');
$test->is($bindable{'charlie'}, 1, 'charlie is update bindable');
$test->is($bindable{'delta'}, 1, 'delta is update bindable');

my $translator = {
	'able' => {
		'U' => {
			'val' => 'lc(?)',
			'binds' => 1
		},
		'A' => {
			'val' => 'NOW()',
			'binds' => 0,
		},
	},
	'baker' => {
		'U' => {
			'val' => 'NOW()',
			'binds' => 0
		},
		'A' => {
			'val' => '?',
			'binds' => 1,
		},
	},
	'charlie' => {
		'U' => {
			'val' => 'NOW()',
			'binds' => 0
		},
	},
	'delta' => {
		'A' => {
			'val' => 'NOW()',
			'binds' => 0
		},
	},
};

$test->is($o->db_write_translation($translator), $translator, 'set db_write_translation');

my %bindable2 = map {$_, 1} $o->update_bindables();

$test->is($bindable2{'able'}, 1, 'able is update bindable');
$test->is($bindable2{'baker'}, undef, 'baker is not update bindable');
$test->is($bindable2{'charlie'}, undef, 'charlie is not update bindable');
$test->is($bindable2{'delta'}, undef, 'delta is not update bindable');

=end btest

=cut

__PACKAGE__->add_attr('_cached_bindables');

sub update_bindables {
	my $self = shift;
	if (my $bindables = $self->_cached_bindables->{'update'}) {
		return @$bindables;
	} else {
        my @excess = $self->primary_cols;
		my @bindables = grep {$self->is_bindable('U', $_)} ($self->update_columns, @excess);
		$self->_cached_bindables->{'update'} = \@bindables;
		return @bindables;
	};
}

=pod

=item delete_bindables

Returns the columns in this table that should be bound with values upon an delete.

 my @deletables = $table->delete_bindables();

=cut

=pod

=begin btest delete_bindables

my $o = __PACKAGE__->new();
$test->ok($o, "Created object");

my $def = {
	'able' => 'SQL_INTEGER',
	'baker' => 'SQL_INTEGER',
	'charlie' => 'SQL_INTEGER',
	'delta' => 'SQL_INTEGER'
};

$test->is($o->definition($def), $def, "Set definition");
$test->is($o->definition, $def, "Got definition");

my %bindable = map {$_, 1} grep {defined} $o->delete_bindables();

$test->is($bindable{'able'}, undef, 'able is not delete bindable');
$test->is($bindable{'baker'}, undef, 'baker is not delete bindable');
$test->is($bindable{'charlie'}, undef, 'charlie is not delete bindable');
$test->is($bindable{'delta'}, undef, 'delta is not delete bindable');

$o->primary_column('able');

my %bindable2 = map {$_, 1} $o->delete_bindables();

$test->is($bindable2{'able'}, 1, 'able is delete bindable');
$test->is($bindable2{'baker'}, undef, 'baker is not delete bindable');
$test->is($bindable2{'charlie'}, undef, 'charlie is not delete bindable');
$test->is($bindable2{'delta'}, undef, 'delta is not delete bindable');

$o->primary_column(['charlie', 'delta']);

my %bindable3 = map {$_, 1} $o->delete_bindables();

$test->is($bindable3{'able'}, undef, 'able is not delete bindable');
$test->is($bindable3{'baker'}, undef, 'baker is not delete bindable');

lib/Basset/DB/Table.pm  view on Meta::CPAN

	my @update = $o->update_columns;

	$test->is($o->update_query, "update mytable set " . join(', ', map {"$_ = ?"} @update), "got default update query");
}

{
	my @update = $o->update_columns(['able']);

	$test->is($o->update_query, "update mytable set " . join(', ', map {"$_ = ?"} @update), "got able update query");

	$o->update_columns(undef);
}

{
	my @update = $o->update_columns(['able', 'baker']);

	$test->is($o->update_query, "update mytable set " . join(', ', map {"$_ = ?"} @update), "got able, baker update query");
	$o->update_columns(undef);
}

{

	$test->is(scalar($o->update_query('foo')), undef, "Could not get update query w/invalid column");
	$test->is($o->errcode, 'BDT-06', 'proper error code');
}

{
	$test->is($o->update_query('able', 'baker'), "update mytable set able = ?, baker = ?", "got able, baker update query");
}

{
	my $translator = 	{
			'able' => {
				'U' => {
					'val' => 'lc(?)',
					'binds' => 0
				},
			},
			'baker' => {
				'A' => {
					'val' => 'uc(?)',
					'binds' => 0,
				},
			},
		};

	$test->is($o->db_write_translation($translator), $translator, "Set translator");

	my @update = $o->update_columns;

	my $q = "update mytable set " . join(', ', map{$_ . ' = ' . ($_ eq 'able' ? 'lc(?)' : $_ eq 'baker' ? 'uc(?)' : '?')} @update);

	$test->is($o->update_query, $q, "got re-written default update query");

}

=end btest

=cut

sub update_query {
	my $self = shift;

	my @cols = @_;

	if (@cols){
		foreach my $col (@cols){
			return $self->error("Cannot update column not in table : $col", "BDT-06")
				unless $self->is_column($col);
		};
	} else {
		@cols = $self->update_columns;
	}

	#my $where = " where " . join(' and ', map {"$_ = ?"} $self->primary_cols);

	my $querykey = join(',', 'update', @cols);

	my $query = $self->_cached_queries->{$querykey} || "update " . $self->name . " set "
		. join(', ', map {$_ . " = " . $self->db_translate_write('U', $_)} @cols)
		;#	. $where;

	$self->_cached_queries->{$querykey} = $query;

	return $query;
};

=pod

=item delete_query

returns a delete query for this table.

 my $delete_query = $table->delete_query

Be warned that no where clause is attached

=cut

=pod

=begin btest delete_query

my $o = __PACKAGE__->new();
$test->ok($o, "Created object");

$test->is($o->name('mytable'), 'mytable', "set tablename");

$test->is($o->delete_query, 'delete from mytable', 'proper delete query');

=end btest

=cut

sub delete_query {
	my $self = shift;

	return "delete from " . $self->name;
};

=pod



( run in 0.946 second using v1.01-cache-2.11-cpan-9581c071862 )