Ejemplo n.º 1
0
        public static ExprList ConvertMerge(ExprMerge merge, string tempTableName)
        {
            var derivedTableValues = merge.Source as ExprDerivedTableValues;

            if (derivedTableValues == null)
            {
                throw new SqExpressException("Only derived table values can be used as a source in MERGE simulation");
            }

            var acc = new List <IExprExec>();

            var keys = ExtractKeys(merge, derivedTableValues.Alias.Alias);

            var exprInsertIntoTmp = TempTableData.FromDerivedTableValuesInsert(derivedTableValues, keys.SourceKeys, out var tempTable, name: tempTableName, alias: Alias.From(derivedTableValues.Alias.Alias));

            acc.AddRange(exprInsertIntoTmp.Expressions);

            //MATCHED
            var e = WhenMatched(merge, tempTable);

            if (e != null)
            {
                acc.Add(e);
            }

            //NOT MATCHED BY TARGET
            e = WhenNotMatchedByTarget(merge, tempTable, keys);
            if (e != null)
            {
                acc.Add(e);
            }

            //NOT MATCHED BY SOURCE
            e = WhenNotMatchedBySource(merge, tempTable);
            if (e != null)
            {
                acc.Add(e);
            }

            acc.Add(new ExprStatement(tempTable.Script.Drop()));
            return(new ExprList(acc));
        }
Ejemplo n.º 2
0
        public static TempTableData FromDerivedTableValues(ExprDerivedTableValues derivedTableValues, IReadOnlyList <ExprColumnName>?keys, Alias alias = default, string?name = null)
        {
            var result = new TempTableData(string.IsNullOrEmpty(name) || name == null ? GenerateName() : name, alias);

            derivedTableValues.Columns.AssertNotEmpty("Columns list cannot be empty");
            derivedTableValues.Values.Items.AssertNotEmpty("Rows list cannot be empty");

            TableColumn?[] tableColumns = new TableColumn?[derivedTableValues.Columns.Count];

            for (var rowIndex = 0; rowIndex < derivedTableValues.Values.Items.Count; rowIndex++)
            {
                var lastRow = rowIndex + 1 == derivedTableValues.Values.Items.Count;
                var row     = derivedTableValues.Values.Items[rowIndex];
                if (row.Items.Count != derivedTableValues.Columns.Count)
                {
                    throw new SqExpressException("Number of values in a row does not match number of columns");
                }

                for (var valueIndex = 0; valueIndex < row.Items.Count; valueIndex++)
                {
                    var value             = row.Items[valueIndex];
                    var previousColumn    = tableColumns[valueIndex];
                    var currentColumnName = derivedTableValues.Columns[valueIndex];
                    var res = value.Accept(ExprValueTypeAnalyzer <TableColumn?, TempTableBuilderCtx> .Instance,
                                           new ExprValueTypeAnalyzerCtx <TableColumn?, TempTableBuilderCtx>(
                                               new TempTableBuilderCtx(
                                                   currentColumnName,
                                                   previousColumn,
                                                   CheckIsPk(currentColumnName)),
                                               result));

                    tableColumns[valueIndex] = res;
                    if (lastRow)
                    {
                        if (ReferenceEquals(res, null))
                        {
                            throw new SqExpressException($"Could not evaluate column type at {valueIndex}");
                        }
                    }
                }
            }
            result.AddColumns(tableColumns !);

            return(result);

            bool CheckIsPk(ExprColumnName columnName)
            {
                if (keys == null || keys.Count < 1)
                {
                    return(false);
                }

                for (var index = 0; index < keys.Count; index++)
                {
                    var key = keys[index];

                    if (key.LowerInvariantName == columnName.LowerInvariantName)
                    {
                        return(true);
                    }
                }

                return(false);
            }
        }