예제 #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);
        }
예제 #2
0
        /// <inheritdoc/>
        protected override SqlProvider VisitAlias(AliasProvider provider)
        {
            var source = Compile(provider.Source);

            SqlSelect sourceSelect = source.Request.Statement;
            var       sqlSelect    = sourceSelect.ShallowClone();
            var       columns      = sqlSelect.Columns.ToList();

            sqlSelect.Columns.Clear();
            for (int i = 0; i < columns.Count; i++)
            {
                var columnName = provider.Header.Columns[i].Name;
                columnName = ProcessAliasedName(columnName);
                var column     = columns[i];
                var columnRef  = column as SqlColumnRef;
                var columnStub = column as SqlColumnStub;
                if (!ReferenceEquals(null, columnRef))
                {
                    sqlSelect.Columns.Add(SqlDml.ColumnRef(columnRef.SqlColumn, columnName));
                }
                else if (!ReferenceEquals(null, columnStub))
                {
                    sqlSelect.Columns.Add(columnStub);
                }
                else
                {
                    sqlSelect.Columns.Add(column, columnName);
                }
            }
            return(CreateProvider(sqlSelect, provider, source));
        }
예제 #3
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)));
     }
 }
예제 #5
0
        public string Compile(HandlerAccessor handlers, IndexInfo index)
        {
            var table = SqlDml.TableRef(CreateStubTable(index.ReflectedType.MappingName, index.Filter.Fields.Count));
            // Translation of ColumnRefs without alias seems broken, use original name as alias.
            var columns = index.Filter.Fields
                          .Select(field => field.Column.Name)
                          .Select((name, i) => SqlDml.ColumnRef(table.Columns[i], name))
                          .Cast <SqlExpression>()
                          .ToList();

            var processor = new ExpressionProcessor(index.Filter.Expression, handlers, null, columns);
            var fragment  = SqlDml.Fragment(processor.Translate());
            var result    = handlers.StorageDriver.Compile(fragment).GetCommandText();

            return(result);
        }
예제 #6
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);
            }
        }
예제 #7
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);
        }
예제 #8
0
        private SqlSelect CreateQuery(string tableName, SqlExtractionTask task, params string[] columnNames)
        {
            var catalog = new Catalog(task.Catalog);
            var schema  = catalog.CreateSchema(task.Schema);
            var table   = schema.CreateTable(tableName);

            foreach (var column in columnNames)
            {
                table.CreateColumn(column);
            }
            var tableRef   = SqlDml.TableRef(table);
            var select     = SqlDml.Select(tableRef);
            var columnRefs = columnNames
                             .Select((c, i) => SqlDml.ColumnRef(tableRef.Columns[i], c));

            foreach (var columnRef in columnRefs)
            {
                select.Columns.Add(columnRef);
            }
            return(select);
        }
        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));
 }
 public void Add(SqlColumn item, string alias)
 {
     base.Add(SqlDml.ColumnRef(item, alias));
 }