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#include "animation_table.h"
17
18#include <cmath>
19
20namespace SysTuning {
21namespace TraceStreamer {
22enum class Index : int32_t { ID = 0, INPUT_TIME, START_POINT, END_POINT, FRAME_INFO, NAME };
23AnimationTable::AnimationTable(const TraceDataCache *dataCache) : TableBase(dataCache)
24{
25    tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
26    tableColumn_.push_back(TableBase::ColumnInfo("input_time", "INTEGER"));
27    tableColumn_.push_back(TableBase::ColumnInfo("start_point", "INTEGER"));
28    tableColumn_.push_back(TableBase::ColumnInfo("end_point", "INTEGER"));
29    tableColumn_.push_back(TableBase::ColumnInfo("frame_info", "TEXT"));
30    tableColumn_.push_back(TableBase::ColumnInfo("name", "TEXT"));
31    tablePriKey_.push_back("id");
32}
33
34AnimationTable::~AnimationTable() {}
35
36std::unique_ptr<TableBase::Cursor> AnimationTable::CreateCursor()
37{
38    return std::make_unique<Cursor>(dataCache_, this);
39}
40
41AnimationTable::Cursor::Cursor(const TraceDataCache *dataCache, TableBase *table)
42    : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstAnimation().Size())),
43      animationObj_(dataCache->GetConstAnimation())
44{
45}
46
47AnimationTable::Cursor::~Cursor() {}
48
49int32_t AnimationTable::Cursor::Column(int32_t col) const
50{
51    switch (static_cast<Index>(col)) {
52        case Index::ID:
53            sqlite3_result_int64(context_, static_cast<sqlite3_int64>(animationObj_.IdsData()[CurrentRow()]));
54            break;
55        case Index::INPUT_TIME: {
56            if (animationObj_.InputTimes()[CurrentRow()] != INVALID_TIME) {
57                sqlite3_result_int64(context_, static_cast<sqlite3_int64>(animationObj_.InputTimes()[CurrentRow()]));
58            }
59            break;
60        }
61        case Index::START_POINT: {
62            if (animationObj_.StartPoints()[CurrentRow()] != INVALID_TIME) {
63                sqlite3_result_int64(context_, static_cast<sqlite3_int64>(animationObj_.StartPoints()[CurrentRow()]));
64            }
65            break;
66        }
67        case Index::END_POINT:
68            if (animationObj_.EndPoints()[CurrentRow()] != INVALID_TIME) {
69                sqlite3_result_int64(context_, static_cast<sqlite3_int64>(animationObj_.EndPoints()[CurrentRow()]));
70            }
71            break;
72        case Index::FRAME_INFO:
73            sqlite3_result_text(context_, dataCache_->GetDataFromDict(animationObj_.FrameInfos()[CurrentRow()]).c_str(),
74                                STR_DEFAULT_LEN, nullptr);
75            break;
76        case Index::NAME:
77            sqlite3_result_text(context_, dataCache_->GetDataFromDict(animationObj_.Names()[CurrentRow()]).c_str(),
78                                STR_DEFAULT_LEN, nullptr);
79            break;
80        default:
81            TS_LOGF("Unregistered column : %d", col);
82            break;
83    }
84    return SQLITE_OK;
85}
86} // namespace TraceStreamer
87} // namespace SysTuning
88