Add docs link, misc format tweaks

This commit is contained in:
2024-07-21 22:02:16 -04:00
parent d8330d828a
commit 0659de3f99
16 changed files with 49 additions and 48 deletions

View File

@@ -38,13 +38,13 @@ class Configuration
public static ?array $options = null;
/** @var Option<Mode> The mode in which the library is operating */
public static Option $_mode;
public static Option $mode;
/** @var Option<string> The data source name (DSN) of the connection string */
private static Option $_pdoDSN;
private static Option $pdoDSN;
/** @var PDO|null The PDO instance to use for database commands */
private static ?PDO $_pdo = null;
private static ?PDO $pdo = null;
/**
* Use a Data Source Name (DSN)
@@ -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 = None::create();
} else {
self::$_mode = Some::create(Mode::deriveFromDSN($dsn));
self::$_pdoDSN = Some::create($dsn);
self::$mode = Some::create(Mode::deriveFromDSN($dsn));
self::$pdoDSN = Some::create($dsn);
}
}
@@ -70,14 +70,14 @@ class Configuration
*/
public static function dbConn(): PDO
{
if (is_null(self::$_pdo)) {
$dsn = (self::$_pdoDSN ?? None::create())->getOrThrow(
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,
self::$pdo = new PDO($dsn, $_ENV['PDO_DOC_USERNAME'] ?? self::$username,
$_ENV['PDO_DOC_PASSWORD'] ?? self::$password, self::$options);
}
return self::$_pdo;
return self::$pdo;
}
/**
@@ -88,7 +88,7 @@ class Configuration
*/
public static function mode(?string $process = null): Mode
{
return self::$_mode->getOrThrow(
return self::$mode->getOrThrow(
new DocumentException('Database mode not set' . (is_null($process) ? '' : "; cannot $process")));
}
@@ -99,7 +99,7 @@ class Configuration
*/
public static function overrideMode(?Mode $mode): void
{
self::$_mode = Option::fromValue($mode);
self::$mode = Option::fromValue($mode);
}
/**
@@ -107,6 +107,6 @@ class Configuration
*/
public static function resetPDO(): void
{
self::$_pdo = null;
self::$pdo = null;
}
}

View File

@@ -45,7 +45,7 @@ class Custom
is_bool($value) => PDO::PARAM_BOOL,
is_int($value) => PDO::PARAM_INT,
is_null($value) => PDO::PARAM_NULL,
default => PDO::PARAM_STR
default => PDO::PARAM_STR,
};
$stmt->bindValue($key, $value, $dataType);
}

View File

@@ -21,8 +21,8 @@ use PDOStatement;
*/
class DocumentList
{
/** @var TDoc|null $_first The first item from the results */
private mixed $_first = null;
/** @var TDoc|null $first The first item from the results */
private mixed $first = null;
/**
* Constructor
@@ -33,7 +33,7 @@ class DocumentList
private function __construct(private ?PDOStatement &$result, private readonly Mapper $mapper)
{
if ($row = $this->result->fetch(PDO::FETCH_ASSOC)) {
$this->_first = $this->mapper->map($row);
$this->first = $this->mapper->map($row);
} else {
$this->result = null;
}
@@ -62,7 +62,7 @@ class DocumentList
public function items(): Generator
{
if (!$this->result) return;
yield $this->_first;
yield $this->first;
while ($row = $this->result->fetch(PDO::FETCH_ASSOC)) {
yield $this->mapper->map($row);
}

View File

@@ -64,20 +64,20 @@ class Field
$fieldName = (empty($this->qualifier) ? '' : "$this->qualifier.") . 'data' . match (true) {
!str_contains($this->fieldName, '.') => "->>'$this->fieldName'",
$mode === Mode::PgSQL => "#>>'{" . implode(',', explode('.', $this->fieldName)) . "}'",
$mode === Mode::SQLite => "->>'" . implode("'->>'", explode('.', $this->fieldName)) . "'"
$mode === Mode::SQLite => "->>'" . implode("'->>'", explode('.', $this->fieldName)) . "'",
};
$fieldPath = match ($mode) {
Mode::PgSQL => match (true) {
$this->op === Op::BT => is_numeric($this->value[0]) ? "($fieldName)::numeric" : $fieldName,
is_numeric($this->value) => "($fieldName)::numeric",
default => $fieldName
default => $fieldName,
},
default => $fieldName
default => $fieldName,
};
$criteria = match ($this->op) {
Op::EX, Op::NEX => '',
Op::BT => " {$this->paramName}min AND {$this->paramName}max",
default => " $this->paramName"
default => " $this->paramName",
};
return $fieldPath . ' ' . $this->op->toSQL() . $criteria;
}

View File

@@ -28,7 +28,7 @@ enum FieldMatch
{
return match ($this) {
FieldMatch::All => 'AND',
FieldMatch::Any => 'OR'
FieldMatch::Any => 'OR',
};
}
}

View File

@@ -26,7 +26,7 @@ class DocumentMapper implements Mapper
* @param class-string<TDoc> $className The type of class to be returned by this mapping
* @param string $fieldName The name of the field (optional; defaults to `data`)
*/
public function __construct(public string $className, public string $fieldName = 'data') { }
public function __construct(public string $className, public string $fieldName = 'data') {}
/**
* Map a result to a domain class instance

View File

@@ -24,7 +24,7 @@ class ExistsMapper implements Mapper
{
return match (Configuration::mode('map existence result')) {
Mode::PgSQL => (bool)$result[0],
Mode::SQLite => (int)$result[0] > 0
Mode::SQLite => (int)$result[0] > 0,
};
}
}

View File

@@ -30,7 +30,7 @@ class StringMapper implements Mapper
return match (false) {
key_exists($this->fieldName, $result) => null,
is_string($result[$this->fieldName]) => "{$result[$this->fieldName]}",
default => $result[$this->fieldName]
default => $result[$this->fieldName],
};
}
}

View File

@@ -30,7 +30,7 @@ enum Mode
return match (true) {
str_starts_with($dsn, 'pgsql:') => Mode::PgSQL,
str_starts_with($dsn, 'sqlite:') => Mode::SQLite,
default => throw new DocumentException('This library currently supports PostgreSQL and SQLite')
default => throw new DocumentException('This library currently supports PostgreSQL and SQLite'),
};
}
}

View File

@@ -48,7 +48,7 @@ enum Op
Op::NE => "<>",
Op::BT => "BETWEEN",
Op::EX => "IS NOT NULL",
Op::NEX => "IS NULL"
Op::NEX => "IS NULL",
};
}
}

View File

@@ -102,7 +102,7 @@ class Query
AutoId::Number => "json_set(:data, '$.$id', "
. "(SELECT coalesce(max(data->>'$id'), 0) + 1 FROM $tableName))",
AutoId::UUID => "json_set(:data, '$.$id', '" . AutoId::generateUUID() . "')",
AutoId::RandomString => "json_set(:data, '$.$id', '" . AutoId::generateRandom() ."')"
AutoId::RandomString => "json_set(:data, '$.$id', '" . AutoId::generateRandom() ."')",
},
Mode::PgSQL => match ($autoId ?? AutoId::None) {
AutoId::None => ':data',

View File

@@ -27,7 +27,7 @@ class Definition
{
$dataType = match (Configuration::mode('make create table statement')) {
Mode::PgSQL => 'JSONB',
Mode::SQLite => 'TEXT'
Mode::SQLite => 'TEXT',
};
return "CREATE TABLE IF NOT EXISTS $name (data $dataType NOT NULL)";
}
@@ -91,7 +91,7 @@ class Definition
[, $tbl] = self::splitSchemaAndTable($tableName);
$extraOps = match ($indexType) {
DocumentIndex::Full => '',
DocumentIndex::Optimized => ' jsonb_path_ops'
DocumentIndex::Optimized => ' jsonb_path_ops',
};
return "CREATE INDEX IF NOT EXISTS idx_{$tbl}_document ON $tableName USING GIN (data$extraOps)";
}

View File

@@ -28,7 +28,7 @@ class Patch
{
$setValue = match (Configuration::mode('make patch statement')) {
Mode::PgSQL => 'data || :data',
Mode::SQLite => 'json_patch(data, json(:data))'
Mode::SQLite => 'json_patch(data, json(:data))',
};
return "UPDATE $tableName SET data = $setValue WHERE $whereClause";
}

View File

@@ -35,7 +35,7 @@ class RemoveFields
Mode::PgSQL => "UPDATE $tableName SET data = data - " . array_keys($parameters)[0]
. "::text[] WHERE $whereClause",
Mode::SQLite => "UPDATE $tableName SET data = json_remove(data, " . implode(', ', array_keys($parameters))
. ") WHERE $whereClause"
. ") WHERE $whereClause",
};
}