1 /*
2  * Copyright (C) 2022 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.ondevicepersonalization.services.data.events;
18 
19 import android.provider.BaseColumns;
20 
21 import java.util.List;
22 
23 /** Contract for the events table. Defines the table. */
24 public class EventsContract {
EventsContract()25     private EventsContract() {
26     }
27 
28     /**
29      * Table containing events. Each row in the table
30      * represents a single event.
31      */
32     public static class EventsEntry implements BaseColumns {
33         public static final String TABLE_NAME = "events";
34 
35         /** The id of the event. */
36         public static final String EVENT_ID = "eventId";
37 
38         /** The id of the query. */
39         public static final String QUERY_ID = "queryId";
40 
41         /** Index of the request log entry for this event */
42         public static final String ROW_INDEX = "rowIndex";
43 
44         /** Name of the service that owns this event */
45         public static final String SERVICE_NAME = "serviceName";
46 
47         /** Integer enum defining the type of event */
48         public static final String TYPE = "type";
49 
50         /** Time of the event in milliseconds. */
51         public static final String TIME_MILLIS = "timeMillis";
52 
53         /** Blob representing the event. */
54         public static final String EVENT_DATA = "eventData";
55 
56         public static final String CREATE_TABLE_STATEMENT =
57                 "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ("
58                     + EVENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
59                     + QUERY_ID + " INTEGER NOT NULL,"
60                     + ROW_INDEX + " INTEGER NOT NULL,"
61                     + SERVICE_NAME + " TEXT NOT NULL,"
62                     + TYPE + " INTEGER NOT NULL,"
63                     + TIME_MILLIS + " INTEGER NOT NULL,"
64                     + EVENT_DATA + " BLOB NOT NULL,"
65                     + "FOREIGN KEY(" + QUERY_ID + ") REFERENCES "
66                         + QueriesContract.QueriesEntry.TABLE_NAME + "("
67                         + QueriesContract.QueriesEntry.QUERY_ID + "))";
68 
69         public static final List<String> UPGRADE_V4_TO_V5_STATEMENTS =
70                 List.of(
71                     "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "_NEW ("
72                         + EVENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
73                         + QUERY_ID + " INTEGER NOT NULL,"
74                         + ROW_INDEX + " INTEGER NOT NULL,"
75                         + SERVICE_NAME + " TEXT NOT NULL,"
76                         + TYPE + " INTEGER NOT NULL,"
77                         + TIME_MILLIS + " INTEGER NOT NULL,"
78                         + EVENT_DATA + " BLOB NOT NULL,"
79                         + "FOREIGN KEY(" + QUERY_ID + ") REFERENCES "
80                             + QueriesContract.QueriesEntry.TABLE_NAME + "("
81                             + QueriesContract.QueriesEntry.QUERY_ID + "))",
82                     "INSERT INTO " + TABLE_NAME + "_NEW "
83                         + "SELECT EVENT_ID, QUERY_ID, ROW_INDEX, "
84                         + "SERVICE_NAME, TYPE, TIME_MILLIS, EVENT_DATA "
85                         + "FROM " + TABLE_NAME,
86                     "DROP TABLE " + TABLE_NAME,
87                     "ALTER TABLE " + TABLE_NAME + "_NEW "
88                         + "RENAME TO " + TABLE_NAME
89                 );
90 
EventsEntry()91         private EventsEntry() {}
92     }
93 }
94