39 lines
1.3 KiB
PHP
39 lines
1.3 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace BitBadger\PDODocument;
|
|
|
|
use PDO;
|
|
|
|
/**
|
|
* Functions to create tables and indexes
|
|
*/
|
|
class Definition
|
|
{
|
|
/**
|
|
* Ensure a document table exists
|
|
*
|
|
* @param string $name The name of the table to be created if it does not exist
|
|
* @param PDO|null $pdo The database connection to use (optional; will obtain one if not provided)
|
|
* @throws DocumentException If any is encountered
|
|
*/
|
|
public static function ensureTable(string $name, ?PDO $pdo = null): void
|
|
{
|
|
Custom::nonQuery(Query\Definition::ensureTable($name), [], $pdo);
|
|
Custom::nonQuery(Query\Definition::ensureKey($name), [], $pdo);
|
|
}
|
|
|
|
/**
|
|
* Ensure a field index exists on a document table
|
|
*
|
|
* @param string $tableName The name of the table which should be indexed
|
|
* @param string $indexName The name of the index
|
|
* @param array $fields Fields which should be a part of this index
|
|
* @param PDO|null $pdo The database connection to use (optional; will obtain one if not provided)
|
|
* @throws DocumentException If any is encountered
|
|
*/
|
|
public static function ensureFieldIndex(string $tableName, string $indexName, array $fields, ?PDO $pdo = null): void
|
|
{
|
|
Custom::nonQuery(Query\Definition::ensureIndexOn($tableName, $indexName, $fields), [], $pdo);
|
|
}
|
|
}
|