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 1486 2011-08-08 12:03:20Z DarkViper $
14
 * @filesource      $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.database.php $
15
 * @lastmodified    $Date: 2011-08-08 14:03:20 +0200 (Mon, 08 Aug 2011) $
16
 *
17
 */
18

    
19
/*
20

    
21
Database class
22

    
23
This class will be used to interface between the database
24
and the Website Baker code
25

    
26
*/
27

    
28
// Must include code to stop this file being access directly
29
if(defined('WB_PATH') == false) { die("Cannot access this file directly"); }
30

    
31
if(!defined('DB_URL')) {
32
	//define('DB_URL', DB_TYPE.'://'.DB_USERNAME.':'.DB_PASSWORD.'@'.DB_HOST.'/'.DB_NAME);
33
}
34

    
35
define('DATABASE_CLASS_LOADED', true);
36

    
37
class database {
38

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

    
41
	private $connected  = false;
42

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

    
47

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

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

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

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

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

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

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

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

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

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

    
287
} /// end of class database
288

    
289
class mysql {
290

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

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

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

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