• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "contacts_json_utils.h"
17 
18 #include "contacts_columns.h"
19 
20 namespace OHOS {
21 namespace Contacts {
ContactsJsonUtils(void)22 ContactsJsonUtils::ContactsJsonUtils(void)
23 {
24 }
25 
~ContactsJsonUtils()26 ContactsJsonUtils::~ContactsJsonUtils()
27 {
28 }
29 
GetDeleteData(std::unique_ptr<OHOS::NativeRdb::AbsSharedResultSet> & resultSet)30 std::string ContactsJsonUtils::GetDeleteData(std::unique_ptr<OHOS::NativeRdb::AbsSharedResultSet> &resultSet)
31 {
32     Json::Value dataResult;
33     Json::Value arrayValue;
34     ConvertResultSet(arrayValue, resultSet);
35     dataResult[AliasName::DATA] = arrayValue;
36     Json::StreamWriterBuilder builder;
37     const std::string personal_ringtone = Json::writeString(builder, dataResult);
38     return personal_ringtone;
39 }
40 
ConvertResultSet(Json::Value & arrayValue,std::unique_ptr<OHOS::NativeRdb::AbsSharedResultSet> & resultSet)41 void ContactsJsonUtils::ConvertResultSet(
42     Json::Value &arrayValue, std::unique_ptr<OHOS::NativeRdb::AbsSharedResultSet> &resultSet)
43 {
44     int resultSetNum = resultSet->GoToFirstRow();
45     std::vector<std::string> columnNames;
46     resultSet->GetAllColumnNames(columnNames);
47     while (resultSetNum == OHOS::NativeRdb::E_OK) {
48         Json::Value data;
49         unsigned int size = columnNames.size();
50         for (unsigned int i = 0; i < size; i++) {
51             GetValue(columnNames, i, data, resultSet);
52         }
53         arrayValue.append(data);
54         resultSetNum = resultSet->GoToNextRow();
55     }
56 }
57 
GetValue(std::vector<std::string> & columnNames,unsigned int & index,Json::Value & data,std::unique_ptr<OHOS::NativeRdb::AbsSharedResultSet> & resultSet)58 void ContactsJsonUtils::GetValue(std::vector<std::string> &columnNames, unsigned int &index, Json::Value &data,
59     std::unique_ptr<OHOS::NativeRdb::AbsSharedResultSet> &resultSet)
60 {
61     std::string typeValue = columnNames[index];
62     int columnIndex;
63     resultSet->GetColumnIndex(typeValue, columnIndex);
64     OHOS::NativeRdb::ColumnType columnType;
65     resultSet->GetColumnType(columnIndex, columnType);
66     if (columnType == OHOS::NativeRdb::ColumnType::TYPE_INTEGER) {
67         int intValue;
68         resultSet->GetInt(columnIndex, intValue);
69         data[typeValue] = intValue;
70     } else if (columnType == OHOS::NativeRdb::ColumnType::TYPE_FLOAT) {
71         double doubleValue;
72         resultSet->GetDouble(columnIndex, doubleValue);
73         data[typeValue] = doubleValue;
74     } else if (columnType == OHOS::NativeRdb::ColumnType::TYPE_STRING) {
75         std::string stringValue;
76         resultSet->GetString(columnIndex, stringValue);
77         if (!stringValue.empty()) {
78             data[typeValue] = stringValue;
79         }
80     }
81 }
82 } // namespace Contacts
83 } // namespace OHOS