Data-Session

 view release on metacpan or  search on metacpan

lib/Data/Session/Driver/Pg.pm  view on Meta::CPAN


} # End of new.

# -----------------------------------------------

sub store
{
	my($self, $id, $data)   = @_;
	my($data_col_name)      = $self -> data_col_name;
	my($dbh)                = $self -> dbh;
	local $$dbh{RaiseError} = 1;
	my($id_col_name)        = $self -> id_col_name;
	my($table_name)         = $self -> table_name;

	# There is a race condition were two clients could run this code concurrently,
	# and both end up trying to insert. That's why we check for "duplicate" below

	try
	{
		my($sql) = "insert into $table_name ($data_col_name, $id_col_name) select ?, ? " .
						"where not exists (select 1 from $table_name where $id_col_name = ? limit 1)";
		my($sth) = $dbh -> prepare($sql);

		$sth -> bind_param(1, $data, {pg_type => $self -> pg_bytea ? PG_BYTEA : PG_TEXT});
		$sth -> bind_param(2, $id);
		$sth -> bind_param(3, $id);

		my($rv);

		try
		{
			$rv = $sth -> execute;

			($rv eq '0E0') && $self -> update($dbh, $table_name, $id_col_name, $data_col_name, $id, $data);
		}
		catch
		{
			if ($_ =~ /duplicate/)
			{
				$self -> update($dbh, $table_name, $id_col_name, $data_col_name, $id, $data);
			}
			else
			{
				die __PACKAGE__ . ". $_";
			}
		};

		$sth -> finish;
	}
	catch
	{
		die __PACKAGE__ . ". $_";
	};

	return 1;

} # End of store.

# -----------------------------------------------

sub update
{
	my($self, $dbh, $table_name, $id_col_name, $data_col_name, $id, $data) = @_;
	my($sql) = "update $table_name set $data_col_name = ? where $id_col_name = ?";
	my($sth) = $dbh -> prepare($sql);

	$sth -> bind_param(1, $data, {pg_type => $self -> pg_bytea ? PG_BYTEA : PG_TEXT});
	$sth -> bind_param(2, $id);

	$sth -> execute;
	$sth -> finish;

	return 1;

} # End of update.

# -----------------------------------------------

1;

=pod

=head1 NAME

L<Data::Session::Driver::Pg> - A persistent session manager

=head1 Synopsis

See L<Data::Session> for details.

=head1 Description

L<Data::Session::Driver::Pg> allows L<Data::Session> to manipulate sessions via L<DBD::Pg>.

To use this module do both of these:

=over 4

=item o Specify a driver of type Pg, as Data::Session -> new(type => 'driver:Pg ...')

=item o Specify a database handle as Data::Session -> new(dbh => $dbh) or a data source as
Data::Session -> new(data_source => $string)

=back

=head1 Case-sensitive Options

See L<Data::Session/Case-sensitive Options> for important information.

=head1 Method: new()

Creates a new object of type L<Data::Session::Driver::Pg>.

C<new()> takes a hash of key/value pairs, some of which might mandatory. Further, some combinations
might be mandatory.

The keys are listed here in alphabetical order.

They are lower-case because they are (also) method names, meaning they can be called to set or get
the value at any time.



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