Search in sources :

Example 1 with Result

use of com.example.getstarted.objects.Result in project getting-started-java by GoogleCloudPlatform.

the class DatastoreDao method listBooks.

// [END entitiesToBooks]
// [START listbooks]
@Override
public Result<Book> listBooks(String startCursorString) {
    // Only show 10 at a time
    FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10);
    if (startCursorString != null && !startCursorString.equals("")) {
        // Where we left off
        fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString));
    }
    Query query = // We only care about Books
    new Query(BOOK_KIND).addSort(Book.TITLE, // Use default Index "title"
    SortDirection.ASCENDING);
    PreparedQuery preparedQuery = datastore.prepare(query);
    QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);
    // Retrieve and convert Entities
    List<Book> resultBooks = entitiesToBooks(results);
    // Where to start next time
    Cursor cursor = results.getCursor();
    if (cursor != null && resultBooks.size() == 10) {
        // Are we paging? Save Cursor
        // Cursors are WebSafe
        String cursorString = cursor.toWebSafeString();
        return new Result<>(resultBooks, cursorString);
    } else {
        return new Result<>(resultBooks);
    }
}
Also used : FetchOptions(com.google.appengine.api.datastore.FetchOptions) Entity(com.google.appengine.api.datastore.Entity) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Query(com.google.appengine.api.datastore.Query) Book(com.example.getstarted.objects.Book) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Cursor(com.google.appengine.api.datastore.Cursor) Result(com.example.getstarted.objects.Result)

Example 2 with Result

use of com.example.getstarted.objects.Result in project getting-started-java by GoogleCloudPlatform.

the class FirestoreDao method listBooksByUser.

// [END bookshelf_firestore_list_books]
// [START bookshelf_firestore_list_by_user]
@Override
public Result<Book> listBooksByUser(String userId, String startTitle) {
    Query booksQuery = booksCollection.orderBy("title").whereEqualTo(Book.CREATED_BY_ID, userId).limit(10);
    if (startTitle != null) {
        booksQuery = booksQuery.startAfter(startTitle);
    }
    try {
        QuerySnapshot snapshot = booksQuery.get().get();
        List<Book> results = documentsToBooks(snapshot.getDocuments());
        String newCursor = null;
        if (results.size() > 0) {
            newCursor = results.get(results.size() - 1).getTitle();
        }
        return new Result<>(results, newCursor);
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
    return new Result<>(Lists.newArrayList(), null);
}
Also used : Query(com.google.cloud.firestore.Query) Book(com.example.getstarted.objects.Book) ExecutionException(java.util.concurrent.ExecutionException) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot) Result(com.example.getstarted.objects.Result)

Example 3 with Result

use of com.example.getstarted.objects.Result in project getting-started-java by GoogleCloudPlatform.

the class DatastoreDao method listBooksByUser.

// [END listbooks]
// [START listbyuser]
@Override
public Result<Book> listBooksByUser(String userId, String startCursorString) {
    // Only show 10 at a time
    FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10);
    if (startCursorString != null && !startCursorString.equals("")) {
        // Where we left off
        fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString));
    }
    Query query = // We only care about Books
    new Query(BOOK_KIND).setFilter(new Query.FilterPredicate(Book.CREATED_BY_ID, Query.FilterOperator.EQUAL, userId)).addSort(Book.TITLE, SortDirection.ASCENDING);
    PreparedQuery preparedQuery = datastore.prepare(query);
    QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);
    // Retrieve and convert Entities
    List<Book> resultBooks = entitiesToBooks(results);
    // Where to start next time
    Cursor cursor = results.getCursor();
    if (cursor != null && resultBooks.size() == 10) {
        // Are we paging? Save Cursor
        // Cursors are WebSafe
        String cursorString = cursor.toWebSafeString();
        return new Result<>(resultBooks, cursorString);
    } else {
        return new Result<>(resultBooks);
    }
}
Also used : FetchOptions(com.google.appengine.api.datastore.FetchOptions) Entity(com.google.appengine.api.datastore.Entity) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Query(com.google.appengine.api.datastore.Query) Book(com.example.getstarted.objects.Book) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Cursor(com.google.appengine.api.datastore.Cursor) Result(com.example.getstarted.objects.Result)

Example 4 with Result

use of com.example.getstarted.objects.Result in project getting-started-java by GoogleCloudPlatform.

the class CloudSqlDao method listBooksByUser.

// [END listbooks]
// [START listbyuser]
@Override
public Result<Book> listBooksByUser(String userId, String startCursor) throws SQLException {
    int offset = 0;
    if (startCursor != null && !startCursor.equals("")) {
        offset = Integer.parseInt(startCursor);
    }
    final String listBooksString = "SELECT SQL_CALC_FOUND_ROWS author, createdBy, createdById, " + "description, id, publishedDate, title, imageUrl FROM books WHERE createdById = ? " + "ORDER BY title ASC LIMIT 10 OFFSET ?";
    try (Connection conn = DriverManager.getConnection(sqlUrl);
        PreparedStatement listBooksStmt = conn.prepareStatement(listBooksString)) {
        listBooksStmt.setString(1, userId);
        listBooksStmt.setInt(2, offset);
        List<Book> resultBooks = new ArrayList<>();
        try (ResultSet rs = listBooksStmt.executeQuery()) {
            while (rs.next()) {
                Book book = new Book.Builder().author(rs.getString(Book.AUTHOR)).createdBy(rs.getString(Book.CREATED_BY)).createdById(rs.getString(Book.CREATED_BY_ID)).description(rs.getString(Book.DESCRIPTION)).id(rs.getLong(Book.ID)).publishedDate(rs.getString(Book.PUBLISHED_DATE)).title(rs.getString(Book.TITLE)).imageUrl(rs.getString(Book.IMAGE_URL)).build();
                resultBooks.add(book);
            }
        }
        try (ResultSet rs = conn.createStatement().executeQuery("SELECT FOUND_ROWS()")) {
            int totalNumRows = 0;
            if (rs.next()) {
                totalNumRows = rs.getInt(1);
            }
            if (totalNumRows > offset + 10) {
                return new Result<>(resultBooks, Integer.toString(offset + 10));
            } else {
                return new Result<>(resultBooks);
            }
        }
    }
}
Also used : Book(com.example.getstarted.objects.Book) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Result(com.example.getstarted.objects.Result)

Example 5 with Result

use of com.example.getstarted.objects.Result in project getting-started-java by GoogleCloudPlatform.

the class CloudSqlDao method listBooks.

// [END delete]
// [START listbooks]
@Override
public Result<Book> listBooks(String cursor) throws SQLException {
    int offset = 0;
    if (cursor != null && !cursor.equals("")) {
        offset = Integer.parseInt(cursor);
    }
    final String listBooksString = "SELECT SQL_CALC_FOUND_ROWS author, createdBy, createdById, " + "description, id, publishedDate, title, imageUrl FROM books5 ORDER BY title ASC " + "LIMIT 10 OFFSET ?";
    try (Connection conn = DriverManager.getConnection(sqlUrl);
        PreparedStatement listBooksStmt = conn.prepareStatement(listBooksString)) {
        listBooksStmt.setInt(1, offset);
        List<Book> resultBooks = new ArrayList<>();
        try (ResultSet rs = listBooksStmt.executeQuery()) {
            while (rs.next()) {
                Book book = new Book.Builder().author(rs.getString(Book.AUTHOR)).createdBy(rs.getString(Book.CREATED_BY)).createdById(rs.getString(Book.CREATED_BY_ID)).description(rs.getString(Book.DESCRIPTION)).id(rs.getLong(Book.ID)).publishedDate(rs.getString(Book.PUBLISHED_DATE)).title(rs.getString(Book.TITLE)).imageUrl(rs.getString(Book.IMAGE_URL)).build();
                resultBooks.add(book);
            }
        }
        try (ResultSet rs = conn.createStatement().executeQuery("SELECT FOUND_ROWS()")) {
            int totalNumRows = 0;
            if (rs.next()) {
                totalNumRows = rs.getInt(1);
            }
            if (totalNumRows > offset + 10) {
                return new Result<>(resultBooks, Integer.toString(offset + 10));
            } else {
                return new Result<>(resultBooks);
            }
        }
    }
}
Also used : Book(com.example.getstarted.objects.Book) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Result(com.example.getstarted.objects.Result)

Aggregations

Book (com.example.getstarted.objects.Book)6 Result (com.example.getstarted.objects.Result)6 Cursor (com.google.appengine.api.datastore.Cursor)2 Entity (com.google.appengine.api.datastore.Entity)2 FetchOptions (com.google.appengine.api.datastore.FetchOptions)2 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)2 Query (com.google.appengine.api.datastore.Query)2 Query (com.google.cloud.firestore.Query)2 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 ExecutionException (java.util.concurrent.ExecutionException)2