1 /*
2  * Copyright (C) 2023 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 com.android.server.healthconnect.storage.datatypehelpers;
18 
19 import static com.android.server.healthconnect.storage.datatypehelpers.RecordHelper.PRIMARY_COLUMN_NAME;
20 import static com.android.server.healthconnect.storage.request.UpsertTableRequest.TYPE_STRING;
21 import static com.android.server.healthconnect.storage.utils.StorageUtils.PRIMARY;
22 import static com.android.server.healthconnect.storage.utils.StorageUtils.TEXT_NOT_NULL_UNIQUE;
23 
24 import android.annotation.NonNull;
25 import android.content.ContentValues;
26 import android.util.Pair;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 import com.android.server.healthconnect.storage.TransactionManager;
30 import com.android.server.healthconnect.storage.request.CreateTableRequest;
31 import com.android.server.healthconnect.storage.request.DeleteTableRequest;
32 import com.android.server.healthconnect.storage.request.UpsertTableRequest;
33 
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.List;
37 
38 /**
39  * A class to help with the DB transaction for storing migration entity identifiers, user for
40  * deduplication logic during the migration process.
41  *
42  * @hide
43  */
44 public final class MigrationEntityHelper extends DatabaseHelper {
45 
46     @VisibleForTesting public static final String TABLE_NAME = "migration_entity_table";
47     private static final String COLUMN_ENTITY_ID = "entity_id";
48 
49     /** Clears all data related to this helper. */
clearData(@onNull TransactionManager transactionManager)50     public void clearData(@NonNull TransactionManager transactionManager) {
51         transactionManager.delete(new DeleteTableRequest(TABLE_NAME));
52     }
53 
54     /** Returns a request to create a table for this helper. */
55     @NonNull
getCreateTableRequest()56     public static CreateTableRequest getCreateTableRequest() {
57         return new CreateTableRequest(TABLE_NAME, getColumnInfo());
58     }
59 
60     @Override
getMainTableName()61     protected String getMainTableName() {
62         return TABLE_NAME;
63     }
64 
getColumnInfo()65     private static List<Pair<String, String>> getColumnInfo() {
66         ArrayList<Pair<String, String>> columnInfo = new ArrayList<>();
67         columnInfo.add(new Pair<>(PRIMARY_COLUMN_NAME, PRIMARY));
68         columnInfo.add(new Pair<>(COLUMN_ENTITY_ID, TEXT_NOT_NULL_UNIQUE));
69 
70         return columnInfo;
71     }
72 
73     /** Returns a request to insert the provided {@code entityId}. */
74     @NonNull
getInsertRequest(@onNull String entityId)75     public static UpsertTableRequest getInsertRequest(@NonNull String entityId) {
76         final ContentValues values = new ContentValues();
77         values.put(COLUMN_ENTITY_ID, entityId);
78         return new UpsertTableRequest(
79                 TABLE_NAME,
80                 values,
81                 Collections.singletonList(new Pair<>(COLUMN_ENTITY_ID, TYPE_STRING)));
82     }
83 }
84