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 #include "table_base.h"
17 #include <cctype>
18 #include <cstring>
19 #include "log.h"
20
21 #define UNUSED(expr) \
22 do { \
23 static_cast<void>(expr); \
24 } while (0)
25
26 namespace SysTuning {
27 namespace TraceStreamer {
28 namespace {
29 struct TableContext {
30 TabTemplate tmplate;
31 const TraceDataCache* dataCache;
32 sqlite3_module module;
33 };
34 } // namespace
35
~TableBase()36 TableBase::~TableBase()
37 {
38 dataCache_ = nullptr;
39 cursor_ = nullptr;
40 }
41
TableRegister(sqlite3 & db,const TraceDataCache * cache,const std::string & tableName,TabTemplate tmplate)42 void TableBase::TableRegister(sqlite3& db,
43 const TraceDataCache* cache,
44 const std::string& tableName,
45 TabTemplate tmplate)
46 {
47 std::unique_ptr<TableContext> context(std::make_unique<TableContext>());
48 context->dataCache = cache;
49 context->tmplate = tmplate;
50 sqlite3_module& module = context->module;
51
52 auto createFn = [](sqlite3* xdb, void* arg, int argc, const char* const* argv, sqlite3_vtab** tab, char** other) {
53 UNUSED(argc);
54 UNUSED(argv);
55 UNUSED(other);
56 auto xdesc = static_cast<const TableContext*>(arg);
57 auto table = xdesc->tmplate(xdesc->dataCache);
58 std::string createStmt = table->CreateTableSql();
59 int res = sqlite3_declare_vtab(xdb, createStmt.c_str());
60 if (res != SQLITE_OK) {
61 return res;
62 }
63 *tab = table.release();
64 return SQLITE_OK;
65 };
66
67 auto destroyFn = [](sqlite3_vtab* t) {
68 delete static_cast<TableBase*>(t);
69 return SQLITE_OK;
70 };
71
72 module.xCreate = createFn;
73 module.xConnect = createFn;
74 module.xBestIndex = [](sqlite3_vtab*, sqlite3_index_info*) { return SQLITE_OK; };
75 module.xDisconnect = destroyFn;
76 module.xDestroy = destroyFn;
77 module.xOpen = [](sqlite3_vtab* t, sqlite3_vtab_cursor** c) {
78 return (static_cast<TableBase*>(t))->Open(c);
79 };
80 module.xClose = [](sqlite3_vtab_cursor* c) {
81 UNUSED(c);
82 return SQLITE_OK;
83 };
84 module.xFilter = [](sqlite3_vtab_cursor* c, int arg1, const char* arg2, int, sqlite3_value** sqlite) {
85 UNUSED(c);
86 UNUSED(arg1);
87 UNUSED(arg2);
88 UNUSED(sqlite);
89 return SQLITE_OK;
90 };
91 module.xNext = [](sqlite3_vtab_cursor* c) { return static_cast<TableBase::Cursor*>(c)->Next(); };
92 module.xEof = [](sqlite3_vtab_cursor* c) { return static_cast<TableBase::Cursor*>(c)->Eof(); };
93 module.xColumn = [](sqlite3_vtab_cursor* c, sqlite3_context* a, int b) {
94 static_cast<TableBase::Cursor*>(c)->context_ = a;
95 return static_cast<TableBase::Cursor*>(c)->Column(b);
96 };
97
98 sqlite3_create_module_v2(&db, tableName.c_str(), &module, context.release(),
99 [](void* arg) { delete static_cast<TableContext*>(arg); });
100 }
101
CreateTableSql() const102 std::string TableBase::CreateTableSql() const
103 {
104 std::string stmt = "CREATE TABLE x(";
105 for (const auto& col : tableColumn_) {
106 stmt += " " + col.name_ + " " + col.type_;
107 stmt += ",";
108 }
109 stmt += " PRIMARY KEY(";
110 for (size_t i = 0; i < tablePriKey_.size(); i++) {
111 if (i != 0)
112 stmt += ", ";
113 stmt += tablePriKey_.at(i);
114 }
115 stmt += ")) WITHOUT ROWID;";
116 return stmt;
117 }
118
Open(sqlite3_vtab_cursor ** ppCursor)119 int TableBase::Open(sqlite3_vtab_cursor** ppCursor)
120 {
121 CreateCursor();
122 *ppCursor = static_cast<sqlite3_vtab_cursor*>(cursor_.get());
123 return SQLITE_OK;
124 }
125
Cursor(const TraceDataCache * dataCache,uint32_t row,uint32_t totalRows)126 TableBase::Cursor::Cursor(const TraceDataCache* dataCache, uint32_t row, uint32_t totalRows)
127 : context_(nullptr), dataCache_(dataCache), currentRow_(row), rowsTotalNum_(totalRows)
128 {
129 }
130
~Cursor()131 TableBase::Cursor::~Cursor()
132 {
133 context_ = nullptr;
134 dataCache_ = nullptr;
135 }
136
Next()137 int TableBase::Cursor::Next()
138 {
139 currentRow_++;
140 return SQLITE_OK;
141 }
142
Eof()143 int TableBase::Cursor::Eof()
144 {
145 return currentRow_ >= rowsTotalNum_;
146 }
147
CurrentRow() const148 uint32_t TableBase::Cursor::CurrentRow() const
149 {
150 return currentRow_;
151 }
152 } // namespace TraceStreamer
153 } // namespace SysTuning
154