@Override public Collection<Resource> members(RequestContext ctx) { Pagination pagination = ctx.pagination(); Stream<String> members = this.db().getCollectionNames().stream().skip(pagination.offset()); if (pagination.limit() > 0) { members = members.limit(pagination.limit()); } return members .filter(name -> !name.equals("system.indexes")) .map(name -> new MongoCollectionResource(this, db().getCollection(name))) .collect(Collectors.toList()); }
public QueryResults query(PreparedStatement ps, Pagination pagination) throws SQLException { try (PreparedStatement s = ps) { if (log.isTraceEnabled()) { log.trace("sql: " + rawSQL(ps)); } if (pagination != null) { s.setMaxRows(pagination.limit()); } try (ResultSet rs = s.executeQuery()) { ResultSetMetaData meta = rs.getMetaData(); int count = meta.getColumnCount(); ArrayList<String> columnNames = new ArrayList<>(count); for (int i = 0; i < count; i++) { columnNames.add(meta.getColumnName(i + 1)); } LinkedList<Row> rows = new LinkedList<>(); while (rs.next()) { ArrayList<Object> row = new ArrayList<>(count); for (int i = 0; i < count; i++) { row.add(rs.getObject(i + 1)); } rows.add(new Row(columnNames, row)); } return new QueryResults(columnNames, rows); } catch (Exception e) { log.error("Exception while executing a query: " + ps, e); } } return new QueryResults(); }
@Override public Map<String, ?> properties(RequestContext ctx) throws Exception { // perform select and store it for readMembers if (results != null) { return null; } results = queryTable(id, null, ctx); List<Resource> links = new LinkedList<>(); MapResource link = new MapResource(); link.put("rel", "schema"); link.put(LiveOak.HREF, uri() + SCHEMA_ENDPOINT); links.add(link); PagingLinksBuilder linksBuilder = new PagingLinksBuilder(ctx) .uri(uri()) .count(results.count()); int totalCount = -1; if (parent.configuration().configuration().includeTotalCount()) { totalCount = queryTableCount(id, ctx); linksBuilder.totalCount(totalCount); } links.addAll(linksBuilder.build()); // keep predictable ordering by using LinkedHashMap Map<String, Object> result = new LinkedHashMap<>(); result.put("links", links); if (totalCount != -1 || results.count() < ctx.pagination().limit()) { result.put("count", totalCount != -1 ? totalCount : results.count()); } result.put("type", "collection"); return result; }
public PreparedStatement prepareSelectAllFromTable(Connection con, Table table, Sorting sorting, Pagination pagination) throws SQLException { StringBuilder sb = new StringBuilder(selectAllFromTable(table)); if (sorting != null) { StringBuilder orderBy = new StringBuilder(); int i = 0; for (Sorting.Spec spec: sorting) { if (i > 0) { orderBy.append(","); } orderBy.append(spec.name()); if (!spec.ascending()) { orderBy.append(" DESC"); } i++; } if (orderBy.length() > 0) { sb.append(" ORDER BY ").append(orderBy); } } sb.append(" LIMIT " + pagination.limit()); if (pagination.offset() > 0) { sb.append(" OFFSET " + pagination.offset()); } return con.prepareStatement(sb.toString()); }
@Override public void encode() throws Exception { //we should only read the members if they are going to be returned in the response //otherwise it could be an expensive operation when all we are requesting is metadata (ie count) //or if the count > 0 (eg no member should be returned). if (requestContext().returnFields().included(LiveOak.MEMBERS) && requestContext().pagination().limit() > 0) { resource().readMembers(requestContext(), new MyResourceSink()); } else { encodeNext(); } }
@Override public Collection<Resource> members(RequestContext ctx) throws Exception { LinkedList<Resource> members = new LinkedList<>(); // if children requested query children info DBCollection col = getUserspace().getFilesCollection(); DBCursor result = col.find().skip(ctx.pagination().offset()).limit(ctx.pagination().limit()); while (result.hasNext()) { DBObject child = result.next(); members.add(wrapDBObject(path(), new GridFSDBObject(child))); } return members; }