• 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_search.h"
17 
18 #include "rdb_errno.h"
19 #include "result_set.h"
20 
21 #include "character_transliterate.h"
22 #include "common.h"
23 #include "contacts_columns.h"
24 #include "hilog_wrapper.h"
25 
26 namespace OHOS {
27 namespace Contacts {
ContactsSearch(void)28 ContactsSearch::ContactsSearch(void)
29 {
30 }
31 
~ContactsSearch()32 ContactsSearch::~ContactsSearch()
33 {
34 }
35 
Insert(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,int64_t contactId,int64_t rawContactId,OHOS::NativeRdb::ValuesBucket rawContactValues,int64_t & searchContactId)36 int64_t ContactsSearch::Insert(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore, int64_t contactId,
37     int64_t rawContactId, OHOS::NativeRdb::ValuesBucket rawContactValues, int64_t &searchContactId)
38 {
39     // searchContact insert data
40     OHOS::NativeRdb::ValuesBucket searchContactValues = StructureSearchContactDataValueBucket(rawContactValues);
41     // add raw_contact_id
42     searchContactValues.PutInt(SearchContactColumns::RAW_CONTACT_ID, rawContactId);
43     // add contact_id
44     searchContactValues.PutInt(SearchContactColumns::CONTACT_ID, contactId);
45     int rowSearchContactRet = rdbStore->Insert(searchContactId, ContactTableName::SEARCH_CONTACT, searchContactValues);
46     return rowSearchContactRet;
47 }
48 
49 /**
50  * @brief Update contact data by quick_search_key
51  *
52  * @param rawContactId Parameters to be passed for update operation
53  * @param type Parameters to be passed for update operation
54  * @param rdbStore Conditions for update operation
55  * @param searchContactValues Parameters to be passed for update operation
56  *
57  * @return The result returned by the update operation
58  */
UpdateSearchContact(int rawContactId,std::string type,std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,OHOS::NativeRdb::ValuesBucket searchContactValues)59 int ContactsSearch::UpdateSearchContact(int rawContactId, std::string type,
60     std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore, OHOS::NativeRdb::ValuesBucket searchContactValues)
61 {
62     std::string upWhereClause;
63     upWhereClause.append(SearchContactColumns::RAW_CONTACT_ID).append(" = ?");
64     std::vector<std::string> upWhereArgs;
65     upWhereArgs.push_back(std::to_string(rawContactId));
66     int changedRows;
67     // update search_contact table name
68     int ret = rdbStore->Update(
69         changedRows, ContactTableName::SEARCH_CONTACT, searchContactValues, upWhereClause, upWhereArgs);
70     if (ret != OHOS::NativeRdb::E_OK) {
71         HILOG_ERROR("ContactsUpdateHelper UpdateDisplay  UpdateSearchContact fail:%{public}d", ret);
72     }
73     return ret;
74 }
75 
76 /**
77  * @brief Convert the rawcontact table insert parameter to the searchcontact table parameter
78  *
79  * @param rawContactValues Parameters to be passed for convert operation
80  *
81  * @return The result returned by the convert operation
82  */
StructureSearchContactDataValueBucket(OHOS::NativeRdb::ValuesBucket rawContactValues)83 OHOS::NativeRdb::ValuesBucket ContactsSearch::StructureSearchContactDataValueBucket(
84     OHOS::NativeRdb::ValuesBucket rawContactValues)
85 {
86     // Will raw_ Convert contact table data to searchcontact table data
87     OHOS::NativeRdb::ValuesBucket searchContactValueBucket;
88     if (rawContactValues.HasColumn(RawContactColumns::ACCOUNT_ID)) {
89         int accountId;
90         OHOS::NativeRdb::ValueObject value;
91         rawContactValues.GetObject(RawContactColumns::ACCOUNT_ID, value);
92         value.GetInt(accountId);
93         searchContactValueBucket.PutInt(SearchContactColumns::ACCOUNT_ID, accountId);
94     }
95     if (rawContactValues.HasColumn(RawContactColumns::DISPLAY_NAME)) {
96         std::string disPlayName;
97         OHOS::NativeRdb::ValueObject value;
98         rawContactValues.GetObject(RawContactColumns::DISPLAY_NAME, value);
99         value.GetString(disPlayName);
100         searchContactValueBucket.PutString(SearchContactColumns::DISPLAY_NAME, disPlayName);
101     }
102     if (rawContactValues.HasColumn(RawContactColumns::PHONETIC_NAME)) {
103         std::string phoneticName;
104         OHOS::NativeRdb::ValueObject value;
105         rawContactValues.GetObject(RawContactColumns::PHONETIC_NAME, value);
106         value.GetString(phoneticName);
107         searchContactValueBucket.PutString(SearchContactColumns::PHONETIC_NAME, phoneticName);
108     }
109     if (rawContactValues.HasColumn(RawContactColumns::PHOTO_FILE_ID)) {
110         int photoFileId;
111         OHOS::NativeRdb::ValueObject value;
112         rawContactValues.GetObject(RawContactColumns::PHOTO_FILE_ID, value);
113         value.GetInt(photoFileId);
114         searchContactValueBucket.PutInt(SearchContactColumns::PHOTO_FILE_ID, photoFileId);
115     }
116     if (rawContactValues.HasColumn(RawContactColumns::PHOTO_ID)) {
117         int photoId;
118         OHOS::NativeRdb::ValueObject value;
119         rawContactValues.GetObject(RawContactColumns::PHOTO_ID, value);
120         value.GetInt(photoId);
121         searchContactValueBucket.PutInt(SearchContactColumns::PHOTO_ID, photoId);
122     }
123     return searchContactValueBucket;
124 }
125 } // namespace Contacts
126 } // namespace OHOS