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
21namespace OHOS {
22namespace Contacts {
23std::shared_ptr<VoiceMailDataBase> VoiceMailDataBase::voiceMailDataBase_ = nullptr;
24std::shared_ptr<OHOS::NativeRdb::RdbStore> VoiceMailDataBase::store_ = nullptr;
25
26VoiceMailDataBase::VoiceMailDataBase()
27{
28    store_ = CallLogDataBase::GetInstance()->store_;
29}
30
31std::shared_ptr<VoiceMailDataBase> VoiceMailDataBase::GetInstance()
32{
33    if (voiceMailDataBase_ == nullptr) {
34        voiceMailDataBase_.reset(new VoiceMailDataBase());
35        return voiceMailDataBase_;
36    }
37    return voiceMailDataBase_;
38}
39
40int64_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
56int 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
75int 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
90std::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
101int 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
114int 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
127int 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