00001 <?php 00024 class BaseDatabaseWrapper extends BaseFrameworkSystem { 00028 private $cacheInstance = null; 00029 00033 private $tableName = "unknown"; 00034 00040 protected function __construct($class) { 00041 // Call parent constructor 00042 parent::__construct($class); 00043 00044 // Initialize the cache instance 00045 $this->initCacheInstance(); 00046 00047 // Clean up a little 00048 $this->removeNumberFormaters(); 00049 $this->removeSystemArray(); 00050 } 00051 00057 private final function initCacheInstance () { 00058 // Set the new instance 00059 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache(); 00060 } 00061 00069 public function doSelectByCriteria (Criteria $criteriaInstance) { 00070 // First get a key suitable for our cache and extend it with this class name 00071 $cacheKey = sprintf("%s@%s", 00072 $this->__toString(), 00073 $criteriaInstance->getCacheKey() 00074 ); 00075 00076 // Does this key exists in cache? 00077 if ($this->cacheInstance->offsetExists($cacheKey)) { 00078 // Then use this result 00079 $result = $cacheInstance->offsetGet($cacheKey); 00080 } else { 00081 // Now it's time to ask the database layer for this select statement 00082 $result = $this->getDatabaseInstance()->doSelectByTableCriteria($this->getTableName(), $criteriaInstance); 00083 00084 // Cache the result if not null 00085 if (!is_null($result)) { 00086 // A valid result has returned from the database layer 00087 $this->cacheInstance->offsetSet($cacheKey, $result); 00088 } else { 00089 // This invalid result must be wrapped 00090 $result = array( 00091 'status' => "invalid", 00092 'exception' => $this->getDatabaseInstance()->getLastException() 00093 ); 00094 } 00095 } 00096 00097 // Create an instance of a DatabaseResult class with the given result 00098 $resultInstance = DatabaseResult::createDatabaseResult($result); 00099 00100 // And return the instance 00101 return $resultInstance; 00102 } 00103 00110 public function doSelectCountByCriteria (Criteria $criteriaInstance) { 00111 // Total numbers is zero by default 00112 $numRows = 0; 00113 00114 // Get the result from above method 00115 $resultInstance = $this->doSelectByCriteria($criteriaInstance); 00116 00117 // Was that query fine? 00118 if ($resultInstance->ifStatusIsOkay()) { 00119 // Then get the number of rows 00120 $numRows = $resultInstance->getAffectedRows(); 00121 } // END - if 00122 00123 // Return the result 00124 return $numRows; 00125 } 00126 00133 protected final function setTableName ($tableName) { 00134 $this->tableName = (string) $tableName; 00135 } 00136 00142 protected final function getTableName () { 00143 return $this->tableName; 00144 } 00145 00151 public final function getPrimaryKeyValue () { 00152 // Get the table name and a database instance and ask for it 00153 $primaryKey = $this->getDatabaseInstance()->getPrimaryKeyOfTable($this->getTableName()); 00154 00155 // Return value 00156 return $primaryKey; 00157 } 00158 } 00159 00160 // [EOF] 00161 ?>
1.5.6