protected void AddInlinableColumn(IInlinableProvider provider, Column column, SqlSelect resultQuery, SqlExpression columnExpression) { var columnName = ProcessAliasedName(column.Name); var columnRef = SqlDml.ColumnRef(SqlDml.Column(columnExpression), columnName); if (provider.IsInlined && !rootColumns.Contains(column.Origin)) { var columnStub = SqlDml.ColumnStub(columnRef); stubColumnMap.Add(columnStub, columnExpression); resultQuery.Columns.Add(columnStub); } else { resultQuery.Columns.Add(columnRef); } }
private SqlSelect ProcessApplyViaSubqueries(ApplyProvider provider, SqlProvider left, SqlProvider right, bool shouldUseQueryReference) { var rightQuery = right.Request.Statement; SqlSelect query; if (shouldUseQueryReference) { var leftTable = left.PermanentReference; query = SqlDml.Select(leftTable); query.Columns.AddRange(leftTable.Columns.Cast <SqlColumn>()); } else { query = left.Request.Statement.ShallowClone(); } var isApplyExistence = provider.Right.Type == ProviderType.Existence || provider.Right.Type == ProviderType.Select && provider.Right.Sources[0].Type == ProviderType.Existence; if (isApplyExistence) { for (int i = 0; i < rightQuery.Columns.Count; i++) { var column = rightQuery.Columns[i]; if (provider.IsInlined) { var columnStub = SqlDml.ColumnStub(column); var userColumn = ExtractUserColumn(column); stubColumnMap.Add(columnStub, userColumn.Expression); column = columnStub; } query.Columns.Add(column); } } else { if (provider.IsInlined) { for (int i = 0; i < rightQuery.Columns.Count; i++) { var subquery = rightQuery.ShallowClone(); var sqlColumn = subquery.Columns[i]; if (IsColumnStub(sqlColumn)) { var columnStub = ExtractColumnStub(sqlColumn); subquery.Columns.Clear(); subquery.Columns.Add(columnStub.Column); query.Columns.Add(subquery, sqlColumn.Name); } else { var columnRef = (SqlColumnRef)sqlColumn; var column = columnRef.SqlColumn; subquery.Columns.Clear(); subquery.Columns.Add(column); var columnName = ProcessAliasedName(provider.Right.Header.Columns[i].Name); var userColumnRef = SqlDml.ColumnRef(SqlDml.Column(subquery), columnName); var columnStub = SqlDml.ColumnStub(userColumnRef); stubColumnMap.Add(columnStub, subquery); query.Columns.Add(columnStub); } } } else { for (int i = 0; i < rightQuery.Columns.Count; i++) { var subquery = rightQuery.ShallowClone(); var column = subquery.Columns[i]; if (IsColumnStub(column)) { var columnStub = ExtractColumnStub(column); subquery.Columns.Clear(); subquery.Columns.Add(columnStub.Column); query.Columns.Add(subquery, column.Name); } else { var columnRef = (SqlColumnRef)column; var sqlColumn = columnRef.SqlColumn; subquery.Columns.Clear(); subquery.Columns.Add(sqlColumn); query.Columns.Add(subquery, columnRef.Name); } } } } return(query); }