# phpDox - Source of AbstractUtil.php

# Source of file AbstractUtil.php

Size: 2,904 Bytes - Last Modified: 2020-04-10T20:54:13-04:00

## src/Drivers/AbstractUtil.php

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

    <?php declare(strict_types=1);

    /**

     * Query

     *

     * SQL Query Builder / Database Abstraction Layer

     *

     * PHP version 7.4

     *

     * @package     Query

     * @author      Timothy J. Warren <tim@timshomepage.net>

     * @copyright   2012 - 2020 Timothy J. Warren

     * @license     http://www.opensource.org/licenses/mit-license.html  MIT License

     * @link        https://git.timshomepage.net/aviat/Query

     * @version     3.0.0

     */

    namespace Query\Drivers;

    /**

     * Abstract class defining database / table creation methods

     */

    abstract class AbstractUtil {

    	/**

    	 * Reference to the current connection object

    	 * @var DriverInterface

    	 */

    	private DriverInterface $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(): DriverInterface

    	{

    		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): string

    	{

    		$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(): string;

    	/**

    	 * Return an SQL file with the database data as insert statements

    	 *

    	 * @abstract

    	 * @return string

    	 */

    	abstract public function backupData(): string;

    }