PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/bluebox/libraries/doctrine/lib/Doctrine/Import/Mssql.php

https://github.com/robertleeplummerjr/bluebox
PHP | 248 lines | 121 code | 30 blank | 97 comment | 13 complexity | 50e7407330df8318e318fb008bb44be6 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: Mssql.php 5847 2009-06-09 08:13:25Z jwage $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.phpdoctrine.org>.
  20. */
  21. /**
  22. * @package Doctrine
  23. * @subpackage Import
  24. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  25. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  26. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  27. * @author Frank M. Kromann <frank@kromann.info> (PEAR MDB2 Mssql driver)
  28. * @author David Coallier <davidc@php.net> (PEAR MDB2 Mssql driver)
  29. * @version $Revision: 5847 $
  30. * @link www.phpdoctrine.org
  31. * @since 1.0
  32. */
  33. class Doctrine_Import_Mssql extends Doctrine_Import
  34. {
  35. /**
  36. * lists all database sequences
  37. *
  38. * @param string|null $database
  39. * @return array
  40. */
  41. public function listSequences($database = null)
  42. {
  43. $query = "SELECT name FROM sysobjects WHERE xtype = 'U'";
  44. $tableNames = $this->conn->fetchColumn($query);
  45. return array_map(array($this->conn->formatter, 'fixSequenceName'), $tableNames);
  46. }
  47. /**
  48. * lists table relations
  49. *
  50. * Expects an array of this format to be returned with all the relationships in it where the key is
  51. * the name of the foreign table, and the value is an array containing the local and foreign column
  52. * name
  53. *
  54. * Array
  55. * (
  56. * [groups] => Array
  57. * (
  58. * [local] => group_id
  59. * [foreign] => id
  60. * )
  61. * )
  62. *
  63. * @param string $table database table name
  64. * @return array
  65. */
  66. public function listTableRelations($tableName)
  67. {
  68. $relations = array();
  69. $sql = 'SELECT o1.name as table_name, c1.name as column_name, o2.name as referenced_table_name, c2.name as referenced_column_name, s.name as constraint_name FROM sysforeignkeys fk inner join sysobjects o1 on fk.fkeyid = o1.id inner join sysobjects o2 on fk.rkeyid = o2.id inner join syscolumns c1 on c1.id = o1.id and c1.colid = fk.fkey inner join syscolumns c2 on c2.id = o2.id and c2.colid = fk.rkey inner join sysobjects s on fk.constid = s.id AND o1.name = \'' . $tableName . '\'';
  70. $results = $this->conn->fetchAssoc($sql);
  71. foreach ($results as $result)
  72. {
  73. $result = array_change_key_case($result, CASE_LOWER);
  74. $relations[] = array('table' => $result['referenced_table_name'],
  75. 'local' => $result['column_name'],
  76. 'foreign' => $result['referenced_column_name']);
  77. }
  78. return $relations;
  79. }
  80. /**
  81. * lists table constraints
  82. *
  83. * @param string $table database table name
  84. * @return array
  85. */
  86. public function listTableColumns($table)
  87. {
  88. $sql = 'EXEC sp_primary_keys_rowset @table_name = ' . $this->conn->quoteIdentifier($table, true);
  89. $result = $this->conn->fetchAssoc($sql);
  90. $primary = array();
  91. foreach ($result as $key => $val) {
  92. $primary[] = $val['COLUMN_NAME'];
  93. }
  94. $sql = 'EXEC sp_columns @table_name = ' . $this->conn->quoteIdentifier($table, true);
  95. $result = $this->conn->fetchAssoc($sql);
  96. $columns = array();
  97. foreach ($result as $key => $val) {
  98. $val = array_change_key_case($val, CASE_LOWER);
  99. if (strstr($val['type_name'], ' ')) {
  100. list($type, $identity) = explode(' ', $val['type_name']);
  101. } else {
  102. $type = $val['type_name'];
  103. $identity = '';
  104. }
  105. if ($type == 'varchar') {
  106. $type .= '(' . $val['length'] . ')';
  107. }
  108. $val['type'] = $type;
  109. $val['identity'] = $identity;
  110. $decl = $this->conn->dataDict->getPortableDeclaration($val);
  111. $isIdentity = (bool) (strtoupper(trim($identity)) == 'IDENTITY');
  112. $isNullable = (bool) (strtoupper(trim($val['is_nullable'])) == 'NO');
  113. $isPrimary = in_array($val['column_name'], $primary);
  114. $description = array(
  115. 'name' => $val['column_name'],
  116. 'ntype' => $type,
  117. 'type' => $decl['type'][0],
  118. 'alltypes' => $decl['type'],
  119. 'length' => $decl['length'],
  120. 'fixed' => $decl['fixed'],
  121. 'unsigned' => $decl['unsigned'],
  122. 'notnull' => $isIdentity ? true : $isNullable,
  123. 'default' => $val['column_def'],
  124. 'primary' => $isPrimary,
  125. 'autoincrement' => $isIdentity,
  126. );
  127. $columns[$val['column_name']] = $description;
  128. }
  129. return $columns;
  130. }
  131. /**
  132. * lists table constraints
  133. *
  134. * @param string $table database table name
  135. * @return array
  136. */
  137. public function listTableIndexes($table)
  138. {
  139. }
  140. /**
  141. * lists tables
  142. *
  143. * @param string|null $database
  144. * @return array
  145. */
  146. public function listTables($database = null)
  147. {
  148. $sql = "SELECT name FROM sysobjects WHERE type = 'U' AND name <> 'dtproperties' AND name <> 'sysdiagrams' ORDER BY name";
  149. return $this->conn->fetchColumn($sql);
  150. }
  151. /**
  152. * lists all triggers
  153. *
  154. * @return array
  155. */
  156. public function listTriggers($database = null)
  157. {
  158. $query = "SELECT name FROM sysobjects WHERE xtype = 'TR'";
  159. $result = $this->conn->fetchColumn($query);
  160. return $result;
  161. }
  162. /**
  163. * lists table triggers
  164. *
  165. * @param string $table database table name
  166. * @return array
  167. */
  168. public function listTableTriggers($table)
  169. {
  170. $table = $this->conn->quote($table, 'text');
  171. $query = "SELECT name FROM sysobjects WHERE xtype = 'TR' AND object_name(parent_obj) = " . $table;
  172. $result = $this->conn->fetchColumn($query);
  173. return $result;
  174. }
  175. /**
  176. * lists table views
  177. *
  178. * @param string $table database table name
  179. * @return array
  180. */
  181. public function listTableViews($table)
  182. {
  183. $keyName = 'INDEX_NAME';
  184. $pkName = 'PK_NAME';
  185. if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) {
  186. if ($this->conn->getAttribute(Doctrine::ATTR_FIELD_CASE) == CASE_LOWER) {
  187. $keyName = strtolower($keyName);
  188. $pkName = strtolower($pkName);
  189. } else {
  190. $keyName = strtoupper($keyName);
  191. $pkName = strtoupper($pkName);
  192. }
  193. }
  194. $table = $this->conn->quote($table, 'text');
  195. $query = 'EXEC sp_statistics @table_name = ' . $table;
  196. $indexes = $this->conn->fetchColumn($query, $keyName);
  197. $query = 'EXEC sp_pkeys @table_name = ' . $table;
  198. $pkAll = $this->conn->fetchColumn($query, $pkName);
  199. $result = array();
  200. foreach ($indexes as $index) {
  201. if ( ! in_array($index, $pkAll) && $index != null) {
  202. $result[] = $this->conn->formatter->fixIndexName($index);
  203. }
  204. }
  205. return $result;
  206. }
  207. /**
  208. * lists database views
  209. *
  210. * @param string|null $database
  211. * @return array
  212. */
  213. public function listViews($database = null)
  214. {
  215. $query = "SELECT name FROM sysobjects WHERE xtype = 'V'";
  216. return $this->conn->fetchColumn($query);
  217. }
  218. }