Skip to content

Commit f13f9cc

Browse files
andrehankeandrehanke
authored andcommitted
enhance: removed env() references
1 parent b7c57a0 commit f13f9cc

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

README.md

Lines changed: 20 additions & 7 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_tables' => true
62+
'cache_tables' => true,
63+
'cache_time' => 3600
6364
],
6465

6566
...
@@ -97,18 +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 property `cache_tables` and `SYBASE_CACHE_TABLES_TIME` to the `.env` file, the default time for cache is 1 hour, 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_TIME=3600 # cache table information by `3600` seconds
123+
'cache_tables' => true,
124+
'cache_time' => 3600
112125
```
113126

114127
## Setting to use numeric data type

src/Database/Connection.php

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

150-
$cache = $builder->connection->config['cache_tables'] || key_exists('cache_tables', $builder->connection->config);
150+
$cache = $builder->connection->config['cache_tables'];
151151

152152
$types = [];
153153

@@ -341,14 +341,17 @@ private function compileNewQuery($query, $bindings)
341341

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

@@ -388,17 +391,21 @@ public function select($query, $bindings = [], $useReadPdo = true)
388391

389392
$result = [...$result];
390393

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

0 commit comments

Comments
 (0)