示例#1
0
        public override String CreateScript()
        {
            StringBuilder        builder         = new StringBuilder(String.Empty);
            IEnumerable <String> includedColumns = null;

            Boolean isXml = TypeStatement.ToLower().Equals("xml");

            if (isXml)
            {
                builder.AppendLine($"CREATE {( UsingXMLIndexId == 0 ? "PRIMARY" : "" )} XML INDEX {Name}");
                builder.Append($"ON {ParentObject.QualifiedName}");
            }
            else if (IsPrimaryKey)
            {
                builder.AppendFormat($"ALTER TABLE {ParentObject.QualifiedName} ADD CONSTRAINT [{Name}] PRIMARY KEY {TypeStatement}");
            }
            else if (IsUniqueConstraint)
            {
                builder.Append($"ALTER TABLE {ParentObject.QualifiedName} ADD CONSTRAINT [{Name}] UNIQUE {TypeStatement}");
            }
            else
            {
                if (IsUnique)
                {
                    builder.Append($"CREATE UNIQUE {TypeStatement} INDEX [{Name}] ON {ParentObject.QualifiedName}");
                }
                else
                {
                    builder.Append($"CREATE {TypeStatement} INDEX [{Name}] ON {ParentObject.QualifiedName}");
                }
                includedColumns = Columns.Where(c => c.IsIncluded).Select(c => $"[{c.Name}]");
            }



            builder.Append("\r\n(");
            if (isXml)
            {
                builder.Append(String.Join(",", Columns.Where(c => !c.IsIncluded).Select(c => $"[{c.Name}]")));
            }
            else
            {
                builder.Append(String.Join(",", Columns.Where(c => !c.IsIncluded).Select(c => c.ScriptStatement)));
            }
            builder.AppendLine(" )");

            if (includedColumns != null && includedColumns.Count() > 0)
            {
                builder.Append("INCLUDE (");
                builder.Append(String.Join(",", includedColumns));
                builder.AppendLine(" )");
            }
            if (isXml && UsingXMLIndexId != 0)
            {
                builder.AppendLine($"USING XML INDEX {UsingXMLIndex.Name} FOR {XMLSecondaryType} ");
            }

            builder.AppendLine("WITH ( ");
            builder.AppendFormat($"PAD_INDEX = {SqlStatement.GetOnOffStatement(IsPadded)}, ");
            if (!isXml)
            {
                builder.AppendFormat($"STATISTICS_NORECOMPUTE = {SqlStatement.GetOnOffStatement(IsAutoStatistics)}, ");
            }
            builder.AppendFormat($"IGNORE_DUP_KEY = {SqlStatement.GetOnOffStatement(IgnoreDupKey)}, ");
            builder.AppendFormat($"ALLOW_ROW_LOCKS = {SqlStatement.GetOnOffStatement(AllowRowLocks)}, ");
            builder.AppendFormat($"ALLOW_PAGE_LOCKS = {SqlStatement.GetOnOffStatement(AllowPageLocks)} ");
            if (FillFactor != 0)
            {
                builder.AppendFormat($", FILLFACTOR =  {FillFactor.ToString()}");
            }
            builder.Append(" ) ");
            //if (!String.IsNullOrEmpty(FileGroup) && !isXml) builder.Append(" ON [" + FileGroup + "]");

            builder.Append(SqlStatement.GO);
            return(builder.ToString());
        }