ePages 6.10 - DE_EPAGES/Database/API/Connection.pm

Package DE_EPAGES::Database::API::Connection

This package provides an interface to make, get and close connections.

@EXPORT_OK
GetCurrentDBHandle
ConnectToSite
ConnectSite
ConnectTestSite
GetTestSite
DisconnectSite
Connect
RunOnStore
Transaction
EncryptDBPassword

Functions

Connect
ConnectSite
ConnectTestSite
ConnectToSite
DisconnectSite
EncryptDBPassword
GetCurrentDBHandle
GetTestSite
RunOnStore
SetCurrentDBHandle
Transaction

Connect

Connects to any database via the connection string (defined at DBI). Username and password are required. The correct DBI interface will be selected to make connection. Use SetCurrentDBHandle to make the database handle accessible from DAL functions.

Syntax
my $dbh = Connect($DataSource, $Login, $Password, $CacheKey)
Example
my $dbh = Connect('dbi:mysql:admindb', 'admin', 'geheim', 'admindb')
Input
$DataSource (string)
DBI connection string, see "perldoc DBI" for details
$Login (string)
database user name
$Password (string)
database password (optional)
$CacheKey (string)
key to identify the connection
Return
$dbh (object)
database handle

ConnectSite

Connects to any database which is descriped at a section at Config/Database.conf The password is not required if it is defined in the config file. This function sets the current database handle to make the database handle accessable from DAL functions.

Syntax
ConnectSite($Site);
ConnectSite($Site, $Password);
ConnectSite($Site, $Password, $ConnectionName);
Example
ConnectSite('Site');
Input
$Site (string)
(site name) section at database config file starts with that
$Password (string)
database password, unless defined read from ini file (optional)
$ConnectionName (string)
connection name, section at database config file ends with that (optional, default is 'Default')

ConnectTestSite

Connects to the test database (see GetTestSite). The password must be set in the Database.conf file.

Syntax
ConnectTestSite();
Input
$Password (string)
database password

ConnectToSite

Connects to site database (section [Site.Default] at Config/Database.conf). The password is not required if it is set in the config file.

Syntax
ConnectToSite();
ConnectToSite($Password);
Input
$Password (string)
database password (optional)

DisconnectSite

Disconnect current site connection and reset global current database handle site alias.

Syntax
DisconnectSite();

EncryptDBPassword

Crypt the plain password to save it at the Database.conf. Enter the crypted password at the Database.conf. So passwords are hidden for users which have file system access.

Syntax
$EncPassword = EncryptDBPassword($Password);
Input
$Password (string)
database password (ini keyword is passwd)
Return
$EncPassword (string)
crypted database password (ini keyword is encpasswd)

GetCurrentDBHandle

Returns the current database handle.

Syntax
$dbh = GetCurrentDBHandle();
Return
$dbh (object)
database handle

GetTestSite

Returns the connection name of the test database. The name can be specified by the EPAGES_TESTSITE environment variable. The default value is 'Site'.

Syntax
my $StoreName = GetTestSite();
Example
RunOnStore( 'Store'=> GetTestSite(), 'Sub' => sub {
    print LoadRootObject()->get('StoreName');
});
Input
$StoreName (string)
database connection name

RunOnStore

Runs the main function in the context of the Error module and enables detailed error information.

Syntax
RunOnStore( %Options );
Example
RunOnStore(
    'Store'=> $Store,
    'DBPassword'=> $DBPassword,
    'Connection'=> $Connection,
    'Sub' => $Sub,
);
Input
%Options (hash)
parameter
  • Sub - a code reference of the main function - code ref
  • Store - store name - string
  • DBPassword - store password (option default read from Config/Database.conf)- string
  • Connection - connection of store (optional default=Default) - string

SetCurrentDBHandle

Sets the current database handle.

Syntax
SetCurrentDBHandle($dbh);
Input
$dbh (object)
database handle

Transaction

Runs a code block in a transaction using the current database handle. In this transaction is no cache reset involved.

Syntax
Transaction( $cCode, $cRollback);
Example
Transaction( sub {
    ProcessSql( ... );
    ProcessSql( ... );
} );
Input
$cCode (code reference)
code block
$cRollback (code reference)
roll back code block (optional)