Derive mode from DSN function
- Add headers in all files - Minor field name changes
This commit is contained in:
@@ -1,8 +1,16 @@
|
||||
<?php declare(strict_types=1);
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BitBadger\PDODocument;
|
||||
|
||||
use Exception;
|
||||
use PDO;
|
||||
use PhpOption\{None, Option, Some};
|
||||
|
||||
/**
|
||||
* Common configuration for the document library
|
||||
@@ -20,9 +28,6 @@ class Configuration
|
||||
*/
|
||||
public static int $idStringLength = 16;
|
||||
|
||||
/** @var string The data source name (DSN) of the connection string */
|
||||
public static string $pdoDSN = '';
|
||||
|
||||
/** @var string|null The username to use to establish a data connection (use env PDO_DOC_USERNAME if possible) */
|
||||
public static ?string $username = null;
|
||||
|
||||
@@ -32,41 +37,71 @@ class Configuration
|
||||
/** @var array|null Options to use for connections (driver-specific) */
|
||||
public static ?array $options = null;
|
||||
|
||||
/** @var Mode|null The mode in which the library is operating (filled after first connection if not configured) */
|
||||
public static ?Mode $mode = null;
|
||||
/** @var Option<Mode> The mode in which the library is operating */
|
||||
public static Option $_mode;
|
||||
|
||||
/** @var Option<string> The data source name (DSN) of the connection string */
|
||||
private static Option $_pdoDSN;
|
||||
|
||||
/** @var PDO|null The PDO instance to use for database commands */
|
||||
private static ?PDO $_pdo = null;
|
||||
|
||||
/**
|
||||
* Use a Data Source Name (DSN)
|
||||
*
|
||||
* @param string $dsn The data source name to use (driver:[parameters])
|
||||
* @throws DocumentException If a DSN does not start with `pgsql:` or `sqlite:`
|
||||
*/
|
||||
public static function useDSN(string $dsn): void
|
||||
{
|
||||
if (empty($dsn)) {
|
||||
self::$_mode = self::$_pdoDSN = None::create();
|
||||
} else {
|
||||
self::$_mode = Some::create(Mode::deriveFromDSN($dsn));
|
||||
self::$_pdoDSN = Some::create($dsn);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a new connection to the database
|
||||
*
|
||||
* @return PDO A new connection to the SQLite database with foreign key support enabled
|
||||
* @throws DocumentException If this is called before a connection string is set
|
||||
* @throws Exception If this is called before a connection string is set
|
||||
*/
|
||||
public static function dbConn(): PDO
|
||||
{
|
||||
if (is_null(self::$_pdo)) {
|
||||
if (empty(self::$pdoDSN)) {
|
||||
throw new DocumentException('Please provide a data source name (DSN) before attempting data access');
|
||||
}
|
||||
self::$_pdo = new PDO(self::$pdoDSN, $_ENV['PDO_DOC_USERNAME'] ?? self::$username,
|
||||
$dsn = (self::$_pdoDSN ?? None::create())->getOrThrow(
|
||||
new DocumentException('Please provide a data source name (DSN) before attempting data access'));
|
||||
self::$_pdo = new PDO($dsn, $_ENV['PDO_DOC_USERNAME'] ?? self::$username,
|
||||
$_ENV['PDO_DOC_PASSWORD'] ?? self::$password, self::$options);
|
||||
|
||||
if (is_null(self::$mode)) {
|
||||
$driver = self::$_pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
|
||||
self::$mode = match ($driver) {
|
||||
'pgsql' => Mode::PgSQL,
|
||||
'sqlite' => Mode::SQLite,
|
||||
default => throw new DocumentException(
|
||||
"Unsupported driver $driver: this library currently supports PostgreSQL and SQLite")
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return self::$_pdo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the mode for the current database connection
|
||||
*
|
||||
* @return Mode The mode for the current database connection
|
||||
* @throws Exception If the database mode has not been set
|
||||
*/
|
||||
public static function mode(?string $process = null): Mode
|
||||
{
|
||||
return self::$_mode->getOrThrow(
|
||||
new DocumentException('Database mode not set' . (is_null($process) ? '' : "; cannot $process")));
|
||||
}
|
||||
|
||||
/**
|
||||
* You probably don't mean to be calling this; it is here for testing only
|
||||
*
|
||||
* @param Mode|null $mode The mode to set
|
||||
*/
|
||||
public static function overrideMode(?Mode $mode): void
|
||||
{
|
||||
self::$_mode = Option::fromValue($mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the current PDO instance
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user