Esempio n. 1
0
        /// <inheritdoc/>
        protected override ISqlCompileUnit BuildExtractTableAndDomainConstraintsQuery(ExtractionContext context)
        {
            var tableMap        = context.TableMap;
            var domainMap       = context.DomainMap;
            var constraintTable = PgConstraint;
            var select          = SqlDml.Select(constraintTable);

            select.Where = SqlDml.In(constraintTable["conrelid"], CreateOidRow(tableMap.Keys)) ||
                           SqlDml.In(constraintTable["contypid"], CreateOidRow(domainMap.Keys));

            select.Columns.AddRange(constraintTable["conname"],
                                    constraintTable["contype"],
                                    constraintTable["condeferrable"],
                                    constraintTable["condeferred"],
                                    constraintTable["conrelid"],
                                    constraintTable["contypid"],
                                    constraintTable["conkey"],
                                    SqlDml.ColumnRef(
                                        SqlDml.Column(SqlDml.FunctionCall("pg_get_constraintdef", constraintTable["oid"])), "consrc"),
                                    constraintTable["confrelid"],
                                    constraintTable["confkey"],
                                    constraintTable["confupdtype"],
                                    constraintTable["confdeltype"],
                                    constraintTable["confmatchtype"]);
            return(select);
        }
Esempio n. 2
0
        /// <inheritdoc/>
        protected override ISqlCompileUnit BuildExtractTableAndViewColumnsQuery(ExtractionContext context)
        {
            var tableMap           = context.TableMap;
            var viewMap            = context.ViewMap;
            var columnsTable       = PgAttribute;
            var defaultValuesTable = PgAttrDef;
            var typesTable         = PgType;

            var select = SqlDml.Select(columnsTable
                                       .LeftOuterJoin(defaultValuesTable,
                                                      columnsTable["attrelid"] == defaultValuesTable["adrelid"] &&
                                                      columnsTable["attnum"] == defaultValuesTable["adnum"])
                                       .InnerJoin(typesTable, typesTable["oid"] == columnsTable["atttypid"]));

            select.Where = columnsTable["attisdropped"] == false &&
                           columnsTable["attnum"] > 0 &&
                           (SqlDml.In(columnsTable["attrelid"], CreateOidRow(tableMap.Keys)) ||
                            SqlDml.In(columnsTable["attrelid"], CreateOidRow(viewMap.Keys)));

            select.Columns.Add(columnsTable["attrelid"]);
            select.Columns.Add(columnsTable["attnum"]);
            select.Columns.Add(columnsTable["attname"]);
            select.Columns.Add(typesTable["typname"]);
            select.Columns.Add(columnsTable["atttypmod"]);
            select.Columns.Add(columnsTable["attnotnull"]);
            select.Columns.Add(columnsTable["atthasdef"]);
            select.Columns.Add(SqlDml.ColumnRef(SqlDml.Column(
                                                    SqlDml.FunctionCall("pg_get_expr", defaultValuesTable["adbin"], defaultValuesTable["adrelid"])), "adsrc"));
            select.OrderBy.Add(columnsTable["attrelid"]);
            select.OrderBy.Add(columnsTable["attnum"]);
            return(select);
        }
 public void Add(SqlExpression expression)
 {
     if (expression is SqlColumn)
     {
         base.Add(expression as SqlColumn);
     }
     else
     {
         base.Add(SqlDml.ColumnRef(SqlDml.Column(expression)));
     }
 }
Esempio n. 4
0
        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);
            }
        }
Esempio n. 5
0
        private SqlSelect BuildTypedQuery(IndexInfo index)
        {
            var underlyingIndex = index.UnderlyingIndexes[0];
            var baseQuery       = BuildProviderQuery(underlyingIndex);
            var query           = SqlDml.Select(baseQuery.From);

            query.Where = baseQuery.Where;

            var baseColumns       = baseQuery.Columns.ToList();
            var typeIdColumnIndex = index.Columns
                                    .Select((c, i) => new { c, i })
                                    .Single(p => p.c.Field.IsTypeId).i;
            var type         = index.ReflectedType;
            var typeIdColumn = SqlDml.ColumnRef(
                SqlDml.Column(SqlDml.Literal(TypeIdRegistry[type])),
                WellKnown.TypeIdFieldName);
            var discriminatorMap = type.Hierarchy.TypeDiscriminatorMap;

            if (discriminatorMap != null)
            {
                var discriminatorColumnIndex = underlyingIndex.Columns.IndexOf(discriminatorMap.Column);
                var discriminatorColumn      = baseQuery.From.Columns[discriminatorColumnIndex];
                var sqlCase = SqlDml.Case(discriminatorColumn);
                foreach (var pair in discriminatorMap)
                {
                    var discriminatorValue = GetDiscriminatorValue(discriminatorMap, pair.First);
                    var typeId             = TypeIdRegistry[pair.Second];
                    sqlCase.Add(SqlDml.Literal(discriminatorValue), SqlDml.Literal(typeId));
                }
                if (discriminatorMap.Default != null)
                {
                    sqlCase.Else = SqlDml.Literal(TypeIdRegistry[discriminatorMap.Default]);
                }
                typeIdColumn = SqlDml.ColumnRef(
                    SqlDml.Column(sqlCase),
                    WellKnown.TypeIdFieldName);
            }
            baseColumns.Insert(typeIdColumnIndex, typeIdColumn);
            query.Columns.AddRange(baseColumns);
            return(query);
        }
Esempio n. 6
0
        public void SqlColumnCloneTest()
        {
            {
                SqlUserColumn c      = SqlDml.Column(1);
                SqlUserColumn cClone = (SqlUserColumn)c.Clone();

                Assert.AreNotEqual(c, cClone);
                Assert.AreNotEqual(c.Expression, cClone.Expression);
                Assert.AreEqual(c.NodeType, cClone.NodeType);
                Assert.AreEqual(c.Expression.NodeType, cClone.Expression.NodeType);
            }
            Console.WriteLine();
            {
                SqlTableRef    t      = SqlDml.TableRef(table1);
                SqlTableColumn c      = (SqlTableColumn)t[0];
                SqlTableColumn cClone = (SqlTableColumn)c.Clone();

                Assert.AreNotEqual(c, cClone);
//        Assert.AreNotEqual(c.SqlTable, cClone.SqlTable);
                Assert.AreEqual(c.NodeType, cClone.NodeType);
            }
        }
Esempio n. 7
0
        public void SqlOrderCloneTest()
        {
            {
                SqlOrder o      = SqlDml.Order(2, true);
                SqlOrder oClone = (SqlOrder)o.Clone();

                Assert.AreNotEqual(o, oClone);
                Assert.AreEqual(o.NodeType, oClone.NodeType);
                Assert.AreEqual(o.Ascending, oClone.Ascending);
                Assert.AreEqual(o.Expression, oClone.Expression);
                Assert.AreEqual(o.Position, oClone.Position);
            }

            {
                SqlOrder o      = SqlDml.Order(SqlDml.Column(SqlDml.Literal(2)), false);
                SqlOrder oClone = (SqlOrder)o.Clone();

                Assert.AreNotEqual(o, oClone);
                Assert.AreEqual(o.NodeType, oClone.NodeType);
                Assert.AreEqual(o.Ascending, oClone.Ascending);
                Assert.AreNotEqual(o.Expression, oClone.Expression);
                Assert.AreEqual(o.Position, oClone.Position);
            }
        }
Esempio n. 8
0
        private void ValidateIgnoredData(Catalog catalog)
        {
            var validatableSchemas = new List <Schema>();

            if (nodeToSchemaMap.Count > 0)
            {
                validatableSchemas.Add(catalog.Schemas[nodeToSchemaMap[MainNodeId]]);
                validatableSchemas.Add(catalog.Schemas[nodeToSchemaMap[AdditionalNodeId]]);
            }
            else
            {
                validatableSchemas.Add(catalog.DefaultSchema);
            }

            foreach (var schema in validatableSchemas)
            {
                using (var connection = driver.CreateConnection()) {
                    connection.Open();

                    var productTable = schema.Tables["Product"];
                    var @ref         = SqlDml.TableRef(productTable);
                    var selectAll    = SqlDml.Select(@ref);
                    selectAll.Columns.Add(SqlDml.Column(SqlDml.Count()), "c0unt");

                    var selectWhere = SqlDml.Select(@ref);
                    selectWhere.Columns.Add(SqlDml.Column(SqlDml.Count()), "c0unt");
                    selectWhere.Where = @ref["HiddenName"] == SqlDml.Literal("Hidden name");
                    using (var expectedCountCommand = connection.CreateCommand(selectAll))
                        using (var actualCountCommand = connection.CreateCommand(selectWhere)) {
                            var expectedCount = Convert.ToInt64(expectedCountCommand.ExecuteScalar());
                            var actualCount   = Convert.ToInt64(actualCountCommand.ExecuteScalar());

                            Assert.That(actualCount, Is.EqualTo(expectedCount));
                        }

                    var priceListTable = schema.Tables["PriceList"];
                    @ref      = SqlDml.TableRef(priceListTable);
                    selectAll = SqlDml.Select(@ref);
                    selectAll.Columns.Add(SqlDml.Column(SqlDml.Count()), "c0unt");

                    selectWhere = SqlDml.Select(@ref);
                    selectWhere.Columns.Add(SqlDml.Column(SqlDml.Count()), "c0unt");
                    selectWhere.Where = @ref["HiddenComment"] == SqlDml.Literal("Some hidden comment");
                    using (var expectedCountCommand = connection.CreateCommand(selectAll))
                        using (var actualCountCommand = connection.CreateCommand(selectWhere)) {
                            var expectedCount = Convert.ToInt64(expectedCountCommand.ExecuteScalar());
                            var actualCount   = Convert.ToInt64(actualCountCommand.ExecuteScalar());

                            Assert.That(actualCount, Is.EqualTo(expectedCount));
                        }

                    var currencyTable = schema.Tables["Currency"];
                    @ref      = SqlDml.TableRef(currencyTable);
                    selectAll = SqlDml.Select(@ref);
                    selectAll.Columns.Add(SqlDml.Column(SqlDml.Count()), "c0unt");

                    selectWhere = SqlDml.Select(@ref);
                    selectWhere.Columns.Add(SqlDml.Column(SqlDml.Count()), "c0unt");
                    selectWhere.Where = @ref["NotInDomainColumn1"] == "Not in domain" && @ref["NotInDomainColumn1"] == "Not in domain" && @ref["NotInDomainColumn1"] == "Not in domain";

                    using (var expectedCountCommand = connection.CreateCommand(selectAll))
                        using (var actualCountCommand = connection.CreateCommand(selectWhere)) {
                            var expectedCount = Convert.ToInt64(expectedCountCommand.ExecuteScalar());
                            var actualCount   = Convert.ToInt64(actualCountCommand.ExecuteScalar());

                            Assert.That(actualCount, Is.EqualTo(expectedCount));
                        }
                }
            }
        }
        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);
        }
 public void Insert(int index, SqlExpression expression, string alias)
 {
     ArgumentValidator.EnsureArgumentNotNull(alias, "alias");
     Insert(index, SqlDml.ColumnRef(SqlDml.Column(expression), alias));
 }
 public void Add(SqlExpression expression, string alias)
 {
     ArgumentValidator.EnsureArgumentNotNull(alias, "alias");
     base.Add(SqlDml.ColumnRef(SqlDml.Column(expression), alias));
 }