Zeile 8 | Zeile 8 |
---|
* */
|
* */
|
class DB_PgSQL
| class DB_PgSQL implements DB_Base
|
{ /** * The title of this layer.
| { /** * The title of this layer.
|
Zeile 23 | Zeile 23 |
---|
* @var string */ public $short_title = "PostgreSQL";
|
* @var string */ public $short_title = "PostgreSQL";
|
| /** * The type of db software being used. * * @var string */ public $type;
|
/** * A count of the number of queries.
| /** * A count of the number of queries.
|
Zeile 44 | Zeile 51 |
---|
* @var boolean */ public $error_reporting = 1;
|
* @var boolean */ public $error_reporting = 1;
|
|
|
/** * The read database connection resource.
|
/** * The read database connection resource.
|
* * @var resource
| * * @var resource
|
*/ public $read_link;
|
*/ public $read_link;
|
/**
| /**
|
* The write database connection resource
|
* The write database connection resource
|
* * @var resource */
| * * @var resource */
|
public $write_link;
/** * Reference to the last database connection resource used.
|
public $write_link;
/** * Reference to the last database connection resource used.
|
* * @var resource */
| * * @var resource */
|
public $current_link;
|
public $current_link;
|
/**
| /** * @var array */ public $connections = array();
/**
|
* Explanation of a query.
|
* Explanation of a query.
|
*
| *
|
* @var string */ public $explain;
|
* @var string */ public $explain;
|
/**
| /**
|
* The current version of PgSQL. * * @var string
| * The current version of PgSQL. * * @var string
|
Zeile 82 | Zeile 94 |
---|
/** * The current table type in use (myisam/innodb)
|
/** * The current table type in use (myisam/innodb)
|
* * @var string
| * * @var string
|
*/ public $table_type = "myisam";
| */ public $table_type = "myisam";
|
Zeile 121 | Zeile 133 |
---|
* @var string */ public $engine = "pgsql";
|
* @var string */ public $engine = "pgsql";
|
|
|
/** * Weather or not this engine can use the search functionality *
| /** * Weather or not this engine can use the search functionality *
|
Zeile 153 | Zeile 165 |
---|
/** * Connect to the database server. *
|
/** * Connect to the database server. *
|
* @param array Array of DBMS connection details.
| * @param array $config Array of DBMS connection details.
|
* @return resource The DB connection resource. Returns false on failure */ function connect($config)
| * @return resource The DB connection resource. Returns false on failure */ function connect($config)
|
Zeile 181 | Zeile 193 |
---|
} }
|
} }
|
$this->db_encoding = $config['encoding'];
| if(isset($config['encoding'])) { $this->db_encoding = $config['encoding']; }
|
// Actually connect to the specified servers foreach(array('read', 'write') as $type)
| // Actually connect to the specified servers foreach(array('read', 'write') as $type)
|
Zeile 194 | Zeile 209 |
---|
if(array_key_exists('hostname', $connections[$type])) { $details = $connections[$type];
|
if(array_key_exists('hostname', $connections[$type])) { $details = $connections[$type];
|
unset($connections);
| unset($connections[$type]);
|
$connections[$type][] = $details; }
| $connections[$type][] = $details; }
|
Zeile 219 | Zeile 234 |
---|
if(strpos($single_connection['hostname'], ':') !== false) { list($single_connection['hostname'], $single_connection['port']) = explode(':', $single_connection['hostname']);
|
if(strpos($single_connection['hostname'], ':') !== false) { list($single_connection['hostname'], $single_connection['port']) = explode(':', $single_connection['hostname']);
|
| } else { $single_connection['port'] = null;
|
}
if($single_connection['port'])
| }
if($single_connection['port'])
|
Zeile 250 | Zeile 269 |
---|
{ $this->connections[] = "<span style=\"color: red\">[FAILED] [".strtoupper($type)."] {$single_connection['username']}@{$single_connection['hostname']}</span>"; }
|
{ $this->connections[] = "<span style=\"color: red\">[FAILED] [".strtoupper($type)."] {$single_connection['username']}@{$single_connection['hostname']}</span>"; }
|
}
| }
|
}
// No write server was specified (simple connection or just multiple servers) - mirror write link
| }
// No write server was specified (simple connection or just multiple servers) - mirror write link
|
Zeile 278 | Zeile 297 |
---|
/** * Query the database.
|
/** * Query the database.
|
* * @param string The query SQL. * @param boolean 1 if hide errors, 0 if not. * @param integer 1 if executes on slave database, 0 if not. * @return resource The query data. */
| * * @param string $string The query SQL. * @param boolean|int $hide_errors 1 if hide errors, 0 if not. * @param integer $write_query 1 if executes on slave database, 0 if not. * @return resource The query data. */
|
function query($string, $hide_errors=0, $write_query=0) {
|
function query($string, $hide_errors=0, $write_query=0) {
|
global $pagestarttime, $db, $mybb;
$string = preg_replace("#LIMIT (\s*)([0-9]+),(\s*)([0-9]+)$#im", "LIMIT $4 OFFSET $2", trim($string));
$this->last_query = $string;
| global $mybb;
$string = preg_replace("#LIMIT (\s*)([0-9]+),(\s*)([0-9]+);?$#im", "LIMIT $4 OFFSET $2", trim($string));
$this->last_query = $string;
|
get_execution_time();
| get_execution_time();
|
Zeile 301 | Zeile 320 |
---|
{ $string = str_replace(' CHANGE ', ' ALTER ', $string); }
|
{ $string = str_replace(' CHANGE ', ' ALTER ', $string); }
|
}
| }
|
if($write_query && $this->write_link) { while(pg_connection_busy($this->write_link)); $this->current_link = &$this->write_link;
|
if($write_query && $this->write_link) { while(pg_connection_busy($this->write_link)); $this->current_link = &$this->write_link;
|
pg_send_query($this->current_link, $string);
| pg_send_query($this->current_link, $string);
|
$query = pg_get_result($this->current_link); } else
| $query = pg_get_result($this->current_link); } else
|
Zeile 316 | Zeile 335 |
---|
$this->current_link = &$this->read_link; pg_send_query($this->current_link, $string); $query = pg_get_result($this->current_link);
|
$this->current_link = &$this->read_link; pg_send_query($this->current_link, $string); $query = pg_get_result($this->current_link);
|
}
| }
|
if((pg_result_error($query) && !$hide_errors)) {
| if((pg_result_error($query) && !$hide_errors)) {
|
Zeile 339 | Zeile 358 |
---|
/** * Execute a write query on the slave database *
|
/** * Execute a write query on the slave database *
|
* @param string The query SQL. * @param boolean 1 if hide errors, 0 if not.
| * @param string $query The query SQL. * @param boolean|int $hide_errors 1 if hide errors, 0 if not.
|
* @return resource The query data. */ function write_query($query, $hide_errors=0) { return $this->query($query, $hide_errors, 1);
|
* @return resource The query data. */ function write_query($query, $hide_errors=0) { return $this->query($query, $hide_errors, 1);
|
}
/**
| }
/**
|
* Explain a query on the database. *
|
* Explain a query on the database. *
|
* @param string The query SQL. * @param string The time it took to perform the query.
| * @param string $string The query SQL. * @param string $qtime The time it took to perform the query.
|
*/ function explain_query($string, $qtime) {
| */ function explain_query($string, $qtime) {
|
Zeile 407 | Zeile 426 |
---|
/** * Return a result array for a query. *
|
/** * Return a result array for a query. *
|
* @param resource The query ID. * @param constant The type of array to return. * @return array The array of results.
| * @param resource $query The query ID. * @param int $resulttype The type of array to return. Either PGSQL_NUM, PGSQL_BOTH or PGSQL_ASSOC * @return array The array of results. Note that all fields are returned as string: http://php.net/manual/en/function.pg-fetch-array.php
|
*/ function fetch_array($query, $resulttype=PGSQL_ASSOC)
|
*/ function fetch_array($query, $resulttype=PGSQL_ASSOC)
|
{
| {
|
switch($resulttype) { case PGSQL_NUM:
| switch($resulttype) { case PGSQL_NUM:
|
Zeile 431 | Zeile 450 |
---|
/** * Return a specific field from a query. *
|
/** * Return a specific field from a query. *
|
* @param resource The query ID. * @param string The name of the field to return. * @param int The number of the row to fetch it from.
| * @param resource $query The query ID. * @param string $field The name of the field to return. * @param int|bool The number of the row to fetch it from. * @return string|bool|null As per http://php.net/manual/en/function.pg-fetch-result.php
|
*/ function fetch_field($query, $field, $row=false) { if($row === false) { $array = $this->fetch_array($query);
|
*/ function fetch_field($query, $field, $row=false) { if($row === false) { $array = $this->fetch_array($query);
|
return $array[$field];
| if($array !== null && $array !== false) { return $array[$field]; } return null;
|
}
|
}
|
else { return pg_fetch_result($query, $row, $field); } }
/**
| return pg_fetch_result($query, $row, $field); }
/**
|
* Moves internal row pointer to the next row *
|
* Moves internal row pointer to the next row *
|
* @param resource The query ID. * @param int The pointer to move the row to.
| * @param resource $query The query ID. * @param int $row The pointer to move the row to. * @return bool
|
*/ function data_seek($query, $row) { return pg_result_seek($query, $row);
|
*/ function data_seek($query, $row) { return pg_result_seek($query, $row);
|
}
/**
| }
/**
|
* Return the number of rows resulting from a query.
|
* Return the number of rows resulting from a query.
|
* * @param resource The query ID.
| * * @param resource $query The query ID.
|
* @return int The number of rows in the result.
|
* @return int The number of rows in the result.
|
*/
| */
|
function num_rows($query)
|
function num_rows($query)
|
{
| {
|
return pg_num_rows($query); }
|
return pg_num_rows($query); }
|
|
|
/** * Return the last id number of inserted data. * * @return int The id number. */ function insert_id()
|
/** * Return the last id number of inserted data. * * @return int The id number. */ function insert_id()
|
{ $this->last_query = str_replace(array("\r", "\t"), '', $this->last_query); $this->last_query = str_replace("\n", ' ', $this->last_query); preg_match('#INSERT INTO ([a-zA-Z0-9_\-]+)#i', $this->last_query, $matches);
$table = $matches[1];
| { preg_match('#INSERT\s+INTO\s+([a-zA-Z0-9_\-]+)#i', $this->last_query, $matches);
$table = $matches[1];
|
$query = $this->query("SELECT column_name FROM information_schema.constraint_column_usage WHERE table_name = '{$table}' and constraint_name = '{$table}_pkey' LIMIT 1"); $field = $this->fetch_field($query, 'column_name');
// Do we not have a primary field? if(!$field) {
|
$query = $this->query("SELECT column_name FROM information_schema.constraint_column_usage WHERE table_name = '{$table}' and constraint_name = '{$table}_pkey' LIMIT 1"); $field = $this->fetch_field($query, 'column_name');
// Do we not have a primary field? if(!$field) {
|
return;
| return 0;
|
}
$id = $this->write_query("SELECT currval(pg_get_serial_sequence('{$table}', '{$field}')) AS last_value");
| }
$id = $this->write_query("SELECT currval(pg_get_serial_sequence('{$table}', '{$field}')) AS last_value");
|
Zeile 512 | Zeile 533 |
---|
/** * Return an error number. *
|
/** * Return an error number. *
|
| * @param resource $query
|
* @return int The error number of the current error. */
|
* @return int The error number of the current error. */
|
function error_number($query="")
| function error_number($query=null)
|
{
|
{
|
if(!$query || !function_exists("pg_result_error_field"))
| if($query != null || !function_exists("pg_result_error_field"))
|
{ return 0; }
return pg_result_error_field($query, PGSQL_DIAG_SQLSTATE);
|
{ return 0; }
return pg_result_error_field($query, PGSQL_DIAG_SQLSTATE);
|
}
| }
|
/** * Return an error string. *
|
/** * Return an error string. *
|
| * @param resource $query
|
* @return string The explanation for the current error. */
|
* @return string The explanation for the current error. */
|
function error_string($query="")
| function error_string($query=null)
|
{
|
{
|
if($query)
| if($query != null)
|
{ return pg_result_error($query);
|
{ return pg_result_error($query);
|
}
| }
|
if($this->current_link) { return pg_last_error($this->current_link);
| if($this->current_link) { return pg_last_error($this->current_link);
|
Zeile 543 | Zeile 566 |
---|
else { return pg_last_error();
|
else { return pg_last_error();
|
} }
| } }
|
/** * Output a database error. *
|
/** * Output a database error. *
|
* @param string The string to present as an error.
| * @param string $string The string to present as an error. * @param resource $query
|
*/
|
*/
|
function error($string="", $query="")
| function error($string="", $query=null)
|
{ if($this->error_reporting) {
| { if($this->error_reporting) {
|
Zeile 592 | Zeile 616 |
---|
/** * Return the number of fields. *
|
/** * Return the number of fields. *
|
* @param resource The query ID.
| * @param resource $query The query ID.
|
* @return int The number of fields. */ function num_fields($query)
| * @return int The number of fields. */ function num_fields($query)
|
Zeile 601 | Zeile 625 |
---|
}
/**
|
}
/**
|
* Lists all functions in the database.
| * Lists all tables in the database.
|
*
|
*
|
* @param string The database name. * @param string Prefix of the table (optional)
| * @param string $database The database name. * @param string $prefix Prefix of the table (optional)
|
* @return array The table list. */ function list_tables($database, $prefix='') { if($prefix)
|
* @return array The table list. */ function list_tables($database, $prefix='') { if($prefix)
|
{
| {
|
$query = $this->query("SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_name LIKE '".$this->escape_string($prefix)."%'");
|
$query = $this->query("SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_name LIKE '".$this->escape_string($prefix)."%'");
|
}
| }
|
else
|
else
|
{
| {
|
$query = $this->query("SELECT table_name FROM information_schema.tables WHERE table_schema='public'");
|
$query = $this->query("SELECT table_name FROM information_schema.tables WHERE table_schema='public'");
|
}
| }
|
$tables = array(); while($table = $this->fetch_array($query)) {
| $tables = array(); while($table = $this->fetch_array($query)) {
|
Zeile 629 | Zeile 653 |
---|
/** * Check if a table exists in a database.
|
/** * Check if a table exists in a database.
|
* * @param string The table name. * @return boolean True when exists, false if not.
| * * @param string $table The table name. * @return boolean True when exists, false if not.
|
*/ function table_exists($table) { // Execute on master server to ensure if we've just created a table that we get the correct result $query = $this->write_query("SELECT COUNT(table_name) as table_names FROM information_schema.tables WHERE table_schema = 'public' AND table_name='{$this->table_prefix}{$table}'");
|
*/ function table_exists($table) { // Execute on master server to ensure if we've just created a table that we get the correct result $query = $this->write_query("SELECT COUNT(table_name) as table_names FROM information_schema.tables WHERE table_schema = 'public' AND table_name='{$this->table_prefix}{$table}'");
|
|
|
$exists = $this->fetch_field($query, 'table_names');
|
$exists = $this->fetch_field($query, 'table_names');
|
if($exists > 0) { return true; } else { return false; } }
/**
| if($exists > 0) { return true; } else { return false; } }
/**
|
* Check if a field exists in a database. *
|
* Check if a field exists in a database. *
|
* @param string The field name. * @param string The table name.
| * @param string $field The field name. * @param string $table The table name.
|
* @return boolean True when exists, false if not. */ function field_exists($field, $table) { $query = $this->write_query("SELECT COUNT(column_name) as column_names FROM information_schema.columns WHERE table_name='{$this->table_prefix}{$table}' AND column_name='{$field}'");
|
* @return boolean True when exists, false if not. */ function field_exists($field, $table) { $query = $this->write_query("SELECT COUNT(column_name) as column_names FROM information_schema.columns WHERE table_name='{$this->table_prefix}{$table}' AND column_name='{$field}'");
|
|
|
$exists = $this->fetch_field($query, "column_names");
if($exists > 0)
| $exists = $this->fetch_field($query, "column_names");
if($exists > 0)
|
Zeile 670 | Zeile 694 |
---|
else { return false;
|
else { return false;
|
} }
| } }
|
/** * Add a shutdown query. *
|
/** * Add a shutdown query. *
|
* @param resource The query data. * @param string An optional name for the query.
| * @param resource $query The query data. * @param string $name An optional name for the query.
|
*/
|
*/
|
function shutdown_query($query, $name=0)
| function shutdown_query($query, $name="")
|
{ global $shutdown_queries; if($name)
|
{ global $shutdown_queries; if($name)
|
{
| {
|
$shutdown_queries[$name] = $query;
|
$shutdown_queries[$name] = $query;
|
}
| }
|
else { $shutdown_queries[] = $query;
| else { $shutdown_queries[] = $query;
|
Zeile 694 | Zeile 718 |
---|
/** * Performs a simple select query.
|
/** * Performs a simple select query.
|
* * @param string The table name to be queried. * @param string Comma delimetered list of fields to be selected. * @param string SQL formatted list of conditions to be matched. * @param array List of options: group by, order by, order direction, limit, limit start.
| * * @param string $table The table name to be queried. * @param string $fields Comma delimetered list of fields to be selected. * @param string $conditions SQL formatted list of conditions to be matched. * @param array $options List of options: group by, order by, order direction, limit, limit start.
|
* @return resource The query data. */ function simple_select($table, $fields="*", $conditions="", $options=array())
| * @return resource The query data. */ function simple_select($table, $fields="*", $conditions="", $options=array())
|
Zeile 738 | Zeile 762 |
---|
/** * Build an insert query from an array. *
|
/** * Build an insert query from an array. *
|
* @param string The table name to perform the query on. * @param array An array of fields and their values. * @param boolean Whether or not to return an insert id. True by default * @return int The insert ID if available
| * @param string $table The table name to perform the query on. * @param array $array An array of fields and their values. * @param boolean $insert_id Whether or not to return an insert id. True by default * @return int|bool The insert ID if available. False on failure and true if $insert_id is false
|
*/ function insert_query($table, $array, $insert_id=true) {
| */ function insert_query($table, $array, $insert_id=true) {
|
Zeile 758 | Zeile 782 |
---|
{ $array[$field] = $value; }
|
{ $array[$field] = $value; }
|
else { $array[$field] = "'{$value}'"; } }
| else { $array[$field] = $this->quote_val($value); } }
|
$fields = implode(",", array_keys($array)); $values = implode(",", $array);
| $fields = implode(",", array_keys($array)); $values = implode(",", $array);
|
Zeile 775 | Zeile 799 |
---|
if($insert_id != false) { return $this->insert_id();
|
if($insert_id != false) { return $this->insert_id();
|
} else { return true; }
| } else { return true; }
|
}
|
}
|
|
|
/** * Build one query for multiple inserts from a multidimensional array. *
|
/** * Build one query for multiple inserts from a multidimensional array. *
|
* @param string The table name to perform the query on. * @param array An array of inserts. * @return int The insert ID if available
| * @param string $table The table name to perform the query on. * @param array $array An array of inserts. * @return void
|
*/ function insert_query_multiple($table, $array) {
| */ function insert_query_multiple($table, $array) {
|
Zeile 795 | Zeile 819 |
---|
if(!is_array($array)) {
|
if(!is_array($array)) {
|
return false;
| return;
|
} // Field names $fields = array_keys($array[0]);
| } // Field names $fields = array_keys($array[0]);
|
Zeile 812 | Zeile 836 |
---|
} else {
|
} else {
|
$values[$field] = "'{$value}'";
| $values[$field] = $this->quote_val($value);
|
} } $insert_rows[] = "(".implode(",", $values).")";
| } } $insert_rows[] = "(".implode(",", $values).")";
|
Zeile 829 | Zeile 853 |
---|
/** * Build an update query from an array. *
|
/** * Build an update query from an array. *
|
* @param string The table name to perform the query on. * @param array An array of fields and their values. * @param string An optional where clause for the query. * @param string An optional limit clause for the query. * @param boolean An option to quote incoming values of the array.
| * @param string $table The table name to perform the query on. * @param array $array An array of fields and their values. * @param string $where An optional where clause for the query. * @param string $limit An optional limit clause for the query. * @param boolean $no_quote An option to quote incoming values of the array.
|
* @return resource The query data. */ function update_query($table, $array, $where="", $limit="", $no_quote=false)
| * @return resource The query data. */ function update_query($table, $array, $where="", $limit="", $no_quote=false)
|
Zeile 862 | Zeile 886 |
---|
} else {
|
} else {
|
$query .= $comma.$field."={$quote}{$value}{$quote}";
| $quoted_value = $this->quote_val($value, $quote);
$query .= $comma.$field."={$quoted_value}";
|
} $comma = ', ';
|
} $comma = ', ';
|
} if(!empty($where)) { $query .= " WHERE $where"; } return $this->write_query("
| } if(!empty($where)) { $query .= " WHERE $where"; } return $this->write_query("
|
UPDATE {$this->table_prefix}$table SET $query ");
|
UPDATE {$this->table_prefix}$table SET $query ");
|
| }
/** * @param int|string $value * @param string $quote * * @return int|string */ private function quote_val($value, $quote="'") { if(is_int($value)) { $quoted = $value; } else { $quoted = $quote . $value . $quote; }
return $quoted;
|
}
/** * Build a delete query. *
|
}
/** * Build a delete query. *
|
* @param string The table name to perform the query on. * @param string An optional where clause for the query. * @param string An optional limit clause for the query.
| * @param string $table The table name to perform the query on. * @param string $where An optional where clause for the query. * @param string $limit An optional limit clause for the query.
|
* @return resource The query data. */ function delete_query($table, $where="", $limit="")
| * @return resource The query data. */ function delete_query($table, $where="", $limit="")
|
Zeile 902 | Zeile 948 |
---|
/** * Escape a string according to the pg escape format. *
|
/** * Escape a string according to the pg escape format. *
|
* @param string The string to be escaped.
| * @param string $string The string to be escaped.
|
* @return string The escaped string. */ function escape_string($string) { if(function_exists("pg_escape_string")) {
|
* @return string The escaped string. */ function escape_string($string) { if(function_exists("pg_escape_string")) {
|
$string = pg_escape_string($string);
| $string = pg_escape_string($this->read_link, $string);
|
} else {
| } else {
|
Zeile 919 | Zeile 965 |
---|
}
/**
|
}
/**
|
* Frees the resources of a MySQLi query.
| * Frees the resources of a PgSQL query.
|
*
|
*
|
* @param object The query to destroy. * @return boolean Returns true on success, false on faliure
| * @param resource $query The query to destroy. * @return boolean Returns true on success, false on failure
|
*/ function free_result($query) {
| */ function free_result($query) {
|
Zeile 932 | Zeile 978 |
---|
/** * Escape a string used within a like command. *
|
/** * Escape a string used within a like command. *
|
* @param string The string to be escaped.
| * @param string $string The string to be escaped.
|
* @return string The escaped string. */ function escape_string_like($string)
|
* @return string The escaped string. */ function escape_string_like($string)
|
{ return $this->escape_string(str_replace(array('%', '_') , array('\\%' , '\\_') , $string)); }
| { return $this->escape_string(str_replace(array('\\', '%', '_') , array('\\\\', '\\%' , '\\_') , $string)); }
|
/** * Gets the current version of PgSQL. *
| /** * Gets the current version of PgSQL. *
|
Zeile 957 | Zeile 1003 |
---|
$this->version = $version['server'];
return $this->version;
|
$this->version = $version['server'];
return $this->version;
|
}
/**
| }
/**
|
* Optimizes a specific table. *
|
* Optimizes a specific table. *
|
* @param string The name of the table to be optimized.
| * @param string $table The name of the table to be optimized.
|
*/ function optimize_table($table) {
| */ function optimize_table($table) {
|
Zeile 972 | Zeile 1018 |
---|
/** * Analyzes a specific table. *
|
/** * Analyzes a specific table. *
|
* @param string The name of the table to be analyzed.
| * @param string $table The name of the table to be analyzed.
|
*/ function analyze_table($table) {
| */ function analyze_table($table) {
|
Zeile 982 | Zeile 1028 |
---|
/** * Show the "create table" command for a specific table. *
|
/** * Show the "create table" command for a specific table. *
|
* @param string The name of the table.
| * @param string $table The name of the table.
|
* @return string The pg command to create the specified table. */ function show_create_table($table)
| * @return string The pg command to create the specified table. */ function show_create_table($table)
|
Zeile 1040 | Zeile 1086 |
---|
}
if(!empty($row['rowdefault']))
|
}
if(!empty($row['rowdefault']))
|
{
| {
|
$line .= " DEFAULT {$row['rowdefault']}"; }
| $line .= " DEFAULT {$row['rowdefault']}"; }
|
Zeile 1065 | Zeile 1111 |
---|
");
$primary_key = array();
|
");
$primary_key = array();
|
| $primary_key_name = '';
$unique_keys = array();
|
// We do this in two steps. It makes placing the comma easier while($row = $this->fetch_array($query))
| // We do this in two steps. It makes placing the comma easier while($row = $this->fetch_array($query))
|
Zeile 1073 | Zeile 1122 |
---|
{ $primary_key[] = $row['column_name']; $primary_key_name = $row['index_name'];
|
{ $primary_key[] = $row['column_name']; $primary_key_name = $row['index_name'];
|
| }
if($row['unique_key'] == 't') { $unique_keys[$row['index_name']][] = $row['column_name'];
|
} }
if(!empty($primary_key)) { $lines[] = " CONSTRAINT $primary_key_name PRIMARY KEY (".implode(', ', $primary_key).")";
|
} }
if(!empty($primary_key)) { $lines[] = " CONSTRAINT $primary_key_name PRIMARY KEY (".implode(', ', $primary_key).")";
|
}
| }
foreach($unique_keys as $key_name => $key_columns) { $lines[] = " CONSTRAINT $key_name UNIQUE (".implode(', ', $key_columns).")"; }
|
$table_lines .= implode(", \n", $lines); $table_lines .= "\n)\n";
|
$table_lines .= implode(", \n", $lines); $table_lines .= "\n)\n";
|
|
|
return $table_lines; }
/** * Show the "show fields from" command for a specific table. *
|
return $table_lines; }
/** * Show the "show fields from" command for a specific table. *
|
* @param string The name of the table. * @return string Field info for that table
| * @param string $table The name of the table. * @return array Field info for that table
|
*/ function show_fields_from($table) {
| */ function show_fields_from($table) {
|
Zeile 1099 | Zeile 1158 |
---|
$primary_key = $this->fetch_field($query, 'column_name');
$query = $this->write_query("
|
$primary_key = $this->fetch_field($query, 'column_name');
$query = $this->write_query("
|
SELECT column_name as Field, data_type as Extra
| SELECT column_name, data_type, is_nullable, column_default, character_maximum_length, numeric_precision, numeric_precision_radix, numeric_scale
|
FROM information_schema.columns WHERE table_name = '{$this->table_prefix}{$table}'
|
FROM information_schema.columns WHERE table_name = '{$this->table_prefix}{$table}'
|
");
| "); $field_info = array();
|
while($field = $this->fetch_array($query)) {
|
while($field = $this->fetch_array($query)) {
|
if($field['field'] == $primary_key)
| if($field['column_name'] == $primary_key) { $field['_key'] = 'PRI'; } else { $field['_key'] = ''; }
if(stripos($field['column_default'], 'nextval') !== false) { $field['_extra'] = 'auto_increment'; } else { $field['_extra'] = ''; }
// bit, character, text fields. if(!is_null($field['character_maximum_length'])) { $field['data_type'] .= '('.(int)$field['character_maximum_length'].')'; } // numeric/decimal fields. else if($field['numeric_precision_radix'] == 10 && !is_null($field['numeric_precision']) && !is_null($field['numeric_scale']))
|
{
|
{
|
$field['extra'] = 'auto_increment';
| $field['data_type'] .= '('.(int)$field['numeric_precision'].','.(int)$field['numeric_scale'].')';
|
}
|
}
|
$field_info[] = array('Extra' => $field['extra'], 'Field' => $field['field']);
| $field_info[] = array( 'Field' => $field['column_name'], 'Type' => $field['data_type'], 'Null' => $field['is_nullable'], 'Key' => $field['_key'], 'Default' => $field['column_default'], 'Extra' => $field['_extra'], );
|
}
return $field_info;
| }
return $field_info;
|
Zeile 1119 | Zeile 1210 |
---|
/** * Returns whether or not the table contains a fulltext index. *
|
/** * Returns whether or not the table contains a fulltext index. *
|
* @param string The name of the table. * @param string Optionally specify the name of the index.
| * @param string $table The name of the table. * @param string $index Optionally specify the name of the index.
|
* @return boolean True or false if the table has a fulltext index or not. */ function is_fulltext($table, $index="")
|
* @return boolean True or false if the table has a fulltext index or not. */ function is_fulltext($table, $index="")
|
{ return false; }
| { return false; }
|
/** * Returns whether or not this database engine supports fulltext indexing.
|
/** * Returns whether or not this database engine supports fulltext indexing.
|
* * @param string The table to be checked. * @return boolean True or false if supported or not. */
| * * @param string $table The table to be checked. * @return boolean True or false if supported or not. */
|
function supports_fulltext($table) {
| function supports_fulltext($table) {
|
Zeile 1143 | Zeile 1234 |
---|
/** * Returns whether or not this database engine supports boolean fulltext matching. *
|
/** * Returns whether or not this database engine supports boolean fulltext matching. *
|
* @param string The table to be checked.
| * @param string $table The table to be checked.
|
* @return boolean True or false if supported or not. */ function supports_fulltext_boolean($table)
|
* @return boolean True or false if supported or not. */ function supports_fulltext_boolean($table)
|
{ return false; }
| { return false; }
|
/** * Creates a fulltext index on the specified column in the specified table with optional index name. *
|
/** * Creates a fulltext index on the specified column in the specified table with optional index name. *
|
* @param string The name of the table. * @param string Name of the column to be indexed. * @param string The index name, optional.
| * @param string $table The name of the table. * @param string $column Name of the column to be indexed. * @param string $name The index name, optional. * @return bool
|
*/ function create_fulltext_index($table, $column, $name="") {
| */ function create_fulltext_index($table, $column, $name="") {
|
Zeile 1166 | Zeile 1258 |
---|
/** * Drop an index with the specified name from the specified table *
|
/** * Drop an index with the specified name from the specified table *
|
* @param string The name of the table. * @param string The name of the index.
| * @param string $table The name of the table. * @param string $name The name of the index.
|
*/ function drop_index($table, $name) {
| */ function drop_index($table, $name) {
|
Zeile 1180 | Zeile 1272 |
---|
/** * Checks to see if an index exists on a specified table *
|
/** * Checks to see if an index exists on a specified table *
|
* @param string The name of the table. * @param string The name of the index.
| * @param string $table The name of the table. * @param string $index The name of the index. * @return bool Returns whether index exists
|
*/ function index_exists($table, $index) {
| */ function index_exists($table, $index) {
|
Zeile 1206 | Zeile 1299 |
---|
/** * Drop an table with the specified table *
|
/** * Drop an table with the specified table *
|
* @param string The name of the table. * @param boolean hard drop - no checking * @param boolean use table prefix
| * @param string $table The name of the table. * @param boolean $hard hard drop - no checking * @param boolean $table_prefix use table prefix
|
*/ function drop_table($table, $hard=false, $table_prefix=true)
|
*/ function drop_table($table, $hard=false, $table_prefix=true)
|
{ if($table_prefix == false) { $table_prefix = ""; } else {
| { if($table_prefix == false) { $table_prefix = ""; } else {
|
$table_prefix = $this->table_prefix; }
|
$table_prefix = $this->table_prefix; }
|
| $table_prefix_bak = $this->table_prefix; $this->table_prefix = ''; $fields = array_column($this->show_fields_from($table_prefix.$table), 'Field');
|
if($hard == false)
|
if($hard == false)
|
{ if($this->table_exists($table))
| { if($this->table_exists($table_prefix.$table))
|
{ $this->write_query('DROP TABLE '.$table_prefix.$table); }
| { $this->write_query('DROP TABLE '.$table_prefix.$table); }
|
Zeile 1231 | Zeile 1328 |
---|
else { $this->write_query('DROP TABLE '.$table_prefix.$table);
|
else { $this->write_query('DROP TABLE '.$table_prefix.$table);
|
}
$query = $this->query("SELECT column_name FROM information_schema.constraint_column_usage WHERE table_name = '{$table}' and constraint_name = '{$table}_pkey' LIMIT 1"); $field = $this->fetch_field($query, 'column_name');
// Do we not have a primary field? if($field) { $this->write_query('DROP SEQUENCE {$table}_{$field}_id_seq'); }
| }
$this->table_prefix = $table_prefix_bak;
if(!empty($fields)) { foreach($fields as &$field) { $field = "{$table_prefix}{$table}_{$field}_seq"; } unset($field);
if(version_compare($this->get_version(), '8.2.0', '>=')) { $fields = implode(', ', $fields); $this->write_query("DROP SEQUENCE IF EXISTS {$fields}"); } else { $fields = "'".implode("', '", $fields)."'"; $query = $this->query("SELECT sequence_name as field FROM information_schema.sequences WHERE sequence_name in ({$fields}) AND sequence_schema = 'public'"); while($row = $this->fetch_array($query)) { $this->write_query("DROP SEQUENCE {$row['field']}"); } } }
|
}
/** * Renames a table *
|
}
/** * Renames a table *
|
* @param string The old table name * @param string the new table name * @param boolean use table prefix
| * @param string $old_table The old table name * @param string $new_table the new table name * @param boolean $table_prefix use table prefix * @return resource
|
*/ function rename_table($old_table, $new_table, $table_prefix=true) { if($table_prefix == false)
|
*/ function rename_table($old_table, $new_table, $table_prefix=true) { if($table_prefix == false)
|
{
| {
|
$table_prefix = "";
|
$table_prefix = "";
|
}
| }
|
else { $table_prefix = $this->table_prefix; }
return $this->write_query("ALTER TABLE {$table_prefix}{$old_table} RENAME TO {$table_prefix}{$new_table}");
|
else { $table_prefix = $this->table_prefix; }
return $this->write_query("ALTER TABLE {$table_prefix}{$old_table} RENAME TO {$table_prefix}{$new_table}");
|
}
| }
|
/** * Replace contents of table with values *
|
/** * Replace contents of table with values *
|
* @param string The table * @param array The replacements * @param mixed The default field(s) * @param boolean Whether or not to return an insert id. True by default
| * @param string $table The table * @param array $replacements The replacements * @param string|array $default_field The default field(s) * @param boolean $insert_id Whether or not to return an insert id. True by default * @return int|resource|bool Returns either the insert id (if a new row is inserted and $insert_id is true), a boolean (if $insert_id is wrong) or the query resource (if a row is updated)
|
*/ function replace_query($table, $replacements=array(), $default_field="", $insert_id=true) {
| */ function replace_query($table, $replacements=array(), $default_field="", $insert_id=true) {
|
Zeile 1287 | Zeile 1403 |
---|
}
$update = false;
|
}
$update = false;
|
if(is_array($main_field) && !empty($main_field)) { $search_bit = array(); $string = ''; foreach($main_field as $field) { if(isset($mybb->binary_fields[$table][$field]) && $mybb->binary_fields[$table][$field]) { $search_bit[] = "{$field} = ".$replacements[$field]; } else { $search_bit[] = "{$field} = '".$replacements[$field]."'"; } }
$search_bit = implode(" AND ", $search_bit); $query = $this->write_query("SELECT COUNT(".$main_field[0].") as count FROM {$this->table_prefix}{$table} WHERE {$search_bit} LIMIT 1"); if($this->fetch_field($query, "count") == 1) { $update = true; } } else { $query = $this->write_query("SELECT {$main_field} FROM {$this->table_prefix}{$table}");
while($column = $this->fetch_array($query)) { if($column[$main_field] == $replacements[$main_field]) { $update = true; break; } } }
if($update === true) { if(is_array($main_field)) { return $this->update_query($table, $replacements, $search_bit); }
| $search_bit = array();
if(!is_array($main_field)) { $main_field = array($main_field); }
foreach($main_field as $field) { if(isset($mybb->binary_fields[$table][$field]) && $mybb->binary_fields[$table][$field]) { $search_bit[] = "{$field} = ".$replacements[$field]; }
|
else {
|
else {
|
return $this->update_query($table, $replacements, "{$main_field}='".$replacements[$main_field]."'");
| $search_bit[] = "{$field} = ".$this->quote_val($replacements[$field]);
|
}
|
}
|
| } $search_bit = implode(" AND ", $search_bit); $query = $this->write_query("SELECT COUNT(".$main_field[0].") as count FROM {$this->table_prefix}{$table} WHERE {$search_bit} LIMIT 1"); if($this->fetch_field($query, "count") == 1) { $update = true; }
if($update === true) { return $this->update_query($table, $replacements, $search_bit);
|
} else {
| } else {
|
Zeile 1341 | Zeile 1438 |
---|
} }
|
} }
|
| /** * @param string $table * @param string $append * * @return string */
|
function build_fields_string($table, $append="")
|
function build_fields_string($table, $append="")
|
{
| {
|
$fields = $this->show_fields_from($table);
|
$fields = $this->show_fields_from($table);
|
$comma = '';
| $comma = $fieldstring = '';
|
foreach($fields as $key => $field) { $fieldstring .= $comma.$append.$field['Field']; $comma = ','; }
|
foreach($fields as $key => $field) { $fieldstring .= $comma.$append.$field['Field']; $comma = ','; }
|
|
|
return $fieldstring; }
/** * Drops a column *
|
return $fieldstring; }
/** * Drops a column *
|
* @param string The table * @param string The column name
| * @param string $table The table * @param string $column The column name * @return resource
|
*/ function drop_column($table, $column) { return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} DROP {$column}");
|
*/ function drop_column($table, $column) { return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} DROP {$column}");
|
}
| }
|
/** * Adds a column
|
/** * Adds a column
|
* * @param string The table * @param string The column name * @param string the new column definition */
| * * @param string $table The table * @param string $column The column name * @param string $definition the new column definition * @return resource */
|
function add_column($table, $column, $definition) { return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} ADD {$column} {$definition}"); }
|
function add_column($table, $column, $definition) { return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} ADD {$column} {$definition}"); }
|
|
|
/** * Modifies a column *
|
/** * Modifies a column *
|
* @param string The table * @param string The column name * @param string the new column definition * @param boolean Whether to drop or set a column * @param boolean The new default value (if one is to be set)
| * @param string $table The table * @param string $column The column name * @param string $new_definition the new column definition * @param boolean|string $new_not_null Whether to "drop" or "set" the NOT NULL attribute (no change if false) * @param boolean|string $new_default_value The new default value, or false to drop the attribute * @return bool Returns true if all queries are executed successfully or false if one of them failed
|
*/ function modify_column($table, $column, $new_definition, $new_not_null=false, $new_default_value=false) { $result1 = $result2 = $result3 = true;
if($new_definition !== false)
|
*/ function modify_column($table, $column, $new_definition, $new_not_null=false, $new_default_value=false) { $result1 = $result2 = $result3 = true;
if($new_definition !== false)
|
{
| {
|
$result1 = $this->write_query("ALTER TABLE {$this->table_prefix}{$table} ALTER COLUMN {$column} TYPE {$new_definition}");
|
$result1 = $this->write_query("ALTER TABLE {$this->table_prefix}{$table} ALTER COLUMN {$column} TYPE {$new_definition}");
|
}
| }
|
if($new_not_null !== false)
|
if($new_not_null !== false)
|
{
| {
|
$set_drop = "DROP";
if(strtolower($new_not_null) == "set") { $set_drop = "SET";
|
$set_drop = "DROP";
if(strtolower($new_not_null) == "set") { $set_drop = "SET";
|
}
| }
|
$result2 = $this->write_query("ALTER TABLE {$this->table_prefix}{$table} ALTER COLUMN {$column} {$set_drop} NOT NULL"); }
|
$result2 = $this->write_query("ALTER TABLE {$this->table_prefix}{$table} ALTER COLUMN {$column} {$set_drop} NOT NULL"); }
|
if($new_default_value !== false)
| if($new_default_value !== null)
|
{
|
{
|
$result3 = $this->write_query("ALTER TABLE {$this->table_prefix}{$table} ALTER COLUMN {$column} SET DEFAULT {$new_default_value}"); } else { $result3 = $this->write_query("ALTER TABLE {$this->table_prefix}{$table} ALTER COLUMN {$column} DROP DEFAULT");
| if($new_default_value !== false) { $result3 = $this->write_query("ALTER TABLE {$this->table_prefix}{$table} ALTER COLUMN {$column} SET DEFAULT {$new_default_value}"); } else { $result3 = $this->write_query("ALTER TABLE {$this->table_prefix}{$table} ALTER COLUMN {$column} DROP DEFAULT"); }
|
}
return $result1 && $result2 && $result3;
| }
return $result1 && $result2 && $result3;
|
Zeile 1423 | Zeile 1532 |
---|
/** * Renames a column *
|
/** * Renames a column *
|
* @param string The table * @param string The old column name * @param string the new column name * @param string the new column definition * @param boolean Whether to drop or set a column * @param boolean The new default value (if one is to be set)
| * @param string $table The table * @param string $old_column The old column name * @param string $new_column the new column name * @param string $new_definition the new column definition * @param boolean|string $new_not_null Whether to "drop" or "set" the NOT NULL attribute (no change if false) * @param boolean|string $new_default_value The new default value, or false to drop the attribute * @return bool Returns true if all queries are executed successfully
|
*/ function rename_column($table, $old_column, $new_column, $new_definition, $new_not_null=false, $new_default_value=false) {
| */ function rename_column($table, $old_column, $new_column, $new_definition, $new_not_null=false, $new_default_value=false) {
|
Zeile 1440 | Zeile 1550 |
---|
/** * Sets the table prefix used by the simple select, insert, update and delete functions *
|
/** * Sets the table prefix used by the simple select, insert, update and delete functions *
|
* @param string The new table prefix
| * @param string $prefix The new table prefix
|
*/ function set_table_prefix($prefix) {
| */ function set_table_prefix($prefix) {
|
Zeile 1450 | Zeile 1560 |
---|
/** * Fetched the total size of all mysql tables or a specific table *
|
/** * Fetched the total size of all mysql tables or a specific table *
|
* @param string The table (optional)
| * @param string $table The table (optional)
|
* @return integer the total size of all mysql tables or a specific table */ function fetch_size($table='')
| * @return integer the total size of all mysql tables or a specific table */ function fetch_size($table='')
|
Zeile 1474 | Zeile 1584 |
---|
/** * Fetch a list of database character sets this DBMS supports *
|
/** * Fetch a list of database character sets this DBMS supports *
|
* @return array Array of supported character sets with array key being the name, array value being display name. False if unsupported
| * @return array|bool Array of supported character sets with array key being the name, array value being display name. False if unsupported
|
*/ function fetch_db_charsets() {
| */ function fetch_db_charsets() {
|
Zeile 1484 | Zeile 1594 |
---|
/** * Fetch a database collation for a particular database character set *
|
/** * Fetch a database collation for a particular database character set *
|
* @param string The database character set * @return string The matching database collation, false if unsupported
| * @param string $charset The database character set * @return string|bool The matching database collation, false if unsupported
|
*/ function fetch_charset_collation($charset) {
| */ function fetch_charset_collation($charset) {
|
Zeile 1515 | Zeile 1625 |
---|
/** * Binary database fields require special attention. *
|
/** * Binary database fields require special attention. *
|
* @param string Binary value
| * @param string $string Binary value
|
* @return string Encoded binary value */ function escape_binary($string) {
|
* @return string Encoded binary value */ function escape_binary($string) {
|
return "'".pg_escape_bytea($string)."'";
| return "'".pg_escape_bytea($this->read_link, $string)."'";
|
}
/** * Unescape binary data. *
|
}
/** * Unescape binary data. *
|
* @param string Binary value
| * @param string $string Binary value
|
* @return string Encoded binary value */ function unescape_binary($string)
| * @return string Encoded binary value */ function unescape_binary($string)
|