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 "value_object.h" 17 18 #include "rdb_errno.h" 19 #include "sqlite_utils.h" 20 21 namespace OHOS { 22 namespace NativeRdb { ValueObject()23ValueObject::ValueObject() 24 { 25 } 26 ValueObject(Type val)27ValueObject::ValueObject(Type val) noexcept : value(std::move(val)) 28 { 29 } 30 ValueObject(ValueObject && val)31ValueObject::ValueObject(ValueObject &&val) noexcept 32 { 33 if (this == &val) { 34 return; 35 } 36 value = std::move(val.value); 37 } 38 ValueObject(const ValueObject & val)39ValueObject::ValueObject(const ValueObject &val) 40 { 41 if (this == &val) { 42 return; 43 } 44 value = val.value; 45 } 46 ~ValueObject()47ValueObject::~ValueObject() 48 { 49 } 50 ValueObject(int val)51ValueObject::ValueObject(int val) : value(static_cast<int64_t>(val)) 52 { 53 } 54 ValueObject(int64_t val)55ValueObject::ValueObject(int64_t val) : value(val) 56 { 57 } 58 ValueObject(double val)59ValueObject::ValueObject(double val) : value(val) 60 { 61 } 62 ValueObject(bool val)63ValueObject::ValueObject(bool val) : value(val) 64 { 65 } 66 ValueObject(std::string val)67ValueObject::ValueObject(std::string val) : value(std::move(val)) 68 { 69 } 70 ValueObject(const char * val)71ValueObject::ValueObject(const char *val) : ValueObject(std::string(val)) 72 { 73 } 74 ValueObject(const std::vector<uint8_t> & val)75ValueObject::ValueObject(const std::vector<uint8_t> &val) : value(val) 76 { 77 } 78 ValueObject(ValueObject::Asset val)79ValueObject::ValueObject(ValueObject::Asset val) : value(std::move(val)) 80 { 81 } 82 ValueObject(ValueObject::Assets val)83ValueObject::ValueObject(ValueObject::Assets val) : value(std::move(val)) 84 { 85 } 86 operator =(ValueObject && val)87ValueObject &ValueObject::operator=(ValueObject &&val) noexcept 88 { 89 if (this == &val) { 90 return *this; 91 } 92 value = std::move(val.value); 93 return *this; 94 } 95 operator =(const ValueObject & val)96ValueObject &ValueObject::operator=(const ValueObject &val) 97 { 98 if (this == &val) { 99 return *this; 100 } 101 value = val.value; 102 return *this; 103 } 104 GetType() const105ValueObjectType ValueObject::GetType() const 106 { 107 return ValueObjectType(value.index()); 108 } 109 GetInt(int & val) const110int ValueObject::GetInt(int &val) const 111 { 112 int64_t value = 0; 113 auto ret = Get(value); 114 val = value; 115 return ret; 116 } 117 GetLong(int64_t & val) const118int ValueObject::GetLong(int64_t &val) const 119 { 120 return Get(val); 121 } 122 GetDouble(double & val) const123int ValueObject::GetDouble(double &val) const 124 { 125 return Get(val); 126 } 127 GetBool(bool & val) const128int ValueObject::GetBool(bool &val) const 129 { 130 return Get(val); 131 } 132 GetString(std::string & val) const133int ValueObject::GetString(std::string &val) const 134 { 135 if (Get(val) == 0) { 136 return E_OK; 137 } 138 139 double ftmp; 140 if (Get(ftmp) == 0) { 141 val = std::to_string(ftmp); 142 return E_OK; 143 } 144 145 int64_t itmp; 146 if (Get(itmp) == 0) { 147 val = std::to_string(itmp); 148 return E_OK; 149 } 150 151 bool btmp; 152 if (Get(btmp) == 0) { 153 val = std::to_string(btmp); 154 return E_OK; 155 } 156 return E_INVALID_OBJECT_TYPE; 157 } 158 GetBlob(std::vector<uint8_t> & val) const159int ValueObject::GetBlob(std::vector<uint8_t> &val) const 160 { 161 return Get(val); 162 } 163 GetAsset(Asset & val) const164int ValueObject::GetAsset(Asset &val) const 165 { 166 return Get(val); 167 } 168 GetAssets(Assets & val) const169int ValueObject::GetAssets(Assets &val) const 170 { 171 return Get(val); 172 } 173 174 template<class T> Get(T & output) const175int ValueObject::Get(T &output) const 176 { 177 const T *v = std::get_if<T>(&value); 178 if (v == nullptr) { 179 return E_INVALID_OBJECT_TYPE; 180 } 181 output = static_cast<T>(*v); 182 return E_OK; 183 } 184 } // namespace NativeRdb 185 } // namespace OHOS 186