Changeset 3 for branches/rsr.v5.1.dev/web/punbb/include/dblayer
- Timestamp:
- Nov 14, 2011, 11:17:15 PM (12 years ago)
- Location:
- branches/rsr.v5.1.dev/web/punbb/include/dblayer
- Files:
-
- 2 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/rsr.v5.1.dev/web/punbb/include/dblayer/common_db.php
r1 r3 1 1 <?php 2 /***********************************************************************3 2 4 Copyright (C) 2002-2005 Rickard Andersson (rickard@punbb.org) 5 6 This file is part of PunBB. 7 8 PunBB is free software; you can redistribute it and/or modify it 9 under the terms of the GNU General Public License as published 10 by the Free Software Foundation; either version 2 of the License, 11 or (at your option) any later version. 12 13 PunBB is distributed in the hope that it will be useful, but 14 WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, 21 MA 02111-1307 USA 22 23 ************************************************************************/ 24 3 /** 4 * Copyright (C) 2008-2011 FluxBB 5 * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB 6 * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher 7 */ 25 8 26 9 // Make sure no one attempts to run this script "directly" … … 29 12 30 13 31 //32 // Return current timestamp (with microseconds) as a float (used in dblayer)33 //34 if (defined('PUN_SHOW_QUERIES'))35 {36 function get_microtime()37 {38 list($usec, $sec) = explode(' ', microtime());39 return ((float)$usec + (float)$sec);40 }41 }42 43 44 14 // Load the appropriate DB layer class 45 15 switch ($db_type) 46 16 { 47 17 case 'mysql': 48 require PUN_ROOT.'include/dblayer/mysql.php'; 18 require_once PUN_ROOT.'include/dblayer/mysql.php'; 19 break; 20 21 case 'mysql_innodb': 22 require_once PUN_ROOT.'include/dblayer/mysql_innodb.php'; 49 23 break; 50 24 51 25 case 'mysqli': 52 require PUN_ROOT.'include/dblayer/mysqli.php'; 26 require_once PUN_ROOT.'include/dblayer/mysqli.php'; 27 break; 28 29 case 'mysqli_innodb': 30 require_once PUN_ROOT.'include/dblayer/mysqli_innodb.php'; 53 31 break; 54 32 55 33 case 'pgsql': 56 require PUN_ROOT.'include/dblayer/pgsql.php';34 require_once PUN_ROOT.'include/dblayer/pgsql.php'; 57 35 break; 58 36 59 37 case 'sqlite': 60 require PUN_ROOT.'include/dblayer/sqlite.php';38 require_once PUN_ROOT.'include/dblayer/sqlite.php'; 61 39 break; 62 40 63 41 default: 64 error('\''.$db_type.'\' n\'est pas un type de base de données valable. Veuillez vérifier vos paramÚtres de configuration dans config.php', __FILE__, __LINE__);42 error('\''.$db_type.'\' is not a valid database type. Please check settings in config.php.', __FILE__, __LINE__); 65 43 break; 66 44 } -
branches/rsr.v5.1.dev/web/punbb/include/dblayer/index.html
r1 r3 1 <html> 2 <head> 3 <title>.</title> 4 </head> 5 <body> 6 . 7 </body> 8 </html> 1 <html><head><title>.</title></head><body>.</body></html> -
branches/rsr.v5.1.dev/web/punbb/include/dblayer/mysql.php
r1 r3 1 1 <?php 2 /*********************************************************************** 3 4 Copyright (C) 2002-2005 Rickard Andersson (rickard@punbb.org) 5 6 This file is part of PunBB. 7 8 PunBB is free software; you can redistribute it and/or modify it 9 under the terms of the GNU General Public License as published 10 by the Free Software Foundation; either version 2 of the License, 11 or (at your option) any later version. 12 13 PunBB is distributed in the hope that it will be useful, but 14 WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, 21 MA 02111-1307 USA 22 23 ************************************************************************/ 24 2 3 /** 4 * Copyright (C) 2008-2011 FluxBB 5 * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB 6 * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher 7 */ 25 8 26 9 // Make sure we have built in support for MySQL … … 38 21 var $num_queries = 0; 39 22 23 var $error_no = false; 24 var $error_msg = 'Unknown'; 25 26 var $datatype_transformations = array( 27 '%^SERIAL$%' => 'INT(10) UNSIGNED AUTO_INCREMENT' 28 ); 29 40 30 41 31 function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect) … … 50 40 if ($this->link_id) 51 41 { 52 if (@mysql_select_db($db_name, $this->link_id)) 53 { mysql_query("SET NAMES utf8"); 54 return $this->link_id; 55 } 56 else 42 if (!@mysql_select_db($db_name, $this->link_id)) 57 43 error('Unable to select database. MySQL reported: '.mysql_error(), __FILE__, __LINE__); 58 44 } 59 45 else 60 46 error('Unable to connect to MySQL server. MySQL reported: '.mysql_error(), __FILE__, __LINE__); 47 48 // Setup the client-server character set (UTF-8) 49 if (!defined('FORUM_NO_SET_NAMES')) 50 $this->set_names('utf8'); 51 52 return $this->link_id; 61 53 } 62 54 … … 98 90 $this->saved_queries[] = array($sql, 0); 99 91 92 $this->error_no = @mysql_errno($this->link_id); 93 $this->error_msg = @mysql_error($this->link_id); 94 100 95 return false; 101 96 } … … 103 98 104 99 105 function result($query_id = 0, $row = 0 )106 { 107 return ($query_id) ? @mysql_result($query_id, $row ) : false;100 function result($query_id = 0, $row = 0, $col = 0) 101 { 102 return ($query_id) ? @mysql_result($query_id, $row, $col) : false; 108 103 } 109 104 … … 159 154 function escape($str) 160 155 { 161 if (function_exists('mysql_real_escape_string')) 156 if (is_array($str)) 157 return ''; 158 else if (function_exists('mysql_real_escape_string')) 162 159 return mysql_real_escape_string($str, $this->link_id); 163 160 else … … 169 166 { 170 167 $result['error_sql'] = @current(@end($this->saved_queries)); 171 $result['error_no'] = @mysql_errno($this->link_id);172 $result['error_msg'] = @mysql_error($this->link_id);168 $result['error_no'] = $this->error_no; 169 $result['error_msg'] = $this->error_msg; 173 170 174 171 return $result; … … 188 185 return false; 189 186 } 187 188 function get_names() 189 { 190 $result = $this->query('SHOW VARIABLES LIKE \'character_set_connection\''); 191 return $this->result($result, 0, 1); 192 } 193 194 195 function set_names($names) 196 { 197 return $this->query('SET NAMES \''.$this->escape($names).'\''); 198 } 199 200 201 function get_version() 202 { 203 $result = $this->query('SELECT VERSION()'); 204 205 return array( 206 'name' => 'MySQL Standard', 207 'version' => preg_replace('%^([^-]+).*$%', '\\1', $this->result($result)) 208 ); 209 } 210 211 212 function table_exists($table_name, $no_prefix = false) 213 { 214 $result = $this->query('SHOW TABLES LIKE \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\''); 215 return $this->num_rows($result) > 0; 216 } 217 218 219 function field_exists($table_name, $field_name, $no_prefix = false) 220 { 221 $result = $this->query('SHOW COLUMNS FROM '.($no_prefix ? '' : $this->prefix).$table_name.' LIKE \''.$this->escape($field_name).'\''); 222 return $this->num_rows($result) > 0; 223 } 224 225 226 function index_exists($table_name, $index_name, $no_prefix = false) 227 { 228 $exists = false; 229 230 $result = $this->query('SHOW INDEX FROM '.($no_prefix ? '' : $this->prefix).$table_name); 231 while ($cur_index = $this->fetch_assoc($result)) 232 { 233 if (strtolower($cur_index['Key_name']) == strtolower(($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name)) 234 { 235 $exists = true; 236 break; 237 } 238 } 239 240 return $exists; 241 } 242 243 244 function create_table($table_name, $schema, $no_prefix = false) 245 { 246 if ($this->table_exists($table_name, $no_prefix)) 247 return true; 248 249 $query = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name." (\n"; 250 251 // Go through every schema element and add it to the query 252 foreach ($schema['FIELDS'] as $field_name => $field_data) 253 { 254 $field_data['datatype'] = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_data['datatype']); 255 256 $query .= $field_name.' '.$field_data['datatype']; 257 258 if (isset($field_data['collation'])) 259 $query .= 'CHARACTER SET utf8 COLLATE utf8_'.$field_data['collation']; 260 261 if (!$field_data['allow_null']) 262 $query .= ' NOT NULL'; 263 264 if (isset($field_data['default'])) 265 $query .= ' DEFAULT '.$field_data['default']; 266 267 $query .= ",\n"; 268 } 269 270 // If we have a primary key, add it 271 if (isset($schema['PRIMARY KEY'])) 272 $query .= 'PRIMARY KEY ('.implode(',', $schema['PRIMARY KEY']).'),'."\n"; 273 274 // Add unique keys 275 if (isset($schema['UNIQUE KEYS'])) 276 { 277 foreach ($schema['UNIQUE KEYS'] as $key_name => $key_fields) 278 $query .= 'UNIQUE KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$key_name.'('.implode(',', $key_fields).'),'."\n"; 279 } 280 281 // Add indexes 282 if (isset($schema['INDEXES'])) 283 { 284 foreach ($schema['INDEXES'] as $index_name => $index_fields) 285 $query .= 'KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.'('.implode(',', $index_fields).'),'."\n"; 286 } 287 288 // We remove the last two characters (a newline and a comma) and add on the ending 289 $query = substr($query, 0, strlen($query) - 2)."\n".') ENGINE = '.(isset($schema['ENGINE']) ? $schema['ENGINE'] : 'MyISAM').' CHARACTER SET utf8'; 290 291 return $this->query($query) ? true : false; 292 } 293 294 295 function drop_table($table_name, $no_prefix = false) 296 { 297 if (!$this->table_exists($table_name, $no_prefix)) 298 return true; 299 300 return $this->query('DROP TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false; 301 } 302 303 304 function rename_table($old_table, $new_table, $no_prefix = false) 305 { 306 // If there new table exists and the old one doesn't, then we're happy 307 if ($this->table_exists($new_table, $no_prefix) && !$this->table_exists($old_table, $no_prefix)) 308 return true; 309 310 return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$old_table.' RENAME TO '.($no_prefix ? '' : $this->prefix).$new_table) ? true : false; 311 } 312 313 314 function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false) 315 { 316 if ($this->field_exists($table_name, $field_name, $no_prefix)) 317 return true; 318 319 $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type); 320 321 if ($default_value !== null && !is_int($default_value) && !is_float($default_value)) 322 $default_value = '\''.$this->escape($default_value).'\''; 323 324 return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ADD '.$field_name.' '.$field_type.($allow_null ? ' ' : ' NOT NULL').($default_value !== null ? ' DEFAULT '.$default_value : ' ').($after_field != null ? ' AFTER '.$after_field : '')) ? true : false; 325 } 326 327 328 function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false) 329 { 330 if (!$this->field_exists($table_name, $field_name, $no_prefix)) 331 return true; 332 333 $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type); 334 335 if ($default_value !== null && !is_int($default_value) && !is_float($default_value)) 336 $default_value = '\''.$this->escape($default_value).'\''; 337 338 return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' MODIFY '.$field_name.' '.$field_type.($allow_null ? ' ' : ' NOT NULL').($default_value !== null ? ' DEFAULT '.$default_value : ' ').($after_field != null ? ' AFTER '.$after_field : '')) ? true : false; 339 } 340 341 342 function drop_field($table_name, $field_name, $no_prefix = false) 343 { 344 if (!$this->field_exists($table_name, $field_name, $no_prefix)) 345 return true; 346 347 return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP '.$field_name) ? true : false; 348 } 349 350 351 function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false) 352 { 353 if ($this->index_exists($table_name, $index_name, $no_prefix)) 354 return true; 355 356 return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ADD '.($unique ? 'UNIQUE ' : '').'INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.' ('.implode(',', $index_fields).')') ? true : false; 357 } 358 359 360 function drop_index($table_name, $index_name, $no_prefix = false) 361 { 362 if (!$this->index_exists($table_name, $index_name, $no_prefix)) 363 return true; 364 365 return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) ? true : false; 366 } 367 368 function truncate_table($table_name, $no_prefix = false) 369 { 370 return $this->query('TRUNCATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false; 371 } 190 372 } -
branches/rsr.v5.1.dev/web/punbb/include/dblayer/mysqli.php
r1 r3 1 1 <?php 2 /*********************************************************************** 3 4 Copyright (C) 2002-2005 Rickard Andersson (rickard@punbb.org) 5 6 This file is part of PunBB. 7 8 PunBB is free software; you can redistribute it and/or modify it 9 under the terms of the GNU General Public License as published 10 by the Free Software Foundation; either version 2 of the License, 11 or (at your option) any later version. 12 13 PunBB is distributed in the hope that it will be useful, but 14 WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, 21 MA 02111-1307 USA 22 23 ************************************************************************/ 24 2 3 /** 4 * Copyright (C) 2008-2011 FluxBB 5 * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB 6 * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher 7 */ 25 8 26 9 // Make sure we have built in support for MySQL … … 38 21 var $num_queries = 0; 39 22 40 41 function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $foo) 23 var $error_no = false; 24 var $error_msg = 'Unknown'; 25 26 var $datatype_transformations = array( 27 '%^SERIAL$%' => 'INT(10) UNSIGNED AUTO_INCREMENT' 28 ); 29 30 31 function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect) 42 32 { 43 33 $this->prefix = $db_prefix; … … 47 37 list($db_host, $db_port) = explode(':', $db_host); 48 38 39 // Persistent connection in MySQLi are only available in PHP 5.3 and later releases 40 $p_connect = $p_connect && version_compare(PHP_VERSION, '5.3.0', '>=') ? 'p:' : ''; 41 49 42 if (isset($db_port)) 50 $this->link_id = @mysqli_connect($ db_host, $db_username, $db_password, $db_name, $db_port);43 $this->link_id = @mysqli_connect($p_connect.$db_host, $db_username, $db_password, $db_name, $db_port); 51 44 else 52 $this->link_id = @mysqli_connect($ db_host, $db_username, $db_password, $db_name);45 $this->link_id = @mysqli_connect($p_connect.$db_host, $db_username, $db_password, $db_name); 53 46 54 47 if (!$this->link_id) 55 48 error('Unable to connect to MySQL and select database. MySQL reported: '.mysqli_connect_error(), __FILE__, __LINE__); 49 50 // Setup the client-server character set (UTF-8) 51 if (!defined('FORUM_NO_SET_NAMES')) 52 $this->set_names('utf8'); 53 54 return $this->link_id; 56 55 } 57 56 … … 90 89 $this->saved_queries[] = array($sql, 0); 91 90 91 $this->error_no = @mysqli_errno($this->link_id); 92 $this->error_msg = @mysqli_error($this->link_id); 93 92 94 return false; 93 95 } … … 95 97 96 98 97 function result($query_id = 0, $row = 0 )99 function result($query_id = 0, $row = 0, $col = 0) 98 100 { 99 101 if ($query_id) 100 102 { 101 if ($row )102 @mysqli_data_seek($query_id, $row);103 if ($row !== 0 && @mysqli_data_seek($query_id, $row) === false) 104 return false; 103 105 104 106 $cur_row = @mysqli_fetch_row($query_id); 105 return $cur_row[0]; 107 if ($cur_row === false) 108 return false; 109 110 return $cur_row[$col]; 106 111 } 107 112 else … … 160 165 function escape($str) 161 166 { 162 return mysqli_real_escape_string($this->link_id, $str);167 return is_array($str) ? '' : mysqli_real_escape_string($this->link_id, $str); 163 168 } 164 169 … … 167 172 { 168 173 $result['error_sql'] = @current(@end($this->saved_queries)); 169 $result['error_no'] = @mysqli_errno($this->link_id);170 $result['error_msg'] = @mysqli_error($this->link_id);174 $result['error_no'] = $this->error_no; 175 $result['error_msg'] = $this->error_msg; 171 176 172 177 return $result; … … 186 191 return false; 187 192 } 193 194 195 function get_names() 196 { 197 $result = $this->query('SHOW VARIABLES LIKE \'character_set_connection\''); 198 return $this->result($result, 0, 1); 199 } 200 201 202 function set_names($names) 203 { 204 return $this->query('SET NAMES \''.$this->escape($names).'\''); 205 } 206 207 208 function get_version() 209 { 210 $result = $this->query('SELECT VERSION()'); 211 212 return array( 213 'name' => 'MySQL Improved', 214 'version' => preg_replace('%^([^-]+).*$%', '\\1', $this->result($result)) 215 ); 216 } 217 218 219 function table_exists($table_name, $no_prefix = false) 220 { 221 $result = $this->query('SHOW TABLES LIKE \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\''); 222 return $this->num_rows($result) > 0; 223 } 224 225 226 function field_exists($table_name, $field_name, $no_prefix = false) 227 { 228 $result = $this->query('SHOW COLUMNS FROM '.($no_prefix ? '' : $this->prefix).$table_name.' LIKE \''.$this->escape($field_name).'\''); 229 return $this->num_rows($result) > 0; 230 } 231 232 233 function index_exists($table_name, $index_name, $no_prefix = false) 234 { 235 $exists = false; 236 237 $result = $this->query('SHOW INDEX FROM '.($no_prefix ? '' : $this->prefix).$table_name); 238 while ($cur_index = $this->fetch_assoc($result)) 239 { 240 if (strtolower($cur_index['Key_name']) == strtolower(($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name)) 241 { 242 $exists = true; 243 break; 244 } 245 } 246 247 return $exists; 248 } 249 250 251 function create_table($table_name, $schema, $no_prefix = false) 252 { 253 if ($this->table_exists($table_name, $no_prefix)) 254 return true; 255 256 $query = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name." (\n"; 257 258 // Go through every schema element and add it to the query 259 foreach ($schema['FIELDS'] as $field_name => $field_data) 260 { 261 $field_data['datatype'] = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_data['datatype']); 262 263 $query .= $field_name.' '.$field_data['datatype']; 264 265 if (isset($field_data['collation'])) 266 $query .= 'CHARACTER SET utf8 COLLATE utf8_'.$field_data['collation']; 267 268 if (!$field_data['allow_null']) 269 $query .= ' NOT NULL'; 270 271 if (isset($field_data['default'])) 272 $query .= ' DEFAULT '.$field_data['default']; 273 274 $query .= ",\n"; 275 } 276 277 // If we have a primary key, add it 278 if (isset($schema['PRIMARY KEY'])) 279 $query .= 'PRIMARY KEY ('.implode(',', $schema['PRIMARY KEY']).'),'."\n"; 280 281 // Add unique keys 282 if (isset($schema['UNIQUE KEYS'])) 283 { 284 foreach ($schema['UNIQUE KEYS'] as $key_name => $key_fields) 285 $query .= 'UNIQUE KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$key_name.'('.implode(',', $key_fields).'),'."\n"; 286 } 287 288 // Add indexes 289 if (isset($schema['INDEXES'])) 290 { 291 foreach ($schema['INDEXES'] as $index_name => $index_fields) 292 $query .= 'KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.'('.implode(',', $index_fields).'),'."\n"; 293 } 294 295 // We remove the last two characters (a newline and a comma) and add on the ending 296 $query = substr($query, 0, strlen($query) - 2)."\n".') ENGINE = '.(isset($schema['ENGINE']) ? $schema['ENGINE'] : 'MyISAM').' CHARACTER SET utf8'; 297 298 return $this->query($query) ? true : false; 299 } 300 301 302 function drop_table($table_name, $no_prefix = false) 303 { 304 if (!$this->table_exists($table_name, $no_prefix)) 305 return true; 306 307 return $this->query('DROP TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false; 308 } 309 310 311 function rename_table($old_table, $new_table, $no_prefix = false) 312 { 313 // If there new table exists and the old one doesn't, then we're happy 314 if ($this->table_exists($new_table, $no_prefix) && !$this->table_exists($old_table, $no_prefix)) 315 return true; 316 317 return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$old_table.' RENAME TO '.($no_prefix ? '' : $this->prefix).$new_table) ? true : false; 318 } 319 320 321 function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false) 322 { 323 if ($this->field_exists($table_name, $field_name, $no_prefix)) 324 return true; 325 326 $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type); 327 328 if ($default_value !== null && !is_int($default_value) && !is_float($default_value)) 329 $default_value = '\''.$this->escape($default_value).'\''; 330 331 return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ADD '.$field_name.' '.$field_type.($allow_null ? ' ' : ' NOT NULL').($default_value !== null ? ' DEFAULT '.$default_value : ' ').($after_field != null ? ' AFTER '.$after_field : '')) ? true : false; 332 } 333 334 335 function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false) 336 { 337 if (!$this->field_exists($table_name, $field_name, $no_prefix)) 338 return true; 339 340 $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type); 341 342 if ($default_value !== null && !is_int($default_value) && !is_float($default_value)) 343 $default_value = '\''.$this->escape($default_value).'\''; 344 345 return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' MODIFY '.$field_name.' '.$field_type.($allow_null ? ' ' : ' NOT NULL').($default_value !== null ? ' DEFAULT '.$default_value : ' ').($after_field != null ? ' AFTER '.$after_field : '')) ? true : false; 346 } 347 348 349 function drop_field($table_name, $field_name, $no_prefix = false) 350 { 351 if (!$this->field_exists($table_name, $field_name, $no_prefix)) 352 return true; 353 354 return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP '.$field_name) ? true : false; 355 } 356 357 358 function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false) 359 { 360 if ($this->index_exists($table_name, $index_name, $no_prefix)) 361 return true; 362 363 return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ADD '.($unique ? 'UNIQUE ' : '').'INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.' ('.implode(',', $index_fields).')') ? true : false; 364 } 365 366 367 function drop_index($table_name, $index_name, $no_prefix = false) 368 { 369 if (!$this->index_exists($table_name, $index_name, $no_prefix)) 370 return true; 371 372 return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) ? true : false; 373 } 374 375 function truncate_table($table_name, $no_prefix = false) 376 { 377 return $this->query('TRUNCATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false; 378 } 188 379 } -
branches/rsr.v5.1.dev/web/punbb/include/dblayer/pgsql.php
r1 r3 1 1 <?php 2 /*********************************************************************** 3 4 Copyright (C) 2002-2005 Rickard Andersson (rickard@punbb.org) 5 6 This file is part of PunBB. 7 8 PunBB is free software; you can redistribute it and/or modify it 9 under the terms of the GNU General Public License as published 10 by the Free Software Foundation; either version 2 of the License, 11 or (at your option) any later version. 12 13 PunBB is distributed in the hope that it will be useful, but 14 WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, 21 MA 02111-1307 USA 22 23 ************************************************************************/ 24 2 3 /** 4 * Copyright (C) 2008-2011 FluxBB 5 * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB 6 * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher 7 */ 25 8 26 9 // Make sure we have built in support for PostgreSQL … … 43 26 var $error_msg = 'Unknown'; 44 27 28 var $datatype_transformations = array( 29 '%^(TINY|SMALL)INT( )?(\\([0-9]+\\))?( )?(UNSIGNED)?$%i' => 'SMALLINT', 30 '%^(MEDIUM)?INT( )?(\\([0-9]+\\))?( )?(UNSIGNED)?$%i' => 'INTEGER', 31 '%^BIGINT( )?(\\([0-9]+\\))?( )?(UNSIGNED)?$%i' => 'BIGINT', 32 '%^(TINY|MEDIUM|LONG)?TEXT$%i' => 'TEXT', 33 '%^DOUBLE( )?(\\([0-9,]+\\))?( )?(UNSIGNED)?$%i' => 'DOUBLE PRECISION', 34 '%^FLOAT( )?(\\([0-9]+\\))?( )?(UNSIGNED)?$%i' => 'REAL' 35 ); 36 45 37 46 38 function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect) … … 48 40 $this->prefix = $db_prefix; 49 41 50 if ($db_host != '')42 if ($db_host) 51 43 { 52 44 if (strpos($db_host, ':') !== false) … … 56 48 } 57 49 else 58 { 59 if ($db_host != 'localhost') 60 $connect_str[] = 'host='.$db_host; 61 } 50 $connect_str[] = 'host='.$db_host; 62 51 } 63 52 … … 65 54 $connect_str[] = 'dbname='.$db_name; 66 55 67 if ($db_username != '')56 if ($db_username) 68 57 $connect_str[] = 'user='.$db_username; 69 58 70 if ($db_password != '')59 if ($db_password) 71 60 $connect_str[] = 'password='.$db_password; 72 61 … … 78 67 if (!$this->link_id) 79 68 error('Unable to connect to PostgreSQL server', __FILE__, __LINE__); 80 else 81 return $this->link_id; 69 70 // Setup the client-server character set (UTF-8) 71 if (!defined('FORUM_NO_SET_NAMES')) 72 $this->set_names('utf8'); 73 74 return $this->link_id; 82 75 } 83 76 … … 105 98 106 99 107 function query($sql, $unbuffered = false) 100 function query($sql, $unbuffered = false) // $unbuffered is ignored since there is no pgsql_unbuffered_query() 108 101 { 109 102 if (strrpos($sql, 'LIMIT') !== false) 110 $sql = preg_replace(' #LIMIT ([0-9]+),([ 0-9]+)#', 'LIMIT \\2 OFFSET \\1', $sql);103 $sql = preg_replace('%LIMIT ([0-9]+),([ 0-9]+)%', 'LIMIT \\2 OFFSET \\1', $sql); 111 104 112 105 if (defined('PUN_SHOW_QUERIES')) … … 132 125 $this->saved_queries[] = array($sql, 0); 133 126 127 $this->error_no = false; 134 128 $this->error_msg = @pg_result_error($this->query_result); 135 129 … … 144 138 145 139 146 function result($query_id = 0, $row = 0 )147 { 148 return ($query_id) ? @pg_fetch_result($query_id, $row, 0) : false;140 function result($query_id = 0, $row = 0, $col = 0) 141 { 142 return ($query_id) ? @pg_fetch_result($query_id, $row, $col) : false; 149 143 } 150 144 … … 180 174 if ($query_id && $this->last_query_text[$query_id] != '') 181 175 { 182 if (preg_match(' /^INSERT INTO ([a-z0-9\_\-]+)/is', $this->last_query_text[$query_id], $table_name))176 if (preg_match('%^INSERT INTO ([a-z0-9\_\-]+)%is', $this->last_query_text[$query_id], $table_name)) 183 177 { 184 178 // Hack (don't ask) … … 218 212 function escape($str) 219 213 { 220 return pg_escape_string($str);214 return is_array($str) ? '' : pg_escape_string($str); 221 215 } 222 216 … … 225 219 { 226 220 $result['error_sql'] = @current(@end($this->saved_queries)); 227 $result['error_no'] = false; 228 /* 229 if (!empty($this->query_result)) 230 { 231 $result['error_msg'] = trim(@pg_result_error($this->query_result)); 232 if ($result['error_msg'] != '') 233 return $result; 234 } 235 236 $result['error_msg'] = (!empty($this->link_id)) ? trim(@pg_last_error($this->link_id)) : trim(@pg_last_error()); 237 */ 221 $result['error_no'] = $this->error_no; 238 222 $result['error_msg'] = $this->error_msg; 239 223 … … 262 246 return false; 263 247 } 248 249 250 function get_names() 251 { 252 $result = $this->query('SHOW client_encoding'); 253 return strtolower($this->result($result)); // MySQL returns lowercase so lets be consistent 254 } 255 256 257 function set_names($names) 258 { 259 return $this->query('SET NAMES \''.$this->escape($names).'\''); 260 } 261 262 263 function get_version() 264 { 265 $result = $this->query('SELECT VERSION()'); 266 267 return array( 268 'name' => 'PostgreSQL', 269 'version' => preg_replace('%^[^0-9]+([^\s,-]+).*$%', '\\1', $this->result($result)) 270 ); 271 } 272 273 274 function table_exists($table_name, $no_prefix = false) 275 { 276 $result = $this->query('SELECT 1 FROM pg_class WHERE relname = \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\''); 277 return $this->num_rows($result) > 0; 278 } 279 280 281 function field_exists($table_name, $field_name, $no_prefix = false) 282 { 283 $result = $this->query('SELECT 1 FROM pg_class c INNER JOIN pg_attribute a ON a.attrelid = c.oid WHERE c.relname = \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\' AND a.attname = \''.$this->escape($field_name).'\''); 284 return $this->num_rows($result) > 0; 285 } 286 287 288 function index_exists($table_name, $index_name, $no_prefix = false) 289 { 290 $result = $this->query('SELECT 1 FROM pg_index i INNER JOIN pg_class c1 ON c1.oid = i.indrelid INNER JOIN pg_class c2 ON c2.oid = i.indexrelid WHERE c1.relname = \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\' AND c2.relname = \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'_'.$this->escape($index_name).'\''); 291 return $this->num_rows($result) > 0; 292 } 293 294 295 function create_table($table_name, $schema, $no_prefix = false) 296 { 297 if ($this->table_exists($table_name, $no_prefix)) 298 return true; 299 300 $query = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name." (\n"; 301 302 // Go through every schema element and add it to the query 303 foreach ($schema['FIELDS'] as $field_name => $field_data) 304 { 305 $field_data['datatype'] = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_data['datatype']); 306 307 $query .= $field_name.' '.$field_data['datatype']; 308 309 // The SERIAL datatype is a special case where we don't need to say not null 310 if (!$field_data['allow_null'] && $field_data['datatype'] != 'SERIAL') 311 $query .= ' NOT NULL'; 312 313 if (isset($field_data['default'])) 314 $query .= ' DEFAULT '.$field_data['default']; 315 316 $query .= ",\n"; 317 } 318 319 // If we have a primary key, add it 320 if (isset($schema['PRIMARY KEY'])) 321 $query .= 'PRIMARY KEY ('.implode(',', $schema['PRIMARY KEY']).'),'."\n"; 322 323 // Add unique keys 324 if (isset($schema['UNIQUE KEYS'])) 325 { 326 foreach ($schema['UNIQUE KEYS'] as $key_name => $key_fields) 327 $query .= 'UNIQUE ('.implode(',', $key_fields).'),'."\n"; 328 } 329 330 // We remove the last two characters (a newline and a comma) and add on the ending 331 $query = substr($query, 0, strlen($query) - 2)."\n".')'; 332 333 $result = $this->query($query) ? true : false; 334 335 // Add indexes 336 if (isset($schema['INDEXES'])) 337 { 338 foreach ($schema['INDEXES'] as $index_name => $index_fields) 339 $result &= $this->add_index($table_name, $index_name, $index_fields, false, $no_prefix); 340 } 341 342 return $result; 343 } 344 345 346 function drop_table($table_name, $no_prefix = false) 347 { 348 if (!$this->table_exists($table_name, $no_prefix)) 349 return true; 350 351 return $this->query('DROP TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false; 352 } 353 354 355 function rename_table($old_table, $new_table, $no_prefix = false) 356 { 357 // If there new table exists and the old one doesn't, then we're happy 358 if ($this->table_exists($new_table, $no_prefix) && !$this->table_exists($old_table, $no_prefix)) 359 return true; 360 361 return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$old_table.' RENAME TO '.($no_prefix ? '' : $this->prefix).$new_table) ? true : false; 362 } 363 364 365 function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false) 366 { 367 if ($this->field_exists($table_name, $field_name, $no_prefix)) 368 return true; 369 370 $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type); 371 372 $result = $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ADD '.$field_name.' '.$field_type) ? true : false; 373 374 if ($default_value !== null) 375 { 376 if (!is_int($default_value) && !is_float($default_value)) 377 $default_value = '\''.$this->escape($default_value).'\''; 378 379 $result &= $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ALTER '.$field_name.' SET DEFAULT '.$default_value) ? true : false; 380 $result &= $this->query('UPDATE '.($no_prefix ? '' : $this->prefix).$table_name.' SET '.$field_name.'='.$default_value) ? true : false; 381 } 382 383 if (!$allow_null) 384 $result &= $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ALTER '.$field_name.' SET NOT NULL') ? true : false; 385 386 return $result; 387 } 388 389 390 function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false) 391 { 392 if (!$this->field_exists($table_name, $field_name, $no_prefix)) 393 return true; 394 395 $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type); 396 397 $result = $this->add_field($table_name, 'tmp_'.$field_name, $field_type, $allow_null, $default_value, $after_field, $no_prefix); 398 $result &= $this->query('UPDATE '.($no_prefix ? '' : $this->prefix).$table_name.' SET tmp_'.$field_name.' = '.$field_name) ? true : false; 399 $result &= $this->drop_field($table_name, $field_name, $no_prefix); 400 $result &= $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' RENAME COLUMN tmp_'.$field_name.' TO '.$field_name) ? true : false; 401 402 return $result; 403 } 404 405 406 function drop_field($table_name, $field_name, $no_prefix = false) 407 { 408 if (!$this->field_exists($table_name, $field_name, $no_prefix)) 409 return true; 410 411 return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP '.$field_name) ? true : false; 412 } 413 414 415 function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false) 416 { 417 if ($this->index_exists($table_name, $index_name, $no_prefix)) 418 return true; 419 420 return $this->query('CREATE '.($unique ? 'UNIQUE ' : '').'INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.' ON '.($no_prefix ? '' : $this->prefix).$table_name.'('.implode(',', $index_fields).')') ? true : false; 421 } 422 423 424 function drop_index($table_name, $index_name, $no_prefix = false) 425 { 426 if (!$this->index_exists($table_name, $index_name, $no_prefix)) 427 return true; 428 429 return $this->query('DROP INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) ? true : false; 430 } 431 432 function truncate_table($table_name, $no_prefix = false) 433 { 434 return $this->query('DELETE FROM '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false; 435 } 264 436 } -
branches/rsr.v5.1.dev/web/punbb/include/dblayer/sqlite.php
r1 r3 1 1 <?php 2 /*********************************************************************** 3 4 Copyright (C) 2002-2005 Rickard Andersson (rickard@punbb.org) 5 6 This file is part of PunBB. 7 8 PunBB is free software; you can redistribute it and/or modify it 9 under the terms of the GNU General Public License as published 10 by the Free Software Foundation; either version 2 of the License, 11 or (at your option) any later version. 12 13 PunBB is distributed in the hope that it will be useful, but 14 WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, 21 MA 02111-1307 USA 22 23 ************************************************************************/ 24 2 3 /** 4 * Copyright (C) 2008-2011 FluxBB 5 * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB 6 * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher 7 */ 25 8 26 9 // Make sure we have built in support for SQLite … … 42 25 var $error_msg = 'Unknown'; 43 26 27 var $datatype_transformations = array( 28 '%^SERIAL$%' => 'INTEGER', 29 '%^(TINY|SMALL|MEDIUM|BIG)?INT( )?(\\([0-9]+\\))?( )?(UNSIGNED)?$%i' => 'INTEGER', 30 '%^(TINY|MEDIUM|LONG)?TEXT$%i' => 'TEXT' 31 ); 32 44 33 45 34 function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect) … … 135 124 136 125 137 function result($query_id = 0, $row = 0 )126 function result($query_id = 0, $row = 0, $col = 0) 138 127 { 139 128 if ($query_id) 140 129 { 141 if ($row != 0) 142 @sqlite_seek($query_id, $row); 143 144 return @current(@sqlite_current($query_id)); 130 if ($row !== 0 && @sqlite_seek($query_id, $row) === false) 131 return false; 132 133 $cur_row = @sqlite_current($query_id); 134 if ($cur_row === false) 135 return false; 136 137 return $cur_row[$col]; 145 138 } 146 139 else … … 157 150 { 158 151 // Horrible hack to get rid of table names and table aliases from the array keys 159 while (list($key, $value) = @each($cur_row))152 foreach ($cur_row as $key => $value) 160 153 { 161 162 163 164 165 166 167 154 $dot_spot = strpos($key, '.'); 155 if ($dot_spot !== false) 156 { 157 unset($cur_row[$key]); 158 $key = substr($key, $dot_spot+1); 159 $cur_row[$key] = $value; 160 } 168 161 } 169 162 } … … 190 183 function affected_rows() 191 184 { 192 return ($this-> query_result) ? @sqlite_changes($this->query_result) : false;185 return ($this->link_id) ? @sqlite_changes($this->link_id) : false; 193 186 } 194 187 … … 220 213 function escape($str) 221 214 { 222 return sqlite_escape_string($str);215 return is_array($str) ? '' : sqlite_escape_string($str); 223 216 } 224 217 … … 251 244 return false; 252 245 } 246 247 248 function get_names() 249 { 250 return ''; 251 } 252 253 254 function set_names($names) 255 { 256 return true; 257 } 258 259 260 function get_version() 261 { 262 return array( 263 'name' => 'SQLite', 264 'version' => sqlite_libversion() 265 ); 266 } 267 268 269 function table_exists($table_name, $no_prefix = false) 270 { 271 $result = $this->query('SELECT 1 FROM sqlite_master WHERE name = \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\' AND type=\'table\''); 272 return $this->num_rows($result) > 0; 273 } 274 275 276 function field_exists($table_name, $field_name, $no_prefix = false) 277 { 278 $result = $this->query('SELECT sql FROM sqlite_master WHERE name = \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\' AND type=\'table\''); 279 if (!$this->num_rows($result)) 280 return false; 281 282 return preg_match('%[\r\n]'.preg_quote($field_name, '%').' %', $this->result($result)); 283 } 284 285 286 function index_exists($table_name, $index_name, $no_prefix = false) 287 { 288 $result = $this->query('SELECT 1 FROM sqlite_master WHERE tbl_name = \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\' AND name = \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'_'.$this->escape($index_name).'\' AND type=\'index\''); 289 return $this->num_rows($result) > 0; 290 } 291 292 293 function create_table($table_name, $schema, $no_prefix = false) 294 { 295 if ($this->table_exists($table_name, $no_prefix)) 296 return true; 297 298 $query = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name." (\n"; 299 300 // Go through every schema element and add it to the query 301 foreach ($schema['FIELDS'] as $field_name => $field_data) 302 { 303 $field_data['datatype'] = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_data['datatype']); 304 305 $query .= $field_name.' '.$field_data['datatype']; 306 307 if (!$field_data['allow_null']) 308 $query .= ' NOT NULL'; 309 310 if (isset($field_data['default'])) 311 $query .= ' DEFAULT '.$field_data['default']; 312 313 $query .= ",\n"; 314 } 315 316 // If we have a primary key, add it 317 if (isset($schema['PRIMARY KEY'])) 318 $query .= 'PRIMARY KEY ('.implode(',', $schema['PRIMARY KEY']).'),'."\n"; 319 320 // Add unique keys 321 if (isset($schema['UNIQUE KEYS'])) 322 { 323 foreach ($schema['UNIQUE KEYS'] as $key_name => $key_fields) 324 $query .= 'UNIQUE ('.implode(',', $key_fields).'),'."\n"; 325 } 326 327 // We remove the last two characters (a newline and a comma) and add on the ending 328 $query = substr($query, 0, strlen($query) - 2)."\n".')'; 329 330 $result = $this->query($query) ? true : false; 331 332 // Add indexes 333 if (isset($schema['INDEXES'])) 334 { 335 foreach ($schema['INDEXES'] as $index_name => $index_fields) 336 $result &= $this->add_index($table_name, $index_name, $index_fields, false, $no_prefix); 337 } 338 339 return $result; 340 } 341 342 343 function drop_table($table_name, $no_prefix = false) 344 { 345 if (!$this->table_exists($table_name, $no_prefix)) 346 return true; 347 348 return $this->query('DROP TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false; 349 } 350 351 352 function rename_table($old_name, $new_name, $no_prefix = false) 353 { 354 // If there new table exists and the old one doesn't, then we're happy 355 if ($this->table_exists($new_table, $no_prefix) && !$this->table_exists($old_table, $no_prefix)) 356 return true; 357 358 $table = $this->get_table_info($old_name, $no_prefix); 359 360 // Create new table 361 $newtable = str_replace('CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($old_name).' (', 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($new_name).' (', $table['sql']); 362 $result = $this->query($newtable) ? true : false; 363 364 // Recreate indexes 365 if (!empty($table['indices'])) 366 { 367 foreach ($table['indices'] as $cur_index) 368 $result &= $this->query($cur_index) ? true : false; 369 } 370 371 // Copy content across 372 $result &= $this->query('INSERT INTO '.($no_prefix ? '' : $this->prefix).$this->escape($new_name).' SELECT * FROM '.($no_prefix ? '' : $this->prefix).$this->escape($old_name)) ? true : false; 373 374 // Drop old table 375 $result &= $this->drop_table(($no_prefix ? '' : $this->prefix).$this->escape($table_name)); 376 377 return $result; 378 } 379 380 381 function get_table_info($table_name, $no_prefix = false) 382 { 383 // Grab table info 384 $result = $this->query('SELECT sql FROM sqlite_master WHERE tbl_name = \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\' ORDER BY type DESC') or error('Unable to fetch table information', __FILE__, __LINE__, $this->error()); 385 $num_rows = $this->num_rows($result); 386 387 if ($num_rows == 0) 388 return; 389 390 $table = array(); 391 $table['indices'] = array(); 392 while ($cur_index = $this->fetch_assoc($result)) 393 { 394 if (empty($cur_index['sql'])) 395 continue; 396 397 if (!isset($table['sql'])) 398 $table['sql'] = $cur_index['sql']; 399 else 400 $table['indices'][] = $cur_index['sql']; 401 } 402 403 // Work out the columns in the table currently 404 $table_lines = explode("\n", $table['sql']); 405 $table['columns'] = array(); 406 foreach ($table_lines as $table_line) 407 { 408 $table_line = trim($table_line, " \t\n\r,"); // trim spaces, tabs, newlines, and commas 409 if (substr($table_line, 0, 12) == 'CREATE TABLE') 410 continue; 411 else if (substr($table_line, 0, 11) == 'PRIMARY KEY') 412 $table['primary_key'] = $table_line; 413 else if (substr($table_line, 0, 6) == 'UNIQUE') 414 $table['unique'] = $table_line; 415 else if (substr($table_line, 0, strpos($table_line, ' ')) != '') 416 $table['columns'][substr($table_line, 0, strpos($table_line, ' '))] = trim(substr($table_line, strpos($table_line, ' '))); 417 } 418 419 return $table; 420 } 421 422 423 function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = 0, $no_prefix = false) 424 { 425 if ($this->field_exists($table_name, $field_name, $no_prefix)) 426 return true; 427 428 $table = $this->get_table_info($table_name, $no_prefix); 429 430 // Create temp table 431 $now = time(); 432 $tmptable = str_replace('CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).' (', 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'_t'.$now.' (', $table['sql']); 433 $result = $this->query($tmptable) ? true : false; 434 $result &= $this->query('INSERT INTO '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'_t'.$now.' SELECT * FROM '.($no_prefix ? '' : $this->prefix).$this->escape($table_name)) ? true : false; 435 436 // Create new table sql 437 $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type); 438 $query = $field_type; 439 if (!$allow_null) 440 $query .= ' NOT NULL'; 441 if ($default_value === null || $default_value === '') 442 $default_value = '\'\''; 443 444 $query .= ' DEFAULT '.$default_value; 445 446 $old_columns = array_keys($table['columns']); 447 array_insert($table['columns'], $after_field, $query, $field_name); 448 449 $new_table = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).' ('; 450 451 foreach ($table['columns'] as $cur_column => $column_details) 452 $new_table .= "\n".$cur_column.' '.$column_details.','; 453 454 if (isset($table['unique'])) 455 $new_table .= "\n".$table['unique'].','; 456 457 if (isset($table['primary_key'])) 458 $new_table .= "\n".$table['primary_key'].','; 459 460 $new_table = trim($new_table, ',')."\n".');'; 461 462 // Drop old table 463 $result &= $this->drop_table(($no_prefix ? '' : $this->prefix).$this->escape($table_name)); 464 465 // Create new table 466 $result &= $this->query($new_table) ? true : false; 467 468 // Recreate indexes 469 if (!empty($table['indices'])) 470 { 471 foreach ($table['indices'] as $cur_index) 472 $result &= $this->query($cur_index) ? true : false; 473 } 474 475 // Copy content back 476 $result &= $this->query('INSERT INTO '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).' ('.implode(', ', $old_columns).') SELECT * FROM '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'_t'.$now) ? true : false; 477 478 // Drop temp table 479 $result &= $this->drop_table(($no_prefix ? '' : $this->prefix).$this->escape($table_name).'_t'.$now); 480 481 return $result; 482 } 483 484 485 function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = 0, $no_prefix = false) 486 { 487 // Unneeded for SQLite 488 return true; 489 } 490 491 492 function drop_field($table_name, $field_name, $no_prefix = false) 493 { 494 if (!$this->field_exists($table_name, $field_name, $no_prefix)) 495 return true; 496 497 $table = $this->get_table_info($table_name, $no_prefix); 498 499 // Create temp table 500 $now = time(); 501 $tmptable = str_replace('CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).' (', 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'_t'.$now.' (', $table['sql']); 502 $result = $this->query($tmptable) ? true : false; 503 $result &= $this->query('INSERT INTO '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'_t'.$now.' SELECT * FROM '.($no_prefix ? '' : $this->prefix).$this->escape($table_name)) ? true : false; 504 505 // Work out the columns we need to keep and the sql for the new table 506 unset($table['columns'][$field_name]); 507 $new_columns = array_keys($table['columns']); 508 509 $new_table = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).' ('; 510 511 foreach ($table['columns'] as $cur_column => $column_details) 512 $new_table .= "\n".$cur_column.' '.$column_details.','; 513 514 if (isset($table['unique'])) 515 $new_table .= "\n".$table['unique'].','; 516 517 if (isset($table['primary_key'])) 518 $new_table .= "\n".$table['primary_key'].','; 519 520 $new_table = trim($new_table, ',')."\n".');'; 521 522 // Drop old table 523 $result &= $this->drop_table(($no_prefix ? '' : $this->prefix).$this->escape($table_name)); 524 525 // Create new table 526 $result &= $this->query($new_table) ? true : false; 527 528 // Recreate indexes 529 if (!empty($table['indices'])) 530 { 531 foreach ($table['indices'] as $cur_index) 532 if (!preg_match('%\('.preg_quote($field_name, '%').'\)%', $cur_index)) 533 $result &= $this->query($cur_index) ? true : false; 534 } 535 536 // Copy content back 537 $result &= $this->query('INSERT INTO '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).' SELECT '.implode(', ', $new_columns).' FROM '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'_t'.$now) ? true : false; 538 539 // Drop temp table 540 $result &= $this->drop_table(($no_prefix ? '' : $this->prefix).$this->escape($table_name).'_t'.$now); 541 542 return $result; 543 } 544 545 546 function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false) 547 { 548 if ($this->index_exists($table_name, $index_name, $no_prefix)) 549 return true; 550 551 return $this->query('CREATE '.($unique ? 'UNIQUE ' : '').'INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.' ON '.($no_prefix ? '' : $this->prefix).$table_name.'('.implode(',', $index_fields).')') ? true : false; 552 } 553 554 555 function drop_index($table_name, $index_name, $no_prefix = false) 556 { 557 if (!$this->index_exists($table_name, $index_name, $no_prefix)) 558 return true; 559 560 return $this->query('DROP INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) ? true : false; 561 } 562 563 function truncate_table($table_name, $no_prefix = false) 564 { 565 return $this->query('DELETE FROM '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false; 566 } 253 567 }
Note: See TracChangeset
for help on using the changeset viewer.