root / nephthys_db.php
View | Annotate | Download (16.6 KB)
| 1 | 1211fb5a | Andreas | <?php |
|---|---|---|---|
| 2 | 1211fb5a | Andreas | |
| 3 | 1211fb5a | Andreas | /***************************************************************************
|
| 4 | 1211fb5a | Andreas | * |
| 5 | 9a37d81c | Andreas | * Nephthys - file sharing management |
| 6 | 1211fb5a | Andreas | * Copyright (c) by Andreas Unterkircher, unki@netshadow.at |
| 7 | 1211fb5a | Andreas | * |
| 8 | 9a37d81c | Andreas | * This file is part of Nephthys. |
| 9 | 9a37d81c | Andreas | * |
| 10 | 9a37d81c | Andreas | * Nephthys is free software: you can redistribute it and/or modify |
| 11 | 1211fb5a | Andreas | * it under the terms of the GNU General Public License as published by |
| 12 | 9a37d81c | Andreas | * the Free Software Foundation, either version 3 of the License, or |
| 13 | 1211fb5a | Andreas | * (at your option) any later version. |
| 14 | 1211fb5a | Andreas | * |
| 15 | 9a37d81c | Andreas | * Nephthys is distributed in the hope that it will be useful, |
| 16 | 1211fb5a | Andreas | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | 1211fb5a | Andreas | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | 1211fb5a | Andreas | * GNU General Public License for more details. |
| 19 | 1211fb5a | Andreas | * |
| 20 | 1211fb5a | Andreas | * You should have received a copy of the GNU General Public License |
| 21 | 9a37d81c | Andreas | * along with Nephthys. If not, see <http://www.gnu.org/licenses/>. |
| 22 | 1211fb5a | Andreas | * |
| 23 | 1211fb5a | Andreas | ***************************************************************************/ |
| 24 | 1211fb5a | Andreas | |
| 25 | 1211fb5a | Andreas | class NEPHTHYS_DB {
|
| 26 | 1211fb5a | Andreas | |
| 27 | 1211fb5a | Andreas | private $db;
|
| 28 | 1211fb5a | Andreas | private $cfg;
|
| 29 | 1211fb5a | Andreas | private $is_connected;
|
| 30 | 1211fb5a | Andreas | private $last_error;
|
| 31 | 9507541c | Andreas | private $parent;
|
| 32 | 1211fb5a | Andreas | |
| 33 | 1211fb5a | Andreas | /**
|
| 34 | 1211fb5a | Andreas | * NEPHTHYS_DB class constructor |
| 35 | 1211fb5a | Andreas | * |
| 36 | 1211fb5a | Andreas | * This constructor initially connect to the database. |
| 37 | 1211fb5a | Andreas | */ |
| 38 | 9507541c | Andreas | public function __construct() |
| 39 | 1211fb5a | Andreas | {
|
| 40 | 9507541c | Andreas | global $nephthys;
|
| 41 | 9507541c | Andreas | $this->parent =& $nephthys;
|
| 42 | 9507541c | Andreas | |
| 43 | 9507541c | Andreas | $this->cfg = $this->parent->cfg; |
| 44 | 1211fb5a | Andreas | |
| 45 | 1211fb5a | Andreas | /* We are starting disconnected */
|
| 46 | 1211fb5a | Andreas | $this->setConnStatus(false); |
| 47 | 1211fb5a | Andreas | |
| 48 | 1211fb5a | Andreas | /* Connect to MySQL Database */
|
| 49 | 1211fb5a | Andreas | $this->db_connect();
|
| 50 | 1211fb5a | Andreas | |
| 51 | 1211fb5a | Andreas | } // __construct()
|
| 52 | 1211fb5a | Andreas | |
| 53 | 1211fb5a | Andreas | /**
|
| 54 | 1211fb5a | Andreas | * NEPHTHYS_DB class deconstructor |
| 55 | 1211fb5a | Andreas | * |
| 56 | 1211fb5a | Andreas | * This destructor will close the current database connection. |
| 57 | 1211fb5a | Andreas | */ |
| 58 | 1211fb5a | Andreas | public function __destruct() |
| 59 | 1211fb5a | Andreas | {
|
| 60 | 1211fb5a | Andreas | $this->db_disconnect();
|
| 61 | 1211fb5a | Andreas | |
| 62 | 1211fb5a | Andreas | } // _destruct()
|
| 63 | 1211fb5a | Andreas | |
| 64 | 1211fb5a | Andreas | /**
|
| 65 | 1211fb5a | Andreas | * NEPHTHYS_DB database connect |
| 66 | 1211fb5a | Andreas | * |
| 67 | 1211fb5a | Andreas | * This function will connect to the database via MDB2 |
| 68 | 1211fb5a | Andreas | */ |
| 69 | 1211fb5a | Andreas | private function db_connect() |
| 70 | 1211fb5a | Andreas | {
|
| 71 | 1211fb5a | Andreas | $options = array(
|
| 72 | 1211fb5a | Andreas | 'debug' => 2, |
| 73 | 1211fb5a | Andreas | 'portability' => 'DB_PORTABILITY_ALL' |
| 74 | 1211fb5a | Andreas | ); |
| 75 | 1211fb5a | Andreas | |
| 76 | 4e920782 | Andreas | switch($this->cfg->db_type) { |
| 77 | 4e920782 | Andreas | default:
|
| 78 | 4e920782 | Andreas | case 'mysql': |
| 79 | de253b48 | Andreas | $dsn = "mysqli://"
|
| 80 | 4e920782 | Andreas | . $this->cfg->mysql_user .":" |
| 81 | 4e920782 | Andreas | . $this->cfg->mysql_pass ."@" |
| 82 | 4e920782 | Andreas | . $this->cfg->mysql_host ."/" |
| 83 | 4e920782 | Andreas | . $this->cfg->mysql_db;
|
| 84 | 4e920782 | Andreas | break;
|
| 85 | 4e920782 | Andreas | case 'sqlite': |
| 86 | 4e920782 | Andreas | $dsn = "sqlite:///"
|
| 87 | 4e920782 | Andreas | . $this->cfg->sqlite_path;
|
| 88 | 4e920782 | Andreas | break;
|
| 89 | 4e920782 | Andreas | |
| 90 | 4e920782 | Andreas | } |
| 91 | 4e920782 | Andreas | |
| 92 | 1211fb5a | Andreas | $this->db = MDB2::connect($dsn, $options);
|
| 93 | 1211fb5a | Andreas | |
| 94 | 1211fb5a | Andreas | if(PEAR::isError($this->db)) { |
| 95 | 1211fb5a | Andreas | $this->throwError("Unable to connect to database: ". $this->db->getMessage() .' - '. $this->db->getUserInfo()); |
| 96 | 1211fb5a | Andreas | $this->setConnStatus(false); |
| 97 | 1211fb5a | Andreas | } |
| 98 | 1211fb5a | Andreas | |
| 99 | 1211fb5a | Andreas | $this->setConnStatus(true); |
| 100 | 1211fb5a | Andreas | |
| 101 | 1211fb5a | Andreas | } // db_connect()
|
| 102 | 1211fb5a | Andreas | |
| 103 | 1211fb5a | Andreas | /**
|
| 104 | 1211fb5a | Andreas | * NEPHTHYS_DB database disconnect |
| 105 | 1211fb5a | Andreas | * |
| 106 | 1211fb5a | Andreas | * This function will disconnected an established database connection. |
| 107 | 1211fb5a | Andreas | */ |
| 108 | 1211fb5a | Andreas | private function db_disconnect() |
| 109 | 1211fb5a | Andreas | {
|
| 110 | 1211fb5a | Andreas | $this->db->disconnect();
|
| 111 | 1211fb5a | Andreas | |
| 112 | 1211fb5a | Andreas | } // db_disconnect()
|
| 113 | 1211fb5a | Andreas | |
| 114 | 1211fb5a | Andreas | /**
|
| 115 | 1211fb5a | Andreas | * NEPHTHYS_DB database query |
| 116 | 1211fb5a | Andreas | * |
| 117 | 1211fb5a | Andreas | * This function will execute a SQL query and return the result as |
| 118 | 1211fb5a | Andreas | * object. |
| 119 | 1211fb5a | Andreas | */ |
| 120 | 1211fb5a | Andreas | public function db_query($query = "", $mode = MDB2_FETCHMODE_OBJECT) |
| 121 | 1211fb5a | Andreas | {
|
| 122 | 1211fb5a | Andreas | if($this->getConnStatus()) { |
| 123 | 1211fb5a | Andreas | |
| 124 | 1211fb5a | Andreas | $this->db->setFetchMode($mode);
|
| 125 | 1211fb5a | Andreas | |
| 126 | 1211fb5a | Andreas | /* for manipulating queries use exec instead of query. can save
|
| 127 | 1211fb5a | Andreas | * some resource because nothing has to be allocated for results. |
| 128 | 1211fb5a | Andreas | */ |
| 129 | 1211fb5a | Andreas | if(preg_match('/^(update|insert)i/', $query)) { |
| 130 | 1211fb5a | Andreas | $result = $this->db->exec($query);
|
| 131 | 1211fb5a | Andreas | } |
| 132 | 1211fb5a | Andreas | else {
|
| 133 | 1211fb5a | Andreas | $result = $this->db->query($query);
|
| 134 | 1211fb5a | Andreas | } |
| 135 | 1211fb5a | Andreas | |
| 136 | 1211fb5a | Andreas | if(PEAR::isError($result))
|
| 137 | 1211fb5a | Andreas | $this->throwError($result->getMessage() .' - '. $result->getUserInfo()); |
| 138 | 1211fb5a | Andreas | |
| 139 | 1211fb5a | Andreas | return $result;
|
| 140 | 1211fb5a | Andreas | } |
| 141 | 1211fb5a | Andreas | else
|
| 142 | 729c0028 | Andreas | $this->throwError("Can't execute query - we are not connected!"); |
| 143 | 1211fb5a | Andreas | |
| 144 | 1211fb5a | Andreas | } // db_query()
|
| 145 | 1211fb5a | Andreas | |
| 146 | 1211fb5a | Andreas | /**
|
| 147 | b1fa0a38 | Andreas | * NEPHTHYS_DB database prepare query |
| 148 | b1fa0a38 | Andreas | * |
| 149 | b1fa0a38 | Andreas | * This function will prepare a SQL query to be executed |
| 150 | b1fa0a38 | Andreas | * @param string $query |
| 151 | b1fa0a38 | Andreas | * @param int $mode |
| 152 | b1fa0a38 | Andreas | * @return mixed |
| 153 | b1fa0a38 | Andreas | */ |
| 154 | b1fa0a38 | Andreas | public function db_prepare($query = "") |
| 155 | b1fa0a38 | Andreas | {
|
| 156 | b1fa0a38 | Andreas | if($this->getConnStatus()) { |
| 157 | b1fa0a38 | Andreas | |
| 158 | 69f0963f | Andreas | $this->db->prepare($query);
|
| 159 | b1fa0a38 | Andreas | |
| 160 | b1fa0a38 | Andreas | /* for manipulating queries use exec instead of query. can save
|
| 161 | b1fa0a38 | Andreas | * some resource because nothing has to be allocated for results. |
| 162 | b1fa0a38 | Andreas | */ |
| 163 | b1fa0a38 | Andreas | if(preg_match('/^(update|insert|delete)i/', $query)) { |
| 164 | b1fa0a38 | Andreas | $sth = $this->db->prepare($query, MDB2_PREPARE_MANIP);
|
| 165 | b1fa0a38 | Andreas | } |
| 166 | b1fa0a38 | Andreas | else {
|
| 167 | b1fa0a38 | Andreas | $sth = $this->db->prepare($query, MDB2_PREPARE_RESULT);
|
| 168 | b1fa0a38 | Andreas | } |
| 169 | b1fa0a38 | Andreas | |
| 170 | b1fa0a38 | Andreas | if(PEAR::isError($sth))
|
| 171 | b1fa0a38 | Andreas | $this->throwError($sth->getMessage() .' - '. $sth->getUserInfo()); |
| 172 | b1fa0a38 | Andreas | |
| 173 | b1fa0a38 | Andreas | return $sth;
|
| 174 | b1fa0a38 | Andreas | } |
| 175 | b1fa0a38 | Andreas | else
|
| 176 | 729c0028 | Andreas | $this->throwError("Can't prepare query - we are not connected!"); |
| 177 | b1fa0a38 | Andreas | |
| 178 | b1fa0a38 | Andreas | } // db_prepare()
|
| 179 | b1fa0a38 | Andreas | |
| 180 | b1fa0a38 | Andreas | /**
|
| 181 | b1fa0a38 | Andreas | * NEPHTHYS_DB database execute a prepared query |
| 182 | b1fa0a38 | Andreas | * |
| 183 | b1fa0a38 | Andreas | * This function will execute a previously prepared SQL query |
| 184 | b1fa0a38 | Andreas | * @param mixed $sth |
| 185 | b1fa0a38 | Andreas | * @param mixed $data |
| 186 | b1fa0a38 | Andreas | */ |
| 187 | b1fa0a38 | Andreas | public function db_execute($sth, $data) |
| 188 | b1fa0a38 | Andreas | {
|
| 189 | b1fa0a38 | Andreas | if($this->getConnStatus()) { |
| 190 | b1fa0a38 | Andreas | |
| 191 | b1fa0a38 | Andreas | $result = $sth->execute($data); |
| 192 | b1fa0a38 | Andreas | |
| 193 | b1fa0a38 | Andreas | if(PEAR::isError($result))
|
| 194 | b1fa0a38 | Andreas | $this->throwError($result->getMessage() .' - '. $result->getUserInfo()); |
| 195 | b1fa0a38 | Andreas | |
| 196 | b1fa0a38 | Andreas | } |
| 197 | b1fa0a38 | Andreas | else
|
| 198 | 729c0028 | Andreas | $this->throwError("Can't prepare query - we are not connected!"); |
| 199 | b1fa0a38 | Andreas | |
| 200 | b1fa0a38 | Andreas | } // db_execute()
|
| 201 | b1fa0a38 | Andreas | |
| 202 | b1fa0a38 | Andreas | /**
|
| 203 | 4e920782 | Andreas | * NEPHTHYS_DB database query & execute |
| 204 | 4e920782 | Andreas | * |
| 205 | 4e920782 | Andreas | * This function will execute a SQL query and return nothing. |
| 206 | 4e920782 | Andreas | */ |
| 207 | 4e920782 | Andreas | public function db_exec($query = "") |
| 208 | 4e920782 | Andreas | {
|
| 209 | 4e920782 | Andreas | if(!$this->getConnStatus()) |
| 210 | 4e920782 | Andreas | return false; |
| 211 | 4e920782 | Andreas | |
| 212 | 4e920782 | Andreas | $affected =& $this->db->exec($query);
|
| 213 | 4e920782 | Andreas | |
| 214 | 4e920782 | Andreas | if(PEAR::isError($affected)) {
|
| 215 | 729c0028 | Andreas | $this->throwError($affected->getMessage());
|
| 216 | 4e920782 | Andreas | return false; |
| 217 | 4e920782 | Andreas | } |
| 218 | 4e920782 | Andreas | |
| 219 | 4e920782 | Andreas | return true; |
| 220 | 4e920782 | Andreas | |
| 221 | 4e920782 | Andreas | } // db_exec()
|
| 222 | 4e920782 | Andreas | |
| 223 | 4e920782 | Andreas | /**
|
| 224 | 1211fb5a | Andreas | * NEPHTHYS_DB fetch ONE row |
| 225 | 1211fb5a | Andreas | * |
| 226 | 1211fb5a | Andreas | * This function will execute the given but only return the |
| 227 | 1211fb5a | Andreas | * first result. |
| 228 | 1211fb5a | Andreas | */ |
| 229 | 1211fb5a | Andreas | public function db_fetchSingleRow($query = "", $mode = MDB2_FETCHMODE_OBJECT) |
| 230 | 1211fb5a | Andreas | {
|
| 231 | 1211fb5a | Andreas | if($this->getConnStatus()) { |
| 232 | 1211fb5a | Andreas | |
| 233 | 1211fb5a | Andreas | $row = $this->db->queryRow($query, array(), $mode); |
| 234 | 1211fb5a | Andreas | |
| 235 | 1211fb5a | Andreas | if(PEAR::isError($row))
|
| 236 | 1211fb5a | Andreas | $this->throwError($row->getMessage() .' - '. $row->getUserInfo()); |
| 237 | 1211fb5a | Andreas | |
| 238 | 1211fb5a | Andreas | return $row;
|
| 239 | 1211fb5a | Andreas | |
| 240 | 1211fb5a | Andreas | } |
| 241 | 1211fb5a | Andreas | else {
|
| 242 | 1211fb5a | Andreas | |
| 243 | 729c0028 | Andreas | $this->throwError("Can't fetch row - we are not connected!"); |
| 244 | 1211fb5a | Andreas | |
| 245 | 1211fb5a | Andreas | } |
| 246 | 1211fb5a | Andreas | |
| 247 | 1211fb5a | Andreas | } // db_fetchSingleRow()
|
| 248 | 1211fb5a | Andreas | |
| 249 | 1211fb5a | Andreas | /**
|
| 250 | 1211fb5a | Andreas | * NEPHTHYS_DB number of affected rows |
| 251 | 1211fb5a | Andreas | * |
| 252 | 1211fb5a | Andreas | * This functions returns the number of affected rows but the |
| 253 | 1211fb5a | Andreas | * given SQL query. |
| 254 | 1211fb5a | Andreas | */ |
| 255 | 1211fb5a | Andreas | public function db_getNumRows($query = "") |
| 256 | 1211fb5a | Andreas | {
|
| 257 | 1211fb5a | Andreas | /* Execute query */
|
| 258 | 1211fb5a | Andreas | $result = $this->db_query($query);
|
| 259 | 1211fb5a | Andreas | |
| 260 | 1211fb5a | Andreas | /* Errors? */
|
| 261 | 729c0028 | Andreas | if(PEAR::isError($result))
|
| 262 | 1211fb5a | Andreas | $this->throwError($result->getMessage() .' - '. $result->getUserInfo()); |
| 263 | 1211fb5a | Andreas | |
| 264 | 1211fb5a | Andreas | return $result->numRows();
|
| 265 | 1211fb5a | Andreas | |
| 266 | 1211fb5a | Andreas | } // db_getNumRows()
|
| 267 | 1211fb5a | Andreas | |
| 268 | 1211fb5a | Andreas | /**
|
| 269 | 1211fb5a | Andreas | * NEPHTHYS_DB get primary key |
| 270 | 1211fb5a | Andreas | * |
| 271 | 1211fb5a | Andreas | * This function returns the primary key of the last |
| 272 | 1211fb5a | Andreas | * operated insert SQL query. |
| 273 | 1211fb5a | Andreas | */ |
| 274 | 4e920782 | Andreas | public function db_getid($table_name = null) |
| 275 | 1211fb5a | Andreas | {
|
| 276 | 1211fb5a | Andreas | /* Get the last primary key ID from execute query */
|
| 277 | 4e920782 | Andreas | $id = $this->db->lastInsertID($table_name);
|
| 278 | 4e920782 | Andreas | if (PEAR::isError($id)) {
|
| 279 | 4e920782 | Andreas | $this->throwError($id->getMessage());
|
| 280 | 4e920782 | Andreas | } |
| 281 | 4e920782 | Andreas | |
| 282 | 4e920782 | Andreas | return $id;
|
| 283 | 1211fb5a | Andreas | |
| 284 | 1211fb5a | Andreas | } // db_getid()
|
| 285 | 1211fb5a | Andreas | |
| 286 | 1211fb5a | Andreas | /**
|
| 287 | 1211fb5a | Andreas | * NEPHTHYS_DB check table exists |
| 288 | 1211fb5a | Andreas | * |
| 289 | 1211fb5a | Andreas | * This function checks if the given table exists in the |
| 290 | 1211fb5a | Andreas | * database |
| 291 | 1211fb5a | Andreas | * @param string, table name |
| 292 | 1211fb5a | Andreas | * @return true if table found otherwise false |
| 293 | 1211fb5a | Andreas | */ |
| 294 | 1211fb5a | Andreas | public function db_check_table_exists($table_name = "") |
| 295 | 1211fb5a | Andreas | {
|
| 296 | 4e920782 | Andreas | if(!$this->getConnStatus()) |
| 297 | 1211fb5a | Andreas | return false; |
| 298 | 4e920782 | Andreas | |
| 299 | 4e920782 | Andreas | switch($this->cfg->db_type) { |
| 300 | 4e920782 | Andreas | default:
|
| 301 | 4e920782 | Andreas | case 'mysql': |
| 302 | 4e920782 | Andreas | $result = $this->db_query("SHOW TABLES"); |
| 303 | 4e920782 | Andreas | $tables_in = "Tables_in_". $this->cfg->mysql_db; |
| 304 | 4e920782 | Andreas | while($row = $result->fetchRow()) {
|
| 305 | 4e920782 | Andreas | if($row->$tables_in == $table_name)
|
| 306 | 4e920782 | Andreas | return true; |
| 307 | 4e920782 | Andreas | } |
| 308 | 4e920782 | Andreas | break;
|
| 309 | 4e920782 | Andreas | case 'sqlite': |
| 310 | 4e920782 | Andreas | $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'"); |
| 311 | 4e920782 | Andreas | while($row = $result->fetchRow()) {
|
| 312 | 4e920782 | Andreas | if($row->name == $table_name)
|
| 313 | 4e920782 | Andreas | return true; |
| 314 | 4e920782 | Andreas | } |
| 315 | 4e920782 | Andreas | break;
|
| 316 | 1211fb5a | Andreas | } |
| 317 | 4e920782 | Andreas | |
| 318 | 4e920782 | Andreas | return false; |
| 319 | 1211fb5a | Andreas | |
| 320 | 1211fb5a | Andreas | } // db_check_table_exists()
|
| 321 | 1211fb5a | Andreas | |
| 322 | 1211fb5a | Andreas | /**
|
| 323 | 1211fb5a | Andreas | * NEPHTHYS_DB rename table |
| 324 | 1211fb5a | Andreas | * |
| 325 | 1211fb5a | Andreas | * This function will rename an database table |
| 326 | 1211fb5a | Andreas | * @param old_name, new_name |
| 327 | 1211fb5a | Andreas | */ |
| 328 | 1211fb5a | Andreas | public function db_rename_table($old, $new) |
| 329 | 1211fb5a | Andreas | {
|
| 330 | 1211fb5a | Andreas | if($this->db_check_table_exists($old)) { |
| 331 | 1211fb5a | Andreas | if(!$this->db_check_table_exists($new)) |
| 332 | 1211fb5a | Andreas | $this->db_query("RENAME TABLE ". $old ." TO ". $new); |
| 333 | 1211fb5a | Andreas | else
|
| 334 | 729c0028 | Andreas | $this->throwError("Can't rename table ". $old ." - ". $new ." already exists!"); |
| 335 | 1211fb5a | Andreas | } |
| 336 | 1211fb5a | Andreas | |
| 337 | 1211fb5a | Andreas | } // db_rename_table()
|
| 338 | 1211fb5a | Andreas | |
| 339 | 1211fb5a | Andreas | /**
|
| 340 | 1211fb5a | Andreas | * NEPHTHYS_DB drop table |
| 341 | 1211fb5a | Andreas | * |
| 342 | 1211fb5a | Andreas | * This function will delete the given table from database |
| 343 | 1211fb5a | Andreas | */ |
| 344 | 1211fb5a | Andreas | public function db_drop_table($table_name) |
| 345 | 1211fb5a | Andreas | {
|
| 346 | 1211fb5a | Andreas | if($this->db_check_table_exists($table_name)) |
| 347 | 1211fb5a | Andreas | $this->db_query("DROP TABLE ". $table_name); |
| 348 | 1211fb5a | Andreas | |
| 349 | 1211fb5a | Andreas | } // db_drop_table()
|
| 350 | 1211fb5a | Andreas | |
| 351 | 1211fb5a | Andreas | /**
|
| 352 | 1211fb5a | Andreas | * NEPHTHYS_DB truncate table |
| 353 | 1211fb5a | Andreas | * |
| 354 | 1211fb5a | Andreas | * This function will truncate (reset) the given table |
| 355 | 1211fb5a | Andreas | */ |
| 356 | 1211fb5a | Andreas | public function db_truncate_table($table_name) |
| 357 | 1211fb5a | Andreas | {
|
| 358 | 1211fb5a | Andreas | if($this->db_check_table_exists($table_name)) |
| 359 | 1211fb5a | Andreas | $this->db_query("TRUNCATE TABLE ". $table_name); |
| 360 | 1211fb5a | Andreas | |
| 361 | 1211fb5a | Andreas | } // db_truncate_table()
|
| 362 | 1211fb5a | Andreas | |
| 363 | 1211fb5a | Andreas | /**
|
| 364 | 1211fb5a | Andreas | * NEPHTHYS_DB check column exist |
| 365 | 1211fb5a | Andreas | * |
| 366 | 1211fb5a | Andreas | * This function checks if the given column exists within |
| 367 | 1211fb5a | Andreas | * the specified table. |
| 368 | 1211fb5a | Andreas | */ |
| 369 | 1211fb5a | Andreas | public function db_check_column_exists($table_name, $column) |
| 370 | 1211fb5a | Andreas | {
|
| 371 | 70ee8a0b | Andreas | if(!$this->getConnStatus()) |
| 372 | 70ee8a0b | Andreas | return false; |
| 373 | 70ee8a0b | Andreas | |
| 374 | 70ee8a0b | Andreas | switch($this->cfg->db_type) { |
| 375 | 70ee8a0b | Andreas | default:
|
| 376 | 70ee8a0b | Andreas | case 'mysql': |
| 377 | 70ee8a0b | Andreas | $result = $this->db_query("DESC ". $table_name, MDB2_FETCHMODE_ORDERED); |
| 378 | 70ee8a0b | Andreas | while($row = $result->fetchRow()) {
|
| 379 | 70ee8a0b | Andreas | if(in_array($column, $row))
|
| 380 | 70ee8a0b | Andreas | return true; |
| 381 | 70ee8a0b | Andreas | } |
| 382 | 70ee8a0b | Andreas | break;
|
| 383 | 70ee8a0b | Andreas | case 'sqlite': |
| 384 | 70ee8a0b | Andreas | $result = $this->db_query(" |
| 385 | 70ee8a0b | Andreas | SELECT sql |
| 386 | 70ee8a0b | Andreas | FROM |
| 387 | 70ee8a0b | Andreas | (SELECT * FROM sqlite_master UNION ALL |
| 388 | 70ee8a0b | Andreas | SELECT * FROM sqlite_temp_master) |
| 389 | 70ee8a0b | Andreas | WHERE |
| 390 | 70ee8a0b | Andreas | tbl_name LIKE '". $table_name ."' |
| 391 | 70ee8a0b | Andreas | AND type!='meta' |
| 392 | 70ee8a0b | Andreas | AND sql NOT NULL |
| 393 | 70ee8a0b | Andreas | AND name NOT LIKE 'sqlite_%' |
| 394 | 70ee8a0b | Andreas | ORDER BY substr(type,2,1), name |
| 395 | 70ee8a0b | Andreas | ");
|
| 396 | 70ee8a0b | Andreas | while($row = $result->fetchRow()) {
|
| 397 | 70ee8a0b | Andreas | /* CREATE TABLE xx ( col1 int, col2 bool, col3 ...) */
|
| 398 | 70ee8a0b | Andreas | if(strstr($row->sql, " ". $column ." ") !== false) |
| 399 | 70ee8a0b | Andreas | return true; |
| 400 | 70ee8a0b | Andreas | } |
| 401 | 70ee8a0b | Andreas | break;
|
| 402 | 1211fb5a | Andreas | } |
| 403 | 70ee8a0b | Andreas | |
| 404 | 70ee8a0b | Andreas | return false; |
| 405 | 1211fb5a | Andreas | |
| 406 | 1211fb5a | Andreas | } // db_check_column_exists()
|
| 407 | 1211fb5a | Andreas | |
| 408 | 1211fb5a | Andreas | /**
|
| 409 | 1211fb5a | Andreas | * NEPHTHYS_DB check index exists |
| 410 | 1211fb5a | Andreas | * |
| 411 | 1211fb5a | Andreas | * This function checks if the given index can be found |
| 412 | 1211fb5a | Andreas | * within the specified table. |
| 413 | 1211fb5a | Andreas | */ |
| 414 | 1211fb5a | Andreas | public function db_check_index_exists($table_name, $index_name) |
| 415 | 1211fb5a | Andreas | {
|
| 416 | 1211fb5a | Andreas | $result = $this->db_query("DESC ". $table_name, MDB2_FETCHMODE_ORDERED); |
| 417 | 1211fb5a | Andreas | |
| 418 | 1211fb5a | Andreas | while($row = $result->fetchRow()) {
|
| 419 | 1211fb5a | Andreas | if(in_array("KEY `". $index_name ."`", $row)) |
| 420 | 1211fb5a | Andreas | return 1; |
| 421 | 1211fb5a | Andreas | } |
| 422 | 1211fb5a | Andreas | |
| 423 | 1211fb5a | Andreas | return 0; |
| 424 | 1211fb5a | Andreas | |
| 425 | 1211fb5a | Andreas | } // db_check_index_exists()
|
| 426 | 1211fb5a | Andreas | |
| 427 | 1211fb5a | Andreas | /**
|
| 428 | 1211fb5a | Andreas | * NEPHTHYS_DB alter table |
| 429 | 1211fb5a | Andreas | * |
| 430 | 1211fb5a | Andreas | * This function offers multiple methods to alter a table. |
| 431 | 1211fb5a | Andreas | * * add/modify/delete columns |
| 432 | 1211fb5a | Andreas | * * drop index |
| 433 | 1211fb5a | Andreas | */ |
| 434 | 1211fb5a | Andreas | public function db_alter_table($table_name, $option, $column, $param1 = "", $param2 = "") |
| 435 | 1211fb5a | Andreas | {
|
| 436 | c163f9b2 | Andreas | if(!$this->db_check_table_exists($table_name)) { |
| 437 | 729c0028 | Andreas | $this->throwError("Table ". $table_name ." does not exist!"); |
| 438 | c163f9b2 | Andreas | return false; |
| 439 | c163f9b2 | Andreas | } |
| 440 | 1211fb5a | Andreas | |
| 441 | c163f9b2 | Andreas | switch($this->cfg->db_type) { |
| 442 | c163f9b2 | Andreas | default:
|
| 443 | c163f9b2 | Andreas | case 'mysql': |
| 444 | 1211fb5a | Andreas | |
| 445 | c163f9b2 | Andreas | switch(strtolower($option)) {
|
| 446 | 1211fb5a | Andreas | |
| 447 | c163f9b2 | Andreas | case 'add': |
| 448 | c163f9b2 | Andreas | if(!$this->db_check_column_exists($table_name, $column)) |
| 449 | c163f9b2 | Andreas | $this->db_query("ALTER TABLE ". $table_name ." ADD ". $column ." ". $param1); |
| 450 | c163f9b2 | Andreas | break;
|
| 451 | 1211fb5a | Andreas | |
| 452 | c163f9b2 | Andreas | case 'change': |
| 453 | c163f9b2 | Andreas | if($this->db_check_column_exists($table_name, $column)) |
| 454 | c163f9b2 | Andreas | $this->db_query("ALTER TABLE ". $table_name ." CHANGE ". $column ." ". $param1); |
| 455 | c163f9b2 | Andreas | break;
|
| 456 | 1211fb5a | Andreas | |
| 457 | c163f9b2 | Andreas | case 'drop': |
| 458 | c163f9b2 | Andreas | if($this->db_check_column_exists($table_name, $column)) |
| 459 | c163f9b2 | Andreas | $this->db_query("ALTER TABLE ". $table_name ." DROP ". $column); |
| 460 | c163f9b2 | Andreas | break;
|
| 461 | c163f9b2 | Andreas | |
| 462 | c163f9b2 | Andreas | case 'dropidx': |
| 463 | c163f9b2 | Andreas | if($this->db_check_index_exists($table_name, $column)) |
| 464 | c163f9b2 | Andreas | $this->db_query("ALTER TABLE ". $table_name ." DROP INDEX ". $column); |
| 465 | c163f9b2 | Andreas | break;
|
| 466 | c163f9b2 | Andreas | |
| 467 | c163f9b2 | Andreas | } |
| 468 | 473edba3 | Andreas | break;
|
| 469 | c163f9b2 | Andreas | |
| 470 | c163f9b2 | Andreas | case 'sqlite': |
| 471 | c163f9b2 | Andreas | |
| 472 | c163f9b2 | Andreas | $this->throwError("SQLite only support ALTER TABLE rudimentary with version 3, so no support here right now."); |
| 473 | c163f9b2 | Andreas | break;
|
| 474 | 1211fb5a | Andreas | |
| 475 | 1211fb5a | Andreas | } |
| 476 | 1211fb5a | Andreas | |
| 477 | 1211fb5a | Andreas | } // db_alter_table()
|
| 478 | 1211fb5a | Andreas | |
| 479 | 1211fb5a | Andreas | /**
|
| 480 | b23116c6 | Andreas | * NEPHTHYS_DB get database version |
| 481 | 1211fb5a | Andreas | * |
| 482 | b23116c6 | Andreas | * This functions returns the current database version |
| 483 | 1211fb5a | Andreas | */ |
| 484 | 1211fb5a | Andreas | public function getVersion() |
| 485 | 1211fb5a | Andreas | {
|
| 486 | 1211fb5a | Andreas | if($this->db_check_table_exists(MYSQL_PREFIX ."settings")) { |
| 487 | 1211fb5a | Andreas | $result = $this->db_fetchSingleRow(" |
| 488 | 1211fb5a | Andreas | SELECT setting_value |
| 489 | 1211fb5a | Andreas | FROM ". MYSQL_PREFIX ."settings |
| 490 | 1211fb5a | Andreas | WHERE setting_key LIKE 'version' |
| 491 | 1211fb5a | Andreas | ");
|
| 492 | 1211fb5a | Andreas | return $result->setting_value;
|
| 493 | 1211fb5a | Andreas | } |
| 494 | 1211fb5a | Andreas | else
|
| 495 | 1211fb5a | Andreas | return 0; |
| 496 | 1211fb5a | Andreas | |
| 497 | 1211fb5a | Andreas | } // getVersion()
|
| 498 | 1211fb5a | Andreas | |
| 499 | 1211fb5a | Andreas | /**
|
| 500 | 1211fb5a | Andreas | * NEPHTHYS_DB set version |
| 501 | 1211fb5a | Andreas | * |
| 502 | b23116c6 | Andreas | * This function sets the version of database |
| 503 | 1211fb5a | Andreas | */ |
| 504 | 1211fb5a | Andreas | public function setVersion($version) |
| 505 | 1211fb5a | Andreas | {
|
| 506 | 1211fb5a | Andreas | $this->db_query(" |
| 507 | 1211fb5a | Andreas | REPLACE INTO ". MYSQL_PREFIX ."settings |
| 508 | 1211fb5a | Andreas | (setting_key, setting_value) |
| 509 | 1211fb5a | Andreas | VALUES ('version', '". $version ."') |
| 510 | 1211fb5a | Andreas | ");
|
| 511 | 1211fb5a | Andreas | |
| 512 | 1211fb5a | Andreas | } // setVersion()
|
| 513 | 1211fb5a | Andreas | |
| 514 | 1211fb5a | Andreas | /**
|
| 515 | 1211fb5a | Andreas | * NEPHTHYS_DB get connection status |
| 516 | 1211fb5a | Andreas | * |
| 517 | 1211fb5a | Andreas | * This function checks the internal state variable if already |
| 518 | 1211fb5a | Andreas | * connected to database. |
| 519 | 1211fb5a | Andreas | */ |
| 520 | 1211fb5a | Andreas | private function setConnStatus($status) |
| 521 | 1211fb5a | Andreas | {
|
| 522 | 1211fb5a | Andreas | $this->is_connected = $status;
|
| 523 | 1211fb5a | Andreas | |
| 524 | 1211fb5a | Andreas | } // setConnStatus()
|
| 525 | 1211fb5a | Andreas | |
| 526 | 1211fb5a | Andreas | /**
|
| 527 | 1211fb5a | Andreas | * NEPHTHYS_DB set connection status |
| 528 | 1211fb5a | Andreas | * This function sets the internal state variable to indicate |
| 529 | 1211fb5a | Andreas | * current database connection status. |
| 530 | 1211fb5a | Andreas | */ |
| 531 | 1211fb5a | Andreas | private function getConnStatus() |
| 532 | 1211fb5a | Andreas | {
|
| 533 | 1211fb5a | Andreas | return $this->is_connected; |
| 534 | 1211fb5a | Andreas | |
| 535 | 1211fb5a | Andreas | } // getConnStatus()
|
| 536 | 1211fb5a | Andreas | |
| 537 | 1211fb5a | Andreas | /**
|
| 538 | 1211fb5a | Andreas | * NEPHTHYS_DB throw error |
| 539 | 1211fb5a | Andreas | * |
| 540 | 1211fb5a | Andreas | * This function shows up error messages and afterwards through exceptions. |
| 541 | 1211fb5a | Andreas | */ |
| 542 | 729c0028 | Andreas | private function throwError($string) |
| 543 | 1211fb5a | Andreas | {
|
| 544 | 1211fb5a | Andreas | if(!defined('DB_NOERROR')) { |
| 545 | 9507541c | Andreas | |
| 546 | 9507541c | Andreas | $this->parent->_error($string);
|
| 547 | 9507541c | Andreas | |
| 548 | 1211fb5a | Andreas | try {
|
| 549 | 9507541c | Andreas | throw new NEPHTHYS_EXCEPTION; |
| 550 | 1211fb5a | Andreas | } |
| 551 | 9507541c | Andreas | catch(NEPHTHYS_EXCEPTION $e) {
|
| 552 | 9507541c | Andreas | print "<br /><br />\n"; |
| 553 | 9507541c | Andreas | $this->parent->_error($e);
|
| 554 | 9507541c | Andreas | die;
|
| 555 | 1211fb5a | Andreas | } |
| 556 | 1211fb5a | Andreas | } |
| 557 | 1211fb5a | Andreas | |
| 558 | 1211fb5a | Andreas | $this->last_error = $string;
|
| 559 | 1211fb5a | Andreas | |
| 560 | 729c0028 | Andreas | } // throwError()
|
| 561 | 1211fb5a | Andreas | |
| 562 | b1fa0a38 | Andreas | /**
|
| 563 | b1fa0a38 | Andreas | * quote string |
| 564 | b1fa0a38 | Andreas | * |
| 565 | b1fa0a38 | Andreas | * this function handsover the provided string to the MDB2 |
| 566 | b1fa0a38 | Andreas | * quote() function which will return the, for the selected |
| 567 | b1fa0a38 | Andreas | * database system, correctly quoted string. |
| 568 | b1fa0a38 | Andreas | * |
| 569 | b1fa0a38 | Andreas | * @param string $query |
| 570 | b1fa0a38 | Andreas | * @return string |
| 571 | b1fa0a38 | Andreas | */ |
| 572 | b1fa0a38 | Andreas | public function db_quote($obj) |
| 573 | b1fa0a38 | Andreas | {
|
| 574 | b1fa0a38 | Andreas | return $this->db->quote($obj); |
| 575 | b1fa0a38 | Andreas | |
| 576 | b1fa0a38 | Andreas | } // db_quote()
|
| 577 | b1fa0a38 | Andreas | |
| 578 | 729c0028 | Andreas | /**
|
| 579 | 729c0028 | Andreas | * start transaction |
| 580 | 729c0028 | Andreas | * |
| 581 | 729c0028 | Andreas | * this will start a transaction on ACID-supporting database |
| 582 | 729c0028 | Andreas | * systems. |
| 583 | 729c0028 | Andreas | * |
| 584 | 729c0028 | Andreas | * @return bool |
| 585 | 729c0028 | Andreas | */ |
| 586 | 729c0028 | Andreas | public function db_start_transaction() |
| 587 | 729c0028 | Andreas | {
|
| 588 | 729c0028 | Andreas | if(!$this->getConnStatus()) |
| 589 | 729c0028 | Andreas | return false; |
| 590 | 729c0028 | Andreas | |
| 591 | 729c0028 | Andreas | if(!$this->db->supports('transactions')) |
| 592 | 729c0028 | Andreas | return false; |
| 593 | 729c0028 | Andreas | |
| 594 | 729c0028 | Andreas | $result = $this->db->beginTransaction();
|
| 595 | 729c0028 | Andreas | |
| 596 | 729c0028 | Andreas | /* Errors? */
|
| 597 | 729c0028 | Andreas | if(PEAR::isError($result))
|
| 598 | 729c0028 | Andreas | $this->throwError($result->getMessage() .' - '. $result->getUserInfo()); |
| 599 | 729c0028 | Andreas | |
| 600 | 729c0028 | Andreas | return true; |
| 601 | 729c0028 | Andreas | |
| 602 | 729c0028 | Andreas | } // db_start_transaction()
|
| 603 | 729c0028 | Andreas | |
| 604 | 729c0028 | Andreas | /**
|
| 605 | 729c0028 | Andreas | * commit transaction |
| 606 | 729c0028 | Andreas | * |
| 607 | 729c0028 | Andreas | * this will commit an ongoing transaction on ACID-supporting |
| 608 | 729c0028 | Andreas | * database systems |
| 609 | 729c0028 | Andreas | * |
| 610 | 729c0028 | Andreas | * @return bool |
| 611 | 729c0028 | Andreas | */ |
| 612 | 729c0028 | Andreas | public function db_commit_transaction() |
| 613 | 729c0028 | Andreas | {
|
| 614 | 729c0028 | Andreas | if(!$this->getConnStatus()) |
| 615 | 729c0028 | Andreas | return false; |
| 616 | 729c0028 | Andreas | |
| 617 | 729c0028 | Andreas | if(!$this->db->inTransaction()) |
| 618 | 729c0028 | Andreas | return false; |
| 619 | 729c0028 | Andreas | |
| 620 | 729c0028 | Andreas | $result = $this->db->commit();
|
| 621 | 729c0028 | Andreas | |
| 622 | 729c0028 | Andreas | /* Errors? */
|
| 623 | 729c0028 | Andreas | if(PEAR::isError($result))
|
| 624 | 729c0028 | Andreas | $this->throwError($result->getMessage() .' - '. $result->getUserInfo()); |
| 625 | 729c0028 | Andreas | |
| 626 | 729c0028 | Andreas | return true; |
| 627 | 729c0028 | Andreas | |
| 628 | 729c0028 | Andreas | } // db_commit_transaction()
|
| 629 | 729c0028 | Andreas | |
| 630 | 729c0028 | Andreas | /**
|
| 631 | 729c0028 | Andreas | * rollback transaction() |
| 632 | 729c0028 | Andreas | * |
| 633 | 729c0028 | Andreas | * this function aborts a on going transaction |
| 634 | 729c0028 | Andreas | * |
| 635 | 729c0028 | Andreas | * @return bool |
| 636 | 729c0028 | Andreas | */ |
| 637 | 729c0028 | Andreas | public function db_rollback_transaction() |
| 638 | 729c0028 | Andreas | {
|
| 639 | 729c0028 | Andreas | if(!$this->getConnStatus()) |
| 640 | 729c0028 | Andreas | return false; |
| 641 | 729c0028 | Andreas | |
| 642 | 729c0028 | Andreas | if(!$this->db->inTransaction()) |
| 643 | 729c0028 | Andreas | return false; |
| 644 | 729c0028 | Andreas | |
| 645 | 729c0028 | Andreas | $result = $this->db->rollback();
|
| 646 | 729c0028 | Andreas | |
| 647 | 729c0028 | Andreas | /* Errors? */
|
| 648 | 729c0028 | Andreas | if(PEAR::isError($result))
|
| 649 | 729c0028 | Andreas | $this->throwError($result->getMessage() .' - '. $result->getUserInfo()); |
| 650 | 729c0028 | Andreas | |
| 651 | 729c0028 | Andreas | return true; |
| 652 | 729c0028 | Andreas | |
| 653 | 729c0028 | Andreas | } // db_rollback_transaction()
|
| 654 | 729c0028 | Andreas | |
| 655 | 1211fb5a | Andreas | } // NEPHTHYS_DB()
|
| 656 | 1211fb5a | Andreas | |
| 657 | b392cb9b | Andreas | // vim: set filetype=php expandtab softtabstop=3 tabstop=3 shiftwidth=3 autoindent smartindent:
|
| 658 | 1211fb5a | Andreas | ?> |