PageRenderTime 154ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/FluentMigrator.Runner/Generators/SqlServer/SqlServer2000Generator.cs

https://github.com/timosaikkonen/fluentmigrator
C# | 213 lines | 156 code | 39 blank | 18 comment | 6 complexity | 21824bf5f068d11aa4f03cff3c355244 MD5 | raw file
  1. #region License
  2. //
  3. // Copyright (c) 2007-2009, Sean Chambers <schambers80@gmail.com>
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. #endregion
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.Text;
  22. using FluentMigrator.Expressions;
  23. using FluentMigrator.Runner.Extensions;
  24. using FluentMigrator.Runner.Generators.Generic;
  25. namespace FluentMigrator.Runner.Generators.SqlServer
  26. {
  27. public class SqlServer2000Generator : GenericGenerator
  28. {
  29. public SqlServer2000Generator()
  30. : base(new SqlServerColumn(new SqlServer2000TypeMap()), new SqlServerQuoter())
  31. {
  32. }
  33. protected SqlServer2000Generator(IColumn column)
  34. : base(column, new SqlServerQuoter())
  35. {
  36. }
  37. public override string RenameTable { get { return "sp_rename '{0}', '{1}'"; } }
  38. public override string RenameColumn { get { return "sp_rename '{0}.{1}', '{2}'"; } }
  39. public override string DropIndex { get { return "DROP INDEX {1}.{0}"; } }
  40. public override string AddColumn { get { return "ALTER TABLE {0} ADD {1}"; } }
  41. public virtual string IdentityInsert { get { return "SET IDENTITY_INSERT {0} {1}"; } }
  42. public override string CreateConstraint { get { return "ALTER TABLE {0} ADD CONSTRAINT {1} {2}{3} ({4})"; } }
  43. //Not need for the nonclusted keyword as it is the default mode
  44. public override string GetClusterTypeString(CreateIndexExpression column)
  45. {
  46. return column.Index.IsClustered ? "CLUSTERED " : string.Empty;
  47. }
  48. protected string GetConstraintClusteringString(CreateConstraintExpression constraint)
  49. {
  50. object indexType;
  51. if (!constraint.Constraint.AdditionalFeatures.TryGetValue(
  52. SqlServerExtensions.ConstraintType, out indexType)) return string.Empty;
  53. return (indexType.Equals(SqlServerConstraintType.Clustered)) ? " CLUSTERED" : " NONCLUSTERED";
  54. }
  55. public override string Generate(CreateConstraintExpression expression)
  56. {
  57. var constraintType = (expression.Constraint.IsPrimaryKeyConstraint) ? "PRIMARY KEY" : "UNIQUE";
  58. var constraintClustering = GetConstraintClusteringString(expression);
  59. string columns = String.Join(", ", expression.Constraint.Columns.Select(x => Quoter.QuoteColumnName(x)).ToArray());
  60. return string.Format(CreateConstraint, Quoter.QuoteTableName(expression.Constraint.TableName),
  61. Quoter.Quote(expression.Constraint.ConstraintName),
  62. constraintType,
  63. constraintClustering,
  64. columns);
  65. }
  66. public override string Generate(RenameTableExpression expression)
  67. {
  68. return String.Format(RenameTable, Quoter.QuoteTableName(Quoter.QuoteCommand(expression.OldName)), Quoter.QuoteCommand(expression.NewName));
  69. }
  70. public override string Generate(RenameColumnExpression expression)
  71. {
  72. return String.Format(RenameColumn, Quoter.QuoteTableName(expression.TableName), Quoter.QuoteColumnName(Quoter.QuoteCommand(expression.OldName)), Quoter.QuoteCommand(expression.NewName));
  73. }
  74. public override string Generate(DeleteColumnExpression expression)
  75. {
  76. // before we drop a column, we have to drop any default value constraints in SQL Server
  77. var builder = new StringBuilder();
  78. foreach (string column in expression.ColumnNames)
  79. {
  80. if (expression.ColumnNames.First() != column) builder.AppendLine("GO");
  81. BuildDelete(expression, column, builder);
  82. }
  83. return builder.ToString();
  84. }
  85. protected virtual void BuildDelete(DeleteColumnExpression expression, string columnName, StringBuilder builder)
  86. {
  87. builder.AppendLine(Generate(new DeleteDefaultConstraintExpression {
  88. ColumnName = columnName,
  89. SchemaName = expression.SchemaName,
  90. TableName = expression.TableName
  91. }));
  92. builder.AppendLine();
  93. builder.AppendLine(String.Format("-- now we can finally drop column\r\nALTER TABLE {0} DROP COLUMN {1};",
  94. Quoter.QuoteTableName(expression.TableName),
  95. Quoter.QuoteColumnName(columnName)));
  96. }
  97. public override string Generate(AlterDefaultConstraintExpression expression)
  98. {
  99. // before we alter a default constraint on a column, we have to drop any default value constraints in SQL Server
  100. var builder = new StringBuilder();
  101. builder.AppendLine(Generate(new DeleteDefaultConstraintExpression
  102. {
  103. ColumnName = expression.ColumnName,
  104. SchemaName = expression.SchemaName,
  105. TableName = expression.TableName
  106. }));
  107. builder.AppendLine();
  108. builder.Append(String.Format("-- create alter table command to create new default constraint as string and run it\r\nALTER TABLE {0} WITH NOCHECK ADD CONSTRAINT {3} DEFAULT({2}) FOR {1};",
  109. Quoter.QuoteTableName(expression.TableName),
  110. Quoter.QuoteColumnName(expression.ColumnName),
  111. Quoter.QuoteValue(expression.DefaultValue),
  112. Quoter.QuoteConstraintName(SqlServerColumn.GetDefaultConstraintName(expression.TableName, expression.ColumnName))));
  113. return builder.ToString();
  114. }
  115. public override string Generate(InsertDataExpression expression)
  116. {
  117. if (IsUsingIdentityInsert(expression))
  118. {
  119. return string.Format("{0}; {1}; {2}",
  120. string.Format(IdentityInsert, Quoter.QuoteTableName(expression.TableName), "ON"),
  121. base.Generate(expression),
  122. string.Format(IdentityInsert, Quoter.QuoteTableName(expression.TableName), "OFF"));
  123. }
  124. return base.Generate(expression);
  125. }
  126. protected static bool IsUsingIdentityInsert(InsertDataExpression expression)
  127. {
  128. if (expression.AdditionalFeatures.ContainsKey(SqlServerExtensions.IdentityInsert))
  129. {
  130. return (bool)expression.AdditionalFeatures[SqlServerExtensions.IdentityInsert];
  131. }
  132. return false;
  133. }
  134. public override string Generate(CreateSequenceExpression expression)
  135. {
  136. return compatabilityMode.HandleCompatabilty("Sequences are not supported in SqlServer2000");
  137. }
  138. public override string Generate(DeleteSequenceExpression expression)
  139. {
  140. return compatabilityMode.HandleCompatabilty("Sequences are not supported in SqlServer2000");
  141. }
  142. public override string Generate(DeleteDefaultConstraintExpression expression)
  143. {
  144. const string sql =
  145. "DECLARE @default sysname, @sql nvarchar(4000);\r\n\r\n" +
  146. "-- get name of default constraint\r\n" +
  147. "SELECT @default = name\r\n" +
  148. "FROM sys.default_constraints\r\n" +
  149. "WHERE parent_object_id = object_id('{0}')\r\n" + "" +
  150. "AND type = 'D'\r\n" + "" +
  151. "AND parent_column_id = (\r\n" + "" +
  152. "SELECT column_id\r\n" +
  153. "FROM sys.columns\r\n" +
  154. "WHERE object_id = object_id('{0}')\r\n" +
  155. "AND name = '{1}'\r\n" +
  156. ");\r\n\r\n" +
  157. "-- create alter table command to drop contraint as string and run it\r\n" +
  158. "SET @sql = N'ALTER TABLE {0} DROP CONSTRAINT ' + @default;\r\n" +
  159. "EXEC sp_executesql @sql;";
  160. return String.Format(sql, Quoter.QuoteTableName(expression.TableName), expression.ColumnName);
  161. }
  162. public override bool IsAdditionalFeatureSupported(string feature)
  163. {
  164. return _supportedAdditionalFeatures.Any(x => x == feature);
  165. }
  166. private readonly IEnumerable<string> _supportedAdditionalFeatures = new List<string>
  167. {
  168. SqlServerExtensions.IdentityInsert,
  169. SqlServerExtensions.IdentitySeed,
  170. SqlServerExtensions.IdentityIncrement,
  171. SqlServerExtensions.ConstraintType
  172. };
  173. }
  174. }