3 * A generic database wrapper
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class BaseDatabaseWrapper extends BaseFrameworkSystem {
28 private $cacheInstance = null;
31 * Current table name to use
33 private $tableName = "unknown";
36 * Protected constructor
40 protected function __construct($class) {
41 // Call parent constructor
42 parent::__construct($class);
44 // Initialize the cache instance
45 $this->initCacheInstance();
48 $this->removeNumberFormaters();
49 $this->removeSystemArray();
53 * Initializes the cache instance with a new object
57 private final function initCacheInstance () {
58 // Set the new instance
59 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
63 * Do a "select" query on the current table with the given search criteria and
64 * store it in cache for later usage
66 * @param $criteriaInstance An instance of a Criteria class
67 * @return $resultInstance An instance of a database result class
69 public function doSelectByCriteria (Criteria $criteriaInstance) {
70 // First get a key suitable for our cache and extend it with this class name
71 $cacheKey = sprintf("%s@%s",
73 $criteriaInstance->getCacheKey()
76 // Does this key exists in cache?
77 if ($this->cacheInstance->offsetExists($cacheKey)) {
78 // Then use this result
79 $result = $cacheInstance->offsetGet($cacheKey);
81 // Now it's time to ask the database layer for this select statement
82 $result = $this->getDatabaseInstance()->doSelectByTableCriteria($this->getTableName(), $criteriaInstance);
84 // Cache the result if not null
85 if (!is_null($result)) {
86 // A valid result has returned from the database layer
87 $this->cacheInstance->offsetSet($cacheKey, $result);
89 // This invalid result must be wrapped
91 'status' => "invalid",
92 'exception' => $this->getDatabaseInstance()->getLastException()
97 // Create an instance of a DatabaseResult class with the given result
98 $resultInstance = DatabaseResult::createDatabaseResult($result);
100 // And return the instance
101 return $resultInstance;
105 * Count the numbers of rows we shall receive
107 * @param $criteriaInstance An instance of a Criteria class
108 * @return $numRows Numbers of rows of database entries
110 public function doSelectCountByCriteria (Criteria $criteriaInstance) {
111 // Total numbers is zero by default
114 // Get the result from above method
115 $resultInstance = $this->doSelectByCriteria($criteriaInstance);
117 // Was that query fine?
118 if ($resultInstance->ifStatusIsOkay()) {
119 // Then get the number of rows
120 $numRows = $resultInstance->getAffectedRows();
128 * Setter for table name
130 * @param $tableName Name of table name to set
133 protected final function setTableName ($tableName) {
134 $this->tableName = (string) $tableName;
138 * Getter for table name
140 * @return $tableName Name of table name to set
142 protected final function getTableName () {
143 return $this->tableName;
147 * Getter for primary key used in wrapped table
149 * @return $primaryKey Primary key used in wrapped table
151 public final function getPrimaryKeyValue () {
152 // Get the table name and a database instance and ask for it
153 $primaryKey = $this->getDatabaseInstance()->getPrimaryKeyOfTable($this->getTableName());