]> git.mxchange.org Git - core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Sun, 6 Dec 2020 19:10:29 +0000 (20:10 +0100)
committerRoland Häder <roland@mxchange.org>
Sun, 6 Dec 2020 19:12:57 +0000 (20:12 +0100)
- renamed DatabaseConnection->$dbLayer to $backendInstance (old-lost name)
- added more checks on empty string parameter and throw an IAE

Signed-off-by: Roland Häder <roland@mxchange.org>
framework/main/classes/criteria/dataset/class_DataSetCriteria.php
framework/main/classes/database/backend/class_BaseDatabaseBackend.php
framework/main/classes/database/backend/lfdb_legacy/class_CachedLocalFileDatabase.php
framework/main/classes/database/class_BaseDatabaseResult.php
framework/main/classes/database/frontend/class_BaseDatabaseFrontend.php
framework/main/classes/database/result/class_CachedDatabaseResult.php
framework/main/middleware/database/class_DatabaseConnection.php

index 0547f50135e4616e7b6e4da7c9daf789edc35b23..8aa63cb7ff58789dbe255d783639e0db896a61d3 100644 (file)
@@ -8,6 +8,9 @@ use Org\Mxchange\CoreFramework\Criteria\Local\LocalSearchCriteria;
 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
 use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
 
+// Import SPL stuff
+use \InvalidArgumentException;
+
 /**
  * A set of data storeable in databases
  *
@@ -71,8 +74,15 @@ class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
         *
         * @param       $tableName                      Name of the table
         * @return      $criteriaInstance       An instance of this criteria
+        * @throws      InvalidArgumentException        If a parameter is not valid
         */
        public static final function createDataSetCriteria (string $tableName) {
+               // Validate parameter
+               if (empty($tableName)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "tableName" is empty');
+               }
+
                // Get a new instance
                $criteriaInstance = new DataSetCriteria();
 
index a9305968c02a6035d981f726ef488708371430fb..3baa8181d8a13cec19042322de7def885535090c 100644 (file)
@@ -34,11 +34,6 @@ abstract class BaseDatabaseBackend extends BaseFrameworkSystem implements Databa
        // Constants for exceptions
        const EXCEPTION_SQL_QUERY = 0x140;
 
-       // Result array indexes
-       const RESULT_INDEX_ROWS      = 'rows';
-       const RESULT_INDEX_STATUS    = 'status';
-       const RESULT_INDEX_EXCEPTION = 'exception';
-
        // Constants for MySQL backward-compatiblity (PLEASE FIX THEM!)
        const DB_CODE_TABLE_MISSING     = 0x100;
        const DB_CODE_TABLE_UNWRITEABLE = 0x101;
index 37426c86d83891a0d974dc2706eb4ce4272617d7..7bdd9d4083f7b0c1615d2cacf6d61ea62c508062 100644 (file)
@@ -12,10 +12,12 @@ use Org\Mxchange\CoreFramework\Database\Backend\DatabaseBackend;
 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
 use Org\Mxchange\CoreFramework\Filesystem\FileNotFoundException;
 use Org\Mxchange\CoreFramework\Generic\FrameworkException;
+use Org\Mxchange\CoreFramework\Result\Database\BaseDatabaseResult;
 use Org\Mxchange\CoreFramework\Traits\Compressor\Channel\CompressorChannelTrait;
 use Org\Mxchange\CoreFramework\Traits\Handler\Io\IoHandlerTrait;
 
 // Import SPL stuff
+use \InvalidArgumentException;
 use \SplFileInfo;
 
 /**
@@ -350,12 +352,19 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
         * @param       $tableName                      Name of the database table
         * @param       $searchInstance         Local search criteria class
         * @return      $resultData                     Result data of the query
+        * @throws      InvalidArgumentException        If a parameter is not valid
         * @throws      UnsupportedCriteriaException    If the criteria is unsupported
         * @throws      SqlException                                    If an 'SQL error' occurs
         */
        public function querySelect (string $tableName, LocalSearchCriteria $searchInstance) {
-               // The result is null by any errors
+               // Validate parameter
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: tableName=%s,searchInstance=%s - CALLED!', $tableName, $searchInstance->__toString()));
+               if (empty($tableName)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "tableName" is empty');
+               }
+
+               // The result is null by any errors
                $resultData = NULL;
 
                // Create full path name
@@ -371,10 +380,10 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                        $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
 
                        // Initialize the result data, this need to be rewritten e.g. if a local file cannot be read
-                       $resultData = array(
-                               BaseDatabaseBackend::RESULT_INDEX_STATUS => self::RESULT_OKAY,
-                               BaseDatabaseBackend::RESULT_INDEX_ROWS   => []
-                       );
+                       $resultData = [
+                               BaseDatabaseResult::RESULT_NAME_STATUS => self::RESULT_OKAY,
+                               BaseDatabaseResult::RESULT_NAME_ROWS   => []
+                       ];
 
                        // Initialize limit/skip
                        $limitFound = 0;
@@ -390,7 +399,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Skipping fileInfoInstance->filename=%s ...', $fileInfoInstance->getFilename()));
                                        $directoryInstance->getDirectoryIteratorInstance()->next();
                                        continue;
-                               } // END - if
+                               }
 
                                // Read the file
                                $dataArray = $this->getDataArrayFromFile($fileInfoInstance);
@@ -410,7 +419,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                                                // Found one entry?
                                                $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
                                                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: key=%s,value[%s]=%s,isFound=%s', $key, gettype($value), $value, intval($isFound)));
-                                       } // END - foreach
+                                       }
 
                                        // Is all found?
                                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: isFound=%d,limitFound=%d,limit=%d', intval($isFound), $limitFound, $searchInstance->getLimit()));
@@ -424,20 +433,20 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                                                                // Skip this entry
                                                                $skipFound++;
                                                                break;
-                                                       } // END - if
-                                               } // END - if
+                                                       }
+                                               }
 
                                                // Set id number
                                                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Setting dataArray[%s]=%d', $this->getIndexKey(), $idx));
                                                $dataArray[$this->getIndexKey()] = $idx;
 
                                                // Entry found!
-                                               array_push($resultData[BaseDatabaseBackend::RESULT_INDEX_ROWS], $dataArray);
+                                               array_push($resultData[BaseDatabaseResult::RESULT_NAME_ROWS], $dataArray);
 
                                                // Count found entries up
-                                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: resultData[%s]()=%d', BaseDatabaseBackend::RESULT_INDEX_ROWS, count($resultData[BaseDatabaseBackend::RESULT_INDEX_ROWS])));
+                                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: resultData[%s]()=%d', BaseDatabaseResult::RESULT_NAME_ROWS, count($resultData[BaseDatabaseResult::RESULT_NAME_ROWS])));
                                                $limitFound++;
-                                       } // END - if
+                                       }
                                } else {
                                        // Throw an exception here
                                        throw new SqlException(array($this, sprintf('File &#39;%s&#39; contains invalid data.', $fileInfoInstance->getPathname()), self::DB_CODE_DATA_FILE_CORRUPT), self::EXCEPTION_SQL_QUERY);
@@ -448,7 +457,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
 
                                // Advance to next entry
                                $directoryInstance->getDirectoryIteratorInstance()->next();
-                       } // END - while
+                       }
 
                        // Close directory and throw the instance away
                        $directoryInstance->closeDirectory();
@@ -546,7 +555,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
                                        $directoryInstance->getDirectoryIteratorInstance()->next();
                                        continue;
-                               } // END - if
+                               }
 
                                // Open this file for reading
                                $dataArray = $this->getDataArrayFromFile($fileInfoInstance);
@@ -564,7 +573,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
 
                                                // Found one entry?
                                                $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
-                                       } // END - foreach
+                                       }
 
                                        // Is all found?
                                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: isFound=' . intval($isFound));
@@ -577,8 +586,8 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                                                                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Found entry, but skipping ...');
                                                                $skipFound++;
                                                                break;
-                                                       } // END - if
-                                               } // END - if
+                                                       }
+                                               }
 
                                                // Entry found, so update it
                                                foreach ($searchArray as $searchKey => $searchValue) {
@@ -589,7 +598,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                                                        // Debug message + add/update it
                                                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue);
                                                        $dataArray[$searchKey] = $searchValue;
-                                               } // END - foreach
+                                               }
 
                                                // Write the data to a local file
                                                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: Writing data[]=' . count($dataArray) . ' to ' . $fileInfoInstance->getPathname() . ' ...');
@@ -597,12 +606,12 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
 
                                                // Count found entries up
                                                $limitFound++;
-                                       } // END - if
-                               } // END - if
+                                       }
+                               }
 
                                // Advance to next entry
                                $directoryInstance->getDirectoryIteratorInstance()->next();
-                       } // END - while
+                       }
 
                        // Close the file pointer
                        $directoryInstance->closeDirectory();
@@ -627,9 +636,16 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
         *
         * @param       $tableName              Name of the table we need the primary key from
         * @return      $primaryKey             Primary key column of the given table
+        * @throws      InvalidArgumentException        If a parameter is not valid
         * @todo        Rename method to getPrimaryKeyFromTableInfo()
         */
        public function getPrimaryKeyOfTable (string $tableName) {
+               // Validate parameter
+               if (empty($tableName)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "tableName" is empty');
+               }
+
                // Default key is null
                $primaryKey = NULL;
 
@@ -637,7 +653,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                if (isset($this->tableInfo[$tableName])) {
                        // Then return the primary key
                        $primaryKey = $this->tableInfo[$tableName]['primary'];
-               } // END - if
+               }
 
                // Return the column
                return $primaryKey;
@@ -664,14 +680,22 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
         *
         * @param       $tableName      Table name
         * @return      $count          Total rows of given table
+        * @throws      InvalidArgumentException        If a parameter is not valid
         */
        public function countTotalRows (string $tableName) {
+               // Validate parameter
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: tableName=' . $tableName . ' - CALLED!');
+               if (empty($tableName)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "tableName" is empty');
+               }
+
 
                // Create full path name
                $pathName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
 
                // Try all the requests
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: pathName=' . $pathName);
                try {
                        // Get a file pointer instance
                        $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
@@ -688,12 +712,12 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.extension=' . $fileInfoInstance->getExtension() . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
                                        // Skip this file!
                                        continue;
-                               } // END - if
+                               }
 
                                // Count this row up
                                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: fileInfoInstance.pathname=' . $fileInfoInstance->getPathname() . ',getFileExtension()=' . $this->getFileExtension() . ' - COUNTED!');
                                $count++;
-                       } // END - while
+                       }
                } catch (FrameworkException $e) {
                        // Catch all exceptions and store them in last error
                        $this->setLastException($e);
index 2b30e9e18371e4b40e30935a53e283ac9619fa8f..463cad568f14d2518ba1b3b497ef90ff166332d2 100644 (file)
@@ -30,6 +30,11 @@ use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 abstract class BaseDatabaseResult extends BaseFrameworkSystem {
+       // Result array names
+       const RESULT_NAME_ROWS      = 'rows';
+       const RESULT_NAME_STATUS    = 'status';
+       const RESULT_NAME_EXCEPTION = 'exception';
+
        /**
         * Update criteria instance
         */
index 1201949f9ad24ba3ce514c5e400ab1ad82a3a54b..4a40b69fa325cc8e793ddbde9324ab884eb68faf 100644 (file)
@@ -9,6 +9,7 @@ use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
 use Org\Mxchange\CoreFramework\Database\Backend\BaseDatabaseBackend;
 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
+use Org\Mxchange\CoreFramework\Result\Database\BaseDatabaseResult;
 use Org\Mxchange\CoreFramework\Traits\Cache\CacheableTrait;
 
 /**
@@ -190,7 +191,7 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
 
                // Does this key exists in cache?
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: cacheKey[%s]=%s', gettype($cacheKey), $cacheKey));
-               if ((FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) && ($this->getCacheInstance()->offsetExists($cacheKey, BaseDatabaseBackend::RESULT_INDEX_ROWS, 1))) {
+               if ((FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === 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));
                        $result = $this->getCacheInstance()->offsetGet($cacheKey);
@@ -211,15 +212,15 @@ abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
                        } else {
                                // This invalid result must be wrapped
                                $result = array(
-                                       BaseDatabaseBackend::RESULT_INDEX_STATUS    => 'invalid',
-                                       BaseDatabaseBackend::RESULT_INDEX_EXCEPTION => FrameworkBootstrap::getDatabaseInstance()->getLastException()
+                                       BaseDatabaseResult::RESULT_NAME_STATUS    => 'invalid',
+                                       BaseDatabaseResult::RESULT_NAME_EXCEPTION => FrameworkBootstrap::getDatabaseInstance()->getLastException()
                                );
                        }
                }
 
                // 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', BaseDatabaseBackend::RESULT_INDEX_STATUS, $result[BaseDatabaseBackend::RESULT_INDEX_STATUS], BaseDatabaseBackend::RESULT_INDEX_ROWS, isset($result[BaseDatabaseBackend::RESULT_INDEX_ROWS]), BaseDatabaseBackend::RESULT_INDEX_EXCEPTION, isset($result[BaseDatabaseBackend::RESULT_INDEX_EXCEPTION])));
+               //* 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])));
                $resultInstance = ObjectFactory::createObjectByConfiguredName('database_result_class', array($result));
 
                // And return the instance
index 03af7c2c702e9cc507fb03084bbbddb5258b241c..664270e653d7821513f33c1da6ea04e8e1a40cd9 100644 (file)
@@ -8,6 +8,7 @@ use Org\Mxchange\CoreFramework\Criteria\Local\LocalUpdateCriteria;
 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
 use Org\Mxchange\CoreFramework\Database\Frontend\DatabaseFrontend;
 use Org\Mxchange\CoreFramework\Database\Backend\BaseDatabaseBackend;
+use Org\Mxchange\CoreFramework\Result\Database\BaseDatabaseResult;
 use Org\Mxchange\CoreFramework\Result\Search\SearchableResult;
 use Org\Mxchange\CoreFramework\Result\Update\UpdateableResult;
 
@@ -94,9 +95,9 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul
                if (count($resultArray) == 0) {
                        // Cannot be empty
                        throw new InvalidArgumentException('Array "resultArray" cannot be empty.');
-               } elseif (!array_key_exists(BaseDatabaseBackend::RESULT_INDEX_ROWS, $resultArray)) {
+               } elseif (!array_key_exists(BaseDatabaseResult::RESULT_NAME_ROWS, $resultArray)) {
                        // Yes, then abort here
-                       throw new InvalidArgumentException(sprintf('resultArray(%d)=%s has no element "%s".', count($resultArray), print_r($resultArray, TRUE), BaseDatabaseBackend::RESULT_INDEX_ROWS));
+                       throw new InvalidArgumentException(sprintf('resultArray(%d)=%s has no element "%s".', count($resultArray), print_r($resultArray, TRUE), BaseDatabaseResult::RESULT_NAME_ROWS));
                }
 
                // Get a new instance
@@ -107,7 +108,7 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul
                $resultInstance->resetCurrentPosition();
 
                // Set affected rows
-               $resultInstance->setAffectedRows(count($resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS]));
+               $resultInstance->setAffectedRows(count($resultArray[BaseDatabaseResult::RESULT_NAME_ROWS]));
 
                // Return the instance
                return $resultInstance;
@@ -136,7 +137,7 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul
                // Now get the update criteria array and update all entries
                foreach ($updateInstance->getUpdateCriteria() as $criteriaKey => $criteriaValue) {
                        // Update data
-                       $this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][$entryKey][$criteriaKey] = $criteriaValue;
+                       $this->resultArray[BaseDatabaseResult::RESULT_NAME_ROWS][$entryKey][$criteriaKey] = $criteriaValue;
 
                        // Mark it as out-dated
                        $this->outDated[$criteriaKey] = 1;
@@ -159,7 +160,7 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul
                // Is the result valid?
                if ($this->valid()) {
                        // Next entry found, so cache it
-                       $this->currentRow = $this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][$this->currentPos];
+                       $this->currentRow = $this->resultArray[BaseDatabaseResult::RESULT_NAME_ROWS][$this->currentPos];
                        $nextValid = true;
                }
 
@@ -194,9 +195,9 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul
                $current = NULL;
 
                // Does the current enty exist?
-               if (isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][$this->currentPos])) {
+               if (isset($this->resultArray[BaseDatabaseResult::RESULT_NAME_ROWS][$this->currentPos])) {
                        // Then get it
-                       $current = $this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][$this->currentPos];
+                       $current = $this->resultArray[BaseDatabaseResult::RESULT_NAME_ROWS][$this->currentPos];
                }
 
                // Return the result
@@ -211,7 +212,7 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul
        public function valid () {
                // Check if all is fine ...
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: this->currentPos=%d - CALLED!', $this->currentPos));
-               $isValid = ($this->ifStatusIsOkay() && isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][$this->currentPos]) && isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][0]));
+               $isValid = ($this->ifStatusIsOkay() && isset($this->resultArray[BaseDatabaseResult::RESULT_NAME_ROWS][$this->currentPos]) && isset($this->resultArray[BaseDatabaseResult::RESULT_NAME_ROWS][0]));
 
                // Return the result
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: isValid=%d - EXIT!', intval($isValid)));
@@ -226,7 +227,7 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul
        public function count () {
                // Count rows
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-DATABASE-RESULT: CALLED!');
-               $count = count($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS]);
+               $count = count($this->resultArray[BaseDatabaseResult::RESULT_NAME_ROWS]);
 
                // Return it
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: count=%d - EXIT!', $count));
@@ -241,7 +242,7 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul
        public function ifStatusIsOkay () {
                // Check all conditions
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: this->currentPos=%d - CALLED!', $this->currentPos));
-               $ifStatusOkay = (isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_STATUS]) && $this->resultArray[BaseDatabaseBackend::RESULT_INDEX_STATUS] === BaseDatabaseBackend::RESULT_OKAY);
+               $ifStatusOkay = (isset($this->resultArray[BaseDatabaseResult::RESULT_NAME_STATUS]) && $this->resultArray[BaseDatabaseResult::RESULT_NAME_STATUS] === BaseDatabaseBackend::RESULT_OKAY);
 
                // Return status
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: ifStatusOkay=%s - EXIT!', intval($ifStatusOkay)));
index 7e01bc05c79f6b165d822c7008860e27038ce999..5563b87729cf161ab5b0ea644ab7e368250cd5d2 100644 (file)
@@ -11,6 +11,9 @@ use Org\Mxchange\CoreFramework\Registry\Registerable;
 use Org\Mxchange\CoreFramework\Middleware\BaseMiddleware;
 use Org\Mxchange\CoreFramework\Middleware\Debug\DebugMiddleware;
 
+// Import SPL stuff
+use \InvalidArgumentException;
+
 /**
  * Database selector class
  *
@@ -47,7 +50,7 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Re
        /**
         * The real database layer
         */
-       private $dbLayer = NULL;
+       private $backendInstance = NULL;
 
        /**
         * An instance of this class
@@ -65,16 +68,16 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Re
        /**
         * Creates a new database connection layer
         *
-        * @param       $debugInstance  An instance of a DebugMiddleware class
-        * @param       $dbLayer                An instance of a DatabaseBackend class
+        * @param       $debugInstance          An instance of a DebugMiddleware class
+        * @param       $backendInstance        An instance of a DatabaseBackend class
         * @todo        $debugInstance is currently not used
         */
-       public static final function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseBackend $dbLayer) {
+       public static final function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseBackend $backendInstance) {
                // Get instance
                $databaseInstance = new DatabaseConnection();
 
                // Set database layer
-               $databaseInstance->setDatabaseLayer($dbLayer);
+               $databaseInstance->setDatabaseLayer($backendInstance);
 
                // Set db instance
                self::$selfInstance = $databaseInstance;
@@ -121,11 +124,11 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Re
 
        /**
         * Setter for the real database layer
-        * @param       $dbLayer        An instance of the real database layer
+        * @param       $backendInstance        An instance of a DatabaseBackend class
         * @return      void
         */
-       public final function setDatabaseLayer (DatabaseBackend $dbLayer) {
-               $this->dbLayer = $dbLayer;
+       public final function setDatabaseLayer (DatabaseBackend $backendInstance) {
+               $this->backendInstance = $backendInstance;
        }
 
        /**
@@ -134,7 +137,7 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Re
         * @return      $indexKey       Index key
         */
        public final function getIndexKey () {
-               return $this->dbLayer->getIndexKey();
+               return $this->backendInstance->getIndexKey();
        }
 
        /**
@@ -144,13 +147,20 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Re
         * @param       $tableName                      Name of the 'table' we shall query
         * @param       $criteriaInstance       An instance of a Criteria class
         * @return      $result                         The result as an array
+        * @throws      InvalidArgumentException        If a parameter is empty
         */
        public function doSelectByTableCriteria (string $tableName, Criteria $criteriaInstance) {
+               // Validate parameter
+               if (empty($tableName)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "tableName" is empty');
+               }
+
                // Connect to the database
-               $this->dbLayer->connectToDatabase();
+               $this->backendInstance->connectToDatabase();
 
                // Get result from query
-               $result = $this->dbLayer->querySelect($tableName, $criteriaInstance);
+               $result = $this->backendInstance->querySelect($tableName, $criteriaInstance);
 
                // Return the result
                return $result;
@@ -162,7 +172,7 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Re
         * @return      $exceptionInstance      Last thrown exception
         */
        public final function getLastException () {
-               $exceptionInstance = $this->dbLayer->getLastException();
+               $exceptionInstance = $this->backendInstance->getLastException();
                return $exceptionInstance;
        }
 
@@ -174,10 +184,10 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Re
         */
        public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
                // Connect to the database
-               $this->dbLayer->connectToDatabase();
+               $this->backendInstance->connectToDatabase();
 
                // Ask the database layer
-               $this->dbLayer->queryInsertDataSet($dataSetInstance);
+               $this->backendInstance->queryInsertDataSet($dataSetInstance);
        }
 
        /**
@@ -188,10 +198,10 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Re
         */
        public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
                // Connect to the database
-               $this->dbLayer->connectToDatabase();
+               $this->backendInstance->connectToDatabase();
 
                // Ask the database layer
-               $this->dbLayer->queryUpdateDataSet($dataSetInstance);
+               $this->backendInstance->queryUpdateDataSet($dataSetInstance);
        }
 
        /**
@@ -199,13 +209,20 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Re
         *
         * @param       $tableName              Name of table we need the primary key column from
         * @return      $primaryKey             Primary key column of requested table
+        * @throws      InvalidArgumentException        If a parameter is empty
         */
        public function getPrimaryKeyOfTable (string $tableName) {
+               // Validate parameter
+               if (empty($tableName)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "tableName" is empty');
+               }
+
                // Connect to the database
-               $this->dbLayer->connectToDatabase();
+               $this->backendInstance->connectToDatabase();
 
                // Ask the database layer
-               $primaryKey = $this->dbLayer->getPrimaryKeyOfTable($tableName);
+               $primaryKey = $this->backendInstance->getPrimaryKeyOfTable($tableName);
 
                // Return the value
                return $primaryKey;
@@ -219,11 +236,11 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Re
         */
        public function removeNonPublicDataFromArray (array $data) {
                // Connect to the database
-               $this->dbLayer->connectToDatabase();
+               $this->backendInstance->connectToDatabase();
 
                // Call database backend
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: Calling this->dbLayer->removeNonPublicDataFromArray(data) ...');
-               $data = $this->dbLayer->removeNonPublicDataFromArray($data);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: Calling this->backendInstance->removeNonPublicDataFromArray(data) ...');
+               $data = $this->backendInstance->removeNonPublicDataFromArray($data);
 
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: data[]=' . gettype($data));
                return $data;
@@ -234,13 +251,20 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Re
         *
         * @param       $tableName      Table name
         * @return      $count          Total row count
+        * @throws      InvalidArgumentException        If a parameter is empty
         */
        public function countTotalRows (string $tableName) {
+               // Validate parameter
+               if (empty($tableName)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "tableName" is empty');
+               }
+
                // Connect to the database
-               $this->dbLayer->connectToDatabase();
+               $this->backendInstance->connectToDatabase();
 
                // Ask the database layer
-               $count = $this->dbLayer->countTotalRows($tableName);
+               $count = $this->backendInstance->countTotalRows($tableName);
 
                // Return the value
                return $count;