Skip to content

Commit 41acc45

Browse files
authored
Merge pull request #92 from andrex47/master
updated charset conversion
2 parents 5c8b5d6 + 4b91d56 commit 41acc45

File tree

4 files changed

+45
-38
lines changed

4 files changed

+45
-38
lines changed

README.md

+10-23
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,9 @@
1414

1515
Add the following in the require section of your **composer.json**:
1616

17-
### Laravel 5.1, 5.2, 5.3
18-
19-
```json
20-
"uepg/laravel-sybase": "~1.0"
21-
```
22-
### Laravel 5.4, 5.5, 5.6, 5.7, 5.8, 6.x, 7.x, 8.x, 9.x
23-
24-
```json
25-
"uepg/laravel-sybase": "~2.0"
26-
```
27-
28-
### Laravel 10.x
29-
17+
### Laravel >=7.x
3018
```json
31-
"uepg/laravel-sybase": "~3.0" // old version
32-
//or The new version
33-
"uepg/laravel-sybase": "~4.0" // new version
19+
"xbu3n0/laravel-sybase": "~4.0"
3420
```
3521

3622
Update the package dependencies executing:
@@ -71,8 +57,9 @@ return [
7157
'database' => env('DB_DATABASE', 'mydatabase'),
7258
'username' => env('DB_USERNAME', 'user'),
7359
'password' => env('DB_PASSWORD', 'password'),
74-
'charset' => 'utf8', // Experimental yet, prefer use the `DB_CHARSET` and `APPLICATION_CHARSET`
60+
'charset' => 'utf8',
7561
'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])
7663
],
7764

7865
...
@@ -110,19 +97,19 @@ The file is usualy found in **/etc/freetds/freetds.conf**. Set the configuration
11097
```
11198

11299
## Configuring the charset between the database and the application
113-
To configure the charset between the database and the application, add the fields `DB_CHARSET` and `APPLICATION_CHARSET` in `.env` file, see the following example:
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:
114101

115102
```env
116-
DB_CHARSET=CP850
117-
APPLICATION_CHARSET=UTF8
103+
SYBASE_DATABASE_CHARSET=CP850
104+
SYBASE_APPLICATION_CHARSET=UTF8
118105
```
119106
## Configuring the cache
120107
As the library consults table information whenever it receives a request, caching can be used to avoid excessive queries
121108

122-
To use the cache, add the fields `SYBASE_CACHE_COLUMNS` and `SYBASE_CACHE_COLUMNS_TIME` to the `.env` file, see the following example:
109+
To use the cache, add the fields `SYBASE_CACHE_TABLES` and `SYBASE_CACHE_TABLES_TIME` to the `.env` file, see the following example:
123110
```dotenv
124-
SYBASE_CACHE_COLUMNS=true
125-
SYBASE_CACHE_COLUMNS_TIME=3600 # cache table information by `3600` seconds
111+
SYBASE_CACHE_TABLES=true
112+
SYBASE_CACHE_TABLES_TIME=3600 # cache table information by `3600` seconds
126113
```
127114

128115
## Setting to use numeric data type

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
"wiki": "https://github.com/uepg/laravel-sybase/wiki"
2525
},
2626
"require": {
27-
"php": "^8.1",
27+
"php": ">=8.1",
2828
"doctrine/dbal": "^3.5.1",
29-
"illuminate/database": "^10",
30-
"illuminate/support": "^10",
29+
"illuminate/database": ">=8.0",
30+
"illuminate/support": ">=8.0",
3131
"ext-pdo": "*"
3232
},
3333
"require-dev": {

src/Database/Connection.php

+26-10
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ private function compile(Builder $builder)
147147
}
148148
}
149149

150-
$cache_columns = env('SYBASE_CACHE_COLUMNS');
150+
$cache_tables = env('SYBASE_CACHE_TABLES');
151+
$cache = ! key_exists('cache_tables', $builder->connection->config) || $builder->connection->config['cache_tables'];
152+
153+
$types = [];
151154

152155
foreach ($arrTables as $tables) {
153156
preg_match(
@@ -162,8 +165,8 @@ private function compile(Builder $builder)
162165
$tables = $alias['table'];
163166
}
164167

165-
if ($cache_columns == true) {
166-
$aux = Cache::remember('sybase_columns/'.$tables.'.columns_info', env('SYBASE_CACHE_COLUMNS_TIME') ?? 600, function () use ($tables) {
168+
if ($cache_tables && $cache) {
169+
$aux = Cache::remember('sybase_columns/'.$tables.'.columns_info', env('SYBASE_CACHE_TABLES_TIME') ?? 3600, function () use ($tables) {
167170
$queryString = $this->queryString($tables);
168171
$queryRes = $this->getPdo()->query($queryString);
169172

@@ -255,7 +258,8 @@ private function compile(Builder $builder)
255258
*/
256259
private function queryString($tables)
257260
{
258-
$explicitDB = explode('..', $tables);
261+
$tables = str_replace('..', '.dbo.', $tables);
262+
$explicitDB = explode('.dbo.', $tables);
259263

260264
// Has domain.table
261265
if (isset($explicitDB[1])) {
@@ -357,12 +361,13 @@ private function compileNewQuery($query, $bindings)
357361

358362
$bindings = array_map(fn ($v) => gettype($v) === 'string' ? str_replace('\'', '\'\'', $v) : $v, $bindings);
359363
$bindings = array_map(fn ($v) => gettype($v) === 'string' ? "'{$v}'" : $v, $bindings);
364+
$bindings = array_map(fn ($v) => gettype($v) === 'NULL' ? 'NULL' : $v, $bindings);
360365

361366
$newQuery = join(array_map(fn ($k1, $k2) => $k1.$k2, $partQuery, $bindings));
362367
$newQuery = str_replace('[]', '', $newQuery);
363368

364-
$db_charset = env('DB_CHARSET');
365-
$app_charset = env('APPLICATION_CHARSET');
369+
$db_charset = env('SYBASE_DATABASE_CHARSET');
370+
$app_charset = env('SYBASE_APPLICATION_CHARSET');
366371

367372
if ($db_charset && $app_charset) {
368373
$newQuery = mb_convert_encoding($newQuery, $db_charset, $app_charset);
@@ -389,15 +394,26 @@ public function select($query, $bindings = [], $useReadPdo = true)
389394
return [];
390395
}
391396

392-
$statement = $this->getPdo()->query($this->compileNewQuery(
397+
$statement = $this->getPdo()->prepare($this->compileNewQuery(
393398
$query,
394399
$bindings
395400
));
396401

397-
$result = $statement->fetchAll($this->getFetchMode());
402+
$statement->execute();
403+
404+
$result = [];
405+
406+
try {
407+
do {
408+
$result += $statement->fetchAll($this->getFetchMode());
409+
} while ($statement->nextRowset());
410+
} catch (\Exception $e) {
411+
}
412+
413+
$result = [...$result];
398414

399-
$db_charset = env('DB_CHARSET');
400-
$app_charset = env('APPLICATION_CHARSET');
415+
$db_charset = env('SYBASE_DATABASE_CHARSET');
416+
$app_charset = env('SYBASE_APPLICATION_CHARSET');
401417

402418
if ($db_charset && $app_charset) {
403419
foreach ($result as &$r) {

src/Database/Connector.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ public function connect(array $config)
1212

1313
$connection = $this->createConnection($this->getDsn($config), $config, $options);
1414

15-
if (array_key_exists('charset', $config) && $config['charset'] != '') {
15+
if (isset($config['charset'])) {
1616
$connection->prepare("set char_convert '{$config['charset']}'")->execute();
1717
}
1818

19-
$this->configureIsolationLevel($connection, $config);
19+
if (isset($config['isolation_level'])) {
20+
$connection->prepare(
21+
"SET TRANSACTION ISOLATION LEVEL {$config['isolation_level']}"
22+
)->execute();
23+
}
2024

2125
return $connection;
2226
}

0 commit comments

Comments
 (0)