]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/database/frontend/class_BaseDatabaseFrontend.php
Continued:
[core.git] / framework / main / classes / database / frontend / class_BaseDatabaseFrontend.php
index 2f4320ccf46da12e605b45d8b1c422f0d65a610e..23de87b3c3f7248a24bb9c69ec90519bff7f803c 100644 (file)
@@ -12,6 +12,9 @@ use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
 use Org\Mxchange\CoreFramework\Result\Database\BaseDatabaseResult;
 use Org\Mxchange\CoreFramework\Traits\Cache\CacheableTrait;
 
+// Import SPL stuff
+use \InvalidArgumentException;
+
 /**
  * A generic database frontend
  *
@@ -55,10 +58,14 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
         */
        protected function __construct (string $className) {
                // Call parent constructor
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATABASE-FRONTEND: CONSTRUCTED!');
                parent::__construct($className);
 
                // Initialize the cache instance
                $this->initCacheInstance();
+
+               // Trace message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATABASE-FRONTEND: EXIT!');
        }
 
        /**
@@ -68,13 +75,18 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
         */
        private final function initCacheInstance () {
                // Set "cache" attributes
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATABASE-FRONTEND: CALLED!');
                $this->databaseCacheEnabled = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled');
 
                // Is the cache enabled?
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: this->databaseCacheEnabled=%d', intval($this->databaseCacheEnabled)));
                if ($this->databaseCacheEnabled === true) {
                        // Set the new instance
                        $this->setCacheInstance(ObjectFactory::createObjectByConfiguredName('cache_class'));
                }
+
+               // Trace message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATABASE-FRONTEND: EXIT!');
        }
 
        /**
@@ -105,7 +117,7 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
         */
        protected function getCacheKeyByCriteria (Criteria $criteriaInstance, array $onlyKeys = []) {
                // Generate it
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FRAMEWORK-SYSTEM: criteriaInstance=' . $criteriaInstance->__toString() . ',onlyKeys()=' . count($onlyKeys) . ' - CALLED!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: criteriaInstance=%s,onlyKeys()=%d - CALLED!', $criteriaInstance->__toString(), count($onlyKeys)));
                $cacheKey = sprintf('%s@%s',
                        $this->__toString(),
                        $criteriaInstance->getCacheKey($onlyKeys)
@@ -125,21 +137,22 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
         */
        protected function queryInsertDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = []) {
                // Default cache key is NULL
-               //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: dataSetInstance=%s,onlyKeys()=%d - CALLED!', $dataSetInstance->__toString(), count($onlyKeys)));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: dataSetInstance=%s,onlyKeys()=%d - CALLED!', $dataSetInstance->__toString(), count($onlyKeys)));
                $cacheKey = NULL;
 
                // Is cache enabled?
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: this->databaseCacheEnabled=%d', intval($this->databaseCacheEnabled)));
                if ($this->databaseCacheEnabled === true) {
                        // First get a key suitable for our cache and extend it with this class name
                        $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
-                       //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FRONTEND: Using cache key ' . $cacheKey . ' for purging ...');
+                       //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: Using cache key %s for purging ...', $cacheKey));
                }
 
                // Does this key exists in cache?
-               //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: cacheKey[%s]=%s', gettype($cacheKey), $cacheKey));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: this->databaseCacheEnabled=%d,cacheKey[%s]=%s', intval($this->databaseCacheEnabled), gettype($cacheKey), $cacheKey));
                if (($this->databaseCacheEnabled === true) && ($this->getCacheInstance()->offsetExists($cacheKey))) {
                        // Purge the cache
-                       //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: Calling this->cacheInstance->purgeOffset(%s) ...', $cacheKey));
+                       //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: Invoking this->cacheInstance->purgeOffset(%s) ...', $cacheKey));
                        $this->getCacheInstance()->purgeOffset($cacheKey);
                }
 
@@ -147,7 +160,7 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
                FrameworkBootstrap::getDatabaseInstance()->queryInsertDataSet($dataSetInstance);
 
                // Trace message
-               //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FRONTEND: EXIT!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATABASE-FRONTEND: EXIT!');
        }
 
        /**
@@ -159,16 +172,19 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
         */
        protected function queryUpdateDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = []) {
                // Init cache key
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: dataSetInstance=%s,onlyKeys()=%d - CALLED!', $dataSetInstance->__toString(), count($onlyKeys)));
                $cacheKey = NULL;
 
                // Is cache enabled?
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: this->databaseCacheEnabled=%d', intval($this->databaseCacheEnabled)));
                if ($this->databaseCacheEnabled === true) {
                        // First get a key suitable for our cache and extend it with this class name
                        $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
-                       //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FRONTEND: Using cache key ' . $cacheKey . ' for purging ...');
+                       //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATABASE-FRONTEND: Using cache key ' . $cacheKey . ' for purging ...');
                }
 
                // Does this key exists in cache?
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: this->databaseCacheEnabled=%d,cacheKey=%s', intval($this->databaseCacheEnabled), $cacheKey));
                if (($this->databaseCacheEnabled === true) && ($this->getCacheInstance()->offsetExists($cacheKey))) {
                        // Purge the cache
                        $this->getCacheInstance()->purgeOffset($cacheKey);
@@ -176,6 +192,9 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
 
                // Handle it over to the middleware
                FrameworkBootstrap::getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
+
+               // Trace message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATABASE-FRONTEND: EXIT!');
        }
 
        /**
@@ -184,6 +203,7 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
         * @return      $indexKey       Index key
         */
        public final function getIndexKey () {
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATABASE-FRONTEND: CALLED!');
                return FrameworkBootstrap::getDatabaseInstance()->getIndexKey();
        }
 
@@ -193,6 +213,7 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
         * @return      $lastException  Last exception or NULL if none occured
         */
        public final function getLastException () {
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATABASE-FRONTEND: CALLED!');
                return FrameworkBootstrap::getDatabaseInstance()->getLastException();
        }
 
@@ -206,34 +227,36 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
         */
        public function doSelectByCriteria (Criteria $criteriaInstance, array $onlyKeys = []) {
                // Default cache key if cache is not enabled
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: criteriaInstance=%s,onlyKeys()=%d - CALLED!', $criteriaInstance->__toString(), count($onlyKeys)));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: criteriaInstance=%s,onlyKeys()=%d - CALLED!', $criteriaInstance->__toString(), count($onlyKeys)));
                $cacheKey = NULL;
                $result = [];
 
                // Is the cache enabled?
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: this->databaseCacheEnabled=%d', intval($this->databaseCacheEnabled)));
                if ($this->databaseCacheEnabled === true) {
                        // First get a key suitable for our cache and extend it with this class name
                        $cacheKey = $this->getCacheKeyByCriteria($criteriaInstance, $onlyKeys);
                }
 
                // Does this key exists in cache?
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: cacheKey[%s]=%s', gettype($cacheKey), $cacheKey));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: this->databaseCacheEnabled=%d,cacheKey[%s]=%s', intval($this->databaseCacheEnabled), gettype($cacheKey), $cacheKey));
                if (($this->databaseCacheEnabled === true) && ($this->getCacheInstance()->offsetExists($cacheKey, BaseDatabaseResult::RESULT_NAME_ROWS, 1))) {
                        // Then use this result
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: Cache used for cacheKey=%s', $cacheKey));
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: Cache used for cacheKey=%s', $cacheKey));
                        $result = $this->getCacheInstance()->offsetGet($cacheKey);
                } else {
                        // Now it's time to ask the database layer for this select statement
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: Quering database, cacheKey=%s ...', $cacheKey));
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: Quering database, cacheKey=%s ...', $cacheKey));
                        $result = FrameworkBootstrap::getDatabaseInstance()->doSelectByTableCriteria($this->getTableName(), $criteriaInstance);
 
                        // Cache the result if not null
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: result[]=%s', gettype($result)));
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: result[]=%s', gettype($result)));
                        if (!is_null($result)) {
                                // Is cache enabled?
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: this->databaseCacheEnabled=%d', intval($this->databaseCacheEnabled)));
                                if ($this->databaseCacheEnabled === true) {
                                        // A valid result has returned from the database layer
-                                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: Setting cacheKey=%s with result()=%d entries', $cacheKey, count($result)));
+                                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: Setting cacheKey=%s with result()=%d entries', $cacheKey, count($result)));
                                        $this->getCacheInstance()->offsetSet($cacheKey, $result);
                                }
                        } else {
@@ -247,11 +270,11 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
 
                // Create an instance of a CachedDatabaseResult class with the given result
                // @TODO Minor: Update above comment to e.g. BaseDatabaseResult
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: result[%s]=%s,result[%s]?=%d,result[%s]?=%d', BaseDatabaseResult::RESULT_NAME_STATUS, $result[BaseDatabaseResult::RESULT_NAME_STATUS], BaseDatabaseResult::RESULT_NAME_ROWS, isset($result[BaseDatabaseResult::RESULT_NAME_ROWS]), BaseDatabaseResult::RESULT_NAME_EXCEPTION, isset($result[BaseDatabaseResult::RESULT_NAME_EXCEPTION])));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: result[%s]=%s,result[%s]?=%d,result[%s]?=%d', BaseDatabaseResult::RESULT_NAME_STATUS, $result[BaseDatabaseResult::RESULT_NAME_STATUS], BaseDatabaseResult::RESULT_NAME_ROWS, isset($result[BaseDatabaseResult::RESULT_NAME_ROWS]), BaseDatabaseResult::RESULT_NAME_EXCEPTION, isset($result[BaseDatabaseResult::RESULT_NAME_EXCEPTION])));
                $resultInstance = ObjectFactory::createObjectByConfiguredName('database_result_class', array($result));
 
                // And return the instance
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: resultInstance=%s - EXIT!', $resultInstance->__toString()));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: resultInstance=%s - EXIT!', $resultInstance->__toString()));
                return $resultInstance;
        }
 
@@ -264,34 +287,36 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
         */
        public function doSelectCountByCriteria (Criteria $criteriaInstance, array $onlyKeys = []) {
                // Total numbers is -1 so we can distinglish between failed and valid queries
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: criteriaInstance=%s,onlyKeys()=%d - CALLED!', $criteriaInstance->__toString(), count($onlyKeys)));
                $numRows = 0;
 
                // Get the result from above method
                $resultInstance = $this->doSelectByCriteria($criteriaInstance, $onlyKeys);
 
                // Was that query fine?
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: resultInstance->ifStatusIsOkay()=%d', $resultInstance->ifStatusIsOkay()));
                if ($resultInstance->ifStatusIsOkay()) {
                        // Then get the number of rows
                        $numRows = $resultInstance->getAffectedRows();
-
-                       // Debug message
-                       //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FRONTEND: numRows=' . $numRows);
                }
 
                // Return the result
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: numRows=%d - EXIT!', $numRows));
                return $numRows;
        }
 
        /**
-        * Getter for primary key used in wrapped table
+        * Generates a primary key for this database frontend
         *
         * @return      $primaryKey             Primary key used in wrapped table
         */
-       public final function getPrimaryKeyValue () {
+       public final function generatePrimaryKey () {
                // Get the table name and a database instance and ask for it
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATABASE-FRONTEND: CALLED!');
                $primaryKey = FrameworkBootstrap::getDatabaseInstance()->getPrimaryKeyOfTable($this->getTableName());
 
                // Return value
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: primaryKey=%s - EXIT!', $primaryKey));
                return $primaryKey;
        }
 
@@ -302,9 +327,11 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
         */
        public final function countTotalRows () {
                // Get the table name and a database instance and ask for it
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATABASE-FRONTEND: CALLED!');
                $count = FrameworkBootstrap::getDatabaseInstance()->countTotalRows($this->getTableName());
 
                // Return value
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: count=%d - EXIT!', $count));
                return $count;
        }
 
@@ -313,12 +340,21 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
         *
         * @param       $data   An array with possible non-public data that needs to be removed.
         * @return      $data   A cleaned up array with only public data.
+        * @throws      InvalidArgumentException        If a parameter has an invalid value
         */
        public function removeNonPublicDataFromArray (array $data) {
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('WRAPPER[' . $this->__toString() . ']: Calling FrameworkBootstrap::getDatabaseInstance()->removeNonPublicDataFromArray(data) ...');
+               // Check parameter
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: data()=%d - CALLED!', count($data)));
+               if (count($data) == 0) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "data" is an empty array', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               }
+
+               // Remove non-public data (aka. sensitive)
                $data = FrameworkBootstrap::getDatabaseInstance()->removeNonPublicDataFromArray($data);
 
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('WRAPPER[' . $this->__toString() . ']: data[]=' . gettype($data));
+               // Return cleaned array
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DATABASE-FRONTEND: data()=%d - EXIT!', count($data)));
                return $data;
        }