public void SetUp () { var sqlTable = SqlStatementModelObjectMother.CreateSqlTable_WithResolvedTableInfo(); _entityExpression = new SqlEntityDefinitionExpression ( typeof (string), "t", null, e => e.GetColumn (typeof (string), "ID", true), new SqlColumnExpression[] { new SqlColumnDefinitionExpression (typeof (string), "t", "ID", true), new SqlColumnDefinitionExpression (typeof (int), "t", "Name", false), new SqlColumnDefinitionExpression (typeof (int), "t", "City", false) }); _sqlStatement = new SqlStatement ( new TestStreamedValueInfo (typeof (int)), _entityExpression, new[] { sqlTable }, null, null, new Ordering[] { }, null, false, null, null); _commandBuilder = new SqlCommandBuilder(); }
public SqlSubStatementExpression (SqlStatement sqlStatement) : base (sqlStatement.DataInfo.DataType) { ArgumentUtility.CheckNotNull ("sqlStatement", sqlStatement); _sqlStatement = sqlStatement; }
public static SqlStatement ApplyContext (SqlStatement sqlStatement, SqlExpressionContext expressionContext, IMappingResolutionStage stage, IMappingResolutionContext mappingresolutionContext) { ArgumentUtility.CheckNotNull ("sqlStatement", sqlStatement); ArgumentUtility.CheckNotNull ("stage", stage); ArgumentUtility.CheckNotNull ("mappingresolutionContext", mappingresolutionContext); var visitor = new SqlContextSelectionAdjuster (stage, mappingresolutionContext); return visitor.VisitSqlStatement (sqlStatement, expressionContext); }
public void SetUp () { _sqlStatement = new SqlStatementBuilder (SqlStatementModelObjectMother.CreateSqlStatement_Resolved (typeof (Cook))) { SelectProjection = new NamedExpression ("test", Expression.Constant (5)), DataInfo = new StreamedSequenceInfo (typeof (int[]), Expression.Constant (0)) }.GetSqlStatement(); _tableInfo = new ResolvedSubStatementTableInfo ("c", _sqlStatement); }
public void SetUp () { _stageMock = MockRepository.GenerateMock<IMappingResolutionStage>(); _resolverMock = MockRepository.GenerateMock<IMappingResolver>(); _unresolvedTableInfo = SqlStatementModelObjectMother.CreateUnresolvedTableInfo (typeof (Cook)); _resolvedTableInfo = SqlStatementModelObjectMother.CreateResolvedTableInfo (typeof (Cook)); _generator = new UniqueIdentifierGenerator(); _sqlStatement = SqlStatementModelObjectMother.CreateSqlStatement_Resolved (typeof (Cook)); _mappingResolutionContext = new MappingResolutionContext(); }
public void PrepareSelectExpression () { var singleDataInfo = new StreamedSingleValueInfo (typeof (int), false); var selectProjection = Expression.Constant (0); var subStatement = new SqlStatement (singleDataInfo, selectProjection, new SqlTable[0], null, null, new Ordering[0], null, false, null, null); var expressionWithSubStatement = new SqlSubStatementExpression (subStatement); var result = _stage.PrepareSelectExpression (expressionWithSubStatement, _context); Assert.That (result, Is.SameAs(expressionWithSubStatement)); }
private Expression GetNewSelectExpressionWithOrderings (SqlStatement sqlStatement) { // wrap original select projection and all orderings into a large tuple expression (new { proj, new { o1, new { o2, ... }}}) var expressionsToBeTupelized = new[] { sqlStatement.SelectProjection }.Concat (sqlStatement.Orderings.Select (o => o.Expression)); var tupleExpression = TupleExpressionBuilder.AggregateExpressionsIntoTuple (expressionsToBeTupelized); var preparedTupleExpression = _stage.PrepareSelectExpression (tupleExpression, _context); if (preparedTupleExpression.Type != tupleExpression.Type) throw new InvalidOperationException ("The SQL Preparation stage must not change the type of the select projection."); return preparedTupleExpression; }
public bool IsSimplifiableGroupAggregate (SqlStatement resolvedSqlStatement) { ArgumentUtility.CheckNotNull ("resolvedSqlStatement", resolvedSqlStatement); return FindAggregationExpression (resolvedSqlStatement.SelectProjection) != null && resolvedSqlStatement.WhereCondition == null && resolvedSqlStatement.Orderings.Count == 0 && resolvedSqlStatement.GroupByExpression == null && resolvedSqlStatement.SqlTables.Count == 1 && resolvedSqlStatement.SqlTables[0].GetResolvedTableInfo() is ResolvedJoinedGroupingTableInfo && resolvedSqlStatement.TopExpression == null && !resolvedSqlStatement.IsDistinctQuery; }
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); }
public virtual void Build ( SqlStatement sqlStatement, ISqlCommandBuilder commandBuilder, bool isOutermostStatement) { ArgumentUtility.CheckNotNull ("sqlStatement", sqlStatement); ArgumentUtility.CheckNotNull ("commandBuilder", commandBuilder); BuildSelectPart (sqlStatement, commandBuilder, isOutermostStatement); BuildFromPart (sqlStatement, commandBuilder); BuildWherePart (sqlStatement, commandBuilder); BuildGroupByPart (sqlStatement, commandBuilder); BuildOrderByPart (sqlStatement, commandBuilder); }
public void WhereCondition_CanBeSetToNull () { var sqlStatement = new SqlStatement ( new TestStreamedValueInfo (typeof (int)), Expression.Constant (1), new SqlTable[] { }, null, null, new Ordering[] { }, null, false, null, null); Assert.That (sqlStatement.WhereCondition, Is.Null); }
public void SetUp () { _stageMock = MockRepository.GenerateStrictMock<ISqlPreparationStage>(); _context = SqlStatementModelObjectMother.CreateSqlPreparationContext (); _generator = new UniqueIdentifierGenerator(); _factory = new SqlPreparationSubStatementTableFactory (_stageMock, _context, _generator); var builderForStatementWithOrderings = new SqlStatementBuilder (SqlStatementModelObjectMother.CreateSqlStatementWithCook ()) { Orderings = { new Ordering (Expression.Constant ("order1"), OrderingDirection.Desc), new Ordering (Expression.Constant ("order2"), OrderingDirection.Asc), } }; _statementWithOrderings = builderForStatementWithOrderings.GetSqlStatement (); }
public SqlStatement VisitSqlStatement (SqlStatement sqlStatement, SqlExpressionContext expressionContext) { ArgumentUtility.CheckNotNull ("sqlStatement", sqlStatement); if (expressionContext == SqlExpressionContext.PredicateRequired) throw new InvalidOperationException ("A SqlStatement cannot be used as a predicate."); var statementBuilder = new SqlStatementBuilder (sqlStatement); var newSelectProjection = _stage.ApplyContext (sqlStatement.SelectProjection, expressionContext, _mappingResolutionContext); statementBuilder.SelectProjection = newSelectProjection; statementBuilder.RecalculateDataInfo (sqlStatement.SelectProjection); var newSqlStatement = statementBuilder.GetSqlStatement(); return newSqlStatement.Equals (sqlStatement) ? sqlStatement : newSqlStatement; }
public SqlStatementBuilder (SqlStatement sqlStatement) { ArgumentUtility.CheckNotNull ("sqlStatement", sqlStatement); DataInfo = sqlStatement.DataInfo; SelectProjection = sqlStatement.SelectProjection; WhereCondition = sqlStatement.WhereCondition; IsDistinctQuery = sqlStatement.IsDistinctQuery; TopExpression = sqlStatement.TopExpression; GroupByExpression = sqlStatement.GroupByExpression; RowNumberSelector = sqlStatement.RowNumberSelector; CurrentRowNumberOffset = sqlStatement.CurrentRowNumberOffset; SqlTables = new List<SqlTable> (sqlStatement.SqlTables); Orderings = new List<Ordering> (sqlStatement.Orderings); }
protected virtual void BuildFromPart (SqlStatement sqlStatement, ISqlCommandBuilder commandBuilder) { ArgumentUtility.CheckNotNull ("sqlStatement", sqlStatement); ArgumentUtility.CheckNotNull ("commandBuilder", commandBuilder); if (sqlStatement.SqlTables.Count > 0) { commandBuilder.Append (" FROM "); bool isFirstTable = true; foreach (var sqlTable in sqlStatement.SqlTables) { _stage.GenerateTextForFromTable (commandBuilder, sqlTable, isFirstTable); isFirstTable = false; } } }
private SqlTable CreateSqlCompatibleSubStatementTable ( SqlStatement originalStatement, Expression newSelectProjection, Func<ITableInfo, SqlTable> tableCreator) { // create a new statement equal to the original one, but with the tuple as its select projection var builder = new SqlStatementBuilder (originalStatement) { SelectProjection = newSelectProjection }; builder.RecalculateDataInfo (originalStatement.SelectProjection); // clear orderings unless required for TopExpression if (originalStatement.TopExpression == null) builder.Orderings.Clear(); var newSqlStatement = builder.GetSqlStatement(); // put new statement into a sub-statement table var subStatementTableInfo = new ResolvedSubStatementTableInfo (_uniqueIdentifierGenerator.GetUniqueIdentifier ("q"), newSqlStatement); return tableCreator (subStatementTableInfo); }
public void SetUp () { _dataInfo = new StreamedScalarValueInfo (typeof (int)); _resolvedElementExpressionReference = new SqlColumnDefinitionExpression (typeof (string), "q0", "element", false); _resolvedSelectProjection = new NamedExpression ( null, new AggregationExpression (typeof (int), _resolvedElementExpressionReference, AggregationModifier.Min)); _associatedGroupingSelectExpression = new SqlGroupingSelectExpression ( new NamedExpression ("key", Expression.Constant ("k")), new NamedExpression ("element", Expression.Constant ("e"))); _resolvedJoinedGroupingSubStatement = SqlStatementModelObjectMother.CreateSqlStatement (_associatedGroupingSelectExpression); _resolvedJoinedGroupingTable = new SqlTable ( new ResolvedJoinedGroupingTableInfo ( "q1", _resolvedJoinedGroupingSubStatement, _associatedGroupingSelectExpression, "q0"), JoinSemantics.Inner); _simplifiableResolvedSqlStatement = new SqlStatement ( _dataInfo, _resolvedSelectProjection, new[] { _resolvedJoinedGroupingTable }, null, null, new Ordering[0], null, false, Expression.Constant (0), Expression.Constant (0)); _simplifiableUnresolvedProjection = new AggregationExpression ( typeof (int), new SqlTableReferenceExpression (_resolvedJoinedGroupingTable), AggregationModifier.Count); _stageMock = MockRepository.GenerateStrictMock<IMappingResolutionStage> (); _context = new MappingResolutionContext(); _groupAggregateSimplifier = new GroupAggregateSimplifier (_stageMock, _context); }
protected virtual void BuildSelectPart ( SqlStatement sqlStatement, ISqlCommandBuilder commandBuilder, bool isOutermostStatement) { ArgumentUtility.CheckNotNull ("sqlStatement", sqlStatement); ArgumentUtility.CheckNotNull ("commandBuilder", commandBuilder); commandBuilder.Append ("SELECT "); if (!(sqlStatement.SelectProjection is AggregationExpression)) { BuildDistinctPart (sqlStatement, commandBuilder); BuildTopPart (sqlStatement, commandBuilder); } if (isOutermostStatement) _stage.GenerateTextForOuterSelectExpression (commandBuilder, sqlStatement.SelectProjection); else _stage.GenerateTextForSelectExpression (commandBuilder, sqlStatement.SelectProjection); }
public void Initialization_WithExistingSqlStatement () { var isDistinctQuery = BooleanObjectMother.GetRandomBoolean(); var selectProjection = Expression.Constant ("select"); var whereCondition = Expression.Constant (true); var topExpression = Expression.Constant ("top"); var sqlTable = new SqlTable (new ResolvedSimpleTableInfo (typeof (Cook), "CookTable", "c"), JoinSemantics.Inner); var ordering = new Ordering (Expression.Constant ("order"), OrderingDirection.Desc); var rowNumberSelector = Expression.Constant("selector"); var currentRowNumberOffset = Expression.Constant(1); var groupExpression = Expression.Constant ("group"); var sqlStatement = new SqlStatement ( new TestStreamedValueInfo (typeof (int)), selectProjection, new[] { sqlTable }, whereCondition, groupExpression, new[] { ordering }, topExpression, isDistinctQuery, rowNumberSelector, currentRowNumberOffset); var testedBuilder = new SqlStatementBuilder (sqlStatement); Assert.That (testedBuilder.SelectProjection, Is.SameAs (selectProjection)); Assert.That (testedBuilder.TopExpression, Is.SameAs (topExpression)); Assert.That (testedBuilder.SqlTables[0], Is.SameAs (sqlTable)); Assert.That (testedBuilder.Orderings[0], Is.SameAs (ordering)); Assert.That (testedBuilder.WhereCondition, Is.EqualTo (whereCondition)); Assert.That (testedBuilder.IsDistinctQuery, Is.EqualTo (isDistinctQuery)); Assert.That (testedBuilder.DataInfo, Is.SameAs (sqlStatement.DataInfo)); Assert.That (testedBuilder.RowNumberSelector, Is.SameAs (sqlStatement.RowNumberSelector)); Assert.That (testedBuilder.CurrentRowNumberOffset, Is.SameAs (currentRowNumberOffset)); Assert.That (testedBuilder.GroupByExpression, Is.SameAs (groupExpression)); }
public SqlSubStatementExpression(SqlStatement sqlStatement) { ArgumentUtility.CheckNotNull("sqlStatement", sqlStatement); _sqlStatement = sqlStatement; }
protected virtual void BuildTopPart (SqlStatement sqlStatement, ISqlCommandBuilder commandBuilder) { if (sqlStatement.TopExpression != null) { commandBuilder.Append ("TOP ("); _stage.GenerateTextForTopExpression (commandBuilder, sqlStatement.TopExpression); commandBuilder.Append (") "); } }
protected virtual void BuildDistinctPart (SqlStatement sqlStatement, ISqlCommandBuilder commandBuilder) { if (sqlStatement.IsDistinctQuery) commandBuilder.Append ("DISTINCT "); }
protected virtual void BuildGroupByPart (SqlStatement sqlStatement, ISqlCommandBuilder commandBuilder) { ArgumentUtility.CheckNotNull ("sqlStatement", sqlStatement); ArgumentUtility.CheckNotNull ("commandBuilder", commandBuilder); if (sqlStatement.GroupByExpression != null) { commandBuilder.Append (" GROUP BY "); _stage.GenerateTextForGroupByExpression (commandBuilder, sqlStatement.GroupByExpression); } }
protected virtual void BuildOrderByPart (SqlStatement sqlStatement, ISqlCommandBuilder commandBuilder) { ArgumentUtility.CheckNotNull ("sqlStatement", sqlStatement); ArgumentUtility.CheckNotNull ("commandBuilder", commandBuilder); if (sqlStatement.Orderings.Count > 0) { commandBuilder.Append (" ORDER BY "); commandBuilder.AppendSeparated (", ", sqlStatement.Orderings, _stage.GenerateTextForOrdering); } }
public void SetUp () { _sqlStatement = SqlStatementModelObjectMother.CreateSqlStatement(); _expression = new SqlSubStatementExpression (_sqlStatement); }
public static ResolvedJoinedGroupingTableInfo CreateResolvedJoinedGroupingTableInfo (SqlStatement sqlStatement) { return new ResolvedJoinedGroupingTableInfo ( "cook", sqlStatement, CreateSqlGroupingSelectExpression(), "q1"); }
public void Build_WithOrderByClause () { var columnExpression = new SqlColumnDefinitionExpression (typeof (string), "t", "Name", false); var orderByClause = new Ordering (columnExpression, OrderingDirection.Asc); var sqlStatement = new SqlStatement ( new TestStreamedValueInfo (typeof (int)), _entityExpression, new[] { _sqlTable }, null, null, new[] { orderByClause }, null, false, null, null); _stageMock.Expect ( mock => mock.GenerateTextForSelectExpression (_commandBuilder, sqlStatement.SelectProjection)) .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append ("[t].[ID],[t].[Name],[t].[City]")); _stageMock.Expect (mock => mock.GenerateTextForFromTable (_commandBuilder, sqlStatement.SqlTables[0], true)) .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append ("[Table] AS [t]")); _stageMock.Expect (mock => mock.GenerateTextForOrdering (_commandBuilder, sqlStatement.Orderings[0])) .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append ("[t].[Name] ASC")); _stageMock.Replay(); _generator.Build (sqlStatement, _commandBuilder, false); _commandBuilder.SetInMemoryProjectionBody (Expression.Constant (0)); var result = _commandBuilder.GetCommand(); Assert.That (result.CommandText, Is.EqualTo ("SELECT [t].[ID],[t].[Name],[t].[City] FROM [Table] AS [t] ORDER BY [t].[Name] ASC")); _stageMock.VerifyAllExpectations(); }
public void Build_WithGroupByExpression () { var sqlGroupExpression = SqlStatementModelObjectMother.CreateSqlGroupingSelectExpression (); sqlGroupExpression.AddAggregationExpressionWithName (Expression.Constant ("aggregation1")); sqlGroupExpression.AddAggregationExpressionWithName (Expression.Constant ("aggregation2")); var sqlStatement = new SqlStatement ( new TestStreamedValueInfo (typeof (int)), _entityExpression, new[] { _sqlTable }, null, sqlGroupExpression, new Ordering[] { }, null, false, null, null); _stageMock.Expect ( mock => mock.GenerateTextForSelectExpression (_commandBuilder, sqlStatement.SelectProjection)) .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append ("[t].[ID],[t].[Name],[t].[City]")); _stageMock.Expect (mock => mock.GenerateTextForFromTable (_commandBuilder, sqlStatement.SqlTables[0], true)) .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append ("[Table] AS [t]")); _stageMock.Expect ( mock => mock.GenerateTextForGroupByExpression (_commandBuilder, sqlStatement.GroupByExpression)) .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append("keyExpression")); _stageMock.Replay (); _generator.Build (sqlStatement, _commandBuilder, false); _stageMock.VerifyAllExpectations(); Assert.That (_commandBuilder.GetCommandText (), Is.EqualTo ("SELECT [t].[ID],[t].[Name],[t].[City] FROM [Table] AS [t] GROUP BY keyExpression")); }
private FromExpressionInfo GetFromExpressionInfoForSubStatement (SqlStatement originalSqlStatement, SqlTable tableWithSubStatement) { var expressionsFromSubStatement = TupleExpressionBuilder.GetExpressionsFromTuple (new SqlTableReferenceExpression (tableWithSubStatement)).ToArray(); var projectionFromSubStatement = expressionsFromSubStatement.First (); // this was the original projection var orderingsFromSubStatement = expressionsFromSubStatement .Skip (1) // ignore original projection .Select ((expr, i) => new Ordering (expr, originalSqlStatement.Orderings[i].OrderingDirection)); return new FromExpressionInfo (tableWithSubStatement, orderingsFromSubStatement.ToArray (), projectionFromSubStatement, null); }
protected virtual void BuildWherePart (SqlStatement sqlStatement, ISqlCommandBuilder commandBuilder) { ArgumentUtility.CheckNotNull ("sqlStatement", sqlStatement); ArgumentUtility.CheckNotNull ("commandBuilder", commandBuilder); if ((sqlStatement.WhereCondition != null)) { commandBuilder.Append (" WHERE "); _stage.GenerateTextForWhereExpression (commandBuilder, sqlStatement.WhereCondition); } }
public void Build_WithWhereCondition () { var sqlStatement = new SqlStatement ( new TestStreamedValueInfo (typeof (int)), _entityExpression, new[] { _sqlTable }, Expression.Constant (true), null, new Ordering[] { }, null, false, null, null); _stageMock.Expect ( mock => mock.GenerateTextForSelectExpression (_commandBuilder, sqlStatement.SelectProjection)) .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append ("[t].[ID],[t].[Name],[t].[City]")); _stageMock.Expect (mock => mock.GenerateTextForFromTable (_commandBuilder, sqlStatement.SqlTables[0], true)) .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append ("[Table] AS [t]")); _stageMock.Expect (mock => mock.GenerateTextForWhereExpression (_commandBuilder, sqlStatement.WhereCondition)) .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append ("(@1 = 1)")); _stageMock.Replay(); _generator.Build (sqlStatement, _commandBuilder, false); _commandBuilder.SetInMemoryProjectionBody (Expression.Constant (0)); var result = _commandBuilder.GetCommand (); Assert.That (result.CommandText, Is.EqualTo ("SELECT [t].[ID],[t].[Name],[t].[City] FROM [Table] AS [t] WHERE (@1 = 1)")); _stageMock.VerifyAllExpectations(); }