Esempio n. 1
0
        public override string ToSql()
        {
            StringBuilder sql      = new StringBuilder();
            string        includes = "";

            if ((Type == IndexTypeEnum.Clustered) && (IsUniqueKey))
            {
                sql.Append("CREATE UNIQUE CLUSTERED ");
            }
            if ((Type == IndexTypeEnum.Clustered) && (!IsUniqueKey))
            {
                sql.Append("CREATE CLUSTERED ");
            }
            if ((Type == IndexTypeEnum.Nonclustered) && (IsUniqueKey))
            {
                sql.Append("CREATE UNIQUE NONCLUSTERED ");
            }
            if ((Type == IndexTypeEnum.Nonclustered) && (!IsUniqueKey))
            {
                sql.Append("CREATE NONCLUSTERED ");
            }
            if (Type == IndexTypeEnum.XML)
            {
                sql.Append("CREATE PRIMARY XML ");
            }
            sql.Append("INDEX [" + Name + "] ON " + Parent.FullName + "\r\n(\r\n");
            /*Ordena la coleccion de campos del Indice en funcion de la propieda IsIncluded*/
            Columns.Sort();
            for (int j = 0; j < Columns.Count; j++)
            {
                if (!Columns[j].IsIncluded)
                {
                    sql.Append("\t[" + Columns[j].Name + "]");
                    if (Type != IndexTypeEnum.XML)
                    {
                        if (Columns[j].Order)
                        {
                            sql.Append(" DESC");
                        }
                        else
                        {
                            sql.Append(" ASC");
                        }
                    }
                    if (j < Columns.Count - 1)
                    {
                        sql.Append(",");
                    }
                    sql.Append("\r\n");
                }
                else
                {
                    if (String.IsNullOrEmpty(includes))
                    {
                        includes = ") INCLUDE (";
                    }
                    includes += "[" + Columns[j].Name + "],";
                }
            }
            if (!String.IsNullOrEmpty(includes))
            {
                includes = includes.Substring(0, includes.Length - 1);
            }
            sql.Append(includes);
            sql.Append(")");
            if (!String.IsNullOrEmpty(FilterDefintion))
            {
                sql.Append("\r\n WHERE " + FilterDefintion + "\r\n");
            }
            sql.Append(" WITH (");
            if (Parent.ObjectType == Enums.ObjectType.TableType)
            {
                if ((IgnoreDupKey) && (IsUniqueKey))
                {
                    sql.Append("IGNORE_DUP_KEY = ON ");
                }
                else
                {
                    sql.Append("IGNORE_DUP_KEY  = OFF ");
                }
            }
            else
            {
                if (IsPadded)
                {
                    sql.Append("PAD_INDEX = ON, ");
                }
                else
                {
                    sql.Append("PAD_INDEX  = OFF, ");
                }
                if (IsAutoStatistics)
                {
                    sql.Append("STATISTICS_NORECOMPUTE = ON, ");
                }
                else
                {
                    sql.Append("STATISTICS_NORECOMPUTE  = OFF, ");
                }
                if (Type != IndexTypeEnum.XML)
                {
                    if ((IgnoreDupKey) && (IsUniqueKey))
                    {
                        sql.Append("IGNORE_DUP_KEY = ON, ");
                    }
                    else
                    {
                        sql.Append("IGNORE_DUP_KEY  = OFF, ");
                    }
                }
                if (AllowRowLocks)
                {
                    sql.Append("ALLOW_ROW_LOCKS = ON, ");
                }
                else
                {
                    sql.Append("ALLOW_ROW_LOCKS  = OFF, ");
                }
                if (AllowPageLocks)
                {
                    sql.Append("ALLOW_PAGE_LOCKS = ON");
                }
                else
                {
                    sql.Append("ALLOW_PAGE_LOCKS  = OFF");
                }
                if (FillFactor != 0)
                {
                    sql.Append(", FILLFACTOR = " + FillFactor.ToString());
                }
            }
            sql.Append(")");
            if (!String.IsNullOrEmpty(FileGroup))
            {
                sql.Append(" ON [" + FileGroup + "]");
            }
            sql.Append("\r\nGO\r\n");
            if (IsDisabled)
            {
                sql.Append("ALTER INDEX [" + Name + "] ON " + ((Table)Parent).FullName + " DISABLE\r\nGO\r\n");
            }

            sql.Append(ExtendedProperties.ToSql());
            return(sql.ToString());
        }