• 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_type.h"
17 
18 #include "common.h"
19 #include "contacts_columns.h"
20 #include "contacts_database.h"
21 #include "hilog_wrapper.h"
22 
23 namespace OHOS {
24 namespace Contacts {
ContactsType(void)25 ContactsType::ContactsType(void)
26 {
27 }
28 
~ContactsType()29 ContactsType::~ContactsType()
30 {
31 }
32 
Insert(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,std::string typeValue,int typeId)33 int64_t ContactsType::Insert(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore, std::string typeValue, int typeId)
34 {
35     std::shared_ptr<OHOS::NativeRdb::RdbStore> &store_ = rdbStore;
36     OHOS::NativeRdb::ValuesBucket values;
37     values.PutString(ContentTypeColumns::CONTENT_TYPE, typeValue);
38     if (typeId != RDB_OBJECT_EMPTY) {
39         values.PutInt(ContentTypeColumns::ID, typeId);
40     }
41     if (store_ == nullptr) {
42         HILOG_ERROR("ContactsType insert store_ is nullptr");
43         return RDB_OBJECT_EMPTY;
44     }
45     int64_t outRowId = RDB_EXECUTE_FAIL;
46     int ret = store_->Insert(outRowId, ContactTableName::CONTACT_TYPE, values);
47     if (ret != OHOS::NativeRdb::E_OK) {
48         HILOG_ERROR("ContactsType insert ret :%{public}d", ret);
49     }
50     return outRowId;
51 }
52 
PrepopulateCommonTypes(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore)53 void ContactsType::PrepopulateCommonTypes(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore)
54 {
55     std::vector<std::string> vectorType;
56     vectorType.push_back(ContentTypeData::EMAIL);
57     vectorType.push_back(ContentTypeData::IM);
58     vectorType.push_back(ContentTypeData::NICKNAME);
59     vectorType.push_back(ContentTypeData::ORGANIZATION);
60     vectorType.push_back(ContentTypeData::PHONE);
61     vectorType.push_back(ContentTypeData::NAME);
62     vectorType.push_back(ContentTypeData::ADDRESS);
63     vectorType.push_back(ContentTypeData::PHOTO);
64     vectorType.push_back(ContentTypeData::GROUP_MEMBERSHIP);
65     vectorType.push_back(ContentTypeData::NOTE);
66     vectorType.push_back(ContentTypeData::CONTACT_EVENT);
67     vectorType.push_back(ContentTypeData::WEBSITE);
68     vectorType.push_back(ContentTypeData::RELATION);
69     vectorType.push_back(ContentTypeData::CONTACT_MISC);
70     vectorType.push_back(ContentTypeData::HICALL_DEVICE);
71     vectorType.push_back(ContentTypeData::CAMCARD);
72     vectorType.push_back(ContentTypeData::SIP_ADDRESS);
73     int size = (int)vectorType.size();
74     for (int i = 0; i < size; i++) {
75         std::string typeValue = vectorType[i];
76         int error = LookupTypeId(rdbStore, typeValue);
77         if (error == RDB_EXECUTE_FAIL) {
78             int typeId = i + 1;
79             Insert(rdbStore, typeValue, typeId);
80         }
81     }
82 }
83 
LookupTypeId(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,std::string typeValue)84 int ContactsType::LookupTypeId(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore, std::string typeValue)
85 {
86     std::shared_ptr<OHOS::NativeRdb::RdbStore> &store_ = rdbStore;
87     std::string sqlBuilder = "SELECT * FROM ";
88     sqlBuilder.append(ContactTableName::CONTACT_TYPE)
89         .append(" WHERE ")
90         .append(ContentTypeColumns::CONTENT_TYPE)
91         .append(" = ? ");
92     std::vector<std::string> selectionArgs;
93     selectionArgs.push_back(typeValue);
94     auto resultSet = store_->QuerySql(sqlBuilder, selectionArgs);
95     int ret = resultSet->GoToFirstRow();
96     if (ret != OHOS::NativeRdb::E_OK) {
97         return RDB_EXECUTE_FAIL;
98     }
99     int columnIndex = RDB_EXECUTE_FAIL;
100     resultSet->GetColumnIndex(ContactPublicColumns::ID, columnIndex);
101     int typeId = RDB_EXECUTE_FAIL;
102     resultSet->GetInt(columnIndex, typeId);
103     resultSet->Close();
104     return typeId;
105 }
106 
GetTypeText(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore,int typeId)107 std::string ContactsType::GetTypeText(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore, int typeId)
108 {
109     std::shared_ptr<OHOS::NativeRdb::RdbStore> &store_ = rdbStore;
110     std::string sqlBuilder = "SELECT * FROM ";
111     sqlBuilder.append(ContactTableName::CONTACT_TYPE)
112         .append(" WHERE ")
113         .append(ContactPublicColumns::ID)
114         .append(" = ?");
115     std::vector<std::string> selectionArgs;
116     selectionArgs.push_back(std::to_string(typeId));
117     auto resultSet = store_->QuerySql(sqlBuilder, selectionArgs);
118     int ret = resultSet->GoToFirstRow();
119     if (ret != OHOS::NativeRdb::E_OK) {
120         resultSet->Close();
121         HILOG_ERROR("ContactsType lookupTypeText ret :%{public}d", ret);
122     }
123     std::string TypeText;
124     int columnIndex = RDB_EXECUTE_FAIL;
125     resultSet->GetColumnIndex(ContentTypeColumns::CONTENT_TYPE, columnIndex);
126     resultSet->GetString(columnIndex, TypeText);
127     resultSet->Close();
128     return TypeText;
129 }
130 } // namespace Contacts
131 } // namespace OHOS