aboutsummaryrefslogtreecommitdiffstats
path: root/library/spam/b8/storage/storage_dba.php
blob: 04618b23ef7c175f77c344d602a01726245fc251 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php

#   Copyright (C) 2006-2010 Tobias Leupold <tobias.leupold@web.de>
#
#   This file is part of the b8 package
#
#   This program is free software; you can redistribute it and/or modify it
#   under the terms of the GNU Lesser General Public License as published by
#   the Free Software Foundation in version 2.1 of the License.
#
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
#   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
#   License for more details.
#
#   You should have received a copy of the GNU Lesser General Public License
#   along with this program; if not, write to the Free Software Foundation,
#   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

/**
 * The DBA (Berkeley DB) abstraction layer for communicating with the database.
 * Copyright (C) 2006-2010 Tobias Leupold <tobias.leupold@web.de>
 *
 * @license LGPL
 * @access public
 * @package b8
 * @author Tobias Leupold
 */

class b8_storage_dba extends b8_storage_base
{

	public $config = array(
		'database'    => 'wordlist.db',
		'handler'     => 'db4',
	);

	public $b8_config = array(
		'degenerator' => NULL,
		'today'       => NULL
	);

	private $_db          = NULL;

	const DATABASE_CONNECTION_FAIL = 'DATABASE_CONNECTION_FAIL';

	/**
	 * Constructs the database layer.
	 *
	 * @access public
	 * @param string $config
	 */

	function __construct($config, $degenerator, $today)
	{

		# Pass some variables of the main b8 config to this class
		$this->b8_config['degenerator'] = $degenerator;
		$this->b8_config['today']       = $today;

		# Validate the config items
		if(count($config) > 0) {
			foreach ($config as $name => $value) {
				$this->config[$name] = (string) $value;
			}
		}

	}

	/**
	 * Closes the database connection.
	 *
	 * @access public
	 * @return void
	 */

	function __destruct()
	{
		if($this->_db !== NULL) {
			dba_close($this->_db);
			$this->connected = FALSE;
		}
	}

	/**
	 * Connect to the database and do some checks.
	 *
	 * @access public
	 * @return mixed Returns TRUE on a successful database connection, otherwise returns a constant from b8.
	 */

	public function connect()
	{

		# Have we already connected?
		if($this->_db !== NULL)
			return TRUE;

		# Open the database connection
		$this->_db = dba_open(dirname(__FILE__) . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . $this->config['database'], "w", $this->config['handler']);

		if($this->_db === FALSE) {
			$this->connected = FALSE;
			$this->_db = NULL;
			return self::DATABASE_CONNECTION_FAIL;
		}

		# Everything is okay and connected

		$this->connected = TRUE;

		# Let's see if this is a b8 database and the version is okay
		return $this->check_database();

	}

	/**
	 * Does the actual interaction with the database when fetching data.
	 *
	 * @access protected
	 * @param array $tokens
	 * @return mixed Returns an array of the returned data in the format array(token => data) or an empty array if there was no data.
	 */

	protected function _get_query($tokens)
	{

		$data = array();

		foreach ($tokens as $token) {

			$count = dba_fetch($token, $this->_db);

			if($count !== FALSE)
				$data[$token] = $count;

		}

		return $data;

	}

	/**
	 * Store a token to the database.
	 *
	 * @access protected
	 * @param string $token
	 * @param string $count
	 * @return bool TRUE on success or FALSE on failure
	 */

	protected function _put($token, $count) {
		return dba_insert($token, $count, $this->_db);
	}

	/**
	 * Update an existing token.
	 *
	 * @access protected
	 * @param string $token
	 * @param string $count
	 * @return bool TRUE on success or FALSE on failure
	 */

	protected function _update($token, $count)
	{
		return dba_replace($token, $count, $this->_db);
	}

	/**
	 * Remove a token from the database.
	 *
	 * @access protected
	 * @param string $token
	 * @return bool TRUE on success or FALSE on failure
	 */

	protected function _del($token)
	{
		return dba_delete($token, $this->_db);
	}

	/**
	 * Does nothing :-D
	 *
	 * @access protected
	 * @return void
	 */

	protected function _commit()
	{
		# We just need this function because the (My)SQL backend(s) need it.
		return;
	}

}

?>