/// <summary> /// Initializes a new instance of the <see cref="JoinSource"/> class. /// </summary> /// <param name="joinType"> /// The join type. /// </param> /// <param name="first"> /// The first. /// </param> /// <param name="second"> /// The second. /// </param> /// <param name="expression"> /// The expression. /// </param> public JoinSource(JoinType joinType, SourceBase first, SourceBase second, [CanBeNull] SqlExpressionBase expression = null) { this.JoinType = joinType; this.First = first; this.Second = second; this.Expression = expression; }
/// <summary> /// Evaluates the <see cref="SqlExpressionBase"/>. /// </summary> /// <param name="expression"> /// The expression to evaluate. /// </param> /// <param name="sideEffects"> /// <c>true</c> if the expression has side effects, <c>false</c> otherwise. /// </param> /// <returns> /// The <see cref="object"/>. /// </returns> private object Evaluate(SqlExpressionBase expression, out bool sideEffects) { try { if (this.parsedScript.Context.NodeData.HasSideEffects(expression, variable => this.statements.HasVariableSideEffects(variable))) { sideEffects = true; return(null); } sideEffects = false; var linqExpression = GenericVisitor.Visit( (ExecutionContextExpression e) => Expression.Constant(this.statements), this.parsedScript.Context.NodeData.ConvertToLinqExpression(expression)); return(Expression.Lambda(linqExpression).Compile().DynamicInvoke()); } catch { sideEffects = true; return(null); } }
/// <summary> /// Initializes a new instance of the <see cref="Trigger"/> class. /// </summary> /// <param name="interval"> /// The interval. /// </param> public Trigger(TimeSpan interval) { var arguments = new SqlExpressionBase[] { new ConstSqlExpression(interval), }; this.Function = new FunctionCallSqlExpression("triggerevery", new ReadOnlyCollection <SqlExpressionBase>(arguments)); }
/// <summary> /// Initializes a new instance of the <see cref="Trigger"/> class. /// </summary> /// <param name="after"> /// The after. /// </param> public Trigger(string after) { var arguments = new SqlExpressionBase[] { new ConstSqlExpression(after), }; this.Function = new FunctionCallSqlExpression("triggerafter", new ReadOnlyCollection <SqlExpressionBase>(arguments)); }
/// <summary> /// Checks if a node is used in a group-by expression. /// </summary> /// <param name="node"> /// The node to check. /// </param> /// <returns> /// <c>true</c> if the node is in a group-by expression, <c>false</c> otherwise. /// </returns> public bool IsGroupByExpression(SqlExpressionBase node) { var scope = this; while (scope != null) { if (scope.groupings.Contains(node)) { return(true); } scope = scope.Parent; } return(false); }
/// <summary> /// Converts the <paramref name="expression"/> to an <see cref="Expression"/>. /// </summary> /// <param name="dataProvider"> /// The data provider. /// </param> /// <param name="expression"> /// The expression to convert. /// </param> /// <param name="allowVariables"> /// <c>true</c> to allow blocks and variables in the expression, <c>false</c> to replace variables by their value (so they can be used in expressions).</param> /// <returns> /// The <see cref="Expression"/>. /// </returns> public static Expression ConvertToLinqExpression(this INodeDataProvider dataProvider, [CanBeNull] SqlExpressionBase expression, bool allowVariables = true) { if (expression == null) { return(null); } new Evaluator(dataProvider).Visit(expression); var result = NodeDataProviderExpressionConverter.CleanExpression(dataProvider.GetExpression(expression)); if (allowVariables) { return(result); } var vars = new Dictionary <ParameterExpression, Expression>(); GenericVisitor.Visit( (BinaryExpression e) => { if (e.NodeType == ExpressionType.Assign && e.Left is ParameterExpression parameter) { vars[parameter] = e.Right; } return(null); }, result); result = new GenericVisitor { (GenericVisitor v, BlockExpression e) => v.Visit(e.Expressions.First(b => b.NodeType != ExpressionType.Assign)), (ParameterExpression e) => vars.TryGetValue(e, out var value) ? value : e }.Visit(result);
/// <summary> /// Initializes a new instance of the <see cref="SelectFromStatement"/> class. /// </summary> /// <param name="expressions"> /// The expressions. /// </param> /// <param name="source"> /// The source. /// </param> /// <param name="where"> /// The where. /// </param> /// <param name="groupings"> /// The groupings. /// </param> /// <param name="having"> /// The having. /// </param> /// <param name="orders"> /// The orders. /// </param> public SelectFromStatement(ReadOnlyCollection <AliasedSqlExpression> expressions, SourceBase source, SqlExpressionBase where, ReadOnlyCollection <SqlExpressionBase> groupings, SqlExpressionBase having, ReadOnlyCollection <OrderBySqlExpression> orders) { this.Expressions = expressions; this.Source = source; this.Where = where; this.Groupings = groupings; this.Orders = orders; this.Having = having; }
/// <summary> /// Initializes a new instance of the <see cref="BinarySqlExpression"/> class. /// </summary> /// <param name="first"> /// The first operand. /// </param> /// <param name="op"> /// The operator. /// </param> /// <param name="second"> /// The second operand. /// </param> public BinarySqlExpression(SqlExpressionBase first, string op, SqlExpressionBase second) { this.First = first; this.Op = op; this.Second = second; }
/// <summary> /// Initializes a new instance of the <see cref="OrderBySqlExpression"/> class. /// </summary> /// <param name="expression"> /// The expression. /// </param> /// <param name="ascending"> /// <c>true</c> to sort ascending, <c>false</c> otherwise. /// </param> public OrderBySqlExpression(SqlExpressionBase expression, bool ascending) { this.Expression = expression; this.Ascending = ascending; }
/// <summary> /// Initializes a new instance of the <see cref="GroupQuery"/> class. /// </summary> /// <param name="rowSelect"> /// The row select. /// </param> /// <param name="expressions"> /// The expressions. /// </param> /// <param name="groupings"> /// The groupings. /// </param> /// <param name="having"> /// The having. /// </param> /// <param name="orderBy"> /// The visitor order by. /// </param> public GroupQuery(SelectFromStatement rowSelect, ReadOnlyCollection <AliasedSqlExpression> expressions, ReadOnlyCollection <string> groupings, SqlExpressionBase having, ReadOnlyCollection <OrderBySqlExpression> orderBy) { this.RowSelect = rowSelect; this.Expressions = expressions; this.Groupings = groupings; this.Having = having; this.OrderBy = orderBy; }
/// <summary> /// Initializes a new instance of the <see cref="VariableDeclaration"/> class. /// </summary> /// <param name="name"> /// The name. /// </param> /// <param name="expression"> /// The expression. /// </param> public VariableDeclaration(string name, SqlExpressionBase expression) { this.Name = name; this.Expression = expression; }
/// <summary> /// Initializes a new instance of the <see cref="AliasedSqlExpression"/> class. /// </summary> /// <param name="expression"> /// The expression. /// </param> /// <param name="alias"> /// The alias. /// </param> public AliasedSqlExpression(SqlExpressionBase expression, string alias) { this.Expression = expression; this.Alias = alias; }
/// <summary> /// Initializes a new instance of the <see cref="UnarySqlExpression"/> class. /// </summary> /// <param name="op"> /// The op. /// </param> /// <param name="expression"> /// The expression. /// </param> public UnarySqlExpression([CanBeNull] string op, SqlExpressionBase expression) { this.Op = op?.ToUpperInvariant(); this.Expression = expression; }
/// <summary> /// Gets the expression connected to the node. /// </summary> /// <param name="dataProvider"> /// The data provider. /// </param> /// <param name="node"> /// The node. /// </param> /// <returns> /// The <see cref="Expression"/>. /// </returns> public static Expression GetExpression([NotNull] this INodeDataProvider dataProvider, SqlExpressionBase node) { return(dataProvider.Get <Expression>(node, "Expression")); }
/// <summary> /// Sets the expression that will replace a <see cref="FieldReferenceSqlExpression"/> in an ORDER BY clause. /// </summary> /// <param name="dataProvider"> /// The data provider. /// </param> /// <param name="field"> /// The field. /// </param> /// <param name="expression"> /// The expression. /// </param> public static void SetFieldReplacer([NotNull] this INodeDataProvider dataProvider, FieldReferenceSqlExpression field, SqlExpressionBase expression) { dataProvider.Set(field, "FieldReplacer", expression); }
/// <summary> /// Sets the expression connected to the node. /// </summary> /// <param name="dataProvider"> /// The data provider. /// </param> /// <param name="node"> /// The node. /// </param> /// <param name="expression"> /// The expression. /// </param> public static void SetExpression([NotNull] this INodeDataProvider dataProvider, SqlExpressionBase node, Expression expression) { dataProvider.Set(node, "Expression", expression); }