Пример #1
0
        public FromExpressionInfo CreateSqlTableForStatement(
            SqlStatement sqlStatement,
            Func <ITableInfo, SqlTable> tableCreator,
            OrderingExtractionPolicy orderingExtractionPolicy)
        {
            ArgumentUtility.CheckNotNull("sqlStatement", sqlStatement);
            ArgumentUtility.CheckNotNull("tableCreator", tableCreator);

            if (sqlStatement.Orderings.Count == 0)
            {
                var tableInfo = new ResolvedSubStatementTableInfo(_uniqueIdentifierGenerator.GetUniqueIdentifier("q"), sqlStatement);
                var sqlTable  = tableCreator(tableInfo);
                return(new FromExpressionInfo(sqlTable, new Ordering[0], new SqlTableReferenceExpression(sqlTable), null));
            }

            // If we have orderings, we need to:
            // - Build a nested projection that includes the original orderings if OrderingExtractionPolicy.ExtractOrderingsIntoProjection is specified.
            // - Create a substatement clearing orderings, unless a TOP expression is present.
            // - Put it into a SqlTable.
            // - Put a reference to everything in the select projection (i.e., the original projection and the extracted orderings) into a
            //   FromExpressionInfo and return that.

            var newSelectProjection = sqlStatement.SelectProjection;

            if (orderingExtractionPolicy == OrderingExtractionPolicy.ExtractOrderingsIntoProjection)
            {
                newSelectProjection = GetNewSelectExpressionWithOrderings(sqlStatement);
            }

            var tableWithSubStatement = CreateSqlCompatibleSubStatementTable(sqlStatement, newSelectProjection, tableCreator);

            return(GetFromExpressionInfoForSubStatement(sqlStatement, tableWithSubStatement));
        }
        public FromExpressionInfo CreateSqlTableForStatement(SqlStatement sqlStatement, Func <ITableInfo, SqlTable> tableCreator)
        {
            if (sqlStatement.Orderings.Count == 0)
            {
                var tableInfo = new ResolvedSubStatementTableInfo(_uniqueIdentifierGenerator.GetUniqueIdentifier("q"), sqlStatement);
                var sqlTable  = tableCreator(tableInfo);
                return(new FromExpressionInfo(sqlTable, new Ordering[0], new SqlTableReferenceExpression(sqlTable), null));
            }

            var selectExpressionWithOrderings = GetNewSelectExpressionWithOrderings(sqlStatement);
            var tableWithSubStatement         = CreateSqlCompatibleSubStatementTable(sqlStatement, selectExpressionWithOrderings, tableCreator);

            return(GetFromExpressionInfoForSubStatement(sqlStatement, tableWithSubStatement));
        }
        private SubStatementWithRowNumber CreateSubStatementWithRowNumber(
            SqlStatementBuilder sqlStatementBuilder,
            UniqueIdentifierGenerator generator,
            ISqlPreparationStage stage,
            ISqlPreparationContext context)
        {
            var originalSelectProjection = sqlStatementBuilder.SelectProjection;

            IncludeRowNumberInSelectProjection(sqlStatementBuilder, stage, context);

            // Orderings are not allowed in SQL substatements unless a TOP expression is present
            if (sqlStatementBuilder.TopExpression == null)
            {
                sqlStatementBuilder.Orderings.Clear();
            }

            sqlStatementBuilder.RecalculateDataInfo(originalSelectProjection);
            // No NamedExpression required here because the row number tuple's items already have named members
            var newSqlStatement = sqlStatementBuilder.GetStatementAndResetBuilder();

            var tableInfo = new ResolvedSubStatementTableInfo(generator.GetUniqueIdentifier("q"), newSqlStatement);
            var sqlTable  = new SqlTable(tableInfo, JoinSemantics.Inner);

            var originalProjectionSelector = Expression.MakeMemberAccess(
                new SqlTableReferenceExpression(sqlTable),
                newSqlStatement.SelectProjection.Type.GetProperty("Key"));
            var rowNumberSelector = Expression.MakeMemberAccess(
                new SqlTableReferenceExpression(sqlTable),
                newSqlStatement.SelectProjection.Type.GetProperty("Value"));

            return(new SubStatementWithRowNumber(sqlTable, originalProjectionSelector, rowNumberSelector));
        }
Пример #4
0
        public IResolvedTableInfo ResolveTableInfo(UnresolvedTableInfo tableInfo, UniqueIdentifierGenerator generator)
        {
            ArgumentUtility.CheckNotNull("tableInfo", tableInfo);
            ArgumentUtility.CheckNotNull("generator", generator);

            var classDefinition = GetClassDefinition(tableInfo.ItemType);

            return(_storageSpecificExpressionResolver.ResolveTable(classDefinition, generator.GetUniqueIdentifier("t")));
        }
Пример #5
0
        public IResolvedTableInfo ResolveTableInfo(UnresolvedTableInfo tableInfo, UniqueIdentifierGenerator generator)
        {
            ArgumentUtility.CheckNotNull("tableInfo", tableInfo);
            ArgumentUtility.CheckNotNull("generator", generator);

            MetaTable table = GetMetaTable(tableInfo.ItemType);

            // TODO RM-3127: Refactor when re-linq supports schema names
            var tableName = table.TableName.StartsWith("dbo.") ? table.TableName.Substring(4) : table.TableName;

            return(new ResolvedSimpleTableInfo(tableInfo.ItemType, tableName, generator.GetUniqueIdentifier("t")));
        }
Пример #6
0
        public ResolvedJoinInfo ResolveJoinInfo(UnresolvedJoinInfo joinInfo, UniqueIdentifierGenerator generator)
        {
            ArgumentUtility.CheckNotNull("joinInfo", joinInfo);
            ArgumentUtility.CheckNotNull("generator", generator);

            var leftEndPointDefinition = GetEndPointDefinition(joinInfo.OriginatingEntity, joinInfo.MemberInfo);

            return(_storageSpecificExpressionResolver.ResolveJoin(
                       joinInfo.OriginatingEntity,
                       leftEndPointDefinition,
                       leftEndPointDefinition.GetOppositeEndPointDefinition(),
                       generator.GetUniqueIdentifier("t")));
        }
        public ITableInfo VisitUnresolvedGroupReferenceTableInfo(UnresolvedGroupReferenceTableInfo tableInfo)
        {
            var groupSourceSubStatementTableInfo = tableInfo.ReferencedGroupSource.GetResolvedTableInfo() as ResolvedSubStatementTableInfo;

            if (groupSourceSubStatementTableInfo == null)
            {
                var message = string.Format(
                    "This SQL generator only supports sequences in from expressions if they are members of an entity or if they come from a GroupBy operator. "
                    + "Sequence: '{0}'", tableInfo);
                throw new NotSupportedException(message);
            }

            var groupingSelectExpression = groupSourceSubStatementTableInfo.SqlStatement.SelectProjection as SqlGroupingSelectExpression;

            if (groupingSelectExpression == null)
            {
                throw new NotSupportedException(
                          "When a sequence retrieved by a subquery is used in a from expression, the subquery must end with a GroupBy operator.");
            }

            var elementSelectingStatementBuilder = new SqlStatementBuilder(groupSourceSubStatementTableInfo.SqlStatement)
            {
                GroupByExpression = null
            };

            var currentKeyExpression = Expression.MakeMemberAccess(
                new SqlTableReferenceExpression(tableInfo.ReferencedGroupSource),
                groupingSelectExpression.Type.GetProperty("Key"));

            var groupKeyJoinCondition = _stage.ResolveWhereExpression(
                Expression.OrElse(
                    Expression.AndAlso(new SqlIsNullExpression(groupingSelectExpression.KeyExpression), new SqlIsNullExpression(currentKeyExpression)),
                    Expression.AndAlso(
                        Expression.AndAlso(
                            new SqlIsNotNullExpression(groupingSelectExpression.KeyExpression),
                            new SqlIsNotNullExpression(currentKeyExpression)),
                        Expression.Equal(groupingSelectExpression.KeyExpression, currentKeyExpression))),
                _context);

            elementSelectingStatementBuilder.AddWhereCondition(groupKeyJoinCondition);

            elementSelectingStatementBuilder.SelectProjection = groupingSelectExpression.ElementExpression;
            elementSelectingStatementBuilder.RecalculateDataInfo(groupingSelectExpression);

            return(new ResolvedJoinedGroupingTableInfo(
                       _generator.GetUniqueIdentifier("q"),
                       elementSelectingStatementBuilder.GetSqlStatement(),
                       groupingSelectExpression,
                       groupSourceSubStatementTableInfo.TableAlias));
        }
    public virtual IResolvedTableInfo ResolveTableInfo (UnresolvedTableInfo tableInfo, UniqueIdentifierGenerator generator)
    {
      switch (tableInfo.ItemType.Name)
      {
        case "Cook":
        case "Knife":
        case "Kitchen":
        case "Restaurant":
        case "Company":
          return CreateResolvedTableInfo (tableInfo.ItemType, generator);
        case "Chef":
          return new ResolvedSimpleTableInfo (tableInfo.ItemType, "dbo."+tableInfo.ItemType.Name + "Table", generator.GetUniqueIdentifier ("t"));
      }

      throw new UnmappedItemException ("The type " + tableInfo.ItemType + " cannot be queried from the stub provider.");
    }
Пример #9
0
        private Expression HandlePotentialSubStatementExpression(
            Expression preparedKeySelector,
            SqlStatementBuilder sqlStatementBuilder,
            UniqueIdentifierGenerator generator)
        {
            var subStatementExpression = preparedKeySelector as SqlSubStatementExpression;

            if (subStatementExpression == null)
            {
                return(preparedKeySelector);
            }

            var sqlTable = subStatementExpression.ConvertToSqlTable(generator.GetUniqueIdentifier("t"));

            sqlStatementBuilder.SqlTables.Add(sqlTable);

            return(new SqlTableReferenceExpression(sqlTable));
        }
        private IExpressionNode ParseNode(Expression expression, string associatedIdentifier)
        {
            if (string.IsNullOrEmpty(associatedIdentifier))
            {
                associatedIdentifier = _identifierGenerator.GetUniqueIdentifier("<generated>_");
            }

            var methodCallExpression = GetQueryOperatorExpression(expression);

            if (methodCallExpression != null)
            {
                return(ParseMethodCallExpression(methodCallExpression, associatedIdentifier));
            }
            else
            {
                return(ParseNonQueryOperatorExpression(expression, associatedIdentifier));
            }
        }
 private ResolvedSimpleTableInfo CreateResolvedTableInfo(Type entityType, UniqueIdentifierGenerator generator)
 {
     return(new ResolvedSimpleTableInfo(entityType, entityType.Name + "Table", generator.GetUniqueIdentifier("t")));
 }
        public virtual IResolvedTableInfo ResolveTableInfo(UnresolvedTableInfo tableInfo, UniqueIdentifierGenerator generator)
        {
            switch (tableInfo.ItemType.Name)
            {
            case "Cook":
            case "Knife":
            case "Kitchen":
            case "Restaurant":
            case "Company":
                return(CreateResolvedTableInfo(tableInfo.ItemType, generator));

            case "Chef":
                return(new ResolvedSimpleTableInfo(tableInfo.ItemType, "dbo." + tableInfo.ItemType.Name + "Table", generator.GetUniqueIdentifier("t")));
            }

            throw new UnmappedItemException("The type " + tableInfo.ItemType + " cannot be queried from the stub provider.");
        }
 private ResolvedSimpleTableInfo CreateResolvedTableInfo (Type entityType, UniqueIdentifierGenerator generator)
 {
   return new ResolvedSimpleTableInfo (entityType, entityType.Name + "Table", generator.GetUniqueIdentifier ("t"));
 }
Пример #14
0
        public void GetUniqueIdentifier()
        {
            var identifier = _generator.GetUniqueIdentifier("x");

            Assert.That(identifier, Is.EqualTo("x0"));
        }