private void JoinViaFrom(SqlStatement statement, SqlSelect select) { var queryRef = SqlDml.QueryRef(select, "source"); SetStatementFrom(statement, queryRef); var sqlTableRef = GetStatementTable(statement); SqlExpression whereExpression = null; PrimaryIndexMapping indexMapping = PrimaryIndexes[0]; foreach (ColumnInfo columnInfo in indexMapping.PrimaryIndex.KeyColumns.Keys) { var leftColumn = queryRef.Columns[columnInfo.Name]; var rightColumn = sqlTableRef == null?GetStatementTable(statement).Columns[columnInfo.Name] : sqlTableRef.Columns[columnInfo.Name]; if (leftColumn == null || rightColumn == null) { throw new InvalidOperationException("Source query doesn't contain one of key columns of updated table."); } var columnEqualityExperssion = SqlDml.Equals(queryRef.Columns[columnInfo.Name], sqlTableRef.Columns[columnInfo.Name]); if (whereExpression == null) { whereExpression = columnEqualityExperssion; } else { whereExpression = SqlDml.And(whereExpression, columnEqualityExperssion); } } SetStatementWhere(statement, whereExpression); }
private void JoinViaIn(SqlStatement statement, SqlSelect @select) { SqlTableRef table = GetStatementTable(statement); SqlExpression where = GetStatementWhere(statement); JoinedTableRef = table; PrimaryIndexMapping indexMapping = PrimaryIndexes[0]; var columns = new List <ColumnInfo>(); foreach (ColumnInfo columnInfo in indexMapping.PrimaryIndex.KeyColumns.Keys) { SqlSelect s = select.ShallowClone(); foreach (ColumnInfo column in columns) { SqlBinary ex = SqlDml.Equals(SqlDml.TableColumn(s.From, column.Name), SqlDml.TableColumn(table, column.Name)); s.Where = s.Where.IsNullReference() ? ex : SqlDml.And(s.Where, ex); } s.Columns.Clear(); s.Columns.Add(SqlDml.TableColumn(s.From, columnInfo.Name)); SqlBinary @in = SqlDml.In(SqlDml.TableColumn(table, columnInfo.Name), s); @where = @where.IsNullReference() ? @in : SqlDml.And(@where, @in); columns.Add(columnInfo); } SetStatementWhere(statement, where); }
private void JoinViaJoin(SqlStatement statement, SqlSelect @select) { PrimaryIndexMapping indexMapping = PrimaryIndexes[0]; SqlTableRef left = SqlDml.TableRef(indexMapping.Table); SqlQueryRef right = SqlDml.QueryRef(@select); SqlExpression joinExpression = null; for (int i = 0; i < indexMapping.PrimaryIndex.KeyColumns.Count; i++) { SqlBinary binary = (left.Columns[i] == right.Columns[i]); if (joinExpression.IsNullReference()) { joinExpression = binary; } else { joinExpression &= binary; } } JoinedTableRef = left; SqlJoinedTable joinedTable = left.InnerJoin(right, joinExpression); SetStatementFrom(statement, joinedTable); }