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 * * @var boolean
| * Weather or not this engine can use the search functionality * * @var boolean
|
Zeile 178 | Zeile 190 |
---|
else { $connections = $config;
|
else { $connections = $config;
|
} }
$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 288 | Zeile 307 |
---|
{ global $mybb;
|
{ global $mybb;
|
$string = preg_replace("#LIMIT (\s*)([0-9]+),(\s*)([0-9]+)$#im", "LIMIT $4 OFFSET $2", trim($string));
| $string = preg_replace("#LIMIT (\s*)([0-9]+),(\s*)([0-9]+);?$#im", "LIMIT $4 OFFSET $2", trim($string));
|
$this->last_query = $string;
| $this->last_query = $string;
|
Zeile 319 | Zeile 338 |
---|
}
if((pg_result_error($query) && !$hide_errors))
|
}
if((pg_result_error($query) && !$hide_errors))
|
{
| {
|
$this->error($string, $query); exit;
|
$this->error($string, $query); exit;
|
}
| }
|
$query_time = get_execution_time(); $this->query_time += $query_time;
| $query_time = get_execution_time(); $this->query_time += $query_time;
|
Zeile 338 | Zeile 357 |
---|
/** * Execute a write query on the slave database
|
/** * Execute a write query on the slave database
|
*
| *
|
* @param string $query The query SQL. * @param boolean|int $hide_errors 1 if hide errors, 0 if not. * @return resource The query data.
| * @param string $query The query SQL. * @param boolean|int $hide_errors 1 if hide errors, 0 if not. * @return resource The query data.
|
Zeile 357 | Zeile 376 |
---|
function explain_query($string, $qtime) { if(preg_match("#^\s*select#i", $string))
|
function explain_query($string, $qtime) { if(preg_match("#^\s*select#i", $string))
|
{
| {
|
$query = pg_query($this->current_link, "EXPLAIN $string"); $this->explain .= "<table style=\"background-color: #666;\" width=\"95%\" cellpadding=\"4\" cellspacing=\"1\" align=\"center\">\n". "<tr>\n".
| $query = pg_query($this->current_link, "EXPLAIN $string"); $this->explain .= "<table style=\"background-color: #666;\" width=\"95%\" cellpadding=\"4\" cellspacing=\"1\" align=\"center\">\n". "<tr>\n".
|
Zeile 414 | Zeile 433 |
---|
function fetch_array($query, $resulttype=PGSQL_ASSOC) { switch($resulttype)
|
function fetch_array($query, $resulttype=PGSQL_ASSOC) { switch($resulttype)
|
{
| {
|
case PGSQL_NUM: case PGSQL_BOTH: break;
| case PGSQL_NUM: case PGSQL_BOTH: break;
|
Zeile 441 | Zeile 460 |
---|
if($row === false) { $array = $this->fetch_array($query);
|
if($row === false) { $array = $this->fetch_array($query);
|
return $array[$field]; } else { return pg_fetch_result($query, $row, $field);
| if($array !== null && $array !== false) { return $array[$field]; } return null;
|
}
|
}
|
| return pg_fetch_result($query, $row, $field);
|
}
/**
| }
/**
|
Zeile 479 | Zeile 500 |
---|
*/ function insert_id() {
|
*/ 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);
| preg_match('#INSERT\s+INTO\s+([a-zA-Z0-9_\-]+)#i', $this->last_query, $matches);
|
$table = $matches[1];
| $table = $matches[1];
|
Zeile 496 | Zeile 515 |
---|
$id = $this->write_query("SELECT currval(pg_get_serial_sequence('{$table}', '{$field}')) AS last_value"); return $this->fetch_field($id, 'last_value');
|
$id = $this->write_query("SELECT currval(pg_get_serial_sequence('{$table}', '{$field}')) AS last_value"); return $this->fetch_field($id, 'last_value');
|
}
/**
| }
/**
|
* Close the connection with the DBMS.
|
* Close the connection with the DBMS.
|
*
| *
|
*/ function close() {
| */ function close() {
|
Zeile 513 | Zeile 532 |
---|
/** * Return an error number.
|
/** * Return an error number.
|
* * @param resource $query
| * * @param resource $query
|
* @return int The error number of the current error. */ function error_number($query=null)
| * @return int The error number of the current error. */ function error_number($query=null)
|
Zeile 523 | Zeile 542 |
---|
{ return 0; }
|
{ return 0; }
|
|
|
return pg_result_error_field($query, PGSQL_DIAG_SQLSTATE); }
| return pg_result_error_field($query, PGSQL_DIAG_SQLSTATE); }
|
Zeile 532 | Zeile 551 |
---|
* * @param resource $query * @return string The explanation for the current error.
|
* * @param resource $query * @return string The explanation for the current error.
|
*/
| */
|
function error_string($query=null) { if($query != null) { return pg_result_error($query);
|
function error_string($query=null) { if($query != null) { return pg_result_error($query);
|
}
| }
|
if($this->current_link)
|
if($this->current_link)
|
{
| {
|
return pg_last_error($this->current_link);
|
return pg_last_error($this->current_link);
|
}
| }
|
else { return pg_last_error();
|
else { return pg_last_error();
|
} }
/**
| } }
/**
|
* Output a database error. * * @param string $string The string to present as an error.
| * Output a database error. * * @param string $string The string to present as an error.
|
Zeile 603 | Zeile 622 |
---|
function num_fields($query) { return pg_num_fields($query);
|
function num_fields($query) { return pg_num_fields($query);
|
}
/**
| }
/**
|
* Lists all tables in the database. * * @param string $database The database name.
| * Lists all tables in the database. * * @param string $database The database name.
|
Zeile 643 | Zeile 662 |
---|
// 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}'");
|
// 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');
if($exists > 0) { return true; } else { return false; } }
| $exists = $this->fetch_field($query, 'table_names');
if($exists > 0) { return true; } else { return false; } }
|
/** * Check if a field exists in a database. *
| /** * Check if a field exists in a database. *
|
Zeile 669 | Zeile 688 |
---|
$exists = $this->fetch_field($query, "column_names");
if($exists > 0)
|
$exists = $this->fetch_field($query, "column_names");
if($exists > 0)
|
{ return true; } else { return false;
| { return true; } else { return false;
|
} }
| } }
|
Zeile 712 | Zeile 731 |
---|
if($conditions != "") { $query .= " WHERE ".$conditions;
|
if($conditions != "") { $query .= " WHERE ".$conditions;
|
}
| }
|
if(isset($options['group_by']))
|
if(isset($options['group_by']))
|
{
| {
|
$query .= " GROUP BY ".$options['group_by'];
|
$query .= " GROUP BY ".$options['group_by'];
|
}
| }
|
if(isset($options['order_by']))
|
if(isset($options['order_by']))
|
{
| {
|
$query .= " ORDER BY ".$options['order_by']; if(isset($options['order_dir'])) {
| $query .= " ORDER BY ".$options['order_by']; if(isset($options['order_dir'])) {
|
Zeile 731 | Zeile 750 |
---|
if(isset($options['limit_start']) && isset($options['limit'])) { $query .= " LIMIT ".$options['limit_start'].", ".$options['limit'];
|
if(isset($options['limit_start']) && isset($options['limit'])) { $query .= " LIMIT ".$options['limit_start'].", ".$options['limit'];
|
}
| }
|
else if(isset($options['limit'])) { $query .= " LIMIT ".$options['limit'];
| else if(isset($options['limit'])) { $query .= " LIMIT ".$options['limit'];
|
Zeile 739 | Zeile 758 |
---|
return $this->query($query); }
|
return $this->query($query); }
|
|
|
/** * Build an insert query from an array. *
| /** * Build an insert query from an array. *
|
Zeile 749 | Zeile 768 |
---|
* @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)
|
* @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)
|
{ global $mybb;
| { global $mybb;
|
if(!is_array($array)) { return false;
| if(!is_array($array)) { return false;
|
Zeile 808 | Zeile 827 |
---|
$insert_rows = array(); foreach($array as $values)
|
$insert_rows = array(); foreach($array as $values)
|
{
| {
|
foreach($values as $field => $value) { if(isset($mybb->binary_fields[$table][$field]) && $mybb->binary_fields[$table][$field])
| foreach($values as $field => $value) { if(isset($mybb->binary_fields[$table][$field]) && $mybb->binary_fields[$table][$field])
|
Zeile 846 | Zeile 865 |
---|
global $mybb;
if(!is_array($array))
|
global $mybb;
if(!is_array($array))
|
{
| {
|
return false; }
| return false; }
|
Zeile 936 | Zeile 955 |
---|
{ if(function_exists("pg_escape_string")) {
|
{ if(function_exists("pg_escape_string")) {
|
$string = pg_escape_string($string);
| $string = pg_escape_string($this->read_link, $string);
|
} else { $string = addslashes($string); } return $string;
|
} else { $string = addslashes($string); } return $string;
|
}
| }
|
/** * Frees the resources of a PgSQL query.
| /** * Frees the resources of a PgSQL query.
|
Zeile 963 | Zeile 982 |
---|
* @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 979 | Zeile 998 |
---|
return $this->version; }
|
return $this->version; }
|
$version = pg_version($this->current_link);
| $version = pg_version($this->current_link);
|
$this->version = $version['server'];
return $this->version;
|
$this->version = $version['server'];
return $this->version;
|
}
| }
|
/** * Optimizes a specific table. *
| /** * Optimizes a specific table. *
|
Zeile 1043 | Zeile 1062 |
---|
else { $row['rowdefault'] = $this->fetch_field($query2, 'rowdefault');
|
else { $row['rowdefault'] = $this->fetch_field($query2, 'rowdefault');
|
}
| }
|
if($row['type'] == 'bpchar')
|
if($row['type'] == 'bpchar')
|
{
| {
|
// Stored in the engine as bpchar, but in the CREATE TABLE statement it's char $row['type'] = 'char';
|
// Stored in the engine as bpchar, but in the CREATE TABLE statement it's char $row['type'] = 'char';
|
}
$line = " {$row['field']} {$row['type']}";
| }
$line = " {$row['field']} {$row['type']}";
|
if(strpos($row['type'], 'char') !== false) {
| if(strpos($row['type'], 'char') !== false) {
|
Zeile 1062 | Zeile 1081 |
---|
}
if(strpos($row['type'], 'numeric') !== false)
|
}
if(strpos($row['type'], 'numeric') !== false)
|
{
| {
|
$line .= '('.sprintf("%s,%s", (($row['lengthvar'] >> 16) & 0xffff), (($row['lengthvar'] - 4) & 0xffff)).')'; }
| $line .= '('.sprintf("%s,%s", (($row['lengthvar'] >> 16) & 0xffff), (($row['lengthvar'] - 4) & 0xffff)).')'; }
|
Zeile 1119 | Zeile 1138 |
---|
foreach($unique_keys as $key_name => $key_columns) { $lines[] = " CONSTRAINT $key_name UNIQUE (".implode(', ', $key_columns).")";
|
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";
|
Zeile 1139 | 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}' "); $field_info = array(); while($field = $this->fetch_array($query)) {
|
FROM information_schema.columns WHERE table_name = '{$this->table_prefix}{$table}' "); $field_info = array(); while($field = $this->fetch_array($query)) {
|
if($field['field'] == $primary_key)
| if($field['column_name'] == $primary_key)
|
{
|
{
|
$field['extra'] = 'auto_increment';
| $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['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 1163 | Zeile 1213 |
---|
* @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.
|
* @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="")
|
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. *
|
Zeile 1183 | Zeile 1233 |
---|
/** * Returns whether or not this database engine supports boolean fulltext matching.
|
/** * Returns whether or not this database engine supports boolean fulltext matching.
|
*
| *
|
* @param string $table 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. */
|
Zeile 1221 | Zeile 1271 |
---|
/** * Checks to see if an index exists on a specified table
|
/** * Checks to see if an index exists on a specified table
|
*
| *
|
* @param string $table The name of the table. * @param string $index The name of the index. * @return bool Returns whether index exists
| * @param string $table The name of the table. * @param string $index The name of the index. * @return bool Returns whether index exists
|
Zeile 1232 | Zeile 1282 |
---|
$this->error_reporting = 0;
$query = $this->write_query("SELECT * FROM pg_indexes WHERE tablename='".$this->escape_string($this->table_prefix.$table)."'");
|
$this->error_reporting = 0;
$query = $this->write_query("SELECT * FROM pg_indexes WHERE tablename='".$this->escape_string($this->table_prefix.$table)."'");
|
|
|
$exists = $this->fetch_field($query, $index); $this->error_reporting = $err;
if($exists) { return true;
|
$exists = $this->fetch_field($query, $index); $this->error_reporting = $err;
if($exists) { return true;
|
} else {
| } else {
|
return false; } }
| return false; } }
|
Zeile 1259 | Zeile 1309 |
---|
{ $table_prefix = ""; }
|
{ $table_prefix = ""; }
|
else { $table_prefix = $this->table_prefix; }
| else { $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);
|
}
| }
|
} 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->table_prefix = $table_prefix_bak;
if(!empty($fields))
|
{
|
{
|
$this->write_query('DROP SEQUENCE {$table}_{$field}_id_seq');
| 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']}"); } }
|
} }
| } }
|
Zeile 1559 | Zeile 1630 |
---|
*/ function escape_binary($string) {
|
*/ function escape_binary($string) {
|
return "'".pg_escape_bytea($string)."'";
| return "'".pg_escape_bytea($this->read_link, $string)."'";
|
}
/**
| }
/**
|