PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/database/sqlsrv/connection.php

http://github.com/fuel/core
PHP | 170 lines | 111 code | 13 blank | 46 comment | 12 complexity | 88427db46b867f461e9759814c271330 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP 5.4+ framework.
  4. *
  5. * @package Fuel
  6. * @version 1.9-dev
  7. * @author Fuel Development Team
  8. * @author cocteau666@gmail.com
  9. * @license MIT License
  10. * @copyright 2010 - 2019 Fuel Development Team
  11. * @copyright 2008 - 2009 Kohana Team
  12. * @link https://fuelphp.com
  13. */
  14. namespace Fuel\Core;
  15. class Database_Sqlsrv_Connection extends \Database_PDO_Connection
  16. {
  17. /**
  18. * Stores the database configuration locally and name the instance.
  19. *
  20. * [!!] This method cannot be accessed directly, you must use [static::instance].
  21. *
  22. * @param string $name
  23. * @param array $config
  24. */
  25. protected function __construct($name, array $config)
  26. {
  27. // this driver only works on Windows
  28. if ( ! is_windows())
  29. {
  30. throw new \Database_Exception('The "SQLSRV" database driver works only on Windows. On *nix, use the "DBLib" driver instead.');
  31. }
  32. parent::__construct($name, $config);
  33. }
  34. /**
  35. * List tables
  36. *
  37. * @param string $like
  38. *
  39. * @throws \FuelException
  40. */
  41. public function list_tables($like = null)
  42. {
  43. $query = "SELECT name FROM sys.objects WHERE type = 'U' AND name != 'sysdiagrams'";
  44. if (is_string($like))
  45. {
  46. $query .= " AND name LIKE ".$this->quote($like);
  47. }
  48. // Find all table names
  49. $result = $this->query(\DB::SELECT, $query, false);
  50. $tables = array();
  51. foreach ($result as $row)
  52. {
  53. $tables[] = reset($row);
  54. }
  55. return $tables;
  56. }
  57. /**
  58. * List table columns
  59. *
  60. * @param string $table table name
  61. * @param string $like column name pattern
  62. * @return array array of column structure
  63. */
  64. public function list_columns($table, $like = null)
  65. {
  66. $query = "SELECT * FROM Sys.Columns WHERE id = object_id('" . $this->quote_table($table) . "')";
  67. if (is_string($like))
  68. {
  69. // Search for column names
  70. $query .= " AND name LIKE ".$this->quote($like);
  71. }
  72. $count = 0;
  73. $columns = array();
  74. foreach ($result as $row)
  75. {
  76. list($type, $length) = $this->_parse_type($row['Type']);
  77. $column = $this->datatype($type);
  78. $column['name'] = $row['Field'];
  79. $column['default'] = $row['Default'];
  80. $column['data_type'] = $type;
  81. $column['null'] = ($row['Null'] == 'YES');
  82. $column['ordinal_position'] = ++$count;
  83. switch ($column['type'])
  84. {
  85. case 'float':
  86. if (isset($length))
  87. {
  88. list($column['numeric_precision'], $column['numeric_scale']) = explode(',', $length);
  89. }
  90. break;
  91. case 'int':
  92. if (isset($length))
  93. {
  94. $column['display'] = $length;
  95. }
  96. break;
  97. case 'string':
  98. switch ($column['data_type'])
  99. {
  100. case 'binary':
  101. case 'varbinary':
  102. $column['character_maximum_length'] = $length;
  103. break;
  104. case 'char':
  105. case 'varchar':
  106. $column['character_maximum_length'] = $length;
  107. case 'text':
  108. case 'tinytext':
  109. case 'mediumtext':
  110. case 'longtext':
  111. $column['collation_name'] = $row['Collation'];
  112. break;
  113. case 'enum':
  114. case 'set':
  115. $column['collation_name'] = $row['Collation'];
  116. $column['options'] = explode('\',\'', substr($length, 1, -1));
  117. break;
  118. }
  119. break;
  120. }
  121. $column['comment'] = $row['Comment'];
  122. $column['extra'] = $row['Extra'];
  123. $column['key'] = $row['Key'];
  124. $column['privileges'] = $row['Privileges'];
  125. $columns[$row['Field']] = $column;
  126. }
  127. return $columns;
  128. }
  129. /**
  130. * Set the charset
  131. *
  132. * @param string $charset
  133. */
  134. public function set_charset($charset)
  135. {
  136. if ($charset == 'utf8' or $charset = 'utf-8')
  137. {
  138. // use utf8 encoding
  139. $this->_connection->setAttribute(\PDO::SQLSRV_ATTR_ENCODING, \PDO::SQLSRV_ENCODING_UTF8);
  140. }
  141. elseif ($charset == 'system')
  142. {
  143. // use system encoding
  144. $this->_connection->setAttribute(\PDO::SQLSRV_ATTR_ENCODING, \PDO::SQLSRV_ENCODING_SYSTEM);
  145. }
  146. elseif (is_numeric($charset))
  147. {
  148. // charset code passed directly
  149. $this->_connection->setAttribute(\PDO::SQLSRV_ATTR_ENCODING, $charset);
  150. }
  151. else
  152. {
  153. // unknown charset, use the default encoding
  154. $this->_connection->setAttribute(\PDO::SQLSRV_ATTR_ENCODING, \PDO::SQLSRV_ENCODING_DEFAULT);
  155. }
  156. }
  157. }