public static BuildSelectList ( |
||
table | ||
model | ||
리턴 | string |
public static string GetBody(Table table, ModelRoot model) { var sb = new StringBuilder(); if (!table.Immutable) { if (table.AllowCreateAudit) { sb.AppendLine("if (@" + model.Database.CreatedDateColumnName + " IS NULL)"); sb.AppendLine("SET @" + model.Database.CreatedDateColumnName + " = " + model.GetSQLDefaultDate()); } if (table.AllowModifiedAudit) { sb.AppendLine("DECLARE @" + model.Database.ModifiedDateColumnName + " [DateTime]"); sb.AppendLine("SET @" + model.Database.ModifiedDateColumnName + " = " + model.GetSQLDefaultDate()); } foreach (var column in table.PrimaryKeyColumns.OrderBy(x => x.Name)) { if (column.Identity == IdentityTypeConstants.Code) { sb.AppendLine("SET @" + column.ToDatabaseCodeIdentifier() + " = (select case when max([" + column.DatabaseName + "]) is null then 1 else max([" + column.DatabaseName + "]) + 1 end from [" + Globals.GetTableDatabaseName(model, table) + "])"); } else if (column.Identity == IdentityTypeConstants.Database) { //sb.AppendLine("DECLARE @" + column.ToDatabaseCodeIdentifier() + " " + dc.DataType); } } if (table.ParentTable == null) { AppendInsertionStatement(sb, table, model); } else { var tableList = table.GetTableHierarchy(); foreach (var t in tableList) { AppendInsertionStatement(sb, t, model); //On the base table save the primary keys as variables if (t.ParentTable == null) { sb.Append(BuildInheritedPkBaseTableVariables(t, model)); } } } } sb.AppendLine(); sb.AppendLine("SELECT "); sb.AppendLine(Globals.BuildSelectList(table, model, true)); sb.AppendLine("FROM"); sb.AppendLine(table.GetFullHierarchyTableJoin()); sb.AppendLine("WHERE"); sb.AppendLine(" " + BuildInsertSelectWhereStatement(table, model) + ";"); return(sb.ToString()); }
public static string GetBody(Table table, ModelRoot model) { var sb = new StringBuilder(); if (table.AllowModifiedAudit) { sb.AppendLine("IF (@" + model.Database.ModifiedDateColumnName + " IS NULL)"); sb.AppendLine("SET @" + model.Database.ModifiedDateColumnName + " = " + model.GetSQLDefaultDate() + ";"); sb.AppendLine(); } sb.AppendLine("SET NOCOUNT ON;"); if (!table.Immutable) { var tableList = table.GetTableHierarchy(); foreach (var t in tableList) { //If there is nothing to set then do not do anything var setStatment = BuildSetStatement(t, model); if (!string.IsNullOrEmpty(setStatment)) { sb.AppendLine("UPDATE "); sb.AppendLine("\t[" + t.GetSQLSchema() + "].[" + t.DatabaseName + "] "); sb.AppendLine("SET"); sb.AppendLine(setStatment); sb.AppendLine("WHERE"); sb.AppendLine("\t" + BuildUpdateWhereStatement(t, model, ((table.GetAbsoluteBaseTable() == t) && table.AllowTimestamp))); sb.AppendLine(); sb.AppendLine("if (@@RowCount = 0) return;"); sb.AppendLine(); } } } sb.AppendLine("SELECT"); sb.Append(Globals.BuildSelectList(table, model, true)); sb.AppendLine("FROM "); sb.AppendLine(table.GetFullHierarchyTableJoin()); sb.AppendLine("WHERE"); sb.AppendLine("\t" + BuildSelectWhereStatement(table, model)); return(sb.ToString()); }
private static string BuildStoredProcedure(Table table, ModelRoot model, List <Column> allColumns) { var sb = new StringBuilder(); var index = 0; sb.AppendLine("CREATE TABLE #tmpTable"); sb.AppendLine("("); foreach (var column in table.PrimaryKeyColumns.OrderBy(x => x.Name)) { sb.Append("\t[" + column.DatabaseName + "] " + column.GetSQLDefaultType()); if (index < table.PrimaryKeyColumns.Count - 1) { sb.Append(","); } sb.AppendLine(); index++; } //sb.Remove(sb.Length - 3, 3); sb.AppendLine(")"); sb.AppendLine(); sb.AppendLine("DECLARE @total__ivqatedr int"); sb.AppendLine("DECLARE @orderByColumnIndex int"); sb.AppendLine("-- remove top x values from the temp table based upon the specific page requested"); sb.AppendLine("SET @total__ivqatedr = (@pageSize * @page)"); sb.AppendLine("IF (@total__ivqatedr <> 0)"); sb.AppendLine("BEGIN"); sb.AppendLine(" SET ROWCOUNT @total__ivqatedr"); sb.AppendLine("END"); sb.AppendLine("INSERT INTO #tmpTable"); sb.AppendLine("("); sb.Append(Globals.BuildPrimaryKeySelectList(model, table, false)); sb.AppendLine(")"); //SELECT CLAUSE sb.AppendLine("SELECT"); sb.Append(Globals.BuildPrimaryKeySelectList(model, table, true)); sb.AppendLine("FROM"); sb.AppendLine(table.GetFullHierarchyTableJoin()); sb.AppendLine("WHERE"); var likeList = allColumns.Where(x => x.DataType != System.Data.SqlDbType.Xml && x.DataType != System.Data.SqlDbType.Text && x.DataType != System.Data.SqlDbType.NText && x.DataType != System.Data.SqlDbType.Image) .ToList(); for (var ii = 0; ii < likeList.Count; ii++) { var column = likeList[ii]; //If this is text then do a like, other wise equals var comparer = "="; if (ModelHelper.IsTextType(column.DataType)) { comparer = "LIKE"; } var t = column.ParentTableRef.Object as Table; var tableName = "[" + t.GetSQLSchema() + "].[" + Globals.GetTableDatabaseName(model, t) + "]"; sb.Append(" (@orderByColumn = '" + column.DatabaseName + "' and (((@filter is null) or (" + tableName + ".[" + column.DatabaseName + "] is null)) or (@filter is not null and " + tableName + ".[" + column.DatabaseName + "] " + comparer + " @filter)))"); if (ii < likeList.Count - 1) { sb.AppendLine(); sb.Append("or"); } sb.AppendLine(); } //ORDER BY CLAUSE sb.AppendLine("ORDER BY"); for (var ii = 0; ii < likeList.Count; ii++) { var column = likeList[ii]; var t = column.ParentTableRef.Object as Table; var tableName = "[" + t.GetSQLSchema() + "].[" + Globals.GetTableDatabaseName(model, t) + "]"; sb.AppendLine(" CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN '" + column.DatabaseName + "' THEN " + tableName + ".[" + column.DatabaseName + "] END END DESC, "); sb.Append(" CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN '" + column.DatabaseName + "' THEN " + tableName + ".[" + column.DatabaseName + "] END END"); if (ii < likeList.Count - 1) { sb.Append(", "); } sb.AppendLine(); } sb.AppendLine(); sb.AppendLine("-- set @count based on the rows moved in the previous statement"); //sb.AppendLine("SET @count = ( SELECT count(*) FROM [#tmpTable] )"); //REPEAT SELECT CLAUSE FOR COUNT sb.AppendLine("SET ROWCOUNT 0"); sb.AppendLine("SET @count = ("); sb.AppendLine("SELECT count(*)"); sb.AppendLine("FROM"); sb.AppendLine(table.GetFullHierarchyTableJoin()); sb.AppendLine("WHERE"); for (var ii = 0; ii < likeList.Count; ii++) { var column = likeList[ii]; var t = column.ParentTableRef.Object as Table; var tableName = "[" + t.GetSQLSchema() + "].[" + Globals.GetTableDatabaseName(model, t) + "]"; var comparer = "="; if (ModelHelper.IsTextType(column.DataType)) { comparer = "LIKE"; } sb.Append(" (@orderByColumn = '" + column.DatabaseName + "' and (((@filter is null) or (" + tableName + ".[" + column.DatabaseName + "] is null)) or (@filter is not null and " + tableName + ".[" + column.DatabaseName + "] " + comparer + " @filter)))"); if (ii < likeList.Count - 1) { sb.AppendLine(); sb.Append("or"); } sb.AppendLine(); } sb.AppendLine(")"); sb.AppendLine(); sb.AppendLine("-- remove top x values from the temp table based upon the specific page requested"); sb.AppendLine("SET @total__ivqatedr = (@pageSize * @page) - @pageSize"); sb.AppendLine("IF (@total__ivqatedr <> 0)"); sb.AppendLine("BEGIN"); sb.AppendLine(" SET ROWCOUNT @total__ivqatedr"); sb.AppendLine(" DELETE FROM #tmpTable"); sb.AppendLine("END"); sb.AppendLine(); sb.AppendLine("-- return the number of rows requested as the page size"); sb.AppendLine("SET ROWCOUNT @pageSize"); sb.AppendLine("SELECT"); sb.Append(Globals.BuildSelectList(table, model, true)); sb.AppendLine("FROM"); sb.AppendLine(" [#tmpTable]"); sb.Append(" INNER JOIN " + table.GetFullHierarchyTableJoin() + " ON "); var pkFirstTime = true; foreach (var pkColumn in table.PrimaryKeyColumns.OrderBy(x => x.Name)) { if (!pkFirstTime) { sb.AppendLine(" AND"); } else { pkFirstTime = false; } sb.AppendFormat("#tmpTable.[{0}] = [{2}].[{1}].[{0}]", pkColumn.DatabaseName, Globals.GetTableDatabaseName(model, table).ToUpper(), table.GetSQLSchema()); } sb.AppendLine(); sb.AppendLine("ORDER BY"); for (var ii = 0; ii < likeList.Count; ii++) { var column = likeList[ii]; var t = column.ParentTableRef.Object as Table; var tableName = "[" + t.GetSQLSchema() + "].[" + Globals.GetTableDatabaseName(model, t) + "]"; sb.AppendLine(" CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN '" + column.DatabaseName + "' THEN " + tableName + ".[" + column.DatabaseName + "] END END DESC, "); sb.Append(" CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN '" + column.DatabaseName + "' THEN " + tableName + ".[" + column.DatabaseName + "] END END"); if (ii < likeList.Count - 1) { sb.Append(", "); } sb.AppendLine(); } sb.AppendLine(); sb.AppendLine("DROP TABLE #tmpTable"); sb.AppendLine(); sb.AppendLine("GO"); return(sb.ToString()); }
public static string GetBody(Table table, ModelRoot model) { var sb = new StringBuilder(); if (!table.Immutable) { if (table.AllowCreateAudit) { sb.AppendLine("if (@" + model.Database.CreatedDateColumnName + " IS NULL)"); sb.AppendLine("SET @" + model.Database.CreatedDateColumnName + " = " + model.GetSQLDefaultDate()); } if (table.AllowModifiedAudit) { if (model.EFVersion == EFVersionConstants.EF4) { sb.AppendLine("DECLARE @" + model.Database.ModifiedDateColumnName + " [DateTime]"); sb.AppendLine("SET @" + model.Database.ModifiedDateColumnName + " = " + model.GetSQLDefaultDate()); } else if (model.EFVersion == EFVersionConstants.EF6) { //Modified Date - This is where we override the placeholder parameter for EF6 runtime. sb.Append("SET @" + model.Database.ModifiedDateColumnName + " = " + model.GetSQLDefaultDate()); sb.AppendLine("--Entity Framework 6 Required Modified Date be passed in, overwrite it here."); } else { throw new NotImplementedException(string.Format("model.EFVersion [{0}] not supported", model.EFVersion)); } } foreach (var column in table.PrimaryKeyColumns.OrderBy(x => x.Name)) { if (column.Identity == IdentityTypeConstants.Code) { sb.AppendLine("SET @" + column.ToDatabaseCodeIdentifier() + " = (select case when max([" + column.DatabaseName + "]) is null then 1 else max([" + column.DatabaseName + "]) + 1 end from [" + Globals.GetTableDatabaseName(model, table) + "])"); } else if (column.Identity == IdentityTypeConstants.Database) { //sb.AppendLine("DECLARE @" + column.ToDatabaseCodeIdentifier() + " " + dc.DataType); } } if (table.ParentTable == null) { AppendInsertionStatement(sb, table, model); } else { var tableList = table.GetTableHierarchy(); foreach (var t in tableList) { AppendInsertionStatement(sb, t, model); //On the base table save the primary keys as variables if (t.ParentTable == null) { sb.Append(BuildInheritedPkBaseTableVariables(t, model)); } } } } sb.AppendLine(); sb.AppendLine("SELECT "); sb.AppendLine(Globals.BuildSelectList(table, model, true)); sb.AppendLine("FROM"); sb.AppendLine(table.GetFullHierarchyTableJoin()); sb.AppendLine("WHERE"); sb.AppendLine(" " + BuildInsertSelectWhereStatement(table, model) + ";"); return(sb.ToString()); }