Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"ipl/stdlib": "dev-main"
},
"autoload": {
"files": [
"src/pdo-driver-consts-forward-compat.php"
],
"psr-4": {
"ipl\\Sql\\": "src"
}
Expand Down
23 changes: 8 additions & 15 deletions src/Adapter/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use ipl\Sql\Config;
use ipl\Sql\Connection;
use PDO;
use Pdo\Mysql as PdoMysql;

class Mysql extends BaseAdapter
{
Expand All @@ -14,7 +14,7 @@

public function setClientTimezone(Connection $db)
{
$db->exec('SET time_zone = ' . $db->quote($this->getTimezoneOffset()));

Check failure on line 17 in src/Adapter/Mysql.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.5) / PHPStan 8.5

Binary operation "." between 'SET time_zone = ' and mixed results in an error.

Check failure on line 17 in src/Adapter/Mysql.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.4) / PHPStan 8.4

Binary operation "." between 'SET time_zone = ' and mixed results in an error.

Check failure on line 17 in src/Adapter/Mysql.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.2) / PHPStan 8.2

Binary operation "." between 'SET time_zone = ' and mixed results in an error.

Check failure on line 17 in src/Adapter/Mysql.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.3) / PHPStan 8.3

Binary operation "." between 'SET time_zone = ' and mixed results in an error.

return $this;
}
Expand All @@ -22,40 +22,33 @@
public function getOptions(Config $config)
{
$options = parent::getOptions($config);
// In PHP 8.5+, driver-specific constants of the PDO class are deprecated,
// but the replacements are only available since php 8.4
if (version_compare(PHP_VERSION, '8.4.0', '<')) {
$mysqlConstantPrefix = 'PDO::MYSQL_ATTR_';
} else {
$mysqlConstantPrefix = 'Pdo\Mysql::ATTR_';
}

if (! empty($config->useSsl)) {
if (! empty($config->sslKey)) {
$options[constant($mysqlConstantPrefix . 'SSL_KEY')] = $config->sslKey;
$options[PdoMysql::ATTR_SSL_KEY] = $config->sslKey;
}

if (! empty($config->sslCert)) {
$options[constant($mysqlConstantPrefix . 'SSL_CERT')] = $config->sslCert;
$options[PdoMysql::ATTR_SSL_CERT] = $config->sslCert;
}

if (! empty($config->sslCa)) {
$options[constant($mysqlConstantPrefix . 'SSL_CA')] = $config->sslCa;
$options[PdoMysql::ATTR_SSL_CA] = $config->sslCa;
}

if (! empty($config->sslCapath)) {
$options[constant($mysqlConstantPrefix . 'SSL_CAPATH')] = $config->sslCapath;
$options[PdoMysql::ATTR_SSL_CAPATH] = $config->sslCapath;
}

if (! empty($config->sslCipher)) {
$options[constant($mysqlConstantPrefix . 'SSL_CIPHER')] = $config->sslCipher;
$options[PdoMysql::ATTR_SSL_CIPHER] = $config->sslCipher;
}

if (
defined($mysqlConstantPrefix . 'SSL_VERIFY_SERVER_CERT')
defined(PdoMysql::class . '::ATTR_SSL_VERIFY_SERVER_CERT')
&& ! empty($config->sslDoNotVerifyServerCert)
) {
$options[constant($mysqlConstantPrefix . 'SSL_VERIFY_SERVER_CERT')] = false;
$options[PdoMysql::ATTR_SSL_VERIFY_SERVER_CERT] = false;
}
}

Expand Down
62 changes: 62 additions & 0 deletions src/pdo-driver-consts-forward-compat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

// phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses

namespace Pdo;

use PDO;

use const PHP_VERSION_ID;

/*
* Forward compatibility shim for PDO MySQL driver constants.
*
* PHP 8.4 introduced driver-specific constants on Pdo\Mysql while older
* versions only expose PDO::MYSQL_* constants. This shim provides
* Pdo\Mysql::ATTR_* for PHP < 8.4.
*/
if (PHP_VERSION_ID < 80400 && extension_loaded('pdo_mysql')) {
/**
* Constants available with mysqlnd and libmysqlclient.
*/
trait MysqlCommonConstants
{
public const ATTR_USE_BUFFERED_QUERY = PDO::MYSQL_ATTR_USE_BUFFERED_QUERY;
public const ATTR_LOCAL_INFILE = PDO::MYSQL_ATTR_LOCAL_INFILE;
public const ATTR_SSL_KEY = PDO::MYSQL_ATTR_SSL_KEY;
public const ATTR_SSL_CERT = PDO::MYSQL_ATTR_SSL_CERT;
public const ATTR_SSL_CA = PDO::MYSQL_ATTR_SSL_CA;
public const ATTR_SSL_CAPATH = PDO::MYSQL_ATTR_SSL_CAPATH;
public const ATTR_SSL_CIPHER = PDO::MYSQL_ATTR_SSL_CIPHER;
public const ATTR_INIT_COMMAND = PDO::MYSQL_ATTR_INIT_COMMAND;
public const ATTR_COMPRESS = PDO::MYSQL_ATTR_COMPRESS;
public const ATTR_DIRECT_QUERY = PDO::MYSQL_ATTR_DIRECT_QUERY;
public const ATTR_FOUND_ROWS = PDO::MYSQL_ATTR_FOUND_ROWS;
public const ATTR_IGNORE_SPACE = PDO::MYSQL_ATTR_IGNORE_SPACE;
public const ATTR_SERVER_PUBLIC_KEY = PDO::MYSQL_ATTR_SERVER_PUBLIC_KEY;
public const ATTR_MULTI_STATEMENTS = PDO::MYSQL_ATTR_MULTI_STATEMENTS;
public const ATTR_LOCAL_INFILE_DIRECTORY = PDO::MYSQL_ATTR_LOCAL_INFILE_DIRECTORY;
}

if (defined('PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT')) {
// mysqlnd variant: includes ATTR_SSL_VERIFY_SERVER_CERT.
// ATTR_READ_DEFAULT_* and ATTR_MAX_BUFFER_SIZE are not available with mysqlnd.
class Mysql
{
use MysqlCommonConstants;

public const ATTR_SSL_VERIFY_SERVER_CERT = PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT;
}
} else {
// non-mysqlnd variant: exposes ATTR_READ_DEFAULT_* and ATTR_MAX_BUFFER_SIZE.
// ATTR_SSL_VERIFY_SERVER_CERT is not available.
class Mysql
{
use MysqlCommonConstants;

public const ATTR_READ_DEFAULT_FILE = PDO::MYSQL_ATTR_READ_DEFAULT_FILE;
public const ATTR_READ_DEFAULT_GROUP = PDO::MYSQL_ATTR_READ_DEFAULT_GROUP;
public const ATTR_MAX_BUFFER_SIZE = PDO::MYSQL_ATTR_MAX_BUFFER_SIZE;
}
}
}
Loading