From dc3fa3bdacb281e5277a87e577c143eb52dd47fb Mon Sep 17 00:00:00 2001 From: liugang Date: Thu, 20 Nov 2025 15:57:38 +0800 Subject: [PATCH] + [refac] Add a method to get the server charset and collation. --- lib/dbh/dbh.class.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/dbh/dbh.class.php b/lib/dbh/dbh.class.php index 90a37695e8..f0322bc77c 100644 --- a/lib/dbh/dbh.class.php +++ b/lib/dbh/dbh.class.php @@ -486,6 +486,24 @@ class dbh return $collations['utf8mb4_uca1400_ai_ci'] ?? $collations['utf8mb4_0900_ai_ci'] ?? $collations['utf8mb4_unicode_ci'] ?? 'utf8mb4_general_ci'; } + /** + * 获取服务器支持的字符集和排序规则。 + * Get the server charset and collation. + * + * @access private + * @return array + */ + private function getServerCharsetAndCollation(): array + { + if($this->dbConfig->driver != 'mysql') return ['charset' => 'utf8', 'collation' => '']; + + $charsets = []; + $statement = $this->rawQuery("SHOW CHARSET WHERE Charset LIKE 'utf8%';"); + while($charset = $statement->fetch(PDO::FETCH_ASSOC)) $charsets[$charset['Charset']] = ['charset' => $charset['Charset'], 'collation' => $charset['Default collation']]; + + return $charsets['utf8mb4'] ?? $charsets['utf8mb3'] ?? ['charset' => 'utf8', 'collation' => 'utf8_general_ci']; + } + /** * Create database. *