From 16f1284825366a0c0d4c0496882deb1250c3769b Mon Sep 17 00:00:00 2001 From: Glauco Morais Date: Wed, 7 Aug 2024 17:27:10 -0300 Subject: [PATCH 1/2] Make the connection charset respect `database` config file --- src/Database/Connection.php | 52 +++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/src/Database/Connection.php b/src/Database/Connection.php index 531e65a..8637a19 100644 --- a/src/Database/Connection.php +++ b/src/Database/Connection.php @@ -40,6 +40,43 @@ class Connection extends IlluminateConnection 'money', ]; + /** + * @var string The application charset + */ + private $appCharset; + + /** + * @var string The database charset + */ + private $dbCharset; + + /** + * Create a new database connection instance. + * + * @param \PDO|\Closure $pdo + * @param string $database + * @param string $tablePrefix + * @param array $config + * @return void + */ + public function __construct($pdo, $database = '', $tablePrefix = '', array $config = []) + { + parent::__construct($pdo, $database, $tablePrefix, $config); + $this->configureCharset($config); + } + + /** + * Configures the encoding for the connection. + * + * @param array $config + * @return void + */ + public function configureCharset($config = []) + { + $this->dbCharset = $config['charset'] ?? null; + $this->appCharset = env('APPLICATION_CHARSET'); + } + /** * Execute a Closure within a transaction. * @@ -356,11 +393,8 @@ private function compileNewQuery($query, $bindings) $newQuery = join(array_map(fn($k1, $k2) => $k1.$k2, $partQuery, $bindings)); $newQuery = str_replace('[]', '', $newQuery); - $db_charset = env('DB_CHARSET'); - $app_charset = env('APPLICATION_CHARSET'); - - if($db_charset && $app_charset) { - $newQuery = mb_convert_encoding($newQuery, $db_charset, $app_charset); + if($this->dbCharset && $this->appCharset) { + $newQuery = mb_convert_encoding($newQuery, $this->dbCharset, $this->appCharset); } return $newQuery; @@ -389,16 +423,12 @@ public function select($query, $bindings = [], $useReadPdo = true) $bindings )); - $result = $statement->fetchAll($this->getFetchMode()); - $db_charset = env('DB_CHARSET'); - $app_charset = env('APPLICATION_CHARSET'); - - if($db_charset && $app_charset) { + if($this->dbCharset && $this->appCharset) { foreach($result as &$r) { foreach($r as $k => &$v) { - $v = gettype($v) === 'string' ? mb_convert_encoding($v, $app_charset, $db_charset) : $v; + $v = gettype($v) === 'string' ? mb_convert_encoding($v, $this->appCharset, $this->dbCharset) : $v; } } } From dcd0c97b6fc874225fc12f1d32372b71066834af Mon Sep 17 00:00:00 2001 From: Glauco Morais Date: Wed, 7 Aug 2024 17:30:53 -0300 Subject: [PATCH 2/2] Fix the column x type matrix Sybase is case-sensitive. Forcing `strtolower` makes, in some cases, everything be parsed as `string`. --- src/Database/Connection.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Database/Connection.php b/src/Database/Connection.php index 8637a19..a42b8fb 100644 --- a/src/Database/Connection.php +++ b/src/Database/Connection.php @@ -189,7 +189,7 @@ private function compile(Builder $builder) foreach ($arrTables as $tables) { preg_match ( "/(?:(?'table'.*)(?: as )(?'alias'.*))|(?'tables'.*)/", - strtolower($tables), + $tables, $alias ); @@ -212,12 +212,12 @@ private function compile(Builder $builder) } foreach ($aux as &$row) { - $types[strtolower($row['name'])] = $row['type']; - $types[strtolower($tables.'.'.$row['name'])] = $row['type']; + $types[$row['name']] = $row['type']; + $types[$tables.'.'.$row['name']] = $row['type']; if (! empty($alias['alias'])) { $types[ - strtolower($alias['alias'].'.'.$row['name']) + $alias['alias'].'.'.$row['name'] ] = $row['type']; } } @@ -228,7 +228,7 @@ private function compile(Builder $builder) $convert = function($column, $v) use($types) { if (is_null($v)) return null; - $variable_type = $types[strtolower($column)]; + $variable_type = $types[$column]; if (in_array($variable_type, $this->numeric)) { return $v / 1;