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.h"
17
18 #include "common.h"
19 #include "contacts_columns.h"
20 #include "hilog_wrapper.h"
21
22 namespace OHOS {
23 namespace Contacts {
Contacts(void)24 Contacts::Contacts(void)
25 {
26 }
27
~Contacts()28 Contacts::~Contacts()
29 {
30 }
31
32 /**
33 * @brief Insert the raw_contact table at the same time as the contact table
34 *
35 * @param rdbStore Database operation object
36 * @param outRawContactId raw_contact table id
37 * @param rawContactValues inserted raw_contact values
38 * @param contactId contact_id address
39 *
40 * @return insert contact error or success code
41 */
InsertContact(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,const int64_t & outRawContactId,OHOS::NativeRdb::ValuesBucket rawContactValues,int64_t & contactId)42 int Contacts::InsertContact(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore, const int64_t &outRawContactId,
43 OHOS::NativeRdb::ValuesBucket rawContactValues, int64_t &contactId)
44 {
45 OHOS::NativeRdb::ValuesBucket contactValues = StructureContactDataValueBucket(rawContactValues);
46 contactValues.PutInt(ContactColumns::NAME_RAW_CONTACT_ID, outRawContactId);
47 int rowContactRet = rdbStore->Insert(contactId, ContactTableName::CONTACT, contactValues);
48 return rowContactRet;
49 }
50
UpdateContact(const int & rawContactId,std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,OHOS::NativeRdb::ValuesBucket linkDataDataValues)51 int Contacts::UpdateContact(const int &rawContactId, std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,
52 OHOS::NativeRdb::ValuesBucket linkDataDataValues)
53 {
54 std::string upWhereClause;
55 upWhereClause.append(ContactColumns::NAME_RAW_CONTACT_ID).append(" = ?");
56 std::vector<std::string> upWhereArgs;
57 upWhereArgs.push_back(std::to_string(rawContactId));
58 int changedRows = OHOS::NativeRdb::E_OK;
59 int ret = rdbStore->Update(changedRows, ContactTableName::CONTACT, linkDataDataValues, upWhereClause, upWhereArgs);
60 if (ret != OHOS::NativeRdb::E_OK) {
61 HILOG_ERROR("ContactsUpdateHelper UpdateDisplay UpdateSearchContact fail:%{public}d", ret);
62 }
63 return ret;
64 }
65
66 /**
67 * @brief Build conatct data
68 *
69 * @param rawContactValues inserted contact values
70 *
71 * @return contact table values
72 */
StructureContactDataValueBucket(OHOS::NativeRdb::ValuesBucket rawContactValues)73 OHOS::NativeRdb::ValuesBucket Contacts::StructureContactDataValueBucket(OHOS::NativeRdb::ValuesBucket rawContactValues)
74 {
75 OHOS::NativeRdb::ValuesBucket contactValueBucket;
76 if (rawContactValues.HasColumn(RawContactColumns::PHOTO_FILE_ID)) {
77 int result = ContactValueBucketGetInt(rawContactValues, RawContactColumns::PHOTO_FILE_ID);
78 contactValueBucket.PutInt(ContactColumns::PHOTO_FILE_ID, result);
79 }
80 if (rawContactValues.HasColumn(RawContactColumns::READ_ONLY)) {
81 int result = ContactValueBucketGetInt(rawContactValues, RawContactColumns::READ_ONLY);
82 contactValueBucket.PutInt(ContactColumns::READ_ONLY, result);
83 }
84 if (rawContactValues.HasColumn(RawContactColumns::PHOTO_ID)) {
85 int result = ContactValueBucketGetInt(rawContactValues, RawContactColumns::PHOTO_ID);
86 contactValueBucket.PutInt(ContactColumns::PHOTO_ID, result);
87 }
88 if (rawContactValues.HasColumn(RawContactColumns::IS_TRANSFER_VOICEMAIL)) {
89 int result = ContactValueBucketGetInt(rawContactValues, RawContactColumns::IS_TRANSFER_VOICEMAIL);
90 contactValueBucket.PutInt(ContactColumns::IS_TRANSFER_VOICEMAIL, result);
91 }
92 if (rawContactValues.HasColumn(RawContactColumns::COMPANY)) {
93 std::string result = ContactValueBucketGetString(rawContactValues, RawContactColumns::COMPANY);
94 contactValueBucket.PutString(ContactColumns::COMPANY, result);
95 }
96 if (rawContactValues.HasColumn(RawContactColumns::POSITION)) {
97 std::string result = ContactValueBucketGetString(rawContactValues, RawContactColumns::POSITION);
98 contactValueBucket.PutString(ContactColumns::POSITION, result);
99 }
100 return contactValueBucket;
101 }
102
ContactValueBucketGetInt(OHOS::NativeRdb::ValuesBucket rawContactValues,std::string columnName)103 int Contacts::ContactValueBucketGetInt(OHOS::NativeRdb::ValuesBucket rawContactValues, std::string columnName)
104 {
105 int result = OHOS::NativeRdb::E_OK;
106 OHOS::NativeRdb::ValueObject value;
107 rawContactValues.GetObject(columnName, value);
108 value.GetInt(result);
109 return result;
110 }
111
ContactValueBucketGetString(OHOS::NativeRdb::ValuesBucket rawContactValues,std::string columnName)112 std::string Contacts::ContactValueBucketGetString(
113 OHOS::NativeRdb::ValuesBucket rawContactValues, std::string columnName)
114 {
115 std::string result;
116 OHOS::NativeRdb::ValueObject value;
117 rawContactValues.GetObject(columnName, value);
118 value.GetString(result);
119 return result;
120 }
121
DeleteContactById(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,int needDeleteContactId)122 int Contacts::DeleteContactById(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore, int needDeleteContactId)
123 {
124 std::shared_ptr<OHOS::NativeRdb::RdbStore> &store = rdbStore;
125 if (store == nullptr) {
126 HILOG_ERROR("ContactsAccount DeletecontactById store is nullptr");
127 return RDB_OBJECT_EMPTY;
128 }
129 if (needDeleteContactId < ID_EMPTY) {
130 HILOG_ERROR("ContactsAccount DeleteDataByRawId needDeleteContactId illegal");
131 return OPERATION_ERROR;
132 }
133 int rowId = OHOS::NativeRdb::E_OK;
134 std::vector<std::string> whereArgs;
135 whereArgs.push_back(std::to_string(needDeleteContactId));
136 std::string whereCase;
137 whereCase.append(ContactColumns::ID).append(" = ?");
138 int delContact = store->Delete(rowId, ContactTableName::CONTACT, whereCase, whereArgs);
139 return delContact;
140 }
141 } // namespace Contacts
142 } // namespace OHOS