Development

/branches/1.1/lib/database/sfPostgreSQLDatabase.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/database/sfPostgreSQLDatabase.class.php

Revision 7792, 4.9 kB (checked in by fabien, 7 years ago)

updated Sean Kerr email address

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
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 <sean@code-box.org>
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 <sean@code-box.org>
38  * @version    SVN: $Id$
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         throw new sfDatabaseException(sprintf('Invalid PostgreSQLDatabase parameter retrieval method "%s".', $method));
87     }
88
89     // let's see if we need a persistent connection
90     $persistent = $this->getParameter('persistent', false);
91     $connect    = $persistent ? 'pg_pconnect' : 'pg_connect';
92
93     $this->connection = @$connect($string);
94
95     // make sure the connection went through
96     if ($this->connection === false)
97     {
98       // the connection's foobar'd
99       throw new sfDatabaseException('Failed to create a PostgreSQLDatabase connection.');
100     }
101
102     // since we're not an abstraction layer, we copy the connection
103     // to the resource
104     $this->resource = $this->connection;
105   }
106
107   /**
108    * Loads connection parameters from an existing array.
109    *
110    * @return string A connection string
111    */
112   protected function loadParameters(&$array)
113   {
114     $database = $this->getParameter('database');
115     $host     = $this->getParameter('host');
116     $password = $this->getParameter('password');
117     $port     = $this->getParameter('port');
118     $username = $this->getParameter('username');
119
120     // construct connection string
121     $string = ($database != null ? (' dbname='  .$array[$database]) : '').
122               ($host != null     ? (' host='    .$array[$host])     : '').
123               ($password != null ? (' password='.$array[$password]) : '').
124               ($port != null     ? (' port='    .$array[$port])     : '').
125               ($username != null ? (' user='    .$array[$username]) : '');
126
127     return $string;
128   }
129
130   /**
131    * Executes the shutdown procedure.
132    *
133    * @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database
134    */
135   public function shutdown()
136   {
137     if ($this->connection != null)
138     {
139       @pg_close($this->connection);
140     }
141   }
142 }
143
Note: See TracBrowser for help on using the browser.