Query/src/Drivers/AbstractUtil.php

100 lines
2.4 KiB
PHP
Raw Normal View History

2016-10-12 22:12:25 -04:00
<?php declare(strict_types=1);
/**
* Query
*
2016-09-07 13:17:17 -04:00
* SQL Query Builder / Database Abstraction Layer
*
2022-09-29 11:33:08 -04:00
* PHP version 8.1
2016-09-07 13:17:17 -04:00
*
* @package Query
2023-01-20 11:30:51 -05:00
* @author Timothy J. Warren <tim@timshome.page>
* @copyright 2012 - 2023 Timothy J. Warren
2016-09-07 13:17:17 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2019-12-11 16:49:42 -05:00
* @link https://git.timshomepage.net/aviat/Query
2023-03-17 16:34:21 -04:00
* @version 4.1.0
*/
2023-03-17 15:18:33 -04:00
2016-09-07 17:39:19 -04:00
namespace Query\Drivers;
2023-03-17 15:18:33 -04:00
use function arrayZipper;
/**
* Abstract class defining database / table creation methods
*/
2023-03-17 15:18:33 -04:00
abstract class AbstractUtil
{
/**
* Save a reference to the connection object for later use
*/
2022-09-29 11:31:25 -04:00
public function __construct(private readonly DriverInterface $connection)
{
}
2014-03-31 12:58:43 -04:00
2012-04-18 16:28:12 -04:00
/**
2014-04-24 13:42:01 -04:00
* Get the driver object for the current connection
2012-04-18 16:28:12 -04:00
*/
2019-12-11 16:49:06 -05:00
public function getDriver(): DriverInterface
2012-04-18 16:28:12 -04:00
{
2018-01-22 15:43:56 -05:00
return $this->connection;
2012-04-18 16:28:12 -04:00
}
2014-03-31 12:58:43 -04:00
/**
2014-04-24 11:31:03 -04:00
* Convenience public function to generate sql for creating a db table
*/
2022-09-29 11:31:25 -04:00
public function createTable(string $name, array $fields, array $constraints=[], bool $ifNotExists=TRUE): string
2014-03-31 12:58:43 -04:00
{
2018-01-22 15:43:56 -05:00
$existsStr = $ifNotExists ? ' IF NOT EXISTS ' : ' ';
2014-04-23 17:03:46 -04:00
2014-03-31 12:58:43 -04:00
// Reorganize into an array indexed with column information
2018-01-26 08:39:30 -05:00
// Eg $columnArray[$colname] = [
2014-03-31 12:58:43 -04:00
// 'type' => ...,
// 'constraint' => ...,
// 'index' => ...,
2018-01-26 08:39:30 -05:00
// ]
2023-03-17 15:18:33 -04:00
$columnArray = arrayZipper([
'type' => $fields,
2023-03-17 15:18:33 -04:00
'constraint' => $constraints,
2016-09-07 13:10:03 -04:00
]);
2014-03-31 12:58:43 -04:00
2014-04-24 11:31:03 -04:00
// Join column definitions together
2016-09-07 13:10:03 -04:00
$columns = [];
2023-03-17 15:18:33 -04:00
foreach ($columnArray as $n => $props)
2014-03-31 12:58:43 -04:00
{
2016-10-13 21:55:23 -04:00
$str = $this->getDriver()->quoteIdent($n);
2019-12-11 16:49:06 -05:00
$str .= isset($props['type']) ? " {$props['type']}" : '';
$str .= isset($props['constraint']) ? " {$props['constraint']}" : '';
2014-03-31 12:58:43 -04:00
$columns[] = $str;
}
// Generate the sql for the creation of the table
2023-03-17 15:18:33 -04:00
$sql = 'CREATE TABLE' . $existsStr . $this->getDriver()->quoteTable($name) . ' (';
2014-03-31 12:58:43 -04:00
$sql .= implode(', ', $columns);
$sql .= ')';
return $sql;
}
/**
2014-03-31 16:35:02 -04:00
* Drop the selected table
*/
2022-09-29 11:31:25 -04:00
public function deleteTable(string $name): string
2014-03-31 16:35:02 -04:00
{
2023-03-17 15:18:33 -04:00
return 'DROP TABLE IF EXISTS ' . $this->getDriver()->quoteTable($name);
2014-03-31 16:35:02 -04:00
}
2014-04-24 13:42:01 -04:00
// --------------------------------------------------------------------------
// ! Abstract Methods
2014-04-15 16:16:15 -04:00
// --------------------------------------------------------------------------
/**
* Return an SQL file with the database table structure
*/
2019-12-11 16:49:06 -05:00
abstract public function backupStructure(): string;
/**
* Return an SQL file with the database data as insert statements
*/
2019-12-11 16:49:06 -05:00
abstract public function backupData(): string;
2023-03-17 15:18:33 -04:00
}