protected override SqlStatement PrepareStatement(IRequest context)
        {
            var tableName = context.Access().ResolveTableName(TableName);

            if (!context.Access().TableExists(tableName))
            {
                throw new ObjectNotFoundException(tableName);
            }

            var queryExpression = new SqlQueryExpression(new[] { SelectColumn.Glob("*") });

            queryExpression.FromClause.AddTable(tableName.FullName);
            queryExpression.WhereExpression = WherExpression;

            var queryFrom = QueryExpressionFrom.Create(context, queryExpression);
            var queryPlan = context.Query.Context.QueryPlanner().PlanQuery(new QueryInfo(context, queryExpression));

            var columns = new List <SqlAssignExpression>();

            foreach (var assignment in Assignments)
            {
                var columnName = ObjectName.Parse(assignment.ColumnName);

                var refName    = queryFrom.ResolveReference(columnName);
                var expression = assignment.Expression.Prepare(queryFrom.ExpressionPreparer);

                var assign = SqlExpression.Assign(SqlExpression.Reference(refName), expression);
                columns.Add(assign);
            }

            return(new Prepared(tableName, queryPlan, columns.ToArray(), Limit));
        }
        IStatement IPreparableStatement.Prepare(IRequest context)
        {
            var viewName = context.Query.ResolveTableName(ViewName);

            var queryFrom = QueryExpressionFrom.Create(context, QueryExpression);
            var queryPlan = context.Query.Context.QueryPlanner().PlanQuery(new QueryInfo(context, QueryExpression));

            var colList = ColumnNames == null ? new string[0] : ColumnNames.ToArray();

            // Wrap the result around a SubsetNode to alias the columns in the
            // table correctly for this view.
            int sz             = colList.Length;
            var originalNames  = queryFrom.GetResolvedColumns();
            var newColumnNames = new ObjectName[originalNames.Length];

            if (sz > 0)
            {
                if (sz != originalNames.Length)
                {
                    throw new InvalidOperationException("Column list is not the same size as the columns selected.");
                }

                for (int i = 0; i < sz; ++i)
                {
                    var colName = colList[i];
                    newColumnNames[i] = new ObjectName(viewName, colName);
                }
            }
            else
            {
                sz = originalNames.Length;
                for (int i = 0; i < sz; ++i)
                {
                    newColumnNames[i] = new ObjectName(viewName, originalNames[i].Name);
                }
            }

            // Check there are no repeat column names in the table.
            for (int i = 0; i < sz; ++i)
            {
                var columnName = newColumnNames[i];
                for (int n = i + 1; n < sz; ++n)
                {
                    if (newColumnNames[n].Equals(columnName))
                    {
                        throw new InvalidOperationException(String.Format("Duplicate column name '{0}' in view. A view may not contain duplicate column names.", columnName));
                    }
                }
            }

            // Wrap the plan around a SubsetNode plan
            queryPlan = new SubsetNode(queryPlan, originalNames, newColumnNames);

            return(new Prepared(viewName, QueryExpression, queryPlan, ReplaceIfExists));
        }
        IStatement IPreparableStatement.Prepare(IRequest request)
        {
            var cursor = request.FindCursor(CursorName);

            if (cursor == null)
            {
                throw new ObjectNotFoundException(new ObjectName(CursorName), "The source cursor was not found.");
            }

            var tableName = request.Query.ResolveTableName(TableName);

            if (tableName == null)
            {
                throw new ObjectNotFoundException(TableName);
            }

            var table = request.Query.GetMutableTable(tableName);

            if (table == null)
            {
                throw new ObjectNotFoundException(tableName);
            }

            var columns = table.TableInfo.Select(x => new ObjectName(tableName, x.ColumnName));

            var queryExpression = cursor.QueryExpression;
            var queryFrom       = QueryExpressionFrom.Create(request, queryExpression);

            var assignments = new List <SqlColumnAssignment>();

            foreach (var column in columns)
            {
                // TODO:
            }

            // TODO: get the columns from the table and the columns exposed by the cursor
            //       and then make a set of assignments
            throw new NotImplementedException();
        }