• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 DISTRIBUTED_DB_DATA_VALUE_H
17 #define DISTRIBUTED_DB_DATA_VALUE_H
18 
19 #include <cstdint>
20 #include <macro_utils.h>
21 #include <map>
22 #include <string>
23 namespace DistributedDB {
24 // field types stored in sqlite
25 enum class StorageType {
26     STORAGE_TYPE_NONE = 0,
27     STORAGE_TYPE_NULL,
28     STORAGE_TYPE_INTEGER,
29     STORAGE_TYPE_REAL,
30     STORAGE_TYPE_TEXT,
31     STORAGE_TYPE_BLOB
32 };
33 
34 class Blob {
35 public:
36     Blob();
37     ~Blob();
38 
39     Blob(Blob &&);
40     Blob(const Blob &) = delete;
41     Blob &operator=(Blob &&) noexcept;
42     Blob &operator=(const Blob &) = delete;
43 
44     const uint8_t* GetData() const;
45     uint32_t GetSize() const;
46 
47     int WriteBlob(const uint8_t *ptrArray, const uint32_t &size);
48 
49 private:
50     uint8_t* ptr_;
51     uint32_t size_;
52 };
53 
54 class DataValue {
55 public:
56     DataValue();
57     ~DataValue();
58 
59     // copy constructor
60     DataValue(const DataValue &dataValue);
61     DataValue &operator=(const DataValue &dataValue);
62     // move constructor
63     DataValue(DataValue &&dataValue) noexcept;
64     DataValue &operator=(DataValue &&dataValue) noexcept;
65     DataValue &operator=(int64_t intVal);
66     DataValue &operator=(double doubleVal);
67     DataValue &operator=(const Blob &blob);
68     DataValue &operator=(const std::string &string);
69     int Set(Blob *&blob);
70 
71     // equals
72     bool operator==(const DataValue &dataValue) const;
73     bool operator!=(const DataValue &dataValue) const;
74 
75     StorageType GetType() const;
76     int GetInt64(int64_t &outVal) const;
77     int GetDouble(double &outVal) const;
78     int GetBlob(Blob *&outVal) const;
79     int SetBlob(const Blob &val);
80     int GetBlob(Blob &outVal) const;
81     int SetText(const std::string &val);
82     int SetText(const uint8_t *val, uint32_t length);
83     int GetText(std::string &outVal) const;
84     void ResetValue();
85     int GetBlobLength(uint32_t &length) const;
86     std::string ToString() const;
87 
88 private:
89     StorageType type_ = StorageType::STORAGE_TYPE_NULL;
90     union {
91         void* zeroMem;
92         Blob* blobPtr;
93         double dValue;
94         int64_t iValue;
95     } value_{};
96 };
97 }
98 #endif // DISTRIBUTED_DB_DATA_VALUE_H
99