# phpDox - Source of Driver.php

# Source of file Driver.php

Size: 2,256 Bytes - Last Modified: 2020-04-17T14:56:27-04:00

## src/Drivers/Mysql/Driver.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

    <?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 PDO;

    use Query\Drivers\AbstractDriver;

    use function defined;

    /**

     * MySQL specific class

     */

    class Driver extends AbstractDriver {

    	/**

    	 * Set the backtick as the MySQL escape character

    	 *

    	 * @var string

    	 */

    	protected string $escapeCharOpen = '`';

    	/**

    	 * Set the backtick as the MySQL escape character

    	 *

    	 * @var string

    	 */

    	protected string $escapeCharClose = '`';

    	/**

    	 * Connect to MySQL Database

    	 *

    	 * @codeCoverageIgnore

    	 * @param string $dsn

    	 * @param string $username

    	 * @param string $password

    	 * @param array $options

    	 */

    	public function __construct(string $dsn, string $username=NULL, string $password=NULL, array $options=[])

    	{

    		// Set the charset to UTF-8

    		if (defined('\\PDO::MYSQL_ATTR_INIT_COMMAND'))

    		{

    			$options = array_merge($options, [

    				PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF-8 COLLATE 'UTF-8'",

    			]);

    		}

    		if (strpos($dsn, 'mysql') === FALSE)

    		{

    			$dsn = 'mysql:'.$dsn;

    		}

    		parent::__construct($dsn, $username, $password, $options);

    	}

    	/**

    	 * Generate the returning clause for the current database

    	 *

    	 * @param string $query

    	 * @param string $select

    	 * @return string

    	 */

    	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', '>=')

    		){

    			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', '>=')

    		){

    			return parent::returning($query, $select);

    		}

    		// Just return the same SQL if the returning clause is not supported

    		return $query;

    	}

    }