PostgresSchema { public static void search(final QueryStatement statement, final CollectionName collection, final IObject criteria) throws QueryBuildException { try { Writer body = statement.getBodyWriter(); body.write("SELECT "); body.write(DOCUMENT_COLUMN); body.write(" FROM "); body.write(collection.toString()); if (criteria == null) { SearchClauses.writeDefaultPaging(statement); return; } SearchClauses.writeSearchWhere(statement, criteria); SearchClauses.writeSearchOrder(statement, criteria); SearchClauses.writeSearchPaging(statement, criteria); } catch (Exception e) { throw new QueryBuildException("Failed to build search query", e); } } private  PostgresSchema(); static void create(final QueryStatement statement, final CollectionName collection, final IObject options); static FieldPath getIdFieldPath(final CollectionName collection); static void insert(final QueryStatement statement, final CollectionName collection); static void update(final QueryStatement statement, final CollectionName collection); static void getById(final QueryStatement statement, final CollectionName collection); static void search(final QueryStatement statement, final CollectionName collection, final IObject criteria); static void delete(final QueryStatement statement, final CollectionName collection); static void count(final QueryStatement statement, final CollectionName collection, final IObject criteria); static final String DOCUMENT_COLUMN; static final String FULLTEXT_COLUMN; static final String ID_FIELD_PATTERN; static final String FTS_DICTIONARY; static final int DEFAULT_PAGE_SIZE; }
@Test public void testSearch() throws InvalidArgumentException, QueryBuildException { IObject criteria = new DSObject("{ \"filter\": { \"a\": { \"$eq\": \"b\" } } }"); PostgresSchema.search(statement, collection, criteria); assertEquals("SELECT document FROM test_collection " + "WHERE ((((document#>'{a}')=to_json(?)::jsonb))) " + "LIMIT(?)OFFSET(?)", body.toString()); verify(statement, times(2)).pushParameterSetter(any()); }
@Test public void testSearchWithPaging() throws InvalidArgumentException, QueryBuildException { IObject criteria = new DSObject("{ \"filter\": { \"a\": { \"$eq\": \"b\" } }," + " \"page\": { \"size\": 10, \"number\": 3 } }"); PostgresSchema.search(statement, collection, criteria); assertEquals("SELECT document FROM test_collection " + "WHERE ((((document#>'{a}')=to_json(?)::jsonb))) " + "LIMIT(?)OFFSET(?)", body.toString()); verify(statement, times(2)).pushParameterSetter(any()); }
@Test public void testSearchWithSorting() throws InvalidArgumentException, QueryBuildException { IObject criteria = new DSObject("{ \"filter\": { \"a\": { \"$eq\": \"b\" } }," + " \"sort\": [ { \"a\": \"desc\" } ] }"); PostgresSchema.search(statement, collection, criteria); assertEquals("SELECT document FROM test_collection " + "WHERE ((((document#>'{a}')=to_json(?)::jsonb))) " + "ORDER BY(document#>'{a}')DESC " + "LIMIT(?)OFFSET(?)", body.toString()); verify(statement, times(2)).pushParameterSetter(any()); }
@Test public void testSearchWithPagingAndSorting() throws InvalidArgumentException, QueryBuildException { IObject criteria = new DSObject("{ \"filter\": { \"a\": { \"$eq\": \"b\" } }," + " \"page\": { \"size\": 10, \"number\": 3 }, " + " \"sort\": [ { \"a\": \"desc\" } ] }"); PostgresSchema.search(statement, collection, criteria); assertEquals("SELECT document FROM test_collection " + "WHERE ((((document#>'{a}')=to_json(?)::jsonb))) " + "ORDER BY(document#>'{a}')DESC " + "LIMIT(?)OFFSET(?)", body.toString()); verify(statement, times(2)).pushParameterSetter(any()); }
@Test public void testSearchWithEmptyFilter() throws InvalidArgumentException, QueryBuildException { IObject criteria = new DSObject("{ \"filter\": { } }"); PostgresSchema.search(statement, collection, criteria); assertEquals("SELECT document FROM test_collection WHERE (TRUE) LIMIT(?)OFFSET(?)", body.toString()); verify(statement, times(1)).pushParameterSetter(any()); }
@Test public void testSearchWithEmptyCriteria() throws InvalidArgumentException, QueryBuildException { IObject criteria = new DSObject("{ }"); PostgresSchema.search(statement, collection, criteria); assertEquals("SELECT document FROM test_collection LIMIT(?)OFFSET(?)", body.toString()); verify(statement, times(1)).pushParameterSetter(any()); }
@Test public void testSearchWithNullCriteria() throws InvalidArgumentException, QueryBuildException { PostgresSchema.search(statement, collection, null); assertEquals("SELECT document FROM test_collection LIMIT(?)OFFSET(?)", body.toString()); verify(statement, times(1)).pushParameterSetter(any()); }
@Test public void testSearchWithNot() throws InvalidArgumentException, QueryBuildException { IObject criteria = new DSObject("{ \"filter\": { \"$not\": { \"a\": { \"$eq\": \"b\" }, \"c\": { \"$eq\": \"d\" } } } }"); PostgresSchema.search(statement, collection, criteria); assertEquals("SELECT document FROM test_collection " + "WHERE ((NOT((((document#>'{a}')=to_json(?)::jsonb))AND(((document#>'{c}')=to_json(?)::jsonb))))) " + "LIMIT(?)OFFSET(?)", body.toString()); verify(statement, times(3)).pushParameterSetter(any()); }