• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "trigger_db_helper.h"
17 
18 #include <string>
19 
20 #include "intell_voice_log.h"
21 #include "rdb_errno.h"
22 #include "rdb_helper.h"
23 #include "rdb_open_callback.h"
24 
25 #define LOG_TAG "TriggerDbhelper"
26 
27 using namespace OHOS::NativeRdb;
28 
29 namespace OHOS {
30 namespace IntellVoiceTrigger {
31 class TriggerModelOpenCallback : public RdbOpenCallback {
32 public:
33     int OnCreate(RdbStore &rdbStore) override;
34     int OnUpgrade(RdbStore &rdbStore, int oldVersion, int newVersion) override;
35 };
36 
OnCreate(RdbStore & rdbStore)37 int TriggerModelOpenCallback::OnCreate(RdbStore &rdbStore)
38 {
39     INTELL_VOICE_LOG_DEBUG("OnCreate");
40     std::string CREATE_TABLE_Trigger =
41         std::string("CREATE TABLE IF NOT EXISTS trigger ") +
42         std::string("(model_uuid INTEGER PRIMARY KEY, vendor_uuid INTEGER, data BLOB, model_version INTEGER)");
43 
44     return rdbStore.ExecuteSql(CREATE_TABLE_Trigger);
45 }
46 
OnUpgrade(RdbStore & rdbStore,int oldVersion,int newVersion)47 int TriggerModelOpenCallback::OnUpgrade(RdbStore &rdbStore, int oldVersion, int newVersion)
48 {
49     return E_OK;
50 }
51 
TriggerDbHelper()52 TriggerDbHelper::TriggerDbHelper()
53 {
54     int errCode = E_OK;
55     RdbStoreConfig config("/data/service/el1/public/database/intell_voice_service_manager/triggerModel.db");
56     TriggerModelOpenCallback helper;
57     store_ = RdbHelper::GetRdbStore(config, 1, helper, errCode);
58     if (store_ == nullptr) {
59         INTELL_VOICE_LOG_ERROR("store is nullptr");
60     }
61 }
62 
~TriggerDbHelper()63 TriggerDbHelper::~TriggerDbHelper()
64 {
65     store_ = nullptr;
66 }
67 
UpdateGenericTriggerModel(std::shared_ptr<GenericTriggerModel> model)68 bool TriggerDbHelper::UpdateGenericTriggerModel(std::shared_ptr<GenericTriggerModel> model)
69 {
70     std::lock_guard<std::mutex> lock(mutex_);
71     INTELL_VOICE_LOG_INFO("db Update");
72 
73     if (store_ == nullptr) {
74         INTELL_VOICE_LOG_ERROR("store is nullptr");
75         return false;
76     }
77 
78     if (model == nullptr) {
79         INTELL_VOICE_LOG_ERROR("model is nullptr");
80         return false;
81     }
82 
83     int64_t rowId = -1;
84     ValuesBucket values;
85     values.PutInt("model_uuid", model->GetUuid());
86     values.PutInt("vendor_uuid", model->GetVendorUuid());
87     values.PutBlob("data", model->GetData());
88     values.PutInt("model_version", model->GetVersion());
89     int ret = store_->InsertWithConflictResolution(rowId, "trigger", values, ConflictResolution::ON_CONFLICT_REPLACE);
90     if (ret != E_OK) {
91         INTELL_VOICE_LOG_ERROR("update generic model failed");
92         return false;
93     }
94     return true;
95 }
96 
GetVendorUuid(std::shared_ptr<AbsSharedResultSet> & set,int32_t & vendorUuid) const97 bool TriggerDbHelper::GetVendorUuid(std::shared_ptr<AbsSharedResultSet> &set, int32_t &vendorUuid) const
98 {
99     int columnIndex;
100     int ret = set->GetColumnIndex("vendor_uuid", columnIndex);
101     if (ret != E_OK) {
102         INTELL_VOICE_LOG_ERROR("failed to get model uuid column index");
103         return false;
104     }
105 
106     ret = set->GetInt(columnIndex, vendorUuid);
107     if (ret != E_OK) {
108         INTELL_VOICE_LOG_ERROR("failed to get model uuid");
109         return false;
110     }
111     return true;
112 }
113 
GetBlob(std::shared_ptr<AbsSharedResultSet> & set,std::vector<uint8_t> & data) const114 bool TriggerDbHelper::GetBlob(std::shared_ptr<AbsSharedResultSet> &set, std::vector<uint8_t> &data) const
115 {
116     int columnIndex;
117     int ret = set->GetColumnIndex("data", columnIndex);
118     if (ret != E_OK) {
119         INTELL_VOICE_LOG_ERROR("failed to get data column index");
120         return false;
121     }
122 
123     ret = set->GetBlob(columnIndex, data);
124     if (ret != E_OK) {
125         INTELL_VOICE_LOG_ERROR("failed to get data");
126         return false;
127     }
128     return true;
129 }
130 
GetModelVersion(std::shared_ptr<AbsSharedResultSet> & set,int32_t & version) const131 bool TriggerDbHelper::GetModelVersion(std::shared_ptr<AbsSharedResultSet> &set, int32_t &version) const
132 {
133     int columnIndex;
134     int ret = set->GetColumnIndex("model_version", columnIndex);
135     if (ret != E_OK) {
136         INTELL_VOICE_LOG_ERROR("failed to get model uuid column index");
137         return false;
138     }
139 
140     ret = set->GetInt(columnIndex, version);
141     if (ret != E_OK) {
142         INTELL_VOICE_LOG_ERROR("failed to get model uuid");
143         return false;
144     }
145     return true;
146 }
147 
GetGenericTriggerModel(const int32_t modelUuid)148 std::shared_ptr<GenericTriggerModel> TriggerDbHelper::GetGenericTriggerModel(const int32_t modelUuid)
149 {
150     std::lock_guard<std::mutex> lock(mutex_);
151     INTELL_VOICE_LOG_INFO("db get generic model");
152     if (store_ == nullptr) {
153         INTELL_VOICE_LOG_ERROR("store is nullptr");
154         return nullptr;
155     }
156 
157     std::shared_ptr<AbsSharedResultSet> set = store_->QuerySql(
158         "SELECT * FROM trigger WHERE model_uuid = ?", std::vector<std::string> {std::to_string(modelUuid)});
159     if (set == nullptr) {
160         INTELL_VOICE_LOG_ERROR("set is nullptr");
161         return nullptr;
162     }
163 
164     set->GoToFirstRow();
165 
166     int32_t vendorUuid;
167     if (!GetVendorUuid(set, vendorUuid)) {
168         INTELL_VOICE_LOG_ERROR("failed to get vendor uuid");
169         return nullptr;
170     }
171 
172     std::vector<uint8_t> data;
173     if (!GetBlob(set, data)) {
174         INTELL_VOICE_LOG_ERROR("failed to get data");
175         return nullptr;
176     }
177 
178     int32_t modelVersion;
179     if (!GetModelVersion(set, modelVersion)) {
180         INTELL_VOICE_LOG_ERROR("failed to get model version");
181         return nullptr;
182     }
183 
184     std::shared_ptr<GenericTriggerModel> model = std::make_shared<GenericTriggerModel>(modelUuid, modelVersion);
185     if (model == nullptr) {
186         INTELL_VOICE_LOG_ERROR("failed to alloc model");
187         return nullptr;
188     }
189     model->SetData(data);
190     return model;
191 }
192 
DeleteGenericTriggerModel(const int32_t modelUuid)193 void TriggerDbHelper::DeleteGenericTriggerModel(const int32_t modelUuid)
194 {
195     std::lock_guard<std::mutex> lock(mutex_);
196     INTELL_VOICE_LOG_INFO("db Delete");
197     if (store_ == nullptr) {
198         INTELL_VOICE_LOG_ERROR("store is nullptr");
199         return;
200     }
201     int deletedRows;
202     store_->Delete(deletedRows, "trigger", "model_uuid = ?", std::vector<std::string> {std::to_string(modelUuid)});
203 }
204 }  // namespace IntellVoiceTrigger
205 }  // namespace OHOS
206