1 /*
2  * Copyright (C) 2017 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.work.impl.model;
18 
19 import static android.arch.persistence.room.OnConflictStrategy.IGNORE;
20 
21 import android.arch.persistence.room.Dao;
22 import android.arch.persistence.room.Insert;
23 import android.arch.persistence.room.Query;
24 
25 import java.util.List;
26 
27 /**
28  * The Data Access Object for {@link Dependency}.
29  */
30 @Dao
31 public interface DependencyDao {
32     /**
33      * Attempts to insert a {@link Dependency} into the database.
34      *
35      * @param dependency The {@link Dependency}s to insert
36      */
37     @Insert(onConflict = IGNORE)
insertDependency(Dependency dependency)38     void insertDependency(Dependency dependency);
39 
40     /**
41      * Determines if a {@link WorkSpec} has completed all prerequisites.
42      *
43      * @param id The identifier for the {@link WorkSpec}
44      * @return {@code true} if the {@link WorkSpec} has no pending prerequisites.
45      */
46     @Query("SELECT COUNT(*)=0 FROM dependency WHERE work_spec_id=:id AND prerequisite_id IN "
47             + "(SELECT id FROM workspec WHERE state!="
48             + WorkTypeConverters.StateIds.SUCCEEDED + ")")
hasCompletedAllPrerequisites(String id)49     boolean hasCompletedAllPrerequisites(String id);
50 
51     /**
52      * Gets all the direct prerequisites for a particular {@link WorkSpec}.
53      *
54      * @param id The {@link WorkSpec} identifier
55      * @return A list of all prerequisites for {@code id}
56      */
57     @Query("SELECT prerequisite_id FROM dependency WHERE work_spec_id=:id")
getPrerequisites(String id)58     List<String> getPrerequisites(String id);
59 
60     /**
61      * Gets all {@link WorkSpec} id's dependent on a given id
62      *
63      * @param id A {@link WorkSpec} identifier
64      * @return A list of all identifiers that depend on the input
65      */
66     @Query("SELECT work_spec_id FROM dependency WHERE prerequisite_id=:id")
getDependentWorkIds(String id)67     List<String> getDependentWorkIds(String id);
68 
69     /**
70      * Determines if a {@link WorkSpec} has any dependents.
71      *
72      * @param id A {@link WorkSpec} identifier
73      * @return {@code true} if the {@link WorkSpec} has WorkSpecs that depend on it
74      */
75     @Query("SELECT COUNT(*)>0 FROM dependency WHERE prerequisite_id=:id")
hasDependents(String id)76     boolean hasDependents(String id);
77 }
78