Initial SQLite development (#1)
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
66
src/Query/RemoveFields.php
Normal file
66
src/Query/RemoveFields.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace BitBadger\PDODocument\Query;
|
||||
|
||||
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode, Query};
|
||||
|
||||
/**
|
||||
* Queries to remove fields from documents
|
||||
*
|
||||
* _NOTE: When using these queries to build custom functions, be aware that different databases use significantly
|
||||
* different syntax. The `$parameters` passed to these functions should be run through `Parameters::fieldNames`
|
||||
* function to generate them appropriately for the database currently being targeted._
|
||||
*/
|
||||
class RemoveFields
|
||||
{
|
||||
/**
|
||||
* Create an UPDATE statement to remove fields from a JSON document
|
||||
*
|
||||
* @param string $tableName The name of the table in which documents should be manipulated
|
||||
* @param array $parameters The parameter list for the query
|
||||
* @param string $whereClause The body of the WHERE clause for the update
|
||||
* @return string The UPDATE statement to remove fields from a JSON document
|
||||
* @throws DocumentException If the database mode has not been set
|
||||
*/
|
||||
public static function update(string $tableName, array $parameters, string $whereClause): string
|
||||
{
|
||||
switch (Configuration::$mode) {
|
||||
case Mode::PgSQL:
|
||||
return "UPDATE $tableName SET data = data - " . array_keys($parameters)[0] . " WHERE $whereClause";
|
||||
case Mode::SQLite:
|
||||
$paramNames = implode(', ', array_keys($parameters));
|
||||
return "UPDATE $tableName SET data = json_remove(data, $paramNames) WHERE $whereClause";
|
||||
default:
|
||||
throw new DocumentException('Database mode not set; cannot generate field removal query');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Query to remove fields from a document by the document's ID
|
||||
*
|
||||
* @param string $tableName The name of the table in which the document should be manipulated
|
||||
* @param array $parameters The parameter list for the query
|
||||
* @return string The UPDATE statement to remove fields from a document by its ID
|
||||
* @throws DocumentException If the database mode has not been set
|
||||
*/
|
||||
public static function byId(string $tableName, array $parameters): string
|
||||
{
|
||||
return self::update($tableName, $parameters, Query::whereById());
|
||||
}
|
||||
|
||||
/**
|
||||
* Query to remove fields from documents via a comparison on JSON fields within the document
|
||||
*
|
||||
* @param string $tableName The name of the table in which documents should be manipulated
|
||||
* @param array|Field[] $fields The field comparison to match
|
||||
* @param array $parameters The parameter list for the query
|
||||
* @param string $conjunction How to handle multiple conditions (optional; defaults to `AND`)
|
||||
* @return string The UPDATE statement to remove fields from documents via field comparison
|
||||
* @throws DocumentException If the database mode has not been set
|
||||
*/
|
||||
public static function byFields(string $tableName, array $fields, array $parameters,
|
||||
string $conjunction = 'AND'): string
|
||||
{
|
||||
return self::update($tableName, $parameters, Query::whereByFields($fields, $conjunction));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user