feat: 添加数值类型转换功能,确保查询结果中的数值类型一致性

This commit is contained in:
kuaifan 2026-01-04 00:29:29 +00:00
parent 4759e28a56
commit 501ff21e55

View File

@ -256,7 +256,7 @@ class ManticoreBase
$stmt = $pdo->prepare($sql); $stmt = $pdo->prepare($sql);
$this->bindParams($stmt, $params); $this->bindParams($stmt, $params);
$stmt->execute(); $stmt->execute();
return $stmt->fetchAll(); return $this->convertNumericTypes($stmt->fetchAll());
} catch (PDOException $e) { } catch (PDOException $e) {
Log::error('Manticore query error: ' . $e->getMessage(), [ Log::error('Manticore query error: ' . $e->getMessage(), [
'sql' => $sql, 'sql' => $sql,
@ -285,7 +285,7 @@ class ManticoreBase
$this->bindParams($stmt, $params); $this->bindParams($stmt, $params);
$stmt->execute(); $stmt->execute();
$result = $stmt->fetch(); $result = $stmt->fetch();
return $result ?: null; return $result ? $this->convertNumericTypesRow($result) : null;
} catch (PDOException $e) { } catch (PDOException $e) {
Log::error('Manticore queryOne error: ' . $e->getMessage(), [ Log::error('Manticore queryOne error: ' . $e->getMessage(), [
'sql' => $sql, 'sql' => $sql,
@ -295,6 +295,34 @@ class ManticoreBase
} }
} }
/**
* 转换结果集中的数值类型
* PDO 默认将 BIGINT 等数值类型返回为字符串,这里统一转换
*
* @param array $rows 结果集
* @return array 转换后的结果集
*/
private function convertNumericTypes(array $rows): array
{
return array_map([$this, 'convertNumericTypesRow'], $rows);
}
/**
* 转换单行数据中的数值类型
*
* @param array $row 单行数据
* @return array 转换后的数据
*/
private function convertNumericTypesRow(array $row): array
{
foreach ($row as $key => $value) {
if (is_string($value) && is_numeric($value) && !str_contains($value, '.')) {
$row[$key] = (int) $value;
}
}
return $row;
}
/** /**
* 绑定参数到预处理语句 * 绑定参数到预处理语句
* Manticore 对参数类型敏感,需要明确指定 INT 类型 * Manticore 对参数类型敏感,需要明确指定 INT 类型