more session related changes

This commit is contained in:
JT Smith 2006-01-09 19:56:26 +00:00
parent 16b9675b0c
commit 024514c549
106 changed files with 1498 additions and 1313 deletions

View file

@ -18,8 +18,6 @@ package WebGUI::DatabaseLink;
use strict;
use Tie::CPHash;
use WebGUI::International;
use WebGUI::Session;
use WebGUI::SQL;
=head1 NAME
@ -32,13 +30,14 @@ This package contains utility methods for WebGUI's database link system.
=head1 SYNOPSIS
use WebGUI::DatabaseLink;
$hashRef = WebGUI::DatabaseLink::getList();
%databaseLink = WebGUI::DatabaseLink::get($databaseLinkId);
$hashRef = WebGUI::DatabaseLink->getList($session);
$dbLink = WebGUI::DatabaseLink->new($databaseLinkId);
$dbh = $dbLink->dbh;
$dbLink = WebGUI::DatabaseLink->new($session,$databaseLinkId);
$dbh = $dbLink->db;
$dbLink->disconnect;
$session = $dbLink->session;
=head1 METHODS
These subroutines are available from this package:
@ -46,94 +45,170 @@ These subroutines are available from this package:
=cut
#-------------------------------------------------------------------
=head2 getList ( )
Returns a hash reference containing all database links. The format is:
databaseLinkId => title
=head2 copy ( )
Returns a new database link id, after copying the properties of this database link to a new entry.
=cut
sub getList {
my $list = WebGUI::SQL->buildHashRef("select databaseLinkId, title from databaseLink order by title");
$list->{'0'} = WebGUI::International::get(1076);
return $list;
sub copy {
my $self = shift;
my %params = %{$self->{_databaseLink}};
$params{databaseLinkId} = "new";
return $self->session->db->setRow("databaseLink","databaseLinkId",\%params);
}
#-------------------------------------------------------------------
=head2 get ( databaseLinkId )
Returns a hash containing a single database link.
=head2 create ( session, params)
=head3 databaseLinkId
Constructor. Creates a new database link based upon the passed in params and returns a reference to the database link.
A valid databaseLinkId
=head3 session
A reference to the current session.
=head3 params
A hash reference containing the list of params to set. See the set() method for details.
=cut
sub get {
return WebGUI::SQL->quickHash("select * from databaseLink where databaseLinkId=".quote($_[0]));
sub create {
my $class = shift;
my $session = shift;
my $params = shift;
$params->{databaseLinkId} = "new";
my $Id = $session->db->setRow("databaseLink","databaseLinkId",$params);
return $class->new($session,$id);
}
#-------------------------------------------------------------------
=head3 delete ( )
Deletes the current database link.
=cut
sub delete {
my $self = shift;
$self->session->db->deleteRow("databaseLink","databaseLinkId",$self->getId) unless ($self->getId eq "0");
$self->disconnect;
}
#-------------------------------------------------------------------
=head2 disconnect ( )
Disconnect cleanly from the current databaseLink.
Disconnect cleanly from the current databaseLink. You should always use this method rather than the disconnect method of the actual WebGUI::SQL database handle otherwise you may accidentally close the database handle to the WebGUI database prematurely.
=cut
sub disconnect {
my ($class, $value);
$class = shift;
my ($self, $value);
$self = shift;
$value = shift;
if (defined $class->{_dbh}) {
$class->{_dbh}->disconnect() unless ($class->{_databaseLink}{databaseLinkId} eq "0");
if (defined $self>{_dbh}) {
$self->{_dbh}->disconnect() unless ($self->getId eq "0");
}
undef $self;
}
#-------------------------------------------------------------------
=head2 dbh ( )
Return a DBI handle for the current databaseLink, connecting if necessary.
=head2 db ( )
Return a WebGUI::SQL database handle for the current databaseLink, connecting if necessary.
=cut
sub dbh {
my ($class, $value);
my ($dsn, $username, $identifier);
$class = shift;
$value = shift;
if (defined $class->{_dbh}) {
return $class->{_dbh};
sub db {
my $self = shift;
my $value = shift;
if (defined $self->{_dbh}) {
return $self->{_dbh};
}
$dsn = $class->{_databaseLink}{DSN};
$username = $class->{_databaseLink}{username};
$identifier = $class->{_databaseLink}{identifier};
if ($class->{_databaseLinkId} eq "0") {
$class->{_dbh} = $session{dbh};
return $session{dbh};
my $dsn = $self->{_databaseLink}{DSN};
my $username = $self->{_databaseLink}{username};
my $identifier = $self->{_databaseLink}{identifier};
if ($self->getId eq "0") {
$self->{_dbh} = $self->sesssion->db;
return $self->{_dbh};
} elsif ($dsn =~ /\DBI\:\w+\:\w+/i) {
eval{
$class->{_dbh} = DBI->connect($dsn,$username,$identifier);
$self->{_dbh} = WebGUI::SQL->connect($session,$dsn,$username,$identifier);
};
if ($@) {
WebGUI::ErrorHandler::warn("DatabaseLink [".$_[0]."] ".$@);
$self->session->errorHandler->warn("DatabaseLink [".$self->getId."] ".$@);
} else {
return $class->{_dbh};
return $self->{_dbh};
}
} else {
WebGUI::ErrorHandler::warn("DatabaseLink [".$_[0]."] The DSN specified is of an improper format.");
$self->session->errorHandler->warn("DatabaseLink [".$self->getId."] The DSN specified is of an improper format.");
}
return undef;
}
#-------------------------------------------------------------------
=head2 new ( databaseLinkId )
=head2 get ( )
Returns the properties of this database link as a hash reference.
=cut
sub get {
my $self = shift;
$self->{_databaseLink};
}
#-------------------------------------------------------------------
=head2 getId ( )
Returns the ID of this database link.
=cut
sub getId {
my $self = shift;
$self->{_databaseLink}{databaseLinkId};
}
#-------------------------------------------------------------------
=head2 getList ( session )
Class method. Returns a hash reference containing all database links. The format is:
databaseLinkId => title
=head3 session
A reference to the current session.
=cut
sub getList {
my $session = shift;
my $list = $session->db->buildHashRef("select databaseLinkId, title from databaseLink order by title");
$list->{'0'} = WebGUI::International::get(1076);
return $list;
}
#-------------------------------------------------------------------
=head2 new ( session, databaseLinkId )
Constructor.
=head3 session
A reference to the current session.
=head3 databaseLinkId
The databaseLinkId of the databaseLink you're creating an object reference for.
@ -149,17 +224,67 @@ sub new {
if ($databaseLinkId eq "0") {
%databaseLink = (
databaseLinkId=>"0",
DSN=>$session{config}{dsn},
username=>$session{config}{dbuser},
identifier=>$session{config}{dbpass},
DSN=>$self->session->config->get("dsn"),
username=>$self->session->config->get("dbuser"),
identifier=>$self->session->config->get("dbpass"),
title=>"WebGUI Database"
);
} else {
%databaseLink = WebGUI::SQL->quickHash("select * from databaseLink where databaseLinkId=".quote($databaseLinkId));
%databaseLink = $self->session->db->quickHash("select * from databaseLink where databaseLinkId=".$self->session->db->quote($databaseLinkId));
}
}
bless {_databaseLinkId => $databaseLinkId, _databaseLink => \%databaseLink }, $class;
return undef unless $databaseLink{databaseLinkId};
bless {_session=>$session, _databaseLink => \%databaseLink }, $class;
}
#-------------------------------------------------------------------
=head2 session
Returns a reference to the current session.
=cut
sub session {
my $self = shift;
return $self->{_session};
}
#-------------------------------------------------------------------
=head3 set ( params )
Updates the parameters of this database link.
=head3 params
A hash reference containing the parameters to set.
=head4 DSN
The database service name, which follows the perl DBI DSN structure. DBI:dbtype:dbname;otherparams
=head4 username
THe username to connect to the database with.
=head4 identifier
The password to connect to the database with.
=head4 title
A text label to identify this database to humans in the UI.
=cut
sub set {
my $self = shift;
my $params = shift;
$params->{databaseLinkId} = $self->getId;
$self->session->db->setRow("databaseLink","databaseLinkId",$params);
}
1;