If no valid entry (e.g. failed query) is counted, this does now return -1
[core.git] / inc / classes / main / database / class_BaseDatabaseWrapper.php
1 <?php
2 /**
3  * A generic database wrapper
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class BaseDatabaseWrapper extends BaseFrameworkSystem {
25         /**
26          * Cache instance
27          */
28         private $cacheInstance = null;
29
30         /**
31          * Current table name to use
32          */
33         private $tableName = 'unknown';
34
35         /**
36          * Protected constructor
37          *
38          * @return      void
39          */
40         protected function __construct ($class) {
41                 // Call parent constructor
42                 parent::__construct($class);
43
44                 // Initialize the cache instance
45                 $this->initCacheInstance();
46         }
47
48         /**
49          * Initializes the cache instance with a new object
50          *
51          * @return      void
52          */
53         private final function initCacheInstance () {
54                 // Set the new instance
55                 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
56         }
57
58         /**
59          * Do a "select" query on the current table with the given search criteria and
60          * store it in cache for later usage
61          *
62          * @param       $criteriaInstance       An instance of a Criteria class
63          * @return      $resultInstance         An instance of a database result class
64          */
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",
68                         $this->__toString(),
69                         $criteriaInstance->getCacheKey()
70                 );
71
72                 // Does this key exists in cache?
73                 if ($this->cacheInstance->offsetExists($cacheKey)) {
74                         // Then use this result
75                         $result = $cacheInstance->offsetGet($cacheKey);
76                 } else {
77                         // Now it's time to ask the database layer for this select statement
78                         $result = $this->getDatabaseInstance()->doSelectByTableCriteria($this->getTableName(), $criteriaInstance);
79
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);
84                         } else {
85                                 // This invalid result must be wrapped
86                                 $result = array(
87                                         'status'                => 'invalid',
88                                         'exception'             => $this->getDatabaseInstance()->getLastException()
89                                 );
90                         }
91                 }
92
93                 // Create an instance of a DatabaseResult class with the given result
94                 $resultInstance = DatabaseResult::createDatabaseResult($result);
95
96                 // And return the instance
97                 return $resultInstance;
98         }
99
100         /**
101          * Count the numbers of rows we shall receive
102          *
103          * @param       $criteriaInstance       An instance of a Criteria class
104          * @return      $numRows                        Numbers of rows of database entries
105          */
106         public function doSelectCountByCriteria (Criteria $criteriaInstance) {
107                 // Total numbers is -1 so we can distinglish between failed and valid queries
108                 $numRows = 0;
109
110                 // Get the result from above method
111                 $resultInstance = $this->doSelectByCriteria($criteriaInstance);
112
113                 // Was that query fine?
114                 if ($resultInstance->ifStatusIsOkay()) {
115                         // Then get the number of rows
116                         $numRows = $resultInstance->getAffectedRows();
117                 } // END - if
118
119                 // Return the result
120                 return $numRows;
121         }
122
123         /**
124          * Setter for table name
125          *
126          * @param       $tableName      Name of table name to set
127          * @return      void
128          */
129         protected final function setTableName ($tableName) {
130                 $this->tableName = (string) $tableName;
131         }
132
133         /**
134          * Getter for table name
135          *
136          * @return      $tableName      Name of table name to set
137          */
138         protected final function getTableName () {
139                 return $this->tableName;
140         }
141
142         /**
143          * Getter for primary key used in wrapped table
144          *
145          * @return      $primaryKey             Primary key used in wrapped table
146          */
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());
150
151                 // Return value
152                 return $primaryKey;
153         }
154 }
155
156 // [EOF]
157 ?>