Skip to content

Commit f89f9e4

Browse files
authored
Merge pull request #100 from andrex47/master
fix for running migrations with sybase as default connection
2 parents 7dfd3eb + 0aaf6ac commit f89f9e4

File tree

3 files changed

+76
-29
lines changed

3 files changed

+76
-29
lines changed

README.md

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ Update your **config/database.php's** default driver with the settings for the *
4747
return [
4848
...
4949

50-
'sybase' =>
51-
[
52-
'application_encoding' => false,
53-
'application_charset' => '',
54-
'database_charset' => ''
55-
],
5650

5751
'connections' => [
5852
...
@@ -67,7 +61,9 @@ return [
6761
'charset' => 'utf8',
6862
'prefix' => '',
6963
'cache_tables' => true,
70-
'cache_time' => 3600
64+
'cache_time' => 3600,
65+
'application_encoding' => false,
66+
'application_charset' => '',
7167
],
7268

7369
...
@@ -107,18 +103,13 @@ The file is usualy found in **/etc/freetds/freetds.conf**. Set the configuration
107103
## Configuring the charset conversion
108104
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:
109105

110-
```database
111-
'sybase' =>
112-
[
113-
'application_encoding' => true,
114-
'application_charset' => '',
115-
'database_charset' => ''
116-
],
117106
```
118107
To use the database layer conversion add the property charset to connection configuration on the sybase configuration array
119108
120109
```charset
121110
'charset' => 'utf8',
111+
'application_encoding' => false,
112+
'application_charset' => '',
122113
```
123114

124115

src/Database/Connection.php

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,54 @@ class Connection extends IlluminateConnection
3939
'money',
4040
];
4141

42+
43+
/**
44+
* @var string The application charset
45+
*/
46+
private $applicationCharset;
47+
48+
/**
49+
* @var string The database charset
50+
*/
51+
private $databaseCharset;
52+
53+
private $applicationEncoding = false;
54+
55+
/**
56+
* Create a new database connection instance.
57+
*
58+
* @param \PDO|\Closure $pdo
59+
* @param string $database
60+
* @param string $tablePrefix
61+
* @param array $config
62+
* @return void
63+
*/
64+
public function __construct($pdo, $database = '', $tablePrefix = '', array $config = [])
65+
{
66+
parent::__construct($pdo, $database, $tablePrefix, $config);
67+
$this->configureExtraSettings($config);
68+
}
69+
70+
/**
71+
* Configures the encoding for the connection.
72+
*
73+
* @param array $config
74+
* @return void
75+
*/
76+
public function configureExtraSettings($config = [])
77+
{
78+
$this->applicationEncoding = key_exists('application_encoding',$config) ? $config['application_encoding'] : false;
79+
if ($this->applicationEncoding)
80+
{
81+
if (!key_exists('application_charset',$config,) || !key_exists('database_charset',$config))
82+
{
83+
throw new \Exception('When application encoding is configured, you need to set up application_charset and database_charset');
84+
}
85+
$this->applicationCharset = $config['application_charset'];
86+
$this->databaseCharset = $config['charset'];
87+
}
88+
}
89+
4290
/**
4391
* Execute a Closure within a transaction.
4492
*
@@ -67,7 +115,7 @@ public function transaction(Closure $callback, $attempts = 1)
67115
// If we catch an exception, we will roll back so nothing gets messed
68116
// up in the database. Then we'll re-throw the exception so it can
69117
// be handled how the developer sees fit for their applications.
70-
catch (Exception $e) {
118+
catch (\Exception|\PDOException $e) {
71119
$this->pdo->exec('ROLLBACK TRAN');
72120

73121
throw $e;
@@ -146,7 +194,7 @@ private function compile(Builder $builder)
146194
}
147195
}
148196

149-
$cache = $builder->connection->config['cache_tables'];
197+
$cache = key_exists('cache_tables',$builder->connection->config) ? $builder->connection->config['cache_tables'] : false;
150198
$types = [];
151199

152200
foreach ($arrTables as $tables) {
@@ -244,7 +292,6 @@ private function compile(Builder $builder)
244292
}
245293
}
246294
}
247-
248295
return $keys;
249296
}
250297

@@ -335,12 +382,12 @@ private function compileNewQuery($query, $bindings)
335382

336383
$newQuery = join(array_map(fn ($k1, $k2) => $k1.$k2, $partQuery, $bindings));
337384
$newQuery = str_replace('[]', '', $newQuery);
338-
$application_encoding = config('database.sybase.application_encoding');
385+
$application_encoding = $this->applicationEncoding;
339386
if (is_null($application_encoding) || $application_encoding == false) {
340387
return $newQuery;
341388
}
342-
$database_charset = config('database.sybase.database_charset');
343-
$application_charset = config('database.sybase.application_charset');
389+
$database_charset = $this->databaseCharset;
390+
$application_charset = $this->applicationCharset;
344391
if (is_null($database_charset) || is_null($application_charset)) {
345392
throw new \Exception('[SYBASE] Database Charset and App Charset not set');
346393
}
@@ -376,21 +423,18 @@ public function select($query, $bindings = [], $useReadPdo = true)
376423

377424
$result = [];
378425

379-
try {
380-
do {
381-
$result += $statement->fetchAll($this->getFetchMode());
382-
} while ($statement->nextRowset());
383-
} catch (\Exception $e) {
384-
}
426+
do {
427+
$result += $statement->fetchAll($this->getFetchMode());
428+
} while ($statement->nextRowset());
385429

386430
$result = [...$result];
387431

388-
$application_encoding = config('database.sybase.application_encoding');
432+
$application_encoding = $this->applicationEncoding;
389433
if (is_null($application_encoding) || $application_encoding == false) {
390434
return $result;
391435
}
392-
$database_charset = config('database.sybase.database_charset');
393-
$application_charset = config('database.sybase.application_charset');
436+
$database_charset = $this->databaseCharset;
437+
$application_charset = $this->applicationCharset;
394438
if (is_null($database_charset) || is_null($application_charset)) {
395439
throw new \Exception('[SYBASE] Database Charset and App Charset not set');
396440
}

src/Database/Schema/Grammar.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,18 @@ public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
264264
) DROP TABLE ".$blueprint->getTable();
265265
}
266266

267+
public function compileTables()
268+
{
269+
return "select
270+
o.name as name,
271+
user_name(o.uid) as [schema],
272+
cast(reserved_pages(db_id(), o.id) as bigint) * @@maxpagesize as size_bytes
273+
from sysobjects o
274+
where o.type = 'U'
275+
order by o.name
276+
";
277+
}
278+
267279
/**
268280
* Compile a drop column command.
269281
*

0 commit comments

Comments
 (0)