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
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ Update your **config/database.php's** default driver with the settings for the *
return [
...

'sybase' =>
[
'application_encoding' => false,
'application_charset' => '',
'database_charset' => ''
],

'connections' => [
...

Expand Down Expand Up @@ -97,23 +104,24 @@ The file is usualy found in **/etc/freetds/freetds.conf**. Set the configuration
tds version = 5.0
```

## Configuring the charset between the database and the application
## Configuring the charset conversion
This package offers to method to charset conversion, it can be converted in application layer or in database layer, we offered both methods because it can be useful for debugging, to config the application layer conversion you need to set up the following entries on the `database.php` file. You can view an example of the application encoding setup below:

```database
'sybase' =>
[
'application_encoding' => true,
'application_charset' => '',
'sybase_database_charset' => ''
'database_charset' => ''
],
```
To use the database layer conversion add the property charset to connection configuration on the sybase configuration array

```charset
'charset' => 'utf8',
```

To use the database layer conversion add the property charset to connection configuration on `database.php`


## Configuring the cache
As the library consults table information whenever it receives a request, caching can be used to avoid excessive queries
Expand Down
29 changes: 14 additions & 15 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ private function compile(Builder $builder)
}

$cache = $builder->connection->config['cache_tables'];

$types = [];

foreach ($arrTables as $tables) {
Expand All @@ -165,10 +164,10 @@ private function compile(Builder $builder)
}

if ($cache) {
$aux = Cache::remember('sybase_columns/'.$tables.'.columns_info', env('SYBASE_CACHE_TABLES_TIME') ?? 3600, function () use ($tables) {
$cacheTime = key_exists('cache_time',$builder->connection->config) ? $builder->connection->config['cache_time'] : 3600;
$aux = cache()->remember("sybase_columns.$tables.columns_info", $cacheTime, function () use ($tables) {
$queryString = $this->queryString($tables);
$queryRes = $this->getPdo()->query($queryString);

return $queryRes->fetchAll(PDO::FETCH_NAMED);
});
} else {
Expand Down Expand Up @@ -341,16 +340,16 @@ private function compileNewQuery($query, $bindings)

$newQuery = join(array_map(fn ($k1, $k2) => $k1.$k2, $partQuery, $bindings));
$newQuery = str_replace('[]', '', $newQuery);
$app_encoding = config('database.sybase.app_encoding');
if (! $app_encoding) {
$application_encoding = config('database.sybase.application_encoding');
if (is_null($application_encoding)) {
return $newQuery;
}
$db_charset = config('database.sybase.db_charset');
$app_charset = config('database.sybase.app_charset');
if (is_null($db_charset) || is_null($app_charset)) {
$database_charset = config('database.sybase.database_charset');
$application_charset = config('database.sybase.application_charset');
if (is_null($database_charset) || is_null($application_charset)) {
throw new \Exception('[SYBASE] Database Charset and App Charset not set');
}
$newQuery = mb_convert_encoding($newQuery, $db_charset, $app_charset);
$newQuery = mb_convert_encoding($newQuery, $database_charset, $application_charset);

return $newQuery;
}
Expand Down Expand Up @@ -391,18 +390,18 @@ public function select($query, $bindings = [], $useReadPdo = true)

$result = [...$result];

$app_encoding = config('database.sybase.app_encoding');
if (! $app_encoding) {
$application_encoding = config('database.sybase.application_encoding');
if (is_null($application_encoding)) {
return $result;
}
$db_charset = config('database.sybase.db_charset');
$app_charset = config('database.sybase.app_charset');
if (is_null($db_charset) || is_null($app_charset)) {
$database_charset = config('database.sybase.database_charset');
$application_charset = config('database.sybase.application_charset');
if (is_null($database_charset) || is_null($application_charset)) {
throw new \Exception('[SYBASE] Database Charset and App Charset not set');
}
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, $application_charset, $database_charset) : $v;
}
}

Expand Down