/**
 * 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.aisddl;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull;

import java.sql.Statement;
import java.sql.SQLException;

import com.akiban.ais.model.AkibanInformationSchema;
import com.akiban.ais.model.Column;
import com.akiban.ais.model.Table;
import com.akiban.ais.model.View;
import com.akiban.sql.pg.PostgresServerITBase;

import java.util.Collection;

public class ViewDDLIT extends PostgresServerITBase {
    private Statement stmt;

    @Before
    public void createStatement() throws Exception {
        stmt = getConnection().createStatement();
        
        stmt.executeUpdate("CREATE TABLE t(id INT PRIMARY KEY NOT NULL, s VARCHAR(10))");
    }

    @After
    public void closeStatement() throws Exception {
        stmt.close();
    }

    public void testCreate() throws Exception {
        stmt.executeUpdate("CREATE VIEW v AS SELECT * FROM t");
        View v = ddl().getAIS(session()).getView(SCHEMA_NAME, "v");
        assertNotNull(v);
        assertEquals(2, v.getColumns().size());
        assertEquals("id", v.getColumn(0).getName());
        assertEquals("s", v.getColumn(1).getName());
        Table t = ddl().getAIS(session()).getTable(SCHEMA_NAME, "t");
        assertEquals(1, v.getTableReferences().size());
        Collection<Column> tcols = v.getTableColumnReferences(t);
        assertNotNull(tcols);
        assertEquals(2, tcols.size());
    }

    @Test(expected=SQLException.class)
    public void testDropNonexistent() throws Exception {
        stmt.executeUpdate("DROP VIEW no_such_view");
    }

    @Test
    public void testDropOptionalNonexistent() throws Exception {
        stmt.executeUpdate("DROP VIEW IF EXISTS no_such_view");
    }

    @Test
    public void testDropExists() throws Exception {
        stmt.executeUpdate("CREATE VIEW v AS SELECT * FROM t");
        assertNotNull(ddl().getAIS(session()).getView(SCHEMA_NAME, "v"));

        stmt.executeUpdate("DROP VIEW v");
        assertNull(ddl().getAIS(session()).getView(SCHEMA_NAME, "v"));
    }

    @Test
    public void testDropOptionalExists() throws Exception {
        stmt.executeUpdate("CREATE VIEW v AS SELECT * FROM t");
        assertNotNull(ddl().getAIS(session()).getView(SCHEMA_NAME, "v"));

        stmt.executeUpdate("DROP VIEW IF EXISTS v");
        assertNull(ddl().getAIS(session()).getView(SCHEMA_NAME, "v"));
    }

    @Test(expected=SQLException.class)
    public void testDropTableReferenced() throws Exception {
        stmt.executeUpdate("CREATE VIEW v AS SELECT * FROM t");
        stmt.executeUpdate("DROP TABLE t");
    }

    @Test(expected=SQLException.class)
    public void testDropViewReferenced() throws Exception {
        stmt.executeUpdate("CREATE VIEW v1 AS SELECT * FROM t");
        stmt.executeUpdate("CREATE VIEW v2 AS SELECT * FROM v1");
        stmt.executeUpdate("DROP VIEW v1");
    }

}

@@ -1741,84 +1741,744 @@
 ort 
-java.util.Collection;%0A%0Apublic class ViewDDLIT extends PostgresServerITBase %7B
+com.akiban.server.service.config.Property;%0Aimport com.akiban.server.service.is.BasicInfoSchemaTablesService;%0Aimport com.akiban.server.service.is.BasicInfoSchemaTablesServiceImpl;%0Aimport com.akiban.server.service.servicemanager.GuicedServiceManager;%0A%0Aimport java.util.Collection;%0A%0Apublic class ViewDDLIT extends PostgresServerITBase %7B%0A    @Override%0A    protected GuicedServiceManager.BindingsConfigurationProvider serviceBindingsProvider() %7B%0A        return super.serviceBindingsProvider()%0A                .bind(BasicInfoSchemaTablesService.class, BasicInfoSchemaTablesServiceImpl.class)%0A;%0A    %7D%0A%0A    @Override%0A    protected Collection%3CProperty%3E startupConfigProperties() %7B%0A        return uniqueStartupConfigProperties(getClass());%0A    %7D%0A
 %0A   
@@ -4906,10 +4906,362 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testSystemView() throws Exception %7B%0A        serviceManager().getServiceByClass(BasicInfoSchemaTablesService.class);%0A        stmt.executeUpdate(%22CREATE VIEW v AS SELECT table_name FROM information_schema.tables WHERE table_schema %3C%3E 'information_schema'%22);%0A        forgetConnection();%0A        safeRestartTestServices();%0A    %7D%0A%0A
 %7D%0A

Add an IT that shows the problem with views on system tables.