Skip to content

Commit bd93a75

Browse files
andrehankeandrehanke
authored andcommitted
enhanced: added compileTables and changed config behaviour for application encoding
1 parent f5106c7 commit bd93a75

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

src/Database/Connection.php

Lines changed: 55 additions & 7 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['database_charset'];
87+
}
88+
}
89+
4290
/**
4391
* Execute a Closure within a transaction.
4492
*
@@ -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) {
@@ -334,12 +382,12 @@ private function compileNewQuery($query, $bindings)
334382

335383
$newQuery = join(array_map(fn ($k1, $k2) => $k1.$k2, $partQuery, $bindings));
336384
$newQuery = str_replace('[]', '', $newQuery);
337-
$application_encoding = config('database.sybase.application_encoding');
385+
$application_encoding = $this->applicationEncoding;
338386
if (is_null($application_encoding) || $application_encoding == false) {
339387
return $newQuery;
340388
}
341-
$database_charset = config('database.sybase.database_charset');
342-
$application_charset = config('database.sybase.application_charset');
389+
$database_charset = $this->databaseCharset;
390+
$application_charset = $this->applicationCharset;
343391
if (is_null($database_charset) || is_null($application_charset)) {
344392
throw new \Exception('[SYBASE] Database Charset and App Charset not set');
345393
}
@@ -381,12 +429,12 @@ public function select($query, $bindings = [], $useReadPdo = true)
381429

382430
$result = [...$result];
383431

384-
$application_encoding = config('database.sybase.application_encoding');
432+
$application_encoding = $this->applicationEncoding;
385433
if (is_null($application_encoding) || $application_encoding == false) {
386434
return $result;
387435
}
388-
$database_charset = config('database.sybase.database_charset');
389-
$application_charset = config('database.sybase.application_charset');
436+
$database_charset = $this->databaseCharset;
437+
$application_charset = $this->applicationCharset;
390438
if (is_null($database_charset) || is_null($application_charset)) {
391439
throw new \Exception('[SYBASE] Database Charset and App Charset not set');
392440
}

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)