From eb719958d81746a0c153a80e47e6686e8a112b8e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 31 May 2008 21:37:21 +0000 Subject: [PATCH] Criteria added for abstract database programming --- .gitattributes | 7 + inc/classes/exceptions/criteria/.htaccess | 1 + inc/classes/interfaces/criteria/.htaccess | 1 + .../interfaces/criteria/class_Criteria.php | 36 +++++ .../interfaces/criteria/extended/.htaccess | 1 + .../criteria/extended/class_LocalCriteria.php | 28 ++++ .../class_DatabaseFrontendInterface.php | 18 +++ inc/classes/main/cache/class_MemoryCache.php | 33 ++++- .../main/class_BaseFrameworkSystem.php | 3 +- inc/classes/main/criteria/.htaccess | 1 + .../main/criteria/class_SearchCriteria.php | 136 +++++++++++++++++ .../databases/class_LocalFileDatabase.php | 126 +++++++++++----- .../wrapper/class_UserDatabaseWrapper.php | 80 +++++----- inc/classes/main/user/class_User.php | 29 ++-- .../middleware/class_BaseMiddleware.php | 1 + .../database/class_DatabaseConnection.php | 138 +++++++----------- .../middleware/io/class_FileIoHandler.php | 4 - index.php | 2 +- 18 files changed, 463 insertions(+), 182 deletions(-) create mode 100644 inc/classes/exceptions/criteria/.htaccess create mode 100644 inc/classes/interfaces/criteria/.htaccess create mode 100644 inc/classes/interfaces/criteria/class_Criteria.php create mode 100644 inc/classes/interfaces/criteria/extended/.htaccess create mode 100644 inc/classes/interfaces/criteria/extended/class_LocalCriteria.php create mode 100644 inc/classes/main/criteria/.htaccess create mode 100644 inc/classes/main/criteria/class_SearchCriteria.php diff --git a/.gitattributes b/.gitattributes index 9a680d2..5ad52fb 100644 --- a/.gitattributes +++ b/.gitattributes @@ -150,6 +150,7 @@ inc/classes/exceptions/container/class_ContainerItemIsNullException.php -text inc/classes/exceptions/container/class_ContainerMaybeDamagedException.php -text inc/classes/exceptions/controller/.htaccess -text inc/classes/exceptions/controller/class_DefaultControllerException.php -text +inc/classes/exceptions/criteria/.htaccess -text inc/classes/exceptions/database/.htaccess -text inc/classes/exceptions/database/local_file/.htaccess -text inc/classes/exceptions/database/local_file/class_SavePathIsEmptyException.php -text @@ -227,6 +228,10 @@ inc/classes/interfaces/compressor/.htaccess -text inc/classes/interfaces/compressor/class_Compressor.php -text inc/classes/interfaces/controller/.htaccess -text inc/classes/interfaces/controller/class_Controller.php -text +inc/classes/interfaces/criteria/.htaccess -text +inc/classes/interfaces/criteria/class_Criteria.php -text +inc/classes/interfaces/criteria/extended/.htaccess -text +inc/classes/interfaces/criteria/extended/class_LocalCriteria.php -text inc/classes/interfaces/database/.htaccess -text inc/classes/interfaces/database/class_FrameworkDatabaseInterface.php -text inc/classes/interfaces/database/frontend/.htaccess -text @@ -287,6 +292,8 @@ inc/classes/main/controller/default/class_WebDefaultController.php -text inc/classes/main/controller/default/class_WebDefaultNewsController.php -text inc/classes/main/controller/form/.htaccess -text inc/classes/main/controller/form/class_WebDoFormController.php -text +inc/classes/main/criteria/.htaccess -text +inc/classes/main/criteria/class_SearchCriteria.php -text inc/classes/main/database/.htaccess -text inc/classes/main/database/class_ -text inc/classes/main/database/class_BaseDatabaseFrontend.php -text diff --git a/inc/classes/exceptions/criteria/.htaccess b/inc/classes/exceptions/criteria/.htaccess new file mode 100644 index 0000000..3a42882 --- /dev/null +++ b/inc/classes/exceptions/criteria/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/interfaces/criteria/.htaccess b/inc/classes/interfaces/criteria/.htaccess new file mode 100644 index 0000000..3a42882 --- /dev/null +++ b/inc/classes/interfaces/criteria/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/interfaces/criteria/class_Criteria.php b/inc/classes/interfaces/criteria/class_Criteria.php new file mode 100644 index 0000000..d69efad --- /dev/null +++ b/inc/classes/interfaces/criteria/class_Criteria.php @@ -0,0 +1,36 @@ + + * @version 0.0.0 + * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +interface Criteria extends FrameworkInterface { + /** + * Add extra criteria + * + * @param $criteriaKey Criteria key + * @param $criteriaValue Criteria value + * @return void + */ + function addCriteria ($criteriaKey, $criteriaValue); +} + +// +?> diff --git a/inc/classes/interfaces/criteria/extended/.htaccess b/inc/classes/interfaces/criteria/extended/.htaccess new file mode 100644 index 0000000..3a42882 --- /dev/null +++ b/inc/classes/interfaces/criteria/extended/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/interfaces/criteria/extended/class_LocalCriteria.php b/inc/classes/interfaces/criteria/extended/class_LocalCriteria.php new file mode 100644 index 0000000..2ac9efd --- /dev/null +++ b/inc/classes/interfaces/criteria/extended/class_LocalCriteria.php @@ -0,0 +1,28 @@ + + * @version 0.0.0 + * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +interface LocalCriteria extends Criteria { +} + +// +?> diff --git a/inc/classes/interfaces/database/frontend/class_DatabaseFrontendInterface.php b/inc/classes/interfaces/database/frontend/class_DatabaseFrontendInterface.php index 9b44eab..14e89d9 100644 --- a/inc/classes/interfaces/database/frontend/class_DatabaseFrontendInterface.php +++ b/inc/classes/interfaces/database/frontend/class_DatabaseFrontendInterface.php @@ -36,6 +36,24 @@ interface DatabaseFrontendInterface extends FrameworkDatabaseInterface { * ObjectContainer */ function loadObject (); + + /** + * Makes sure that the database connection is alive + * + * @return void + */ + function connectToDatabase (); + + /** + * Starts a SELECT query on the database by given return type, table name + * and search criteria + * + * @param $resultType Result type ("array", "object" and "indexed" are valid) + * @param $tableName Name of the database table + * @param $criteria Search criteria class + * @return $resultData Result data of the query + */ + function querySelect ($resultType, $tableName, Criteria $criteriaInstance); } // [EOF] diff --git a/inc/classes/main/cache/class_MemoryCache.php b/inc/classes/main/cache/class_MemoryCache.php index 8dd2537..7da2e16 100644 --- a/inc/classes/main/cache/class_MemoryCache.php +++ b/inc/classes/main/cache/class_MemoryCache.php @@ -76,13 +76,44 @@ class MemoryCache extends BaseFrameworkSystem implements Cacheable { /** * Does the specified offset exist in cache? * - * @param $offset The offsrt we are looking for + * @param $offset The offset we are looking for * @return $exists Wether the offset exists */ public final function offsetExists ($offset) { $exists = $this->dataCache->offsetExists($offset); return $exists; } + + /** + * Setter for cache offset + * + * @param $offset The offset we shall set + * @param $data Data to store in the cache + * @return void + */ + public final function offsetSet ($offset, $data) { + $this->dataCache->offsetSet($offset, $data); + } + + /** + * Getter for cache offset or "null" if not found + * + * @param $offset The offset we shall set + * @return $data Data to store in the cache + */ + public final function offsetget ($offset) { + // Default is offset not found + $data = null; + + // Is the offset there? + if ($this->offsetExists($offset)) { + // Then get the data from it + $data = $this->dataCache->offsetGet($offset); + } + + // Return data + return $data; + } } // [EOF] diff --git a/inc/classes/main/class_BaseFrameworkSystem.php b/inc/classes/main/class_BaseFrameworkSystem.php index 1ccb1c2..2862772 100644 --- a/inc/classes/main/class_BaseFrameworkSystem.php +++ b/inc/classes/main/class_BaseFrameworkSystem.php @@ -150,6 +150,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { const EXCEPTION_HEADERS_ALREADY_SENT = 0x033; const EXCEPTION_DEFAUL_CONTROLLER_GONE = 0x034; const EXCEPTION_CLASS_NOT_FOUND = 0x035; + const EXCEPTION_REQUIRED_INTERFACE_MISSING = 0x036; /** * In the super constructor these system classes shall be ignored or else @@ -236,7 +237,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { } elseif (is_float($arg)) { // Floating point $argsString .= $arg."(float)"; - } elseif ($arg instanceof BaseFramework) { + } elseif ($arg instanceof BaseFrameworkSystem) { // Own object instance $argsString .= $arg->__toString()."(Object)"; } elseif (is_object($arg)) { diff --git a/inc/classes/main/criteria/.htaccess b/inc/classes/main/criteria/.htaccess new file mode 100644 index 0000000..3a42882 --- /dev/null +++ b/inc/classes/main/criteria/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/criteria/class_SearchCriteria.php b/inc/classes/main/criteria/class_SearchCriteria.php new file mode 100644 index 0000000..a2f0910 --- /dev/null +++ b/inc/classes/main/criteria/class_SearchCriteria.php @@ -0,0 +1,136 @@ + + * @version 0.3.0 + * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software + * @license GNU GPL 3.0 or any newer version + * @link http://www.mxchange.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class SearchCriteria extends BaseFrameworkSystem implements LocalCriteria { + /** + * Criteria to handle + */ + private $searchCriteria = array(); + + /** + * Limitation for the search + */ + private $limit = 0; + + /** + * Skip these entries before using them + */ + private $skip = 0; + + /** + * Private constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set part description + $this->setObjectDescription("Search criteria class"); + + // Create unique ID number + $this->createUniqueID(); + + // Clean up a little + $this->removeNumberFormaters(); + $this->removeSystemArray(); + } + + /** + * Create an instance of this class + * + * @return $criteriaInstance An instance of this criteria + */ + public final static function createSearchCriteria () { + // Get a new instance + $criteriaInstance = new SearchCriteria(); + + // Return this instance + return $criteriaInstance; + } + + /** + * Add extra criteria + * + * @param $criteriaKey Criteria key + * @param $criteriaValue Criteria value + * @return void + */ + public function addCriteria ($criteriaKey, $criteriaValue) { + $this->searchCriteria[$criteriaKey] = $criteriaValue; + } + + /** + * Setter for limit + * + * @param $limit Search limit + * @return void + */ + public final function setLimit ($limit) { + $this->limit = $limit; + } + + /** + * Setter for skip + * + * @param $skip Search skip + * @return void + */ + public final function setSkip ($skip) { + $this->skip = $skip; + } + + /** + * "Getter" for a cache key + * + * @return $cacheKey The key suitable for the cache system + */ + public function getCacheKey () { + // Initialize the key + $cacheKey = ""; + + // Now walk through all criterias + foreach ($this->searchCriteria as $criteriaKey => $criteriaValue) { + // Add the value URL encoded to avoid any trouble with special characters + $cacheKey .= sprintf("%s=%s;", + $criteriaKey, + urlencode($criteriaValue) + ); + } + + // Add limit and skip values + $cacheKey .= sprintf("%%limit%%=%s;%%skip%%=%s", + $this->limit, + $this->skip + ); + + // Return the cache key + return $cacheKey; + } +} + +// [EOF] +?> diff --git a/inc/classes/main/database/databases/class_LocalFileDatabase.php b/inc/classes/main/database/databases/class_LocalFileDatabase.php index 432a168..16314e1 100644 --- a/inc/classes/main/database/databases/class_LocalFileDatabase.php +++ b/inc/classes/main/database/databases/class_LocalFileDatabase.php @@ -44,6 +44,11 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend */ private $lastContents = array(); + /** + * Wether the "connection is already up + */ + private $alreadyConnected = false; + /** * The private constructor. Do never instance from outside! * You need to set a local file path. The class will then validate it. @@ -72,37 +77,18 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend * @param $ioInstance The input/output handler. This * should be FileIoHandler * @return $dbInstance An instance of LocalFileDatabase - * @throws SavePathIsEmptyException If the given save path is an - * empty string - * @throws SavePathIsNoDirectoryException If the save path is no - * path (e.g. a file) - * @throws SavePathReadProtectedException If the save path is read- - * protected - * @throws SavePathWriteProtectedException If the save path is write- - * protected */ public final static function createLocalFileDatabase ($savePath, FileIoHandler $ioInstance) { // Get an instance $dbInstance = new LocalFileDatabase(); - if (empty($savePath)) { - // Empty string - throw new SavePathIsEmptyException($dbInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING); - } elseif (!is_dir($savePath)) { - // Is not a dir - throw new SavePathIsNoDirectoryException($savePath, self::EXCEPTION_INVALID_PATH_NAME); - } elseif (!is_readable($savePath)) { - // Path not readable - throw new SavePathReadProtectedException($savePath, self::EXCEPTION_READ_PROTECED_PATH); - } elseif (!is_writeable($savePath)) { - // Path not writeable - throw new SavePathWriteProtectedException($savePath, self::EXCEPTION_WRITE_PROTECED_PATH); - } - // Set save path and IO instance $dbInstance->setSavePath($savePath); $dbInstance->setFileIoInstance($ioInstance); + // "Connect" to the database + $dbInstance->connectToDatabase(); + // Return database instance return $dbInstance; } @@ -340,19 +326,19 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend /** * Get cached (last fetched) data from the local file database * - * @param $uniqueID The ID number for looking up the data + * @param $uniqueID The ID number for looking up the data * @return $object The restored object from the maybe compressed * serialized data - * @throws MismatchingCompressorsException If the compressor from - * the loaded file - * mismatches with the - * current used one. - * @throws NullPointerException If the restored object - * is null - * @throws NoObjectException If the restored "object" - * is not an object instance - * @throws MissingMethodException If the required method - * toString() is missing + * @throws MismatchingCompressorsException If the compressor from + * the loaded file + * mismatches with the + * current used one. + * @throws NullPointerException If the restored object + * is null + * @throws NoObjectException If the restored "object" + * is not an object instance + * @throws MissingMethodException If the required method + * toString() is missing */ public final function getObjectFromCachedData ($uniqueID) { // Get instance for file handler @@ -386,7 +372,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend } elseif (!is_object($object)) { // Is not an object, throw exception throw new NoObjectException($object, self::EXCEPTION_IS_NO_OBJECT); - } elseif (!method_exists($object, '__toString')) { + } elseif (!$object instanceof FrameworkInterface) { // A highly required method was not found... :-( throw new MissingMethodException(array($object, '__toString'), self::EXCEPTION_MISSING_METHOD); } @@ -458,7 +444,77 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend return $contents; } - /* DUMMY */ public final function loadObject () {} + /** + * Makes sure that the database connection is alive + * + * @return void + */ + public function connectToDatabase () { + // @TODO Do some checks on the database directory and files here + } + + /** + * Loads data saved with saveObject from the database and re-creates a + * full object from it. + * If limitObject() was called before a new object ObjectContainer with + * all requested attributes will be returned instead. + * + * @return Object The fully re-created object or instance to + * ObjectContainer + * @throws SavePathIsEmptyException If the given save path is an + * empty string + * @throws SavePathIsNoDirectoryException If the save path is no + * path (e.g. a file) + * @throws SavePathReadProtectedException If the save path is read- + * protected + * @throws SavePathWriteProtectedException If the save path is write- + * protected + */ + public function loadObject () { + // Already connected? Then abort here + if ($this->alreadyConnected === true) return true; + + // Get the save path + $savePath = $this->getSavePath(); + + if (empty($savePath)) { + // Empty string + throw new SavePathIsEmptyException($dbInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING); + } elseif (!is_dir($savePath)) { + // Is not a dir + throw new SavePathIsNoDirectoryException($savePath, self::EXCEPTION_INVALID_PATH_NAME); + } elseif (!is_readable($savePath)) { + // Path not readable + throw new SavePathReadProtectedException($savePath, self::EXCEPTION_READ_PROTECED_PATH); + } elseif (!is_writeable($savePath)) { + // Path not writeable + throw new SavePathWriteProtectedException($savePath, self::EXCEPTION_WRITE_PROTECED_PATH); + } + + // "Connection" established... ;-) + $this->alreadyConnected = true; + } + + /** + * Starts a SELECT query on the database by given return type, table name + * and search criteria + * + * @param $resultType Result type ("array", "object" and "indexed" are valid) + * @param $tableName Name of the database table + * @param $criteria Local search criteria class + * @return $resultData Result data of the query + * @throws UnsupportedCriteriaException If the criteria is unsupported + */ + public function querySelect ($resultType, $tableName, Criteria $criteriaInstance) { + // Is this criteria supported? + if (!$criteriaInstance instanceof LocalCriteria) { + // Not supported by this database layer + throw new UnsupportedCriteriaException(array($this, $criteriaInstance), self::EXCEPTION_REQUIRED_INTERFACE_MISSING); + } + + // A "select" query on local files is not that easy, so begin slowly with it... + $this->partialStub(sprintf("type=%s,table=%s,criteria=%s", $resultType, $tableName, $criteriaInstance)); + } } // [EOF] diff --git a/inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php b/inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php index f9caae5..c8eb465 100644 --- a/inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php +++ b/inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php @@ -28,9 +28,15 @@ class UserDatabaseWrapper extends BaseDatabaseWrapper { */ private $cacheInstance = null; - // Constants + // Constants for exceptions const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0xe00; + // Constants for database columns + const DB_COLUMN_USERNAME = "username"; + + // Constants for database table names + const DB_TABLE_USER = "user"; + /** * Protected constructor * @@ -45,31 +51,21 @@ class UserDatabaseWrapper extends BaseDatabaseWrapper { // Create unique ID number $this->createUniqueID(); - - // Initialize the cache instance - $this->initCacheInstance(); } /** * Creates an instance of this database wrapper by a provided user class * - * @param $userInstance An instance of a user class * @return $wrapperInstance An instance of the created wrapper class * @throws WrapperUserNameNotFoundException If the supplied username * does not exist */ - public final static function createUserDatabaseWrapper (ManageableUser $userInstance) { + public final static function createUserDatabaseWrapper () { // Get a new instance $wrapperInstance = new UserDatabaseWrapper(); - // Does the username exists? - if (!$wrapperInstance->ifUserNameExists($userInstance->getUserName())) { - // Throw an exception here - throw new WrapperUserNameNotFoundException (array($wrapperInstance, $userInstance), self::EXCEPTION_CLIENT_USERNAME_NOT_FOUND); - } - - // The user exists - $wrapperInstance->partialStub("Add loading of full user details"); + // Initialize the cache instance + $wrapperInstance->initCacheInstance(); // Return the instance return $wrapperInstance; @@ -80,48 +76,42 @@ class UserDatabaseWrapper extends BaseDatabaseWrapper { * * @return void */ - private function initCacheInstance () { + protected function initCacheInstance () { // Set the new instance $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache(); } /** - * Checks wether the given username is already used + * Do a "select" query on the user table with the given search criteria and + * store it in cache for later usage * - * @param $userName The username we shall look up - * @return $exists Wether the username exists + * @param $criteriaInstance An instance of a Criteria class + * @return $resultInstance An instance of a database result class */ - public function ifUserNameExists ($userName) { - // By default no entry exist - $exists = false; - - // Does this entry exist? - if ($this->cacheInstance->offsetExists($userName)) { - // Then we have a user! - $exists = true; + public function doSelectByCriteria (Criteria $criteriaInstance) { + // First get a key suitable for our cache and extend it with this class name + $cacheKey = sprintf("%s@%s", + $this->__toString(), + $criteriaInstance->getCacheKey() + ); + + // Does this key exists in cache? + if ($this->cacheInstance->offsetExists($cacheKey)) { + // Then use this result + $result = $cacheInstance->offsetGet($cacheKey); } else { - // Create a search criteria - $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria'); - - // Add the username as a criteria and set limit to one entry - $criteriaInstance->add(self::DB_USERNAME, $userName); - $criteriaInstance->setLimit(1); - - // Get a search result - $result = $this->doSelectByCriteria($criteriaInstance); + // Now it's time to ask the database layer for this select statement + $result = $this->getDatabaseInstance()->doSelectByTableCriteria(self::DB_TABLE_USER, $criteriaInstance); - // Search for it - if ($result->next()) { - // Get the result - $this->cacheInstance->add($userName, $result->getResultArray()); - - // Entry found, so all is fine - $exists = true; - } + // Cache the result + $this->cacheInstance->offsetSet($cacheKey, $result); } - // Return the result - return $exists; + // Create an instance of a DatabaseResult class with the given result + $resultInstance = DatabaseResult::createDatabaseResult($result); + + // And return the instance + return $resultInstance; } } diff --git a/inc/classes/main/user/class_User.php b/inc/classes/main/user/class_User.php index 62c4f17..280adec 100644 --- a/inc/classes/main/user/class_User.php +++ b/inc/classes/main/user/class_User.php @@ -27,11 +27,6 @@ class User extends BaseFrameworkSystem implements ManageableUser { */ private $username = ""; - /** - * An instance of a database wrapper - */ - private $userWrapper = null; - // Exceptions const EXCEPTION_USERNAME_NOT_FOUND = 0xd00; @@ -112,13 +107,23 @@ class User extends BaseFrameworkSystem implements ManageableUser { // By default the username does exist $exists = true; - // Try to get a UserDatabaseWrapper object back - try { - // Get the instance by providing this class - $this->userWrapper = UserDatabaseWrapper::createUserDatabaseWrapper($this); - } catch (WrapperUserNameNotFoundException $e) { - // Does not exist! - $exists = false; + // Get a UserDatabaseWrapper instance + $wrapperInstance = UserDatabaseWrapper::createUserDatabaseWrapper(); + + // Create a search criteria + $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria'); + + // Add the username as a criteria and set limit to one entry + $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUsername()); + $criteriaInstance->setLimit(1); + + // Get a search result + $result = $wrapperInstance->doSelectByCriteria($criteriaInstance); + + // Search for it + if ($result->next()) { + // Entry found, so all is fine + $exists = true; } // Return the status diff --git a/inc/classes/middleware/class_BaseMiddleware.php b/inc/classes/middleware/class_BaseMiddleware.php index 470f6f9..6dc346b 100644 --- a/inc/classes/middleware/class_BaseMiddleware.php +++ b/inc/classes/middleware/class_BaseMiddleware.php @@ -33,6 +33,7 @@ class BaseMiddleware extends BaseFrameworkSystem { // Clean up a little $this->removeNumberFormaters(); + $this->removeSystemArray(); } } diff --git a/inc/classes/middleware/database/class_DatabaseConnection.php b/inc/classes/middleware/database/class_DatabaseConnection.php index 6362069..ebb6933 100644 --- a/inc/classes/middleware/database/class_DatabaseConnection.php +++ b/inc/classes/middleware/database/class_DatabaseConnection.php @@ -22,8 +22,15 @@ * along with this program. If not, see . */ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, LimitableObject { - // Array for connection data - private $connectData = array(); + /** + * Array for connection data + */ + private $connectData = array( + 'login' => "", + 'pass' => "", + 'dbase' => "", + 'host' => "" + ); // The real database layer private $dbLayer = null; @@ -31,7 +38,11 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Li // An instance of this class private static $thisInstance = null; - // Private constructor + /** + * Protected constructor + * + * @return void + */ protected function __construct() { // Call parent constructor parent::__construct(__CLASS__); @@ -41,9 +52,6 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Li // Create an unique ID $this->createUniqueID(); - - // Clean up a little - $this->removeSystemArray(); } // Create new database connection layer @@ -70,7 +78,7 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Li } // Public setter for database connection - public final function setConnectionData ($login, $pass, $dbase, $host) { + public final function setConnectionData ($login, $pass, $dbase, $host="localhost") { // Transfer connection data $this->connectData['login'] = (string) $login; $this->connectData['pass'] = (string) $pass; @@ -83,23 +91,10 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Li * * @param $object The object we shall save * @return void - * @throws NullPointerException If $limitInstance is null - * @throws NoObjectException If $limitInstance is not an object - * @throws MissingMethodException If the required method - * saveObject() was not found */ - public final function saveObject ($object) { - // Some sanity checks - if (is_null($this->dbLayer)) { - // Is null - throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); - } elseif (!is_object($this->dbLayer)) { - // Is not an object - throw new NoObjectException($this->dbLayer, self::EXCEPTION_IS_NO_OBJECT); - } elseif (!method_exists($this->dbLayer, 'saveObject')) { - // Does not have the required instance - throw new MissingMethodException(array($this->dbLayer, 'saveObject'), self::EXCEPTION_MISSING_METHOD); - } + public function saveObject ($object) { + // Connect to the database + $this->dbLayer->connectToDatabase(); // For now just pipe it through to the database layer $this->dbLayer->saveObject($object); @@ -113,26 +108,10 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Li * elements we shall exclusivly include in * saving process * @return void - * @throws NullPointerException If $limitInstance is null - * @throws NoObjectException If $limitInstance is not an object - * @throws MissingMethodException If the required method - * limitObject() was not found */ - public final function limitObject (ObjectLimits $limitInstance) { - // Get real database connection - $this->dbLayer = $this->getDatabaseInstance(); - - // Some sanity checks - if (is_null($this->dbLayer)) { - // Is null - throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); - } elseif (!is_object($this->dbLayer)) { - // Is not an object - throw new NoObjectException($object, self::EXCEPTION_IS_NO_OBJECT); - } elseif (!method_exists($this->dbLayer, 'limitObject')) { - // Does not have the required instance - throw new MissingMethodException(array($this->dbLayer, 'limitObject'), self::EXCEPTION_MISSING_METHOD); - } + public function limitObject (ObjectLimits $limitInstance) { + // Connect to the database + $this->dbLayer->connectToDatabase(); // For now we pipe this through to the real database instance $this->dbLayer->limitObject($limitInstance); @@ -142,29 +121,16 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Li * Analyses if a unique ID has already been used or not. This method does * only pass the given ID through to the "real" database layer. * - * @param $uniqueID A unique ID number which shall be checked - * before it will be used - * @param $inConstructor If called from a constructor or from - * somewhere else + * @param $uniqueID A unique ID number which shall be checked + * before it will be used + * @param $inConstructor If called from a constructor or from + * somewhere else * @return $isUnused true = The unique ID was not found in the database, - * false = It is already in use by an other object - * @throws NullPointerException If $this->dbLayer is null - * @throws NoObjectException If $this->dbLayer is not an object - * @throws MissingMethodException If the required method - * isUniqueIdUsed() was not found + * false = It is already in use by an other object */ - public final function isUniqueIdUsed ($uniqueID, $inConstructor = false) { - // Some sanity checks - if (is_null($this->dbLayer)) { - // Is null - throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); - } elseif (!is_object($this->dbLayer)) { - // Is not an object - throw new NoObjectException($object, self::EXCEPTION_IS_NO_OBJECT); - } elseif (!method_exists($this->dbLayer, 'isUniqueIdUsed')) { - // Does not have the required instance - throw new MissingMethodException(array($this->dbLayer, 'isUniqueIdUsed'), self::EXCEPTION_MISSING_METHOD); - } + public function isUniqueIdUsed ($uniqueID, $inConstructor = false) { + // Connect to the database + $this->dbLayer->connectToDatabase(); // Pass the returning result through return $this->dbLayer->isUniqueIdUsed($uniqueID, $inConstructor); @@ -173,28 +139,15 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Li /** * Gets cached data from the database layer and if not found fetch it from * the database again. This method does not return the header stuff because - * The underlaying database class will return only the requested content. + * the underlaying database class will return only the requested content. * - * @param $idNumber The ID number which we need for looking up - * the requested data + * @param $idNumber The ID number which we need for looking up + * the requested data * @return $cachedArray The maybe cached data from the database - * @throws NullPointerException If $this->dbLayer is null - * @throws NoObjectException If $this->dbLayer is not an object - * @throws MissingMethodException If the required method - * isUniqueIdUsed() was not found */ - public final function getObjectFromCachedData ($idNumber) { - // Some sanity checks - if (is_null($this->dbLayer)) { - // Is null - throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); - } elseif (!is_object($this->dbLayer)) { - // Is not an object - throw new NoObjectException($object, self::EXCEPTION_IS_NO_OBJECT); - } elseif (!method_exists($this->dbLayer, 'getObjectFromCachedData')) { - // Does not have the required instance - throw new MissingMethodException(array($this->dbLayer, 'getObjectFromCachedData'), self::EXCEPTION_MISSING_METHOD); - } + public function getObjectFromCachedData ($idNumber) { + // Connect to the database + $this->dbLayer->connectToDatabase(); // Pass the returning result through return $this->dbLayer->getObjectFromCachedData($idNumber); @@ -202,12 +155,31 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Li /** * Setter for the real database layer - * @param $dbLayer An instance of the real database layer + * @param $dbLayer An instance of the real database layer * @return void */ public final function setDatabaseLayer (DatabaseFrontendInterface $dbLayer) { $this->dbLayer = $dbLayer; } + + /** + * Runs a "select" statement on the database layer with given table name + * and criteria. If this doesn't fail the result will be returned + * + * @param $tableName Name of the "table" we shall query + * @param $criteriaInstance An instance of a Criteria class + * @return $result The result as an array + */ + public function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) { + // Connect to the database + $this->dbLayer->connectToDatabase(); + + // Get result from query + $result = $this->dbLayer->querySelect("array", $tableName, $criteriaInstance); + + // Return the result + return $result; + } } // [EOF] diff --git a/inc/classes/middleware/io/class_FileIoHandler.php b/inc/classes/middleware/io/class_FileIoHandler.php index a7322fb..8e1b4b3 100644 --- a/inc/classes/middleware/io/class_FileIoHandler.php +++ b/inc/classes/middleware/io/class_FileIoHandler.php @@ -53,10 +53,6 @@ class FileIoHandler extends BaseMiddleware { // Create an unique ID $this->createUniqueID(); - // Clean up a little - $this->removeNumberFormaters(); - $this->removeSystemArray(); - // Set own instance self::$thisInstance = $this; } diff --git a/index.php b/index.php index 057a021..8dd6132 100644 --- a/index.php +++ b/index.php @@ -8,7 +8,7 @@ * * But good little boys and girls would always initialize their variables... ;-) * - * @author Roland Haeder + * @author Roland Haeder * @version 0.0.0 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software * @license GNU GPL 3.0 or any newer version -- 2.30.2