Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        framework
5
 * @package         database
6
 * @author          WebsiteBaker Project
7
 * @copyright       2004-2009, Ryan Djurovich
8
 * @copyright       2009-2011, Website Baker Org. e.V.
9
 * @link            http://www.websitebaker2.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.x
12
 * @requirements    PHP 5.2.2 and higher
13
 * @version         $Id: class.database.php 1507 2011-09-05 23:39:04Z Luisehahne $
14
 * @filesource      $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.database.php $
15
 * @lastmodified    $Date: 2011-09-06 01:39:04 +0200 (Tue, 06 Sep 2011) $
16
 *
17
 */
18
/*
19
Database class
20
This class will be used to interface between the database
21
and the Website Baker code
22
*/
23
/* -------------------------------------------------------- */
24
// Must include code to stop this file being accessed directly
25
if(!defined('WB_PATH')) {
26
	require_once(dirname(__FILE__).'/globalExceptionHandler.php');
27
	throw new IllegalFileException();
28
}
29
/* -------------------------------------------------------- */
30
if(!defined('DB_URL')) {
31
	//define('DB_URL', DB_TYPE.'://'.DB_USERNAME.':'.DB_PASSWORD.'@'.DB_HOST.'/'.DB_NAME);
32
}
33

    
34
define('DATABASE_CLASS_LOADED', true);
35

    
36
class database {
37

    
38
	private $db_handle  = null; // readonly from outside
39

    
40
	private $connected  = false;
41

    
42
	private $error      = '';
43
	private $error_type = '';
44
	private $message    = array();
45

    
46

    
47
	// Set DB_URL
48
	function database($url = '') {
49
		// Connect to database
50
		$this->connect();
51
		// Check for database connection error
52
		if($this->is_error()) {
53
			die($this->get_error());
54
		}
55
	}
56
	
57
	// Connect to the database
58
	function connect() {
59
		$status = $this->db_handle = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
60
		if(mysql_error()) {
61
			$this->connected = false;
62
			$this->error = mysql_error();
63
		} else {
64
			if(!mysql_select_db(DB_NAME)) {
65
				$this->connected = false;
66
				$this->error = mysql_error();
67
			} else {
68
				$this->connected = true;
69
			}
70
		}
71
		return $this->connected;
72
	}
73
	
74
	// Disconnect from the database
75
	function disconnect() {
76
		if($this->connected==true) {
77
			mysql_close();
78
			return true;
79
		} else {
80
			return false;
81
		}
82
	}
83
	
84
	// Run a query
85
	function query($statement) {
86
		$mysql = new mysql();
87
		$mysql->query($statement);
88
		$this->set_error($mysql->error());
89
		if($mysql->error()) {
90
			return null;
91
		} else {
92
			return $mysql;
93
		}
94
	}
95

    
96
	// Gets the first column of the first row
97
	function get_one( $statement )
98
	{
99
		$fetch_row = mysql_fetch_array(mysql_query($statement) );
100
		$result = $fetch_row[0];
101
		$this->set_error(mysql_error());
102
		if(mysql_error()) {
103
			return null;
104
		} else {
105
			return $result;
106
		}
107
	}
108
	
109
	// Set the DB error
110
	function set_error($message = null) {
111
		global $TABLE_DOES_NOT_EXIST, $TABLE_UNKNOWN;
112
		$this->error = $message;
113
		if(strpos($message, 'no such table')) {
114
			$this->error_type = $TABLE_DOES_NOT_EXIST;
115
		} else {
116
			$this->error_type = $TABLE_UNKNOWN;
117
		}
118
	}
119
	
120
	// Return true if there was an error
121
	function is_error() {
122
		return (!empty($this->error)) ? true : false;
123
	}
124
	
125
	// Return the error
126
	function get_error() {
127
		return $this->error;
128
	}
129

    
130
/*
131
 * default Getter
132
 */
133
	public function __get($var_name)
134
	{
135
		if($var_name == 'db_handle')
136
		{
137
			return $this->db_handle;
138
		}
139
		return null;
140
	}
141

    
142
/*
143
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
144
 * @param string $field_name: name of the field to seek for
145
 * @return bool: true if field exists
146
 */
147
	public function field_exists($table_name, $field_name)
148
	{
149
		$sql = 'DESCRIBE `'.$table_name.'` `'.$field_name.'` ';
150
		$query = $this->query($sql);
151
		return ($query->numRows() != 0);
152
	}
153

    
154
/*
155
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
156
 * @param string $index_name: name of the index to seek for
157
 * @return bool: true if field exists
158
 */
159
	public function index_exists($table_name, $index_name, $number_fields = 0)
160
	{
161
		$number_fields = intval($number_fields);
162
		$keys = 0;
163
		$sql = 'SHOW INDEX FROM `'.$table_name.'`';
164
		if( ($res_keys = $this->query($sql)) )
165
		{
166
			while(($rec_key = $res_keys->fetchRow()))
167
			{
168
				if( $rec_key['Key_name'] == $index_name )
169
				{
170
					$keys++;
171
				}
172
			}
173

    
174
		}
175
		if( $number_fields == 0 )
176
		{
177
			return ($keys != $number_fields);
178
		}else
179
		{
180
			return ($keys == $number_fields);
181
		}
182
	}
183
/*
184
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
185
 * @param string $field_name: name of the field to add
186
 * @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
187
 * @return bool: true if successful, otherwise false and error will be set
188
 */
189
	public function field_add($table_name, $field_name, $description)
190
	{
191
		if( !$this->field_exists($table_name, $field_name) )
192
		{ // add new field into a table
193
			$sql = 'ALTER TABLE `'.$table_name.'` ADD '.$field_name.' '.$description.' ';
194
			$query = $this->query($sql);
195
			$this->set_error(mysql_error());
196
			if( !$this->is_error() )
197
			{
198
				return ( $this->field_exists($table_name, $field_name) ) ? true : false;
199
			}
200
		}else
201
		{
202
			$this->set_error('field \''.$field_name.'\' already exists');
203
		}
204
		return false;
205
	}
206

    
207
/*
208
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
209
 * @param string $field_name: name of the field to add
210
 * @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
211
 * @return bool: true if successful, otherwise false and error will be set
212
 */
213
	public function field_modify($table_name, $field_name, $description)
214
	{
215
		$retval = false;
216
		if( $this->field_exists($table_name, $field_name) )
217
		{ // modify a existing field in a table
218
			$sql  = 'ALTER TABLE `'.$table_name.'` MODIFY `'.$field_name.'` '.$description;
219
			$retval = ( $this->query($sql) ? true : false);
220
			$this->set_error(mysql_error());
221
		}
222
		return $retval;
223
	}
224

    
225
/*
226
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
227
 * @param string $field_name: name of the field to remove
228
 * @return bool: true if successful, otherwise false and error will be set
229
 */
230
	public function field_remove($table_name, $field_name)
231
	{
232
		$retval = false;
233
		if( $this->field_exists($table_name, $field_name) )
234
		{ // modify a existing field in a table
235
			$sql  = 'ALTER TABLE `'.$table_name.'` DROP `'.$field_name.'`';
236
			$retval = ( $this->query($sql) ? true : false );
237
		}
238
		return $retval;
239
	}
240

    
241
/*
242
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
243
 * @param string $index_name: name of the new index
244
 * @param string $field_list: comma seperated list of fields for this index
245
 * @param string $index_type: kind of index (UNIQUE, PRIMARY, '')
246
 * @return bool: true if successful, otherwise false and error will be set
247
 */
248
	public function index_add($table_name, $index_name, $field_list, $index_type = '')
249
	{
250
		$retval = false;
251
		$field_list = str_replace(' ', '', $field_list);
252
		$field_list = explode(',', $field_list);
253
		$number_fields = sizeof($field_list);
254
		$field_list = '`'.implode('`,`', $field_list).'`';
255
		if( $this->index_exists($table_name, $index_name, $number_fields) ||
256
		    $this->index_exists($table_name, $index_name))
257
		{
258
			$sql  = 'ALTER TABLE `'.$table_name.'` ';
259
			$sql .= 'DROP INDEX `'.$index_name.'`';
260
			if( $this->query($sql))
261
			{
262
				$sql  = 'ALTER TABLE `'.$table_name.'` ';
263
				$sql .= 'ADD '.$index_type.' `'.$index_name.'` ( '.$field_list.' ); ';
264
				if( $this->query($sql)) { $retval = true; }
265
			}
266
		}
267
		return $retval;
268
	}
269

    
270
/*
271
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
272
 * @param string $field_name: name of the field to remove
273
 * @return bool: true if successful, otherwise false and error will be set
274
 */
275
	public function index_remove($table_name, $index_name)
276
	{
277
		$retval = false;
278
		if( $this->index_exists($table_name, $index_name) )
279
		{ // modify a existing field in a table
280
			$sql  = 'ALTER TABLE `'.$table_name.'` DROP INDEX `'.$index_name.'`';
281
			$retval = ( $this->query($sql) ? true : false );
282
		}
283
		return $retval;
284
	}
285

    
286
} /// end of class database
287

    
288
class mysql {
289

    
290
	// Run a query
291
	function query($statement) {
292
		$this->result = mysql_query($statement);
293
		$this->error = mysql_error();
294
		return $this->result;
295
	}
296
	
297
	// Fetch num rows
298
	function numRows() {
299
		return mysql_num_rows($this->result);
300
	}
301

    
302
	// Fetch row  $typ = MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH
303
	function fetchRow($typ = MYSQL_BOTH) {
304
		return mysql_fetch_array($this->result, $typ);
305
	}
306

    
307
	// Get error
308
	function error() {
309
		if(isset($this->error)) {
310
			return $this->error;
311
		} else {
312
			return null;
313
		}
314
	}
315

    
316
}
317
/* this function is placed inside this file temporarely until a better place is found */
318
/*  function to update a var/value-pair(s) in table ****************************
319
 *  nonexisting keys are inserted
320
 *  @param string $table: name of table to use (without prefix)
321
 *  @param mixed $key:    a array of key->value pairs to update
322
 *                        or a string with name of the key to update
323
 *  @param string $value: a sting with needed value, if $key is a string too
324
 *  @return bool:  true if any keys are updated, otherwise false
325
 */
326
	function db_update_key_value($table, $key, $value = '')
327
	{
328
		global $database;
329
		if( !is_array($key))
330
		{
331
			if( trim($key) != '' )
332
			{
333
				$key = array( trim($key) => trim($value) );
334
			} else {
335
				$key = array();
336
			}
337
		}
338
		$retval = true;
339
		foreach( $key as $index=>$val)
340
		{
341
			$index = strtolower($index);
342
			$sql = 'SELECT COUNT(`setting_id`) FROM `'.TABLE_PREFIX.$table.'` WHERE `name` = \''.$index.'\' ';
343
			if($database->get_one($sql))
344
			{
345
				$sql = 'UPDATE ';
346
				$sql_where = 'WHERE `name` = \''.$index.'\'';
347
			}else {
348
				$sql = 'INSERT INTO ';
349
				$sql_where = '';
350
			}
351
			$sql .= '`'.TABLE_PREFIX.$table.'` ';
352
			$sql .= 'SET `name` = \''.$index.'\', ';
353
			$sql .= '`value` = \''.$val.'\' '.$sql_where;
354
			if( !$database->query($sql) )
355
			{
356
				$retval = false;
357
			}
358
		}
359
		return $retval;
360
	}
361
?>
(8-8/22)