/// <summary> /// Translates the <paramref name="dbExpression"/> into a <see cref="DbTranslateResult"/> instance. /// </summary> /// <param name="dbExpression">The <see cref="DbExpression"/> to translate.</param> /// <returns><see cref="DbTranslateResult"/></returns> public virtual DbTranslateResult Translate(DbExpression dbExpression) { Parameters.Clear(); var sqlExpression = Visit(dbExpression); var translateResult = new DbTranslateResult(((DbSqlExpression)sqlExpression).Sql,Parameters,_providerFactory); return translateResult; }
/// <summary> /// Specifies the column to be updated. /// </summary> /// <param name="dbUpdateQuery">The target <see cref="DbQuery{TQueryExpression}"/>.</param> /// <param name="target">The <see cref="DbExpression"/> that represents the target table or view.</param> /// <param name="valueExpression">The <see cref="DbExpression"/> that represents the new value.</param> /// <returns><see cref="DbQuery{TQueryExpression}"/></returns> public static DbQuery<DbUpdateExpression> Set(this DbQuery<DbUpdateExpression> dbUpdateQuery, DbExpression target, DbExpression valueExpression) { var dbExpression = (DbExpression)DbExpressionFactory.Assign(target, valueExpression); if (!dbUpdateQuery.QueryExpression.SetExpression.IsNull()) dbExpression = DbExpressionFactory.List(new[] { dbUpdateQuery.QueryExpression.SetExpression, dbExpression }); dbUpdateQuery.QueryExpression.SetExpression = dbExpression; return dbUpdateQuery; }
/// <summary> /// Visits each node of the <see cref="DbExpression"/> tree and rewrites the tree if any changes has been made to any of the child nodes. /// </summary> /// <param name="dbExpression">The <see cref="DbExpression"/> to visit.</param> /// <returns>A <see cref="DbExpression"/> instance.</returns> public virtual DbExpression Visit(DbExpression dbExpression) { if (dbExpression.IsNull()) return dbExpression; var expressionType = dbExpression.ExpressionType; switch (expressionType) { case DbExpressionType.Function: return VisitFunctionExpression((DbFunctionExpression)dbExpression); case DbExpressionType.Select: return VisitSelectExpression((DbSelectExpression)dbExpression); case DbExpressionType.Update: return VisitUpdateExpression((DbUpdateExpression)dbExpression); case DbExpressionType.Insert: return VisitInsertExpression((DbInsertExpression)dbExpression); case DbExpressionType.Delete: return VisitDeleteExpression((DbDeleteExpression)dbExpression); case DbExpressionType.Column: return VisitColumnExpression((DbColumnExpression)dbExpression); case DbExpressionType.Table: return VisitTableExpression((DbTableExpression)dbExpression); case DbExpressionType.Binary: return VisitBinaryExpression((DbBinaryExpression)dbExpression); case DbExpressionType.Constant: return VisitConstantExpression((DbConstantExpression)dbExpression); case DbExpressionType.Alias: return VisitAliasExpression((DbAliasExpression)dbExpression); case DbExpressionType.Concat: return VisitConcatExpression((DbConcatExpression)dbExpression); case DbExpressionType.Conditional: return VisitConditionalExpression((DbConditionalExpression)dbExpression); case DbExpressionType.Exists: return VisitExistsExpression((DbExistsExpression)dbExpression); case DbExpressionType.List: return VisitListExpression((DbListExpression)dbExpression); case DbExpressionType.Batch: return VisitBatchExpression((DbBatchExpression)dbExpression); case DbExpressionType.In: return VisitInExpression((DbInExpression)dbExpression); case DbExpressionType.Query: return VisitQueryExpression((DbQuery)dbExpression); case DbExpressionType.Join: return VisitJoinExpression((DbJoinExpression)dbExpression); case DbExpressionType.Unary: return VisitUnaryExpression((DbUnaryExpression)dbExpression); case DbExpressionType.OrderBy: return VisitOrderByExpression((DbOrderByExpression)dbExpression); case DbExpressionType.Prefix: return VisitPrefixExpression((DbPrefixExpression)dbExpression); case DbExpressionType.Sql: return VisitSqlExpression((DbSqlExpression)dbExpression); } throw new ArgumentOutOfRangeException( "dbExpression", string.Format("The expression type '{0}' is not supported", dbExpression.ExpressionType)); }
/// <summary> /// Specifies the projection of the query. /// </summary> /// <param name="dbSelectQuery">The target <see cref="DbSelectQuery"/></param> /// <param name="expression">The <see cref="DbExpression"/> that represents the projection.</param> /// <returns><see cref="DbSelectQuery"/></returns> public static DbSelectQuery Select(this DbSelectQuery dbSelectQuery, DbExpression expression) { dbSelectQuery.QueryExpression.ProjectionExpression = expression; return dbSelectQuery; }
private DbExpression AddRowNumWhereClause(DbExpression dbExpression) { //dbExpression.Replace<DbSelectQuery>(dsq => (dsq.GetQueryExpression().) return null; }
/// <summary> /// Initializes a new instance of the <see cref="DbUnaryExpression"/> class. /// </summary> /// <param name="unaryExpressionType">The <see cref="DbUnaryExpressionType"/> that this <see cref="DbUnaryExpression"/> represents.</param> /// <param name="operand">The <see cref="DbExpression"/> that represents the unary operand.</param> /// <param name="targetType">The <see cref="Type"/> that specifies the type to convert to.</param> internal DbUnaryExpression(DbUnaryExpressionType unaryExpressionType, DbExpression operand, Type targetType) { UnaryExpressionType = unaryExpressionType; Operand = operand; TargetType = targetType; }
/// <summary> /// Creates a new <see cref="DbSelectQuery"/> that represents a sub query. /// </summary> /// <param name="expression">The <see cref="DbExpression"/> that represents the projection.</param> /// <returns>A <see cref="DbSelectQuery"/> instance.</returns> public DbSelectQuery Select(DbExpression expression) { var dbSelectQuery = new DbSelectQuery { QueryExpression = { IsSubQuery = true, ProjectionExpression = expression } }; return dbSelectQuery; }
/// <summary> /// Creates a <see cref="DbMathematicalFunctionExpression"/> that represents returning /// a pseudo-random float value from 0 through 1, exclusive. /// </summary> /// <param name="target">The target <see cref="DbExpression"/> that represents a numeric value.</param> /// <param name="precision">A <see cref="DbExpression"/> that represents the number of decimals returned.</param> /// <returns>A <see cref="DbMathematicalFunctionExpression"/> instance.</returns> public DbMathematicalFunctionExpression Round(DbExpression target, DbExpression precision) { return MakeMathematicalFunction(DbMathematicalFunctionExpressionType.Round, new[] { target, precision }); }
/// <summary> /// Creates a <see cref="DbStringFunctionExpression"/> that represents reversing a string value. /// </summary> /// <param name="expression">The <see cref="DbExpression"/> to convert.</param> /// <returns>A <see cref="DbStringFunctionExpression"/> instance.</returns> public DbStringFunctionExpression Reverse(DbExpression expression) { return MakeStringFunction(DbStringFunctionExpressionType.Reverse, new[] { expression }); }
/// <summary> /// Creates a new <see cref="DbUnaryExpression"/> that represents negating the result of a boolean <see cref="DbExpression"/>. /// </summary> /// <param name="operand">The <see cref="DbExpression"/> that represents the unary operand.</param> /// <returns>A <see cref="DbUnaryExpression"/> instance.</returns> public DbUnaryExpression Not(DbExpression operand) { return MakeUnary(DbUnaryExpressionType.Not, operand, null); }
/// <summary> /// Creates a <see cref="DbBinaryExpression"/> that represents arithmetic multiplication. /// </summary> /// <param name="leftExpression">A <see cref="DbExpression"/> that represents the left operand.</param> /// <param name="rightExpression">A <see cref="DbExpression"/> that represents the right operand.</param> /// <returns>A <see cref="DbBinaryExpression"/> instance.</returns> public DbBinaryExpression Multiply(DbExpression leftExpression, DbExpression rightExpression) { return MakeBinary(DbBinaryExpressionType.Multiply, leftExpression, rightExpression); }
/// <summary> /// Creates a <see cref="DbDateTimeFunctionExpression"/> that represents returning month component of a date/time value. /// </summary> /// <param name="target">A <see cref="DbExpression"/> that represents the target date time.</param> /// <returns>A <see cref="DbDateTimeFunctionExpression"/> instance.</returns> public DbDateTimeFunctionExpression Month(DbExpression target) { return MakeDateTimeFunction(DbDateTimeFunctionExpressionType.Month, new[] { target }); }
/// <summary> /// Creates a <see cref="DbAggregateFunctionExpression"/> that represents returning the minimum value in the <paramref name="target"/> expression. /// </summary> /// <param name="target">The <see cref="DbExpression"/> that the aggregate function operates on.</param> /// <returns>A <see cref="DbAggregateFunctionExpression"/> instance.</returns> public DbAggregateFunctionExpression Min(DbExpression target) { return MakeAggregateFunction(DbAggregateFunctionExpressionType.Min, target); }
/// <summary> /// Creates a new <see cref="DbUnaryExpression"/>. /// </summary> /// <param name="unaryExpressionType">The <see cref="DbUnaryExpressionType"/> that specifies the type of unary expression to create.</param> /// <param name="operand">The <see cref="DbExpression"/> that represents the unary operand.</param> /// <param name="targetType">The <see cref="Type"/> that specifies the type to convert to.</param> /// <returns>A <see cref="DbUnaryExpression"/> instance.</returns> public DbUnaryExpression MakeUnary(DbUnaryExpressionType unaryExpressionType, DbExpression operand, Type targetType) { var unaryExpression = new DbUnaryExpression(unaryExpressionType, operand, targetType); return unaryExpression; }
private DbExpression ReplaceSquareWithPower(DbExpression dbExpression) { return(dbExpression.Replace <DbMathematicalFunctionExpression>( dme => dme.MathematicalFunctionExpressionType == DbMathematicalFunctionExpressionType.Square, te => ExpressionFactory.Power(te.Arguments[0], ExpressionFactory.Constant(2)))); }
/// <summary> /// Creates a <see cref="DbBinaryExpression"/> that represents an inequality comparison. /// </summary> /// <param name="leftExpression">A <see cref="DbExpression"/> that represents the left operand.</param> /// <param name="rightExpression">A <see cref="DbExpression"/> that represents the right operand.</param> /// <returns>A <see cref="DbBinaryExpression"/> instance.</returns> public DbBinaryExpression NotEqual(DbExpression leftExpression, DbExpression rightExpression) { return MakeBinary(DbBinaryExpressionType.NotEqual, leftExpression, rightExpression); }
/// <summary> /// Creates a <see cref="DbStringFunctionExpression"/> that represents replacing /// all occurrences of a specified string value with another string value. /// </summary> /// <param name="expression">The <see cref="DbExpression"/> that represents target string.</param> /// <param name="oldValue">The value to be replaced.</param> /// <param name="newValue">The value to replace all occurrences of <paramref name="oldValue"/>.</param> /// <returns>A <see cref="DbStringFunctionExpression"/> instance.</returns> public DbStringFunctionExpression Replace(DbExpression expression, DbExpression oldValue, DbExpression newValue) { return MakeStringFunction(DbStringFunctionExpressionType.Replace, new[] { expression, oldValue, newValue }); }
/// <summary> /// Creates a <see cref="DbBinaryExpression"/> that represents a logical <b>OR</b> operation. /// </summary> /// <param name="leftExpression">A <see cref="DbExpression"/> that represents the left operand.</param> /// <param name="rightExpression">A <see cref="DbExpression"/> that represents the right operand.</param> /// <returns>A <see cref="DbBinaryExpression"/> instance.</returns> public DbBinaryExpression Or(DbExpression leftExpression, DbExpression rightExpression) { return MakeBinary(DbBinaryExpressionType.Or, leftExpression, rightExpression); }
/// <summary> /// Create a new <see cref="DbJoinExpression"/> that represents a 'RIGHT OUTER JOIN'. /// </summary> /// <param name="target">The join target.</param> /// <param name="condition">The join condition.</param> /// <returns>A <see cref="DbJoinExpression"/> instance.</returns> public DbJoinExpression RightOuterJoin(DbExpression target, DbExpression condition) { return MakeJoin(DbJoinExpressionType.RightOuterJoin, target, condition); }
/// <summary> /// Creates a new <see cref="DbOrderByExpression"/> that represents an descending ordering of the result set. /// </summary> /// <param name="expression">The <see cref="DbExpression"/> representing an element in the 'ORDER BY' clause.</param> /// <returns>A <see cref="DbOrderByExpression"/> instance.</returns> public DbOrderByExpression OrderByDescending(DbExpression expression) { return MakeOrderBy(DbOrderByExpressionType.Descending, expression); }
/// <summary> /// Creates a <see cref="DbDateTimeFunctionExpression"/> that represents returning second component of a date/time value. /// </summary> /// <param name="target">A <see cref="DbExpression"/> that represents the target date time.</param> /// <returns>A <see cref="DbDateTimeFunctionExpression"/> instance.</returns> public DbDateTimeFunctionExpression Second(DbExpression target) { return MakeDateTimeFunction(DbDateTimeFunctionExpressionType.Second, new[] { target }); }
/// <summary> /// Creates a <see cref="DbMathematicalFunctionExpression"/> that represents returning /// the value of the specified expression to the specified power. /// </summary> /// <param name="target">The target <see cref="DbExpression"/> that represents a numeric value.</param> /// <param name="y">The power to which to raise the numeric value.</param> /// <returns>A <see cref="DbMathematicalFunctionExpression"/> instance.</returns> public DbMathematicalFunctionExpression Power(DbExpression target, DbExpression y) { return MakeMathematicalFunction(DbMathematicalFunctionExpressionType.Power, new[] { target, y }); }
/// <summary> /// Initializes a new instance of the <see cref="DbUnaryExpression"/> class. /// </summary> /// <param name="unaryExpressionType">The <see cref="DbUnaryExpressionType"/> that this <see cref="DbUnaryExpression"/> represents.</param> /// <param name="operand">The <see cref="DbExpression"/> that represents the unary operand.</param> internal DbUnaryExpression(DbUnaryExpressionType unaryExpressionType, DbExpression operand) : this(unaryExpressionType,operand,null) { }
/// <summary> /// Creates a new <see cref="DbPrefixExpression"/>. /// </summary> /// <param name="target">The target <see cref="DbExpression"/> to be aliased.</param> /// <param name="prefix">the prefix to be used to reference the <paramref name="target"/>.</param> /// <returns>A <see cref="DbPrefixExpression"/> instance.</returns> public DbPrefixExpression Prefix(DbExpression target, string prefix) { return new DbPrefixExpression(target, prefix); }
public override DbTranslateResult Translate(DbExpression dbExpression) { dbExpression = ReplaceSquareWithPower(dbExpression); return base.Translate(dbExpression); }
/// <summary> /// Creates a <see cref="DbMathematicalFunctionExpression"/> that represents returning /// the radians of the specified numeric expression. /// </summary> /// <param name="target">The target <see cref="DbExpression"/> that represents a numeric value.</param> /// <returns>A <see cref="DbMathematicalFunctionExpression"/> instance.</returns> public DbMathematicalFunctionExpression Radians(DbExpression target) { return MakeMathematicalFunction(DbMathematicalFunctionExpressionType.Radians, new[] { target }); }
private DbExpression ReplaceSquareWithPower(DbExpression dbExpression) { return dbExpression.Replace<DbMathematicalFunctionExpression>( dme => dme.MathematicalFunctionExpressionType == DbMathematicalFunctionExpressionType.Square, te => ExpressionFactory.Power(te.Arguments[0], ExpressionFactory.Constant(2))); }
/// <summary> /// Creates a <see cref="DbDateTimeFunctionExpression"/> that represents adding the specified number of minutes /// to the <paramref name="target"/> date time. /// </summary> /// <param name="target">A <see cref="DbExpression"/> that represents the target date time.</param> /// <param name="numberExpression">A <see cref="DbExpression"/> that represents the number of minutes to add.</param> /// <returns>A <see cref="DbDateTimeFunctionExpression"/> instance.</returns> public DbDateTimeFunctionExpression AddMinutes(DbExpression target, DbExpression numberExpression) { return MakeDateTimeFunction(DbDateTimeFunctionExpressionType.AddMinutes, new[] { target, numberExpression }); }
/// <summary> /// Specifies the projection of the query. /// </summary> /// <param name="dbSelectQuery">The target <see cref="DbSelectQuery"/></param> /// <param name="expression">The <see cref="DbExpression"/> that represents the projection.</param> /// <returns><see cref="DbSelectQuery"/></returns> public static DbSelectQuery SelectDistinct(this DbSelectQuery dbSelectQuery, DbExpression expression) { dbSelectQuery.QueryExpression.ProjectionExpression = expression; dbSelectQuery.QueryExpression.IsDistinct = true; return dbSelectQuery; }
/// <summary> /// Creates a <see cref="DbMathematicalFunctionExpression"/> that represents returning /// a pseudo-random float value from 0 through 1, exclusive. /// </summary> /// <param name="seed">The target <see cref="DbExpression"/> that represents the seed value.</param> /// <returns>A <see cref="DbMathematicalFunctionExpression"/> instance.</returns> public DbMathematicalFunctionExpression Rand(DbExpression seed) { return MakeMathematicalFunction(DbMathematicalFunctionExpressionType.RandSeed, new[] { seed }); }
/// <summary> /// Specifies the target table to update. /// </summary> /// <param name="dbUpdateQuery">The target <see cref="DbUpdateQuery"/></param> /// <param name="target">The <see cref="DbExpression"/> that represents the target table or view.</param> /// <returns><see cref="DbUpdateQuery"/></returns> public static DbUpdateQuery Update(this DbUpdateQuery dbUpdateQuery, DbExpression target) { dbUpdateQuery.QueryExpression.Target = target; return dbUpdateQuery; }
public override DbTranslateResult Translate(DbExpression dbExpression) { dbExpression = ReplaceSquareWithPower(dbExpression); return(base.Translate(dbExpression)); }