Query/src/Drivers/Mysql/Driver.php

87 lines
2.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
*
2022-09-29 11:33:08 -04:00
* PHP version 8.1
2016-09-07 13:17:17 -04:00
*
* @package Query
2023-01-20 11:30:51 -05:00
* @author Timothy J. Warren <tim@timshome.page>
* @copyright 2012 - 2023 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
2023-03-17 16:34:21 -04:00
* @version 4.1.0
2012-04-10 14:06:34 -04:00
*/
2023-03-17 15:18:33 -04:00
namespace Query\Drivers\Mysql;
2014-04-02 17:08:50 -04:00
2016-10-13 21:55:23 -04:00
use PDO;
use PHPUnit\Framework\Attributes\CodeCoverageIgnore;
2016-10-13 21:55:23 -04:00
use Query\Drivers\AbstractDriver;
use function defined;
2016-09-07 17:39:19 -04:00
2012-04-10 14:06:34 -04:00
/**
* MySQL specific class
*/
2023-03-17 15:18:33 -04:00
class Driver extends AbstractDriver
{
2016-09-07 17:39:19 -04:00
/**
* Set the backtick as the MySQL escape character
*/
protected string $escapeCharOpen = '`';
2012-04-10 14:06:34 -04:00
/**
* Set the backtick as the MySQL escape character
*/
protected string $escapeCharClose = '`';
2012-04-10 14:06:34 -04:00
/**
* Connect to MySQL Database
*/
#[CodeCoverageIgnore]
2023-03-17 15:18:33 -04:00
public function __construct(string $dsn, ?string $username=NULL, ?string $password=NULL, array $options=[])
2012-04-10 14:06:34 -04:00
{
2014-02-11 14:38:08 -05:00
// Set the charset to UTF-8
if (defined('\\PDO::MYSQL_ATTR_INIT_COMMAND'))
{
2016-09-07 13:10:03 -04:00
$options = array_merge($options, [
2016-10-13 21:55:23 -04:00
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF-8 COLLATE 'UTF-8'",
2016-09-07 13:10:03 -04:00
]);
}
if ( ! str_contains($dsn, 'mysql'))
2015-11-11 09:25:21 -05:00
{
2023-03-17 15:18:33 -04:00
$dsn = 'mysql:' . $dsn;
2015-11-11 09:25:21 -05:00
}
parent::__construct($dsn, $username, $password, $options);
2012-04-10 14:06:34 -04:00
}
/**
* Generate the returning clause for the current database
*/
public function returning(string $query, string $select): string
{
// @TODO add checks for MariaDB for future-proofing
// MariaDB 10.5.0+ supports the returning clause for insert
if (
stripos($query, 'insert') !== FALSE
&& version_compare($this->getVersion(), '10.5.0', '>=')
2023-03-17 15:18:33 -04:00
) {
return parent::returning($query, $select);
}
// MariaDB 10.0.5+ supports the returning clause for delete
if (
stripos($query, 'delete') !== FALSE
&& version_compare($this->getVersion(), '10.0.5', '>=')
2023-03-17 15:18:33 -04:00
) {
return parent::returning($query, $select);
}
// Just return the same SQL if the returning clause is not supported
return $query;
}
2023-03-17 15:18:33 -04:00
}