/**
 * END USER LICENSE AGREEMENT (“EULA”)
 *
 * READ THIS AGREEMENT CAREFULLY (date: 9/13/2011):
 * http://www.akiban.com/licensing/20110913
 *
 * BY INSTALLING OR USING ALL OR ANY PORTION OF THE SOFTWARE, YOU ARE ACCEPTING
 * ALL OF THE TERMS AND CONDITIONS OF THIS AGREEMENT. YOU AGREE THAT THIS
 * AGREEMENT IS ENFORCEABLE LIKE ANY WRITTEN AGREEMENT SIGNED BY YOU.
 *
 * IF YOU HAVE PAID A LICENSE FEE FOR USE OF THE SOFTWARE AND DO NOT AGREE TO
 * THESE TERMS, YOU MAY RETURN THE SOFTWARE FOR A FULL REFUND PROVIDED YOU (A) DO
 * NOT USE THE SOFTWARE AND (B) RETURN THE SOFTWARE WITHIN THIRTY (30) DAYS OF
 * YOUR INITIAL PURCHASE.
 *
 * IF YOU WISH TO USE THE SOFTWARE AS AN EMPLOYEE, CONTRACTOR, OR AGENT OF A
 * CORPORATION, PARTNERSHIP OR SIMILAR ENTITY, THEN YOU MUST BE AUTHORIZED TO SIGN
 * FOR AND BIND THE ENTITY IN ORDER TO ACCEPT THE TERMS OF THIS AGREEMENT. THE
 * LICENSES GRANTED UNDER THIS AGREEMENT ARE EXPRESSLY CONDITIONED UPON ACCEPTANCE
 * BY SUCH AUTHORIZED PERSONNEL.
 *
 * IF YOU HAVE ENTERED INTO A SEPARATE WRITTEN LICENSE AGREEMENT WITH AKIBAN FOR
 * USE OF THE SOFTWARE, THE TERMS AND CONDITIONS OF SUCH OTHER AGREEMENT SHALL
 * PREVAIL OVER ANY CONFLICTING TERMS OR CONDITIONS IN THIS AGREEMENT.
 */

package com.akiban.sql.views;

import com.akiban.sql.parser.*;

import com.akiban.sql.StandardException;

public class ViewDefinition
{
    private SQLParserContext parserContext;
    private NodeFactory nodeFactory;
    private CreateViewNode definition;
    private FromSubquery subquery = null;

    /**
     * Parse the given SQL as CREATE VIEW and remember the definition.
     */
    public ViewDefinition(String sql, SQLParser parser)
            throws StandardException {
        this(parser.parseStatement(sql), parser);
    }

    public ViewDefinition(QueryTreeNode parsed, SQLParser parser)
            throws StandardException {
        parserContext = parser;
        nodeFactory = parserContext.getNodeFactory();
        if (parsed.getNodeType() != NodeTypes.CREATE_VIEW_NODE) {
            throw new StandardException("Parsed statement was not a view");
        }
        definition = (CreateViewNode)parsed;
    }

    /** 
     * Get the name of the view.
     */
    public TableName getName() {
        return definition.getObjectName();
    }

    /**
     * @deprecated
     * @see #copySubquery
     **/
    @Deprecated
    public FromSubquery getSubquery(Visitor binder) throws StandardException {
        return copySubquery(binder, parserContext);
    }

    /**
     * Get the view as an equivalent subquery belonging to the given context.
     */
    public FromSubquery copySubquery(Visitor binder, SQLParserContext parserContext) 
            throws StandardException {
        ensureSubquery(binder);
        return (FromSubquery)nodeFactory.copyNode(subquery, parserContext);
    }

    protected void ensureSubquery(Visitor binder) throws StandardException {
        if (subquery == null) {
            subquery = (FromSubquery)
                nodeFactory.getNode(NodeTypes.FROM_SUBQUERY,
                                    definition.getParsedQueryExpression(),
                                    null, null, null,
                                    getName().getTableName(),
                                    definition.getResultColumns(),
                                    null,
                                    parserContext);
            subquery = (FromSubquery)subquery.accept(binder);
        }
    }

}

@@ -1367,89 +1367,8 @@
 n%0A%7B%0A
-    private SQLParserContext parserContext;%0A    private NodeFactory nodeFactory;%0A
     
@@ -1439,15 +1439,8 @@
 uery
- = null
 ;%0A%0A 
@@ -1707,17 +1707,17 @@
 ion(
-QueryTree
+Statement
 Node
@@ -1726,39 +1726,53 @@
 arsed, SQLParser
- parser
+Context parserContext
 )%0A            th
@@ -1800,94 +1800,8 @@
 n %7B%0A
-        parserContext = parser;%0A        nodeFactory = parserContext.getNodeFactory();%0A
     
@@ -2001,298 +2001,1018 @@
     
-%7D%0A%0A    /** %0A     * Get the name of the view.%0A     */%0A    public TableName getName() %7B%0A        return definition.getObjectName();%0A    %7D%0A%0A    /**%0A     * @deprecated%0A     * @see #copySubquery%0A     **/%0A    @Deprecated%0A    public FromSubquery getSubquery(Visitor binder) throws StandardException
+    subquery = (FromSubquery)%0A            parserContext.getNodeFactory().getNode(NodeTypes.FROM_SUBQUERY,%0A                                                   definition.getParsedQueryExpression(),%0A                                                   null, null, null,%0A                                                   getName().getTableName(),%0A                                                   definition.getResultColumns(),%0A                                                   null,%0A                                                   parserContext);%0A    %7D%0A%0A    /** %0A     * Get the name of the view.%0A     */%0A    public TableName getName() %7B%0A        return definition.getObjectName();%0A    %7D%0A%0A    /**%0A     * Get the result columns for this view.%0A     * Also binds the view to detect unresolved references.%0A     */%0A    public ResultColumnList getResultColumns() %7B%0A        return definition.getResultColumns();%0A    %7D%0A%0A    /**%0A     * Get the original subquery for binding.%0A     */%0A    public FromSubquery getSubquery()
  %7B%0A 
@@ -3029,43 +3029,16 @@
 urn 
-copyS
+s
 ubquery
-(binder, parserContext)
 ;%0A  
@@ -3177,24 +3177,8 @@
 ery(
-Visitor binder, 
 SQLP
@@ -3257,62 +3257,60 @@
     
-ensureSubquery(binder);%0A        return (FromSubquery)n
+return (FromSubquery)%0A            parserContext.getN
 odeF
@@ -3315,16 +3315,18 @@
 eFactory
+()
 .copyNod
@@ -3368,29 +3368,108 @@
     
+/**%0A     * @deprecated%0A     * @see #copySubquery%0A     */%0A    @De
 pr
-ot
 ec
+a
 ted
- void ensure
+%0A    public FromSubquery get
 Subq
@@ -3528,519 +3528,85 @@
     
-if (subquery == null) %7B%0A            subquery = (FromSubquery)%0A                nodeFactory.getNode(NodeTypes.FROM_SUBQUERY,%0A                                    definition.getParsedQueryExpression(),%0A                                    null, null, null,%0A                                    getName().getTableName(),%0A                                    definition.getResultColumns(),%0A                                    null,%0A                                    parserContext);%0A            subquery = (From
+subquery = (FromSubquery)subquery.accept(binder);%0A        return copy
 Subquery
 )sub
@@ -3593,33 +3593,33 @@
 urn copySubquery
-)
+(
 subquery.accept(
@@ -3615,33 +3615,28 @@
 ery.
-accept(binder);%0A        %7D
+getParserContext());
 %0A   

Clean up API, but leave enough that old system still passes.