WIP on new option library
This commit is contained in:
@@ -8,9 +8,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace BitBadger\PDODocument;
|
||||
|
||||
use BitBadger\InspiredByFSharp\Option;
|
||||
use Exception;
|
||||
use PDO;
|
||||
use PhpOption\{None, Option, Some};
|
||||
|
||||
/**
|
||||
* Common configuration for the document library
|
||||
@@ -55,10 +55,10 @@ class Configuration
|
||||
public static function useDSN(string $dsn): void
|
||||
{
|
||||
if (empty($dsn)) {
|
||||
self::$mode = self::$pdoDSN = None::create();
|
||||
self::$mode = self::$pdoDSN = Option::None();
|
||||
} else {
|
||||
self::$mode = Some::create(Mode::deriveFromDSN($dsn));
|
||||
self::$pdoDSN = Some::create($dsn);
|
||||
self::$mode = Option::Some(Mode::deriveFromDSN($dsn));
|
||||
self::$pdoDSN = Option::Some($dsn);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,14 +66,15 @@ class Configuration
|
||||
* Retrieve a new connection to the database
|
||||
*
|
||||
* @return PDO A new connection to the SQLite database with foreign key support enabled
|
||||
* @throws Exception If this is called before a connection string is set
|
||||
* @throws DocumentException If this is called before a connection string is set
|
||||
*/
|
||||
public static function dbConn(): PDO
|
||||
{
|
||||
if (is_null(self::$pdo)) {
|
||||
$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,
|
||||
if (self::$pdoDSN->isNone()) {
|
||||
throw new DocumentException('Please provide a data source name (DSN) before attempting data access');
|
||||
}
|
||||
self::$pdo = new PDO(self::$pdoDSN->get(), $_ENV['PDO_DOC_USERNAME'] ?? self::$username,
|
||||
$_ENV['PDO_DOC_PASSWORD'] ?? self::$password, self::$options);
|
||||
}
|
||||
|
||||
@@ -88,8 +89,10 @@ class Configuration
|
||||
*/
|
||||
public static function mode(?string $process = null): Mode
|
||||
{
|
||||
return self::$mode->getOrThrow(
|
||||
new DocumentException('Database mode not set' . (is_null($process) ? '' : "; cannot $process")));
|
||||
if (self::$mode->isNone()) {
|
||||
throw new DocumentException('Database mode not set' . (is_null($process) ? '' : "; cannot $process"));
|
||||
}
|
||||
return self::$mode->get();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,7 +102,7 @@ class Configuration
|
||||
*/
|
||||
public static function overrideMode(?Mode $mode): void
|
||||
{
|
||||
self::$mode = Option::fromValue($mode);
|
||||
self::$mode = Option::of($mode);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,11 +8,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace BitBadger\PDODocument;
|
||||
|
||||
use BitBadger\InspiredByFSharp\Option;
|
||||
use BitBadger\PDODocument\Mapper\Mapper;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use PDOStatement;
|
||||
use PhpOption\{None, Option, Some};
|
||||
|
||||
/**
|
||||
* Functions to execute custom queries
|
||||
@@ -106,7 +106,7 @@ class Custom
|
||||
{
|
||||
try {
|
||||
$stmt = &self::runQuery("$query LIMIT 1", $parameters);
|
||||
return ($first = $stmt->fetch(PDO::FETCH_ASSOC)) ? Some::create($mapper->map($first)) : None::create();
|
||||
return ($first = $stmt->fetch(PDO::FETCH_ASSOC)) ? Option::Some($mapper->map($first)) : Option::None();
|
||||
} finally {
|
||||
$stmt = null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user