_create($url);
}
/**
* _create
*
* Creates the DB connection.
*
* @param $url The url with the database params. Example: pgsql://username:password@localhost/databasename
*
* @version 0.1
*/
function _create($url) {
$url = parse_url($url);
$conn_string = '';
if (isset($url['user'])) {
$conn_string .= ' user='. urldecode($url['user']);
}
if (isset($url['pass'])) {
$conn_string .= ' password='. urldecode($url['pass']);
}
if (isset($url['host'])) {
$conn_string .= ' host='. urldecode($url['host']);
}
if (isset($url['path'])) {
$conn_string .= ' dbname='. substr(urldecode($url['path']), 1);
}
if (isset($url['port'])) {
$conn_string .= ' port='. urldecode($url['port']);
}
if ( defined('dbcharset') ){
$this->charset = dbcharset;
}
$this->dbcon = @pg_connect($conn_string);
if (!$this->dbcon){
pt_kill("There were problems with the database connection, please check the information of your pt_settings.php file.
- Make sure your username and your password are right.
- Make sure your hostname is right. The hostname i attempted to contact is
$dbhost
- Make sure your database server is running
");
return false;
}
if ( !empty($this->charset) && version_compare(mysql_get_server_info(), '4.1.0', '>=') )
@pg_query($this->dbcon, "SET NAMES '$this->charset'");
@pg_set_client_encoding($this->charset);
}
/**
* db_query
*
* Executes a query.
*
* @param $query The query to execute
* @param $fetch The type of fetch wished. Example: db_query($query, array);
*
* @version 0.1
*/
function db_query($query, $fetch = "")
{
$this->flushVars();
$this->execution = @pg_query($this->dbcon, $query);
if (!$this->execution){
//to handle mysql errors;
echo pg_last_error($this->dbcon);
}
if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) {
$this->affected_rows = pg_affected_rows($this->execution);
} else {
switch ($fetch) {
case 'array':
$i=0;
while ($row = @pg_fetch_assoc($this->execution)) {
$this->row[$i] = $row;
$i++;
}
break;
case 'row':
while ($row = @pg_fetch_row($this->execution))
$this->row = $row;
break;
case 'field':
$i=0;
$this->affected_fields = @mysql_num_fields($this->execution);
while ($i < $this->affected_fields) {
$this->col = $this->_pg_fetch_field($this->result, $i);
$i++;
}
break;
case 'alive':
return true;
default:
$i = 0;
while ( $row = @pg_fetch_object($this->execution) ) {
$this->row[$i] = $row;
$i++;
}
$this->affected_rows = $i;
break;
}
}
}
function _pg_fetch_field($result, $i) {
$meta['name'] = @pg_field_name($result, $i);
$met['type'] = @pg_field_type($result, $i);
$meta['table'] = @pg_field_table($result, $i);
return $meta;
}
/**
* flushvars
*
* Flushes the vars of the module.
*
* @version 0.1
*/
function flushVars()
{
$this->query = null;
$this->error = null;
$this->result = null;
$this->execution = null;
}
/**
* is_alive
*
* Check if the db connection is alive.
*
* @return true Return tru if the connection is alive
*
* @version 0.1
*/
function is_alive() {
if (pg_ping($this->dbcon))
return true;
}
/**
* db_version
*
* Gets the version of the db server.
*
* @return version The database version.
*
* @version 0.1
*/
function db_version() {
return pg_parameter_status($this->dbcon, server_version);
}
}
?>