3 * A generic database wrapper
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
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();
49 * Initializes the cache instance with a new object
53 private final function initCacheInstance () {
54 // Set the new instance
55 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
59 * Do a "select" query on the current table with the given search criteria and
60 * store it in cache for later usage
62 * @param $criteriaInstance An instance of a Criteria class
63 * @return $resultInstance An instance of a database result class
65 public function doSelectByCriteria (Criteria $criteriaInstance) {
66 // First get a key suitable for our cache and extend it with this class name
67 $cacheKey = sprintf("%s@%s",
69 $criteriaInstance->getCacheKey()
72 // Does this key exists in cache?
73 if ($this->cacheInstance->offsetExists($cacheKey)) {
74 // Then use this result
75 $result = $cacheInstance->offsetGet($cacheKey);
77 // Now it's time to ask the database layer for this select statement
78 $result = $this->getDatabaseInstance()->doSelectByTableCriteria($this->getTableName(), $criteriaInstance);
80 // Cache the result if not null
81 if (!is_null($result)) {
82 // A valid result has returned from the database layer
83 $this->cacheInstance->offsetSet($cacheKey, $result);
85 // This invalid result must be wrapped
87 'status' => "invalid",
88 'exception' => $this->getDatabaseInstance()->getLastException()
93 // Create an instance of a DatabaseResult class with the given result
94 $resultInstance = DatabaseResult::createDatabaseResult($result);
96 // And return the instance
97 return $resultInstance;
101 * Count the numbers of rows we shall receive
103 * @param $criteriaInstance An instance of a Criteria class
104 * @return $numRows Numbers of rows of database entries
106 public function doSelectCountByCriteria (Criteria $criteriaInstance) {
107 // Total numbers is zero by default
110 // Get the result from above method
111 $resultInstance = $this->doSelectByCriteria($criteriaInstance);
113 // Was that query fine?
114 if ($resultInstance->ifStatusIsOkay()) {
115 // Then get the number of rows
116 $numRows = $resultInstance->getAffectedRows();
124 * Setter for table name
126 * @param $tableName Name of table name to set
129 protected final function setTableName ($tableName) {
130 $this->tableName = (string) $tableName;
134 * Getter for table name
136 * @return $tableName Name of table name to set
138 protected final function getTableName () {
139 return $this->tableName;
143 * Getter for primary key used in wrapped table
145 * @return $primaryKey Primary key used in wrapped table
147 public final function getPrimaryKeyValue () {
148 // Get the table name and a database instance and ask for it
149 $primaryKey = $this->getDatabaseInstance()->getPrimaryKeyOfTable($this->getTableName());