• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "db_upgrade_utils.h"
16 
17 #include <string>
18 #include <vector>
19 
20 #include "rdb_store.h"
21 #include "media_log.h"
22 
23 namespace OHOS::Media {
24 namespace DataTransfer {
25 /**
26  * @brief Check table exists or not.
27  */
IsTableExists(NativeRdb::RdbStore & store,const std::string & tableName)28 bool DbUpgradeUtils::IsTableExists(NativeRdb::RdbStore &store, const std::string &tableName)
29 {
30     std::string querySql = this->SQL_SQLITE_MASTER_QUERY;
31     std::vector<NativeRdb::ValueObject> bindArgs = {tableName};
32     auto resultSet = store.QuerySql(querySql, bindArgs);
33     if (resultSet == nullptr) {
34         MEDIA_ERR_LOG("Query resultSql is null.");
35         return false;
36     }
37     int rowCount = 0;
38     bool isExists = !resultSet->GetRowCount(rowCount) && rowCount > 0;
39     MEDIA_INFO_LOG("Media_Restore: tableName=%{public}s, isExists:%{public}d", tableName.c_str(), isExists);
40     return isExists;
41 }
42 
43 /**
44  * @brief Check table column exists or not.
45  */
IsColumnExists(NativeRdb::RdbStore & store,const std::string & tableName,const std::string & columnName)46 bool DbUpgradeUtils::IsColumnExists(
47     NativeRdb::RdbStore &store, const std::string &tableName, const std::string &columnName)
48 {
49     std::string querySql = this->SQL_PRAGMA_TABLE_INFO_QUERY;
50     std::vector<NativeRdb::ValueObject> bindArgs = {tableName, columnName};
51     auto resultSet = store.QuerySql(querySql, bindArgs);
52     if (resultSet == nullptr) {
53         MEDIA_ERR_LOG("Query resultSql is null.");
54         return false;
55     }
56     int rowCount = 0;
57     bool isExists = !resultSet->GetRowCount(rowCount) && rowCount > 0;
58     MEDIA_INFO_LOG("Media_Restore: tableName=%{public}s, columnName=%{public}s, isExists:%{public}d",
59         tableName.c_str(),
60         columnName.c_str(),
61         isExists);
62     return isExists;
63 }
64 }  // namespace DataTransfer
65 }  // namespace OHOS::Media