Add PostgreSQL Support (#3)

Reviewed-on: #3
This commit was merged in pull request #3.
This commit is contained in:
2024-06-21 13:46:41 +00:00
parent 330e272187
commit 124426fa12
61 changed files with 2290 additions and 223 deletions

View File

@@ -2,7 +2,7 @@
namespace BitBadger\PDODocument\Query;
use BitBadger\PDODocument\{Configuration, DocumentException, Mode};
use BitBadger\PDODocument\{Configuration, DocumentException, DocumentIndex, Mode};
/**
* Queries to define tables and indexes
@@ -68,4 +68,25 @@ class Definition
{
return str_replace('INDEX', 'UNIQUE INDEX', self::ensureIndexOn($tableName, 'key', [Configuration::$idField]));
}
/**
* Create a document-wide index on a table (PostgreSQL only)
*
* @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
*/
public static function ensureDocumentIndexOn(string $tableName, DocumentIndex $indexType): string
{
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::Optimized => ' jsonb_path_ops'
};
return "CREATE INDEX IF NOT EXISTS idx_{$tbl}_document ON $tableName USING GIN (data$extraOps)";
}
}