private void AddColumns(StringBuilder sb) { if (Columns.Count == 0) { return; } sb.AppendLine(); foreach (gsSelectColumn selCol in Columns) { if (selCol is gsCaseColumn) { gsCaseColumn caseColumn = selCol as gsCaseColumn; caseColumn.QueryName = QryName; sb.Append(caseColumn.ToString()); } else if (selCol is gsSubQueryColumn) { gsSelectQuery subQry = (selCol as gsSubQueryColumn).SubQuery; subQry.QryName = $"subQry{GetNextID()}"; sb.AppendLine(); sb.AppendLine(subQry.ToString()); sb.Append($"{QryName}.Columns.Add(new SelectColumn(SqlExpression.SubQuery({subQry.QryName}), {selCol.ColumnAlias.Quoted()}));"); } else { sb.Append($"{QryName}.Columns.Add(new SelectColumn({selCol.ToString()}));"); } sb.AppendLine(); } }
private void AddNestedCase(StringBuilder sb, gsCaseColumn caseColumn, string originCase) { caseColumn.CaseClauseName = "cc" + GetNextID(); sb.AppendLine($"CaseClause {caseColumn.CaseClauseName} = new CaseClause();"); foreach (gsCaseTerm caseTerm in caseColumn.CaseTerms) { sb.Append(caseTerm.When.ToString()); string thenStr; if (caseTerm.Then is gsCaseColumn) { var caseCol = caseTerm.Then as gsCaseColumn; caseCol.CaseOrigin = originCase; AddNestedCase(sb, caseCol, CaseClauseName); thenStr = $"SqlExpression.Case({caseCol.CaseClauseName})"; } else { caseTerm.Then.ToStringUseExpression = true; thenStr = caseTerm.Then.ToString(); } sb.AppendLine($"{caseColumn.CaseClauseName}.Terms.Add(new CaseTerm({caseTerm.When.WhereClauseName}, {thenStr}));"); } caseColumn.CaseAlias = ColumnAlias; if (caseColumn.Else.ColumnIsValid()) { caseColumn.CaseAlias = caseColumn.Else.ColumnAlias; caseColumn.Else.ColumnAlias = null; // Messes up the case ElseValue string tempElseStr = caseColumn.Else.ToString(); if (!tempElseStr.StartsWith("SqlExpression.")) { tempElseStr = $"SqlExpression.Field({tempElseStr});"; } sb.AppendLine($"{caseColumn.CaseClauseName}.ElseValue = {tempElseStr};"); } }