public SqlUpdateCommand GenerateSqlCreateView(int databaseVersion, out int createOrderIndex) { createOrderIndex = 0; List <string> sqls = new List <string>(); if (IsView) { string viewSql = DatabaseDefinition.GetViewSql(TableName, databaseVersion, out createOrderIndex); if (viewSql != "") { // If we update, the view may be created in a version before StringBuilder sql = new StringBuilder(); sql.AppendLine("if exists (select * from sys.views where object_id = object_id(N'[dbo].[" + TableName + "View]'))"); sql.Append("drop view [" + TableName + "View]"); sqls.Add(sql.ToString()); sql = new StringBuilder(); sql.AppendLine("create view [" + TableName + "View] as"); sql.Append(viewSql); sqls.Add(sql.ToString()); } } return(new SqlUpdateCommand(sqls.ToArray())); }
/// <summary> /// Generates SQL to create the latest view until the specified database version. /// </summary> /// <param name="databaseVersion">The last database version to care about.</param> /// <param name="createdInDatabaseVersion">The database verision in which the view is defined.</param> /// <returns>A SQL command as a string. An empty string if no view definition could be found.</returns> public string[] GenerateSqlCreateLatestView(int databaseVersion, out int createdInDatabaseVersion, out int createOrderIndex) { createdInDatabaseVersion = 0; createOrderIndex = 0; List <string> sqls = new List <string>(); string viewSql = ""; int version = 0; while (version <= databaseVersion) { version++; int createOrderIndexTemp; string tmpViewSql = DatabaseDefinition.GetViewSql(TableName, version, out createOrderIndexTemp); if (tmpViewSql != "") { viewSql = tmpViewSql; createdInDatabaseVersion = version; createOrderIndex = createOrderIndexTemp; } } if (viewSql == "" && IsView) { throw new DatabaseSchemaException("View for table '" + TableName + "' is missing."); } if (viewSql != "") { // If we update, the view may be created in a version before StringBuilder sql = new StringBuilder(); sql.AppendLine("if exists (select * from sys.views where object_id = object_id(N'[dbo].[" + TableName + "View]'))"); sql.Append("drop view [" + TableName + "View]"); sqls.Add(sql.ToString()); sql = new StringBuilder(); sql.AppendLine("create view [" + TableName + "View] as"); sql.Append(viewSql); sqls.Add(sql.ToString()); } return(sqls.ToArray()); }