internal void ChangeToInsert <T>(string tableName, Expression <Func <T, object> > insertObjectExpression)
        {
            Validate();
            var    sqlSelectClause       = Clauses.FirstOrDefault();
            string columnsToInsert       = string.Join(",", insertObjectExpression.GetObjectProperties());
            string insertValueExpression = string.Format("INTO {0} ({1})", tableName, columnsToInsert);

            Clauses.Insert(0, new SqlClause {
                Name = "INSERT", InputText = insertValueExpression
            });
            sqlSelectClause.InputText = columnsToInsert;
        }
        public void ChangeToUpdate(string updateExpression, string setExpression)
        {
            Validate();
            var sqlClause = Clauses.FirstOrDefault();

            if (sqlClause != null)
            {
                sqlClause.Name      = "UPDATE";
                sqlClause.InputText = updateExpression;
                Clauses.Insert(1, new SqlClause {
                    Name = "SET", InputText = setExpression
                });
            }
        }
Example #3
0
        /// <summary>
        /// Insert a selector clause at the specified position.
        /// </summary>
        ///
        /// <exception cref="ArgumentException">
        /// Thrown if the selector is not valid to insert at this position.
        /// </exception>
        ///
        /// <param name="index">
        /// The position in the selector chain to insert this clause
        /// </param>
        /// <param name="clause">
        /// The clause to insert
        /// </param>
        /// <param name="combinatorType">
        /// (optional) type of the combinator.
        /// </param>

        public void Insert(int index, SelectorClause clause, CombinatorType combinatorType = CombinatorType.Chained)
        {
            if (combinatorType == CombinatorType.Root && Clauses.Count != 0)
            {
                throw new ArgumentException("Combinator type can only be root if there are no other selectors.");
            }

            if (Clauses.Count > 0 && index == 0)
            {
                Clauses[0].CombinatorType = combinatorType;
                clause.CombinatorType     = CombinatorType.Root;
                clause.TraversalType      = TraversalType.All;
            }
            Clauses.Insert(index, clause);
        }
Example #4
0
        private void SetStatement(SqlStatement value)
        {
            if (!Clauses.Any())
            {
                Clauses.Add(value);
            }

            if (Clauses[0] is SqlStatement)
            {
                Clauses[0] = value;
            }
            else
            {
                Clauses.Insert(0, value);
            }
        }
Example #5
0
        private void Parse()
        {
            if (Expression is LambdaExpression lambda)
            {
                Clauses.Add(new SqlExpression(lambda.Body));

                return;
            }

            if (Expression is UnaryExpression unary)
            {
                Clauses.AddRange(GetNodeClause(unary.NodeType), new SqlExpression(unary.Operand));

                return;
            }

            if (Expression is BinaryExpression binary)
            {
                Clauses.AddRange(new SqlExpression(binary.Left), GetNodeClause(binary.NodeType), new SqlExpression(binary.Right));

                if (binary.NodeType == ExpressionType.AndAlso || binary.NodeType == ExpressionType.OrElse)
                {
                    Clauses.Insert(0, new RawSqlClause("("));
                    Clauses.Add(new RawSqlClause(")"));
                }

                return;
            }

            if (Expression is ConstantExpression constant)
            {
                object value = constant.Value;

                if (value is int)
                {
                    Clauses.Add(new RawSqlClause(value.ToString()));

                    return;
                }

                if (value is string)
                {
                    value = (string)value;
                }

                Clauses.Add(new ParameterSqlClause(value));

                return;
            }

            if (Expression is MemberExpression member)
            {
                if (member.Member is PropertyInfo property)
                {
                    Clauses.Add(new RawSqlClause($"{member.Expression.Type.GetTableName()}.{property.GetColumnName()}"));

                    return;
                }

                if (member.Member is FieldInfo)
                {
                    Clauses.Add(new ParameterSqlClause(GetMemberExpressionValue(member)));

                    return;
                }

                throw new ArgumentOutOfRangeException(nameof(Expression), "Expression does not refer to a property or field.");
            }

            if (Expression is MethodCallExpression methodCall)
            {
                Clauses.Add(new ParameterSqlClause(GetMethodExpressionValue(methodCall)));

                return;
            }

            throw new ArgumentOutOfRangeException(nameof(Expression), $"Unsupported expression type '{Expression.GetType().Name}'.");
        }