PageRenderTime 29ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/propel-generator/classes/propel/engine/database/reverse/mssql/MssqlSchemaParser.php

https://github.com/IDCI-Consulting/WebsiteEval
PHP | 248 lines | 151 code | 34 blank | 63 comment | 11 complexity | e90b7ff733d1253805babdc254259b75 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: MssqlSchemaParser.php 1262 2009-10-26 20:54:39Z francois $
  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 please see
  19. * <http://propel.phpdb.org>.
  20. */
  21. require_once 'propel/engine/database/reverse/BaseSchemaParser.php';
  22. /**
  23. * Microsoft SQL Server database schema parser.
  24. *
  25. * @author Hans Lellelid <hans@xmpl.org>
  26. * @version $Revision: 1262 $
  27. * @package propel.engine.database.reverse.mssql
  28. */
  29. class MssqlSchemaParser extends BaseSchemaParser {
  30. /**
  31. * Map MSSQL native types to Propel types.
  32. * @var array
  33. */
  34. private static $mssqlTypeMap = array(
  35. "binary" => CreoleTypes::BINARY,
  36. "bit" => PropelTypes::BOOLEAN,
  37. "char" => PropelTypes::CHAR,
  38. "datetime" => PropelTypes::TIMESTAMP,
  39. "decimal() identity" => PropelTypes::DECIMAL,
  40. "decimal" => PropelTypes::DECIMAL,
  41. "image" => PropelTypes::LONGVARBINARY,
  42. "int" => PropelTypes::INTEGER,
  43. "int identity" => PropelTypes::INTEGER,
  44. "integer" => PropelTypes::INTEGER,
  45. "money" => PropelTypes::DECIMAL,
  46. "nchar" => PropelTypes::CHAR,
  47. "ntext" => PropelTypes::LONGVARCHAR,
  48. "numeric() identity" => PropelTypes::NUMERIC,
  49. "numeric" => PropelTypes::NUMERIC,
  50. "nvarchar" => PropelTypes::VARCHAR,
  51. "real" => PropelTypes::REAL,
  52. "float" => PropelTypes::FLOAT,
  53. "smalldatetime" => PropelTypes::TIMESTAMP,
  54. "smallint" => PropelTypes::SMALLINT,
  55. "smallint identity" => PropelTypes::SMALLINT,
  56. "smallmoney" => PropelTypes::DECIMAL,
  57. "sysname" => PropelTypes::VARCHAR,
  58. "text" => PropelTypes::LONGVARCHAR,
  59. "timestamp" => PropelTypes::BINARY,
  60. "tinyint identity" => PropelTypes::TINYINT,
  61. "tinyint" => PropelTypes::TINYINT,
  62. "uniqueidentifier" => PropelTypes::CHAR,
  63. "varbinary" => PropelTypes::VARBINARY,
  64. "varchar" => PropelTypes::VARCHAR,
  65. "uniqueidentifier" => PropelTypes::CHAR,
  66. // SQL Server 2000 only
  67. "bigint identity" => PropelTypes::BIGINT,
  68. "bigint" => PropelTypes::BIGINT,
  69. "sql_variant" => PropelTypes::VARCHAR,
  70. );
  71. /**
  72. * Gets a type mapping from native types to Propel types
  73. *
  74. * @return array
  75. */
  76. protected function getTypeMapping()
  77. {
  78. return self::$mssqlTypeMap;
  79. }
  80. /**
  81. *
  82. */
  83. public function parse(Database $database)
  84. {
  85. $stmt = $this->dbh->query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME <> 'dtproperties'");
  86. // First load the tables (important that this happen before filling out details of tables)
  87. $tables = array();
  88. while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
  89. $name = $row[0];
  90. $table = new Table($name);
  91. $database->addTable($table);
  92. $tables[] = $table;
  93. }
  94. // Now populate only columns.
  95. foreach ($tables as $table) {
  96. $this->addColumns($table);
  97. }
  98. // Now add indexes and constraints.
  99. foreach ($tables as $table) {
  100. $this->addForeignKeys($table);
  101. $this->addIndexes($table);
  102. $this->addPrimaryKey($table);
  103. }
  104. }
  105. /**
  106. * Adds Columns to the specified table.
  107. *
  108. * @param Table $table The Table model class to add columns to.
  109. */
  110. protected function addColumns(Table $table)
  111. {
  112. $stmt = $this->dbh->query("sp_columns '" . $table->getName() . "'");
  113. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  114. $name = $row['COLUMN_NAME'];
  115. $type = $row['TYPE_NAME'];
  116. $size = $row['LENGTH'];
  117. $is_nullable = $row['NULLABLE'];
  118. $default = $row['COLUMN_DEF'];
  119. $precision = $row['PRECISION'];
  120. $scale = $row['SCALE'];
  121. $autoincrement = false;
  122. if (strtolower($type) == "int identity") {
  123. $autoincrement = true;
  124. }
  125. $propelType = $this->getMappedPropelType($type);
  126. if (!$propelType) {
  127. $propelType = Column::DEFAULT_TYPE;
  128. $this->warn("Column [" . $table->getName() . "." . $name. "] has a column type (".$type.") that Propel does not support.");
  129. }
  130. $column = new Column($name);
  131. $column->setTable($table);
  132. $column->setDomainForType($propelType);
  133. // We may want to provide an option to include this:
  134. // $column->getDomain()->replaceSqlType($type);
  135. $column->getDomain()->replaceSize($size);
  136. $column->getDomain()->replaceScale($scale);
  137. if ($default !== null) {
  138. $column->getDomain()->setDefaultValue(new ColumnDefaultValue($default, ColumnDefaultValue::TYPE_VALUE));
  139. }
  140. $column->setAutoIncrement($autoincrement);
  141. $column->setNotNull(!$is_nullable);
  142. $table->addColumn($column);
  143. }
  144. } // addColumn()
  145. /**
  146. * Load foreign keys for this table.
  147. */
  148. protected function addForeignKeys(Table $table)
  149. {
  150. $database = $table->getDatabase();
  151. $stmt = $this->dbh->query("SELECT ccu1.TABLE_NAME, ccu1.COLUMN_NAME, ccu2.TABLE_NAME AS FK_TABLE_NAME, ccu2.COLUMN_NAME AS FK_COLUMN_NAME
  152. FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu1 INNER JOIN
  153. INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc1 ON tc1.CONSTRAINT_NAME = ccu1.CONSTRAINT_NAME AND
  154. CONSTRAINT_TYPE = 'Foreign Key' INNER JOIN
  155. INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc1 ON rc1.CONSTRAINT_NAME = tc1.CONSTRAINT_NAME INNER JOIN
  156. INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu2 ON ccu2.CONSTRAINT_NAME = rc1.UNIQUE_CONSTRAINT_NAME
  157. WHERE (ccu1.table_name = '".$table->getName()."')");
  158. $row = $stmt->fetch(PDO::FETCH_NUM);
  159. $foreignKeys = array(); // local store to avoid duplicates
  160. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  161. $lcol = $row['COLUMN_NAME'];
  162. $ftbl = $row['FK_TABLE_NAME'];
  163. $fcol = $row['FK_COLUMN_NAME'];
  164. $foreignTable = $database->getTable($ftbl);
  165. $foreignColumn = $foreignTable->getColumn($fcol);
  166. $localColumn = $table->getColumn($lcol);
  167. if (!isset($foreignKeys[$name])) {
  168. $fk = new ForeignKey($name);
  169. $fk->setForeignTableName($foreignTable->getName());
  170. //$fk->setOnDelete($fkactions['ON DELETE']);
  171. //$fk->setOnUpdate($fkactions['ON UPDATE']);
  172. $table->addForeignKey($fk);
  173. $foreignKeys[$name] = $fk;
  174. }
  175. $foreignKeys[$name]->addReference($localColumn, $foreignColumn);
  176. }
  177. }
  178. /**
  179. * Load indexes for this table
  180. */
  181. protected function addIndexes(Table $table)
  182. {
  183. $stmt = $this->dbh->query("sp_indexes_rowset " . $table->getName());
  184. $indexes = array();
  185. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  186. $colName = $row["COLUMN_NAME"];
  187. $name = $row['INDEX_NAME'];
  188. // FIXME -- Add UNIQUE support
  189. if (!isset($indexes[$name])) {
  190. $indexes[$name] = new Index($name);
  191. $table->addIndex($indexes[$name]);
  192. }
  193. $indexes[$name]->addColumn($table->getColumn($colName));
  194. }
  195. }
  196. /**
  197. * Loads the primary key for this table.
  198. */
  199. protected function addPrimaryKey(Table $table)
  200. {
  201. $stmt = $this->dbh->query("SELECT COLUMN_NAME
  202. FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
  203. INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ON
  204. INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_NAME = INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE.constraint_name
  205. WHERE (INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = 'PRIMARY KEY') AND
  206. (INFORMATION_SCHEMA.TABLE_CONSTRAINTS.TABLE_NAME = '".$table->getName()."')");
  207. // Loop through the returned results, grouping the same key_name together
  208. // adding each column for that key.
  209. while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
  210. $name = $row[0];
  211. $table->getColumn($name)->setPrimaryKey(true);
  212. }
  213. }
  214. }