/vendor/laravel/framework/src/Illuminate/Database/Connectors/SqlServerConnector.php

https://gitlab.com/ducnv/BTL_CN_WEB · PHP · 145 lines · 75 code · 19 blank · 51 comment · 9 complexity · ebf726f8e6962c6706f2b0fa5fe5d20f MD5 · raw file

  1. <?php
  2. namespace Illuminate\Database\Connectors;
  3. use PDO;
  4. use Illuminate\Support\Arr;
  5. class SqlServerConnector extends Connector implements ConnectorInterface
  6. {
  7. /**
  8. * The PDO connection options.
  9. *
  10. * @var array
  11. */
  12. protected $options = [
  13. PDO::ATTR_CASE => PDO::CASE_NATURAL,
  14. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  15. PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
  16. PDO::ATTR_STRINGIFY_FETCHES => false,
  17. ];
  18. /**
  19. * Establish a database connection.
  20. *
  21. * @param array $config
  22. * @return \PDO
  23. */
  24. public function connect(array $config)
  25. {
  26. $options = $this->getOptions($config);
  27. return $this->createConnection($this->getDsn($config), $config, $options);
  28. }
  29. /**
  30. * Create a DSN string from a configuration.
  31. *
  32. * @param array $config
  33. * @return string
  34. */
  35. protected function getDsn(array $config)
  36. {
  37. // First we will create the basic DSN setup as well as the port if it is in
  38. // in the configuration options. This will give us the basic DSN we will
  39. // need to establish the PDO connections and return them back for use.
  40. if (in_array('dblib', $this->getAvailableDrivers())) {
  41. return $this->getDblibDsn($config);
  42. } else {
  43. return $this->getSqlSrvDsn($config);
  44. }
  45. }
  46. /**
  47. * Get the DSN string for a DbLib connection.
  48. *
  49. * @param array $config
  50. * @return string
  51. */
  52. protected function getDblibDsn(array $config)
  53. {
  54. $arguments = [
  55. 'host' => $this->buildHostString($config, ':'),
  56. 'dbname' => $config['database'],
  57. ];
  58. $arguments = array_merge(
  59. $arguments, Arr::only($config, ['appname', 'charset'])
  60. );
  61. return $this->buildConnectString('dblib', $arguments);
  62. }
  63. /**
  64. * Get the DSN string for a SqlSrv connection.
  65. *
  66. * @param array $config
  67. * @return string
  68. */
  69. protected function getSqlSrvDsn(array $config)
  70. {
  71. $arguments = [
  72. 'Server' => $this->buildHostString($config, ','),
  73. ];
  74. if (isset($config['database'])) {
  75. $arguments['Database'] = $config['database'];
  76. }
  77. if (isset($config['appname'])) {
  78. $arguments['APP'] = $config['appname'];
  79. }
  80. if (isset($config['readonly'])) {
  81. $arguments['ApplicationIntent'] = 'ReadOnly';
  82. }
  83. if (isset($config['pooling']) && $config['pooling'] === false) {
  84. $arguments['ConnectionPooling'] = '0';
  85. }
  86. return $this->buildConnectString('sqlsrv', $arguments);
  87. }
  88. /**
  89. * Build a connection string from the given arguments.
  90. *
  91. * @param string $driver
  92. * @param array $arguments
  93. * @return string
  94. */
  95. protected function buildConnectString($driver, array $arguments)
  96. {
  97. $options = array_map(function ($key) use ($arguments) {
  98. return sprintf('%s=%s', $key, $arguments[$key]);
  99. }, array_keys($arguments));
  100. return $driver.':'.implode(';', $options);
  101. }
  102. /**
  103. * Build a host string from the given configuration.
  104. *
  105. * @param array $config
  106. * @param string $separator
  107. * @return string
  108. */
  109. protected function buildHostString(array $config, $separator)
  110. {
  111. if (isset($config['port'])) {
  112. return $config['host'].$separator.$config['port'];
  113. } else {
  114. return $config['host'];
  115. }
  116. }
  117. /**
  118. * Get the available PDO drivers.
  119. *
  120. * @return array
  121. */
  122. protected function getAvailableDrivers()
  123. {
  124. return PDO::getAvailableDrivers();
  125. }
  126. }