Development

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

You must first sign up to be able to contribute.

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

Revision 17858, 3.1 kB (checked in by FabianLange, 6 years ago)

[1.1, 1.2, 1.3] fixed some codeing standards (fixes #6376 - patch from gimler)

  • 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  * sfPDODatabase provides connectivity for the PDO database abstraction layer.
14  *
15  * @package    symfony
16  * @subpackage database
17  * @author     Daniel Swarbrick (daniel@pressure.net.nz)
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @author     Sean Kerr <sean@code-box.org>
20  * @author     Dustin Whittle <dustin.whittle@symfony-project.com>
21  * @version    SVN: $Id$
22  */
23 class sfPDODatabase extends sfDatabase
24 {
25   /**
26    * Connects to the database.
27    *
28    * @throws <b>sfDatabaseException</b> If a connection could not be created
29    */
30   public function connect()
31   {
32     // determine how to get our parameters
33     $method = $this->getParameter('method', 'dsn');
34
35     // get parameters
36     switch ($method)
37     {
38       case 'dsn':
39
40         $dsn = $this->getParameter('dsn');
41
42         if ($dsn == null)
43         {
44           // missing required dsn parameter
45           throw new sfDatabaseException('Database configuration specifies method "dsn", but is missing dsn parameter.');
46         }
47
48         break;
49     }
50
51     try
52     {
53       $pdo_class  = $this->getParameter('class', 'PDO');
54       $username   = $this->getParameter('username');
55       $password   = $this->getParameter('password');
56       $persistent = $this->getParameter('persistent');
57
58       $options = ($persistent) ? array(PDO::ATTR_PERSISTENT => true) : array();
59
60       $this->connection = new $pdo_class($dsn, $username, $password, $options);
61
62     }
63     catch (PDOException $e)
64     {
65       throw new sfDatabaseException($e->getMessage());
66     }
67
68     // lets generate exceptions instead of silent failures
69     if (sfConfig::get('sf_debug'))
70     {
71       $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
72     }
73     else
74     {
75       $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
76     }
77
78     // compatability
79     $compatability = $this->getParameter('compat');
80     if ($compatability)
81     {
82       $this->connection->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
83     }
84
85     // nulls
86     $nulls = $this->getParameter('nulls');
87     if ($nulls)
88     {
89       $this->connection->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);
90     }
91
92     // auto commit
93     $autocommit = $this->getParameter('autocommit');
94     if ($autocommit)
95     {
96       $this->connection->setAttribute(PDO::ATTR_AUTOCOMMIT, true);
97     }
98
99     $this->resource = $this->connection;
100
101   }
102
103   /**
104    * Execute the shutdown procedure.
105    *
106    * @return void
107    */
108   public function shutdown ()
109   {
110     if ($this->connection !== null)
111     {
112       @$this->connection = null;
113     }
114   }
115
116   /**
117    * Magic method for calling PDO directly via sfPDODatabase
118    *
119    * @param string $method
120    * @param array $arguments
121    * @return mixed
122    */
123   public function __call($method, $arguments)
124   {
125     return $this->getConnection()->$method($arguments);
126   }
127 }
128
Note: See TracBrowser for help on using the browser.