1 | <?php |
---|
2 | /* |
---|
3 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
4 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
5 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
6 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
7 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
8 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
9 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
10 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
11 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
12 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
13 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
14 | * |
---|
15 | * This software consists of voluntary contributions made by many individuals |
---|
16 | * and is licensed under the LGPL. For more information, see |
---|
17 | * <http://www.doctrine-project.org>. |
---|
18 | */ |
---|
19 | |
---|
20 | namespace Doctrine\DBAL\Driver\PDOMySql; |
---|
21 | |
---|
22 | use Doctrine\DBAL\Connection; |
---|
23 | |
---|
24 | /** |
---|
25 | * PDO MySql driver. |
---|
26 | * |
---|
27 | * @since 2.0 |
---|
28 | */ |
---|
29 | class Driver implements \Doctrine\DBAL\Driver |
---|
30 | { |
---|
31 | /** |
---|
32 | * Attempts to establish a connection with the underlying driver. |
---|
33 | * |
---|
34 | * @param array $params |
---|
35 | * @param string $username |
---|
36 | * @param string $password |
---|
37 | * @param array $driverOptions |
---|
38 | * @return Doctrine\DBAL\Driver\Connection |
---|
39 | */ |
---|
40 | public function connect(array $params, $username = null, $password = null, array $driverOptions = array()) |
---|
41 | { |
---|
42 | $conn = new \Doctrine\DBAL\Driver\PDOConnection( |
---|
43 | $this->_constructPdoDsn($params), |
---|
44 | $username, |
---|
45 | $password, |
---|
46 | $driverOptions |
---|
47 | ); |
---|
48 | return $conn; |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * Constructs the MySql PDO DSN. |
---|
53 | * |
---|
54 | * @return string The DSN. |
---|
55 | */ |
---|
56 | private function _constructPdoDsn(array $params) |
---|
57 | { |
---|
58 | $dsn = 'mysql:'; |
---|
59 | if (isset($params['host']) && $params['host'] != '') { |
---|
60 | $dsn .= 'host=' . $params['host'] . ';'; |
---|
61 | } |
---|
62 | if (isset($params['port'])) { |
---|
63 | $dsn .= 'port=' . $params['port'] . ';'; |
---|
64 | } |
---|
65 | if (isset($params['dbname'])) { |
---|
66 | $dsn .= 'dbname=' . $params['dbname'] . ';'; |
---|
67 | } |
---|
68 | if (isset($params['unix_socket'])) { |
---|
69 | $dsn .= 'unix_socket=' . $params['unix_socket'] . ';'; |
---|
70 | } |
---|
71 | if (isset($params['charset'])) { |
---|
72 | $dsn .= 'charset=' . $params['charset'] . ';'; |
---|
73 | } |
---|
74 | |
---|
75 | return $dsn; |
---|
76 | } |
---|
77 | |
---|
78 | public function getDatabasePlatform() |
---|
79 | { |
---|
80 | return new \Doctrine\DBAL\Platforms\MySqlPlatform(); |
---|
81 | } |
---|
82 | |
---|
83 | public function getSchemaManager(\Doctrine\DBAL\Connection $conn) |
---|
84 | { |
---|
85 | return new \Doctrine\DBAL\Schema\MySqlSchemaManager($conn); |
---|
86 | } |
---|
87 | |
---|
88 | public function getName() |
---|
89 | { |
---|
90 | return 'pdo_mysql'; |
---|
91 | } |
---|
92 | |
---|
93 | public function getDatabase(\Doctrine\DBAL\Connection $conn) |
---|
94 | { |
---|
95 | $params = $conn->getParams(); |
---|
96 | |
---|
97 | if (isset($params['dbname'])) { |
---|
98 | return $params['dbname']; |
---|
99 | } |
---|
100 | return $conn->query('SELECT DATABASE()')->fetchColumn(); |
---|
101 | } |
---|
102 | } |
---|