Zeile 6 | Zeile 6 |
---|
* Website: http://www.mybboard.com * License: http://www.mybboard.com/license.php *
|
* Website: http://www.mybboard.com * License: http://www.mybboard.com/license.php *
|
* $Id: db_pgsql.php 3902 2008-06-11 02:21:37Z Tikitiki $
| * $Id: db_pgsql.php 4340 2009-04-05 17:10:22Z Tikitiki $
|
*/
class DB_PgSQL
| */
class DB_PgSQL
|
Zeile 157 | Zeile 157 |
---|
{ $connections['read'][] = $config; }
|
{ $connections['read'][] = $config; }
|
| else
|
// Connecting to more than one server { // Specified multiple servers, but no specific read/write servers if(!array_key_exists('read', $config))
|
// Connecting to more than one server { // Specified multiple servers, but no specific read/write servers if(!array_key_exists('read', $config))
|
{
| {
|
foreach($config as $key => $settings) { if(is_int($key)) $connections['read'][] = $settings;
| foreach($config as $key => $settings) { if(is_int($key)) $connections['read'][] = $settings;
|
Zeile 171 | Zeile 172 |
---|
else { $connections = $config;
|
else { $connections = $config;
|
} }
| } }
|
$this->db_encoding = $config['encoding'];
// Actually connect to the specified servers
| $this->db_encoding = $config['encoding'];
// Actually connect to the specified servers
|
Zeile 189 | Zeile 190 |
---|
$details = $connections[$type]; unset($connections); $connections[$type][] = $details;
|
$details = $connections[$type]; unset($connections); $connections[$type][] = $details;
|
}
| }
|
// Shuffle the connections shuffle($connections[$type]);
| // Shuffle the connections shuffle($connections[$type]);
|
Zeile 216 | Zeile 217 |
---|
if($single_connection['port']) {
|
if($single_connection['port']) {
|
$this->connect_string .= "port={$single_connection['port']} ";
| $this->connect_string .= " port={$single_connection['port']}";
|
} if($single_connection['hostname'] != "localhost") {
|
} if($single_connection['hostname'] != "localhost") {
|
$this->connect_string .= "host={$single_connection['hostname']} ";
| $this->connect_string .= " host={$single_connection['hostname']}";
|
} if($single_connection['password'])
| } if($single_connection['password'])
|
Zeile 288 | Zeile 289 |
---|
if(strtolower(substr(ltrim($string), 0, 5)) == 'alter') { $string = preg_replace("#\sAFTER\s([a-z_]+?)(;*?)$#i", "", $string);
|
if(strtolower(substr(ltrim($string), 0, 5)) == 'alter') { $string = preg_replace("#\sAFTER\s([a-z_]+?)(;*?)$#i", "", $string);
|
| if(strstr($string, 'CHANGE') !== false) { $string = str_replace(' CHANGE ', ' ALTER ', $string); }
|
}
if($write_query && $this->write_link)
| }
if($write_query && $this->write_link)
|
Zeile 463 | Zeile 468 |
---|
// Do we not have a primary field? if(!$field)
|
// Do we not have a primary field? if(!$field)
|
{
| {
|
return; }
| return; }
|
Zeile 481 | Zeile 486 |
---|
if($this->write_link) { @pg_close($this->write_link);
|
if($this->write_link) { @pg_close($this->write_link);
|
}
| }
|
}
/**
| }
/**
|
Zeile 518 | Zeile 523 |
---|
else { return pg_last_error();
|
else { return pg_last_error();
|
} }
| } }
|
/** * Output a database error.
| /** * Output a database error.
|
Zeile 557 | Zeile 562 |
---|
/** * Returns the number of affected rows in a query.
|
/** * Returns the number of affected rows in a query.
|
*
| *
|
* @return int The number of affected rows.
|
* @return int The number of affected rows.
|
*/
| */
|
function affected_rows()
|
function affected_rows()
|
{
| {
|
return pg_affected_rows($this->current_link);
|
return pg_affected_rows($this->current_link);
|
}
/**
| }
/**
|
* Return the number of fields. * * @param resource The query ID.
| * Return the number of fields. * * @param resource The query ID.
|
Zeile 574 | Zeile 579 |
---|
function num_fields($query) { return pg_num_fields($query);
|
function num_fields($query) { return pg_num_fields($query);
|
}
/**
| }
/**
|
* Lists all functions in the database. * * @param string The database name.
| * Lists all functions in the database. * * @param string The database name.
|
Zeile 614 | Zeile 619 |
---|
$this->error_reporting = 0; // Execute on slave server to ensure if we've just created a table that we get the correct result
|
$this->error_reporting = 0; // Execute on slave 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}'");
| $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'); $this->error_reporting = $err;
| $exists = $this->fetch_field($query, 'table_names'); $this->error_reporting = $err;
|
Zeile 647 | Zeile 652 |
---|
$this->error_reporting = $err; if($exists > 0)
|
$this->error_reporting = $err; if($exists > 0)
|
{ return true; } else { return false; }
| { return true; } else { return false; }
|
}
/**
| }
/**
|
Zeile 672 | Zeile 677 |
---|
else { $shutdown_queries[] = $query;
|
else { $shutdown_queries[] = $query;
|
}
| }
|
}
|
}
|
|
|
/** * Performs a simple select query. *
| /** * Performs a simple select query. *
|
Zeile 690 | Zeile 695 |
---|
if($conditions != "") { $query .= " WHERE ".$conditions;
|
if($conditions != "") { $query .= " WHERE ".$conditions;
|
}
| }
|
if(isset($options['order_by'])) { $query .= " ORDER BY ".$options['order_by'];
| if(isset($options['order_by'])) { $query .= " ORDER BY ".$options['order_by'];
|
Zeile 718 | Zeile 723 |
---|
* * @param string The table name to perform the query on. * @param array An array of fields and their values.
|
* * @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 */
|
* @return int The insert ID if available */
|
function insert_query($table, $array)
| function insert_query($table, $array, $insert_id=true)
|
{ if(!is_array($array)) {
| { if(!is_array($array)) {
|
Zeile 733 | Zeile 739 |
---|
INTO {$this->table_prefix}{$table} (".$fields.") VALUES ('".$values."') ");
|
INTO {$this->table_prefix}{$table} (".$fields.") VALUES ('".$values."') ");
|
return $this->insert_id();
| if($insert_id != false) { return $this->insert_id(); } else { return true; }
|
} /**
| } /**
|
Zeile 769 | Zeile 783 |
---|
/** * 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. * @return string The query string.
| * @param string The table name to perform the query on. * @param array An array of fields and their values. * @return string The query string.
|
Zeile 805 | Zeile 819 |
---|
* @return resource The query data. */ function update_query($table, $array, $where="", $limit="")
|
* @return resource The query data. */ function update_query($table, $array, $where="", $limit="")
|
{ if(!is_array($array)) {
| { if(!is_array($array)) {
|
return false;
|
return false;
|
}
| }
|
$comma = ""; $query = ""; foreach($array as $field => $value) { $query .= $comma.$field."='".$value."'"; $comma = ", ";
|
$comma = ""; $query = ""; foreach($array as $field => $value) { $query .= $comma.$field."='".$value."'"; $comma = ", ";
|
}
| }
|
if(!empty($where)) { $query .= " WHERE $where";
| if(!empty($where)) { $query .= " WHERE $where";
|
Zeile 824 | Zeile 838 |
---|
return $this->write_query(" UPDATE {$this->table_prefix}$table SET $query
|
return $this->write_query(" UPDATE {$this->table_prefix}$table SET $query
|
"); }
| "); }
|
/** * Build an update query from an array.
| /** * Build an update query from an array.
|
Zeile 919 | Zeile 933 |
---|
function escape_string_like($string) { return $this->escape_string(str_replace(array('%', '_') , array('\\%' , '\\_') , $string));
|
function escape_string_like($string) { return $this->escape_string(str_replace(array('%', '_') , array('\\%' , '\\_') , $string));
|
}
/**
| }
/**
|
* Gets the current version of PgSQL. * * @return string Version of PgSQL.
| * Gets the current version of PgSQL. * * @return string Version of PgSQL.
|
Zeile 957 | Zeile 971 |
---|
*/ function optimize_table($table) {
|
*/ function optimize_table($table) {
|
$this->write_query("OPTIMIZE TABLE ".$this->table_prefix.$table."");
| $this->write_query("VACUUM ".$this->table_prefix.$table."");
|
} /**
| } /**
|
Zeile 967 | Zeile 981 |
---|
*/ function analyze_table($table) {
|
*/ function analyze_table($table) {
|
$this->write_query("ANALYZE TABLE ".$this->table_prefix.$table."");
| $this->write_query("ANALYZE ".$this->table_prefix.$table."");
|
}
/**
| }
/**
|
Zeile 988 | Zeile 1002 |
---|
");
$lines = array();
|
");
$lines = array();
|
$lines[] = "CREATE TABLE {$this->table_prefix}{$table} (\n";
| $table_lines = "CREATE TABLE {$this->table_prefix}{$table} (\n";
|
while($row = $this->fetch_array($query)) {
| while($row = $this->fetch_array($query)) {
|
Zeile 1072 | Zeile 1086 |
---|
$lines[] = " CONSTRAINT $primary_key_name PRIMARY KEY (".implode(', ', $primary_key).")"; }
|
$lines[] = " CONSTRAINT $primary_key_name PRIMARY KEY (".implode(', ', $primary_key).")"; }
|
$table .= implode(", \n", $lines); $table .= "\n);\n";
| $table_lines .= implode(", \n", $lines); $table_lines .= "\n)\n";
|
|
|
return $table;
| return $table_lines;
|
}
/**
| }
/**
|
Zeile 1166 | Zeile 1180 |
---|
ALTER TABLE {$this->table_prefix}$table DROP INDEX $name ");
|
ALTER TABLE {$this->table_prefix}$table DROP INDEX $name ");
|
| } /** * 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. */ function index_exists($table, $index) { $err = $this->error_reporting; $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; } else { return false; }
|
} /**
| } /**
|
Zeile 1176 | Zeile 1216 |
---|
* @param boolean use table prefix */ function drop_table($table, $hard=false, $table_prefix=true)
|
* @param boolean use table prefix */ function drop_table($table, $hard=false, $table_prefix=true)
|
{
| {
|
if($table_prefix == false)
|
if($table_prefix == false)
|
{
| {
|
$table_prefix = ""; } else { $table_prefix = $this->table_prefix;
|
$table_prefix = ""; } else { $table_prefix = $this->table_prefix;
|
}
| }
|
if($hard == false) { if($this->table_exists($table)) { $this->write_query('DROP TABLE '.$table_prefix.$table);
|
if($hard == false) { if($this->table_exists($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);
|
Zeile 1204 | Zeile 1244 |
---|
* * @param string The table * @param array The replacements
|
* * @param string The table * @param array The replacements
|
| * @param string The default field * @param boolean Whether or not to return an insert id. True by default
|
*/
|
*/
|
function replace_query($table, $replacements=array(), $default_field="")
| function replace_query($table, $replacements=array(), $default_field="", $insert_id=true)
|
{ $i = 0;
| { $i = 0;
|
Zeile 1217 | Zeile 1259 |
---|
else { $main_field = $default_field;
|
else { $main_field = $default_field;
|
}
| } $update = false;
|
$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]) {
|
$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]) {
|
++$i;
| $update = true; break;
|
} }
|
} }
|
if($i > 0)
| if($update === true)
|
{ return $this->update_query($table, $replacements, "{$main_field}='".$replacements[$main_field]."'"); }
|
{ return $this->update_query($table, $replacements, "{$main_field}='".$replacements[$main_field]."'"); }
|
else { return $this->insert_query($table, $replacements); }
| else { return $this->insert_query($table, $replacements, $insert_id); }
|
} /**
| } /**
|
Zeile 1246 | Zeile 1290 |
---|
* @param array The replacements */ function build_replace_query($table, $replacements=array(), $default_field="")
|
* @param array The replacements */ function build_replace_query($table, $replacements=array(), $default_field="")
|
{ $i = 0;
| {
|
if($default_field == "") { $query = $this->write_query("SELECT column_name FROM information_schema.constraint_column_usage WHERE table_name = '{$this->table_prefix}{$table}' and constraint_name = '{$this->table_prefix}{$table}_pkey' LIMIT 1"); $main_field = $this->fetch_field($query, 'column_name');
|
if($default_field == "") { $query = $this->write_query("SELECT column_name FROM information_schema.constraint_column_usage WHERE table_name = '{$this->table_prefix}{$table}' and constraint_name = '{$this->table_prefix}{$table}_pkey' LIMIT 1"); $main_field = $this->fetch_field($query, 'column_name');
|
} else {
| } else {
|
$main_field = $default_field;
|
$main_field = $default_field;
|
}
| }
|
|
|
| $update = false;
|
$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]) {
|
$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]) {
|
++$i;
| $update = true; break;
|
} }
|
} }
|
if($i > 0)
| if($update === true)
|
{ return $this->build_update_query($table, $replacements, "{$main_field}='".$replacements[$main_field]."'");
|
{ return $this->build_update_query($table, $replacements, "{$main_field}='".$replacements[$main_field]."'");
|
} else {
| } else {
|
return $this->build_insert_query($table, $replacements);
|
return $this->build_insert_query($table, $replacements);
|
}
| }
|
} function build_fields_string($table, $append="") { $fields = $this->show_fields_from($table); $comma = '';
|
} function build_fields_string($table, $append="") { $fields = $this->show_fields_from($table); $comma = '';
|
|
|
foreach($fields as $key => $field) { $fieldstring .= $comma.$append.$field['Field'];
| foreach($fields as $key => $field) { $fieldstring .= $comma.$append.$field['Field'];
|
Zeile 1291 | Zeile 1336 |
---|
} return $fieldstring;
|
} return $fieldstring;
|
} /**
| } /**
|
* Sets the table prefix used by the simple select, insert, update and delete functions * * @param string The new table prefix
| * Sets the table prefix used by the simple select, insert, update and delete functions * * @param string The new table prefix
|
Zeile 1368 | Zeile 1413 |
---|
static $time_start;
$time = strtok(microtime(), ' ') + strtok('');
|
static $time_start;
$time = strtok(microtime(), ' ') + strtok('');
|
|
|
// Just starting timer, init and return if(!$time_start)
|
// Just starting timer, init and return if(!$time_start)
|
{
| {
|
$time_start = $time; return; }
| $time_start = $time; return; }
|
Zeile 1386 | Zeile 1431 |
---|
} } }
|
} } }
|
if(!class_exists('databaseEngine')) { class databaseEngine extends DB_PgSQL { } }
|
|
?>
| ?>
|