| 1 |
lars |
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: sfPDODatabase.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
|
|
|
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 |
if (!$dsn = $this->getParameter('dsn'))
|
|
|
33 |
{
|
|
|
34 |
// missing required dsn parameter
|
|
|
35 |
throw new sfDatabaseException('Database configuration is missing the "dsn" parameter.');
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
try
|
|
|
39 |
{
|
|
|
40 |
$pdo_class = $this->getParameter('class', 'PDO');
|
|
|
41 |
$username = $this->getParameter('username');
|
|
|
42 |
$password = $this->getParameter('password');
|
|
|
43 |
$persistent = $this->getParameter('persistent');
|
|
|
44 |
|
|
|
45 |
$options = ($persistent) ? array(PDO::ATTR_PERSISTENT => true) : array();
|
|
|
46 |
|
|
|
47 |
$this->connection = new $pdo_class($dsn, $username, $password, $options);
|
|
|
48 |
|
|
|
49 |
}
|
|
|
50 |
catch (PDOException $e)
|
|
|
51 |
{
|
|
|
52 |
throw new sfDatabaseException($e->getMessage());
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
// lets generate exceptions instead of silent failures
|
|
|
56 |
if (sfConfig::get('sf_debug'))
|
|
|
57 |
{
|
|
|
58 |
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
59 |
}
|
|
|
60 |
else
|
|
|
61 |
{
|
|
|
62 |
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
// compatability
|
|
|
66 |
$compatability = $this->getParameter('compat');
|
|
|
67 |
if ($compatability)
|
|
|
68 |
{
|
|
|
69 |
$this->connection->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
// nulls
|
|
|
73 |
$nulls = $this->getParameter('nulls');
|
|
|
74 |
if ($nulls)
|
|
|
75 |
{
|
|
|
76 |
$this->connection->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
// auto commit
|
|
|
80 |
$autocommit = $this->getParameter('autocommit');
|
|
|
81 |
if ($autocommit)
|
|
|
82 |
{
|
|
|
83 |
$this->connection->setAttribute(PDO::ATTR_AUTOCOMMIT, true);
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
$this->resource = $this->connection;
|
|
|
87 |
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Execute the shutdown procedure.
|
|
|
92 |
*
|
|
|
93 |
* @return void
|
|
|
94 |
*/
|
|
|
95 |
public function shutdown()
|
|
|
96 |
{
|
|
|
97 |
if ($this->connection !== null)
|
|
|
98 |
{
|
|
|
99 |
@$this->connection = null;
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Magic method for calling PDO directly via sfPDODatabase
|
|
|
105 |
*
|
|
|
106 |
* @param string $method
|
|
|
107 |
* @param array $arguments
|
|
|
108 |
* @return mixed
|
|
|
109 |
*/
|
|
|
110 |
public function __call($method, $arguments)
|
|
|
111 |
{
|
|
|
112 |
return $this->getConnection()->$method($arguments);
|
|
|
113 |
}
|
|
|
114 |
}
|