1 /*
2  * Copyright (c) 2023 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 OHOS_DISTRIBUTED_DATA_SERVICES_EXTENSION_CLOUD_CURSOR_IMPL_H
17 #define OHOS_DISTRIBUTED_DATA_SERVICES_EXTENSION_CLOUD_CURSOR_IMPL_H
18 
19 #include <set>
20 #include "basic_rust_types.h"
21 #include "cloud_ext_types.h"
22 #include "cloud/schema_meta.h"
23 #include "store/cursor.h"
24 
25 namespace OHOS::CloudData {
26 using DBVBucket = DistributedData::VBucket;
27 using DBSchemaMeta = DistributedData::SchemaMeta;
28 using DBValue = DistributedData::Value;
29 using DBErr = DistributedData::GeneralError;
30 class CloudCursorImpl : public DistributedData::Cursor {
31 public:
32     explicit CloudCursorImpl(OhCloudExtCloudDbData *cloudData);
33     ~CloudCursorImpl();
34     int32_t GetColumnNames(std::vector<std::string> &names) const override;
35     int32_t GetColumnName(int32_t col, std::string &name) const override;
36     int32_t GetColumnType(int32_t col) const override;
37     int32_t GetCount() const override;
38     int32_t MoveToFirst() override;
39     int32_t MoveToNext() override;
40     int32_t MoveToPrev() override;
41     int32_t GetEntry(DBVBucket &entry) override;
42     int32_t GetRow(DBVBucket &data) override;
43     int32_t Get(int32_t col, DBValue &value) override;
44     int32_t Get(const std::string &col, DBValue &value) override;
45     int32_t Close() override;
46     bool IsEnd() override;
47 
48 private:
49     enum Flag : int32_t {
50         INSERT = 0,
51         UPDATE = 1,
52         DELETE = 2
53     };
54     static const size_t INVALID_INDEX = SIZE_MAX;
55     static constexpr const char *OPERATION_KEY = "operation";
56     static constexpr const char *GID_KEY = "id";
57     static constexpr const char *CREATE_TIME_KEY = "createTime";
58     static constexpr const char *MODIFY_TIME_KEY = "modifyTime";
59     template<class T>
60     inline static constexpr auto TYPE_INDEX = DistributedData::TYPE_INDEX<T>;
61 
62     std::vector<std::pair<std::string, DBValue>> GetData(OhCloudExtValueBucket *vb);
63     DBValue GetExtend(OhCloudExtValueBucket *vb, const std::string &col);
64     OhCloudExtCloudDbData *cloudData_ = nullptr;
65     OhCloudExtVector *values_ = nullptr;
66     size_t valuesLen_ = 0;
67     bool finished_ = true;
68     std::string cursor_;
69     size_t index_ = INVALID_INDEX;
70     bool consumed_ = false;
71     std::vector<std::string> names_;
72 };
73 } // namespace OHOS::CloudData
74 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_EXTENSION_CLOUD_CURSOR_IMPL_H
75