• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef TRACE_DATA_DB_H
17 #define TRACE_DATA_DB_H
18 
19 #include <functional>
20 #include <list>
21 #include <string>
22 
23 extern "C" {
24 #include "sqlite3.h"
25 }
26 
27 namespace SysTuning {
28 namespace TraceStreamer {
29 const int SEND_CONTINUE = 0;
30 const int SEND_FINISH = 1;
31 class TraceDataDB {
32 public:
33     TraceDataDB();
34     TraceDataDB(const TraceDataDB&) = delete;
35     TraceDataDB& operator=(const TraceDataDB&) = delete;
36     virtual ~TraceDataDB();
37     virtual void InitDB() = 0;
38     void Prepare();
39 
40 public:
41     int ExportDatabase(const std::string& outputName);
42     int SearchData();
43     int OperateDatabase(const std::string& sql);
44     using ResultCallBack = std::function<void(const std::string /* json result */, int)>;
45     int SearchDatabase(const std::string& sql, ResultCallBack resultCallBack);
46     int SearchDatabase(const std::string& sql, uint8_t* out, int outLen);
47     void SetCancel(bool cancel);
48     void AppendNewTable(std::string tableName);
49     void EnableMetaTable(bool enabled);
Cancel()50     bool Cancel() const
51     {
52         return cancelQuery_;
53     }
54 
55 public:
56     sqlite3* db_;
57 
58 private:
59     void ExecuteSql(const std::string_view& sql);
60     static void GetRowString(sqlite3_stmt* stmt, int colCount, std::string& rowStr);
61     int SearchDatabase(const std::string& sql, bool print);
62     std::list<std::string> internalTables_ = {};
63     bool exportMetaTable_ = true;
64     bool pared_ = false;
65     bool cancelQuery_ = false;
66 };
67 } // namespace TraceStreamer
68 } // namespace SysTuning
69 #endif
70