/**
* 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
*/
- function offsetExists ($offset);
+ function offsetExists($offset);
+
+ /**
+ * Setter for cache offset
+ *
+ * @param $offset The offset we shall set
+ * @param $data Data to store in cache
+ * @return void
+ */
+ function 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 cache
+ */
+ function offsetGet($offset);
+
+ /**
+ * Purges the given cache entry
+ *
+ * @param $offset The offset we shall set
+ * @return void
+ */
+ function purgeOffset($offset);
}
// [EOF]
* @param $offset The offset we are looking for
* @return $exists Wether the offset exists
*/
- public final function offsetExists ($offset) {
+ public function offsetExists ($offset) {
$exists = $this->dataCache->offsetExists($offset);
return $exists;
}
* @param $data Data to store in cache
* @return void
*/
- public final function offsetSet ($offset, $data) {
+ public function offsetSet ($offset, $data) {
$this->dataCache->offsetSet($offset, $data);
}
* @param $offset The offset we shall set
* @return $data Data to store in cache
*/
- public final function offsetGet ($offset) {
+ public function offsetGet ($offset) {
// Default is offset not found
$data = NULL;
if ($this->offsetExists($offset)) {
// Then get the data from it
$data = $this->dataCache->offsetGet($offset);
- }
+ } // END - if
// Return data
return $data;
}
+
+ /**
+ * Purges the given cache entry
+ *
+ * @param $offset The offset we shall set
+ * @return void
+ */
+ public function purgeOffset ($offset) {
+ // Is the offset there?
+ if ($this->offsetExists($offset)) {
+ // Purge only existing keys
+ /* DEBUG: */ $this->debugOutput('CACHE: Unsetting cache ' . $offset);
+ $this->dataCache->offsetUnset($offset);
+ } // END - if
+ }
}
// [EOF]
// Return it
return $responseType;
}
+
+ /**
+ * Gets a cache key from Criteria instance
+ *
+ * @param $criteriaInstance An instance of a Criteria class
+ * @param $onlyKeys Only use these keys for a cache key
+ * @return $cacheKey A cache key suitable for lookup/storage purposes
+ */
+ protected function getCacheKeyByCriteria (Criteria $criteriaInstance, $onlyKeys = array()) {
+ // Generate it
+ $cacheKey = sprintf("%s@%s",
+ $this->__toString(),
+ $criteriaInstance->getCacheKey($onlyKeys)
+ );
+
+ // And return it
+ //* NOISY-DEBUG: */ $this->debugOutput($this->__toString() . ': cacheKey=' . $cacheKey);
+ return $cacheKey;
+ }
}
// [EOF]
*/
private $wrapperConfigEntry = '';
+ /**
+ * Criteria to handle
+ */
+ private $criteria = array();
/**
* Protected constructor
*
public final function getWrapperConfigEntry () {
return $this->wrapperConfigEntry;
}
+
+ /**
+ * Getter for criteria array
+ *
+ * @return $criteria
+ */
+ public final function getCriteriaArray () {
+ return $this->criteria;
+ }
+
+ /**
+ * Add criteria
+ *
+ * @param $criteriaKey Criteria key
+ * @param $criteriaValue Criteria value
+ * @return void
+ */
+ public final function addCriteria ($criteriaKey, $criteriaValue) {
+ $this->criteria[(string)$criteriaKey] = (string)$criteriaValue;
+ }
+
+ /**
+ * Add configured criteria
+ *
+ * @param $criteriaKey Criteria key
+ * @param $configEntry Configuration entry
+ * @return void
+ */
+ public final function addConfiguredCriteria ($criteriaKey, $configEntry) {
+ // Add the configuration entry as a criteria
+ $value = $this->getConfigInstance()->getConfigEntry($configEntry);
+ $this->addCriteria($criteriaKey, $value);
+ }
+
+ /**
+ * Get criteria element or null if not found
+ *
+ * @param $criteria The criteria we want to have
+ * @return $value Wether the value of the critera or null
+ */
+ public function getCriteriaElemnent ($criteria) {
+ // Default is not found
+ $value = NULL;
+
+ // Is the criteria there?
+ if (isset($this->criteria[$criteria])) {
+ // Then use it
+ $value = $this->criteria[$criteria];
+ }
+
+ // Return the value
+ return $value;
+ }
+
+ /**
+ * Checks wether given array entry matches
+ *
+ * @param $entryArray Array with the entries to find
+ * @return $matches Wether the entry matches or not
+ */
+ public function ifEntryMatches (array $entryArray) {
+ // First nothing matches and nothing is counted
+ $matches = false;
+ $counted = 0;
+
+ // Walk through all entries
+ foreach ($entryArray as $key => $entry) {
+ // Then walk through all search criteria
+ foreach ($this->criteria as $criteriaKey => $criteriaValue) {
+ // Is the element found and does it match?
+ if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
+ // Then count this one up
+ $counted++;
+ } // END - if
+ } // END - foreach
+ } // END - foreach
+
+ // Now check if expected criteria counts match
+ $matches = ($counted == count($this->criteria));
+
+ // Return the result
+ return $matches;
+ }
+
+ /**
+ * "Getter" for a cache key
+ *
+ * @param $onlyKeys Only use these keys for a cache key
+ * @return $cacheKey The key suitable for the cache system
+ */
+ public function getCacheKey ($onlyKeys = array()) {
+ // Initialize the key
+ $cacheKey = '';
+
+ // Now walk through all criterias
+ foreach ($this->criteria as $criteriaKey => $criteriaValue) {
+ // Is the value in array or is $onlyKeys empty?
+ if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) {
+ // Add the value URL encoded to avoid any trouble with special characters
+ $cacheKey .= sprintf("%s=%s;",
+ $criteriaKey,
+ urlencode($criteriaValue)
+ );
+ } // END - if
+ } // END - foreach
+
+ // Remove last semicolon
+ $cacheKey = substr($cacheKey, 0, -1);
+
+ // Is the instance SearchCriteria?
+ if ($this instanceof SearchCriteria) {
+ // Check if 'limit' and 'skip' are in
+ if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) {
+ // Add limit and skip values
+ $cacheKey .= sprintf(";%%limit%%=%s;%%skip%%=%s",
+ $this->getLimit(),
+ $this->getSkip()
+ );
+ } // END - if
+ } // END - if
+
+ // Return the cache key
+ return $cacheKey;
+ }
}
// [EOF]
*/
private $tableName = '';
- /**
- * Table columns (criteria) to store
- */
- private $tableColumns = array();
-
/**
* Unique key
*/
return $criteriaInstance;
}
- /**
- * Add criteria
- *
- * @param $criteriaKey Criteria key
- * @param $criteriaValue Criteria value
- * @return void
- */
- public function addCriteria ($criteriaKey, $criteriaValue) {
- $this->tableColumns[(string) $criteriaKey] = $criteriaValue;
- }
-
- /**
- * Add configured criteria
- *
- * @param $criteriaKey Criteria key
- * @param $configEntry Configuration entry
- * @return void
- */
- public function addConfiguredCriteria ($criteriaKey, $configEntry) {
- // Add configuration entry as criteria
- $value = $this->getConfigInstance()->getConfigEntry($configEntry);
- $this->addCriteria($criteriaKey, $value);
- }
-
/**
* Setter for table name
*
* @return $uniqueValue Value of the unique key
*/
public final function getUniqueValue () {
- return $this->tableColumns[$this->getUniqueKey()];
- }
-
- /**
- * Getter for criteria array
- *
- * @return $tableColumns
- */
- public final function getCriteriaArray () {
- return $this->tableColumns;
+ return $this->getCriteriaElemnent($this->getUniqueKey());
}
/**
/**
* Criteria to handle
*/
- private $searchCriteria = array();
+ private $criteria = array();
/**
* Limitation for the search
return $criteriaInstance;
}
- /**
- * Add criteria
- *
- * @param $criteriaKey Criteria key
- * @param $criteriaValue Criteria value
- * @return void
- */
- public final function addCriteria ($criteriaKey, $criteriaValue) {
- $this->searchCriteria[(string)$criteriaKey] = (string)$criteriaValue;
- }
-
- /**
- * Add configured criteria
- *
- * @param $criteriaKey Criteria key
- * @param $configEntry Configuration entry
- * @return void
- */
- public final function addConfiguredCriteria ($criteriaKey, $configEntry) {
- // Add the configuration entry as a criteria
- $value = $this->getConfigInstance()->getConfigEntry($configEntry);
- $this->addCriteria($criteriaKey, $value);
- }
-
/**
* Setter for limit
*
public final function getSkip () {
return $this->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;
- }
-
- /**
- * Get criteria element or null if not found
- *
- * @param $criteria The criteria we want to have
- * @return $value Wether the value of the critera or null
- */
- public function getCriteriaElemnent ($criteria) {
- // Default is not found
- $value = NULL;
-
- // Is the criteria there?
- if (isset($this->searchCriteria[$criteria])) {
- // Then use it
- $value = $this->searchCriteria[$criteria];
- }
-
- // Return the value
- return $value;
- }
-
- /**
- * Checks wether given array entry matches
- *
- * @param $entryArray Array with the entries to find
- * @return $matches Wether the entry matches or not
- */
- public function ifEntryMatches (array $entryArray) {
- // First nothing matches and nothing is counted
- $matches = false;
- $counted = 0;
-
- // Walk through all entries
- foreach ($entryArray as $key => $entry) {
- // Then walk through all search criteria
- foreach ($this->searchCriteria as $criteriaKey => $criteriaValue) {
- // Is the element found and does it match?
- if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
- // Then count this one up
- $counted++;
- } // END - if
- } // END - foreach
- } // END - foreach
-
- // Now check if expected criteria counts match
- $matches = ($counted == count($this->searchCriteria));
-
- // Return the result
- return $matches;
- }
}
// [EOF]
// Constants for exceptions
const EXCEPTION_SQL_QUERY = 0x140;
- /**
- * The limiter instance
- */
- private $limitInstance = NULL;
+ // Result array indexes
+ const RESULT_INDEX_ROWS = 'rows';
+ const RESULT_INDEX_STATUS = 'status';
+ const RESULT_INDEX_EXCEPTION = 'exception';
/**
* Protected constructor
$this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
}
+ /**
+ * Setter for table name
+ *
+ * @param $tableName Name of table name to set
+ * @return void
+ */
+ protected final function setTableName ($tableName) {
+ $this->tableName = (string) $tableName;
+ }
+
+ /**
+ * Getter for table name
+ *
+ * @return $tableName Name of table name to set
+ */
+ protected final function getTableName () {
+ return $this->tableName;
+ }
+
+ /**
+ * 'Inserts' a data set instance into a local file database folder
+ *
+ * @param $dataSetInstance A storeable data set
+ * @param $onlyKeys Only use these keys for a cache key
+ * @return void
+ */
+ protected function queryInsertDataSet (StoreableCriteria $dataSetInstance, $onlyKeys = array()) {
+ // First get a key suitable for our cache and extend it with this class name
+ $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
+ //* DEBUG: */ $this->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...');
+
+ // Does this key exists in cache?
+ if ($this->cacheInstance->offsetExists($cacheKey)) {
+ // Purge the cache
+ $this->cacheInstance->purgeOffset($cacheKey);
+ } // END - if
+
+ // Handle it over to the middleware
+ $this->getDatabaseInstance()->queryInsertDataSet($dataSetInstance);
+ }
+
+ /**
+ * 'Updates' a data set instance with a database layer
+ *
+ * @param $dataSetInstance A storeable data set
+ * @param $onlyKeys Only use these keys for a cache key
+ * @return void
+ */
+ protected function queryUpdateDataSet (StoreableCriteria $dataSetInstance, $onlyKeys = array()) {
+ // First get a key suitable for our cache and extend it with this class name
+ $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
+ //* DEBUG: */ $this->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...');
+
+ // Does this key exists in cache?
+ if ($this->cacheInstance->offsetExists($cacheKey)) {
+ // Purge the cache
+ $this->cacheInstance->purgeOffset($cacheKey);
+ } // END - if
+
+ // Handle it over to the middleware
+ $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
+ }
+
/**
* Do a "select" query on the current table with the given search criteria and
* store it in cache for later usage
*
* @param $criteriaInstance An instance of a Criteria class
+ * @param $onlyKeys Only use these keys for a cache key
* @return $resultInstance An instance of a database result class
*/
- public function doSelectByCriteria (Criteria $criteriaInstance) {
+ public function doSelectByCriteria (Criteria $criteriaInstance, $onlyKeys = array()) {
// First get a key suitable for our cache and extend it with this class name
- $cacheKey = sprintf("%s@%s",
- $this->__toString(),
- $criteriaInstance->getCacheKey()
- );
+ $cacheKey = $this->getCacheKeyByCriteria($criteriaInstance, $onlyKeys);
// Does this key exists in cache?
if ($this->cacheInstance->offsetExists($cacheKey)) {
+ // Debug message
+ //* DEBUG: */ $this->debugOutput('BASE-WRAPPER: Cache used for cacheKey=' . $cacheKey);
+
// Then use this result
$result = $this->cacheInstance->offsetGet($cacheKey);
} else {
+ // Debug message
+ //* DEBUG: */ $this->debugOutput('BASE-WRAPPER: Quering database, cacheKey=' . $cacheKey);
+
// Now it's time to ask the database layer for this select statement
$result = $this->getDatabaseInstance()->doSelectByTableCriteria($this->getTableName(), $criteriaInstance);
} else {
// This invalid result must be wrapped
$result = array(
- 'status' => 'invalid',
- 'exception' => $this->getDatabaseInstance()->getLastException()
+ BaseDatabaseFrontend::RESULT_INDEX_STATUS => 'invalid',
+ BaseDatabaseFrontend::RESULT_INDEX_EXCEPTION => $this->getDatabaseInstance()->getLastException()
);
}
}
* Count the numbers of rows we shall receive
*
* @param $criteriaInstance An instance of a Criteria class
+ * @param $onlyKeys Only use these keys for a cache key
* @return $numRows Numbers of rows of database entries
*/
- public function doSelectCountByCriteria (Criteria $criteriaInstance) {
+ public function doSelectCountByCriteria (Criteria $criteriaInstance, $onlyKeys = array()) {
// Total numbers is -1 so we can distinglish between failed and valid queries
$numRows = 0;
// Get the result from above method
- $resultInstance = $this->doSelectByCriteria($criteriaInstance);
+ $resultInstance = $this->doSelectByCriteria($criteriaInstance, $onlyKeys);
// Was that query fine?
if ($resultInstance->ifStatusIsOkay()) {
// Then get the number of rows
$numRows = $resultInstance->getAffectedRows();
+
+ // Debug message
+ //* DEBUG: */ $this->debugOutput('BASE-WRAPPER: numRows=' . $numRows);
} // END - if
// Return the result
return $numRows;
}
- /**
- * Setter for table name
- *
- * @param $tableName Name of table name to set
- * @return void
- */
- protected final function setTableName ($tableName) {
- $this->tableName = (string) $tableName;
- }
-
- /**
- * Getter for table name
- *
- * @return $tableName Name of table name to set
- */
- protected final function getTableName () {
- return $this->tableName;
- }
-
/**
* Getter for primary key used in wrapped table
*
// Initialize the result data, this need to be rewritten e.g. if a local file cannot be read
$resultData = array(
- 'status' => LocalfileDatabase::RESULT_OKAY,
- 'rows' => array()
+ BaseDatabaseFrontend::RESULT_INDEX_STATUS => LocalfileDatabase::RESULT_OKAY,
+ BaseDatabaseFrontend::RESULT_INDEX_ROWS => array()
);
// Initialize limit/skip
$idx = 1;
// Read the directory with some exceptions
- while (($dataFile = $directoryInstance->readDirectoryExcept(array('.', '..', '.htaccess', '.svn', "info." . $this->getFileExtension()))) && ($limitFound < $criteriaInstance->getLimit())) {
+ while (($dataFile = $directoryInstance->readDirectoryExcept(array('.', '..', '.htaccess', '.svn', 'info.' . $this->getFileExtension()))) && (($limitFound < $criteriaInstance->getLimit()) || ($criteriaInstance->getLimit() == 0))) {
// Does the extension match?
if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
// Skip this file!
$criteria = $criteriaInstance->getCriteriaElemnent($key);
// Is the criteria met?
+ //* NOISY-DEBUG: */ $this->debugOutput('DATABASE: criteria[' . gettype($criteria) . ']=' . $criteria . ',()=' . strlen($criteria) . ',value=' . $value . ',()=' . strlen($value));
if ((!is_null($criteria)) && ($criteria == $value)) {
-
// Shall we skip this entry?
if ($criteriaInstance->getSkip() > 0) {
// We shall skip some entries
$dataArray[$this->getIndexKey()] = $idx;
// Entry found!
- $resultData['rows'][] = $dataArray;
+ //* NOISY-DEBUG: */ $this->debugOutput('DATABASE: indexKey=' . $this->getIndexKey() . ',idx=' . $idx . ',dataArray=' . print_r($dataArray, true));
+ $resultData[BaseDatabaseFrontend::RESULT_INDEX_ROWS][] = $dataArray;
// Count found entries up
$limitFound++;
} catch (PathIsNoDirectoryException $e) {
// Path not found means "table not found" for real databases...
$this->lastException = $e;
- $this->lastError = $e->getMessage();
+ $this->lastError = $e->getMessage();
// So throw an SqlException here with faked error message
throw new SqlException (array($this, sprintf("Table '%s' not found", $tableName), self::DB_CODE_TABLE_MISSING), self::EXCEPTION_SQL_QUERY);
} catch (FrameworkException $e) {
// Catch all exceptions and store them in last error
$this->lastException = $e;
- $this->lastError = $e->getMessage();
+ $this->lastError = $e->getMessage();
}
// Return the gathered result
$this->lastError = $e->getMessage();
// Throw an SQL exception
- throw new SqlException (array($this, sprintf("Cannot write data to table '%s', is the table created?", $dataSetInstance->getTableName()), self::DB_CODE_TABLE_UNWRITEABLE), self::EXCEPTION_SQL_QUERY);
+ throw new SqlException(array($this, sprintf("Cannot write data to table '%s', is the table created?", $dataSetInstance->getTableName()), self::DB_CODE_TABLE_UNWRITEABLE), self::EXCEPTION_SQL_QUERY);
}
}
$this->lastError = $e->getMessage();
// Throw an SQL exception
- throw new SqlException (array($this, sprintf("Cannot write data to table '%s', is the table created?", $dataSetInstance->getTableName()), self::DB_CODE_TABLE_UNWRITEABLE), self::EXCEPTION_SQL_QUERY);
+ throw new SqlException(array($this, sprintf("Cannot write data to table '%s', is the table created?", $dataSetInstance->getTableName()), self::DB_CODE_TABLE_UNWRITEABLE), self::EXCEPTION_SQL_QUERY);
}
}
$registrationInstance->addElementsToDataSet($dataSetInstance);
// "Insert" this request instance completely into the database
- $this->getDatabaseInstance()->queryInsertDataSet($dataSetInstance);
+ $this->queryInsertDataSet($dataSetInstance);
}
/**
$pointsInstance->addElementsToDataSet($dataSetInstance);
// "Insert" this request instance completely into the database
- $this->getDatabaseInstance()->queryInsertDataSet($dataSetInstance);
+ $this->queryInsertDataSet($dataSetInstance);
}
/**
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-abstract class BaseFilterDecorator extends BaseFrameworkSystem implements Filterable {
+class BaseFilterDecorator extends BaseFrameworkSystem implements Filterable {
/**
* The decorated filter instance
*/
// Set the result array
$resultInstance->setResultArray($resultArray);
+ // Set affected rows
+ $resultInstance->setAffectedRows(count($resultArray[BaseDatabaseFrontend::RESULT_INDEX_ROWS]));
+
// Return the instance
return $resultInstance;
}
// Now get the update criteria array and update all entries
foreach ($updateInstance->getUpdateCriteria() as $criteriaKey => $criteriaValue) {
// Update data
- $this->resultArray['rows'][$entryKey][$criteriaKey] = $criteriaValue;
+ $this->resultArray[BaseDatabaseFrontend::RESULT_INDEX_ROWS][$entryKey][$criteriaKey] = $criteriaValue;
// Mark it as out-dated
$this->outDated[$criteriaKey] = 1;
if ($this->valid()) {
// Next entry found, so count one up and cache it
$this->currentPos++;
- $this->currentRow = $this->resultArray['rows'][$this->currentPos];
+ $this->currentRow = $this->resultArray[BaseDatabaseFrontend::RESULT_INDEX_ROWS][$this->currentPos];
$nextValid = true;
} // END - if
$current = NULL;
// Does the current enty exist?
- if (isset($this->resultArray['rows'][$this->currentPos])) {
+ if (isset($this->resultArray[BaseDatabaseFrontend::RESULT_INDEX_ROWS][$this->currentPos])) {
// Then get it
- $current = $this->resultArray['rows'][$this->currentPos];
+ $current = $this->resultArray[BaseDatabaseFrontend::RESULT_INDEX_ROWS][$this->currentPos];
} // END - if
// Return the result
$isValid = false;
// Check if
- if (($this->ifStatusIsOkay()) && (isset($this->resultArray['rows'][($this->currentPos + 1)])) && (isset($this->resultArray['rows'][0]))) {
+ if (($this->ifStatusIsOkay()) && (isset($this->resultArray[BaseDatabaseFrontend::RESULT_INDEX_ROWS][($this->currentPos + 1)])) && (isset($this->resultArray[BaseDatabaseFrontend::RESULT_INDEX_ROWS][0]))) {
// All fine!
$isValid = true;
} // END - if
* @return $ifStatusOkay Wether the status of the query was okay
*/
public function ifStatusIsOkay () {
- return ((isset($this->resultArray['status'])) && ($this->resultArray['status'] === LocalfileDatabase::RESULT_OKAY));
+ return ((isset($this->resultArray[BaseDatabaseFrontend::RESULT_INDEX_STATUS])) && ($this->resultArray[BaseDatabaseFrontend::RESULT_INDEX_STATUS] === LocalfileDatabase::RESULT_OKAY));
}
/**
* @todo 0% done
*/
public function searchEntry (LocalSearchCriteria $criteriaInstance) {
- die(__METHOD__.": Unfinished!");
+ die(__METHOD__.': Unfinished!');
}
/**