Driver.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 MIT license. 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. * {@inheritdoc}
  30. */
  31. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  32. {
  33. $conn = new \Doctrine\DBAL\Driver\PDOConnection(
  34. $this->_constructPdoDsn($params),
  35. $username,
  36. $password,
  37. $driverOptions
  38. );
  39. return $conn;
  40. }
  41. /**
  42. * Constructs the MySql PDO DSN.
  43. *
  44. * @param array $params
  45. *
  46. * @return string The DSN.
  47. */
  48. private function _constructPdoDsn(array $params)
  49. {
  50. $dsn = 'mysql:';
  51. if (isset($params['host']) && $params['host'] != '') {
  52. $dsn .= 'host=' . $params['host'] . ';';
  53. }
  54. if (isset($params['port'])) {
  55. $dsn .= 'port=' . $params['port'] . ';';
  56. }
  57. if (isset($params['dbname'])) {
  58. $dsn .= 'dbname=' . $params['dbname'] . ';';
  59. }
  60. if (isset($params['unix_socket'])) {
  61. $dsn .= 'unix_socket=' . $params['unix_socket'] . ';';
  62. }
  63. if (isset($params['charset'])) {
  64. $dsn .= 'charset=' . $params['charset'] . ';';
  65. }
  66. return $dsn;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function getDatabasePlatform()
  72. {
  73. return new \Doctrine\DBAL\Platforms\MySqlPlatform();
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
  79. {
  80. return new \Doctrine\DBAL\Schema\MySqlSchemaManager($conn);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function getName()
  86. {
  87. return 'pdo_mysql';
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  93. {
  94. $params = $conn->getParams();
  95. if (isset($params['dbname'])) {
  96. return $params['dbname'];
  97. }
  98. return $conn->query('SELECT DATABASE()')->fetchColumn();
  99. }
  100. }