3 # Copyright (C) 2006-2010 Tobias Leupold <tobias.leupold@web.de>
5 # This file is part of the b8 package
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation in version 2.1 of the License.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 # License for more details.
16 # You should have received a copy of the GNU Lesser General Public License
17 # along with this program; if not, write to the Free Software Foundation,
18 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21 * The DBA (Berkeley DB) abstraction layer for communicating with the database.
22 * Copyright (C) 2006-2010 Tobias Leupold <tobias.leupold@web.de>
27 * @author Tobias Leupold
30 class b8_storage_dba extends b8_storage_base
33 public $config = array(
34 'database' => 'wordlist.db',
38 public $b8_config = array(
39 'degenerator' => NULL,
45 const DATABASE_CONNECTION_FAIL = 'DATABASE_CONNECTION_FAIL';
48 * Constructs the database layer.
51 * @param string $config
54 function __construct($config, $degenerator, $today)
57 # Pass some variables of the main b8 config to this class
58 $this->b8_config['degenerator'] = $degenerator;
59 $this->b8_config['today'] = $today;
61 # Validate the config items
62 if(count($config) > 0) {
63 foreach ($config as $name => $value) {
64 $this->config[$name] = (string) $value;
71 * Closes the database connection.
79 if($this->_db !== NULL) {
80 dba_close($this->_db);
81 $this->connected = FALSE;
86 * Connect to the database and do some checks.
89 * @return mixed Returns TRUE on a successful database connection, otherwise returns a constant from b8.
92 public function connect()
95 # Have we already connected?
96 if($this->_db !== NULL)
99 # Open the database connection
100 $this->_db = dba_open(dirname(__FILE__) . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . $this->config['database'], "w", $this->config['handler']);
102 if($this->_db === FALSE) {
103 $this->connected = FALSE;
105 return self::DATABASE_CONNECTION_FAIL;
108 # Everything is okay and connected
110 $this->connected = TRUE;
112 # Let's see if this is a b8 database and the version is okay
113 return $this->check_database();
118 * Does the actual interaction with the database when fetching data.
121 * @param array $tokens
122 * @return mixed Returns an array of the returned data in the format array(token => data) or an empty array if there was no data.
125 protected function _get_query($tokens)
130 foreach ($tokens as $token) {
132 $count = dba_fetch($token, $this->_db);
135 $data[$token] = $count;
144 * Store a token to the database.
147 * @param string $token
148 * @param string $count
149 * @return bool TRUE on success or FALSE on failure
152 protected function _put($token, $count) {
153 return dba_insert($token, $count, $this->_db);
157 * Update an existing token.
160 * @param string $token
161 * @param string $count
162 * @return bool TRUE on success or FALSE on failure
165 protected function _update($token, $count)
167 return dba_replace($token, $count, $this->_db);
171 * Remove a token from the database.
174 * @param string $token
175 * @return bool TRUE on success or FALSE on failure
178 protected function _del($token)
180 return dba_delete($token, $this->_db);
190 protected function _commit()
192 # We just need this function because the (My)SQL backend(s) need it.