1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. 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 DEMO_TRACE_DATA_DB_H 17 #define DEMO_TRACE_DATA_DB_H 18 19 #include <functional> 20 #include <list> 21 #include <string> 22 #include "sqlite3.h" 23 24 namespace SysTuning { 25 namespace TraceStreamer { 26 const int32_t SEND_CONTINUE = 0; 27 const int32_t SEND_FINISH = 1; 28 class DemoTraceDataDB { 29 public: 30 DemoTraceDataDB(); 31 DemoTraceDataDB(const DemoTraceDataDB &) = delete; 32 DemoTraceDataDB &operator=(const DemoTraceDataDB &) = delete; 33 virtual ~DemoTraceDataDB(); 34 virtual void DemoInitDB() = 0; 35 void DemoPrepare(); 36 37 public: 38 int32_t DemoExportDatabase(const std::string &outputName); 39 int32_t DemoSearchData(); 40 int32_t DemoOperateDatabase(const std::string &sql); 41 using ResultCallBack = std::function<void(const std::string /* json result */, int32_t, int32_t)>; 42 bool AddColumnsToJsonArray(sqlite3_stmt *stmtSql, 43 char *resValue, 44 const int32_t outLen, 45 int32_t &pos, 46 const int32_t colCount); 47 bool AddRowsToJsonArray(sqlite3_stmt *stmtSql, 48 char *resValue, 49 const int32_t outLen, 50 int32_t &pos, 51 const int32_t colCount); 52 int32_t DemoSearchDatabase(const std::string &sql, ResultCallBack resultCallBack); 53 int32_t DemoSearchDatabase(const std::string &sql, uint8_t *out, int32_t outLen); 54 void DemoSetCancel(bool cancel); 55 void DemoAppendNewTable(std::string tableName); 56 void DemoEnableMetaTable(bool enabled); DemoCancel()57 bool DemoCancel() const 58 { 59 return demoCancelQuery_; 60 } 61 62 public: 63 sqlite3 *demoDb_; 64 65 private: 66 void DemoExecuteSql(const std::string_view &sql); 67 static void DemoGetRowString(sqlite3_stmt *stmt, int32_t colCount, std::string &rowStr); 68 int32_t DemoSearchDatabase(const std::string &sql, bool print); 69 std::list<std::string> demoInternalTables_ = {}; 70 bool demoExportMetaTable_ = true; 71 bool demoPared_ = false; 72 bool demoCancelQuery_ = false; 73 }; 74 } // namespace TraceStreamer 75 } // namespace SysTuning 76 #endif 77