Skip to content
Merged
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 @@ -381,7 +381,7 @@ private String computeWhereClause() {
case match:
pred = computeFieldReference(SmartStore.SOUP_ENTRY_ID) + " IN ("
+ SELECT + SmartStore.ROWID_COL + " " + FROM + computeSoupFtsReference() + " " + WHERE
+ computeSoupFtsReference() + " MATCH '" + qualifyMatchKey(field, matchKey) + "'"
+ computeSoupFtsReference() + " MATCH '" + qualifyMatchKey(field, matchKey).replace("'", "''") + "'"
// statement arg binding doesn't seem to work so inlining matchKey
+ ") ";
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private String getColumnNameForPathForSmartSql(SQLiteDatabase db, String soupNam

if (!indexed) {
// Thanks to the json1 extension we can query the data even if it is not indexed
columnName = "json_extract(" + SmartStore.SOUP_COL + ", '$." + path + "')";
columnName = "json_extract(" + SmartStore.SOUP_COL + ", '$." + path.replace("'", "''") + "')";
Comment thread
wmathurin marked this conversation as resolved.
} else {
try {
columnName = DBHelper.getInstance(db).getColumnNameForPath(db, soupName, path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ protected void registerSoupUsingTableName(String soupName, IndexSpec[] indexSpec
// Column name or expression the db index is on
String columnName = soupTableName + "_" + i;
if (TypeGroup.value_indexed_with_json_extract.isMember(indexSpec.type)) {
columnName = "json_extract(" + SOUP_COL + ", '$." + indexSpec.path + "')";
columnName = "json_extract(" + SOUP_COL + ", '$." + indexSpec.path.replace("'", "''") + "')";
}

// for create table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ public void testSmartQueryIdsSmartSql() {
Assert.assertEquals("Wrong ids smart sql", "SELECT id FROM (select {employees:salary} from {employees} where {employees:lastName} = 'Haas')", querySpec.idsSmartSql);
}

@Test
public void testMatchQuerySmartSqlWithSingleQuoteInMatchKey() {
// Single quotes in matchKey must be doubled so they don't break the surrounding MATCH '...' literal.
QuerySpec querySpec = QuerySpec.buildMatchQuerySpec("employees", "lastName", "O'Brien", "firstName", QuerySpec.Order.ascending, 1);
Assert.assertEquals("Wrong smart sql for match query spec with single quote in matchKey",
"SELECT {employees:_soup} FROM {employees} WHERE {employees:_soupEntryId} IN (SELECT rowid FROM {employees}_fts WHERE {employees}_fts MATCH '{employees:lastName}:O''Brien') ORDER BY {employees:firstName} ASC ",
querySpec.smartSql);
}

@Test
public void testQualifyMatchKey() {
Assert.assertEquals("Wrong qualified match query", "abc", QuerySpec.qualifyMatchKey(null, "abc"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ public void testConvertSmartSqlForNonIndexedColumns() {
store.convertSmartSql("select {employees:education}, {employees:address.zipcode} from {employees} where {employees:address.city} = 'San Francisco'"));
}

@Test
public void testConvertSmartSqlForNonIndexedColumnWithSingleQuoteInPath() {
// Single quotes in non-indexed paths must be doubled so they don't break the surrounding json_extract(soup, '$.path') literal.
// The path with ' is in the WHERE clause so that the FROM {employees} token is resolved before any single-quote appears in beforeStr.
Assert.assertEquals("select TABLE_1_3 from TABLE_1 where json_extract(soup, '$.user''s.address') = 'foo'",
store.convertSmartSql("select {employees:employeeId} from {employees} where {employees:user's.address} = 'foo'"));
}

@Test
public void testConvertSmartSqlWithQuotedCurlyBraces() {
Assert.assertEquals("select json_extract(soup, '$.education') from TABLE_1 where json_extract(soup, '$.education') like 'Account(where: {Name: {eq: \"Jason\"}})'",
Expand Down
Loading