Development

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

You must first sign up to be able to contribute.

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

Revision 23810, 3.4 kB (checked in by Kris.Wallsmith, 5 years ago)

[1.3] set svn:eol-style property to native and svn:keywords property to Id on all .php files

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
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>username</b>   - [none]      - The database username.
20  * # <b>password</b>   - [none]      - The database password.
21  * # <b>persistent</b> - [No]        - Indicates that the connection should be persistent.
22  *
23  * @package    symfony
24  * @subpackage database
25  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
26  * @author     Sean Kerr <sean@code-box.org>
27  * @version    SVN: $Id$
28  */
29 class sfMySQLDatabase extends sfDatabase
30 {
31   /**
32    * Connects to the database.
33    *
34    * @throws <b>sfDatabaseException</b> If a connection could not be created
35    */
36   public function connect()
37   {
38     $database = $this->getParameter('database');
39     $host     = $this->getParameter('host', 'localhost');
40     $password = $this->getParameter('password');
41     $username = $this->getParameter('username');
42     $encoding = $this->getParameter('encoding');
43
44     // let's see if we need a persistent connection
45     $connect = $this->getConnectMethod($this->getParameter('persistent', false));
46     if ($password == null)
47     {
48       if ($username == null)
49       {
50         $this->connection = @$connect($host);
51       }
52       else
53       {
54         $this->connection = @$connect($host, $username);
55       }
56     }
57     else
58     {
59       $this->connection = @$connect($host, $username, $password);
60     }
61
62     // make sure the connection went through
63     if ($this->connection === false)
64     {
65       // the connection's foobar'd
66       throw new sfDatabaseException('Failed to create a MySQLDatabase connection.');
67     }
68
69     // select our database
70     if ($this->selectDatabase($database))
71     {
72       // can't select the database
73       throw new sfDatabaseException(sprintf('Failed to select MySQLDatabase "%s".', $database));
74     }
75
76     // set encoding if specified
77     if ($encoding)
78     {
79       @mysql_query("SET NAMES '".$encoding."'", $this->connection);
80     }
81
82     // since we're not an abstraction layer, we copy the connection
83     // to the resource
84     $this->resource = $this->connection;
85   }
86
87   /**
88    * Returns the appropriate connect method.
89    *
90    * @param bool $persistent wether persistent connections are use or not
91    * @return string name of connect method.
92    */
93   protected function getConnectMethod($persistent)
94   {
95     return $persistent ? 'mysql_pconnect' : 'mysql_connect';
96   }
97  
98   /**
99    * Selects the database to be used in this connection
100    *
101    * @param string $database Name of database to be connected
102    *
103    * @return bool true if this was successful
104    */
105   protected function selectDatabase($database)
106   {
107    return ($database != null && !@mysql_select_db($database, $this->connection));
108   }
109
110   /**
111    * Execute the shutdown procedure
112    *
113    * @return void
114    *
115    * @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database
116    */
117   public function shutdown()
118   {
119     if ($this->connection != null)
120     {
121       @mysql_close($this->connection);
122     }
123   }
124 }
125
Note: See TracBrowser for help on using the browser.