Deleted dblayer folder

This commit is contained in:
Visman 2017-01-07 12:32:58 +07:00
parent 3bd5693039
commit 45007513fe
8 changed files with 0 additions and 2645 deletions

View file

@ -1,48 +0,0 @@
<?php
/**
* Copyright (C) 2008-2012 FluxBB
* based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
*/
// Make sure no one attempts to run this script "directly"
if (!defined('PUN'))
exit;
// Load the appropriate DB layer class
switch ($db_type)
{
case 'mysql':
require_once PUN_ROOT.'include/dblayer/mysql.php';
break;
case 'mysql_innodb':
require_once PUN_ROOT.'include/dblayer/mysql_innodb.php';
break;
case 'mysqli':
require_once PUN_ROOT.'include/dblayer/mysqli.php';
break;
case 'mysqli_innodb':
require_once PUN_ROOT.'include/dblayer/mysqli_innodb.php';
break;
case 'pgsql':
require_once PUN_ROOT.'include/dblayer/pgsql.php';
break;
case 'sqlite':
require_once PUN_ROOT.'include/dblayer/sqlite.php';
break;
default:
error('\''.$db_type.'\' is not a valid database type. Please check settings in config.php.', __FILE__, __LINE__);
break;
}
// Create the database adapter object (and open/connect to/select db)
$db = new DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect);

View file

@ -1 +0,0 @@
<html><head><title>.</title></head><body>.</body></html>

View file

@ -1,378 +0,0 @@
<?php
/**
* Copyright (C) 2008-2012 FluxBB
* based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
*/
// Make sure we have built in support for MySQL
if (!function_exists('mysql_connect'))
exit('This PHP environment doesn\'t have MySQL support built in. MySQL support is required if you want to use a MySQL database to run this forum. Consult the PHP documentation for further assistance.');
class DBLayer
{
var $prefix;
var $link_id;
var $query_result;
var $saved_queries = array();
var $num_queries = 0;
var $error_no = false;
var $error_msg = 'Unknown';
var $datatype_transformations = array(
'%^SERIAL$%' => 'INT(10) UNSIGNED AUTO_INCREMENT'
);
function __construct($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
{
$this->prefix = $db_prefix;
if ($p_connect)
$this->link_id = @mysql_pconnect($db_host, $db_username, $db_password);
else
$this->link_id = @mysql_connect($db_host, $db_username, $db_password);
if ($this->link_id)
{
if (!@mysql_select_db($db_name, $this->link_id))
error('Unable to select database. MySQL reported: '.mysql_error(), __FILE__, __LINE__);
}
else
error('Unable to connect to MySQL server. MySQL reported: '.mysql_error(), __FILE__, __LINE__);
// Setup the client-server character set (UTF-8)
if (!defined('FORUM_NO_SET_NAMES'))
$this->set_names('utf8');
return $this->link_id;
}
function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
{
$this->__construct($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect);
}
function start_transaction()
{
return;
}
function end_transaction()
{
return;
}
function query($sql, $unbuffered = false)
{
if (defined('PUN_SHOW_QUERIES'))
$q_start = microtime(true);
if ($unbuffered)
$this->query_result = @mysql_unbuffered_query($sql, $this->link_id);
else
$this->query_result = @mysql_query($sql, $this->link_id);
if ($this->query_result)
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array($sql, sprintf('%.5f', microtime(true) - $q_start));
++$this->num_queries;
return $this->query_result;
}
else
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array($sql, 0);
$this->error_no = @mysql_errno($this->link_id);
$this->error_msg = @mysql_error($this->link_id);
return false;
}
}
function result($query_id = 0, $row = 0, $col = 0)
{
return ($query_id) ? @mysql_result($query_id, $row, $col) : false;
}
function fetch_assoc($query_id = 0)
{
return ($query_id) ? @mysql_fetch_assoc($query_id) : false;
}
function fetch_row($query_id = 0)
{
return ($query_id) ? @mysql_fetch_row($query_id) : false;
}
function num_rows($query_id = 0)
{
return ($query_id) ? @mysql_num_rows($query_id) : false;
}
function affected_rows()
{
return ($this->link_id) ? @mysql_affected_rows($this->link_id) : false;
}
function insert_id()
{
return ($this->link_id) ? @mysql_insert_id($this->link_id) : false;
}
function get_num_queries()
{
return $this->num_queries;
}
function get_saved_queries()
{
return $this->saved_queries;
}
function free_result($query_id = false)
{
return ($query_id) ? @mysql_free_result($query_id) : false;
}
function escape($str)
{
if (is_array($str))
return '';
else if (function_exists('mysql_real_escape_string'))
return mysql_real_escape_string($str, $this->link_id);
else
return mysql_escape_string($str);
}
function error()
{
$result['error_sql'] = @current(@end($this->saved_queries));
$result['error_no'] = $this->error_no;
$result['error_msg'] = $this->error_msg;
return $result;
}
function close()
{
if ($this->link_id)
{
if (is_resource($this->query_result))
@mysql_free_result($this->query_result);
return @mysql_close($this->link_id);
}
else
return false;
}
function get_names()
{
$result = $this->query('SHOW VARIABLES LIKE \'character_set_connection\'');
return $this->result($result, 0, 1);
}
function set_names($names)
{
return $this->query('SET NAMES \''.$this->escape($names).'\'');
}
function get_version()
{
$result = $this->query('SELECT VERSION()');
return array(
'name' => 'MySQL Standard',
'version' => preg_replace('%^([^-]+).*$%', '\\1', $this->result($result))
);
}
function table_exists($table_name, $no_prefix = false)
{
$result = $this->query('SHOW TABLES LIKE \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\'');
return $this->num_rows($result) > 0;
}
function field_exists($table_name, $field_name, $no_prefix = false)
{
$result = $this->query('SHOW COLUMNS FROM '.($no_prefix ? '' : $this->prefix).$table_name.' LIKE \''.$this->escape($field_name).'\'');
return $this->num_rows($result) > 0;
}
function index_exists($table_name, $index_name, $no_prefix = false)
{
$exists = false;
$result = $this->query('SHOW INDEX FROM '.($no_prefix ? '' : $this->prefix).$table_name);
while ($cur_index = $this->fetch_assoc($result))
{
if (strtolower($cur_index['Key_name']) == strtolower(($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name))
{
$exists = true;
break;
}
}
return $exists;
}
function create_table($table_name, $schema, $no_prefix = false)
{
if ($this->table_exists($table_name, $no_prefix))
return true;
$query = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name." (\n";
// Go through every schema element and add it to the query
foreach ($schema['FIELDS'] as $field_name => $field_data)
{
$field_data['datatype'] = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_data['datatype']);
$query .= $field_name.' '.$field_data['datatype'];
if (isset($field_data['collation']))
$query .= 'CHARACTER SET utf8 COLLATE utf8_'.$field_data['collation'];
if (!$field_data['allow_null'])
$query .= ' NOT NULL';
if (isset($field_data['default']))
$query .= ' DEFAULT '.$field_data['default'];
$query .= ",\n";
}
// If we have a primary key, add it
if (isset($schema['PRIMARY KEY']))
$query .= 'PRIMARY KEY ('.implode(',', $schema['PRIMARY KEY']).'),'."\n";
// Add unique keys
if (isset($schema['UNIQUE KEYS']))
{
foreach ($schema['UNIQUE KEYS'] as $key_name => $key_fields)
$query .= 'UNIQUE KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$key_name.'('.implode(',', $key_fields).'),'."\n";
}
// Add indexes
if (isset($schema['INDEXES']))
{
foreach ($schema['INDEXES'] as $index_name => $index_fields)
$query .= 'KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.'('.implode(',', $index_fields).'),'."\n";
}
// We remove the last two characters (a newline and a comma) and add on the ending
$query = substr($query, 0, strlen($query) - 2)."\n".') ENGINE = '.(isset($schema['ENGINE']) ? $schema['ENGINE'] : 'MyISAM').' CHARACTER SET utf8';
return $this->query($query) ? true : false;
}
function drop_table($table_name, $no_prefix = false)
{
if (!$this->table_exists($table_name, $no_prefix))
return true;
return $this->query('DROP TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
}
function rename_table($old_table, $new_table, $no_prefix = false)
{
// If the new table exists and the old one doesn't, then we're happy
if ($this->table_exists($new_table, $no_prefix) && !$this->table_exists($old_table, $no_prefix))
return true;
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$old_table.' RENAME TO '.($no_prefix ? '' : $this->prefix).$new_table) ? true : false;
}
function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
{
if ($this->field_exists($table_name, $field_name, $no_prefix))
return true;
$field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
if (!is_null($default_value) && !is_int($default_value) && !is_float($default_value))
$default_value = '\''.$this->escape($default_value).'\'';
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ADD '.$field_name.' '.$field_type.($allow_null ? '' : ' NOT NULL').(!is_null($default_value) ? ' DEFAULT '.$default_value : '').(!is_null($after_field) ? ' AFTER '.$after_field : '')) ? true : false;
}
function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
{
if (!$this->field_exists($table_name, $field_name, $no_prefix))
return true;
$field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
if (!is_null($default_value) && !is_int($default_value) && !is_float($default_value))
$default_value = '\''.$this->escape($default_value).'\'';
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' MODIFY '.$field_name.' '.$field_type.($allow_null ? '' : ' NOT NULL').(!is_null($default_value) ? ' DEFAULT '.$default_value : '').(!is_null($after_field) ? ' AFTER '.$after_field : '')) ? true : false;
}
function drop_field($table_name, $field_name, $no_prefix = false)
{
if (!$this->field_exists($table_name, $field_name, $no_prefix))
return true;
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP '.$field_name) ? true : false;
}
function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false)
{
if ($this->index_exists($table_name, $index_name, $no_prefix))
return true;
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;
}
function drop_index($table_name, $index_name, $no_prefix = false)
{
if (!$this->index_exists($table_name, $index_name, $no_prefix))
return true;
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) ? true : false;
}
function truncate_table($table_name, $no_prefix = false)
{
return $this->query('TRUNCATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
}
}

View file

@ -1,392 +0,0 @@
<?php
/**
* Copyright (C) 2008-2012 FluxBB
* based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
*/
// Make sure we have built in support for MySQL
if (!function_exists('mysql_connect'))
exit('This PHP environment doesn\'t have MySQL support built in. MySQL support is required if you want to use a MySQL database to run this forum. Consult the PHP documentation for further assistance.');
class DBLayer
{
var $prefix;
var $link_id;
var $query_result;
var $in_transaction = 0;
var $saved_queries = array();
var $num_queries = 0;
var $error_no = false;
var $error_msg = 'Unknown';
var $datatype_transformations = array(
'%^SERIAL$%' => 'INT(10) UNSIGNED AUTO_INCREMENT'
);
function __construct($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
{
$this->prefix = $db_prefix;
if ($p_connect)
$this->link_id = @mysql_pconnect($db_host, $db_username, $db_password);
else
$this->link_id = @mysql_connect($db_host, $db_username, $db_password);
if ($this->link_id)
{
if (!@mysql_select_db($db_name, $this->link_id))
error('Unable to select database. MySQL reported: '.mysql_error(), __FILE__, __LINE__);
}
else
error('Unable to connect to MySQL server. MySQL reported: '.mysql_error(), __FILE__, __LINE__);
// Setup the client-server character set (UTF-8)
if (!defined('FORUM_NO_SET_NAMES'))
$this->set_names('utf8');
return $this->link_id;
}
function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
{
$this->__construct($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect);
}
function start_transaction()
{
++$this->in_transaction;
mysql_query('START TRANSACTION', $this->link_id);
return;
}
function end_transaction()
{
--$this->in_transaction;
mysql_query('COMMIT', $this->link_id);
return;
}
function query($sql, $unbuffered = false)
{
if (defined('PUN_SHOW_QUERIES'))
$q_start = microtime(true);
if ($unbuffered)
$this->query_result = @mysql_unbuffered_query($sql, $this->link_id);
else
$this->query_result = @mysql_query($sql, $this->link_id);
if ($this->query_result)
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array($sql, sprintf('%.5f', microtime(true) - $q_start));
++$this->num_queries;
return $this->query_result;
}
else
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array($sql, 0);
$this->error_no = @mysql_errno($this->link_id);
$this->error_msg = @mysql_error($this->link_id);
// Rollback transaction
if ($this->in_transaction)
mysql_query('ROLLBACK', $this->link_id);
--$this->in_transaction;
return false;
}
}
function result($query_id = 0, $row = 0, $col = 0)
{
return ($query_id) ? @mysql_result($query_id, $row, $col) : false;
}
function fetch_assoc($query_id = 0)
{
return ($query_id) ? @mysql_fetch_assoc($query_id) : false;
}
function fetch_row($query_id = 0)
{
return ($query_id) ? @mysql_fetch_row($query_id) : false;
}
function num_rows($query_id = 0)
{
return ($query_id) ? @mysql_num_rows($query_id) : false;
}
function affected_rows()
{
return ($this->link_id) ? @mysql_affected_rows($this->link_id) : false;
}
function insert_id()
{
return ($this->link_id) ? @mysql_insert_id($this->link_id) : false;
}
function get_num_queries()
{
return $this->num_queries;
}
function get_saved_queries()
{
return $this->saved_queries;
}
function free_result($query_id = false)
{
return ($query_id) ? @mysql_free_result($query_id) : false;
}
function escape($str)
{
if (is_array($str))
return '';
else if (function_exists('mysql_real_escape_string'))
return mysql_real_escape_string($str, $this->link_id);
else
return mysql_escape_string($str);
}
function error()
{
$result['error_sql'] = @current(@end($this->saved_queries));
$result['error_no'] = $this->error_no;
$result['error_msg'] = $this->error_msg;
return $result;
}
function close()
{
if ($this->link_id)
{
if (is_resource($this->query_result))
@mysql_free_result($this->query_result);
return @mysql_close($this->link_id);
}
else
return false;
}
function get_names()
{
$result = $this->query('SHOW VARIABLES LIKE \'character_set_connection\'');
return $this->result($result, 0, 1);
}
function set_names($names)
{
return $this->query('SET NAMES \''.$this->escape($names).'\'');
}
function get_version()
{
$result = $this->query('SELECT VERSION()');
return array(
'name' => 'MySQL Standard (InnoDB)',
'version' => preg_replace('%^([^-]+).*$%', '\\1', $this->result($result))
);
}
function table_exists($table_name, $no_prefix = false)
{
$result = $this->query('SHOW TABLES LIKE \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\'');
return $this->num_rows($result) > 0;
}
function field_exists($table_name, $field_name, $no_prefix = false)
{
$result = $this->query('SHOW COLUMNS FROM '.($no_prefix ? '' : $this->prefix).$table_name.' LIKE \''.$this->escape($field_name).'\'');
return $this->num_rows($result) > 0;
}
function index_exists($table_name, $index_name, $no_prefix = false)
{
$exists = false;
$result = $this->query('SHOW INDEX FROM '.($no_prefix ? '' : $this->prefix).$table_name);
while ($cur_index = $this->fetch_assoc($result))
{
if (strtolower($cur_index['Key_name']) == strtolower(($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name))
{
$exists = true;
break;
}
}
return $exists;
}
function create_table($table_name, $schema, $no_prefix = false)
{
if ($this->table_exists($table_name, $no_prefix))
return true;
$query = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name." (\n";
// Go through every schema element and add it to the query
foreach ($schema['FIELDS'] as $field_name => $field_data)
{
$field_data['datatype'] = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_data['datatype']);
$query .= $field_name.' '.$field_data['datatype'];
if (isset($field_data['collation']))
$query .= 'CHARACTER SET utf8 COLLATE utf8_'.$field_data['collation'];
if (!$field_data['allow_null'])
$query .= ' NOT NULL';
if (isset($field_data['default']))
$query .= ' DEFAULT '.$field_data['default'];
$query .= ",\n";
}
// If we have a primary key, add it
if (isset($schema['PRIMARY KEY']))
$query .= 'PRIMARY KEY ('.implode(',', $schema['PRIMARY KEY']).'),'."\n";
// Add unique keys
if (isset($schema['UNIQUE KEYS']))
{
foreach ($schema['UNIQUE KEYS'] as $key_name => $key_fields)
$query .= 'UNIQUE KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$key_name.'('.implode(',', $key_fields).'),'."\n";
}
// Add indexes
if (isset($schema['INDEXES']))
{
foreach ($schema['INDEXES'] as $index_name => $index_fields)
$query .= 'KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.'('.implode(',', $index_fields).'),'."\n";
}
// We remove the last two characters (a newline and a comma) and add on the ending
$query = substr($query, 0, strlen($query) - 2)."\n".') ENGINE = '.(isset($schema['ENGINE']) ? $schema['ENGINE'] : 'InnoDB').' CHARACTER SET utf8';
return $this->query($query) ? true : false;
}
function drop_table($table_name, $no_prefix = false)
{
if (!$this->table_exists($table_name, $no_prefix))
return true;
return $this->query('DROP TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
}
function rename_table($old_table, $new_table, $no_prefix = false)
{
// If the new table exists and the old one doesn't, then we're happy
if ($this->table_exists($new_table, $no_prefix) && !$this->table_exists($old_table, $no_prefix))
return true;
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$old_table.' RENAME TO '.($no_prefix ? '' : $this->prefix).$new_table) ? true : false;
}
function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
{
if ($this->field_exists($table_name, $field_name, $no_prefix))
return true;
$field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
if (!is_null($default_value) && !is_int($default_value) && !is_float($default_value))
$default_value = '\''.$this->escape($default_value).'\'';
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ADD '.$field_name.' '.$field_type.($allow_null ? '' : ' NOT NULL').(!is_null($default_value) ? ' DEFAULT '.$default_value : '').(!is_null($after_field) ? ' AFTER '.$after_field : '')) ? true : false;
}
function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
{
if (!$this->field_exists($table_name, $field_name, $no_prefix))
return true;
$field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
if (!is_null($default_value) && !is_int($default_value) && !is_float($default_value))
$default_value = '\''.$this->escape($default_value).'\'';
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' MODIFY '.$field_name.' '.$field_type.($allow_null ? '' : ' NOT NULL').(!is_null($default_value) ? ' DEFAULT '.$default_value : '').(!is_null($after_field) ? ' AFTER '.$after_field : '')) ? true : false;
}
function drop_field($table_name, $field_name, $no_prefix = false)
{
if (!$this->field_exists($table_name, $field_name, $no_prefix))
return true;
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP '.$field_name) ? true : false;
}
function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false)
{
if ($this->index_exists($table_name, $index_name, $no_prefix))
return true;
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;
}
function drop_index($table_name, $index_name, $no_prefix = false)
{
if (!$this->index_exists($table_name, $index_name, $no_prefix))
return true;
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) ? true : false;
}
function truncate_table($table_name, $no_prefix = false)
{
return $this->query('TRUNCATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
}
}

View file

@ -1,385 +0,0 @@
<?php
/**
* Copyright (C) 2008-2012 FluxBB
* based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
*/
// Make sure we have built in support for MySQL
if (!function_exists('mysqli_connect'))
exit('This PHP environment doesn\'t have Improved MySQL (mysqli) support built in. Improved MySQL support is required if you want to use a MySQL 4.1 (or later) database to run this forum. Consult the PHP documentation for further assistance.');
class DBLayer
{
var $prefix;
var $link_id;
var $query_result;
var $saved_queries = array();
var $num_queries = 0;
var $error_no = false;
var $error_msg = 'Unknown';
var $datatype_transformations = array(
'%^SERIAL$%' => 'INT(10) UNSIGNED AUTO_INCREMENT'
);
function __construct($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
{
$this->prefix = $db_prefix;
// Was a custom port supplied with $db_host?
if (strpos($db_host, ':') !== false)
list($db_host, $db_port) = explode(':', $db_host);
// Persistent connection in MySQLi are only available in PHP 5.3 and later releases
$p_connect = $p_connect && version_compare(PHP_VERSION, '5.3.0', '>=') ? 'p:' : '';
if (isset($db_port))
$this->link_id = @mysqli_connect($p_connect.$db_host, $db_username, $db_password, $db_name, $db_port);
else
$this->link_id = @mysqli_connect($p_connect.$db_host, $db_username, $db_password, $db_name);
if (!$this->link_id)
error('Unable to connect to MySQL and select database. MySQL reported: '.mysqli_connect_error(), __FILE__, __LINE__);
// Setup the client-server character set (UTF-8)
if (!defined('FORUM_NO_SET_NAMES'))
$this->set_names('utf8');
return $this->link_id;
}
function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
{
$this->__construct($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect);
}
function start_transaction()
{
return;
}
function end_transaction()
{
return;
}
function query($sql, $unbuffered = false)
{
if (defined('PUN_SHOW_QUERIES'))
$q_start = microtime(true);
$this->query_result = @mysqli_query($this->link_id, $sql);
if ($this->query_result)
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array($sql, sprintf('%.5f', microtime(true) - $q_start));
++$this->num_queries;
return $this->query_result;
}
else
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array($sql, 0);
$this->error_no = @mysqli_errno($this->link_id);
$this->error_msg = @mysqli_error($this->link_id);
return false;
}
}
function result($query_id = 0, $row = 0, $col = 0)
{
if ($query_id)
{
if ($row !== 0 && @mysqli_data_seek($query_id, $row) === false)
return false;
$cur_row = @mysqli_fetch_row($query_id);
if ($cur_row === false)
return false;
return $cur_row[$col];
}
else
return false;
}
function fetch_assoc($query_id = 0)
{
return ($query_id) ? @mysqli_fetch_assoc($query_id) : false;
}
function fetch_row($query_id = 0)
{
return ($query_id) ? @mysqli_fetch_row($query_id) : false;
}
function num_rows($query_id = 0)
{
return ($query_id) ? @mysqli_num_rows($query_id) : false;
}
function affected_rows()
{
return ($this->link_id) ? @mysqli_affected_rows($this->link_id) : false;
}
function insert_id()
{
return ($this->link_id) ? @mysqli_insert_id($this->link_id) : false;
}
function get_num_queries()
{
return $this->num_queries;
}
function get_saved_queries()
{
return $this->saved_queries;
}
function free_result($query_id = false)
{
return ($query_id) ? @mysqli_free_result($query_id) : false;
}
function escape($str)
{
return is_array($str) ? '' : mysqli_real_escape_string($this->link_id, $str);
}
function error()
{
$result['error_sql'] = @current(@end($this->saved_queries));
$result['error_no'] = $this->error_no;
$result['error_msg'] = $this->error_msg;
return $result;
}
function close()
{
if ($this->link_id)
{
if ($this->query_result instanceof mysqli_result)
@mysqli_free_result($this->query_result);
return @mysqli_close($this->link_id);
}
else
return false;
}
function get_names()
{
$result = $this->query('SHOW VARIABLES LIKE \'character_set_connection\'');
return $this->result($result, 0, 1);
}
function set_names($names)
{
return $this->query('SET NAMES \''.$this->escape($names).'\'');
}
function get_version()
{
$result = $this->query('SELECT VERSION()');
return array(
'name' => 'MySQL Improved',
'version' => preg_replace('%^([^-]+).*$%', '\\1', $this->result($result))
);
}
function table_exists($table_name, $no_prefix = false)
{
$result = $this->query('SHOW TABLES LIKE \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\'');
return $this->num_rows($result) > 0;
}
function field_exists($table_name, $field_name, $no_prefix = false)
{
$result = $this->query('SHOW COLUMNS FROM '.($no_prefix ? '' : $this->prefix).$table_name.' LIKE \''.$this->escape($field_name).'\'');
return $this->num_rows($result) > 0;
}
function index_exists($table_name, $index_name, $no_prefix = false)
{
$exists = false;
$result = $this->query('SHOW INDEX FROM '.($no_prefix ? '' : $this->prefix).$table_name);
while ($cur_index = $this->fetch_assoc($result))
{
if (strtolower($cur_index['Key_name']) == strtolower(($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name))
{
$exists = true;
break;
}
}
return $exists;
}
function create_table($table_name, $schema, $no_prefix = false)
{
if ($this->table_exists($table_name, $no_prefix))
return true;
$query = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name." (\n";
// Go through every schema element and add it to the query
foreach ($schema['FIELDS'] as $field_name => $field_data)
{
$field_data['datatype'] = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_data['datatype']);
$query .= $field_name.' '.$field_data['datatype'];
if (isset($field_data['collation']))
$query .= 'CHARACTER SET utf8 COLLATE utf8_'.$field_data['collation'];
if (!$field_data['allow_null'])
$query .= ' NOT NULL';
if (isset($field_data['default']))
$query .= ' DEFAULT '.$field_data['default'];
$query .= ",\n";
}
// If we have a primary key, add it
if (isset($schema['PRIMARY KEY']))
$query .= 'PRIMARY KEY ('.implode(',', $schema['PRIMARY KEY']).'),'."\n";
// Add unique keys
if (isset($schema['UNIQUE KEYS']))
{
foreach ($schema['UNIQUE KEYS'] as $key_name => $key_fields)
$query .= 'UNIQUE KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$key_name.'('.implode(',', $key_fields).'),'."\n";
}
// Add indexes
if (isset($schema['INDEXES']))
{
foreach ($schema['INDEXES'] as $index_name => $index_fields)
$query .= 'KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.'('.implode(',', $index_fields).'),'."\n";
}
// We remove the last two characters (a newline and a comma) and add on the ending
$query = substr($query, 0, strlen($query) - 2)."\n".') ENGINE = '.(isset($schema['ENGINE']) ? $schema['ENGINE'] : 'MyISAM').' CHARACTER SET utf8';
return $this->query($query) ? true : false;
}
function drop_table($table_name, $no_prefix = false)
{
if (!$this->table_exists($table_name, $no_prefix))
return true;
return $this->query('DROP TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
}
function rename_table($old_table, $new_table, $no_prefix = false)
{
// If the new table exists and the old one doesn't, then we're happy
if ($this->table_exists($new_table, $no_prefix) && !$this->table_exists($old_table, $no_prefix))
return true;
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$old_table.' RENAME TO '.($no_prefix ? '' : $this->prefix).$new_table) ? true : false;
}
function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
{
if ($this->field_exists($table_name, $field_name, $no_prefix))
return true;
$field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
if (!is_null($default_value) && !is_int($default_value) && !is_float($default_value))
$default_value = '\''.$this->escape($default_value).'\'';
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ADD '.$field_name.' '.$field_type.($allow_null ? '' : ' NOT NULL').(!is_null($default_value) ? ' DEFAULT '.$default_value : '').(!is_null($after_field) ? ' AFTER '.$after_field : '')) ? true : false;
}
function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
{
if (!$this->field_exists($table_name, $field_name, $no_prefix))
return true;
$field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
if (!is_null($default_value) && !is_int($default_value) && !is_float($default_value))
$default_value = '\''.$this->escape($default_value).'\'';
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' MODIFY '.$field_name.' '.$field_type.($allow_null ? '' : ' NOT NULL').(!is_null($default_value) ? ' DEFAULT '.$default_value : '').(!is_null($after_field) ? ' AFTER '.$after_field : '')) ? true : false;
}
function drop_field($table_name, $field_name, $no_prefix = false)
{
if (!$this->field_exists($table_name, $field_name, $no_prefix))
return true;
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP '.$field_name) ? true : false;
}
function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false)
{
if ($this->index_exists($table_name, $index_name, $no_prefix))
return true;
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;
}
function drop_index($table_name, $index_name, $no_prefix = false)
{
if (!$this->index_exists($table_name, $index_name, $no_prefix))
return true;
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) ? true : false;
}
function truncate_table($table_name, $no_prefix = false)
{
return $this->query('TRUNCATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
}
}

View file

@ -1,398 +0,0 @@
<?php
/**
* Copyright (C) 2008-2012 FluxBB
* based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
*/
// Make sure we have built in support for MySQL
if (!function_exists('mysqli_connect'))
exit('This PHP environment doesn\'t have Improved MySQL (mysqli) support built in. Improved MySQL support is required if you want to use a MySQL 4.1 (or later) database to run this forum. Consult the PHP documentation for further assistance.');
class DBLayer
{
var $prefix;
var $link_id;
var $query_result;
var $saved_queries = array();
var $num_queries = 0;
var $in_transaction = 0;
var $error_no = false;
var $error_msg = 'Unknown';
var $datatype_transformations = array(
'%^SERIAL$%' => 'INT(10) UNSIGNED AUTO_INCREMENT'
);
function __construct($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
{
$this->prefix = $db_prefix;
// Was a custom port supplied with $db_host?
if (strpos($db_host, ':') !== false)
list($db_host, $db_port) = explode(':', $db_host);
// Persistent connection in MySQLi are only available in PHP 5.3 and later releases
$p_connect = $p_connect && version_compare(PHP_VERSION, '5.3.0', '>=') ? 'p:' : '';
if (isset($db_port))
$this->link_id = @mysqli_connect($p_connect.$db_host, $db_username, $db_password, $db_name, $db_port);
else
$this->link_id = @mysqli_connect($p_connect.$db_host, $db_username, $db_password, $db_name);
if (!$this->link_id)
error('Unable to connect to MySQL and select database. MySQL reported: '.mysqli_connect_error(), __FILE__, __LINE__);
// Setup the client-server character set (UTF-8)
if (!defined('FORUM_NO_SET_NAMES'))
$this->set_names('utf8');
return $this->link_id;
}
function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
{
$this->__construct($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect);
}
function start_transaction()
{
++$this->in_transaction;
mysqli_query($this->link_id, 'START TRANSACTION');
return;
}
function end_transaction()
{
--$this->in_transaction;
mysqli_query($this->link_id, 'COMMIT');
return;
}
function query($sql, $unbuffered = false)
{
if (defined('PUN_SHOW_QUERIES'))
$q_start = microtime(true);
$this->query_result = @mysqli_query($this->link_id, $sql);
if ($this->query_result)
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array($sql, sprintf('%.5f', microtime(true) - $q_start));
++$this->num_queries;
return $this->query_result;
}
else
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array($sql, 0);
$this->error_no = @mysqli_errno($this->link_id);
$this->error_msg = @mysqli_error($this->link_id);
// Rollback transaction
if ($this->in_transaction)
mysqli_query($this->link_id, 'ROLLBACK');
--$this->in_transaction;
return false;
}
}
function result($query_id = 0, $row = 0, $col = 0)
{
if ($query_id)
{
if ($row !== 0 && @mysqli_data_seek($query_id, $row) === false)
return false;
$cur_row = @mysqli_fetch_row($query_id);
if ($cur_row === false)
return false;
return $cur_row[$col];
}
else
return false;
}
function fetch_assoc($query_id = 0)
{
return ($query_id) ? @mysqli_fetch_assoc($query_id) : false;
}
function fetch_row($query_id = 0)
{
return ($query_id) ? @mysqli_fetch_row($query_id) : false;
}
function num_rows($query_id = 0)
{
return ($query_id) ? @mysqli_num_rows($query_id) : false;
}
function affected_rows()
{
return ($this->link_id) ? @mysqli_affected_rows($this->link_id) : false;
}
function insert_id()
{
return ($this->link_id) ? @mysqli_insert_id($this->link_id) : false;
}
function get_num_queries()
{
return $this->num_queries;
}
function get_saved_queries()
{
return $this->saved_queries;
}
function free_result($query_id = false)
{
return ($query_id) ? @mysqli_free_result($query_id) : false;
}
function escape($str)
{
return is_array($str) ? '' : mysqli_real_escape_string($this->link_id, $str);
}
function error()
{
$result['error_sql'] = @current(@end($this->saved_queries));
$result['error_no'] = $this->error_no;
$result['error_msg'] = $this->error_msg;
return $result;
}
function close()
{
if ($this->link_id)
{
if ($this->query_result instanceof mysqli_result)
@mysqli_free_result($this->query_result);
return @mysqli_close($this->link_id);
}
else
return false;
}
function get_names()
{
$result = $this->query('SHOW VARIABLES LIKE \'character_set_connection\'');
return $this->result($result, 0, 1);
}
function set_names($names)
{
return $this->query('SET NAMES \''.$this->escape($names).'\'');
}
function get_version()
{
$result = $this->query('SELECT VERSION()');
return array(
'name' => 'MySQL Improved (InnoDB)',
'version' => preg_replace('%^([^-]+).*$%', '\\1', $this->result($result))
);
}
function table_exists($table_name, $no_prefix = false)
{
$result = $this->query('SHOW TABLES LIKE \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\'');
return $this->num_rows($result) > 0;
}
function field_exists($table_name, $field_name, $no_prefix = false)
{
$result = $this->query('SHOW COLUMNS FROM '.($no_prefix ? '' : $this->prefix).$table_name.' LIKE \''.$this->escape($field_name).'\'');
return $this->num_rows($result) > 0;
}
function index_exists($table_name, $index_name, $no_prefix = false)
{
$exists = false;
$result = $this->query('SHOW INDEX FROM '.($no_prefix ? '' : $this->prefix).$table_name);
while ($cur_index = $this->fetch_assoc($result))
{
if (strtolower($cur_index['Key_name']) == strtolower(($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name))
{
$exists = true;
break;
}
}
return $exists;
}
function create_table($table_name, $schema, $no_prefix = false)
{
if ($this->table_exists($table_name, $no_prefix))
return true;
$query = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name." (\n";
// Go through every schema element and add it to the query
foreach ($schema['FIELDS'] as $field_name => $field_data)
{
$field_data['datatype'] = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_data['datatype']);
$query .= $field_name.' '.$field_data['datatype'];
if (isset($field_data['collation']))
$query .= 'CHARACTER SET utf8 COLLATE utf8_'.$field_data['collation'];
if (!$field_data['allow_null'])
$query .= ' NOT NULL';
if (isset($field_data['default']))
$query .= ' DEFAULT '.$field_data['default'];
$query .= ",\n";
}
// If we have a primary key, add it
if (isset($schema['PRIMARY KEY']))
$query .= 'PRIMARY KEY ('.implode(',', $schema['PRIMARY KEY']).'),'."\n";
// Add unique keys
if (isset($schema['UNIQUE KEYS']))
{
foreach ($schema['UNIQUE KEYS'] as $key_name => $key_fields)
$query .= 'UNIQUE KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$key_name.'('.implode(',', $key_fields).'),'."\n";
}
// Add indexes
if (isset($schema['INDEXES']))
{
foreach ($schema['INDEXES'] as $index_name => $index_fields)
$query .= 'KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.'('.implode(',', $index_fields).'),'."\n";
}
// We remove the last two characters (a newline and a comma) and add on the ending
$query = substr($query, 0, strlen($query) - 2)."\n".') ENGINE = '.(isset($schema['ENGINE']) ? $schema['ENGINE'] : 'InnoDB').' CHARACTER SET utf8';
return $this->query($query) ? true : false;
}
function drop_table($table_name, $no_prefix = false)
{
if (!$this->table_exists($table_name, $no_prefix))
return true;
return $this->query('DROP TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
}
function rename_table($old_table, $new_table, $no_prefix = false)
{
// If the new table exists and the old one doesn't, then we're happy
if ($this->table_exists($new_table, $no_prefix) && !$this->table_exists($old_table, $no_prefix))
return true;
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$old_table.' RENAME TO '.($no_prefix ? '' : $this->prefix).$new_table) ? true : false;
}
function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
{
if ($this->field_exists($table_name, $field_name, $no_prefix))
return true;
$field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
if (!is_null($default_value) && !is_int($default_value) && !is_float($default_value))
$default_value = '\''.$this->escape($default_value).'\'';
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ADD '.$field_name.' '.$field_type.($allow_null ? '' : ' NOT NULL').(!is_null($default_value) ? ' DEFAULT '.$default_value : '').(!is_null($after_field) ? ' AFTER '.$after_field : '')) ? true : false;
}
function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
{
if (!$this->field_exists($table_name, $field_name, $no_prefix))
return true;
$field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
if (!is_null($default_value) && !is_int($default_value) && !is_float($default_value))
$default_value = '\''.$this->escape($default_value).'\'';
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' MODIFY '.$field_name.' '.$field_type.($allow_null ? '' : ' NOT NULL').(!is_null($default_value) ? ' DEFAULT '.$default_value : '').(!is_null($after_field) ? ' AFTER '.$after_field : '')) ? true : false;
}
function drop_field($table_name, $field_name, $no_prefix = false)
{
if (!$this->field_exists($table_name, $field_name, $no_prefix))
return true;
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP '.$field_name) ? true : false;
}
function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false)
{
if ($this->index_exists($table_name, $index_name, $no_prefix))
return true;
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;
}
function drop_index($table_name, $index_name, $no_prefix = false)
{
if (!$this->index_exists($table_name, $index_name, $no_prefix))
return true;
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) ? true : false;
}
function truncate_table($table_name, $no_prefix = false)
{
return $this->query('TRUNCATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
}
}

View file

@ -1,442 +0,0 @@
<?php
/**
* Copyright (C) 2008-2012 FluxBB
* based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
*/
// Make sure we have built in support for PostgreSQL
if (!function_exists('pg_connect'))
exit('This PHP environment doesn\'t have PostgreSQL support built in. PostgreSQL support is required if you want to use a PostgreSQL database to run this forum. Consult the PHP documentation for further assistance.');
class DBLayer
{
var $prefix;
var $link_id;
var $query_result;
var $last_query_text = array();
var $in_transaction = 0;
var $saved_queries = array();
var $num_queries = 0;
var $error_no = false;
var $error_msg = 'Unknown';
var $datatype_transformations = array(
'%^(TINY|SMALL)INT( )?(\\([0-9]+\\))?( )?(UNSIGNED)?$%i' => 'SMALLINT',
'%^(MEDIUM)?INT( )?(\\([0-9]+\\))?( )?(UNSIGNED)?$%i' => 'INTEGER',
'%^BIGINT( )?(\\([0-9]+\\))?( )?(UNSIGNED)?$%i' => 'BIGINT',
'%^(TINY|MEDIUM|LONG)?TEXT$%i' => 'TEXT',
'%^DOUBLE( )?(\\([0-9,]+\\))?( )?(UNSIGNED)?$%i' => 'DOUBLE PRECISION',
'%^FLOAT( )?(\\([0-9]+\\))?( )?(UNSIGNED)?$%i' => 'REAL'
);
function __construct($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
{
$this->prefix = $db_prefix;
if ($db_host)
{
if (strpos($db_host, ':') !== false)
{
list($db_host, $dbport) = explode(':', $db_host);
$connect_str[] = 'host='.$db_host.' port='.$dbport;
}
else
$connect_str[] = 'host='.$db_host;
}
if ($db_name)
$connect_str[] = 'dbname='.$db_name;
if ($db_username)
$connect_str[] = 'user='.$db_username;
if ($db_password)
$connect_str[] = 'password='.$db_password;
if ($p_connect)
$this->link_id = @pg_pconnect(implode(' ', $connect_str));
else
$this->link_id = @pg_connect(implode(' ', $connect_str));
if (!$this->link_id)
error('Unable to connect to PostgreSQL server', __FILE__, __LINE__);
// Setup the client-server character set (UTF-8)
if (!defined('FORUM_NO_SET_NAMES'))
$this->set_names('utf8');
return $this->link_id;
}
function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
{
$this->__construct($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect);
}
function start_transaction()
{
++$this->in_transaction;
return (@pg_query($this->link_id, 'BEGIN')) ? true : false;
}
function end_transaction()
{
--$this->in_transaction;
if (@pg_query($this->link_id, 'COMMIT'))
return true;
else
{
@pg_query($this->link_id, 'ROLLBACK');
return false;
}
}
function query($sql, $unbuffered = false) // $unbuffered is ignored since there is no pgsql_unbuffered_query()
{
if (strrpos($sql, 'LIMIT') !== false)
$sql = preg_replace('%LIMIT ([0-9]+),([ 0-9]+)%', 'LIMIT \\2 OFFSET \\1', $sql);
if (defined('PUN_SHOW_QUERIES'))
$q_start = microtime(true);
@pg_send_query($this->link_id, $sql);
$this->query_result = @pg_get_result($this->link_id);
if (pg_result_status($this->query_result) != PGSQL_FATAL_ERROR)
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array($sql, sprintf('%.5f', microtime(true) - $q_start));
++$this->num_queries;
$this->last_query_text[intval($this->query_result)] = $sql;
return $this->query_result;
}
else
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array($sql, 0);
$this->error_no = false;
$this->error_msg = @pg_result_error($this->query_result);
if ($this->in_transaction)
@pg_query($this->link_id, 'ROLLBACK');
--$this->in_transaction;
return false;
}
}
function result($query_id = 0, $row = 0, $col = 0)
{
return ($query_id) ? @pg_fetch_result($query_id, $row, $col) : false;
}
function fetch_assoc($query_id = 0)
{
return ($query_id) ? @pg_fetch_assoc($query_id) : false;
}
function fetch_row($query_id = 0)
{
return ($query_id) ? @pg_fetch_row($query_id) : false;
}
function num_rows($query_id = 0)
{
return ($query_id) ? @pg_num_rows($query_id) : false;
}
function affected_rows()
{
return ($this->query_result) ? @pg_affected_rows($this->query_result) : false;
}
function insert_id()
{
$query_id = $this->query_result;
if ($query_id && $this->last_query_text[intval($query_id)] != '')
{
if (preg_match('%^INSERT INTO ([a-z0-9\_\-]+)%is', $this->last_query_text[intval($query_id)], $table_name))
{
// Hack (don't ask)
if (substr($table_name[1], -6) == 'groups')
$table_name[1] .= '_g';
$temp_q_id = @pg_query($this->link_id, 'SELECT currval(\''.$table_name[1].'_id_seq\')');
return ($temp_q_id) ? intval(@pg_fetch_result($temp_q_id, 0)) : false;
}
}
return false;
}
function get_num_queries()
{
return $this->num_queries;
}
function get_saved_queries()
{
return $this->saved_queries;
}
function free_result($query_id = false)
{
if (!$query_id)
$query_id = $this->query_result;
return ($query_id) ? @pg_free_result($query_id) : false;
}
function escape($str)
{
return is_array($str) ? '' : pg_escape_string($str);
}
function error()
{
$result['error_sql'] = @current(@end($this->saved_queries));
$result['error_no'] = $this->error_no;
$result['error_msg'] = $this->error_msg;
return $result;
}
function close()
{
if ($this->link_id)
{
if ($this->in_transaction)
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array('COMMIT', 0);
@pg_query($this->link_id, 'COMMIT');
}
if ($this->query_result)
@pg_free_result($this->query_result);
return @pg_close($this->link_id);
}
else
return false;
}
function get_names()
{
$result = $this->query('SHOW client_encoding');
return strtolower($this->result($result)); // MySQL returns lowercase so lets be consistent
}
function set_names($names)
{
return $this->query('SET NAMES \''.$this->escape($names).'\'');
}
function get_version()
{
$result = $this->query('SELECT VERSION()');
return array(
'name' => 'PostgreSQL',
'version' => preg_replace('%^[^0-9]+([^\s,-]+).*$%', '\\1', $this->result($result))
);
}
function table_exists($table_name, $no_prefix = false)
{
$result = $this->query('SELECT 1 FROM pg_class WHERE relname = \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\'');
return $this->num_rows($result) > 0;
}
function field_exists($table_name, $field_name, $no_prefix = false)
{
$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).'\'');
return $this->num_rows($result) > 0;
}
function index_exists($table_name, $index_name, $no_prefix = false)
{
$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).'\'');
return $this->num_rows($result) > 0;
}
function create_table($table_name, $schema, $no_prefix = false)
{
if ($this->table_exists($table_name, $no_prefix))
return true;
$query = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name." (\n";
// Go through every schema element and add it to the query
foreach ($schema['FIELDS'] as $field_name => $field_data)
{
$field_data['datatype'] = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_data['datatype']);
$query .= $field_name.' '.$field_data['datatype'];
// The SERIAL datatype is a special case where we don't need to say not null
if (!$field_data['allow_null'] && $field_data['datatype'] != 'SERIAL')
$query .= ' NOT NULL';
if (isset($field_data['default']))
$query .= ' DEFAULT '.$field_data['default'];
$query .= ",\n";
}
// If we have a primary key, add it
if (isset($schema['PRIMARY KEY']))
$query .= 'PRIMARY KEY ('.implode(',', $schema['PRIMARY KEY']).'),'."\n";
// Add unique keys
if (isset($schema['UNIQUE KEYS']))
{
foreach ($schema['UNIQUE KEYS'] as $key_name => $key_fields)
$query .= 'UNIQUE ('.implode(',', $key_fields).'),'."\n";
}
// We remove the last two characters (a newline and a comma) and add on the ending
$query = substr($query, 0, strlen($query) - 2)."\n".')';
$result = $this->query($query) ? true : false;
// Add indexes
if (isset($schema['INDEXES']))
{
foreach ($schema['INDEXES'] as $index_name => $index_fields)
$result &= $this->add_index($table_name, $index_name, $index_fields, false, $no_prefix);
}
return $result;
}
function drop_table($table_name, $no_prefix = false)
{
if (!$this->table_exists($table_name, $no_prefix))
return true;
return $this->query('DROP TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
}
function rename_table($old_table, $new_table, $no_prefix = false)
{
// If the new table exists and the old one doesn't, then we're happy
if ($this->table_exists($new_table, $no_prefix) && !$this->table_exists($old_table, $no_prefix))
return true;
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$old_table.' RENAME TO '.($no_prefix ? '' : $this->prefix).$new_table) ? true : false;
}
function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
{
if ($this->field_exists($table_name, $field_name, $no_prefix))
return true;
$field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
$result = $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ADD '.$field_name.' '.$field_type) ? true : false;
if (!is_null($default_value))
{
if (!is_int($default_value) && !is_float($default_value))
$default_value = '\''.$this->escape($default_value).'\'';
$result &= $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ALTER '.$field_name.' SET DEFAULT '.$default_value) ? true : false;
$result &= $this->query('UPDATE '.($no_prefix ? '' : $this->prefix).$table_name.' SET '.$field_name.'='.$default_value) ? true : false;
}
if (!$allow_null)
$result &= $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ALTER '.$field_name.' SET NOT NULL') ? true : false;
return $result;
}
function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
{
if (!$this->field_exists($table_name, $field_name, $no_prefix))
return true;
$field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
$result = $this->add_field($table_name, 'tmp_'.$field_name, $field_type, $allow_null, $default_value, $after_field, $no_prefix);
$result &= $this->query('UPDATE '.($no_prefix ? '' : $this->prefix).$table_name.' SET tmp_'.$field_name.' = '.$field_name) ? true : false;
$result &= $this->drop_field($table_name, $field_name, $no_prefix);
$result &= $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' RENAME COLUMN tmp_'.$field_name.' TO '.$field_name) ? true : false;
return $result;
}
function drop_field($table_name, $field_name, $no_prefix = false)
{
if (!$this->field_exists($table_name, $field_name, $no_prefix))
return true;
return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP '.$field_name) ? true : false;
}
function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false)
{
if ($this->index_exists($table_name, $index_name, $no_prefix))
return true;
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;
}
function drop_index($table_name, $index_name, $no_prefix = false)
{
if (!$this->index_exists($table_name, $index_name, $no_prefix))
return true;
return $this->query('DROP INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) ? true : false;
}
function truncate_table($table_name, $no_prefix = false)
{
return $this->query('DELETE FROM '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
}
}

View file

@ -1,601 +0,0 @@
<?php
/**
* Copyright (C) 2008-2012 FluxBB
* based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
*/
// Make sure we have built in support for SQLite
if (!function_exists('sqlite_open'))
exit('This PHP environment doesn\'t have SQLite support built in. SQLite support is required if you want to use a SQLite database to run this forum. Consult the PHP documentation for further assistance.');
class DBLayer
{
var $prefix;
var $link_id;
var $query_result;
var $in_transaction = 0;
var $saved_queries = array();
var $num_queries = 0;
var $error_no = false;
var $error_msg = 'Unknown';
var $datatype_transformations = array(
'%^SERIAL$%' => 'INTEGER',
'%^(TINY|SMALL|MEDIUM|BIG)?INT( )?(\\([0-9]+\\))?( )?(UNSIGNED)?$%i' => 'INTEGER',
'%^(TINY|MEDIUM|LONG)?TEXT$%i' => 'TEXT'
);
function __construct($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
{
// Prepend $db_name with the path to the forum root directory
$db_name = PUN_ROOT.$db_name;
$this->prefix = $db_prefix;
if (!file_exists($db_name))
{
@touch($db_name);
@chmod($db_name, 0666);
if (!file_exists($db_name))
error('Unable to create new database \''.$db_name.'\'. Permission denied', __FILE__, __LINE__);
}
if (!is_readable($db_name))
error('Unable to open database \''.$db_name.'\' for reading. Permission denied', __FILE__, __LINE__);
if (!forum_is_writable($db_name))
error('Unable to open database \''.$db_name.'\' for writing. Permission denied', __FILE__, __LINE__);
if ($p_connect)
$this->link_id = @sqlite_popen($db_name, 0666, $sqlite_error);
else
$this->link_id = @sqlite_open($db_name, 0666, $sqlite_error);
if (!$this->link_id)
error('Unable to open database \''.$db_name.'\'. SQLite reported: '.$sqlite_error, __FILE__, __LINE__);
else
return $this->link_id;
}
function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
{
$this->__construct($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect);
}
function start_transaction()
{
++$this->in_transaction;
return (@sqlite_query($this->link_id, 'BEGIN')) ? true : false;
}
function end_transaction()
{
--$this->in_transaction;
if (@sqlite_query($this->link_id, 'COMMIT'))
return true;
else
{
@sqlite_query($this->link_id, 'ROLLBACK');
return false;
}
}
function query($sql, $unbuffered = false)
{
if (defined('PUN_SHOW_QUERIES'))
$q_start = microtime(true);
if ($unbuffered)
$this->query_result = @sqlite_unbuffered_query($this->link_id, $sql);
else
$this->query_result = @sqlite_query($this->link_id, $sql);
if ($this->query_result)
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array($sql, sprintf('%.5f', microtime(true) - $q_start));
++$this->num_queries;
return $this->query_result;
}
else
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array($sql, 0);
$this->error_no = @sqlite_last_error($this->link_id);
$this->error_msg = @sqlite_error_string($this->error_no);
if ($this->in_transaction)
@sqlite_query($this->link_id, 'ROLLBACK');
--$this->in_transaction;
return false;
}
}
function result($query_id = 0, $row = 0, $col = 0)
{
if ($query_id)
{
if ($row !== 0 && @sqlite_seek($query_id, $row) === false)
return false;
$cur_row = @sqlite_current($query_id);
if ($cur_row === false)
return false;
return $cur_row[$col];
}
else
return false;
}
function fetch_assoc($query_id = 0)
{
if ($query_id)
{
$cur_row = @sqlite_fetch_array($query_id, SQLITE_ASSOC);
if ($cur_row)
{
// Horrible hack to get rid of table names and table aliases from the array keys
foreach ($cur_row as $key => $value)
{
$dot_spot = strpos($key, '.');
if ($dot_spot !== false)
{
unset($cur_row[$key]);
$key = substr($key, $dot_spot+1);
$cur_row[$key] = $value;
}
}
}
return $cur_row;
}
else
return false;
}
function fetch_row($query_id = 0)
{
return ($query_id) ? @sqlite_fetch_array($query_id, SQLITE_NUM) : false;
}
function num_rows($query_id = 0)
{
return ($query_id) ? @sqlite_num_rows($query_id) : false;
}
function affected_rows()
{
return ($this->link_id) ? @sqlite_changes($this->link_id) : false;
}
function insert_id()
{
return ($this->link_id) ? @sqlite_last_insert_rowid($this->link_id) : false;
}
function get_num_queries()
{
return $this->num_queries;
}
function get_saved_queries()
{
return $this->saved_queries;
}
function free_result($query_id = false)
{
return true;
}
function escape($str)
{
return is_array($str) ? '' : sqlite_escape_string($str);
}
function error()
{
$result['error_sql'] = @current(@end($this->saved_queries));
$result['error_no'] = $this->error_no;
$result['error_msg'] = $this->error_msg;
return $result;
}
function close()
{
if ($this->link_id)
{
if ($this->in_transaction)
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array('COMMIT', 0);
@sqlite_query($this->link_id, 'COMMIT');
}
return @sqlite_close($this->link_id);
}
else
return false;
}
function get_names()
{
return '';
}
function set_names($names)
{
return true;
}
function get_version()
{
return array(
'name' => 'SQLite',
'version' => sqlite_libversion()
);
}
function table_exists($table_name, $no_prefix = false)
{
$result = $this->query('SELECT 1 FROM sqlite_master WHERE name = \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\' AND type=\'table\'');
return $this->num_rows($result) > 0;
}
function field_exists($table_name, $field_name, $no_prefix = false)
{
$result = $this->query('SELECT sql FROM sqlite_master WHERE name = \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\' AND type=\'table\'');
if (!$this->num_rows($result))
return false;
return preg_match('%[\r\n]'.preg_quote($field_name, '%').' %', $this->result($result));
}
function index_exists($table_name, $index_name, $no_prefix = false)
{
$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\'');
return $this->num_rows($result) > 0;
}
function create_table($table_name, $schema, $no_prefix = false)
{
if ($this->table_exists($table_name, $no_prefix))
return true;
$query = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name." (\n";
// Go through every schema element and add it to the query
foreach ($schema['FIELDS'] as $field_name => $field_data)
{
$field_data['datatype'] = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_data['datatype']);
$query .= $field_name.' '.$field_data['datatype'];
if (!$field_data['allow_null'])
$query .= ' NOT NULL';
if (isset($field_data['default']))
$query .= ' DEFAULT '.$field_data['default'];
$query .= ",\n";
}
// If we have a primary key, add it
if (isset($schema['PRIMARY KEY']))
$query .= 'PRIMARY KEY ('.implode(',', $schema['PRIMARY KEY']).'),'."\n";
// Add unique keys
if (isset($schema['UNIQUE KEYS']))
{
foreach ($schema['UNIQUE KEYS'] as $key_name => $key_fields)
$query .= 'UNIQUE ('.implode(',', $key_fields).'),'."\n";
}
// We remove the last two characters (a newline and a comma) and add on the ending
$query = substr($query, 0, strlen($query) - 2)."\n".')';
$result = $this->query($query) ? true : false;
// Add indexes
if (isset($schema['INDEXES']))
{
foreach ($schema['INDEXES'] as $index_name => $index_fields)
$result &= $this->add_index($table_name, $index_name, $index_fields, false, $no_prefix);
}
return $result;
}
function drop_table($table_name, $no_prefix = false)
{
if (!$this->table_exists($table_name, $no_prefix))
return true;
return $this->query('DROP TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($table_name)) ? true : false;
}
function rename_table($old_table, $new_table, $no_prefix = false)
{
// If the old table does not exist
if (!$this->table_exists($old_table, $no_prefix))
return false;
// If the table names are the same
else if ($old_table == $new_table)
return true;
// If the new table already exists
else if ($this->table_exists($new_table, $no_prefix))
return false;
$table = $this->get_table_info($old_table, $no_prefix);
// Create new table
$query = str_replace('CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($old_table).' (', 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($new_table).' (', $table['sql']);
$result = $this->query($query) ? true : false;
// Recreate indexes
if (!empty($table['indices']))
{
foreach ($table['indices'] as $cur_index)
{
$query = str_replace('CREATE INDEX '.($no_prefix ? '' : $this->prefix).$this->escape($old_table), 'CREATE INDEX '.($no_prefix ? '' : $this->prefix).$this->escape($new_table), $cur_index);
$query = str_replace('ON '.($no_prefix ? '' : $this->prefix).$this->escape($old_table), 'ON '.($no_prefix ? '' : $this->prefix).$this->escape($new_table), $query);
$result &= $this->query($query) ? true : false;
}
}
// Copy content across
$result &= $this->query('INSERT INTO '.($no_prefix ? '' : $this->prefix).$this->escape($new_table).' SELECT * FROM '.($no_prefix ? '' : $this->prefix).$this->escape($old_table)) ? true : false;
// Drop the old table if the new one exists
if ($this->table_exists($new_table, $no_prefix))
$result &= $this->drop_table($old_table, $no_prefix);
return $result;
}
function get_table_info($table_name, $no_prefix = false)
{
// Grab table info
$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());
$num_rows = $this->num_rows($result);
if ($num_rows == 0)
return;
$table = array();
$table['indices'] = array();
while ($cur_index = $this->fetch_assoc($result))
{
if (empty($cur_index['sql']))
continue;
if (!isset($table['sql']))
$table['sql'] = $cur_index['sql'];
else
$table['indices'][] = $cur_index['sql'];
}
// Work out the columns in the table currently
$table_lines = explode("\n", $table['sql']);
$table['columns'] = array();
foreach ($table_lines as $table_line)
{
$table_line = trim($table_line, " \t\n\r,"); // trim spaces, tabs, newlines, and commas
if (substr($table_line, 0, 12) == 'CREATE TABLE')
continue;
else if (substr($table_line, 0, 11) == 'PRIMARY KEY')
$table['primary_key'] = $table_line;
else if (substr($table_line, 0, 6) == 'UNIQUE')
$table['unique'] = $table_line;
else if (substr($table_line, 0, strpos($table_line, ' ')) != '')
$table['columns'][substr($table_line, 0, strpos($table_line, ' '))] = trim(substr($table_line, strpos($table_line, ' ')));
}
return $table;
}
function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
{
if ($this->field_exists($table_name, $field_name, $no_prefix))
return true;
$table = $this->get_table_info($table_name, $no_prefix);
// Create temp table
$now = time();
$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']);
$result = $this->query($tmptable) ? true : false;
$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;
// Create new table sql
$field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
$query = $field_type;
if (!$allow_null)
$query .= ' NOT NULL';
if (is_string($default_value))
$default_value = '\''.$this->escape($default_value).'\'';
if (!is_null($default_value))
$query .= ' DEFAULT '.$default_value;
$old_columns = array_keys($table['columns']);
// Determine the proper offset
if (!is_null($after_field))
$offset = array_search($after_field, array_keys($table['columns']), true) + 1;
else
$offset = count($table['columns']);
// Out of bounds checks
if ($offset > count($table['columns']))
$offset = count($table['columns']);
else if ($offset < 0)
$offset = 0;
if (!is_null($field_name) && $field_name !== '')
$table['columns'] = array_merge(array_slice($table['columns'], 0, $offset), array($field_name => $query), array_slice($table['columns'], $offset));
$new_table = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).' (';
foreach ($table['columns'] as $cur_column => $column_details)
$new_table .= "\n".$cur_column.' '.$column_details.',';
if (isset($table['unique']))
$new_table .= "\n".$table['unique'].',';
if (isset($table['primary_key']))
$new_table .= "\n".$table['primary_key'].',';
$new_table = trim($new_table, ',')."\n".');';
// Drop old table
$result &= $this->drop_table($table_name, $no_prefix);
// Create new table
$result &= $this->query($new_table) ? true : false;
// Recreate indexes
if (!empty($table['indices']))
{
foreach ($table['indices'] as $cur_index)
$result &= $this->query($cur_index) ? true : false;
}
// Copy content back
$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;
// Drop temp table
$result &= $this->drop_table($table_name.'_t'.$now, $no_prefix);
return $result;
}
function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
{
// Unneeded for SQLite
return true;
}
function drop_field($table_name, $field_name, $no_prefix = false)
{
if (!$this->field_exists($table_name, $field_name, $no_prefix))
return true;
$table = $this->get_table_info($table_name, $no_prefix);
// Create temp table
$now = time();
$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']);
$result = $this->query($tmptable) ? true : false;
$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;
// Work out the columns we need to keep and the sql for the new table
unset($table['columns'][$field_name]);
$new_columns = array_keys($table['columns']);
$new_table = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).' (';
foreach ($table['columns'] as $cur_column => $column_details)
$new_table .= "\n".$cur_column.' '.$column_details.',';
if (isset($table['unique']))
$new_table .= "\n".$table['unique'].',';
if (isset($table['primary_key']))
$new_table .= "\n".$table['primary_key'].',';
$new_table = trim($new_table, ',')."\n".');';
// Drop old table
$result &= $this->drop_table($table_name, $no_prefix);
// Create new table
$result &= $this->query($new_table) ? true : false;
// Recreate indexes
if (!empty($table['indices']))
{
foreach ($table['indices'] as $cur_index)
if (!preg_match('%\('.preg_quote($field_name, '%').'\)%', $cur_index))
$result &= $this->query($cur_index) ? true : false;
}
// Copy content back
$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;
// Drop temp table
$result &= $this->drop_table($table_name.'_t'.$now, $no_prefix);
return $result;
}
function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false)
{
if ($this->index_exists($table_name, $index_name, $no_prefix))
return true;
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;
}
function drop_index($table_name, $index_name, $no_prefix = false)
{
if (!$this->index_exists($table_name, $index_name, $no_prefix))
return true;
return $this->query('DROP INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) ? true : false;
}
function truncate_table($table_name, $no_prefix = false)
{
return $this->query('DELETE FROM '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
}
}