1 /* 2 * Copyright (C) 2016 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 foo.bar; 18 import androidx.lifecycle.LiveData; 19 import androidx.paging.PagingSource; 20 import androidx.room.*; 21 import androidx.sqlite.db.SupportSQLiteQuery; 22 import com.google.common.util.concurrent.ListenableFuture; 23 import java.util.List; 24 25 @Dao 26 abstract class ComplexDao { 27 static class FullName { 28 public int id; 29 public String fullName; 30 } 31 32 private final ComplexDatabase mDb; 33 ComplexDao(ComplexDatabase db)34 public ComplexDao(ComplexDatabase db) { 35 mDb = db; 36 } 37 38 @Transaction transactionMethod(int i, String s, long l)39 public boolean transactionMethod(int i, String s, long l) { 40 return true; 41 } 42 43 @Query("SELECT name || lastName as fullName, uid as id FROM user where uid = :id") fullNames(int id)44 abstract public List<FullName> fullNames(int id); 45 46 @Query("SELECT * FROM user where uid = :id") getById(int id)47 abstract public User getById(int id); 48 49 @Query("SELECT * FROM user where name LIKE :name AND lastName LIKE :lastName") findByName(String name, String lastName)50 abstract public User findByName(String name, String lastName); 51 52 @Query("SELECT * FROM user where uid IN (:ids)") loadAllByIds(int... ids)53 abstract public List<User> loadAllByIds(int... ids); 54 55 @Query("SELECT ageColumn FROM user where uid = :id") getAge(int id)56 abstract int getAge(int id); 57 58 @Query("SELECT ageColumn FROM user where uid IN(:ids)") getAllAges(int... ids)59 abstract public int[] getAllAges(int... ids); 60 61 @Query("SELECT ageColumn FROM user where uid IN(:ids)") getAllAgesAsList(List<Integer> ids)62 abstract public List<Integer> getAllAgesAsList(List<Integer> ids); 63 64 @Query("SELECT * FROM user where uid = :id") getByIdLive(int id)65 abstract public LiveData<User> getByIdLive(int id); 66 67 @Query("SELECT * FROM user where uid IN (:ids)") loadUsersByIdsLive(int... ids)68 abstract public LiveData<List<User>> loadUsersByIdsLive(int... ids); 69 70 @Query("SELECT ageColumn FROM user where uid IN(:ids1) OR uid IN (:ids2) OR uid IN (:ids3)") getAllAgesAsList(List<Integer> ids1, int[] ids2, int... ids3)71 abstract public List<Integer> getAllAgesAsList(List<Integer> ids1, 72 int[] ids2, int... ids3); 73 74 @Query("SELECT * FROM Child1") getChild1List()75 abstract public List<Child1> getChild1List(); 76 77 @Query("SELECT * FROM Child2") getChild2List()78 abstract public List<Child2> getChild2List(); 79 80 @Query("SELECT * FROM Child1") getChild1ListListenableFuture()81 abstract public ListenableFuture<List<Child1>> getChild1ListListenableFuture(); 82 83 @RewriteQueriesToDropUnusedColumns 84 @Query("SELECT * FROM User") getUserNames()85 abstract public List<UserSummary> getUserNames(); 86 87 @RawQuery(observedEntities = User.class) getUserViaRawQuery(SupportSQLiteQuery rawQuery)88 abstract public User getUserViaRawQuery(SupportSQLiteQuery rawQuery); 89 90 @Query("SELECT * FROM Child1 ORDER BY id ASC") loadItems()91 abstract public PagingSource<Integer, Child1> loadItems(); 92 } 93