1 /*
2  * Copyright (C) 2024 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 #define MLOG_TAG "RingtoneProps"
16 
17 #include "ringtone_props.h"
18 
19 #include "rdb_store.h"
20 #include "result_set_utils.h"
21 #include "ringtone_db_const.h"
22 #include "ringtone_errno.h"
23 #include "ringtone_log.h"
24 
25 namespace OHOS::Media {
26 using namespace std;
27 using namespace OHOS;
28 const std::string CREATE_PROPERTIES_TABLE = "CREATE TABLE IF NOT EXISTS " + PROPERTIES_TABLE + "(" +
29     RINGTONEPROPS_COLUMN_ID                       + " INTEGER  PRIMARY KEY AUTOINCREMENT, " +
30     RINGTONEPROPS_COLUMN_NAME                     + " TEXT NOT NULL UNIQUE, " +
31     RINGTONEPROPS_COLUMN_VALUE                    + " TEXT DEFAULT NULL" + ")";
32 
RingtoneProps(shared_ptr<NativeRdb::RdbStore> rdb)33 RingtoneProps::RingtoneProps(shared_ptr<NativeRdb::RdbStore> rdb) : store_(rdb)
34 {
35 }
36 
Init()37 int32_t RingtoneProps::Init()
38 {
39     if (store_ == nullptr) {
40         RINGTONE_ERR_LOG("store_ is nullptr");
41         return E_HAS_DB_ERROR;
42     }
43     if (store_->ExecuteSql(CREATE_PROPERTIES_TABLE) != NativeRdb::E_OK) {
44         RINGTONE_ERR_LOG("Failed to execute sql");
45         return E_HAS_DB_ERROR;
46     }
47     return E_OK;
48 }
49 
GetPropFromResultSet(shared_ptr<NativeRdb::ResultSet> resultSet,string & propVal)50 int32_t RingtoneProps::GetPropFromResultSet(shared_ptr<NativeRdb::ResultSet> resultSet, string &propVal)
51 {
52     if (resultSet == nullptr) {
53         RINGTONE_INFO_LOG("invalid argument");
54         return E_INVALID_ARGUMENTS;
55     }
56     auto ret = resultSet->GoToFirstRow();
57     if (ret != NativeRdb::E_OK) {
58         RINGTONE_ERR_LOG("go to first row err");
59         return E_HAS_DB_ERROR;
60     }
61     propVal = std::get<string>(ResultSetUtils::GetValFromColumn<shared_ptr<NativeRdb::ResultSet>>(
62         RINGTONEPROPS_COLUMN_VALUE, resultSet, RingtoneResultSetDataType::DATA_TYPE_STRING));
63 
64     return E_OK;
65 }
66 
GetProp(const string & propName,const string & defaultVal)67 string RingtoneProps::GetProp(const string &propName, const string &defaultVal)
68 {
69     if (store_ == nullptr) {
70         RINGTONE_ERR_LOG("store_ is nullptr");
71         return defaultVal;
72     }
73 
74     const string queryPropsSql = "SELECT " + RINGTONEPROPS_COLUMN_VALUE + " FROM " + PROPERTIES_TABLE + " WHERE " +
75         RINGTONEPROPS_COLUMN_NAME + " = \"" + propName + "\"";
76 
77     auto resultSet = store_->QuerySql(queryPropsSql);
78     if (resultSet == nullptr) {
79         RINGTONE_INFO_LOG("resultset is null");
80         return defaultVal;
81     }
82 
83     int32_t count = 0;
84     int32_t nativeRdbRet = resultSet->GetRowCount(count);
85     if (count <= 0 || nativeRdbRet != NativeRdb::E_OK) {
86         RINGTONE_INFO_LOG("resultset row count is zero, count=%{public}d", count);
87         return defaultVal;
88     }
89 
90     string retVal = {};
91     int32_t ret = GetPropFromResultSet(resultSet, retVal);
92     if (ret != E_OK) {
93         RINGTONE_INFO_LOG("get pro err");
94         return defaultVal;
95     }
96 
97     return retVal;
98 }
99 
SetProp(const string & propName,const string & propVal)100 bool RingtoneProps::SetProp(const string &propName, const string &propVal)
101 {
102     if (store_ == nullptr) {
103         RINGTONE_ERR_LOG("store_ is nullptr");
104         return false;
105     }
106 
107     const string queryPropsSql = "SELECT " + RINGTONEPROPS_COLUMN_VALUE + " FROM " + PROPERTIES_TABLE + " WHERE " +
108         RINGTONEPROPS_COLUMN_NAME + " = \"" + propName + "\"";
109 
110     const string insertPropsSql = "INSERT INTO " + PROPERTIES_TABLE + "(" +  RINGTONEPROPS_COLUMN_NAME + ", " +
111         RINGTONEPROPS_COLUMN_VALUE + ")" + " VALUES (\"" +  propName + "\", \"" + propVal + "\"" + ")";
112 
113     const string updatePropsSql = "UPDATE RingtoneProps SET " + RINGTONEPROPS_COLUMN_VALUE + " = \"" + propVal + "\"" +
114         " WHERE " + RINGTONEPROPS_COLUMN_NAME + " = \"" + propName + "\"";
115 
116     auto resultSet = store_->QuerySql(queryPropsSql);
117 
118     int32_t ret = NativeRdb::E_OK;
119     if (resultSet != nullptr) {
120         int32_t count = 0;
121         auto internalRet = resultSet->GetRowCount(count);
122         if (count <= 0 || internalRet != NativeRdb::E_OK) {
123             ret = store_->ExecuteSql(insertPropsSql);
124         } else {
125             ret = store_->ExecuteSql(updatePropsSql);
126         }
127     } else {
128         ret = store_->ExecuteSql(insertPropsSql);
129     }
130 
131     return (ret != NativeRdb::E_OK) ? false : true;
132 }
133 } // namespace OHOS::Media
134