ParsedQueryFactory { public static ParsedQuery parseQuery(String db, Query toParse, Session session) throws FieldNotIndexedException { WhereClause where = new WhereClause(toParse.getWhere()); List<String> fieldsToQueryOn = where.getFields(); IndexRepository indexRepo = new IndexRepositoryImpl(session); List<Index> indices = indexRepo.readAllCached(new Identifier(db, toParse.getTable())); Index indexToUse = null; for (Index index : indices) { if (Utils.equalLists(index.getFieldsValues(), fieldsToQueryOn)) { indexToUse = index; break; } } if (indexToUse == null) { for (Index index : indices) { ArrayList<String> fieldsToQueryOnCopy = new ArrayList<>(fieldsToQueryOn); ArrayList<String> indexFields = new ArrayList<>(index.getFieldsValues()); fieldsToQueryOnCopy.removeAll(indexFields); if (fieldsToQueryOnCopy.isEmpty() && fieldsToQueryOn.contains(indexFields.get(0))) { indexToUse = index; break; } } } if (indexToUse == null) { throw new FieldNotIndexedException(fieldsToQueryOn); } ParsedQuery toReturn = new ParsedQuery(toParse, where, indexToUse); return toReturn; }  static ParsedQuery getParsedQuery(String db, Query toParse, Session session); static ParsedQuery parseQuery(String db, Query toParse, Session session);  }
@Test public void testParseQueryBasic() throws FieldNotIndexedException { System.out.println("testParseQueryBasic"); String db = Fixtures.DB; Query toParse = Fixtures.createTestQuery(); ParsedQuery expResult = new ParsedQuery(toParse, new WhereClause(toParse.getWhere()), Fixtures.createTestIndexOneField()); ParsedQuery result = ParsedQueryFactory.parseQuery(db, toParse, f.getSession()); assertEquals(expResult, result); }
@Test public void testParseQueryTwoFields() throws FieldNotIndexedException { System.out.println("testParseQueryTwoFields"); String db = Fixtures.DB; Query toParse = Fixtures.createTestQuery2(); ParsedQuery expResult = new ParsedQuery(toParse, new WhereClause(toParse.getWhere()), Fixtures.createTestIndexTwoField()); ParsedQuery result = ParsedQueryFactory.parseQuery(db, toParse, f.getSession()); assertEquals(expResult, result); }
@Test public void testParseQueryTwoFieldsImperfectMatch() throws FieldNotIndexedException { System.out.println("testParseQueryTwoFieldsImperfectMatch"); String db = Fixtures.DB; Query toParse = Fixtures.createTestQuery2(); toParse.setWhere("myindexedfield1 = 'thisismyfield'"); ParsedQuery expResult = new ParsedQuery(toParse, new WhereClause(toParse.getWhere()), Fixtures.createTestIndexTwoField()); ParsedQuery result = ParsedQueryFactory.parseQuery(db, toParse, f.getSession()); assertEquals(expResult, result); }
@Test public void testParseQueryException() { System.out.println("testParseQueryException"); String db = Fixtures.DB; Query toParse = Fixtures.createTestQuery(); toParse.setWhere("nonIndexedField = 'boo'"); boolean expectedExceptionThrown = false; try { ParsedQuery result = ParsedQueryFactory.parseQuery(db, toParse, f.getSession()); } catch (FieldNotIndexedException e) { expectedExceptionThrown = true; assertTrue(e.getLocalizedMessage().contains("nonIndexedField")); } assertTrue("Expected exception not thrown", expectedExceptionThrown); }
@Test public void testGetParsedQueryException() { System.out.println("testGetParsedQueryException"); String db = Fixtures.DB; Query toParse = Fixtures.createTestQuery(); toParse.setWhere("nonIndexedField = 'boo'"); boolean expectedExceptionThrown = false; try { ParsedQuery result = ParsedQueryFactory.parseQuery(db, toParse, f.getSession()); } catch (FieldNotIndexedException e) { expectedExceptionThrown = true; assertTrue(e.getLocalizedMessage().contains("nonIndexedField")); } assertTrue("Expected exception not thrown", expectedExceptionThrown); }