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 "voicemail_database.h"
17
18 #include "calllog_database.h"
19 #include "predicates_convert.h"
20
21 namespace OHOS {
22 namespace Contacts {
23 std::shared_ptr<VoiceMailDataBase> VoiceMailDataBase::voiceMailDataBase_ = nullptr;
24 std::shared_ptr<OHOS::NativeRdb::RdbStore> VoiceMailDataBase::store_ = nullptr;
25
VoiceMailDataBase()26 VoiceMailDataBase::VoiceMailDataBase()
27 {
28 store_ = CallLogDataBase::GetInstance()->store_;
29 }
30
GetInstance()31 std::shared_ptr<VoiceMailDataBase> VoiceMailDataBase::GetInstance()
32 {
33 if (voiceMailDataBase_ == nullptr) {
34 voiceMailDataBase_.reset(new VoiceMailDataBase());
35 return voiceMailDataBase_;
36 }
37 return voiceMailDataBase_;
38 }
39
InsertVoiceMail(std::string tableName,OHOS::NativeRdb::ValuesBucket insertValues)40 int64_t VoiceMailDataBase::InsertVoiceMail(std::string tableName, OHOS::NativeRdb::ValuesBucket insertValues)
41 {
42 int64_t outRowId = RDB_EXECUTE_FAIL;
43 if (store_ == nullptr) {
44 HILOG_ERROR("VoiceMailDataBase Insert store_ is nullptr");
45 return RDB_OBJECT_EMPTY;
46 }
47 CallLogDataBase::GetInstance()->QueryContactsByInsertCalls(insertValues);
48 int ret = store_->Insert(outRowId, tableName, insertValues);
49 if (ret != OHOS::NativeRdb::E_OK) {
50 HILOG_ERROR("VoiceMailDataBase InsertVoiceMail ret :%{public}d", ret);
51 return RDB_EXECUTE_FAIL;
52 }
53 return outRowId;
54 }
55
UpdateVoiceMail(OHOS::NativeRdb::ValuesBucket values,OHOS::NativeRdb::RdbPredicates & rdbPredicates)56 int VoiceMailDataBase::UpdateVoiceMail(
57 OHOS::NativeRdb::ValuesBucket values, OHOS::NativeRdb::RdbPredicates &rdbPredicates)
58 {
59 int changedRows = RDB_EXECUTE_FAIL;
60 if (store_ == nullptr) {
61 HILOG_ERROR("VoiceMailDataBase Update store_ is nullptr");
62 return RDB_OBJECT_EMPTY;
63 }
64 if (values.HasColumn(CallLogColumns::PHONE_NUMBER)) {
65 CallLogDataBase::GetInstance()->QueryContactsByInsertCalls(values);
66 }
67 int ret = store_->Update(changedRows, values, rdbPredicates);
68 if (ret != OHOS::NativeRdb::E_OK) {
69 HILOG_ERROR("VoiceMailDataBase Update ret :%{public}d", ret);
70 return RDB_EXECUTE_FAIL;
71 }
72 return ret;
73 }
74
DeleteVoiceMail(OHOS::NativeRdb::RdbPredicates & rdbPredicates)75 int VoiceMailDataBase::DeleteVoiceMail(OHOS::NativeRdb::RdbPredicates &rdbPredicates)
76 {
77 int deletedRows = RDB_EXECUTE_FAIL;
78 if (store_ == nullptr) {
79 HILOG_ERROR("VoiceMailDataBase Delete store_ is nullptr");
80 return RDB_OBJECT_EMPTY;
81 }
82 int ret = store_->Delete(deletedRows, rdbPredicates);
83 if (ret != OHOS::NativeRdb::E_OK) {
84 HILOG_ERROR("VoiceMailDataBase Delete ret :%{public}d", ret);
85 return RDB_EXECUTE_FAIL;
86 }
87 return ret;
88 }
89
Query(OHOS::NativeRdb::RdbPredicates & rdbPredicates,const std::vector<std::string> columns)90 std::shared_ptr<OHOS::NativeRdb::ResultSet> VoiceMailDataBase::Query(
91 OHOS::NativeRdb::RdbPredicates &rdbPredicates, const std::vector<std::string> columns)
92 {
93 if (store_ == nullptr) {
94 HILOG_ERROR("CallLogDataBase Delete store_ is nullptr");
95 return nullptr;
96 }
97
98 return store_->Query(rdbPredicates, columns);
99 }
100
BeginTransaction()101 int VoiceMailDataBase::BeginTransaction()
102 {
103 if (store_ == nullptr) {
104 HILOG_ERROR("VoiceMailDataBase BeginTransaction store_ is nullptr");
105 return RDB_OBJECT_EMPTY;
106 }
107 int ret = store_->BeginTransaction();
108 if (ret != OHOS::NativeRdb::E_OK) {
109 HILOG_ERROR("VoiceMailDataBase BeginTransaction fail :%{public}d", ret);
110 }
111 return ret;
112 }
113
Commit()114 int VoiceMailDataBase::Commit()
115 {
116 if (store_ == nullptr) {
117 HILOG_ERROR(" VoiceMailDataBase Commit store_ is nullptr");
118 return RDB_OBJECT_EMPTY;
119 }
120 int ret = store_->Commit();
121 if (ret != OHOS::NativeRdb::E_OK) {
122 HILOG_ERROR(" VoiceMailDataBase Commit fail :%{public}d", ret);
123 }
124 return ret;
125 }
126
RollBack()127 int VoiceMailDataBase::RollBack()
128 {
129 if (store_ == nullptr) {
130 HILOG_ERROR(" VoiceMailDataBase RollBack store_ is nullptr");
131 return RDB_OBJECT_EMPTY;
132 }
133 int ret = store_->RollBack();
134 if (ret != OHOS::NativeRdb::E_OK) {
135 HILOG_ERROR(" VoiceMailDataBase RollBack fail :%{public}d", ret);
136 }
137 return ret;
138 }
139 } // namespace Contacts
140 } // namespace OHOS