QueryOSQLConverter { static Query select(DocumentQuery documentQuery) { StringBuilder query = new StringBuilder(); List<Object> params = new java.util.ArrayList<>(); List<ORecordId> ids = new ArrayList<>(); query.append("SELECT FROM "); query.append(documentQuery.getDocumentCollection()); if (documentQuery.getCondition().isPresent()) { query.append(WHERE); definesCondition(documentQuery.getCondition().get(), query, params, 0, ids); } if (!documentQuery.getSorts().isEmpty()) { appendSort(documentQuery.getSorts(), query); } appendPagination(documentQuery, query); return new Query(query.toString(), params, ids); } private  QueryOSQLConverter();   }
@Test public void shouldRunEqualsQueryAnd() { DocumentQuery query = select().from("collection") .where("name").eq("value") .and("age").lte(10) .build(); QueryOSQLConverter.Query convert = QueryOSQLConverter.select(query); String sql = convert.getQuery(); List<Object> values = convert.getParams(); assertEquals("value", values.get(0)); assertEquals(10, values.get(1)); assertEquals("SELECT FROM collection WHERE name = ? AND age <= ?", sql); }
@Test public void shouldRunEqualsQueryOr() { DocumentQuery query = select().from("collection") .where("name").eq("value") .or("age").lte(10) .build(); QueryOSQLConverter.Query convert = QueryOSQLConverter.select(query); String sql = convert.getQuery(); List<Object> values = convert.getParams(); assertEquals("value", values.get(0)); assertEquals(10, values.get(1)); assertEquals("SELECT FROM collection WHERE name = ? OR age <= ?", sql); }
@Test public void shouldRunEqualsQueryNot() { DocumentQuery query = select().from("collection") .where("name").not().eq("value").build(); QueryOSQLConverter.Query convert = QueryOSQLConverter.select(query); String sql = convert.getQuery(); List<Object> values = convert.getParams(); assertEquals("value", values.get(0)); assertEquals("SELECT FROM collection WHERE NOT (name = ?)", sql); }
@Test public void shouldNegate() { DocumentQuery query = select().from("collection") .where("city").not().eq("Assis") .and("name").eq("Otavio") .or("name").not().eq("Lucas").build(); QueryOSQLConverter.Query convert = QueryOSQLConverter.select(query); String sql = convert.getQuery(); List<Object> values = convert.getParams(); assertEquals(3, values.size()); assertEquals("Assis", values.get(0)); assertEquals("Otavio", values.get(1)); assertEquals("Lucas", values.get(2)); assertEquals("SELECT FROM collection WHERE NOT (city = ?) AND name = ? OR NOT (name = ?)", sql); }
@Test public void shouldPaginateWithStart() { DocumentQuery query = select().from("collection") .skip(10) .build(); QueryOSQLConverter.Query convert = QueryOSQLConverter.select(query); assertEquals("SELECT FROM collection SKIP 10", convert.getQuery()); }
@Test public void shouldPaginateWithLimit() { DocumentQuery query = select().from("collection") .limit(100) .build(); QueryOSQLConverter.Query convert = QueryOSQLConverter.select(query); assertEquals("SELECT FROM collection LIMIT 100", convert.getQuery()); }
@Test public void shouldPaginateWithStartAndLimit() { DocumentQuery query = select().from("collection") .skip(10) .limit(100) .build(); QueryOSQLConverter.Query convert = QueryOSQLConverter.select(query); assertEquals("SELECT FROM collection SKIP 10 LIMIT 100", convert.getQuery()); }
@Test public void shouldSortAsc() { DocumentQuery query = select().from("collection") .orderBy("name").asc() .build(); QueryOSQLConverter.Query convert = QueryOSQLConverter.select(query); assertEquals("SELECT FROM collection ORDER BY name ASC", convert.getQuery()); }
@Test public void shouldSortDesc() { DocumentQuery query = select().from("collection") .orderBy("name").desc() .build(); QueryOSQLConverter.Query convert = QueryOSQLConverter.select(query); assertEquals("SELECT FROM collection ORDER BY name DESC", convert.getQuery()); }
@Test public void shouldMultipleSort() { DocumentQuery query = select().from("collection") .orderBy("name").asc() .orderBy("age").desc() .build(); QueryOSQLConverter.Query convert = QueryOSQLConverter.select(query); assertEquals("SELECT FROM collection ORDER BY name ASC, age DESC", convert.getQuery()); }
@Test public void shouldRunEqualsQuery() { DocumentQuery query = select().from("collection") .where("name").eq("value").build(); QueryOSQLConverter.Query convert = QueryOSQLConverter.select(query); String sql = convert.getQuery(); List<Object> values = convert.getParams(); assertEquals("value", values.get(0)); assertEquals("SELECT FROM collection WHERE name = ?", sql); }