1 |
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$
|
|
14 |
* @filesource $HeadURL: $
|
|
15 |
* @lastmodified $Date: $
|
|
16 |
*
|
|
17 |
*/
|
2 |
18 |
|
3 |
|
// $Id$
|
4 |
|
|
5 |
19 |
/*
|
6 |
20 |
|
7 |
|
Website Baker Project <http://www.websitebaker.org/>
|
8 |
|
Copyright (C) 2004-2009, Ryan Djurovich
|
9 |
|
|
10 |
|
Website Baker is free software; you can redistribute it and/or modify
|
11 |
|
it under the terms of the GNU General Public License as published by
|
12 |
|
the Free Software Foundation; either version 2 of the License, or
|
13 |
|
(at your option) any later version.
|
14 |
|
|
15 |
|
Website Baker is distributed in the hope that it will be useful,
|
16 |
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17 |
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18 |
|
GNU General Public License for more details.
|
19 |
|
|
20 |
|
You should have received a copy of the GNU General Public License
|
21 |
|
along with Website Baker; if not, write to the Free Software
|
22 |
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
23 |
|
|
24 |
|
*/
|
25 |
|
|
26 |
|
/*
|
27 |
|
|
28 |
21 |
Database class
|
29 |
22 |
|
30 |
23 |
This class will be used to interface between the database
|
... | ... | |
45 |
38 |
define('DATABASE_CLASS_LOADED', true);
|
46 |
39 |
|
47 |
40 |
class database {
|
48 |
|
|
|
41 |
|
|
42 |
private $db_handle = null; // readonly from outside
|
|
43 |
|
|
44 |
private $connected = false;
|
|
45 |
|
|
46 |
private $error = '';
|
|
47 |
private $error_type = '';
|
|
48 |
private $message = array();
|
|
49 |
|
|
50 |
|
49 |
51 |
// Set DB_URL
|
50 |
52 |
function database($url = '') {
|
51 |
53 |
// Connect to database
|
... | ... | |
94 |
96 |
return $mysql;
|
95 |
97 |
}
|
96 |
98 |
}
|
97 |
|
|
|
99 |
|
98 |
100 |
// Gets the first column of the first row
|
99 |
|
function get_one($statement) {
|
100 |
|
$fetch_row = mysql_fetch_row(mysql_query($statement));
|
|
101 |
function get_one( $statement )
|
|
102 |
{
|
|
103 |
$fetch_row = mysql_fetch_array(mysql_query($statement) );
|
101 |
104 |
$result = $fetch_row[0];
|
102 |
105 |
$this->set_error(mysql_error());
|
103 |
106 |
if(mysql_error()) {
|
... | ... | |
127 |
130 |
function get_error() {
|
128 |
131 |
return $this->error;
|
129 |
132 |
}
|
130 |
|
|
131 |
|
}
|
132 |
133 |
|
|
134 |
/*
|
|
135 |
* default Getter
|
|
136 |
*/
|
|
137 |
public function __get($var_name)
|
|
138 |
{
|
|
139 |
if($var_name == 'db_handle')
|
|
140 |
{
|
|
141 |
return $this->db_handle;
|
|
142 |
}
|
|
143 |
return null;
|
|
144 |
}
|
|
145 |
|
|
146 |
/*
|
|
147 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
|
148 |
* @param string $field_name: name of the field to seek for
|
|
149 |
* @return bool: true if field exists
|
|
150 |
*/
|
|
151 |
public function field_exists($table_name, $field_name)
|
|
152 |
{
|
|
153 |
$sql = 'DESCRIBE `'.$table_name.'` `'.$field_name.'` ';
|
|
154 |
$query = $this->query($sql);
|
|
155 |
return ($query->numRows() != 0);
|
|
156 |
}
|
|
157 |
|
|
158 |
/*
|
|
159 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
|
160 |
* @param string $index_name: name of the index to seek for
|
|
161 |
* @return bool: true if field exists
|
|
162 |
*/
|
|
163 |
public function index_exists($table_name, $index_name, $number_fields = 0)
|
|
164 |
{
|
|
165 |
$number_fields = intval($number_fields);
|
|
166 |
$keys = 0;
|
|
167 |
$sql = 'SHOW INDEX FROM `'.$table_name.'`';
|
|
168 |
if( ($res_keys = $this->query($sql)) )
|
|
169 |
{
|
|
170 |
while(($rec_key = $res_keys->fetchRow()))
|
|
171 |
{
|
|
172 |
if( $rec_key['Key_name'] == $index_name )
|
|
173 |
{
|
|
174 |
$keys++;
|
|
175 |
}
|
|
176 |
}
|
|
177 |
|
|
178 |
}
|
|
179 |
if( $number_fields == 0 )
|
|
180 |
{
|
|
181 |
return ($keys != $number_fields);
|
|
182 |
}else
|
|
183 |
{
|
|
184 |
return ($keys == $number_fields);
|
|
185 |
}
|
|
186 |
}
|
|
187 |
/*
|
|
188 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
|
189 |
* @param string $field_name: name of the field to add
|
|
190 |
* @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
|
|
191 |
* @return bool: true if successful, otherwise false and error will be set
|
|
192 |
*/
|
|
193 |
public function field_add($table_name, $field_name, $description)
|
|
194 |
{
|
|
195 |
if( !$this->field_exists($field_name, $table_name) )
|
|
196 |
{ // add new field into a table
|
|
197 |
$sql = 'ALTER TABLE `'.$table_name.'` ADD '.$field_name.' '.$description.' ';
|
|
198 |
$query = $this->query($sql);
|
|
199 |
$this->set_error(mysql_error());
|
|
200 |
if( !$this->is_error() )
|
|
201 |
{
|
|
202 |
return ( $this->field_exists($field_name, $table_name) ) ? true : false;
|
|
203 |
}
|
|
204 |
}else
|
|
205 |
{
|
|
206 |
$this->set_error('field \''.$field_name.'\' already exists');
|
|
207 |
}
|
|
208 |
return false;
|
|
209 |
}
|
|
210 |
|
|
211 |
/*
|
|
212 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
|
213 |
* @param string $field_name: name of the field to add
|
|
214 |
* @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
|
|
215 |
* @return bool: true if successful, otherwise false and error will be set
|
|
216 |
*/
|
|
217 |
public function field_modify($table_name, $field_name, $description)
|
|
218 |
{
|
|
219 |
$retval = false;
|
|
220 |
if( $this->field_exists($field_name, $table_name) )
|
|
221 |
{ // modify a existing field in a table
|
|
222 |
$sql = 'ALTER TABLE `'.$table_name.'` DROP `'.$field_name.'`';
|
|
223 |
}
|
|
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 |
|
133 |
289 |
class mysql {
|
134 |
290 |
|
135 |
291 |
// Run a query
|
additional functions added in class.database