Initial SQLite development #1

Merged
danieljsummers merged 25 commits from develop into main 2024-06-08 23:58:45 +00:00
4 changed files with 45 additions and 23 deletions
Showing only changes of commit ecc13a30cf - Show all commits

View File

@ -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;
}
}

View File

@ -38,7 +38,7 @@ class Query
*/
public static function whereById(string $paramName = '@id'): string
{
return self::whereByFields([Field::EQ(Configuration::idField(), 0, $paramName)]);
return self::whereByFields([Field::EQ(Configuration::$idField, 0, $paramName)]);
}
/**
@ -61,7 +61,7 @@ class Query
public static function save(string $tableName): string
{
return self::insert($tableName)
. " ON CONFLICT ((data->>'" . Configuration::idField() . "')) DO UPDATE SET data = EXCLUDED.data";
. " ON CONFLICT ((data->>'" . Configuration::$idField . "')) DO UPDATE SET data = EXCLUDED.data";
}
/**

View File

@ -61,6 +61,6 @@ class Definition
*/
public static function ensureKey(string $tableName): string
{
return str_replace('INDEX', 'UNIQUE INDEX', self::ensureIndexOn($tableName, 'key', [Configuration::idField()]));
return str_replace('INDEX', 'UNIQUE INDEX', self::ensureIndexOn($tableName, 'key', [Configuration::$idField]));
}
}

View File

@ -3,6 +3,8 @@
namespace Test\Unit;
use BitBadger\PDODocument\Configuration;
use BitBadger\PDODocument\DocumentException;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
/**
@ -12,17 +14,24 @@ class ConfigurationTest extends TestCase
{
public function testIdFieldDefaultSucceeds(): void
{
$this->assertEquals('id', Configuration::idField(), 'Default ID field should be "id"');
$this->assertEquals('id', Configuration::$idField, 'Default ID field should be "id"');
}
public function testUseIdFieldSucceeds()
public function testIdFieldChangeSucceeds()
{
try {
Configuration::useIdField('EyeDee');
$this->assertEquals('EyeDee', Configuration::idField(), 'ID field should have been updated');
Configuration::$idField = 'EyeDee';
$this->assertEquals('EyeDee', Configuration::$idField, 'ID field should have been updated');
} finally {
Configuration::useIdField('id');
$this->assertEquals('id', Configuration::idField(), 'Default ID value should have been restored');
Configuration::$idField = 'id';
$this->assertEquals('id', Configuration::$idField, 'Default ID value should have been restored');
}
}
#[TestDox("Db conn fails when no DSN specified")]
public function testDbConnFailsWhenNoDSNSpecified(): void
{
$this->expectException(DocumentException::class);
Configuration::dbConn();
}
}