* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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;
// An instance of this class
private static $thisInstance = null;
- // Private constructor
+ /**
+ * Protected constructor
+ *
+ * @return void
+ */
protected function __construct() {
// Call parent constructor
parent::__construct(__CLASS__);
// Create an unique ID
$this->createUniqueID();
-
- // Clean up a little
- $this->removeSystemArray();
}
// Create new database connection layer
}
// 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;
*
* @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);
* 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);
* 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);
/**
* 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);
/**
* 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]