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 "raw_contacts.h"
17
18 #include "common.h"
19 #include "contacts_columns.h"
20 #include "hilog_wrapper.h"
21
22 namespace OHOS {
23 namespace Contacts {
RawContacts(void)24 RawContacts::RawContacts(void)
25 {
26 }
27
~RawContacts()28 RawContacts::~RawContacts()
29 {
30 }
31
32 /**
33 * @brief Insert data to the raw_contact table
34 *
35 * @param rdbStore Insert operation based on rdbStore
36 * @param rawContactValues Raw contact values to be inserted
37 *
38 * @return Insert database results code
39 */
InsertRawContact(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,int64_t & outRawContactId,OHOS::NativeRdb::ValuesBucket rawContactValues)40 int RawContacts::InsertRawContact(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore, int64_t &outRawContactId,
41 OHOS::NativeRdb::ValuesBucket rawContactValues)
42 {
43 int rowRet = rdbStore->Insert(outRawContactId, ContactTableName::RAW_CONTACT, rawContactValues);
44 if (rowRet != OHOS::NativeRdb::E_OK) {
45 HILOG_ERROR("RawContacts InsertRawContact fail:%{public}d", rowRet);
46 }
47 return rowRet;
48 }
49
50 /**
51 * @brief Update data in the raw_contact table
52 *
53 * @param rdbStore Update operation based on rdbStore
54 * @param upRawContactValues Pass in parameter upRawContactValues
55 * @param whereClause Clauses for update operation
56 * @param whereArgs Conditions for update operation
57 *
58 * @return Update database results code
59 */
UpdateRawContact(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,OHOS::NativeRdb::ValuesBucket upRawContactValues,std::string whereClause,std::vector<std::string> whereArgs)60 int RawContacts::UpdateRawContact(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,
61 OHOS::NativeRdb::ValuesBucket upRawContactValues, std::string whereClause, std::vector<std::string> whereArgs)
62 {
63 int changedRows = OHOS::NativeRdb::E_OK;
64 int ret = rdbStore->Update(changedRows, ContactTableName::RAW_CONTACT, upRawContactValues, whereClause, whereArgs);
65 if (ret != OHOS::NativeRdb::E_OK) {
66 HILOG_ERROR("RawContacts UpdateRawContact fail:%{public}d", ret);
67 }
68 return ret;
69 }
70
UpdateRawContactById(int & rawContactId,std::string type,std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,OHOS::NativeRdb::ValuesBucket rawContactValues)71 int RawContacts::UpdateRawContactById(int &rawContactId, std::string type,
72 std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore, OHOS::NativeRdb::ValuesBucket rawContactValues)
73 {
74 std::string upWhereClause;
75 upWhereClause.append(ContactPublicColumns::ID).append(" = ?");
76 std::vector<std::string> upWhereArgs;
77 upWhereArgs.push_back(std::to_string(rawContactId));
78 int changedRows = OHOS::NativeRdb::E_OK;
79 int ret =
80 rdbStore->Update(changedRows, ContactTableName::RAW_CONTACT, rawContactValues, upWhereClause, upWhereArgs);
81 if (ret != OHOS::NativeRdb::E_OK) {
82 HILOG_ERROR("RawContacts UpdateRawContactById fail:%{public}d", ret);
83 }
84 return ret;
85 }
86
GetDeleteContactIdByAccountId(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,int accountId)87 int RawContacts::GetDeleteContactIdByAccountId(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore, int accountId)
88 {
89 std::shared_ptr<OHOS::NativeRdb::RdbStore> &store_ = rdbStore;
90 if (store_ == nullptr) {
91 HILOG_ERROR("RawContacts GetDeleteContactIdByAccountId store_ is nullptr or accountId illegal");
92 return RDB_OBJECT_EMPTY;
93 }
94 if (accountId < ID_EMPTY) {
95 HILOG_ERROR("RawContacts GetDeleteContactIdByAccountId accountId illegal");
96 return OPERATION_ERROR;
97 }
98 std::vector<std::string> selectArgs;
99 selectArgs.push_back(std::to_string(accountId));
100 selectArgs.push_back(std::to_string(accountId));
101 std::string sql = "";
102 sql.append("SELECT ")
103 .append(RawContactColumns::CONTACT_ID)
104 .append(" FROM ")
105 .append(ContactTableName::RAW_CONTACT)
106 .append(" WHERE ")
107 .append(RawContactColumns::ACCOUNT_ID)
108 .append(" = ? AND ")
109 .append(RawContactColumns::CONTACT_ID)
110 .append(" NOT NULL AND ")
111 .append(RawContactColumns::CONTACT_ID)
112 .append(" NOT IN (")
113 .append(" SELECT ")
114 .append(RawContactColumns::CONTACT_ID)
115 .append(" FROM ")
116 .append(ContactTableName::RAW_CONTACT)
117 .append(" WHERE ")
118 .append(RawContactColumns::ACCOUNT_ID)
119 .append(" != ?")
120 .append(" AND ")
121 .append(RawContactColumns::CONTACT_ID)
122 .append(" NOT NULL )");
123 auto rawResult = store_->QuerySql(sql, selectArgs);
124 int resultSetNum = rawResult->GoToFirstRow();
125 int currConcactIdValue = 0;
126 while (resultSetNum == OHOS::NativeRdb::E_OK) {
127 int currValueIndex = 0;
128 rawResult->GetColumnIndex(RawContactColumns::CONTACT_ID, currValueIndex);
129 rawResult->GetInt(currValueIndex, currConcactIdValue);
130 if (currConcactIdValue > 0) {
131 break;
132 }
133 resultSetNum = rawResult->GoToNextRow();
134 }
135 rawResult->Close();
136 return currConcactIdValue;
137 }
138
GetDeleteRawContactIdByAccountId(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,int accountId)139 int RawContacts::GetDeleteRawContactIdByAccountId(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore, int accountId)
140 {
141 std::shared_ptr<OHOS::NativeRdb::RdbStore> &store_ = rdbStore;
142 if (store_ == nullptr) {
143 HILOG_ERROR("RawContacts GetDeleteRawContactIdByAccountId store_ is nullptr");
144 return RDB_OBJECT_EMPTY;
145 }
146 if (accountId < ID_EMPTY) {
147 HILOG_ERROR("RawContacts GetDeleteContactIdByAccountId accountId illegal");
148 return OPERATION_ERROR;
149 }
150 std::vector<std::string> selectArgs;
151 selectArgs.push_back(std::to_string(accountId));
152 std::string sql = "";
153 sql.append("SELECT ")
154 .append(RawContactColumns::ID)
155 .append(" FROM ")
156 .append(ContactTableName::RAW_CONTACT)
157 .append(" WHERE ")
158 .append(RawContactColumns::ACCOUNT_ID)
159 .append(" = ?")
160 .append(" AND ")
161 .append(RawContactColumns::ACCOUNT_ID)
162 .append(" NOT NULL");
163 auto rawResult = store_->QuerySql(sql, selectArgs);
164 int resultSetNum = rawResult->GoToFirstRow();
165 int currConcactIdValue = 0;
166 while (resultSetNum == OHOS::NativeRdb::E_OK) {
167 rawResult->GetInt(0, currConcactIdValue);
168 if (currConcactIdValue > 0) {
169 break;
170 }
171 resultSetNum = rawResult->GoToNextRow();
172 }
173 rawResult->Close();
174 return currConcactIdValue;
175 }
176
DeleteRawcontactByRawId(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,int needDeleteRawContactId)177 int RawContacts::DeleteRawcontactByRawId(
178 std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore, int needDeleteRawContactId)
179 {
180 std::shared_ptr<OHOS::NativeRdb::RdbStore> &store_ = rdbStore;
181 if (store_ == nullptr) {
182 HILOG_ERROR("RawContacts DeleteRawcontactByRawId store_ is nullptr");
183 return RDB_OBJECT_EMPTY;
184 }
185 if (needDeleteRawContactId < ID_EMPTY) {
186 HILOG_ERROR("RawContacts DeleteRawcontactByRawId needDeleteRawContactId illegal");
187 return OPERATION_ERROR;
188 }
189 int rowId = OHOS::NativeRdb::E_OK;
190 std::vector<std::string> whereArgs;
191 whereArgs.push_back(std::to_string(needDeleteRawContactId));
192 std::string whereCase;
193 whereCase.append(RawContactColumns::ID).append(" = ?");
194 int delRawContact = store_->Delete(rowId, ContactTableName::RAW_CONTACT, whereCase, whereArgs);
195 HILOG_INFO("DeleteRawcontactByRawId : status is %{public}d", delRawContact);
196 return delRawContact;
197 }
198 } // namespace Contacts
199 } // namespace OHOS