Add mode, bring in definition/patch queries

This commit is contained in:
2024-06-03 21:09:03 -04:00
parent ecc13a30cf
commit 98bfceb7c9
10 changed files with 219 additions and 7 deletions

View File

@@ -24,6 +24,9 @@ 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;
/**
* Retrieve a new connection to the database
*
@@ -38,8 +41,15 @@ class Configuration
$db = new PDO(self::$pdoDSN, $_ENV['PDO_DOC_USERNAME'] ?? self::$username,
$_ENV['PDO_DOC_PASSWORD'] ?? self::$password, self::$options);
// TODO: determine driver, set mode for other queries
echo $db->getAttribute(PDO::ATTR_DRIVER_NAME);
if (is_null(self::$mode)) {
$driver = $db->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 $db;
}
}

15
src/Mode.php Normal file
View File

@@ -0,0 +1,15 @@
<?php declare(strict_types=1);
namespace BitBadger\PDODocument;
/**
* The mode for queries generated by the library
*/
enum Mode
{
/** Storing documents in a PostgreSQL database */
case PgSQL;
/** Storing documents in a SQLite database */
case SQLite;
}

View File

@@ -3,6 +3,8 @@
namespace BitBadger\PDODocument\Query;
use BitBadger\PDODocument\Configuration;
use BitBadger\PDODocument\DocumentException;
use BitBadger\PDODocument\Mode;
/**
* Queries to define tables and indexes
@@ -13,11 +15,16 @@ class Definition
* SQL statement to create a document table
*
* @param string $name The name of the table (including schema, if applicable)
* @param string $dataType The data type used for the document column
* @return string The CREATE TABLE statement for the document table
* @throws DocumentException If the database mode has not been set
*/
public static function ensureTableFor(string $name, string $dataType): string
public static function ensureTable(string $name): string
{
$dataType = match (Configuration::$mode) {
Mode::PgSQL => 'JSONB',
Mode::SQLite => 'TEXT',
default => throw new DocumentException('Database mode not set; cannot make create table statement')
};
return "CREATE TABLE IF NOT EXISTS $name (data $dataType NOT NULL)";
}

59
src/Query/Patch.php Normal file
View File

@@ -0,0 +1,59 @@
<?php declare(strict_types=1);
namespace BitBadger\PDODocument\Query;
use BitBadger\PDODocument\Configuration;
use BitBadger\PDODocument\DocumentException;
use BitBadger\PDODocument\Field;
use BitBadger\PDODocument\Mode;
use BitBadger\PDODocument\Query;
/**
* Queries to perform partial updates on documents
*/
class Patch
{
/**
* Create an UPDATE statement to patch documents
*
* @param string $tableName The name of the table in which documents should be patched
* @param string $whereClause The body of the WHERE clause to use in the UPDATE statement
* @return string The UPDATE statement to perform the patch
* @throws DocumentException If the database mode has not been set
*/
public static function update(string $tableName, string $whereClause): string
{
$setValue = match (Configuration::$mode) {
Mode::PgSQL => 'data || @data',
Mode::SQLite => 'json_patch(data, json(@data))',
default => throw new DocumentException('Database mode not set; cannot make patch statement')
};
return "UPDATE $tableName SET data = $setValue WHERE $whereClause";
}
/**
* Query to patch (partially update) a document by its ID
*
* @param string $tableName The name of the table in which a document should be patched
* @return string The query to patch a document by its ID
* @throws DocumentException If the database mode has not been set
*/
public static function byId(string $tableName): string
{
return self::update($tableName, Query::whereById());
}
/**
* Query to patch (partially update) a document via a comparison on a JSON field
*
* @param string $tableName The name of the table in which documents should be patched
* @param array|Field[] $field The field comparison to match
* @param string $conjunction How to handle multiple conditions (optional; defaults to `AND`)
* @return string The query to patch documents via field comparison
* @throws DocumentException If the database mode has not been set
*/
public static function byFields(string $tableName, array $field, string $conjunction = 'AND'): string
{
return self::update($tableName, Query::whereByFields($field, $conjunction));
}
}