Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import org.h2.result.SortOrder;
import org.h2.table.IndexColumn;
import org.h2.util.LocalDateTimeUtils;
import org.h2.util.StringUtils;
import org.h2.value.CompareMode;
import org.h2.value.DataType;
import org.h2.value.Value;
Expand Down Expand Up @@ -147,9 +148,6 @@ public class H2Utils {
private static final String SPATIAL_IDX_FACTORY_CLS =
"org.apache.ignite.internal.processors.query.h2.opt.GeoSpatialUtils";

/** Quotation character. */
private static final char ESC_CH = '\"';

/**
* @param c1 First column.
* @param c2 Second column.
Expand Down Expand Up @@ -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.
*
* @param str String.
* @return String with quotes.
* @param str Name.
* @return Quoted name.
*/
public static String withQuotes(String str) {
return ESC_CH + str + ESC_CH;
return StringUtils.quoteIdentifier(str);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

package org.apache.ignite.internal.processors.query;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import javax.cache.CacheException;
Expand All @@ -26,11 +29,13 @@
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.CacheAtomicityMode;
import org.apache.ignite.cache.CacheMode;
import org.apache.ignite.cache.QueryEntity;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.cache.query.annotations.QuerySqlField;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest;
import org.apache.ignite.internal.processors.query.h2.H2PooledConnection;
import org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing;
import org.apache.ignite.testframework.GridTestUtils;
import org.junit.Ignore;
Expand Down Expand Up @@ -194,6 +199,84 @@ public void testSchemaEscapeAll() throws Exception {
ignite(0).destroyCache(cfgEsc.getName());
}

/**
* Tests that creating a cache with this schema name does not create SCHEMA_INJECTED,
* and a row is readable through that schema.
*
* @throws Exception If failed.
*/
@Test
public void testSchemaNameIsNotExecuted() 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));
}

/**
* Tests that creating a cache with this table name does not create TABLE_INJECTED,
* and a row is readable through that table.
*
* @throws Exception If failed.
*/
@Test
public void testTableNameIsNotExecuted() 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));
}

/**
* @param tblName Table name.
*/
private void assertH2TableAbsent(String tblName) throws Exception {
IgniteH2Indexing idx = (IgniteH2Indexing)ignite(0).context().query().getIndexing();

try (H2PooledConnection conn = idx.connections().connection()) {
try (Statement stmt = conn.connection().createStatement();
ResultSet rs = stmt.executeQuery(
"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '" + tblName + "'")) {
assertFalse(rs.next());
}
}
}

/**
* Executes query with and without escaped schema name.
* @param cache cache for querying
Expand Down
Loading