Driver.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\DrizzlePDOMySql;
  20. /**
  21. * Drizzle driver using PDO MySql.
  22. *
  23. * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
  24. */
  25. class Driver implements \Doctrine\DBAL\Driver
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  31. {
  32. $conn = new Connection(
  33. $this->_constructPdoDsn($params),
  34. $username,
  35. $password,
  36. $driverOptions
  37. );
  38. return $conn;
  39. }
  40. /**
  41. * Constructs the Drizzle MySql PDO DSN.
  42. *
  43. * @return string The DSN.
  44. */
  45. private function _constructPdoDsn(array $params)
  46. {
  47. $dsn = 'mysql:';
  48. if (isset($params['host']) && $params['host'] != '') {
  49. $dsn .= 'host=' . $params['host'] . ';';
  50. }
  51. if (isset($params['port'])) {
  52. $dsn .= 'port=' . $params['port'] . ';';
  53. }
  54. if (isset($params['dbname'])) {
  55. $dsn .= 'dbname=' . $params['dbname'] . ';';
  56. }
  57. if (isset($params['unix_socket'])) {
  58. $dsn .= 'unix_socket=' . $params['unix_socket'] . ';';
  59. }
  60. return $dsn;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function getDatabasePlatform()
  66. {
  67. return new \Doctrine\DBAL\Platforms\DrizzlePlatform();
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
  73. {
  74. return new \Doctrine\DBAL\Schema\DrizzleSchemaManager($conn);
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function getName()
  80. {
  81. return 'drizzle_pdo_mysql';
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  87. {
  88. $params = $conn->getParams();
  89. return $params['dbname'];
  90. }
  91. }