You are here

function DB_pgsql::connect in Flickr API 5

Connect to the database server, log in and open the database

Don't call this method directly. Use DB::connect() instead.

PEAR DB's pgsql driver supports the following extra DSN options: + connect_timeout How many seconds to wait for a connection to be established. Available since PEAR DB 1.7.0. + new_link If set to true, causes subsequent calls to connect() to return a new connection link instead of the existing one. WARNING: this is not portable to other DBMS's. Available only if PHP is >= 4.3.0 and PEAR DB is >= 1.7.0. + options Command line options to be sent to the server. Available since PEAR DB 1.6.4. + service Specifies a service name in pg_service.conf that holds additional connection parameters. Available since PEAR DB 1.7.0. + sslmode How should SSL be used when connecting? Values: disable, allow, prefer or require. Available since PEAR DB 1.7.0. + tty This was used to specify where to send server debug output. Available since PEAR DB 1.6.4.

Example of connecting to a new link via a socket: <code> require_once 'DB.php';

$dsn = 'pgsql://user:pass@unix(/tmp)/dbname?new_link=true'; $options = array( 'portability' => DB_PORTABILITY_ALL, );

$db =& DB::connect($dsn, $options); if (PEAR::isError($db)) { die($db->getMessage()); } </code>

@link http://www.postgresql.org/docs/current/static/libpq.html#LIBPQ-CONNECT

Parameters

array $dsn the data source name:

bool $persistent should the connection be persistent?:

Return value

int DB_OK on success. A DB_Error object on failure.

File

phpFlickr/PEAR/DB/pgsql.php, line 208

Class

DB_pgsql
The methods PEAR DB uses to interact with PHP's pgsql extension for interacting with PostgreSQL databases

Code

function connect($dsn, $persistent = false) {
  if (!PEAR::loadExtension('pgsql')) {
    return $this
      ->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
  }
  $this->dsn = $dsn;
  if ($dsn['dbsyntax']) {
    $this->dbsyntax = $dsn['dbsyntax'];
  }
  $protocol = $dsn['protocol'] ? $dsn['protocol'] : 'tcp';
  $params = array(
    '',
  );
  if ($protocol == 'tcp') {
    if ($dsn['hostspec']) {
      $params[0] .= 'host=' . $dsn['hostspec'];
    }
    if ($dsn['port']) {
      $params[0] .= ' port=' . $dsn['port'];
    }
  }
  elseif ($protocol == 'unix') {

    // Allow for pg socket in non-standard locations.
    if ($dsn['socket']) {
      $params[0] .= 'host=' . $dsn['socket'];
    }
    if ($dsn['port']) {
      $params[0] .= ' port=' . $dsn['port'];
    }
  }
  if ($dsn['database']) {
    $params[0] .= ' dbname=\'' . addslashes($dsn['database']) . '\'';
  }
  if ($dsn['username']) {
    $params[0] .= ' user=\'' . addslashes($dsn['username']) . '\'';
  }
  if ($dsn['password']) {
    $params[0] .= ' password=\'' . addslashes($dsn['password']) . '\'';
  }
  if (!empty($dsn['options'])) {
    $params[0] .= ' options=' . $dsn['options'];
  }
  if (!empty($dsn['tty'])) {
    $params[0] .= ' tty=' . $dsn['tty'];
  }
  if (!empty($dsn['connect_timeout'])) {
    $params[0] .= ' connect_timeout=' . $dsn['connect_timeout'];
  }
  if (!empty($dsn['sslmode'])) {
    $params[0] .= ' sslmode=' . $dsn['sslmode'];
  }
  if (!empty($dsn['service'])) {
    $params[0] .= ' service=' . $dsn['service'];
  }
  if (isset($dsn['new_link']) && ($dsn['new_link'] == 'true' || $dsn['new_link'] === true)) {
    if (version_compare(phpversion(), '4.3.0', '>=')) {
      $params[] = PGSQL_CONNECT_FORCE_NEW;
    }
  }
  $connect_function = $persistent ? 'pg_pconnect' : 'pg_connect';
  $ini = ini_get('track_errors');
  $php_errormsg = '';
  if ($ini) {
    $this->connection = @call_user_func_array($connect_function, $params);
  }
  else {
    ini_set('track_errors', 1);
    $this->connection = @call_user_func_array($connect_function, $params);
    ini_set('track_errors', $ini);
  }
  if (!$this->connection) {
    return $this
      ->raiseError(DB_ERROR_CONNECT_FAILED, null, null, null, $php_errormsg);
  }
  return DB_OK;
}