Skip to content

Commit fa57067

Browse files
andrehankeandrehanke
andrehanke
authored and
andrehanke
committed
Merge remote-tracking branch 'refs/remotes/xbueno/laravel-sybase'
# Conflicts: # src/Database/Connection.php # src/Database/Connector.php
2 parents 4384885 + e9c40d5 commit fa57067

File tree

5 files changed

+65
-64
lines changed

5 files changed

+65
-64
lines changed

.github/ISSUE_TEMPLATE/BUG_REPORT.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ If applicable, add screenshots to help explain your problem. If no have screensh
3232
* Laravel: Run `php artisan --version` to show the version.
3333
* PHP: Run `php --version` to show the version.
3434
* Composer: Run `composer --version` to show the version.
35-
* uepg/laravel-sybase: Get the version in `composer.lock`.
35+
* xbu3n0/laravel-sybase: Get the version in `composer.lock`.
3636

3737
## Additional context
3838

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "uepg/laravel-sybase",
2+
"name": "xbu3n0/laravel-sybase",
33
"description": "Sybase based Eloquent module extension for Laravel 10.x",
44
"keywords": [
55
"sybase"
@@ -20,14 +20,14 @@
2020
}
2121
],
2222
"support": {
23-
"issues": "https://github.com/uepg/laravel-sybase/issues",
24-
"wiki": "https://github.com/uepg/laravel-sybase/wiki"
23+
"issues": "https://github.com/xbu3n0/laravel-sybase/issues",
24+
"wiki": "https://github.com/xbu3n0/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

+42-32
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ public function transaction(Closure $callback, $attempts = 1)
6565
$this->pdo->exec('COMMIT TRAN');
6666
}
6767

68-
// If we catch an exception, we will roll back so nothing gets messed
69-
// up in the database. Then we'll re-throw the exception so it can
70-
// be handled how the developer sees fit for their applications.
68+
// If we catch an exception, we will roll back so nothing gets messed
69+
// up in the database. Then we'll re-throw the exception so it can
70+
// be handled how the developer sees fit for their applications.
7171
catch (Exception $e) {
7272
$this->pdo->exec('ROLLBACK TRAN');
7373

@@ -147,10 +147,13 @@ 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) {
153-
preg_match(
156+
preg_match (
154157
"/(?:(?'table'.*)(?: as )(?'alias'.*))|(?'tables'.*)/",
155158
strtolower($tables),
156159
$alias
@@ -162,11 +165,10 @@ 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);
169-
170172
return $queryRes->fetchAll(PDO::FETCH_NAMED);
171173
});
172174
} else {
@@ -189,10 +191,8 @@ private function compile(Builder $builder)
189191

190192
$keys = [];
191193

192-
$convert = function ($column, $v) use ($types) {
193-
if (is_null($v)) {
194-
return null;
195-
}
194+
$convert = function($column, $v) use($types) {
195+
if (is_null($v)) return null;
196196

197197
$variable_type = $types[strtolower($column)];
198198

@@ -205,7 +205,7 @@ private function compile(Builder $builder)
205205

206206
if (isset($builder->values)) {
207207
foreach ($builder->values as $key => $value) {
208-
if (gettype($value) === 'array') {
208+
if(gettype($value) === 'array') {
209209
foreach ($value as $k => $v) {
210210
$keys[] = $convert($k, $v);
211211
}
@@ -233,9 +233,7 @@ private function compile(Builder $builder)
233233
}
234234
}
235235
} elseif ($w['type'] == 'between') {
236-
if (count($w['values']) != 2) {
237-
return [];
238-
}
236+
if(count($w['values']) != 2) { return []; }
239237
foreach ($w['values'] as $v) {
240238
if (gettype($v) !== 'object') {
241239
$keys[] = $convert($k, $v);
@@ -255,7 +253,8 @@ private function compile(Builder $builder)
255253
*/
256254
private function queryString($tables)
257255
{
258-
$explicitDB = explode('..', $tables);
256+
$tables = str_replace('..', '.dbo.', $tables);
257+
$explicitDB = explode('.dbo.', $tables);
259258

260259
// Has domain.table
261260
if (isset($explicitDB[1])) {
@@ -317,7 +316,7 @@ private function queryString($tables)
317316
*
318317
* @param string $query
319318
* @param array $bindings
320-
* @return mixed $newBinds
319+
* @return mixed $newBinds
321320
*/
322321
private function compileBindings($query, $bindings)
323322
{
@@ -355,16 +354,17 @@ private function compileNewQuery($query, $bindings)
355354
$bindings = $this->compileBindings($query, $bindings);
356355
$partQuery = explode('?', $query);
357356

358-
$bindings = array_map(fn ($v) => gettype($v) === 'string' ? str_replace('\'', '\'\'', $v) : $v, $bindings);
359-
$bindings = array_map(fn ($v) => gettype($v) === 'string' ? "'{$v}'" : $v, $bindings);
357+
$bindings = array_map(fn($v) => gettype($v) === 'string' ? str_replace('\'', '\'\'', $v) : $v, $bindings);
358+
$bindings = array_map(fn($v) => gettype($v) === 'string' ? "'{$v}'" : $v, $bindings);
359+
$bindings = array_map(fn($v) => gettype($v) === 'NULL' ? 'NULL' : $v, $bindings);
360360

361-
$newQuery = join(array_map(fn ($k1, $k2) => $k1.$k2, $partQuery, $bindings));
361+
$newQuery = join(array_map(fn($k1, $k2) => $k1.$k2, $partQuery, $bindings));
362362
$newQuery = str_replace('[]', '', $newQuery);
363363

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

367-
if ($db_charset && $app_charset) {
367+
if($db_charset && $app_charset) {
368368
$newQuery = mb_convert_encoding($newQuery, $db_charset, $app_charset);
369369
}
370370

@@ -389,19 +389,29 @@ public function select($query, $bindings = [], $useReadPdo = true)
389389
return [];
390390
}
391391

392-
$statement = $this->getPdo()->query($this->compileNewQuery(
392+
$statement = $this->getPdo()->prepare($this->compileNewQuery(
393393
$query,
394394
$bindings
395395
));
396396

397-
$result = $statement->fetchAll($this->getFetchMode());
397+
$statement->execute();
398+
399+
$result = [];
400+
401+
try {
402+
do {
403+
$result += $statement->fetchAll($this->getFetchMode());
404+
} while ($statement->nextRowset());
405+
} catch (\Exception $e) {}
406+
407+
$result = [...$result];
398408

399-
$db_charset = env('DB_CHARSET');
400-
$app_charset = env('APPLICATION_CHARSET');
409+
$db_charset = env('SYBASE_DATABASE_CHARSET');
410+
$app_charset = env('SYBASE_APPLICATION_CHARSET');
401411

402-
if ($db_charset && $app_charset) {
403-
foreach ($result as &$r) {
404-
foreach ($r as $k => &$v) {
412+
if($db_charset && $app_charset) {
413+
foreach($result as &$r) {
414+
foreach($r as $k => &$v) {
405415
$v = gettype($v) === 'string' ? mb_convert_encoding($v, $app_charset, $db_charset) : $v;
406416
}
407417
}
@@ -415,7 +425,7 @@ public function select($query, $bindings = [], $useReadPdo = true)
415425
* Get the statement.
416426
*
417427
* @param string $query
418-
* @param mixed|array $bindings
428+
* @param mixed|array $bindings
419429
* @return bool
420430
*/
421431
public function statement($query, $bindings = [])

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)