Source of file AbstractUtil.php

Size: 2,916 Bytes - Last Modified: 2018-01-25T20:41:57+00:00

src/Drivers/AbstractUtil.php

1234567891011121314151617181920212223242526272829303132333435363738
Covered by 7 test(s):
  • Query\Tests\ConnectionManagerTest::testConnect
  • Query\Tests\ConnectionManagerTest::testGetConnection
  • Query\Tests\QueryParserTest::testGeneric
  • Query\Tests\QueryParserTest::testGeneric2
  • Query\Tests\QueryParserTest::testWUnderscore
  • Query\Tests\QueryParserTest::testFunction
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testConnection
39404142434445464748
Covered by 8 test(s):
  • Query\Tests\Drivers\MySQL\MySQLDriverTest::testCreateTable
  • Query\Tests\Drivers\MySQL\MySQLDriverTest::testBackup
  • Query\Tests\Drivers\MySQL\MySQLDriverTest::testBackupData
  • Query\Tests\Drivers\PgSQL\PgSQLDriverTest::testCreateTable
  • Query\Tests\Drivers\PgSQL\PgSQLDriverTest::testBackupData
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupStructure
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testDeleteTable
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
4950515253545556575859606162
Covered by 2 test(s):
  • Query\Tests\Drivers\MySQL\MySQLDriverTest::testCreateTable
  • Query\Tests\Drivers\PgSQL\PgSQLDriverTest::testCreateTable
6364656667686970
Covered by 2 test(s):
  • Query\Tests\Drivers\MySQL\MySQLDriverTest::testCreateTable
  • Query\Tests\Drivers\PgSQL\PgSQLDriverTest::testCreateTable
71
Covered by 2 test(s):
  • Query\Tests\Drivers\MySQL\MySQLDriverTest::testCreateTable
  • Query\Tests\Drivers\PgSQL\PgSQLDriverTest::testCreateTable
72
Covered by 2 test(s):
  • Query\Tests\Drivers\MySQL\MySQLDriverTest::testCreateTable
  • Query\Tests\Drivers\PgSQL\PgSQLDriverTest::testCreateTable
73747576
Covered by 2 test(s):
  • Query\Tests\Drivers\MySQL\MySQLDriverTest::testCreateTable
  • Query\Tests\Drivers\PgSQL\PgSQLDriverTest::testCreateTable
77
Covered by 2 test(s):
  • Query\Tests\Drivers\MySQL\MySQLDriverTest::testCreateTable
  • Query\Tests\Drivers\PgSQL\PgSQLDriverTest::testCreateTable
7879
Covered by 2 test(s):
  • Query\Tests\Drivers\MySQL\MySQLDriverTest::testCreateTable
  • Query\Tests\Drivers\PgSQL\PgSQLDriverTest::testCreateTable
80
Covered by 2 test(s):
  • Query\Tests\Drivers\MySQL\MySQLDriverTest::testCreateTable
  • Query\Tests\Drivers\PgSQL\PgSQLDriverTest::testCreateTable
81
Covered by 2 test(s):
  • Query\Tests\Drivers\MySQL\MySQLDriverTest::testCreateTable
  • Query\Tests\Drivers\PgSQL\PgSQLDriverTest::testCreateTable
8283
Covered by 2 test(s):
  • Query\Tests\Drivers\MySQL\MySQLDriverTest::testCreateTable
  • Query\Tests\Drivers\PgSQL\PgSQLDriverTest::testCreateTable
84858687
Covered by 2 test(s):
  • Query\Tests\Drivers\MySQL\MySQLDriverTest::testCreateTable
  • Query\Tests\Drivers\PgSQL\PgSQLDriverTest::testCreateTable
88
Covered by 2 test(s):
  • Query\Tests\Drivers\MySQL\MySQLDriverTest::testCreateTable
  • Query\Tests\Drivers\PgSQL\PgSQLDriverTest::testCreateTable
89
Covered by 2 test(s):
  • Query\Tests\Drivers\MySQL\MySQLDriverTest::testCreateTable
  • Query\Tests\Drivers\PgSQL\PgSQLDriverTest::testCreateTable
9091
Covered by 2 test(s):
  • Query\Tests\Drivers\MySQL\MySQLDriverTest::testCreateTable
  • Query\Tests\Drivers\PgSQL\PgSQLDriverTest::testCreateTable
9293949596979899100101102
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testDeleteTable
103104105106107108109110111112113114115116117118119120121122123124125
<?php declare(strict_types=1);
/**
 * Query
 *
 * SQL Query Builder / Database Abstraction Layer
 *
 * PHP version 7.1
 *
 * @package     Query
 * @author      Timothy J. Warren <tim@timshomepage.net>
 * @copyright   2012 - 2018 Timothy J. Warren
 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
 * @link        https://git.timshomepage.net/aviat4ion/Query
 */
namespace Query\Drivers;

/**
 * Abstract class defining database / table creation methods
 *
 * @method string quoteIdent(string $sql)
 * @method string quoteTable(string $sql)
 */
abstract class AbstractUtil {

	/**
	 * Reference to the current connection object
	 * @var DriverInterface
	 */
	private $connection;

	/**
	 * Save a reference to the connection object for later use
	 *
	 * @param DriverInterface $connection
	 */
	public function __construct(DriverInterface $connection)
	{
		$this->connection = $connection;
	}

	/**
	 * Get the driver object for the current connection
	 *
	 * @return DriverInterface
	 */
	public function getDriver()
	{
		return $this->connection;
	}

	/**
	 * Convenience public function to generate sql for creating a db table
	 *
	 * @param string $name
	 * @param array $fields
	 * @param array $constraints
	 * @param bool $ifNotExists
	 * @return string
	 */
	public function createTable($name, $fields, array $constraints=[], $ifNotExists=TRUE)
	{
		$existsStr = $ifNotExists ? ' IF NOT EXISTS ' : ' ';

		// Reorganize into an array indexed with column information
		// Eg $columnArray[$colname] = [
		// 		'type' => ...,
		// 		'constraint' => ...,
		// 		'index' => ...,
		// ]
		$columnArray = \arrayZipper([
			'type' => $fields,
			'constraint' => $constraints
		]);

		// Join column definitions together
		$columns = [];
		foreach($columnArray as $n => $props)
		{
			$str = $this->getDriver()->quoteIdent($n);
			$str .= isset($props['type']) ? " {$props['type']}" : "";
			$str .= isset($props['constraint']) ? " {$props['constraint']}" : "";

			$columns[] = $str;
		}

		// Generate the sql for the creation of the table
		$sql = 'CREATE TABLE'.$existsStr.$this->getDriver()->quoteTable($name).' (';
		$sql .= implode(', ', $columns);
		$sql .= ')';

		return $sql;
	}

	/**
	 * Drop the selected table
	 *
	 * @param string $name
	 * @return string
	 */
	public function deleteTable($name): string
	{
		return 'DROP TABLE IF EXISTS '.$this->getDriver()->quoteTable($name);
	}

	// --------------------------------------------------------------------------
	// ! Abstract Methods
	// --------------------------------------------------------------------------

	/**
	 * Return an SQL file with the database table structure
	 *
	 * @abstract
	 * @return string
	 */
	abstract public function backupStructure();

	/**
	 * Return an SQL file with the database data as insert statements
	 *
	 * @abstract
	 * @return string
	 */
	abstract public function backupData();

}