IGNITE-5305: CREATE TABLE and DROP TABLE now correctly pass schema name cache configuration. This closes #2055.
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/a93af295 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/a93af295 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/a93af295 Branch: refs/heads/ignite-5075 Commit: a93af295649cabc2e5d4b4a035c922570809e371 Parents: be3d606 Author: Alexander Paschenko <[email protected]> Authored: Fri Jun 2 14:51:56 2017 +0300 Committer: devozerov <[email protected]> Committed: Fri Jun 2 14:51:56 2017 +0300 ---------------------------------------------------------------------- .../processors/query/GridQueryProcessor.java | 1 + .../query/h2/ddl/DdlStatementsProcessor.java | 10 ++ .../cache/index/H2DynamicTableSelfTest.java | 103 ++++++++++++++----- 3 files changed, 90 insertions(+), 24 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/a93af295/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java index fef7d4c..ea8f890 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java @@ -1297,6 +1297,7 @@ public class GridQueryProcessor extends GridProcessorAdapter { newCfg.setName(entity.getTableName()); newCfg.setQueryEntities(Collections.singleton(entity)); + newCfg.setSqlSchema(schemaName); // Preserve user specified names as they are. newCfg.setSqlEscapeAll(true); http://git-wip-us.apache.org/repos/asf/ignite/blob/a93af295/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/ddl/DdlStatementsProcessor.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/ddl/DdlStatementsProcessor.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/ddl/DdlStatementsProcessor.java index 04a0c62..d278e37 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/ddl/DdlStatementsProcessor.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/ddl/DdlStatementsProcessor.java @@ -33,6 +33,7 @@ import org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode; import org.apache.ignite.internal.processors.query.GridQueryProperty; import org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor; import org.apache.ignite.internal.processors.query.IgniteSQLException; +import org.apache.ignite.internal.processors.query.QueryUtils; import org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing; import org.apache.ignite.internal.processors.query.h2.opt.GridH2Table; import org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn; @@ -44,6 +45,7 @@ import org.apache.ignite.internal.processors.query.h2.sql.GridSqlQueryParser; import org.apache.ignite.internal.processors.query.h2.sql.GridSqlStatement; import org.apache.ignite.internal.processors.query.schema.SchemaOperationException; import org.apache.ignite.internal.util.future.GridFinishedFuture; +import org.apache.ignite.internal.util.typedef.F; import org.h2.command.Prepared; import org.h2.command.ddl.CreateIndex; import org.h2.command.ddl.CreateTable; @@ -148,6 +150,10 @@ public class DdlStatementsProcessor { else if (stmt0 instanceof GridSqlCreateTable) { GridSqlCreateTable cmd = (GridSqlCreateTable)stmt0; + if (!F.eq(QueryUtils.DFLT_SCHEMA, cmd.schemaName())) + throw new SchemaOperationException("CREATE TABLE can only be executed on " + + QueryUtils.DFLT_SCHEMA + " schema."); + GridH2Table tbl = idx.dataTable(cmd.schemaName(), cmd.tableName()); if (tbl != null) { @@ -162,6 +168,10 @@ public class DdlStatementsProcessor { else if (stmt0 instanceof GridSqlDropTable) { GridSqlDropTable cmd = (GridSqlDropTable)stmt0; + if (!F.eq(QueryUtils.DFLT_SCHEMA, cmd.schemaName())) + throw new SchemaOperationException("DROP TABLE can only be executed on " + + QueryUtils.DFLT_SCHEMA + " schema."); + GridH2Table tbl = idx.dataTable(cmd.schemaName(), cmd.tableName()); if (tbl == null) { http://git-wip-us.apache.org/repos/asf/ignite/blob/a93af295/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/index/H2DynamicTableSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/index/H2DynamicTableSelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/index/H2DynamicTableSelfTest.java index 4d74bac..6f0dec6 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/index/H2DynamicTableSelfTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/index/H2DynamicTableSelfTest.java @@ -40,6 +40,7 @@ import org.apache.ignite.internal.processors.cache.DynamicCacheDescriptor; import org.apache.ignite.internal.processors.query.GridQueryProperty; import org.apache.ignite.internal.processors.query.IgniteSQLException; import org.apache.ignite.internal.processors.query.QueryTypeDescriptorImpl; +import org.apache.ignite.internal.processors.query.QueryUtils; import org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing; import org.apache.ignite.internal.processors.query.h2.opt.GridH2Table; import org.apache.ignite.internal.util.typedef.F; @@ -55,6 +56,9 @@ public class H2DynamicTableSelfTest extends AbstractSchemaSelfTest { /** */ private final static String INDEXED_CACHE_NAME = CACHE_NAME + "_idx"; + /** */ + private final static String INDEXED_CACHE_NAME_2 = INDEXED_CACHE_NAME + "_2"; + /** {@inheritDoc} */ @Override protected void beforeTestsStarted() throws Exception { super.beforeTestsStarted(); @@ -77,12 +81,13 @@ public class H2DynamicTableSelfTest extends AbstractSchemaSelfTest { super.beforeTest(); client().getOrCreateCache(cacheConfigurationForIndexing()); + client().getOrCreateCache(cacheConfigurationForIndexingInPublicSchema()); } /** {@inheritDoc} */ @Override protected void afterTest() throws Exception { if (client().cache("Person") != null) - cache().query(new SqlFieldsQuery("DROP TABLE IF EXISTS \"Person\".\"Person\"")); + executeDdl("DROP TABLE IF EXISTS PUBLIC.\"Person\""); super.afterTest(); } @@ -92,9 +97,9 @@ public class H2DynamicTableSelfTest extends AbstractSchemaSelfTest { * @throws Exception if failed. */ public void testCreateTable() throws Exception { - cache().query(new SqlFieldsQuery("CREATE TABLE \"Person\" (\"id\" int, \"city\" varchar," + + executeDdl("CREATE TABLE \"Person\" (\"id\" int, \"city\" varchar," + " \"name\" varchar, \"surname\" varchar, \"age\" int, PRIMARY KEY (\"id\", \"city\")) WITH " + - "\"cacheTemplate=cache\"")); + "\"cacheTemplate=cache\""); for (int i = 0; i < 4; i++) { IgniteEx node = grid(i); @@ -132,7 +137,7 @@ public class H2DynamicTableSelfTest extends AbstractSchemaSelfTest { assertProperty(desc, "age", Integer.class, false); - GridH2Table tbl = ((IgniteH2Indexing)node.context().query().getIndexing()).dataTable("Person", "Person"); + GridH2Table tbl = ((IgniteH2Indexing)node.context().query().getIndexing()).dataTable("PUBLIC", "Person"); assertNotNull(tbl); } @@ -144,13 +149,13 @@ public class H2DynamicTableSelfTest extends AbstractSchemaSelfTest { * @throws Exception if failed. */ public void testCreateTableIfNotExists() throws Exception { - cache().query(new SqlFieldsQuery("CREATE TABLE \"Person\" (\"id\" int, \"city\" varchar," + + executeDdl("CREATE TABLE \"Person\" (\"id\" int, \"city\" varchar," + " \"name\" varchar, \"surname\" varchar, \"age\" int, PRIMARY KEY (\"id\", \"city\")) WITH " + - "\"cacheTemplate=cache\"")); + "\"cacheTemplate=cache\""); - cache().query(new SqlFieldsQuery("CREATE TABLE IF NOT EXISTS \"Person\" (\"id\" int, \"city\" varchar," + + executeDdl("CREATE TABLE IF NOT EXISTS \"Person\" (\"id\" int, \"city\" varchar," + " \"name\" varchar, \"surname\" varchar, \"age\" int, PRIMARY KEY (\"id\", \"city\")) WITH " + - "\"cacheTemplate=cache\"")); + "\"cacheTemplate=cache\""); } /** @@ -159,15 +164,15 @@ public class H2DynamicTableSelfTest extends AbstractSchemaSelfTest { */ @SuppressWarnings("ThrowableResultOfMethodCallIgnored") public void testCreateExistingTable() throws Exception { - cache().query(new SqlFieldsQuery("CREATE TABLE \"Person\" (\"id\" int, \"city\" varchar," + + executeDdl("CREATE TABLE \"Person\" (\"id\" int, \"city\" varchar," + " \"name\" varchar, \"surname\" varchar, \"age\" int, PRIMARY KEY (\"id\", \"city\")) WITH " + - "\"cacheTemplate=cache\"")); + "\"cacheTemplate=cache\""); GridTestUtils.assertThrows(null, new Callable<Object>() { @Override public Object call() throws Exception { - cache().query(new SqlFieldsQuery("CREATE TABLE \"Person\" (\"id\" int, \"city\" varchar" + + executeDdl("CREATE TABLE \"Person\" (\"id\" int, \"city\" varchar" + ", \"name\" varchar, \"surname\" varchar, \"age\" int, PRIMARY KEY (\"id\", \"city\")) WITH " + - "\"cacheTemplate=cache\"")); + "\"cacheTemplate=cache\""); return null; } @@ -179,11 +184,11 @@ public class H2DynamicTableSelfTest extends AbstractSchemaSelfTest { * @throws Exception if failed. */ public void testDropTable() throws Exception { - cache().query(new SqlFieldsQuery("CREATE TABLE IF NOT EXISTS \"Person\" (\"id\" int, \"city\" varchar," + + executeDdl("CREATE TABLE IF NOT EXISTS \"Person\" (\"id\" int, \"city\" varchar," + " \"name\" varchar, \"surname\" varchar, \"age\" int, PRIMARY KEY (\"id\", \"city\")) WITH " + - "\"cacheTemplate=cache\"")); + "\"cacheTemplate=cache\""); - cache().query(new SqlFieldsQuery("DROP TABLE \"Person\".\"Person\"")); + executeDdl("DROP TABLE \"Person\""); for (int i = 0; i < 4; i++) { IgniteEx node = grid(i); @@ -199,10 +204,11 @@ public class H2DynamicTableSelfTest extends AbstractSchemaSelfTest { /** * Test that attempting to {@code DROP TABLE} that does not exist does not yield an error if the statement contains * {@code IF EXISTS} clause. + * * @throws Exception if failed. */ public void testDropMissingTableIfExists() throws Exception { - cache().query(new SqlFieldsQuery("DROP TABLE IF EXISTS \"cache_idx\".\"City\"")); + executeDdl("DROP TABLE IF EXISTS \"City\""); } /** @@ -213,7 +219,7 @@ public class H2DynamicTableSelfTest extends AbstractSchemaSelfTest { public void testDropMissingTable() throws Exception { GridTestUtils.assertThrows(null, new Callable<Object>() { @Override public Object call() throws Exception { - cache().query(new SqlFieldsQuery("DROP TABLE \"cache_idx\".\"City\"")); + executeDdl("DROP TABLE \"City\""); return null; } @@ -228,12 +234,12 @@ public class H2DynamicTableSelfTest extends AbstractSchemaSelfTest { public void testDropNonDynamicTable() throws Exception { GridTestUtils.assertThrows(null, new Callable<Object>() { @Override public Object call() throws Exception { - cache().query(new SqlFieldsQuery("DROP TABLE \"Integer\"")); + executeDdl("DROP TABLE PUBLIC.\"Integer\""); return null; } }, IgniteSQLException.class, - "Only cache created with CREATE TABLE may be removed with DROP TABLE [cacheName=cache_idx]"); + "Only cache created with CREATE TABLE may be removed with DROP TABLE [cacheName=cache_idx_2]"); } /** @@ -242,9 +248,9 @@ public class H2DynamicTableSelfTest extends AbstractSchemaSelfTest { */ @SuppressWarnings("ThrowableResultOfMethodCallIgnored") public void testDestroyDynamicSqlCache() throws Exception { - cache().query(new SqlFieldsQuery("CREATE TABLE \"Person\" (\"id\" int, \"city\" varchar," + + executeDdl("CREATE TABLE \"Person\" (\"id\" int, \"city\" varchar," + " \"name\" varchar, \"surname\" varchar, \"age\" int, PRIMARY KEY (\"id\", \"city\")) WITH " + - "\"cacheTemplate=cache\"")); + "\"cacheTemplate=cache\""); GridTestUtils.assertThrows(null, new Callable<Object>() { @Override public Object call() throws Exception { @@ -263,9 +269,8 @@ public class H2DynamicTableSelfTest extends AbstractSchemaSelfTest { */ @SuppressWarnings("ThrowableResultOfMethodCallIgnored") public void testSqlFlagCompatibilityCheck() throws Exception { - cache().query(new SqlFieldsQuery("CREATE TABLE \"Person\" (\"id\" int, \"city\" varchar," + - " \"name\" varchar, \"surname\" varchar, \"age\" int, PRIMARY KEY (\"id\", \"city\")) WITH " + - "\"cacheTemplate=cache\"")); + executeDdl("CREATE TABLE \"Person\" (\"id\" int, \"city\" varchar, \"name\" varchar, \"surname\" varchar, " + + "\"age\" int, PRIMARY KEY (\"id\", \"city\")) WITH \"cacheTemplate=cache\""); GridTestUtils.assertThrows(null, new Callable<Object>() { @Override public Object call() throws Exception { @@ -277,6 +282,49 @@ public class H2DynamicTableSelfTest extends AbstractSchemaSelfTest { } /** + * Test that {@code CREATE TABLE} on non-public schema causes an exception. + * + * @throws Exception if failed. + */ + @SuppressWarnings("ThrowableResultOfMethodCallIgnored") + public void testCreateTableNotPublicSchema() throws Exception { + GridTestUtils.assertThrows(null, new Callable<Object>() { + @Override public Object call() throws Exception { + executeDdl("CREATE TABLE \"cache_idx\".\"Person\" (\"id\" int, \"city\" varchar," + + " \"name\" varchar, \"surname\" varchar, \"age\" int, PRIMARY KEY (\"id\", \"city\")) WITH " + + "\"cacheTemplate=cache\""); + + return null; + } + }, IgniteSQLException.class, "CREATE TABLE can only be executed on PUBLIC schema."); + } + + /** + * Test that {@code DROP TABLE} on non-public schema causes an exception. + * + * @throws Exception if failed. + */ + @SuppressWarnings("ThrowableResultOfMethodCallIgnored") + public void testDropTableNotPublicSchema() throws Exception { + GridTestUtils.assertThrows(null, new Callable<Object>() { + @Override public Object call() throws Exception { + executeDdl("DROP TABLE \"cache_idx\".\"Person\""); + + return null; + } + }, IgniteSQLException.class, "DROP TABLE can only be executed on PUBLIC schema."); + } + + /** + * Execute DDL statement. + * + * @param sql Statement. + */ + private void executeDdl(String sql) { + queryProcessor(client()).querySqlFieldsNoCache(new SqlFieldsQuery(sql).setSchema("PUBLIC"), true); + } + + /** * Check that a property in given descriptor is present and has parameters as expected. * @param desc Descriptor. * @param name Property name. @@ -390,4 +438,11 @@ public class H2DynamicTableSelfTest extends AbstractSchemaSelfTest { return ccfg; } + + /** + * @return Cache configuration with query entities in {@code PUBLIC} schema. + */ + private CacheConfiguration cacheConfigurationForIndexingInPublicSchema() { + return cacheConfigurationForIndexing().setName(INDEXED_CACHE_NAME_2).setSqlSchema(QueryUtils.DFLT_SCHEMA); + } }
