shishkovilja commented on code in PR #13841:
URL: https://github.com/apache/ignite/pull/13841#discussion_r4676900115
##########
modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSchemaIndexingTest.java:
##########
@@ -194,6 +200,206 @@ public void testSchemaEscapeAll() throws Exception {
ignite(0).destroyCache(cfgEsc.getName());
}
+ /**
+ * Tests that a schema name with a quote does not create SCHEMA_INJECTED,
+ * a row is readable through that schema, and destroying the cache drops
the schema.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testSchemaNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String schemaName = "A\";CREATE TABLE PUBLIC.SCHEMA_INJECTED(ID
INT);--";
+
+ String quotedSchema = "\"" + schemaName.replace("\"", "\"\"") + "\"";
+
+ IgniteCache<Integer, Fact> cache =
ignite(0).createCache(cacheConfig("stmtSchema", true, Integer.class, Fact.class)
+ .setSqlSchema("\"" + schemaName + "\""));
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select f.name from " + quotedSchema + ".Fact f")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ assertFalse("Schema is not dropped in H2", h2SchemaExists(schemaName));
+ }
+
+ /**
+ * Tests that a table name with a quote does not create TABLE_INJECTED,
+ * a row is readable through that table, and destroying the cache drops
the table.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testTableNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String tblName = "A\";CREATE TABLE PUBLIC.TABLE_INJECTED(ID INT);--";
+
+ String quotedTbl = "\"" + tblName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity()
+ .setTableName(tblName)
+ .setKeyType(Integer.class.getName())
+ .setValueFieldName("name")
+ .setValueType(String.class.getName())
+ .addQueryField("name", String.class.getName(), null);
+
+ IgniteCache<Integer, String> cache = ignite(0).createCache(new
CacheConfiguration<Integer, String>("stmtTable")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ cache.put(1, "v");
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select name from " + quotedTbl)).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ assertFalse("Table is not dropped in H2", h2TableExists(tblName));
+ }
+
+ /**
+ * Tests that creating a cache with this column name does not create
COLUMN_INJECTED,
+ * the index on that column is created, and a row is readable through that
column.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testColumnNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String colName = "C\" VARCHAR);CREATE TABLE PUBLIC.COLUMN_INJECTED(ID
INT);--";
+
+ String quotedCol = "\"" + colName.replace("\"", "\"\"") + "\"";
Review Comment:
Move it just before line 308 (current usage).
##########
modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2Utils.java:
##########
Review Comment:
```suggestion
* Generate {@code DROP INDEX} SQL statement for given params.
```
##########
modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSchemaIndexingTest.java:
##########
@@ -194,6 +200,206 @@ public void testSchemaEscapeAll() throws Exception {
ignite(0).destroyCache(cfgEsc.getName());
}
+ /**
+ * Tests that a schema name with a quote does not create SCHEMA_INJECTED,
+ * a row is readable through that schema, and destroying the cache drops
the schema.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testSchemaNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String schemaName = "A\";CREATE TABLE PUBLIC.SCHEMA_INJECTED(ID
INT);--";
+
+ String quotedSchema = "\"" + schemaName.replace("\"", "\"\"") + "\"";
Review Comment:
Let's move it just before its' usage (currenlty used at line 224):
```
>>> Place it here
List<List<?>> rows = cache.query(new SqlFieldsQuery(
"select f.name from " + quotedSchema + ".Fact f")).getAll();
```
##########
modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSchemaIndexingTest.java:
##########
@@ -194,6 +200,206 @@ public void testSchemaEscapeAll() throws Exception {
ignite(0).destroyCache(cfgEsc.getName());
}
+ /**
+ * Tests that a schema name with a quote does not create SCHEMA_INJECTED,
+ * a row is readable through that schema, and destroying the cache drops
the schema.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testSchemaNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String schemaName = "A\";CREATE TABLE PUBLIC.SCHEMA_INJECTED(ID
INT);--";
+
+ String quotedSchema = "\"" + schemaName.replace("\"", "\"\"") + "\"";
+
+ IgniteCache<Integer, Fact> cache =
ignite(0).createCache(cacheConfig("stmtSchema", true, Integer.class, Fact.class)
+ .setSqlSchema("\"" + schemaName + "\""));
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select f.name from " + quotedSchema + ".Fact f")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ assertFalse("Schema is not dropped in H2", h2SchemaExists(schemaName));
+ }
+
+ /**
+ * Tests that a table name with a quote does not create TABLE_INJECTED,
+ * a row is readable through that table, and destroying the cache drops
the table.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testTableNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String tblName = "A\";CREATE TABLE PUBLIC.TABLE_INJECTED(ID INT);--";
+
+ String quotedTbl = "\"" + tblName.replace("\"", "\"\"") + "\"";
Review Comment:
The same: move it just before the usage place (currently used at line 265).
##########
modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSchemaIndexingTest.java:
##########
@@ -194,6 +200,206 @@ public void testSchemaEscapeAll() throws Exception {
ignite(0).destroyCache(cfgEsc.getName());
}
+ /**
+ * Tests that a schema name with a quote does not create SCHEMA_INJECTED,
+ * a row is readable through that schema, and destroying the cache drops
the schema.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testSchemaNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String schemaName = "A\";CREATE TABLE PUBLIC.SCHEMA_INJECTED(ID
INT);--";
+
+ String quotedSchema = "\"" + schemaName.replace("\"", "\"\"") + "\"";
+
+ IgniteCache<Integer, Fact> cache =
ignite(0).createCache(cacheConfig("stmtSchema", true, Integer.class, Fact.class)
+ .setSqlSchema("\"" + schemaName + "\""));
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select f.name from " + quotedSchema + ".Fact f")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ assertFalse("Schema is not dropped in H2", h2SchemaExists(schemaName));
+ }
+
+ /**
+ * Tests that a table name with a quote does not create TABLE_INJECTED,
+ * a row is readable through that table, and destroying the cache drops
the table.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testTableNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String tblName = "A\";CREATE TABLE PUBLIC.TABLE_INJECTED(ID INT);--";
+
+ String quotedTbl = "\"" + tblName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity()
+ .setTableName(tblName)
+ .setKeyType(Integer.class.getName())
+ .setValueFieldName("name")
+ .setValueType(String.class.getName())
+ .addQueryField("name", String.class.getName(), null);
+
+ IgniteCache<Integer, String> cache = ignite(0).createCache(new
CacheConfiguration<Integer, String>("stmtTable")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ cache.put(1, "v");
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select name from " + quotedTbl)).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ assertFalse("Table is not dropped in H2", h2TableExists(tblName));
Review Comment:
Can be replaced with `assertH2TableAbsent`
##########
modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSchemaIndexingTest.java:
##########
@@ -194,6 +200,206 @@ public void testSchemaEscapeAll() throws Exception {
ignite(0).destroyCache(cfgEsc.getName());
}
+ /**
+ * Tests that a schema name with a quote does not create SCHEMA_INJECTED,
+ * a row is readable through that schema, and destroying the cache drops
the schema.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testSchemaNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String schemaName = "A\";CREATE TABLE PUBLIC.SCHEMA_INJECTED(ID
INT);--";
+
+ String quotedSchema = "\"" + schemaName.replace("\"", "\"\"") + "\"";
+
+ IgniteCache<Integer, Fact> cache =
ignite(0).createCache(cacheConfig("stmtSchema", true, Integer.class, Fact.class)
+ .setSqlSchema("\"" + schemaName + "\""));
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select f.name from " + quotedSchema + ".Fact f")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ assertFalse("Schema is not dropped in H2", h2SchemaExists(schemaName));
+ }
+
+ /**
+ * Tests that a table name with a quote does not create TABLE_INJECTED,
+ * a row is readable through that table, and destroying the cache drops
the table.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testTableNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String tblName = "A\";CREATE TABLE PUBLIC.TABLE_INJECTED(ID INT);--";
+
+ String quotedTbl = "\"" + tblName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity()
+ .setTableName(tblName)
+ .setKeyType(Integer.class.getName())
+ .setValueFieldName("name")
+ .setValueType(String.class.getName())
+ .addQueryField("name", String.class.getName(), null);
+
+ IgniteCache<Integer, String> cache = ignite(0).createCache(new
CacheConfiguration<Integer, String>("stmtTable")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ cache.put(1, "v");
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select name from " + quotedTbl)).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ assertFalse("Table is not dropped in H2", h2TableExists(tblName));
+ }
+
+ /**
+ * Tests that creating a cache with this column name does not create
COLUMN_INJECTED,
+ * the index on that column is created, and a row is readable through that
column.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testColumnNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String colName = "C\" VARCHAR);CREATE TABLE PUBLIC.COLUMN_INJECTED(ID
INT);--";
+
+ String quotedCol = "\"" + colName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity(Integer.class.getName(),
Fact.class.getName())
+ .setTableName("FACT")
+ .addQueryField("id", Integer.class.getName(), null)
+ .addQueryField("name", String.class.getName(), colName)
+ .setIndexes(Collections.singletonList(new
QueryIndex("name").setName("NAME_IDX")));
+
+ IgniteCache<Integer, Fact> cache = ignite(0).createCache(new
CacheConfiguration<Integer, Fact>("colCache")
+ .setSqlSchema("COL_SCHEMA")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("COLUMN_INJECTED");
+
+ assertTrue("Index is not created in H2", h2IndexExists("COL_SCHEMA",
"NAME_IDX"));
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select " + quotedCol + " from COL_SCHEMA.FACT where " + quotedCol
+ " = ?").setArgs("v")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+ }
+
+ /**
+ * Tests that creating and dropping an index with this name does not
create INDEX_INJECTED,
+ * and the index is created and dropped in H2.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testIndexNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String idxName = "I\";CREATE TABLE PUBLIC.INDEX_INJECTED(ID INT);--";
+
+ String quotedIdx = "\"" + idxName.replace("\"", "\"\"") + "\"";
Review Comment:
Move it just before usage (currently used at line 343).
##########
modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSchemaIndexingTest.java:
##########
@@ -194,6 +200,206 @@ public void testSchemaEscapeAll() throws Exception {
ignite(0).destroyCache(cfgEsc.getName());
}
+ /**
Review Comment:
There is no necessity in such excessive JavaDoc, can be shortened, e.g.:
`/** Tests a schema whose name contains a quote. */`
##########
modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSchemaIndexingTest.java:
##########
@@ -194,6 +200,206 @@ public void testSchemaEscapeAll() throws Exception {
ignite(0).destroyCache(cfgEsc.getName());
}
+ /**
+ * Tests that a schema name with a quote does not create SCHEMA_INJECTED,
+ * a row is readable through that schema, and destroying the cache drops
the schema.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testSchemaNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String schemaName = "A\";CREATE TABLE PUBLIC.SCHEMA_INJECTED(ID
INT);--";
+
+ String quotedSchema = "\"" + schemaName.replace("\"", "\"\"") + "\"";
+
+ IgniteCache<Integer, Fact> cache =
ignite(0).createCache(cacheConfig("stmtSchema", true, Integer.class, Fact.class)
+ .setSqlSchema("\"" + schemaName + "\""));
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select f.name from " + quotedSchema + ".Fact f")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ assertFalse("Schema is not dropped in H2", h2SchemaExists(schemaName));
+ }
+
+ /**
+ * Tests that a table name with a quote does not create TABLE_INJECTED,
+ * a row is readable through that table, and destroying the cache drops
the table.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testTableNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String tblName = "A\";CREATE TABLE PUBLIC.TABLE_INJECTED(ID INT);--";
+
+ String quotedTbl = "\"" + tblName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity()
+ .setTableName(tblName)
+ .setKeyType(Integer.class.getName())
+ .setValueFieldName("name")
+ .setValueType(String.class.getName())
+ .addQueryField("name", String.class.getName(), null);
+
+ IgniteCache<Integer, String> cache = ignite(0).createCache(new
CacheConfiguration<Integer, String>("stmtTable")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ cache.put(1, "v");
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select name from " + quotedTbl)).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ assertFalse("Table is not dropped in H2", h2TableExists(tblName));
+ }
+
+ /**
+ * Tests that creating a cache with this column name does not create
COLUMN_INJECTED,
+ * the index on that column is created, and a row is readable through that
column.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testColumnNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String colName = "C\" VARCHAR);CREATE TABLE PUBLIC.COLUMN_INJECTED(ID
INT);--";
+
+ String quotedCol = "\"" + colName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity(Integer.class.getName(),
Fact.class.getName())
+ .setTableName("FACT")
+ .addQueryField("id", Integer.class.getName(), null)
+ .addQueryField("name", String.class.getName(), colName)
+ .setIndexes(Collections.singletonList(new
QueryIndex("name").setName("NAME_IDX")));
+
+ IgniteCache<Integer, Fact> cache = ignite(0).createCache(new
CacheConfiguration<Integer, Fact>("colCache")
+ .setSqlSchema("COL_SCHEMA")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("COLUMN_INJECTED");
+
+ assertTrue("Index is not created in H2", h2IndexExists("COL_SCHEMA",
"NAME_IDX"));
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select " + quotedCol + " from COL_SCHEMA.FACT where " + quotedCol
+ " = ?").setArgs("v")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+ }
+
+ /**
+ * Tests that creating and dropping an index with this name does not
create INDEX_INJECTED,
+ * and the index is created and dropped in H2.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testIndexNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String idxName = "I\";CREATE TABLE PUBLIC.INDEX_INJECTED(ID INT);--";
+
+ String quotedIdx = "\"" + idxName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity(Integer.class.getName(),
Fact.class.getName())
+ .setTableName("FACT")
+ .addQueryField("id", Integer.class.getName(), null)
+ .addQueryField("name", String.class.getName(), null)
+ .setIndexes(Collections.singletonList(new
QueryIndex("name").setName(idxName)));
+
+ IgniteCache<Integer, Fact> cache = ignite(0).createCache(new
CacheConfiguration<Integer, Fact>("idxCache")
+ .setSqlSchema("IDX_SCHEMA")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("INDEX_INJECTED");
+
+ assertTrue("Index is not created in H2", h2IndexExists("IDX_SCHEMA",
idxName));
+
+ cache.query(new SqlFieldsQuery("DROP INDEX IDX_SCHEMA." +
quotedIdx)).getAll();
+
+ assertH2TableAbsent("INDEX_INJECTED");
+
+ assertFalse("Index is not dropped in H2", h2IndexExists("IDX_SCHEMA",
idxName));
+ }
+
+ /**
+ * @param tblName Table name.
+ */
+ private void assertH2TableAbsent(String tblName) throws Exception {
+ assertFalse("Unexpected H2 table: " + tblName, h2TableExists(tblName));
+ }
+
+ /**
+ * @param schemaName Schema name.
+ * @return {@code True} if H2 has the schema.
+ */
+ private boolean h2SchemaExists(String schemaName) throws Exception {
+ return h2RowExists(
+ "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE
SCHEMA_NAME = ?",
+ schemaName);
+ }
+
+ /**
+ * @param tblName Table name.
+ * @return {@code True} if H2 has the table.
+ */
+ private boolean h2TableExists(String tblName) throws Exception {
+ return h2RowExists(
+ "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME
= ?",
+ tblName);
+ }
+
+ /**
+ * @param schemaName Schema name.
Review Comment:
Can be shortened to `/** */`
##########
modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSchemaIndexingTest.java:
##########
@@ -194,6 +200,206 @@ public void testSchemaEscapeAll() throws Exception {
ignite(0).destroyCache(cfgEsc.getName());
}
+ /**
+ * Tests that a schema name with a quote does not create SCHEMA_INJECTED,
+ * a row is readable through that schema, and destroying the cache drops
the schema.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testSchemaNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String schemaName = "A\";CREATE TABLE PUBLIC.SCHEMA_INJECTED(ID
INT);--";
+
+ String quotedSchema = "\"" + schemaName.replace("\"", "\"\"") + "\"";
+
+ IgniteCache<Integer, Fact> cache =
ignite(0).createCache(cacheConfig("stmtSchema", true, Integer.class, Fact.class)
+ .setSqlSchema("\"" + schemaName + "\""));
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select f.name from " + quotedSchema + ".Fact f")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ assertFalse("Schema is not dropped in H2", h2SchemaExists(schemaName));
+ }
+
+ /**
+ * Tests that a table name with a quote does not create TABLE_INJECTED,
+ * a row is readable through that table, and destroying the cache drops
the table.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testTableNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String tblName = "A\";CREATE TABLE PUBLIC.TABLE_INJECTED(ID INT);--";
+
+ String quotedTbl = "\"" + tblName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity()
+ .setTableName(tblName)
+ .setKeyType(Integer.class.getName())
+ .setValueFieldName("name")
+ .setValueType(String.class.getName())
+ .addQueryField("name", String.class.getName(), null);
+
+ IgniteCache<Integer, String> cache = ignite(0).createCache(new
CacheConfiguration<Integer, String>("stmtTable")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ cache.put(1, "v");
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select name from " + quotedTbl)).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ assertFalse("Table is not dropped in H2", h2TableExists(tblName));
+ }
+
+ /**
+ * Tests that creating a cache with this column name does not create
COLUMN_INJECTED,
+ * the index on that column is created, and a row is readable through that
column.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testColumnNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String colName = "C\" VARCHAR);CREATE TABLE PUBLIC.COLUMN_INJECTED(ID
INT);--";
+
+ String quotedCol = "\"" + colName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity(Integer.class.getName(),
Fact.class.getName())
+ .setTableName("FACT")
+ .addQueryField("id", Integer.class.getName(), null)
+ .addQueryField("name", String.class.getName(), colName)
+ .setIndexes(Collections.singletonList(new
QueryIndex("name").setName("NAME_IDX")));
+
+ IgniteCache<Integer, Fact> cache = ignite(0).createCache(new
CacheConfiguration<Integer, Fact>("colCache")
+ .setSqlSchema("COL_SCHEMA")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("COLUMN_INJECTED");
+
+ assertTrue("Index is not created in H2", h2IndexExists("COL_SCHEMA",
"NAME_IDX"));
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select " + quotedCol + " from COL_SCHEMA.FACT where " + quotedCol
+ " = ?").setArgs("v")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+ }
+
+ /**
+ * Tests that creating and dropping an index with this name does not
create INDEX_INJECTED,
+ * and the index is created and dropped in H2.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testIndexNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String idxName = "I\";CREATE TABLE PUBLIC.INDEX_INJECTED(ID INT);--";
+
+ String quotedIdx = "\"" + idxName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity(Integer.class.getName(),
Fact.class.getName())
+ .setTableName("FACT")
+ .addQueryField("id", Integer.class.getName(), null)
+ .addQueryField("name", String.class.getName(), null)
+ .setIndexes(Collections.singletonList(new
QueryIndex("name").setName(idxName)));
+
+ IgniteCache<Integer, Fact> cache = ignite(0).createCache(new
CacheConfiguration<Integer, Fact>("idxCache")
+ .setSqlSchema("IDX_SCHEMA")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("INDEX_INJECTED");
+
+ assertTrue("Index is not created in H2", h2IndexExists("IDX_SCHEMA",
idxName));
+
+ cache.query(new SqlFieldsQuery("DROP INDEX IDX_SCHEMA." +
quotedIdx)).getAll();
+
+ assertH2TableAbsent("INDEX_INJECTED");
+
+ assertFalse("Index is not dropped in H2", h2IndexExists("IDX_SCHEMA",
idxName));
+ }
+
+ /**
+ * @param tblName Table name.
Review Comment:
Can be shortened to `/** */`
##########
modules/calcite-testing/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/ViewsIntegrationTest.java:
##########
@@ -381,6 +387,55 @@ public void testSysSchema() throws Exception {
assertThrows("DROP VIEW IF EXISTS sys.views",
IgniteSQLException.class, msg);
}
+ /**
+ * Tests that a view name with a quote does not run a second statement in
the H2 database,
Review Comment:
Can be shortened:
`/** Tests a view whose name contains a quote. */`
##########
modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSchemaIndexingTest.java:
##########
@@ -194,6 +200,206 @@ public void testSchemaEscapeAll() throws Exception {
ignite(0).destroyCache(cfgEsc.getName());
}
+ /**
+ * Tests that a schema name with a quote does not create SCHEMA_INJECTED,
+ * a row is readable through that schema, and destroying the cache drops
the schema.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testSchemaNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String schemaName = "A\";CREATE TABLE PUBLIC.SCHEMA_INJECTED(ID
INT);--";
+
+ String quotedSchema = "\"" + schemaName.replace("\"", "\"\"") + "\"";
+
+ IgniteCache<Integer, Fact> cache =
ignite(0).createCache(cacheConfig("stmtSchema", true, Integer.class, Fact.class)
+ .setSqlSchema("\"" + schemaName + "\""));
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select f.name from " + quotedSchema + ".Fact f")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ assertFalse("Schema is not dropped in H2", h2SchemaExists(schemaName));
+ }
+
+ /**
+ * Tests that a table name with a quote does not create TABLE_INJECTED,
+ * a row is readable through that table, and destroying the cache drops
the table.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testTableNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String tblName = "A\";CREATE TABLE PUBLIC.TABLE_INJECTED(ID INT);--";
+
+ String quotedTbl = "\"" + tblName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity()
+ .setTableName(tblName)
+ .setKeyType(Integer.class.getName())
+ .setValueFieldName("name")
+ .setValueType(String.class.getName())
+ .addQueryField("name", String.class.getName(), null);
+
+ IgniteCache<Integer, String> cache = ignite(0).createCache(new
CacheConfiguration<Integer, String>("stmtTable")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ cache.put(1, "v");
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select name from " + quotedTbl)).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ assertFalse("Table is not dropped in H2", h2TableExists(tblName));
+ }
+
+ /**
+ * Tests that creating a cache with this column name does not create
COLUMN_INJECTED,
+ * the index on that column is created, and a row is readable through that
column.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testColumnNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String colName = "C\" VARCHAR);CREATE TABLE PUBLIC.COLUMN_INJECTED(ID
INT);--";
+
+ String quotedCol = "\"" + colName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity(Integer.class.getName(),
Fact.class.getName())
+ .setTableName("FACT")
+ .addQueryField("id", Integer.class.getName(), null)
+ .addQueryField("name", String.class.getName(), colName)
+ .setIndexes(Collections.singletonList(new
QueryIndex("name").setName("NAME_IDX")));
+
+ IgniteCache<Integer, Fact> cache = ignite(0).createCache(new
CacheConfiguration<Integer, Fact>("colCache")
+ .setSqlSchema("COL_SCHEMA")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("COLUMN_INJECTED");
+
+ assertTrue("Index is not created in H2", h2IndexExists("COL_SCHEMA",
"NAME_IDX"));
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select " + quotedCol + " from COL_SCHEMA.FACT where " + quotedCol
+ " = ?").setArgs("v")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+ }
+
+ /**
+ * Tests that creating and dropping an index with this name does not
create INDEX_INJECTED,
+ * and the index is created and dropped in H2.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testIndexNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String idxName = "I\";CREATE TABLE PUBLIC.INDEX_INJECTED(ID INT);--";
+
+ String quotedIdx = "\"" + idxName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity(Integer.class.getName(),
Fact.class.getName())
+ .setTableName("FACT")
+ .addQueryField("id", Integer.class.getName(), null)
+ .addQueryField("name", String.class.getName(), null)
+ .setIndexes(Collections.singletonList(new
QueryIndex("name").setName(idxName)));
+
+ IgniteCache<Integer, Fact> cache = ignite(0).createCache(new
CacheConfiguration<Integer, Fact>("idxCache")
+ .setSqlSchema("IDX_SCHEMA")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("INDEX_INJECTED");
+
+ assertTrue("Index is not created in H2", h2IndexExists("IDX_SCHEMA",
idxName));
+
+ cache.query(new SqlFieldsQuery("DROP INDEX IDX_SCHEMA." +
quotedIdx)).getAll();
+
+ assertH2TableAbsent("INDEX_INJECTED");
+
+ assertFalse("Index is not dropped in H2", h2IndexExists("IDX_SCHEMA",
idxName));
+ }
+
+ /**
+ * @param tblName Table name.
+ */
+ private void assertH2TableAbsent(String tblName) throws Exception {
+ assertFalse("Unexpected H2 table: " + tblName, h2TableExists(tblName));
+ }
+
+ /**
+ * @param schemaName Schema name.
+ * @return {@code True} if H2 has the schema.
+ */
+ private boolean h2SchemaExists(String schemaName) throws Exception {
+ return h2RowExists(
+ "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE
SCHEMA_NAME = ?",
+ schemaName);
+ }
+
+ /**
+ * @param tblName Table name.
Review Comment:
Can be shortened to `/** */`
##########
modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSchemaIndexingTest.java:
##########
@@ -194,6 +200,206 @@ public void testSchemaEscapeAll() throws Exception {
ignite(0).destroyCache(cfgEsc.getName());
}
+ /**
+ * Tests that a schema name with a quote does not create SCHEMA_INJECTED,
+ * a row is readable through that schema, and destroying the cache drops
the schema.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testSchemaNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String schemaName = "A\";CREATE TABLE PUBLIC.SCHEMA_INJECTED(ID
INT);--";
+
+ String quotedSchema = "\"" + schemaName.replace("\"", "\"\"") + "\"";
+
+ IgniteCache<Integer, Fact> cache =
ignite(0).createCache(cacheConfig("stmtSchema", true, Integer.class, Fact.class)
+ .setSqlSchema("\"" + schemaName + "\""));
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select f.name from " + quotedSchema + ".Fact f")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ assertFalse("Schema is not dropped in H2", h2SchemaExists(schemaName));
+ }
+
+ /**
Review Comment:
There is no necessity in such excessive JavaDoc, can be shortened, e.g.:
`/** Tests a table whose name contains a quote. */`
##########
modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSchemaIndexingTest.java:
##########
@@ -194,6 +200,206 @@ public void testSchemaEscapeAll() throws Exception {
ignite(0).destroyCache(cfgEsc.getName());
}
+ /**
+ * Tests that a schema name with a quote does not create SCHEMA_INJECTED,
+ * a row is readable through that schema, and destroying the cache drops
the schema.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testSchemaNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String schemaName = "A\";CREATE TABLE PUBLIC.SCHEMA_INJECTED(ID
INT);--";
+
+ String quotedSchema = "\"" + schemaName.replace("\"", "\"\"") + "\"";
+
+ IgniteCache<Integer, Fact> cache =
ignite(0).createCache(cacheConfig("stmtSchema", true, Integer.class, Fact.class)
+ .setSqlSchema("\"" + schemaName + "\""));
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select f.name from " + quotedSchema + ".Fact f")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ assertFalse("Schema is not dropped in H2", h2SchemaExists(schemaName));
+ }
+
+ /**
+ * Tests that a table name with a quote does not create TABLE_INJECTED,
+ * a row is readable through that table, and destroying the cache drops
the table.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testTableNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String tblName = "A\";CREATE TABLE PUBLIC.TABLE_INJECTED(ID INT);--";
+
+ String quotedTbl = "\"" + tblName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity()
+ .setTableName(tblName)
+ .setKeyType(Integer.class.getName())
+ .setValueFieldName("name")
+ .setValueType(String.class.getName())
+ .addQueryField("name", String.class.getName(), null);
+
+ IgniteCache<Integer, String> cache = ignite(0).createCache(new
CacheConfiguration<Integer, String>("stmtTable")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ cache.put(1, "v");
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select name from " + quotedTbl)).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ assertFalse("Table is not dropped in H2", h2TableExists(tblName));
+ }
+
+ /**
+ * Tests that creating a cache with this column name does not create
COLUMN_INJECTED,
Review Comment:
There is no necessity in such excessive JavaDoc, can be shortened, e.g.:
/** Tests a column whose name contains a quote. */
##########
modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2Utils.java:
##########
Review Comment:
```suggestion
* @param schemaName Schema name.
```
##########
modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSchemaIndexingTest.java:
##########
@@ -194,6 +200,206 @@ public void testSchemaEscapeAll() throws Exception {
ignite(0).destroyCache(cfgEsc.getName());
}
+ /**
+ * Tests that a schema name with a quote does not create SCHEMA_INJECTED,
+ * a row is readable through that schema, and destroying the cache drops
the schema.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testSchemaNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String schemaName = "A\";CREATE TABLE PUBLIC.SCHEMA_INJECTED(ID
INT);--";
+
+ String quotedSchema = "\"" + schemaName.replace("\"", "\"\"") + "\"";
+
+ IgniteCache<Integer, Fact> cache =
ignite(0).createCache(cacheConfig("stmtSchema", true, Integer.class, Fact.class)
+ .setSqlSchema("\"" + schemaName + "\""));
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select f.name from " + quotedSchema + ".Fact f")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ assertFalse("Schema is not dropped in H2", h2SchemaExists(schemaName));
+ }
+
+ /**
+ * Tests that a table name with a quote does not create TABLE_INJECTED,
+ * a row is readable through that table, and destroying the cache drops
the table.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testTableNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String tblName = "A\";CREATE TABLE PUBLIC.TABLE_INJECTED(ID INT);--";
+
+ String quotedTbl = "\"" + tblName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity()
+ .setTableName(tblName)
+ .setKeyType(Integer.class.getName())
+ .setValueFieldName("name")
+ .setValueType(String.class.getName())
+ .addQueryField("name", String.class.getName(), null);
+
+ IgniteCache<Integer, String> cache = ignite(0).createCache(new
CacheConfiguration<Integer, String>("stmtTable")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ cache.put(1, "v");
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select name from " + quotedTbl)).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ assertFalse("Table is not dropped in H2", h2TableExists(tblName));
+ }
+
+ /**
+ * Tests that creating a cache with this column name does not create
COLUMN_INJECTED,
+ * the index on that column is created, and a row is readable through that
column.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testColumnNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String colName = "C\" VARCHAR);CREATE TABLE PUBLIC.COLUMN_INJECTED(ID
INT);--";
+
+ String quotedCol = "\"" + colName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity(Integer.class.getName(),
Fact.class.getName())
+ .setTableName("FACT")
+ .addQueryField("id", Integer.class.getName(), null)
+ .addQueryField("name", String.class.getName(), colName)
+ .setIndexes(Collections.singletonList(new
QueryIndex("name").setName("NAME_IDX")));
+
+ IgniteCache<Integer, Fact> cache = ignite(0).createCache(new
CacheConfiguration<Integer, Fact>("colCache")
+ .setSqlSchema("COL_SCHEMA")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("COLUMN_INJECTED");
+
+ assertTrue("Index is not created in H2", h2IndexExists("COL_SCHEMA",
"NAME_IDX"));
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select " + quotedCol + " from COL_SCHEMA.FACT where " + quotedCol
+ " = ?").setArgs("v")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+ }
+
+ /**
+ * Tests that creating and dropping an index with this name does not
create INDEX_INJECTED,
Review Comment:
There is no necessity in such excessive JavaDoc, can be shortened, e.g.:
/** Tests a index whose name contains a quote. */
##########
modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2Utils.java:
##########
@@ -300,13 +298,13 @@ public static boolean checkSpatialIndexEnabled() {
}
/**
- * Add quotes around the name.
+ * Add quotes around the name. A quote inside the name is doubled so it
stays inside that identifier.
Review Comment:
Sounds more correctly:
```
/**
* Quotes an identifier, doubling quotes inside it.
*
* @param str Identifier.
* @return Quoted identifier.
*/
```
##########
modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSchemaIndexingTest.java:
##########
@@ -194,6 +200,206 @@ public void testSchemaEscapeAll() throws Exception {
ignite(0).destroyCache(cfgEsc.getName());
}
+ /**
+ * Tests that a schema name with a quote does not create SCHEMA_INJECTED,
+ * a row is readable through that schema, and destroying the cache drops
the schema.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testSchemaNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String schemaName = "A\";CREATE TABLE PUBLIC.SCHEMA_INJECTED(ID
INT);--";
+
+ String quotedSchema = "\"" + schemaName.replace("\"", "\"\"") + "\"";
+
+ IgniteCache<Integer, Fact> cache =
ignite(0).createCache(cacheConfig("stmtSchema", true, Integer.class, Fact.class)
+ .setSqlSchema("\"" + schemaName + "\""));
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select f.name from " + quotedSchema + ".Fact f")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ assertFalse("Schema is not dropped in H2", h2SchemaExists(schemaName));
+ }
+
+ /**
+ * Tests that a table name with a quote does not create TABLE_INJECTED,
+ * a row is readable through that table, and destroying the cache drops
the table.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testTableNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String tblName = "A\";CREATE TABLE PUBLIC.TABLE_INJECTED(ID INT);--";
+
+ String quotedTbl = "\"" + tblName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity()
+ .setTableName(tblName)
+ .setKeyType(Integer.class.getName())
+ .setValueFieldName("name")
+ .setValueType(String.class.getName())
+ .addQueryField("name", String.class.getName(), null);
+
+ IgniteCache<Integer, String> cache = ignite(0).createCache(new
CacheConfiguration<Integer, String>("stmtTable")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ cache.put(1, "v");
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select name from " + quotedTbl)).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ assertFalse("Table is not dropped in H2", h2TableExists(tblName));
+ }
+
+ /**
+ * Tests that creating a cache with this column name does not create
COLUMN_INJECTED,
+ * the index on that column is created, and a row is readable through that
column.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testColumnNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String colName = "C\" VARCHAR);CREATE TABLE PUBLIC.COLUMN_INJECTED(ID
INT);--";
+
+ String quotedCol = "\"" + colName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity(Integer.class.getName(),
Fact.class.getName())
+ .setTableName("FACT")
+ .addQueryField("id", Integer.class.getName(), null)
+ .addQueryField("name", String.class.getName(), colName)
+ .setIndexes(Collections.singletonList(new
QueryIndex("name").setName("NAME_IDX")));
+
+ IgniteCache<Integer, Fact> cache = ignite(0).createCache(new
CacheConfiguration<Integer, Fact>("colCache")
+ .setSqlSchema("COL_SCHEMA")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("COLUMN_INJECTED");
+
+ assertTrue("Index is not created in H2", h2IndexExists("COL_SCHEMA",
"NAME_IDX"));
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select " + quotedCol + " from COL_SCHEMA.FACT where " + quotedCol
+ " = ?").setArgs("v")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+ }
+
+ /**
+ * Tests that creating and dropping an index with this name does not
create INDEX_INJECTED,
+ * and the index is created and dropped in H2.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testIndexNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String idxName = "I\";CREATE TABLE PUBLIC.INDEX_INJECTED(ID INT);--";
+
+ String quotedIdx = "\"" + idxName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity(Integer.class.getName(),
Fact.class.getName())
+ .setTableName("FACT")
+ .addQueryField("id", Integer.class.getName(), null)
+ .addQueryField("name", String.class.getName(), null)
+ .setIndexes(Collections.singletonList(new
QueryIndex("name").setName(idxName)));
+
+ IgniteCache<Integer, Fact> cache = ignite(0).createCache(new
CacheConfiguration<Integer, Fact>("idxCache")
+ .setSqlSchema("IDX_SCHEMA")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("INDEX_INJECTED");
+
+ assertTrue("Index is not created in H2", h2IndexExists("IDX_SCHEMA",
idxName));
+
+ cache.query(new SqlFieldsQuery("DROP INDEX IDX_SCHEMA." +
quotedIdx)).getAll();
+
+ assertH2TableAbsent("INDEX_INJECTED");
+
+ assertFalse("Index is not dropped in H2", h2IndexExists("IDX_SCHEMA",
idxName));
+ }
+
+ /**
+ * @param tblName Table name.
+ */
+ private void assertH2TableAbsent(String tblName) throws Exception {
+ assertFalse("Unexpected H2 table: " + tblName, h2TableExists(tblName));
+ }
+
+ /**
+ * @param schemaName Schema name.
+ * @return {@code True} if H2 has the schema.
+ */
+ private boolean h2SchemaExists(String schemaName) throws Exception {
+ return h2RowExists(
+ "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE
SCHEMA_NAME = ?",
+ schemaName);
+ }
+
+ /**
+ * @param tblName Table name.
+ * @return {@code True} if H2 has the table.
+ */
+ private boolean h2TableExists(String tblName) throws Exception {
+ return h2RowExists(
+ "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME
= ?",
+ tblName);
+ }
+
+ /**
+ * @param schemaName Schema name.
+ * @param idxName Index name.
+ * @return {@code True} if H2 has the index.
+ */
+ private boolean h2IndexExists(String schemaName, String idxName) throws
Exception {
+ return h2RowExists(
+ "SELECT INDEX_NAME FROM INFORMATION_SCHEMA.INDEXES WHERE
TABLE_SCHEMA = ? AND INDEX_NAME = ?",
+ schemaName, idxName);
+ }
+
+ /** */
Review Comment:
```suggestion
/**
* Runs a query on the internal H2 database. H2 errors in schema
listeners are only logged and Ignite metadata
* stays correct, so a wrong DDL statement in H2 is visible only there.
*/
```
##########
modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSchemaIndexingTest.java:
##########
@@ -194,6 +200,206 @@ public void testSchemaEscapeAll() throws Exception {
ignite(0).destroyCache(cfgEsc.getName());
}
+ /**
+ * Tests that a schema name with a quote does not create SCHEMA_INJECTED,
+ * a row is readable through that schema, and destroying the cache drops
the schema.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testSchemaNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String schemaName = "A\";CREATE TABLE PUBLIC.SCHEMA_INJECTED(ID
INT);--";
+
+ String quotedSchema = "\"" + schemaName.replace("\"", "\"\"") + "\"";
+
+ IgniteCache<Integer, Fact> cache =
ignite(0).createCache(cacheConfig("stmtSchema", true, Integer.class, Fact.class)
+ .setSqlSchema("\"" + schemaName + "\""));
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select f.name from " + quotedSchema + ".Fact f")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("SCHEMA_INJECTED");
+
+ assertFalse("Schema is not dropped in H2", h2SchemaExists(schemaName));
+ }
+
+ /**
+ * Tests that a table name with a quote does not create TABLE_INJECTED,
+ * a row is readable through that table, and destroying the cache drops
the table.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testTableNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String tblName = "A\";CREATE TABLE PUBLIC.TABLE_INJECTED(ID INT);--";
+
+ String quotedTbl = "\"" + tblName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity()
+ .setTableName(tblName)
+ .setKeyType(Integer.class.getName())
+ .setValueFieldName("name")
+ .setValueType(String.class.getName())
+ .addQueryField("name", String.class.getName(), null);
+
+ IgniteCache<Integer, String> cache = ignite(0).createCache(new
CacheConfiguration<Integer, String>("stmtTable")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ cache.put(1, "v");
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select name from " + quotedTbl)).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+
+ ignite(0).destroyCache(cache.getName());
+
+ assertH2TableAbsent("TABLE_INJECTED");
+
+ assertFalse("Table is not dropped in H2", h2TableExists(tblName));
+ }
+
+ /**
+ * Tests that creating a cache with this column name does not create
COLUMN_INJECTED,
+ * the index on that column is created, and a row is readable through that
column.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testColumnNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String colName = "C\" VARCHAR);CREATE TABLE PUBLIC.COLUMN_INJECTED(ID
INT);--";
+
+ String quotedCol = "\"" + colName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity(Integer.class.getName(),
Fact.class.getName())
+ .setTableName("FACT")
+ .addQueryField("id", Integer.class.getName(), null)
+ .addQueryField("name", String.class.getName(), colName)
+ .setIndexes(Collections.singletonList(new
QueryIndex("name").setName("NAME_IDX")));
+
+ IgniteCache<Integer, Fact> cache = ignite(0).createCache(new
CacheConfiguration<Integer, Fact>("colCache")
+ .setSqlSchema("COL_SCHEMA")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("COLUMN_INJECTED");
+
+ assertTrue("Index is not created in H2", h2IndexExists("COL_SCHEMA",
"NAME_IDX"));
+
+ cache.put(1, new Fact(1, "v"));
+
+ List<List<?>> rows = cache.query(new SqlFieldsQuery(
+ "select " + quotedCol + " from COL_SCHEMA.FACT where " + quotedCol
+ " = ?").setArgs("v")).getAll();
+
+ assertEquals(1, rows.size());
+ assertEquals("v", rows.get(0).get(0));
+ }
+
+ /**
+ * Tests that creating and dropping an index with this name does not
create INDEX_INJECTED,
+ * and the index is created and dropped in H2.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testIndexNameWithQuote() throws Exception {
+ startGrid(0);
+
+ String idxName = "I\";CREATE TABLE PUBLIC.INDEX_INJECTED(ID INT);--";
+
+ String quotedIdx = "\"" + idxName.replace("\"", "\"\"") + "\"";
+
+ QueryEntity entity = new QueryEntity(Integer.class.getName(),
Fact.class.getName())
+ .setTableName("FACT")
+ .addQueryField("id", Integer.class.getName(), null)
+ .addQueryField("name", String.class.getName(), null)
+ .setIndexes(Collections.singletonList(new
QueryIndex("name").setName(idxName)));
+
+ IgniteCache<Integer, Fact> cache = ignite(0).createCache(new
CacheConfiguration<Integer, Fact>("idxCache")
+ .setSqlSchema("IDX_SCHEMA")
+ .setQueryEntities(Collections.singletonList(entity)));
+
+ assertH2TableAbsent("INDEX_INJECTED");
+
+ assertTrue("Index is not created in H2", h2IndexExists("IDX_SCHEMA",
idxName));
+
+ cache.query(new SqlFieldsQuery("DROP INDEX IDX_SCHEMA." +
quotedIdx)).getAll();
+
+ assertH2TableAbsent("INDEX_INJECTED");
+
+ assertFalse("Index is not dropped in H2", h2IndexExists("IDX_SCHEMA",
idxName));
+ }
+
+ /**
+ * @param tblName Table name.
+ */
+ private void assertH2TableAbsent(String tblName) throws Exception {
+ assertFalse("Unexpected H2 table: " + tblName, h2TableExists(tblName));
+ }
+
+ /**
+ * @param schemaName Schema name.
Review Comment:
Can be shortened to `/** */`
##########
modules/calcite-testing/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/ViewsIntegrationTest.java:
##########
@@ -381,6 +387,55 @@ public void testSysSchema() throws Exception {
assertThrows("DROP VIEW IF EXISTS sys.views",
IgniteSQLException.class, msg);
}
+ /**
+ * Tests that a view name with a quote does not run a second statement in
the H2 database,
+ * and the view is created and dropped in H2.
+ */
+ @Test
+ public void testViewNameWithQuote() throws Exception {
+ assumeTrue(CalciteQueryEngineConfiguration.ENGINE_NAME.equals(engine),
+ "Only Calcite parser supports doubled quotes inside quoted
identifiers");
+
+ initGrids(1);
+ initTable(3);
+
+ String viewName = "V\" AS SELECT 1;CREATE TABLE
PUBLIC.VIEW_INJECTED(ID INT);--";
+
+ String quotedView = "\"" + viewName.replace("\"", "\"\"") + "\"";
+
+ sql("create view " + quotedView + " as select * from my_table");
+
+ assertViewsCount("PUBLIC", viewName, 1);
+
+ assertFalse("Unexpected H2 table: VIEW_INJECTED",
h2TableExists("PUBLIC", "VIEW_INJECTED"));
+ assertTrue("View is not created in H2", h2TableExists("PUBLIC",
viewName));
+
+ assertRowsCount(3, "select * from " + quotedView);
+
+ sql("drop view " + quotedView);
+
+ assertViewsCount("PUBLIC", viewName, 0);
+
+ assertFalse("Unexpected H2 table: VIEW_INJECTED",
h2TableExists("PUBLIC", "VIEW_INJECTED"));
+ assertFalse("View is not dropped in H2", h2TableExists("PUBLIC",
viewName));
+ }
+
+ /** */
Review Comment:
```suggestion
/**
* Checks a table or view in the internal H2 database. SYS views and
Calcite queries use Ignite metadata,
* and H2 errors during view registration are only logged, so a wrong
statement in H2 is visible only there.
*/
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]