43 lines
1.3 KiB
PHP
43 lines
1.3 KiB
PHP
<?php // phpcs:ignoreFile
|
|
|
|
/**
|
|
* Create a PDO connection to the archive SQL Server instance that hosts dbo.id_names.
|
|
*
|
|
* @throws \RuntimeException When the required PDO sqlsrv driver is unavailable.
|
|
*/
|
|
function archive_db_connect(): \PDO
|
|
{
|
|
if (!class_exists(\PDO::class)) {
|
|
throw new \RuntimeException('PDO is required to connect to the archive database.');
|
|
}
|
|
|
|
if (!in_array('sqlsrv', \PDO::getAvailableDrivers(), true)) {
|
|
throw new \RuntimeException('The sqlsrv PDO driver is not available. Enable it to query the archive database.');
|
|
}
|
|
|
|
static $cachedConnection = null;
|
|
|
|
if ($cachedConnection instanceof \PDO) {
|
|
return $cachedConnection;
|
|
}
|
|
|
|
$server = getenv('ARCHIVE_DB_SERVER') ?: '192.168.0.13\SQLEXPRESS';
|
|
$database = getenv('ARCHIVE_DB_NAME') ?: 'history';
|
|
$username = getenv('ARCHIVE_DB_USER') ?: 'opce';
|
|
$password = getenv('ARCHIVE_DB_PASSWORD') ?: 'opcelasuca';
|
|
|
|
$dsn = sprintf('sqlsrv:Server=%s;Database=%s;LoginTimeout=5', $server, $database);
|
|
|
|
$cachedConnection = new \PDO(
|
|
$dsn,
|
|
$username,
|
|
$password,
|
|
[
|
|
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
|
|
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
|
|
]
|
|
);
|
|
|
|
return $cachedConnection;
|
|
}
|