Derive mode from DSN function

- Add headers in all files
- Minor field name changes
This commit is contained in:
2024-07-20 21:47:21 -04:00
parent 1a37b009ea
commit d8330d828a
81 changed files with 1053 additions and 551 deletions

View File

@@ -1,8 +1,15 @@
<?php declare(strict_types=1);
<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
namespace BitBadger\PDODocument\Query;
use BitBadger\PDODocument\{Configuration, DocumentException, DocumentIndex, Mode};
use Exception;
/**
* Queries to define tables and indexes
@@ -14,14 +21,13 @@ class Definition
*
* @param string $name The name of the table (including schema, if applicable)
* @return string The CREATE TABLE statement for the document table
* @throws DocumentException If the database mode has not been set
* @throws Exception If the database mode has not been set
*/
public static function ensureTable(string $name): string
{
$dataType = match (Configuration::$mode) {
$dataType = match (Configuration::mode('make create table statement')) {
Mode::PgSQL => 'JSONB',
Mode::SQLite => 'TEXT',
default => throw new DocumentException('Database mode not set; cannot make create table statement')
Mode::SQLite => 'TEXT'
};
return "CREATE TABLE IF NOT EXISTS $name (data $dataType NOT NULL)";
}
@@ -35,7 +41,7 @@ class Definition
private static function splitSchemaAndTable(string $tableName): array
{
$parts = explode('.', $tableName);
return sizeof($parts) == 1 ? ["", $tableName] : [$parts[0], $parts[1]];
return sizeof($parts) === 1 ? ["", $tableName] : [$parts[0], $parts[1]];
}
/**
@@ -51,7 +57,7 @@ class Definition
[, $tbl] = self::splitSchemaAndTable($tableName);
$jsonFields = implode(', ', array_map(function (string $field) {
$parts = explode(' ', $field);
$fieldName = sizeof($parts) == 1 ? $field : $parts[0];
$fieldName = sizeof($parts) === 1 ? $field : $parts[0];
$direction = sizeof($parts) < 2 ? "" : " $parts[1]";
return "(data->>'$fieldName')$direction";
}, $fields));
@@ -75,16 +81,16 @@ class Definition
* @param string $tableName The name of the table on which the document index should be created
* @param DocumentIndex $indexType The type of index to be created
* @return string The SQL statement to create an index on JSON documents in the specified table
* @throws DocumentException If the database mode is not PostgreSQL
* @throws Exception|DocumentException If the database mode is not PostgreSQL
*/
public static function ensureDocumentIndexOn(string $tableName, DocumentIndex $indexType): string
{
if (Configuration::$mode <> Mode::PgSQL) {
if (Configuration::mode() <> Mode::PgSQL) {
throw new DocumentException('Document indexes are only supported on PostgreSQL');
}
[, $tbl] = self::splitSchemaAndTable($tableName);
$extraOps = match ($indexType) {
DocumentIndex::Full => '',
DocumentIndex::Full => '',
DocumentIndex::Optimized => ' jsonb_path_ops'
};
return "CREATE INDEX IF NOT EXISTS idx_{$tbl}_document ON $tableName USING GIN (data$extraOps)";