Query/src/Drivers/AbstractUtil.php

125 lines
2.8 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
*
2018-01-19 13:43:19 -05:00
* PHP version 7.1
2016-09-07 13:17:17 -04:00
*
* @package Query
* @author Timothy J. Warren <tim@timshomepage.net>
2018-01-19 13:43:19 -05:00
* @copyright 2012 - 2018 Timothy J. Warren
2016-09-07 13:17:17 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://git.timshomepage.net/aviat4ion/Query
*/
2016-09-07 17:39:19 -04:00
namespace Query\Drivers;
/**
* Abstract class defining database / table creation methods
2012-04-20 13:17:39 -04:00
*
2016-10-13 21:55:23 -04:00
* @method string quoteIdent(string $sql)
* @method string quoteTable(string $sql)
*/
2015-11-10 10:12:23 -05:00
abstract class AbstractUtil {
/**
* Reference to the current connection object
2016-09-07 14:22:52 -04:00
* @var DriverInterface
*/
2018-01-22 15:43:56 -05:00
private $connection;
2014-03-31 12:58:43 -04:00
/**
* Save a reference to the connection object for later use
*
2018-01-22 15:43:56 -05:00
* @param DriverInterface $connection
*/
2018-01-22 15:43:56 -05:00
public function __construct(DriverInterface $connection)
{
2018-01-22 15:43:56 -05:00
$this->connection = $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
*
2016-10-12 22:12:25 -04:00
* @return DriverInterface
2012-04-18 16:28:12 -04:00
*/
2016-10-13 21:55:23 -04:00
public function getDriver()
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
*
* @param string $name
2014-03-31 12:58:43 -04:00
* @param array $fields
* @param array $constraints
2016-10-13 21:55:23 -04:00
* @param bool $ifNotExists
* @return string
*/
2016-10-13 21:55:23 -04:00
public function createTable($name, $fields, array $constraints=[], $ifNotExists=TRUE)
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
// ]
2018-01-24 13:14:03 -05:00
$columnArray = \arrayZipper([
'type' => $fields,
'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 = [];
2016-10-13 21:55:23 -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);
2018-01-22 15:43:56 -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
2016-10-13 21:55:23 -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
*
* @param string $name
* @return string
*/
2018-01-22 15:43:56 -05:00
public function deleteTable($name): string
2014-03-31 16:35:02 -04:00
{
2016-10-13 21:55:23 -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
// --------------------------------------------------------------------------
2014-03-31 12:58:43 -04:00
/**
* Return an SQL file with the database table structure
*
* @abstract
* @return string
*/
2016-10-13 21:55:23 -04:00
abstract public function backupStructure();
/**
* Return an SQL file with the database data as insert statements
*
* @abstract
* @return string
*/
2016-10-13 21:55:23 -04:00
abstract public function backupData();
2016-10-13 21:55:23 -04:00
}