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
33 changes: 10 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,9 @@

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

### Laravel 5.1, 5.2, 5.3

```json
"uepg/laravel-sybase": "~1.0"
```
### Laravel 5.4, 5.5, 5.6, 5.7, 5.8, 6.x, 7.x, 8.x, 9.x

```json
"uepg/laravel-sybase": "~2.0"
```

### Laravel 10.x

### Laravel >=7.x
```json
"uepg/laravel-sybase": "~3.0" // old version
//or The new version
"uepg/laravel-sybase": "~4.0" // new version
"xbu3n0/laravel-sybase": "~4.0"
```

Update the package dependencies executing:
Expand Down Expand Up @@ -71,8 +57,9 @@ return [
'database' => env('DB_DATABASE', 'mydatabase'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'password'),
'charset' => 'utf8', // Experimental yet, prefer use the `DB_CHARSET` and `APPLICATION_CHARSET`
'charset' => 'utf8',
'prefix' => '',
'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])
],

...
Expand Down Expand Up @@ -110,19 +97,19 @@ The file is usualy found in **/etc/freetds/freetds.conf**. Set the configuration
```

## Configuring the charset between the database and the application
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:
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:

```env
DB_CHARSET=CP850
APPLICATION_CHARSET=UTF8
SYBASE_DATABASE_CHARSET=CP850
SYBASE_APPLICATION_CHARSET=UTF8
```
## Configuring the cache
As the library consults table information whenever it receives a request, caching can be used to avoid excessive queries

To use the cache, add the fields `SYBASE_CACHE_COLUMNS` and `SYBASE_CACHE_COLUMNS_TIME` to the `.env` file, see the following example:
To use the cache, add the fields `SYBASE_CACHE_TABLES` and `SYBASE_CACHE_TABLES_TIME` to the `.env` file, see the following example:
```dotenv
SYBASE_CACHE_COLUMNS=true
SYBASE_CACHE_COLUMNS_TIME=3600 # cache table information by `3600` seconds
SYBASE_CACHE_TABLES=true
SYBASE_CACHE_TABLES_TIME=3600 # cache table information by `3600` seconds
```

## Setting to use numeric data type
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
"wiki": "https://github.com/uepg/laravel-sybase/wiki"
},
"require": {
"php": "^8.1",
"php": ">=8.1",
"doctrine/dbal": "^3.5.1",
"illuminate/database": "^10",
"illuminate/support": "^10",
"illuminate/database": ">=8.0",
"illuminate/support": ">=8.0",
"ext-pdo": "*"
},
"require-dev": {
Expand Down
36 changes: 26 additions & 10 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ private function compile(Builder $builder)
}
}

$cache_columns = env('SYBASE_CACHE_COLUMNS');
$cache_tables = env('SYBASE_CACHE_TABLES');
$cache = ! key_exists('cache_tables', $builder->connection->config) || $builder->connection->config['cache_tables'];

$types = [];

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

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

Expand Down Expand Up @@ -255,7 +258,8 @@ private function compile(Builder $builder)
*/
private function queryString($tables)
{
$explicitDB = explode('..', $tables);
$tables = str_replace('..', '.dbo.', $tables);
$explicitDB = explode('.dbo.', $tables);

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

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

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

$db_charset = env('DB_CHARSET');
$app_charset = env('APPLICATION_CHARSET');
$db_charset = env('SYBASE_DATABASE_CHARSET');
$app_charset = env('SYBASE_APPLICATION_CHARSET');

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

$statement = $this->getPdo()->query($this->compileNewQuery(
$statement = $this->getPdo()->prepare($this->compileNewQuery(
$query,
$bindings
));

$result = $statement->fetchAll($this->getFetchMode());
$statement->execute();

$result = [];

try {
do {
$result += $statement->fetchAll($this->getFetchMode());
} while ($statement->nextRowset());
} catch (\Exception $e) {
}

$result = [...$result];

$db_charset = env('DB_CHARSET');
$app_charset = env('APPLICATION_CHARSET');
$db_charset = env('SYBASE_DATABASE_CHARSET');
$app_charset = env('SYBASE_APPLICATION_CHARSET');

if ($db_charset && $app_charset) {
foreach ($result as &$r) {
Expand Down
8 changes: 6 additions & 2 deletions src/Database/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ public function connect(array $config)

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

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

$this->configureIsolationLevel($connection, $config);
if (isset($config['isolation_level'])) {
$connection->prepare(
"SET TRANSACTION ISOLATION LEVEL {$config['isolation_level']}"
)->execute();
}

return $connection;
}
Expand Down