*/
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.
* @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;
}
/**
* 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
} 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);
}
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]