[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/database/ -> sfPostgreSQLDatabase.class.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the symfony package.
   5   * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
   6   * (c) 2004-2006 Sean Kerr.
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  /**
  13   * sfPostgreSQLDatabase provides connectivity for the PostgreSQL brand database.
  14   *
  15   * <b>Optional parameters:</b>
  16   *
  17   * # <b>database</b>   - [none]      - The database name.
  18   * # <b>host</b>       - [localhost] - The database host.
  19   * # <b>method</b>     - [normal]    - How to read connection parameters.
  20   *                                     Possible values are normal, server, and
  21   *                                     env. The normal method reads them from
  22   *                                     the specified values. server reads them
  23   *                                     from $_SERVER where the keys to retrieve
  24   *                                     the values are what you specify the value
  25   *                                     as in the settings. env reads them from
  26   *                                     $_ENV and works like $_SERVER.
  27   * # <b>password</b>   - [none]      - The database password.
  28   * # <b>persistent</b> - [No]        - Indicates that the connection should be
  29   *                                     persistent.
  30   * # <b>port</b>       - [none]      - TCP/IP port on which PostgreSQL is
  31   *                                     listening.
  32   * # <b>username</b>       - [none]  - The database username.
  33   *
  34   * @package    symfony
  35   * @subpackage database
  36   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  37   * @author     Sean Kerr <skerr@mojavi.org>
  38   * @version    SVN: $Id: sfPostgreSQLDatabase.class.php 3210 2007-01-10 20:28:16Z fabien $
  39   */
  40  class sfPostgreSQLDatabase extends sfDatabase
  41  {
  42    /**
  43     * Connects to the database.
  44     *
  45     * @throws <b>sfDatabaseException</b> If a connection could not be created
  46     */
  47    public function connect()
  48    {
  49      // determine how to get our parameters
  50      $method = $this->getParameter('method', 'normal');
  51  
  52      // get parameters
  53      switch ($method)
  54      {
  55        case 'normal':
  56          // get parameters normally
  57          $database = $this->getParameter('database');
  58          $host     = $this->getParameter('host');
  59          $password = $this->getParameter('password');
  60          $port     = $this->getParameter('port');
  61          $username = $this->getParameter('username');
  62  
  63          // construct connection string
  64          $string = ($database != null ? (' dbname='   .$database) : '').
  65                    ($host != null     ? (' host='     .$host)     : '').
  66                    ($password != null ? (' password=' .$password) : '').
  67                    ($port != null     ? (' port='     .$port)     : '').
  68                    ($username != null ? (' user='     .$username) : '');
  69  
  70          break;
  71  
  72        case 'server':
  73          // construct a connection string from existing $_SERVER values
  74          $string = $this->loadParameters($_SERVER);
  75  
  76          break;
  77  
  78        case 'env':
  79          // construct a connection string from existing $_ENV values
  80          $string = $this->loadParameters($_ENV);
  81  
  82          break;
  83  
  84        default:
  85          // who knows what the user wants...
  86          $error = 'Invalid PostgreSQLDatabase parameter retrieval method "%s"';
  87          $error = sprintf($error, $method);
  88  
  89          throw new sfDatabaseException($error);
  90      }
  91  
  92      // let's see if we need a persistent connection
  93      $persistent = $this->getParameter('persistent', false);
  94      $connect    = $persistent ? 'pg_pconnect' : 'pg_connect';
  95  
  96      $this->connection = @$connect($string);
  97  
  98      // make sure the connection went through
  99      if ($this->connection === false)
 100      {
 101        // the connection's foobar'd
 102        $error = 'Failed to create a PostgreSQLDatabase connection';
 103  
 104        throw new sfDatabaseException($error);
 105      }
 106  
 107      // since we're not an abstraction layer, we copy the connection
 108      // to the resource
 109      $this->resource = $this->connection;
 110    }
 111  
 112    /**
 113     * Loads connection parameters from an existing array.
 114     *
 115     * @return string A connection string
 116     */
 117    protected function loadParameters(&$array)
 118    {
 119      $database = $this->getParameter('database');
 120      $host     = $this->getParameter('host');
 121      $password = $this->getParameter('password');
 122      $port     = $this->getParameter('port');
 123      $username = $this->getParameter('username');
 124  
 125      // construct connection string
 126      $string = ($database != null ? (' dbname='  .$array[$database]) : '').
 127                ($host != null     ? (' host='    .$array[$host])     : '').
 128                ($password != null ? (' password='.$array[$password]) : '').
 129                ($port != null     ? (' port='    .$array[$port])     : '').
 130                ($username != null ? (' user='    .$array[$username]) : '');
 131  
 132      return $string;
 133    }
 134  
 135    /**
 136     * Executes the shutdown procedure.
 137     *
 138     * @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database
 139     */
 140    public function shutdown()
 141    {
 142      if ($this->connection != null)
 143      {
 144        @pg_close($this->connection);
 145      }
 146    }
 147  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7