Query/src/Drivers/Mysql/SQL.php

215 lines
4.0 KiB
PHP
Raw Normal View History

2016-10-12 22:12:25 -04:00
<?php declare(strict_types=1);
2012-04-10 14:06:34 -04:00
/**
* Query
*
2016-09-07 13:17:17 -04:00
* SQL Query Builder / Database Abstraction Layer
2012-04-10 14:06:34 -04:00
*
2020-04-10 20:54:31 -04:00
* PHP version 7.4
2016-09-07 13:17:17 -04:00
*
* @package Query
* @author Timothy J. Warren <tim@timshomepage.net>
2020-03-18 11:31:56 -04:00
* @copyright 2012 - 2020 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
* @version 3.0.0
2012-04-10 14:06:34 -04:00
*/
namespace Query\Drivers\Mysql;
2014-04-02 17:08:50 -04:00
2016-09-07 17:39:19 -04:00
use Query\Drivers\AbstractSQL;
2012-04-10 14:06:34 -04:00
/**
2016-09-07 17:39:19 -04:00
* MySQL specific SQL
2012-04-10 14:06:34 -04:00
*/
2016-09-07 17:39:19 -04:00
class SQL extends AbstractSQL {
2012-04-10 14:06:34 -04:00
/**
* Limit clause
*
* @param string $sql
* @param int $limit
2016-09-07 17:39:19 -04:00
* @param int|boolean $offset
2012-04-10 14:06:34 -04:00
* @return string
*/
public function limit(string $sql, int $limit, ?int $offset=NULL): string
2012-04-10 14:06:34 -04:00
{
if ( ! is_numeric($offset))
{
return $sql." LIMIT {$limit}";
}
return $sql." LIMIT {$offset}, {$limit}";
}
2014-02-25 13:47:35 -05:00
2014-02-04 20:59:30 -05:00
/**
* Get the query plan for the sql query
*
* @param string $sql
* @return string
*/
2018-01-24 13:14:03 -05:00
public function explain(string $sql): string
2014-02-04 20:59:30 -05:00
{
return "EXPLAIN EXTENDED {$sql}";
}
2012-04-10 14:06:34 -04:00
/**
* Random ordering keyword
*
* @return string
*/
2018-01-24 13:14:03 -05:00
public function random(): string
2012-04-10 14:06:34 -04:00
{
return ' RAND() DESC';
2012-04-10 14:06:34 -04:00
}
/**
* Returns sql to list other databases
*
* @return string
*/
2018-01-24 13:14:03 -05:00
public function dbList(): string
2012-04-10 14:06:34 -04:00
{
return <<<SQL
SHOW DATABASES WHERE `Database` NOT IN ('information_schema','mysql')
SQL;
2012-04-10 14:06:34 -04:00
}
/**
* Returns sql to list tables
*
* @param string $database
2012-04-10 14:06:34 -04:00
* @return string
*/
2018-01-24 13:14:03 -05:00
public function tableList($database=''): string
2012-04-10 14:06:34 -04:00
{
2018-01-24 13:14:03 -05:00
// @codeCoverageIgnoreStart
2015-11-11 09:25:21 -05:00
if ( ! empty($database))
{
return "SHOW TABLES FROM `{$database}`";
}
2018-01-24 13:14:03 -05:00
// @codeCoverageIgnoreEnd
2014-02-25 13:47:35 -05:00
2012-04-10 14:06:34 -04:00
return 'SHOW TABLES';
}
/**
* Overridden in MySQL class
*
* @return string
*/
2018-01-24 13:14:03 -05:00
public function systemTableList(): string
2012-04-10 14:06:34 -04:00
{
return <<<SQL
SELECT `TABLE_NAME` FROM `information_schema`.`TABLES`
WHERE `TABLE_SCHEMA`='information_schema'
SQL;
2012-04-10 14:06:34 -04:00
}
/**
* Returns sql to list views
*
* @return string
*/
2018-01-24 13:14:03 -05:00
public function viewList(): string
2012-04-10 14:06:34 -04:00
{
return 'SELECT `table_name` FROM `information_schema`.`views`';
}
/**
* Returns sql to list triggers
*
* @return string
*/
2018-01-24 13:14:03 -05:00
public function triggerList(): string
2012-04-10 14:06:34 -04:00
{
return 'SHOW TRIGGERS';
}
/**
* Return sql to list functions
*
* @return string
*/
2018-01-24 13:14:03 -05:00
public function functionList(): string
2012-04-10 14:06:34 -04:00
{
return 'SHOW FUNCTION STATUS';
}
/**
* Return sql to list stored procedures
*
* @return string
*/
2018-01-24 13:14:03 -05:00
public function procedureList(): string
2012-04-10 14:06:34 -04:00
{
return 'SHOW PROCEDURE STATUS';
}
/**
* Return sql to list sequences
*
2018-01-24 13:14:03 -05:00
* @return string
2012-04-10 14:06:34 -04:00
*/
2018-01-24 13:14:03 -05:00
public function sequenceList(): ?string
2012-04-10 14:06:34 -04:00
{
return NULL;
2012-04-10 14:06:34 -04:00
}
2014-02-25 13:47:35 -05:00
/**
* SQL to show list of field types
*
* @return string
*/
2018-01-24 13:14:03 -05:00
public function typeList(): string
{
return 'SELECT DISTINCT `DATA_TYPE` FROM `information_schema`.`COLUMNS`';
}
2014-02-25 13:47:35 -05:00
/**
* SQL to show information about columns in a table
*
* @param string $table
* @return string
*/
2018-01-24 13:14:03 -05:00
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
*/
2018-01-24 13:14:03 -05:00
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;
}
2014-04-08 14:43:07 -04:00
/**
* Get the list of indexes for the current table
*
* @param string $table
2018-01-24 13:14:03 -05:00
* @return string
2014-04-08 14:43:07 -04:00
*/
2018-01-24 13:14:03 -05:00
public function indexList(string $table): string
2014-04-08 14:43:07 -04:00
{
return "SHOW INDEX IN {$table}";
}
2016-10-13 21:55:23 -04:00
}