Support connect to mysql using socket (#1749)

* Support connect to mysql using socket

* pgsql can also use unix_socket

fix Deprecated: pg_escape_string(): Automatic fetching of PostgreSQL connection

* Update install.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update var/Typecho/Db/Adapter/Mysqli.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fix quoteValue method to correctly escape single quotes in strings

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Luffy
2025-11-20 23:55:43 +08:00
committed by GitHub
parent 15967ea059
commit 7a3c86a110
4 changed files with 51 additions and 22 deletions

View File

@@ -1064,7 +1064,11 @@ function install_step_2_perform()
// intval port number
if (isset($dbConfig['port'])) {
$dbConfig['port'] = intval($dbConfig['port']);
if (strpos($dbConfig['host'], '/') !== false && $type == 'Mysql') {
$dbConfig['port'] = null;
} else {
$dbConfig['port'] = intval($dbConfig['port']);
}
}
// bool ssl verify

View File

@@ -5,6 +5,7 @@ namespace Typecho\Db\Adapter;
use Typecho\Config;
use Typecho\Db;
use Typecho\Db\Adapter;
use mysqli_sql_exception;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
@@ -49,26 +50,40 @@ class Mysqli implements Adapter
{
$mysqli = mysqli_init();
if ($mysqli) {
if (!empty($config->sslCa)) {
$mysqli->ssl_set(null, null, $config->sslCa, null, null);
try {
if (!empty($config->sslCa)) {
$mysqli->ssl_set(null, null, $config->sslCa, null, null);
if (isset($config->sslVerify)) {
$mysqli->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, $config->sslVerify);
if (isset($config->sslVerify)) {
$mysqli->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, $config->sslVerify);
}
}
}
$mysqli->real_connect(
$config->host,
$config->user,
$config->password,
$config->database,
(empty($config->port) ? null : $config->port)
);
$host = $config->host;
$port = empty($config->port) ? null : $config->port;
$socket = null;
if (strpos($host, '/') !== false) {
$socket = $host;
$host = 'localhost';
$port = null;
}
$this->dbLink = $mysqli;
$mysqli->real_connect(
$host,
$config->user,
$config->password,
$config->database,
$port,
$socket
);
if ($config->charset) {
$this->dbLink->query("SET NAMES '{$config->charset}'");
$this->dbLink = $mysqli;
if ($config->charset) {
$this->dbLink->query("SET NAMES '{$config->charset}'");
}
} catch (mysqli_sql_exception $e) {
throw new ConnectionException($e->getMessage(), $e->getCode());
}
return $this->dbLink;
@@ -106,8 +121,13 @@ class Mysqli implements Adapter
?string $action = null,
?string $table = null
) {
if ($resource = @$this->dbLink->query($query)) {
return $resource;
try {
if ($resource = $this->dbLink->query($query)) {
return $resource;
}
} catch (mysqli_sql_exception $e) {
/** 数据库异常 */
throw new SQLException($e->getMessage(), $e->getCode());
}
/** 数据库异常 */

View File

@@ -61,9 +61,14 @@ class Mysql extends Pdo
}
}
$dsn = !empty($config->dsn)
? $config->dsn
: (strpos($config->host, '/') !== false
? "mysql:dbname={$config->database};unix_socket={$config->host}"
: "mysql:dbname={$config->database};host={$config->host};port={$config->port}");
$pdo = new \PDO(
!empty($config->dsn)
? $config->dsn : "mysql:dbname={$config->database};host={$config->host};port={$config->port}",
$dsn,
$config->user,
$config->password,
$options

View File

@@ -119,7 +119,7 @@ class Pgsql implements Adapter
/**
* @param resource $resource
* @return array|null
* @return array
*/
public function fetchAll($resource): array
{
@@ -146,6 +146,6 @@ class Pgsql implements Adapter
*/
public function quoteValue($string): string
{
return '\'' . pg_escape_string($string) . '\'';
return '\'' . str_replace('\'', '\'\'', $string) . '\'';
}
}