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 TABLE_H 17 #define TABLE_H 18 19 #include <memory> 20 #include <sqlite3.h> 21 #include <string> 22 #include <vector> 23 #include "trace_data_cache.h" 24 25 namespace SysTuning { 26 namespace TraceStreamer { 27 class TableBase; 28 constexpr int STR_DEFAULT_LEN = -1; 29 using TabTemplate = std::unique_ptr<TableBase> (*)(const TraceDataCache* dataCache); 30 class TableBase : public sqlite3_vtab { 31 public: 32 virtual ~TableBase(); 33 TableBase(const TableBase&) = delete; 34 TableBase& operator=(const TableBase&) = delete; 35 36 template<typename T> TableDeclare(sqlite3 & db,TraceDataCache * dataCache,const std::string & name)37 static void TableDeclare(sqlite3& db, TraceDataCache* dataCache, const std::string& name) 38 { 39 TableRegister(db, dataCache, name, [](const TraceDataCache* cache) { 40 return std::unique_ptr<TableBase>(std::make_unique<T>(cache)); 41 }); 42 dataCache->AppendNewTable(name); 43 } 44 45 std::string CreateTableSql() const; 46 47 class Cursor : public sqlite3_vtab_cursor { 48 public: 49 Cursor(const TraceDataCache*, uint32_t, uint32_t); 50 virtual ~Cursor(); 51 virtual int Next(); 52 virtual int Eof(); 53 virtual int Column(int) const = 0; 54 public: 55 sqlite3_context* context_; 56 protected: 57 uint32_t CurrentRow() const; 58 protected: 59 const TraceDataCache* dataCache_; 60 private: 61 uint32_t currentRow_; 62 uint32_t rowsTotalNum_; 63 }; 64 65 struct ColumnInfo { ColumnInfoColumnInfo66 ColumnInfo(const std::string& name, const std::string& type) : name_(name), type_(type) {} 67 std::string name_; 68 std::string type_; 69 }; 70 71 protected: TableBase(const TraceDataCache * dataCache)72 explicit TableBase(const TraceDataCache* dataCache) : dataCache_(dataCache), cursor_(nullptr) {} 73 virtual void CreateCursor() = 0; 74 protected: 75 std::vector<ColumnInfo> tableColumn_ = {}; 76 std::vector<std::string> tablePriKey_ = {}; 77 const TraceDataCache* dataCache_; 78 std::unique_ptr<Cursor> cursor_; 79 private: 80 static void TableRegister(sqlite3& db, const TraceDataCache* cache, const std::string& name, TabTemplate tmplate); 81 int Open(sqlite3_vtab_cursor** ppCursor); 82 }; 83 } // namespace TraceStreamer 84 } // namespace SysTuning 85 86 #endif // TABLE_H 87