Driver.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. namespace Doctrine\DBAL\Driver\PDOMySql;
  20. use Doctrine\DBAL\Connection;
  21. /**
  22. * PDO MySql driver.
  23. *
  24. * @since 2.0
  25. */
  26. class Driver implements \Doctrine\DBAL\Driver
  27. {
  28. /**
  29. * Attempts to establish a connection with the underlying driver.
  30. *
  31. * @param array $params
  32. * @param string $username
  33. * @param string $password
  34. * @param array $driverOptions
  35. * @return Doctrine\DBAL\Driver\Connection
  36. */
  37. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  38. {
  39. $conn = new \Doctrine\DBAL\Driver\PDOConnection(
  40. $this->_constructPdoDsn($params),
  41. $username,
  42. $password,
  43. $driverOptions
  44. );
  45. return $conn;
  46. }
  47. /**
  48. * Constructs the MySql PDO DSN.
  49. *
  50. * @return string The DSN.
  51. */
  52. private function _constructPdoDsn(array $params)
  53. {
  54. $dsn = 'mysql:';
  55. if (isset($params['host']) && $params['host'] != '') {
  56. $dsn .= 'host=' . $params['host'] . ';';
  57. }
  58. if (isset($params['port'])) {
  59. $dsn .= 'port=' . $params['port'] . ';';
  60. }
  61. if (isset($params['dbname'])) {
  62. $dsn .= 'dbname=' . $params['dbname'] . ';';
  63. }
  64. if (isset($params['unix_socket'])) {
  65. $dsn .= 'unix_socket=' . $params['unix_socket'] . ';';
  66. }
  67. if (isset($params['charset'])) {
  68. $dsn .= 'charset=' . $params['charset'] . ';';
  69. }
  70. return $dsn;
  71. }
  72. public function getDatabasePlatform()
  73. {
  74. return new \Doctrine\DBAL\Platforms\MySqlPlatform();
  75. }
  76. public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
  77. {
  78. return new \Doctrine\DBAL\Schema\MySqlSchemaManager($conn);
  79. }
  80. public function getName()
  81. {
  82. return 'pdo_mysql';
  83. }
  84. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  85. {
  86. $params = $conn->getParams();
  87. if (isset($params['dbname'])) {
  88. return $params['dbname'];
  89. }
  90. return $conn->query('SELECT DATABASE()')->fetchColumn();
  91. }
  92. }