/libraries/dabl/database/propel/platform/MssqlPlatform.php

https://github.com/coleHafner/coleandheather_dabl · PHP · 185 lines · 144 code · 20 blank · 21 comment · 13 complexity · 98de5215140223e41b01dd0ac8b4e1a2 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. require_once dirname(__FILE__) . '/DefaultPlatform.php';
  10. require_once dirname(__FILE__) . '/../model/Domain.php';
  11. /**
  12. * MS SQL PropelPlatformInterface implementation.
  13. *
  14. * @author Hans Lellelid <hans@xmpl.org> (Propel)
  15. * @author Martin Poeschl <mpoeschl@marmot.at> (Torque)
  16. * @version $Revision: 2194 $
  17. * @package propel.generator.platform
  18. */
  19. class MssqlPlatform extends DefaultPlatform
  20. {
  21. protected static $dropCount = 0;
  22. /**
  23. * Initializes db specific domain mapping.
  24. */
  25. protected function initialize()
  26. {
  27. parent::initialize();
  28. $this->setSchemaDomainMapping(new Domain(PropelTypes::INTEGER, "INT"));
  29. $this->setSchemaDomainMapping(new Domain(PropelTypes::BOOLEAN, "INT"));
  30. $this->setSchemaDomainMapping(new Domain(PropelTypes::DOUBLE, "FLOAT"));
  31. $this->setSchemaDomainMapping(new Domain(PropelTypes::LONGVARCHAR, "VARCHAR(MAX)"));
  32. $this->setSchemaDomainMapping(new Domain(PropelTypes::CLOB, "VARCHAR(MAX)"));
  33. $this->setSchemaDomainMapping(new Domain(PropelTypes::DATE, "DATETIME"));
  34. $this->setSchemaDomainMapping(new Domain(PropelTypes::BU_DATE, "DATETIME"));
  35. $this->setSchemaDomainMapping(new Domain(PropelTypes::TIME, "DATETIME"));
  36. $this->setSchemaDomainMapping(new Domain(PropelTypes::TIMESTAMP, "DATETIME"));
  37. $this->setSchemaDomainMapping(new Domain(PropelTypes::BU_TIMESTAMP, "DATETIME"));
  38. $this->setSchemaDomainMapping(new Domain(PropelTypes::BINARY, "BINARY(7132)"));
  39. $this->setSchemaDomainMapping(new Domain(PropelTypes::VARBINARY, "VARBINARY(MAX)"));
  40. $this->setSchemaDomainMapping(new Domain(PropelTypes::LONGVARBINARY, "VARBINARY(MAX)"));
  41. $this->setSchemaDomainMapping(new Domain(PropelTypes::BLOB, "VARBINARY(MAX)"));
  42. $this->setSchemaDomainMapping(new Domain(PropelTypes::OBJECT, "VARCHAR(MAX)"));
  43. $this->setSchemaDomainMapping(new Domain(PropelTypes::PHP_ARRAY, "VARCHAR(MAX)"));
  44. $this->setSchemaDomainMapping(new Domain(PropelTypes::ENUM, "TINYINT"));
  45. }
  46. public function getMaxColumnNameLength()
  47. {
  48. return 128;
  49. }
  50. public function getNullString($notNull)
  51. {
  52. return ($notNull ? "NOT NULL" : "NULL");
  53. }
  54. public function supportsNativeDeleteTrigger()
  55. {
  56. return true;
  57. }
  58. public function supportsInsertNullPk()
  59. {
  60. return false;
  61. }
  62. public function getDropTableDDL(Table $table)
  63. {
  64. $ret = '';
  65. foreach ($table->getForeignKeys() as $fk) {
  66. $ret .= "
  67. IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='" . $fk->getName() . "')
  68. ALTER TABLE " . $this->quoteIdentifier($table->getName()) . " DROP CONSTRAINT " . $this->quoteIdentifier($fk->getName()) . ";
  69. ";
  70. }
  71. self::$dropCount++;
  72. $ret .= "
  73. IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = '" . $table->getName() . "')
  74. BEGIN
  75. DECLARE @reftable_" . self::$dropCount . " nvarchar(60), @constraintname_" . self::$dropCount . " nvarchar(60)
  76. DECLARE refcursor CURSOR FOR
  77. select reftables.name tablename, cons.name constraintname
  78. from sysobjects tables,
  79. sysobjects reftables,
  80. sysobjects cons,
  81. sysreferences ref
  82. where tables.id = ref.rkeyid
  83. and cons.id = ref.constid
  84. and reftables.id = ref.fkeyid
  85. and tables.name = '" . $table->getName() . "'
  86. OPEN refcursor
  87. FETCH NEXT from refcursor into @reftable_" . self::$dropCount . ", @constraintname_" . self::$dropCount . "
  88. while @@FETCH_STATUS = 0
  89. BEGIN
  90. exec ('alter table '+@reftable_" . self::$dropCount . "+' drop constraint '+@constraintname_" . self::$dropCount . ")
  91. FETCH NEXT from refcursor into @reftable_" . self::$dropCount . ", @constraintname_" . self::$dropCount . "
  92. END
  93. CLOSE refcursor
  94. DEALLOCATE refcursor
  95. DROP TABLE " . $this->quoteIdentifier($table->getName()) . "
  96. END
  97. ";
  98. return $ret;
  99. }
  100. public function getPrimaryKeyDDL(Table $table)
  101. {
  102. if ($table->hasPrimaryKey()) {
  103. $pattern = 'CONSTRAINT %s PRIMARY KEY (%s)';
  104. return sprintf($pattern,
  105. $this->quoteIdentifier($this->getPrimaryKeyName($table)),
  106. $this->getColumnListDDL($table->getPrimaryKey())
  107. );
  108. }
  109. }
  110. public function getAddForeignKeyDDL(ForeignKey $fk)
  111. {
  112. if ($fk->isSkipSql()) {
  113. return;
  114. }
  115. $pattern = "
  116. BEGIN
  117. ALTER TABLE %s ADD %s
  118. END
  119. ;
  120. ";
  121. return sprintf($pattern,
  122. $this->quoteIdentifier($fk->getTable()->getName()),
  123. $this->getForeignKeyDDL($fk)
  124. );
  125. }
  126. public function getForeignKeyDDL(ForeignKey $fk)
  127. {
  128. if ($fk->isSkipSql()) {
  129. return;
  130. }
  131. $pattern = 'CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)';
  132. $script = sprintf($pattern,
  133. $this->quoteIdentifier($fk->getName()),
  134. $this->getColumnListDDL($fk->getLocalColumns()),
  135. $this->quoteIdentifier($fk->getForeignTableName()),
  136. $this->getColumnListDDL($fk->getForeignColumns())
  137. );
  138. if ($fk->hasOnUpdate() && $fk->getOnUpdate() != ForeignKey::SETNULL) {
  139. $script .= ' ON UPDATE ' . $fk->getOnUpdate();
  140. }
  141. if ($fk->hasOnDelete() && $fk->getOnDelete() != ForeignKey::SETNULL) {
  142. $script .= ' ON DELETE '. $fk->getOnDelete();
  143. }
  144. return $script;
  145. }
  146. /**
  147. * @see Platform::supportsSchemas()
  148. */
  149. public function supportsSchemas()
  150. {
  151. return true;
  152. }
  153. public function hasSize($sqlType)
  154. {
  155. return !("INT" == $sqlType || "TEXT" == $sqlType);
  156. }
  157. public function quoteIdentifier($text)
  158. {
  159. return $this->isIdentifierQuotingEnabled ? '[' . strtr($text, array('.' => '].[')) . ']' : $text;
  160. }
  161. public function getTimestampFormatter()
  162. {
  163. return 'Y-m-d H:i:s';
  164. }
  165. }