_errorNum = 1;
$this->_errorMsg = 'The PostgreSQL adapter "pg" is not available.';
return;
}
// connect to the server
if (!($this->_resource = @pg_connect( "host=$host user=$user password=$password" ))) {
$this->_errorNum = 2;
$this->_errorMsg = 'Could not connect to PostgreSQL';
return;
}
// finalize initialization
parent::__construct($options);
}
/**
* Database object destructor
*
* @return boolean
* @since 1.5
*/
function __destruct()
{
$return = false;
if (is_resource($this->_resource)) {
$return = pg_close($this->_resource);
}
return $return;
}
/**
* Test to see if the PostgreSQL connector is available
*
* @static
* @access public
* @return boolean True on success, false otherwise.
*/
function test()
{
return (function_exists( 'pg_connect' ));
}
/**
* Determines if the connection to the server is active.
*
* @access public
* @return boolean
* @since 1.5
*/
function connected()
{
if(is_resource($this->_resource)) {
return pg_ping($this->_resource);
}
return false;
}
/**
* Determines UTF support
*
* @access public
* @return boolean True - UTF is supported
*/
function hasUTF()
{
return true;//TODO
//return pg_client_encoding( $this->_resource );
}
/**
* Custom settings for UTF support
*
* @access public
*/
function setUTF()
{
pg_set_client_encoding( $this->_resource, 'UTF8' );
}
/**
* Get a database escaped string
*
* @param string The string to be escaped
* @param boolean Optional parameter to provide extra escaping
* @return string
* @access public
* @abstract
*/
function getEscaped( $text, $extra = false )
{
$result = pg_escape_string( $this->_resource, $text );
if ($extra) {
$result = addcslashes( $result, '%_' );
}
return $result;
}
/**
* Generate SQL command for getting string position
*
* @access public
* @param string The string being sought
* @param string The string/column being searched
* @return string The resulting SQL
*/
function stringPositionSQL($substring, $string)
{
$sql = "POSITION($substring, $string)";
return $sql;
}
/**
* Generate SQL command for returning random value
*
* @access public
* @return string The resulting SQL
*/
function stringRandomSQL()
{
return "RANDOM()";
}
/**
* Create database
*
* @access public
* @param string The database name
* @param bool Whether or not to create with UTF support
* @return string Database creation string
*/
function createDatabase($DBname, $DButfSupport)
{
if ($DButfSupport)
{
$sql = "CREATE DATABASE ".$this->nameQuote($DBname)." ENCODING '$DBname')";
}
else
{
$sql = "CREATE DATABASE ".$this->nameQuote($DBname);
}
$this->setQuery($sql);
$this->query();
$result = $this->getErrorNum();
if ($result != 0)
{
return false;
}
return true;
}
/**
* Rename a database table
*
* @param string The old table name
* @param string The new table name
*/
function renameTable($oldTable, $newTable)
{
$query = "ALTER TABLE ".$oldTable." TO ".$newTable;
$db->setQuery($query);
$db->query();
$result = $db->getErrorNum();
if ($result != 0)
{
return false;
}
return true;
}
/**
* Execute the query
*
* @access public
* @return mixed A database resource if successful, FALSE if not.
*/
function query()
{
if (!is_resource($this->_resource)) {
return false;
}
// Take a local copy so that we don't modify the original query and cause issues later
$sql = $this->_sql;
if ($this->_limit > 0 || $this->_offset > 0) {
$sql .= ' LIMIT '.$this->_limit.' OFFSET '.$this->_offset;
}
if ($this->_debug) {
$this->_ticker++;
$this->_log[] = $sql;
}
$this->_errorNum = 0;
$this->_errorMsg = '';
$this->_cursor = pg_query( $this->_resource, $sql );
if (!$this->_cursor)
{
$this->_errorNum = pg_result_error_field( $this->_cursor, PGSQL_DIAG_SQLSTATE ) . ' ';
$this->_errorMsg = pg_result_error_field( $this->_cursor, PGSQL_DIAG_MESSAGE_PRIMARY )." SQL=$sql
";
if ($this->_debug) {
JError::raiseError(500, 'JDatabasePostgreSQL::query: '.$this->_errorNum.' - '.$this->_errorMsg );
}
return false;
}
return $this->_cursor;
}
/**
* Description
*
* @access public
* @return int The number of affected rows in the previous operation
* @since 1.0.5
*/
function getAffectedRows()
{
return pg_affected_rows( $this->_resource );
}
/**
* Execute a batch query
*
* @access public
* @return mixed A database resource if successful, FALSE if not.
*/
function queryBatch( $abort_on_error=true, $p_transaction_safe = false)
{
$this->_errorNum = 0;
$this->_errorMsg = '';
if ($p_transaction_safe) {
$this->_sql = rtrim($this->_sql, "; \t\r\n\0");
$si = $this->getVersion();
preg_match_all( "/(\d+)\.(\d+)\.(\d+)/i", $si, $m );
if ($m[1] >= 4) {
$this->_sql = 'START TRANSACTION;' . $this->_sql . '; COMMIT;';
} else if ($m[2] >= 23 && $m[3] >= 19) {
$this->_sql = 'BEGIN WORK;' . $this->_sql . '; COMMIT;';
} else if ($m[2] >= 23 && $m[3] >= 17) {
$this->_sql = 'BEGIN;' . $this->_sql . '; COMMIT;';
}
}
$query_split = $this->splitSql($this->_sql);
$error = 0;
foreach ($query_split as $command_line) {
$command_line = trim( $command_line );
if ($command_line != '') {
$this->_cursor = pg_query( $this->_resource, $command_line );
if ($this->_debug) {
$this->_ticker++;
$this->_log[] = $command_line;
}
if (!$this->_cursor) {
$error = 1;
$this->_errorNum .= pg_result_error_field( $this->_cursor, PGSQL_DIAG_SQLSTATE ) . ' ';
$this->_errorMsg .= pg_result_error_field( $this->_cursor, PGSQL_DIAG_MESSAGE_PRIMARY )." SQL=$command_line
";
if ($abort_on_error) {
return $this->_cursor;
}
}
}
}
return $error ? false : true;
}
/**
* Diagnostic function
*
* @access public
* @return string
*/
function explain()
{
$temp = $this->_sql;
$this->_sql = "EXPLAIN $this->_sql";
if (!($cur = $this->query())) {
return null;
}
$first = true;
$buffer = '
'.$this->getQuery().' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'.$k.' | '; } $buffer .= '||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
'.$v.' | '; } $buffer .= '