1 /*
<lambda>null2  * Copyright 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 package androidx.work.impl.model
17 
18 import androidx.room.Dao
19 import androidx.room.Insert
20 import androidx.room.OnConflictStrategy
21 import androidx.room.Query
22 
23 @JvmDefaultWithCompatibility
24 /** The Data Access Object for [WorkTag]s. */
25 @Dao
26 interface WorkTagDao {
27     /**
28      * Inserts a [WorkTag] into the table.
29      *
30      * @param workTag The [WorkTag] to insert
31      */
32     @Insert(onConflict = OnConflictStrategy.IGNORE) fun insert(workTag: WorkTag)
33 
34     /**
35      * Deletes [WorkSpec]s from the database.
36      *
37      * @param id The WorkSpec id to delete.
38      */
39     @Query("DELETE FROM worktag WHERE work_spec_id=:id") fun deleteByWorkSpecId(id: String)
40 
41     /**
42      * Retrieves all [WorkSpec] ids with the given tag.
43      *
44      * @param tag The matching tag
45      * @return All [WorkSpec] ids with the given tag
46      */
47     @Query("SELECT work_spec_id FROM worktag WHERE tag=:tag")
48     fun getWorkSpecIdsWithTag(tag: String): List<String>
49 
50     /**
51      * Retrieves all tags for a given [WorkSpec] id.
52      *
53      * @param id The id of the [WorkSpec]
54      * @return A list of tags for that [WorkSpec]
55      */
56     @Query("SELECT DISTINCT tag FROM worktag WHERE work_spec_id=:id")
57     fun getTagsForWorkSpecId(id: String): List<String>
58 
59     fun insertTags(id: String, tags: Set<String>) {
60         tags.forEach { tag -> insert(WorkTag(tag, id)) }
61     }
62 }
63