// Constants for MySQL backward-compatiblity (PLEASE FIX THEM!)
const DB_CODE_TABLE_MISSING = 0x010;
const DB_CODE_TABLE_UNWRITEABLE = 0x011;
+ const DB_CODE_DATA_FILE_CORRUPT = 0x012;
/**
* Save path for "file database"
*
* @param $object The object we shall save to the local file system
* @return void
+ * @deprecated
*/
public final function saveObject (FrameworkInterface $object) {
// Get a string containing the serialized object. We cannot exchange
* @see ObjectLimits An object holding limition information
* @see SerializationContainer A special container class for e.g.
* attributes from limited objects
+ * @deprecated
*/
private function serializeObject (FrameworkInterface $object) {
// If there is no limiter instance we serialize the whole object
return $contents;
}
+ /**
+ * Reads a local data file and returns it's contents in an array
+ *
+ * @param $fqfn The FQFN for the requested file
+ * @return $dataArray
+ */
+ private function getDataArrayFromFile ($fqfn) {
+ // Get a file pointer
+ $fileInstance = FrameworkFileInputPointer::createFrameworkFileInputPointer($fqfn);
+
+ // Get the raw data and BASE64-decode it
+ $compressedData = base64_decode($fileInstance->readLinesFromFile());
+
+ // Close the file and throw the instance away
+ $fileInstance->closeFile();
+ unset($fileInstance);
+
+ // Decompress it
+ $serializedData = $this->getCompressorChannel()->getCompressor()->decompressStream($compressedData);
+
+ // Unserialize it
+ $dataArray = unserialize($serializedData);
+
+ // Finally return it
+ return $dataArray;
+ }
+
+ /**
+ * Writes data array to local file
+ *
+ * @param $fqfn The FQFN of the local file
+ * @param $dataArray An array with all the data we shall write
+ * @return void
+ */
+ private function writeDataArrayToFqfn ($fqfn, array $dataArray) {
+ // Get a file pointer instance
+ $fileInstance = FrameworkFileOutputPointer::createFrameworkFileOutputPointer($fqfn, 'w');
+
+ // Serialize and compress it
+ $compressedData = $this->getCompressorChannel()->getCompressor()->compressStream(serialize($dataArray));
+
+ // Write this data BASE64 encoded to the file
+ $fileInstance->writeToFile(base64_encode($compressedData));
+
+ // Close the file pointer
+ $fileInstance->closeFile();
+ }
+
/**
* Makes sure that the database connection is alive
*
// Read the directory with some exceptions
while (($dataFile = $directoryInstance->readDirectoryExcept(array(".", "..", ".htaccess", ".svn"))) && ($limitFound < $criteriaInstance->getLimit())) {
- // Open this file for reading
- $filePointer = FrameworkFileInputPointer::createFrameworkFileInputPointer($pathName . $dataFile);
-
- // Get the raw data and BASE64-decode it
- $compressedData = base64_decode($filePointer->readLinesFromFile());
-
- // Close the file and throw the instance away
- $filePointer->closeFile();
- unset($filePointer);
-
- // Decompress it
- $serializedData = $this->getCompressorChannel()->getCompressor()->decompressStream($compressedData);
-
- // Unserialize it
- $dataArray = unserialize($serializedData);
+ // Read the file
+ $dataArray = $this->getDataArrayFromFile($pathName . $dataFile);
// Is this an array?
if (is_array($dataArray)) {
} // END - if
// Entry found!
- $resultData['rows'][] = $dataArray;
+ $resultData['rows'][] = $dataArray;
$limitFound++;
break;
} // END - if
} // END - foreach
- } // END - if
+ } else {
+ // Throw an exception here
+ throw new SqlException(sprintf("File '%s' contains invalid data.", $dataFile), self::DB_CODE_DATA_FILE_CORRUPT);
+ }
} // END - while
// Close directory and throw the instance away
);
// Try to save the request away
+ try {
+ // Write the data away
+ $this->writeDataArrayToFqfn($fqfn, $dataSetInstance->getCriteriaArray());
+
+ // Reset last error message and exception
+ $this->resetLastError();
+ } catch (FrameworkException $e) {
+ // Catch all exceptions and store them in last error
+ $this->lastException = $e;
+ $this->lastError = $e->getMessage();
+
+ // Throw an SQL exception
+ throw new SqlException (array($this, sprintf("Cannot write data to table '%s'", $tableName), self::DB_CODE_TABLE_UNWRITEABLE), self::EXCEPTION_SQL_QUERY);
+ }
+ }
+
+ /**
+ * "Updates" a data set instance with a database layer
+ *
+ * @param $dataSetInstance A storeable data set
+ * @return void
+ * @throws SqlException If an SQL error occurs
+ */
+ public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
+ // Create full path name
+ $pathName = $this->getSavePath() . $dataSetInstance->getTableName() . '/';
+
+ // Try all the requests
try {
// Get a file pointer instance
- $filePointer = FrameworkFileOutputPointer::createFrameworkFileOutputPointer($fqfn, 'w');
+ $directoryInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($pathName);
+
+ // Initialize limit/skip
+ $limitFound = 0; $skipFound = 0;
// Get the criteria array from the dataset
$criteriaArray = $dataSetInstance->getCriteriaArray();
- // Serialize and compress it
- $compressedData = $this->getCompressorChannel()->getCompressor()->compressStream(serialize($criteriaArray));
+ // Get search criteria
+ $searchInstance = $dataSetInstance->getSearchInstance();
+
+ // Read the directory with some exceptions
+ while (($dataFile = $directoryInstance->readDirectoryExcept(array(".", "..", ".htaccess", ".svn"))) && ($limitFound < $searchInstance->getLimit())) {
+ // Open this file for reading
+ $dataArray = $this->getDataArrayFromFile($pathName . $dataFile);
+
+ // Is this an array?
+ if (is_array($dataArray)) {
+ // Search in the criteria with FMFW (First Matches, First Wins)
+ foreach ($dataArray as $key=>$value) {
+ // Get criteria element
+ $criteria = $searchInstance->getCriteriaElemnent($key);
+
+ // Is the criteria met?
+ if ((!is_null($criteria)) && ($criteria == $value)) {
+
+ // Shall we skip this entry?
+ if ($searchInstance->getSkip() > 0) {
+ // We shall skip some entries
+ if ($skipFound < $searchInstance->getSkip()) {
+ // Skip this entry
+ $skipFound++;
+ break;
+ } // END - if
+ } // END - if
+
+ // Entry found, so update it
+ foreach ($criteriaArray as $criteriaKey=>$criteriaValue) {
+ $dataArray[$criteriaKey] = $criteriaValue;
+ } // END - foreach
+
+ // Write the data to a local file
+ $this->writeDataArrayToFqfn($pathName . $dataFile, $dataArray);
- // Write this data BASE64 encoded to the file
- $filePointer->writeToFile(base64_encode($compressedData));
+ // Count it
+ $limitFound++;
+ break;
+ } // END - if
+ } // END - foreach
+ } // END - if
+ } // END - while
// Close the file pointer
- $filePointer->closeFile();
+ $directoryInstance->closeDirectory();
// Reset last error message and exception
$this->resetLastError();