Пример #1
0
        public static ExprList FromDerivedTableValuesInsert(ExprDerivedTableValues derivedTableValues, IReadOnlyList <ExprColumnName>?keys, out TempTableBase tempTable, Alias alias = default, string?name = null)
        {
            tempTable = FromDerivedTableValues(derivedTableValues, keys, alias, name);

            var insertData = derivedTableValues.Values.Items.SelectToReadOnlyList(r => new ExprInsertValueRow(r.Items));

            var insert = SqQueryBuilder.InsertInto(tempTable, derivedTableValues.Columns).Values(new ExprInsertValues(insertData));

            return(new ExprList(new IExprExec[] { new ExprStatement(tempTable.Script.Create()), insert }));
        }
Пример #2
0
        private static IExprExec?WhenNotMatchedByTarget(ExprMerge merge, TempTableBase tempTable, ExtractKeysResult keys)
        {
            IExprExec?e = null;

            if (merge.WhenNotMatchedByTarget != null)
            {
                if (merge.WhenNotMatchedByTarget is ExprExprMergeNotMatchedInsert insert)
                {
                    var filter = !SqQueryBuilder.Exists(SqQueryBuilder
                                                        .SelectOne()
                                                        .From(merge.TargetTable)
                                                        .Where(merge.On));

                    if (insert.And != null)
                    {
                        filter = filter & insert.And;
                    }

                    e = SqQueryBuilder.InsertInto(merge.TargetTable, insert.Columns)
                        .From(SqQueryBuilder.Select(insert.Values.SelectToReadOnlyList(i =>
                                                                                       i is ExprValue v
                                    ? v
                                    : throw new SqExpressException("DEFAULT value cannot be used in MERGE polyfill")))
                              .From(tempTable)
                              .Where(filter));
                }
                else if (merge.WhenNotMatchedByTarget is ExprExprMergeNotMatchedInsertDefault insertDefault)
                {
                    var filter = !SqQueryBuilder.Exists(SqQueryBuilder
                                                        .SelectOne()
                                                        .From(merge.TargetTable)
                                                        .Where(merge.On));

                    if (insertDefault.And != null)
                    {
                        filter = filter & insertDefault.And;
                    }

                    e = SqQueryBuilder.InsertInto(merge.TargetTable, keys.TargetKeys)
                        .From(SqQueryBuilder.Select(keys.SourceKeys)
                              .From(tempTable)
                              .Where(filter));
                }
                else
                {
                    throw new SqExpressException($"Unknown type: '{merge.WhenNotMatchedByTarget.GetType().Name}'");
                }
            }
            return(e);
        }