commit e6b044a0fbbd0f08ca2e7f7f7c2b11f251a82ddd
parent 985c8651434792e6c5c9a8525c8c74e3ce6672ca
Author: Jan Dankert <develop@jandankert.de>
Date: Thu, 19 Nov 2020 10:45:05 +0100
Fix: Default database.
Diffstat:
3 files changed, 23 insertions(+), 21 deletions(-)
diff --git a/modules/cms/Dispatcher.class.php b/modules/cms/Dispatcher.class.php
@@ -23,6 +23,7 @@ use language\Messages;
use util\Cookie;
use util\ClassName;
use util\ClassUtils;
+use util\exception\DatabaseException;
use util\exception\ObjectNotFoundException;
use util\exception\ValidationException;
use util\Http;
@@ -377,17 +378,17 @@ class Dispatcher
*/
private function connectToDatabase()
{
- $dbConfig = Configuration::subset('database');
+ $allDbConfig = Configuration::subset('database');
// Filter all enabled databases
- $databases = array_filter($dbConfig->subsets(), function ($dbConfig) {
+ $enabledDatabases = array_filter($allDbConfig->subsets(), function ($dbConfig) {
return $dbConfig->is('enabled',true);
});
- $dbids = array_keys( $databases );
+ $enabledDbids = array_keys( $enabledDatabases );
- if ( ! $dbids )
- throw new \RuntimeException('No database configured.');
+ if ( ! $enabledDbids )
+ throw new UIException(Messages::DATABASE_CONNECTION_ERROR, 'No database configured.',new DatabaseException('No database configured' ) );
$firstDbContact = ! Session::getDatabaseId();
@@ -404,12 +405,12 @@ class Dispatcher
$possibleDbIds[] = Configuration::subset('database-default')->get('default-id' );
- $possibleDbIds[] = $dbids[0];
+ $possibleDbIds[] = $enabledDbids[0];
foreach( $possibleDbIds as $dbid ) {
- if ( $dbConfig->has( $dbid ) ) {
+ if ( $allDbConfig->has( $dbid ) ) {
- $dbConfig = $dbConfig->subset($dbid );
+ $dbConfig = $allDbConfig->subset( $dbid );
try
{
@@ -422,7 +423,7 @@ class Dispatcher
Session::setDatabase ( $db );
}
catch(\Exception $e) {
- throw new UIException(Messages::DATABASE_CONNECTION_ERROR, $e->getMessage(),$e);
+ throw new UIException(Messages::DATABASE_CONNECTION_ERROR, "Could not connect to DB ".$dbid, $e);
}
diff --git a/modules/database/Database.class.php b/modules/database/Database.class.php
@@ -117,10 +117,12 @@ class Database
*/
public function __construct( $dbconf )
{
- $this->conf = $dbconf +
- C::subset('database-default')->subset('defaults')->getConfig() +
- Database::$DEFAULT_CONFIG;
-
+ $this->conf = array_merge(
+ Database::$DEFAULT_CONFIG, // internal defaults
+ C::subset('database-default')->subset('defaults')->getConfig(), // defaults from config
+ $dbconf // per-connection DB configuration
+ );
+
$this->connect();
}
@@ -133,7 +135,7 @@ class Database
public function connect()
{
// Ausfuehren des Systemkommandos vor Verbindungsaufbau
- if (!empty($this->conf['cmd']))
+ if ( $this->conf['cmd'] )
$this->executeSystemCommand( $this->conf['cmd'] );
// Client instanziieren
@@ -236,9 +238,9 @@ class Database
private function executeSystemCommand( $cmd )
{
$ausgabe = array();
- $rc = false;
+ $rc = false;
- Logger::debug("Database command executing: " . $this->conf['cmd']);
+ Logger::debug("Database command executing: " . $cmd );
exec($cmd, $ausgabe, $rc);
foreach ($ausgabe as $zeile)
@@ -249,7 +251,4 @@ class Database
}
}
-}
-
-
-?>-
\ No newline at end of file
+}+
\ No newline at end of file
diff --git a/modules/database/driver/PDODriver.class.php b/modules/database/driver/PDODriver.class.php
@@ -94,6 +94,8 @@ class PDODriver
},array_keys($dsnParts),$dsnParts));
}
+ // we must have a prefix or suffix
+ // this is because some table names are reserved words in some RDBMS
if ( ! $conf['prefix'] && ! $conf['suffix'] )
throw new DatabaseException('database tables must have a prefix or a suffix, both are empty.');