• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "sqlite_relational_utils.h"
17 #include "db_errno.h"
18 #include "sqlite_utils.h"
19 
20 namespace DistributedDB {
GetDataValueByType(sqlite3_stmt * statement,int cid,DataValue & value)21 int SQLiteRelationalUtils::GetDataValueByType(sqlite3_stmt *statement, int cid, DataValue &value)
22 {
23     if (statement == nullptr || cid < 0 || cid >= sqlite3_column_count(statement)) {
24         return -E_INVALID_ARGS;
25     }
26 
27     int errCode = E_OK;
28     int storageType = sqlite3_column_type(statement, cid);
29     switch (storageType) {
30         case SQLITE_INTEGER: {
31             value = static_cast<int64_t>(sqlite3_column_int64(statement, cid));
32             break;
33         }
34         case SQLITE_FLOAT: {
35             value = sqlite3_column_double(statement, cid);
36             break;
37         }
38         case SQLITE_BLOB: {
39             std::vector<uint8_t> blobValue;
40             errCode = SQLiteUtils::GetColumnBlobValue(statement, cid, blobValue);
41             if (errCode != E_OK) {
42                 return errCode;
43             }
44             auto blob = new (std::nothrow) Blob;
45             if (blob == nullptr) {
46                 return -E_OUT_OF_MEMORY;
47             }
48             blob->WriteBlob(blobValue.data(), static_cast<uint32_t>(blobValue.size()));
49             errCode = value.Set(blob);
50             break;
51         }
52         case SQLITE_NULL: {
53             break;
54         }
55         case SQLITE3_TEXT: {
56             std::string str;
57             (void)SQLiteUtils::GetColumnTextValue(statement, cid, str);
58             value = str;
59             if (value.GetType() != StorageType::STORAGE_TYPE_TEXT) {
60                 errCode = -E_OUT_OF_MEMORY;
61             }
62             break;
63         }
64         default: {
65             break;
66         }
67     }
68     return errCode;
69 }
70 
GetSelectValues(sqlite3_stmt * stmt)71 std::vector<DataValue> SQLiteRelationalUtils::GetSelectValues(sqlite3_stmt *stmt)
72 {
73     std::vector<DataValue> values;
74     for (int cid = 0, colCount = sqlite3_column_count(stmt); cid < colCount; ++cid) {
75         DataValue value;
76         (void)GetDataValueByType(stmt,  cid, value);
77         values.emplace_back(std::move(value));
78     }
79     return values;
80 }
81 } // namespace DistributedDB