Add PDO connection config
This commit is contained in:
@@ -2,31 +2,44 @@
|
||||
|
||||
namespace BitBadger\PDODocument;
|
||||
|
||||
use PDO;
|
||||
|
||||
/**
|
||||
* Common configuration for the document library
|
||||
*/
|
||||
class Configuration
|
||||
{
|
||||
/** @var string The name of the ID field used in the database (will be treated as the primary key) */
|
||||
private static string $_idField = 'id';
|
||||
public static string $idField = 'id';
|
||||
|
||||
/** @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;
|
||||
|
||||
/** @var string|null The password to use to establish a data connection (use env PDO_DOC_PASSWORD if possible) */
|
||||
public static ?string $password = null;
|
||||
|
||||
/** @var array|null Options to use for connections (driver-specific) */
|
||||
public static ?array $options = null;
|
||||
|
||||
/**
|
||||
* Configure the ID field used by the library
|
||||
* Retrieve a new connection to the database
|
||||
*
|
||||
* @param string $name The name of the ID field within each document
|
||||
* @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
|
||||
*/
|
||||
public static function useIdField(string $name): void
|
||||
public static function dbConn(): PDO
|
||||
{
|
||||
self::$_idField = $name;
|
||||
}
|
||||
if (empty(self::$pdoDSN)) {
|
||||
throw new DocumentException('Please provide a data source name (DSN) before attempting data access');
|
||||
}
|
||||
$db = new PDO(self::$pdoDSN, $_ENV['PDO_DOC_USERNAME'] ?? self::$username,
|
||||
$_ENV['PDO_DOC_PASSWORD'] ?? self::$password, self::$options);
|
||||
|
||||
/**
|
||||
* Retrieve the ID field for documents within this library
|
||||
*
|
||||
* @return string The configured ID field
|
||||
*/
|
||||
public static function idField(): string
|
||||
{
|
||||
return self::$_idField;
|
||||
// TODO: determine driver, set mode for other queries
|
||||
echo $db->getAttribute(PDO::ATTR_DRIVER_NAME);
|
||||
return $db;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user