1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.paging.integration.testapp.room;
18 
19 import androidx.lifecycle.LiveData;
20 import androidx.paging.DataSource;
21 import androidx.paging.PagingSource;
22 import androidx.room.Dao;
23 import androidx.room.Insert;
24 import androidx.room.Query;
25 
26 import java.util.List;
27 
28 /**
29  * Simple Customer DAO for Room Customer list sample.
30  */
31 @Dao
32 public interface CustomerDao {
33 
34     /**
35      * Insert a customer
36      * @param customer Customer.
37      */
38     @Insert
insert(Customer customer)39     void insert(Customer customer);
40 
41     /**
42      * Insert multiple customers.
43      * @param customers Customers.
44      */
45     @Insert
insertAll(Customer[] customers)46     void insertAll(Customer[] customers);
47 
48     /**
49      * Delete all customers
50      */
51     @Query("DELETE FROM customer")
removeAll()52     void removeAll();
53 
54     /**
55      * @return DataSource.Factory of customers, ordered by id. Use
56      * {@link androidx.paging.LivePagedListBuilder} to get a LiveData of PagedLists.
57      */
58     @Query("SELECT * FROM customer ORDER BY mId ASC")
loadPagedAgeOrder()59     DataSource.Factory<Integer, Customer> loadPagedAgeOrder();
60 
61     /**
62      * @return PagingSource of customers, ordered by id.
63      */
64     @Query("SELECT * FROM customer ORDER BY mId ASC")
loadPagedAgeOrderPagingSource()65     PagingSource<Integer, Customer> loadPagedAgeOrderPagingSource();
66 
67     /**
68      * @return number of customers
69      */
70     @Query("SELECT COUNT(*) FROM customer")
countCustomers()71     int countCustomers();
72 
73     /**
74      * @return All customers
75      */
76     @Query("SELECT * FROM customer")
all()77     LiveData<List<Customer>> all();
78 
79     // Keyed
80 
81     @Query("SELECT * from customer ORDER BY mLastName DESC LIMIT :limit")
customerNameInitial(int limit)82     List<Customer> customerNameInitial(int limit);
83 
84     @Query("SELECT * from customer WHERE mLastName < :key ORDER BY mLastName DESC LIMIT :limit")
customerNameLoadAfter(String key, int limit)85     List<Customer> customerNameLoadAfter(String key, int limit);
86 
87     @Query("SELECT COUNT(*) from customer WHERE mLastName < :key ORDER BY mLastName DESC")
customerNameCountAfter(String key)88     int customerNameCountAfter(String key);
89 
90     @Query("SELECT * from customer WHERE mLastName > :key ORDER BY mLastName ASC LIMIT :limit")
customerNameLoadBefore(String key, int limit)91     List<Customer> customerNameLoadBefore(String key, int limit);
92 
93     @Query("SELECT COUNT(*) from customer WHERE mLastName > :key ORDER BY mLastName ASC")
customerNameCountBefore(String key)94     int customerNameCountBefore(String key);
95 }
96