Development

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

You must first sign up to be able to contribute.

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

Revision 9144, 5.2 kB (checked in by FabianLange, 7 years ago)

created sfMySQLiDatabase that inherits from sfMySQLDatabase but uses some different method names.
Added unit tests provided by Dejan.Spasic for #3535, which test sfMySQLSessionStorage and sfMySQLiSessionStorage. Reproduced bug #3535 with them. Fixes #3535

  • 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  * sfMySQLDatabase provides connectivity for the MySQL 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>username</b>       - [none]  - The database username.
31  *
32  * @package    symfony
33  * @subpackage database
34  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
35  * @author     Sean Kerr <sean@code-box.org>
36  * @version    SVN: $Id$
37  */
38 class sfMySQLDatabase extends sfDatabase
39 {
40   /**
41    * Connects to the database.
42    *
43    * @throws <b>sfDatabaseException</b> If a connection could not be created
44    */
45   public function connect()
46   {
47     // determine how to get our
48     $method = $this->getParameter('method', 'normal');
49
50     switch ($method)
51     {
52       case 'normal':
53         // get parameters normally
54         $database = $this->getParameter('database');
55         $host     = $this->getParameter('host', 'localhost');
56         $password = $this->getParameter('password');
57         $username = $this->getParameter('username');
58
59         break;
60
61       case 'server':
62         // construct a connection string from existing $_SERVER values
63         // and extract them to local scope
64         $parameters =& $this->loadParameters($_SERVER);
65         extract($parameters);
66
67         break;
68
69       case 'env':
70         // construct a connection string from existing $_ENV values
71         // and extract them to local scope
72         $string =& $this->loadParameters($_ENV);
73         extract($parameters);
74
75         break;
76
77       default:
78         // who knows what the user wants...
79         throw new sfDatabaseException(sprintf('Invalid MySQLDatabase parameter retrieval method "%s".', $method));
80     }
81     
82     // let's see if we need a persistent connection
83     $connect = $this->getConnectMethod($this->getParameter('persistent', false));
84     if ($password == null)
85     {
86       if ($username == null)
87       {
88         $this->connection = @$connect($host);
89       }
90       else
91       {
92         $this->connection = @$connect($host, $username);
93       }
94     }
95     else
96     {
97       $this->connection = @$connect($host, $username, $password);
98     }
99
100     // make sure the connection went through
101     if ($this->connection === false)
102     {
103       // the connection's foobar'd
104       throw new sfDatabaseException('Failed to create a MySQLDatabase connection.');
105     }
106
107     // select our database
108     if ($this->selectDatabase($database))
109     {
110       // can't select the database
111       throw new sfDatabaseException(sprintf('Failed to select MySQLDatabase "%s".', $database));
112     }
113
114     // since we're not an abstraction layer, we copy the connection
115     // to the resource
116     $this->resource = $this->connection;
117   }
118
119   /**
120    * Returns the appropriate connect method.
121    *
122    * @param  bool $persistent wether persistent connections are use or not
123    * @return string name of connect method.
124    */
125   protected function getConnectMethod($persistent)
126   {
127     return $persistent ? 'mysql_pconnect' : 'mysql_connect';
128   }
129  
130   /**
131    * Selects the database to be used in this connection
132    *
133    * @param  string $database Name of database to be connected
134    *
135    * @return bool true if this was successful
136    */
137   protected function selectDatabase($database)
138   {
139    return ($database != null && !@mysql_select_db($database, $this->connection));
140   }
141  
142   /**
143    * Loads connection parameters from an existing array.
144    *
145    * @return array An associative array of connection parameters
146    */
147   protected function & loadParameters(&$array)
148   {
149     // list of available parameters
150     $available = array('database', 'host', 'password', 'user');
151
152     $parameters = array();
153
154     foreach ($available as $parameter)
155     {
156       $$parameter = $this->getParameter($parameter);
157
158       $parameters[$parameter] = ($$parameter != null) ? $array[$$parameter] : null;
159     }
160
161     return $parameters;
162   }
163
164   /**
165    * Execute the shutdown procedure
166    *
167    * @return void
168    *
169    * @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database
170    */
171   public function shutdown()
172   {
173     if ($this->connection != null)
174     {
175       @mysql_close($this->connection);
176     }
177   }
178 }
179
Note: See TracBrowser for help on using the browser.