# phpDox - Source of SQL.php

# Source of file SQL.php

Size: 4,076 Bytes - Last Modified: 2020-04-17T14:56:27-04:00

## src/Drivers/Mysql/SQL.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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

    <?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\Mysql;

    use Query\Drivers\AbstractSQL;

    /**

     * MySQL specific SQL

     */

    class SQL extends AbstractSQL {

    	/**

    	 * Limit clause

    	 *

    	 * @param string $sql

    	 * @param int $limit

    	 * @param int|boolean $offset

    	 * @return string

    	 */

    	public function limit(string $sql, int $limit, ?int $offset=NULL): string

    	{

    		if ( ! is_numeric($offset))

    		{

    			return $sql." LIMIT {$limit}";

    		}

    		return $sql." LIMIT {$offset}, {$limit}";

    	}

    	/**

    	 * Get the query plan for the sql query

    	 *

    	 * @param string $sql

    	 * @return string

    	 */

    	public function explain(string $sql): string

    	{

    		return "EXPLAIN EXTENDED {$sql}";

    	}

    	/**

    	 * Random ordering keyword

    	 *

    	 * @return string

    	 */

    	public function random(): string

    	{

    		return ' RAND() DESC';

    	}

    	/**

    	 * Returns sql to list other databases

    	 *

    	 * @return string

    	 */

    	public function dbList(): string

    	{

    		return <<<SQL

    			SHOW DATABASES WHERE `Database` NOT IN ('information_schema','mysql')

    SQL;

    	}

    	/**

    	 * Returns sql to list tables

    	 *

    	 * @param string $database

    	 * @return string

    	 */

    	public function tableList($database=''): string

    	{

    		// @codeCoverageIgnoreStart

    		if ( ! empty($database))

    		{

    			return "SHOW TABLES FROM `{$database}`";

    		}

    		// @codeCoverageIgnoreEnd

    		return 'SHOW TABLES';

    	}

    	/**

    	 * Overridden in MySQL class

    	 *

    	 * @return string

    	 */

    	public function systemTableList(): string

    	{

    		return <<<SQL

    			SELECT `TABLE_NAME` FROM `information_schema`.`TABLES`

    			WHERE `TABLE_SCHEMA`='information_schema'

    SQL;

    	}

    	/**

    	 * Returns sql to list views

    	 *

    	 * @return string

    	 */

    	public function viewList(): string

    	{

    		return 'SELECT `table_name` FROM `information_schema`.`views`';

    	}

    	/**

    	 * Returns sql to list triggers

    	 *

    	 * @return string

    	 */

    	public function triggerList(): string

    	{

    		return 'SHOW TRIGGERS';

    	}

    	/**

    	 * Return sql to list functions

    	 *

    	 * @return string

    	 */

    	public function functionList(): string

    	{

    		return 'SHOW FUNCTION STATUS';

    	}

    	/**

    	 * Return sql to list stored procedures

    	 *

    	 * @return string

    	 */

    	public function procedureList(): string

    	{

    		return 'SHOW PROCEDURE STATUS';

    	}

    	/**

    	 * Return sql to list sequences

    	 *

    	 * @return string

    	 */

    	public function sequenceList(): ?string

    	{

    		return NULL;

    	}

    	/**

    	 * SQL to show list of field types

    	 *

    	 * @return string

    	 */

    	public function typeList(): string

    	{

    		return 'SELECT DISTINCT `DATA_TYPE` FROM `information_schema`.`COLUMNS`';

    	}

    	/**

    	 * SQL to show information about columns in a table

    	 *

    	 * @param string $table

    	 * @return string

    	 */

    	public function columnList(string $table): string

    	{

    		return "SHOW FULL COLUMNS FROM {$table}";

    	}

    	/**

    	 * Get the list of foreign keys for the current

    	 * table

    	 *

    	 * @param string $table

    	 * @return string

    	 */

    	public function fkList(string $table): string

    	{

    		return <<<SQL

    			SELECT DISTINCT

    				`kcu`.`COLUMN_NAME` as `child_column`,

    				`kcu`.`REFERENCED_TABLE_NAME` as `parent_table`,

    				`kcu`.`REFERENCED_COLUMN_NAME` as `parent_column`,

    				`rc`.`UPDATE_RULE` AS `update`,

    				`rc`.`DELETE_RULE` AS `delete`

    			FROM `INFORMATION_SCHEMA`.`TABLE_CONSTRAINTS` `tc`

    			INNER JOIN `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE` `kcu`

    				ON `kcu`.`CONSTRAINT_NAME`=`tc`.`CONSTRAINT_NAME`

    			INNER JOIN `INFORMATION_SCHEMA`.`REFERENTIAL_CONSTRAINTS` `rc`

    				ON `rc`.`CONSTRAINT_NAME`=`tc`.`CONSTRAINT_NAME`

    			WHERE `tc`.`CONSTRAINT_TYPE`='FOREIGN KEY'

    			AND `tc`.`TABLE_NAME`='{$table}'

    SQL;

    	}

    	/**

    	 * Get the list of indexes for the current table

    	 *

    	 * @param string $table

    	 * @return string

    	 */

    	public function indexList(string $table): string

    	{

    		return "SHOW INDEX IN {$table}";

    	}

    }