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_account.h"
17
18 #include "common.h"
19 #include "contacts_columns.h"
20 #include "hilog_wrapper.h"
21
22 namespace OHOS {
23 namespace Contacts {
24 std::shared_ptr<ContactsAccount> ContactsAccount::instance_ = nullptr;
25
GetInstance()26 std::shared_ptr<ContactsAccount> ContactsAccount::GetInstance()
27 {
28 if (instance_ == nullptr) {
29 instance_.reset(new ContactsAccount());
30 }
31 return instance_;
32 }
33
ContactsAccount(void)34 ContactsAccount::ContactsAccount(void)
35 {
36 }
37
~ContactsAccount()38 ContactsAccount::~ContactsAccount()
39 {
40 }
41
42 /**
43 * @brief Insert ContactsAccount into rdbStore
44 *
45 * @param rdbStore Insert operation based on rdbStore
46 * @param accountName Pass in parameter account name
47 * @param accountType Pass in parameter account type
48 *
49 * @return Insert database results code
50 */
Insert(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,std::string accountName,std::string accountType)51 int64_t ContactsAccount::Insert(
52 std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore, std::string accountName, std::string accountType)
53 {
54 std::shared_ptr<OHOS::NativeRdb::RdbStore> &store_ = rdbStore;
55 OHOS::NativeRdb::ValuesBucket values;
56 values.PutString(AccountColumns::ACCOUNT_NAME, accountName);
57 values.PutString(AccountColumns::ACCOUNT_TYPE, accountType);
58 int64_t outRowId = OHOS::NativeRdb::E_OK;
59 if (store_ == nullptr) {
60 HILOG_ERROR("ContactsAccount insert store_ is nullptr");
61 return RDB_OBJECT_EMPTY;
62 }
63 int ret = store_->Insert(outRowId, ContactTableName::ACCOUNT, values);
64 HILOG_INFO(" ContactsAccount insert ret :%{public}d", ret);
65 return outRowId;
66 };
67
PrepopulateCommonAccountTypes(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore)68 void ContactsAccount::PrepopulateCommonAccountTypes(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore)
69 {
70 int typeResult = LookupAccountTypeId(rdbStore, AccountData::ACCOUNT_NAME, AccountData::ACCOUNT_TYPE);
71 if (typeResult == RDB_EXECUTE_FAIL) {
72 Insert(rdbStore, AccountData::ACCOUNT_NAME, AccountData::ACCOUNT_TYPE);
73 } else {
74 HILOG_INFO("ContactsAccount account is exist");
75 }
76 }
77
LookupAccountTypeId(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,std::string accountName,std::string accountType)78 int ContactsAccount::LookupAccountTypeId(
79 std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore, std::string accountName, std::string accountType)
80 {
81 std::shared_ptr<OHOS::NativeRdb::RdbStore> &store_ = rdbStore;
82 std::string sqlBuilder = "SELECT * FROM ";
83 sqlBuilder.append(ContactTableName::ACCOUNT)
84 .append(" WHERE ")
85 .append(AccountColumns::ACCOUNT_NAME)
86 .append(" = ")
87 .append("'")
88 .append(accountName)
89 .append("'")
90 .append(" AND ")
91 .append(AccountColumns::ACCOUNT_TYPE)
92 .append(" = ")
93 .append("'")
94 .append(accountType)
95 .append("'");
96 std::vector<std::string> selectionArgs;
97 std::unique_ptr<OHOS::NativeRdb::AbsSharedResultSet> resultSet = store_->QuerySql(sqlBuilder, selectionArgs);
98 int ret = resultSet->GoToFirstRow();
99 if (ret != OHOS::NativeRdb::E_OK) {
100 resultSet->Close();
101 return RDB_EXECUTE_FAIL;
102 }
103 int columnIndex = 0;
104 resultSet->GetColumnIndex(ContactPublicColumns::ID, columnIndex);
105 int accountId = 0;
106 resultSet->GetInt(columnIndex, accountId);
107 resultSet->Close();
108 return accountId;
109 }
110
GetAccountFromLocal(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore)111 std::vector<AccountDataCollection> ContactsAccount::GetAccountFromLocal(
112 std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore)
113 {
114 std::shared_ptr<OHOS::NativeRdb::RdbStore> &store_ = rdbStore;
115 std::vector<AccountDataCollection> values;
116 if (store_ == nullptr) {
117 HILOG_ERROR("ContactsAccount GetAccountFromLocal store_ is nullptr");
118 return values;
119 }
120 std::string buildQuery = "";
121 buildQuery.append("select ")
122 .append(AccountColumns::ACCOUNT_NAME)
123 .append(",")
124 .append(AccountColumns::ACCOUNT_TYPE)
125 .append(",")
126 .append(AccountColumns::DATA_INFO)
127 .append(" from ")
128 .append(ContactTableName::ACCOUNT);
129 std::vector<std::string> selectArgs;
130 std::unique_ptr<OHOS::NativeRdb::AbsSharedResultSet> result = store_->QuerySql(buildQuery, selectArgs);
131 int resultSetNum = result->GoToFirstRow();
132 while (resultSetNum == OHOS::NativeRdb::E_OK) {
133 std::string accountName;
134 std::string accountType;
135 std::string accountCollection;
136 int accountNameIndex = 0;
137 int accountTypeIndex = 0;
138 int accountCollectionIndex = 0;
139 result->GetColumnIndex(AccountColumns::ACCOUNT_NAME, accountNameIndex);
140 result->GetColumnIndex(AccountColumns::ACCOUNT_TYPE, accountTypeIndex);
141 result->GetColumnIndex(AccountColumns::DATA_INFO, accountCollectionIndex);
142 result->GetString(accountNameIndex, accountName);
143 result->GetString(accountTypeIndex, accountType);
144 result->GetString(accountCollectionIndex, accountCollection);
145 AccountDataCollection collection = AccountDataCollection(accountName, accountType, accountCollection);
146 values.push_back(collection);
147 resultSetNum = result->GoToNextRow();
148 }
149 result->Close();
150 return values;
151 }
152
GetNotExistAccount(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,AccountDataCollection collection)153 int ContactsAccount::GetNotExistAccount(
154 std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore, AccountDataCollection collection)
155 {
156 std::shared_ptr<OHOS::NativeRdb::RdbStore> &store_ = rdbStore;
157 if (store_ == nullptr) {
158 HILOG_ERROR("ContactsAccount GetNotExistAccount store_ is nullptr");
159 return RDB_OBJECT_EMPTY;
160 }
161 if (collection.GetcAccountName() != "" || collection.GetcAccountType() != "") {
162 std::vector<std::string> selectArgs;
163 std::string buildSql = "";
164 buildSql.append("select ")
165 .append(AccountColumns::ID)
166 .append(" from ")
167 .append(ContactTableName::ACCOUNT)
168 .append(" where ")
169 .append(AccountColumns::ACCOUNT_NAME)
170 .append(" = ? and ")
171 .append(AccountColumns::ACCOUNT_TYPE)
172 .append(" = ? and (")
173 .append(AccountColumns::DATA_INFO)
174 .append(" IS NULL or ")
175 .append(AccountColumns::DATA_INFO)
176 .append(" = ?)");
177 selectArgs.push_back(collection.GetcAccountName());
178 selectArgs.push_back(collection.GetcAccountType());
179 selectArgs.push_back(collection.GetcDataCollection());
180 std::unique_ptr<OHOS::NativeRdb::AbsSharedResultSet> result = store_->QuerySql(buildSql, selectArgs);
181 int resultSetNum = result->GoToFirstRow();
182 int reValue = RDB_EXECUTE_FAIL;
183 while (resultSetNum == OHOS::NativeRdb::E_OK) {
184 int reValueIndex = 0;
185 result->GetColumnIndex(AccountColumns::ID, reValueIndex);
186 if (reValueIndex < 0) {
187 break;
188 }
189 result->GetInt(reValueIndex, reValue);
190 result->GoToNextRow();
191 break;
192 }
193 result->Close();
194 return reValue;
195 }
196 return OPERATION_ERROR;
197 }
198
DeleteAccountByAccountId(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,int accountId)199 int ContactsAccount::DeleteAccountByAccountId(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore, int accountId)
200 {
201 std::shared_ptr<OHOS::NativeRdb::RdbStore> &store_ = rdbStore;
202 if (store_ == nullptr) {
203 HILOG_ERROR("ContactsAccount DeleteAccountByAccountId store_ is nullptr");
204 return RDB_OBJECT_EMPTY;
205 }
206 if (accountId < ID_EMPTY) {
207 return OPERATION_ERROR;
208 }
209 int rowId = 0;
210 std::vector<std::string> whereArgs;
211 whereArgs.push_back(std::to_string(accountId));
212 std::string whereCase;
213 whereCase.append(AccountColumns::ID).append(" = ?");
214 int delAccount = store_->Delete(rowId, ContactTableName::ACCOUNT, whereCase, whereArgs);
215 return delAccount;
216 }
217
DeleteDataByRawId(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,int needDeleteRawContactId)218 int ContactsAccount::DeleteDataByRawId(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore, int needDeleteRawContactId)
219 {
220 std::shared_ptr<OHOS::NativeRdb::RdbStore> &store_ = rdbStore;
221 if (store_ == nullptr) {
222 HILOG_ERROR("ContactsAccount DeleteDataByRawId store_ is nullptr");
223 return RDB_OBJECT_EMPTY;
224 }
225 if (needDeleteRawContactId < ID_EMPTY) {
226 return OPERATION_ERROR;
227 }
228 int rowId = 0;
229 std::vector<std::string> whereArgs;
230 whereArgs.push_back(std::to_string(needDeleteRawContactId));
231 std::string whereCase;
232 whereCase.append(ContactDataColumns::RAW_CONTACT_ID).append(" = ?");
233 int delData = store_->Delete(rowId, ContactTableName::CONTACT_DATA, whereCase, whereArgs);
234 return delData;
235 }
236
DeleteGroupsByAccountId(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,int accountId)237 int ContactsAccount::DeleteGroupsByAccountId(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore, int accountId)
238 {
239 std::shared_ptr<OHOS::NativeRdb::RdbStore> &store_ = rdbStore;
240 if (store_ == nullptr) {
241 HILOG_ERROR("ContactsAccount DeleteGroupsByAccountId store_ is nullptr");
242 return RDB_OBJECT_EMPTY;
243 }
244 if (accountId < ID_EMPTY) {
245 return OPERATION_ERROR;
246 }
247 int rowId = 0;
248 std::vector<std::string> whereArgs;
249 whereArgs.push_back(std::to_string(accountId));
250 std::string whereCase;
251 whereCase.append(GroupsColumns::ACCOUNT_ID).append(" = ? ");
252 int delGroup = store_->Delete(rowId, ContactTableName::GROUPS, whereCase, whereArgs);
253 return delGroup;
254 }
255
StopForegin(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore)256 int ContactsAccount::StopForegin(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore)
257 {
258 std::shared_ptr<OHOS::NativeRdb::RdbStore> &store_ = rdbStore;
259 if (store_ == nullptr) {
260 HILOG_ERROR("ContactsAccount StopForegin store_ is nullptr");
261 return RDB_OBJECT_EMPTY;
262 }
263 return store_->ExecuteSql("PRAGMA foreign_keys = OFF");
264 }
265
OpenForegin(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore)266 int ContactsAccount::OpenForegin(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore)
267 {
268 std::shared_ptr<OHOS::NativeRdb::RdbStore> &store_ = rdbStore;
269 if (store_ == nullptr) {
270 HILOG_ERROR("ContactsAccount OpenForegin store_ is nullptr");
271 return RDB_OBJECT_EMPTY;
272 }
273 return store_->ExecuteSql("PRAGMA foreign_keys = ON");
274 }
275 } // namespace Contacts
276 } // namespace OHOS