3 * A database wrapper for news classes
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 NewsDatabaseWrapper extends BaseDatabaseWrapper {
28 private $cacheInstance = null;
30 // Constants for exceptions
31 const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0x160;
33 // Constants for database columns
35 // Constants for database table names
36 const DB_TABLE_NEWS = "news";
39 * Protected constructor
43 protected function __construct() {
44 // Call parent constructor
45 parent::__construct(__CLASS__);
47 // Set part description
48 $this->setObjectDescription("Database wrapper for user objects");
50 // Create unique ID number
51 $this->generateUniqueId();
55 * Creates an instance of this database wrapper by a provided user class
57 * @return $wrapperInstance An instance of the created wrapper class
59 public final static function createNewsDatabaseWrapper () {
61 $wrapperInstance = new NewsDatabaseWrapper();
63 // Initialize the cache instance
64 $wrapperInstance->initCacheInstance();
66 // Return the instance
67 return $wrapperInstance;
71 * Initializes the cache instance with a new object
75 protected function initCacheInstance () {
76 // Set the new instance
77 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
81 * Do a "select" query on the user table with the given search criteria and
82 * store it in cache for later usage
84 * @param $criteriaInstance An instance of a Criteria class
85 * @return $resultInstance An instance of a database result class
87 public function doSelectByCriteria (Criteria $criteriaInstance) {
88 // First get a key suitable for our cache and extend it with this class name
89 $cacheKey = sprintf("%s@%s",
91 $criteriaInstance->getCacheKey()
94 // Does this key exists in cache?
95 if ($this->cacheInstance->offsetExists($cacheKey)) {
96 // Then use this result
97 $result = $cacheInstance->offsetGet($cacheKey);
99 // Now it's time to ask the database layer for this select statement
100 $result = $this->getDatabaseInstance()->doSelectByTableCriteria(self::DB_TABLE_NEWS, $criteriaInstance);
102 // Cache the result if not null
103 if (!is_null($result)) {
104 // A valid result has returned from the database layer
105 $this->cacheInstance->offsetSet($cacheKey, $result);
107 // This invalid result must be wrapped
109 'status' => "invalid",
110 'exception' => $this->getDatabaseInstance()->getLastException()
115 // Create an instance of a DatabaseResult class with the given result
116 $resultInstance = DatabaseResult::createDatabaseResult($result);
118 // And return the instance
119 return $resultInstance;