DBIx-PDlib

 view release on metacpan or  search on metacpan

PDlib.pm  view on Meta::CPAN

	my($select, $from, $where, $other) = @_;

	# make sure connection is still up
	$self->_check_active_connection();

	my $query = "SELECT $select ";
	$query .= "FROM $from " if $from;
	$query .= "WHERE $where " if $where;
	$query .= "$other" if $other;
	
	my $alldata = $self->{_dbh}->selectall_arrayref($query);
	if ($alldata)
	{
		return $alldata
	} else {
		my ($pkg,$file,$line) = caller;
		carp "Unable to execute handle at line $line in file $file package $pkg\n";
		return; # if there was an error, return nothing
	}
}

sub insert 
{
	my ($pkg,$file,$line) = caller;
	####################################################
	####################################################
	##
	## Useful SQL Insert wrapper to cut down on code
	## in our friendly main scripts
	##
	## Usage: insert($tablename,$fields_array_ref,$values_array_ref);

	ref(my $self = shift) or croak "instance variable needed";

	my($table, $fields, $values) = @_;

	# make sure connection is still up
	$self->_check_active_connection();

	return unless ($table &&
	               (ref $fields eq "ARRAY") &&
	               (ref $values eq "ARRAY") &&
	               (@$fields == @$values)
	              );

	my $f_list = join(', ',@$fields);
	my $v_list = join(',', map { "?" } @$values );
	my $handle = $self->{_dbh}->prepare("INSERT INTO $table ($f_list) VALUES ($v_list)");
	if ($handle->execute(@$values))
	{	# will auto-quote stuff this way. pass 'undef' for NULL values
		$handle->finish;
		# return 1 (success) if they want a return value, or just return.
		return defined(wantarray()) ? (1) : "";
	} else {
		# couldn't execute it.
		carp "Unable to execute insert handle at line $line in file $file package $pkg\n";
		return;
	}
}

sub update
{
	my ($pkg,$file,$line) = caller;

	####################################################
	####################################################
	##
	## Useful SQL Update wrapper to cut down on code
	## in our friendly main scripts
	##
	## Usage: update($tablename,$fields_array_ref,$values_array_ref,$where_statement);

	ref(my $self = shift) or croak "instance variable needed";

	my($table, $fields, $values, $where) = @_;

	# make sure connection is still up
	$self->_check_active_connection();

	# they must give us everything. UPDATE's without $where are valid SQL, but
	# I see no reason we should have them called from any script using this
	# sql wrapper. So we make sure they give us some where statement,
	# and they can pass "$where=1" if they really know what they're doing.
	return unless ($table &&
	               (ref($fields) eq "ARRAY") &&
	               (ref($values) eq "ARRAY") &&
	               (@$fields == @$values) &&
	               $where
	              );
	my $query = "UPDATE $table SET " . 
	            join(',', map { " $_ = ?" } @$fields ) .
	            " WHERE $where";

	my $handle = $self->{_dbh}->prepare($query);
	if ($handle->execute(@$values))
	{	# will auto-quote stuff this way.
		$handle->finish;
		# return 1 (success) if they want a return value, or just return.
		return defined(wantarray()) ? (1) : "";
	} else {
		# couldn't execute it.
		carp "Unable to execute update handle at line $line in file $file package $pkg\n";
		return;
	}
}

sub delete
{
	####################################################
	####################################################
	##
	## Useful delete wrapper to cut down on code
	## in our friendly main scripts

	ref(my $self = shift) or croak "instance variable needed";

	my($table,$where) = @_;

	# make sure connection is still up
	$self->_check_active_connection();



( run in 0.586 second using v1.01-cache-2.11-cpan-39bf76dae61 )