Initial SQLite development (#1)
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
47
src/Document.php
Normal file
47
src/Document.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace BitBadger\PDODocument;
|
||||
|
||||
/**
|
||||
* Functions that apply at a whole document level
|
||||
*/
|
||||
class Document
|
||||
{
|
||||
/**
|
||||
* Insert a new document
|
||||
*
|
||||
* @param string $tableName The name of the table into which the document should be inserted
|
||||
* @param array|object $document The document to be inserted
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function insert(string $tableName, array|object $document): void
|
||||
{
|
||||
Custom::nonQuery(Query::insert($tableName), Parameters::json(':data', $document));
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
|
||||
*
|
||||
* @param string $tableName The name of the table to which the document should be saved
|
||||
* @param array|object $document The document to be saved
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function save(string $tableName, array|object $document): void
|
||||
{
|
||||
Custom::nonQuery(Query::save($tableName), Parameters::json(':data', $document));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update (replace) an entire document by its ID
|
||||
*
|
||||
* @param string $tableName The table in which the document should be updated
|
||||
* @param mixed $docId The ID of the document to be updated
|
||||
* @param array|object $document The document to be updated
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function update(string $tableName, mixed $docId, array|object $document): void
|
||||
{
|
||||
Custom::nonQuery(Query::update($tableName),
|
||||
array_merge(Parameters::id($docId), Parameters::json(':data', $document)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user