Exemple #1
0
        /// <summary>
        /// Devuelve el schema de la tabla en formato SQL.
        /// </summary>
        public string ToSql(Boolean showFK)
        {
            Database    database = null;
            ISchemaBase current  = this;

            while (database == null && current.Parent != null)
            {
                database = current.Parent as Database;
                current  = current.Parent;
            }

            if (database == null)
            {
                return(string.Empty);
            }

            var isAzure10 = database.Info.Version == DatabaseInfo.VersionTypeEnum.SQLServerAzure10;

            string sql   = "";
            string sqlPK = "";
            string sqlUC = "";
            string sqlFK = "";

            if (Columns.Any())
            {
                sql += "CREATE TABLE " + FullName + "\r\n(\r\n";
                sql += Columns.ToSql();
                if (Constraints.Any())
                {
                    sql += ",\r\n";
                    Constraints.AsQueryable()
                    // Add the constraint if it's not in DropStatus
                    .Where(c => !c.HasState(ObjectStatus.Drop))
                    .ToList()
                    .ForEach(item =>
                    {
                        if (item.Type == Constraint.ConstraintType.PrimaryKey)
                        {
                            sqlPK += "\t" + item.ToSql() + ",\r\n";
                        }
                        if (item.Type == Constraint.ConstraintType.Unique)
                        {
                            sqlUC += "\t" + item.ToSql() + ",\r\n";
                        }
                        if (showFK && item.Type == Constraint.ConstraintType.ForeignKey)
                        {
                            sqlFK += "\t" + item.ToSql() + ",\r\n";
                        }
                    });
                    sql += sqlPK + sqlUC + sqlFK;
                    sql  = sql.Substring(0, sql.Length - 3) + "\r\n";
                }
                else
                {
                    sql += "\r\n";
                    if (!String.IsNullOrEmpty(CompressType))
                    {
                        sql += "WITH (DATA_COMPRESSION = " + CompressType + ")\r\n";
                    }
                }
                sql += ")";

                if (!isAzure10)
                {
                    if (!String.IsNullOrEmpty(FileGroup))
                    {
                        sql += " ON [" + FileGroup + "]";
                    }

                    if (!String.IsNullOrEmpty(FileGroupText))
                    {
                        if (HasBlobColumn)
                        {
                            sql += " TEXTIMAGE_ON [" + FileGroupText + "]";
                        }
                    }
                    if ((!String.IsNullOrEmpty(FileGroupStream)) && (HasFileStream))
                    {
                        sql += " FILESTREAM_ON [" + FileGroupStream + "]";
                    }
                }
                sql += "\r\n";
                sql += "GO\r\n";
                Constraints.ForEach(item =>
                {
                    if (item.Type == Constraint.ConstraintType.Check)
                    {
                        sql += item.ToSqlAdd() + "\r\n";
                    }
                });
                if (HasChangeTracking)
                {
                    sql += ToSqlChangeTracking();
                }
                sql += Indexes.ToSql();
                sql += FullTextIndex.ToSql();
                sql += Options.ToSql();
                sql += Triggers.ToSql();
            }
            return(sql);
        }