Skip to content

Commit a5e85a7

Browse files
authored
Merge pull request #96 from andrex47/master
remove env() references
2 parents 89b06a2 + f155020 commit a5e85a7

File tree

2 files changed

+43
-25
lines changed

2 files changed

+43
-25
lines changed

README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ return [
5959
'password' => env('DB_PASSWORD', 'password'),
6060
'charset' => 'utf8',
6161
'prefix' => '',
62-
'cache' => true // By default it caches on all connections, if you want some connection not remembered assign `false` (Recommended when modification is performed on tables frequently [development])
62+
'cache_tables' => true,
63+
'cache_time' => 3600
6364
],
6465

6566
...
@@ -97,19 +98,30 @@ The file is usualy found in **/etc/freetds/freetds.conf**. Set the configuration
9798
```
9899

99100
## Configuring the charset between the database and the application
100-
To configure the charset between the database and the application, add the fields `SYBASE_DATABASE_CHARSET` and `SYBASE_APPLICATION_CHARSET` in `.env` file, see the following example:
101+
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:
102+
103+
```database
104+
'sybase' =>
105+
[
106+
'application_encoding' => true,
107+
'application_charset' => '',
108+
'sybase_database_charset' => ''
109+
],
110+
```
101111

102-
```env
103-
SYBASE_DATABASE_CHARSET=CP850
104-
SYBASE_APPLICATION_CHARSET=UTF8
112+
```charset
113+
'charset' => 'utf8',
105114
```
115+
116+
To use the database layer conversion add the property charset to connection configuration on `database.php`
117+
106118
## Configuring the cache
107119
As the library consults table information whenever it receives a request, caching can be used to avoid excessive queries
108120

109-
To use the cache, add the fields `SYBASE_CACHE_TABLES` and `SYBASE_CACHE_TABLES_TIME` to the `.env` file, see the following example:
121+
To use the cache, add the property `cache_tables` to the database.php connection configuration, you can customize the time of the cache with the property `cache_time` in the same configuration
110122
```dotenv
111-
SYBASE_CACHE_TABLES=true
112-
SYBASE_CACHE_TABLES_TIME=3600 # cache table information by `3600` seconds
123+
'cache_tables' => true,
124+
'cache_time' => 3600
113125
```
114126

115127
## Setting to use numeric data type

src/Database/Connection.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ private function compile(Builder $builder)
147147
}
148148
}
149149

150-
$cache_tables = env('SYBASE_CACHE_TABLES');
151-
$cache = ! key_exists('cache_tables', $builder->connection->config) || $builder->connection->config['cache_tables'];
150+
$cache = $builder->connection->config['cache_tables'];
152151

153152
$types = [];
154153

@@ -165,7 +164,7 @@ private function compile(Builder $builder)
165164
$tables = $alias['table'];
166165
}
167166

168-
if ($cache_tables && $cache) {
167+
if ($cache) {
169168
$aux = Cache::remember('sybase_columns/'.$tables.'.columns_info', env('SYBASE_CACHE_TABLES_TIME') ?? 3600, function () use ($tables) {
170169
$queryString = $this->queryString($tables);
171170
$queryRes = $this->getPdo()->query($queryString);
@@ -342,13 +341,16 @@ private function compileNewQuery($query, $bindings)
342341

343342
$newQuery = join(array_map(fn ($k1, $k2) => $k1.$k2, $partQuery, $bindings));
344343
$newQuery = str_replace('[]', '', $newQuery);
345-
346-
$db_charset = env('SYBASE_DATABASE_CHARSET');
347-
$app_charset = env('SYBASE_APPLICATION_CHARSET');
348-
349-
if ($db_charset && $app_charset) {
350-
$newQuery = mb_convert_encoding($newQuery, $db_charset, $app_charset);
344+
$app_encoding = config('database.sybase.app_encoding');
345+
if (! $app_encoding) {
346+
return $newQuery;
351347
}
348+
$db_charset = config('database.sybase.db_charset');
349+
$app_charset = config('database.sybase.app_charset');
350+
if (is_null($db_charset) || is_null($app_charset)) {
351+
throw new \Exception('[SYBASE] Database Charset and App Charset not set');
352+
}
353+
$newQuery = mb_convert_encoding($newQuery, $db_charset, $app_charset);
352354

353355
return $newQuery;
354356
}
@@ -389,14 +391,18 @@ public function select($query, $bindings = [], $useReadPdo = true)
389391

390392
$result = [...$result];
391393

392-
$db_charset = env('SYBASE_DATABASE_CHARSET');
393-
$app_charset = env('SYBASE_APPLICATION_CHARSET');
394-
395-
if ($db_charset && $app_charset) {
396-
foreach ($result as &$r) {
397-
foreach ($r as $k => &$v) {
398-
$v = gettype($v) === 'string' ? mb_convert_encoding($v, $app_charset, $db_charset) : $v;
399-
}
394+
$app_encoding = config('database.sybase.app_encoding');
395+
if (! $app_encoding) {
396+
return $result;
397+
}
398+
$db_charset = config('database.sybase.db_charset');
399+
$app_charset = config('database.sybase.app_charset');
400+
if (is_null($db_charset) || is_null($app_charset)) {
401+
throw new \Exception('[SYBASE] Database Charset and App Charset not set');
402+
}
403+
foreach ($result as &$r) {
404+
foreach ($r as $k => &$v) {
405+
$v = gettype($v) === 'string' ? mb_convert_encoding($v, $app_charset, $db_charset) : $v;
400406
}
401407
}
402408

0 commit comments

Comments
 (0)