public override SqlExpression VisitAssign(SqlAssignExpression assign)
        {
            if (context.Request == null)
                throw new ExpressionEvaluateException("Cannot assign a variable outside a query context.");

            var valueExpression = Visit(assign.ValueExpression);

            if (valueExpression.ExpressionType != SqlExpressionType.Constant)
                throw new ExpressionEvaluateException("Cannot assign a variable from a non constant value.");

            var reference = assign.ReferenceExpression;
            var value = ((SqlConstantExpression)valueExpression).Value;

            string variableName;

            if (reference is SqlVariableReferenceExpression) {
                variableName = ((SqlVariableReferenceExpression) reference).VariableName;
            } else if (reference is SqlReferenceExpression) {
                var refName = ((SqlReferenceExpression) reference).ReferenceName;
                if (refName.Parent != null)  // This might be the assignment to a complex type attribute
                    throw new NotSupportedException(string.Format("Reference to '{0}' is not supported.", refName));

                variableName = refName.Name;
            } else {
                throw new NotSupportedException();
            }

            try {
                context.Request.SetVariable(variableName, valueExpression);
            } catch (Exception ex) {
                throw new ExpressionEvaluateException(String.Format("Could not assign value to variable '{0}'", reference), ex);
            }

            return SqlExpression.Constant(value);
        }
        public override SqlExpression VisitAssign(SqlAssignExpression assign)
        {
            if (context.QueryContext == null)
                throw new ExpressionEvaluateException("Cannot assign a variable outside a query context.");

            var valueExpression = Visit(assign.ValueExpression);

            if (valueExpression.ExpressionType != SqlExpressionType.Constant)
                throw new ExpressionEvaluateException("Cannot assign a variable from a non constant value.");

            var reference = assign.ReferenceExpression;
            var value = ((SqlConstantExpression)valueExpression).Value;

            if (reference is SqlVariableReferenceExpression) {
                try {
                    var variable = ((SqlVariableReferenceExpression) reference).VariableName;
                    context.QueryContext.SetVariable(variable, value);
                } catch (Exception ex) {
                    throw new ExpressionEvaluateException(String.Format("Could not assign value to variable '{0}'", reference), ex);
                }
            } else {
                throw new NotImplementedException();
            }

            return SqlExpression.Constant(value);
        }
        public override SqlExpression VisitAssign(SqlAssignExpression assign)
        {
            Visit(assign.ReferenceExpression);
            builder.Append(" = ");
            Visit(assign.ValueExpression);

            return assign;
        }
Beispiel #4
0
        public override SqlExpression VisitAssign(SqlAssignExpression assign)
        {
            Visit(assign.ReferenceExpression);
            builder.Append(" = ");
            Visit(assign.ValueExpression);

            return(assign);
        }
Beispiel #5
0
        public void InsertRegular()
        {
            var tableName = ObjectName.Parse("APP.people");
            var assignments = new SqlAssignExpression[] {
                SqlExpression.Assign(SqlExpression.Reference(new ObjectName(tableName, "id")),
                    SqlExpression.Constant(1)),
                SqlExpression.Assign(SqlExpression.Reference(new ObjectName(tableName, "first_name")),
                    SqlExpression.Constant("Antonello")),
                SqlExpression.Assign(SqlExpression.Reference(new ObjectName(tableName, "last_name")),
                    SqlExpression.Constant("Provenzano"))
            };

            Query.InsertIntoTable(tableName, assignments);
        }
        protected override SqlPreparedStatement PrepareStatement(IExpressionPreparer preparer, IQueryContext context)
        {
            var tableName = context.ResolveTableName(TableName);

            var table = context.GetTable(tableName);
            if (table == null)
                throw new InvalidOperationException();

            if (Values.Any(x => x.OfType<SqlQueryExpression>().Any()))
                throw new InvalidOperationException("Cannot set a value from a query.");

            var columnInfos = new List<ColumnInfo>();
            foreach (var name in ColumnNames) {
                var columnName = ObjectName.Parse(name);
                var colIndex = table.FindColumn(columnName);
                if (colIndex < 0)
                    throw new InvalidOperationException(String.Format("Cannot find column '{0}' in table '{1}'", columnName, table.FullName));

                columnInfos.Add(table.TableInfo[colIndex]);
            }

            var assignments = new List<SqlAssignExpression[]>();

            foreach (var valueSet in Values) {
                var valueAssign = new SqlAssignExpression[valueSet.Length];

                for (int i = 0; i < valueSet.Length; i++) {
                    var columnInfo = columnInfos[i];

                    var value = valueSet[i];
                    if (value != null) {
                        // TODO: Deference columns with a preparer
                    }

                    if (value != null) {
                        var expReturnType = value.ReturnType(context, null);
                        if (!columnInfo.ColumnType.IsComparable(expReturnType))
                            throw new InvalidOperationException();
                    }

                    valueAssign[i] = SqlExpression.Assign(SqlExpression.Reference(columnInfo.FullColumnName), value);
                }

                assignments.Add(valueAssign);
            }

            return new Prepared(tableName, assignments);
        }
Beispiel #7
0
        public override SqlExpression VisitAssign(SqlAssignExpression assign)
        {
            if (context.Request == null)
            {
                throw new ExpressionEvaluateException("Cannot assign a variable outside a query context.");
            }

            var valueExpression = Visit(assign.ValueExpression);

            if (valueExpression.ExpressionType != SqlExpressionType.Constant)
            {
                throw new ExpressionEvaluateException("Cannot assign a variable from a non constant value.");
            }

            var reference = assign.ReferenceExpression;
            var value     = ((SqlConstantExpression)valueExpression).Value;

            string variableName;

            if (reference is SqlVariableReferenceExpression)
            {
                variableName = ((SqlVariableReferenceExpression)reference).VariableName;
            }
            else if (reference is SqlReferenceExpression)
            {
                var refName = ((SqlReferenceExpression)reference).ReferenceName;
                if (refName.Parent != null)                  // This might be the assignment to a complex type attribute
                {
                    throw new NotSupportedException(string.Format("Reference to '{0}' is not supported.", refName));
                }

                variableName = refName.Name;
            }
            else
            {
                throw new NotSupportedException();
            }

            try {
                context.Request.SetVariable(variableName, valueExpression);
            } catch (Exception ex) {
                throw new ExpressionEvaluateException(String.Format("Could not assign value to variable '{0}'", reference), ex);
            }


            return(SqlExpression.Constant(value));
        }
Beispiel #8
0
        public override SqlExpression VisitAssign(SqlAssignExpression assign)
        {
            var reference = assign.ReferenceExpression;

            if (reference != null)
            {
                reference = reference.Prepare(preparer);
            }

            var valueExpression = assign.ValueExpression;

            if (valueExpression != null)
            {
                valueExpression = valueExpression.Prepare(preparer);
            }

            return(SqlExpression.Assign(reference, valueExpression));
        }
Beispiel #9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="assign"></param>
        /// <returns></returns>
        public virtual SqlExpression VisitAssign(SqlAssignExpression assign)
        {
            var reference = assign.ReferenceExpression;

            if (reference != null)
            {
                reference = Visit(reference);
            }

            var expression = assign.ValueExpression;

            if (expression != null)
            {
                expression = Visit(expression);
            }

            return(SqlExpression.Assign(reference, expression));
        }
Beispiel #10
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="assign"></param>
        /// <returns></returns>
        public virtual SqlExpression VisitAssign(SqlAssignExpression assign)
        {
            var reference = assign.ReferenceExpression;
            if (reference != null)
                reference = Visit(reference);

            var expression = assign.ValueExpression;
            if (expression != null)
                expression = Visit(expression);

            return SqlExpression.Assign(reference, expression);
        }
Beispiel #11
0
 internal Prepared(ObjectName tableName, IQueryPlanNode queryPlan, SqlAssignExpression[] columns, int limit)
 {
     TableName = tableName;
     QueryPlan = queryPlan;
     Columns = columns;
     Limit = limit;
 }
Beispiel #12
0
        public void EvaluateAssignment(SqlAssignExpression assignExpression, IQueryContext context)
        {
            // Get the variable resolver and evaluate over this data.
            var vresolver = VariableResolver;
            var evalExp = assignExpression.Evaluate(context, vresolver);

            if (evalExp.ExpressionType != SqlExpressionType.Constant)
                throw new InvalidOperationException();

            var value = ((SqlConstantExpression) evalExp).Value;

            // Check the column name is within this row.
            var variable = ((SqlReferenceExpression) assignExpression.ReferenceExpression).ReferenceName;
            int column = Table.FindColumn(variable);

            SetValue(column, value);
        }
Beispiel #13
0
            private Prepared(SerializationInfo info, StreamingContext context)
            {
                TableName = (ObjectName) info.GetValue("TableName", typeof (ObjectName));
                int setCount = info.GetInt32("SetCount");
                var assignmenets = new SqlAssignExpression[setCount][];
                for (int i = 0; i < setCount; i++) {
                    assignmenets[i] =
                        (SqlAssignExpression[]) info.GetValue(String.Format("Assign[{0}]", i), typeof (SqlAssignExpression[]));
                }

                Assignments = assignmenets;
            }
Beispiel #14
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var values = Values.ToArray();

            int firstLen = -1;
            for (int n = 0; n < values.Length; ++n) {
                var expList = (IList)values[n];
                if (firstLen == -1 || firstLen == expList.Count) {
                    firstLen = expList.Count;
                } else {
                    throw new InvalidOperationException("The insert data list varies in size.");
                }
            }

            var tableName = context.Access().ResolveTableName(TableName);

            var table = context.Access().GetTable(tableName);
            if (table == null)
                throw new ObjectNotFoundException(TableName);

            if (Values.Any(x => x.OfType<SqlQueryExpression>().Any()))
                throw new InvalidOperationException("Cannot set a value from a query.");

            var tableQueryInfo = context.Access().GetTableQueryInfo(tableName, null);
            var fromTable = new FromTableDirectSource(context.Query.IgnoreIdentifiersCase(), tableQueryInfo, "INSERT_TABLE", tableName, tableName);

            var columns = new string[0];
            if (ColumnNames != null)
                columns = ColumnNames.ToArray();

            if (columns.Length == 0) {
                columns = new string[table.TableInfo.ColumnCount];
                for (int i = 0; i < columns.Length; i++) {
                    columns[i] = table.TableInfo[i].ColumnName;
                }
            }

            var colIndices = new int[columns.Length];
            var colResolved = new ObjectName[columns.Length];
            for (int i = 0; i < columns.Length; ++i) {
                var inVar = new ObjectName(columns[i]);
                var col = ResolveColumn(fromTable, inVar);
                int index = table.FindColumn(col);
                if (index == -1)
                    throw new InvalidOperationException(String.Format("Cannot find column '{0}' in table '{1}'.", col, tableName));

                colIndices[i] = index;
                colResolved[i] = col;
            }

            var columnInfos = new List<ColumnInfo>();
            foreach (var name in columns) {
                var columnName = new ObjectName(tableName, name);
                var colIndex = table.FindColumn(columnName);
                if (colIndex < 0)
                    throw new InvalidOperationException(String.Format("Cannot find column '{0}' in table '{1}'", columnName, table.TableInfo.TableName));

                columnInfos.Add(table.TableInfo[colIndex]);
            }

            var assignments = new List<SqlAssignExpression[]>();

            foreach (var valueSet in values) {
                var valueAssign = new SqlAssignExpression[valueSet.Length];

                for (int i = 0; i < valueSet.Length; i++) {
                    var columnInfo = columnInfos[i];

                    var value = valueSet[i];
                    if (value != null) {
                        // TODO: Deference columns with a preparer
                    }

                    if (value != null) {
                        var expReturnType = value.ReturnType(context, null);
                        if (!expReturnType.Equals(columnInfo.ColumnType) &&
                            !expReturnType.CanCastTo(columnInfo.ColumnType))
                        {
                            var sb = new StringBuilder ();
                            sb.AppendFormat ("Unable to convert type {0} of {1} into type {2} of column {3}", expReturnType, value, columnInfo.ColumnType, columnInfo.FullColumnName.FullName);
                            var ioe = new InvalidOperationException (sb.ToString());
                            throw ioe;
                        }
                    }

                    valueAssign[i] = SqlExpression.Assign(SqlExpression.Reference(columnInfo.FullColumnName), value);
                }

                assignments.Add(valueAssign);
            }

            return new Prepared(tableName, assignments);
        }
Beispiel #15
0
        public void EvaluateAssignment(SqlAssignExpression assignExpression, IQuery context)
        {
            var colRef = (SqlReferenceExpression) assignExpression.ReferenceExpression;
            var valueExp = assignExpression.ValueExpression;
            var value = valueExp.EvaluateToConstant(context, VariableResolver);

            // Check the column name is within this row.
            var columnName = colRef.ReferenceName;
            int column = Table.FindColumn(columnName);

            if (column == -1)
                throw new ObjectNotFoundException(columnName,
                    String.Format("Table '{0}' has none column named '{1}': cannot assign.", Table.TableInfo.TableName, columnName));

            SetValue(column, value);
        }
            private Prepared(ObjectData data)
            {
                TableName = data.GetValue<ObjectName>("TableName");
                int setCount = data.GetInt32("SetCount");
                var assignmenets = new SqlAssignExpression[setCount][];
                for (int i = 0; i < setCount; i++) {
                    assignmenets[i] = data.GetValue<SqlAssignExpression[]>(String.Format("Assign[{0}]", i));
                }

                Assignments = assignmenets;
            }