Code syncronized with shipsimu code base
[mailer.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, this is free software
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                 // Clean up a little
48                 $this->removeNumberFormaters();
49                 $this->removeSystemArray();
50         }
51
52         /**
53          * Initializes the cache instance with a new object
54          *
55          * @return      void
56          */
57         private final function initCacheInstance () {
58                 // Set the new instance
59                 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
60         }
61
62         /**
63          * Do a "select" query on the current table with the given search criteria and
64          * store it in cache for later usage
65          *
66          * @param       $criteriaInstance       An instance of a Criteria class
67          * @return      $resultInstance         An instance of a database result class
68          */
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",
72                         $this->__toString(),
73                         $criteriaInstance->getCacheKey()
74                 );
75
76                 // Does this key exists in cache?
77                 if ($this->cacheInstance->offsetExists($cacheKey)) {
78                         // Then use this result
79                         $result = $cacheInstance->offsetGet($cacheKey);
80                 } else {
81                         // Now it's time to ask the database layer for this select statement
82                         $result = $this->getDatabaseInstance()->doSelectByTableCriteria($this->getTableName(), $criteriaInstance);
83
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);
88                         } else {
89                                 // This invalid result must be wrapped
90                                 $result = array(
91                                         'status'                => "invalid",
92                                         'exception'             => $this->getDatabaseInstance()->getLastException()
93                                 );
94                         }
95                 }
96
97                 // Create an instance of a DatabaseResult class with the given result
98                 $resultInstance = DatabaseResult::createDatabaseResult($result);
99
100                 // And return the instance
101                 return $resultInstance;
102         }
103
104         /**
105          * Count the numbers of rows we shall receive
106          *
107          * @param       $criteriaInstance       An instance of a Criteria class
108          * @return      $numRows                        Numbers of rows of database entries
109          */
110         public function doSelectCountByCriteria (Criteria $criteriaInstance) {
111                 // Total numbers is zero by default
112                 $numRows = 0;
113
114                 // Get the result from above method
115                 $resultInstance = $this->doSelectByCriteria($criteriaInstance);
116
117                 // Was that query fine?
118                 if ($resultInstance->ifStatusIsOkay()) {
119                         // Then get the number of rows
120                         $numRows = $resultInstance->getAffectedRows();
121                 } // END - if
122
123                 // Return the result
124                 return $numRows;
125         }
126
127         /**
128          * Setter for table name
129          *
130          * @param       $tableName      Name of table name to set
131          * @return      void
132          */
133         protected final function setTableName ($tableName) {
134                 $this->tableName = (string) $tableName;
135         }
136
137         /**
138          * Getter for table name
139          *
140          * @return      $tableName      Name of table name to set
141          */
142         protected final function getTableName () {
143                 return $this->tableName;
144         }
145
146         /**
147          * Getter for primary key used in wrapped table
148          *
149          * @return      $primaryKey             Primary key used in wrapped table
150          */
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());
154
155                 // Return value
156                 return $primaryKey;
157         }
158 }
159
160 // [EOF]
161 ?>