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 #pragma once
17
18 #include <sqlite3.h>
19
20 #include "config/ConfigKey.h"
21 #include "logd/LogEvent.h"
22
23 using std::string;
24 using std::vector;
25
26 namespace android {
27 namespace os {
28 namespace statsd {
29 namespace dbutils {
30
31 #define STATS_RESTRICTED_DATA_DIR "/data/misc/stats-data/restricted-data"
32
getDbVersion()33 inline int32_t getDbVersion() {
34 return SQLITE_VERSION_NUMBER;
35 };
36
37 string getDbName(const ConfigKey& key);
38
39 string reformatMetricId(const int64_t metricId);
40
41 /* Creates a new data table for a specified metric if one does not yet exist. */
42 bool createTableIfNeeded(const ConfigKey& key, int64_t metricId, const LogEvent& event);
43
44 /* Checks whether the table schema for the given metric matches the event.
45 * Returns true if the table has not yet been created.
46 */
47 bool isEventCompatible(const ConfigKey& key, int64_t metricId, const LogEvent& event);
48
49 /* Deletes a data table for the specified metric. */
50 bool deleteTable(const ConfigKey& key, int64_t metricId);
51
52 /* Deletes the SQLite db data file. */
53 void deleteDb(const ConfigKey& key);
54
55 /* Gets a handle to the sqlite db. You must call closeDb to free the allocated memory.
56 * Returns a nullptr if an error occurs.
57 */
58 sqlite3* getDb(const ConfigKey& key);
59
60 /* Closes the handle to the sqlite db. */
61 void closeDb(sqlite3* db);
62
63 /* Inserts new data into the specified metric data table.
64 * A temp sqlite handle is created using the ConfigKey.
65 */
66 bool insert(const ConfigKey& key, int64_t metricId, const vector<LogEvent>& events, string& error);
67
68 /* Inserts new data into the specified sqlite db handle. */
69 bool insert(sqlite3* db, int64_t metricId, const vector<LogEvent>& events, string& error);
70
71 /* Executes a sql query on the specified SQLite db.
72 * A temp sqlite handle is created using the ConfigKey.
73 */
74 bool query(const ConfigKey& key, const string& zSql, vector<vector<string>>& rows,
75 vector<int32_t>& columnTypes, vector<string>& columnNames, string& err);
76
77 bool flushTtl(sqlite3* db, int64_t metricId, int64_t ttlWallClockNs);
78
79 /* Checks for database corruption and deletes the db if it is corrupted. */
80 void verifyIntegrityAndDeleteIfNecessary(const ConfigKey& key);
81
82 /* Creates and updates the device info table for the given configKey. */
83 bool updateDeviceInfoTable(const ConfigKey& key, string& error);
84
85 } // namespace dbutils
86 } // namespace statsd
87 } // namespace os
88 } // namespace android
89