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 #ifndef OHOS_MEDIA_DATATRANSFER_DB_UPGRADE_UTILS_H
16 #define OHOS_MEDIA_DATATRANSFER_DB_UPGRADE_UTILS_H
17 
18 #include <string>
19 
20 #include "rdb_store.h"
21 
22 namespace OHOS::Media {
23 namespace DataTransfer {
24 class DbUpgradeUtils {
25 public:
26     bool IsTableExists(NativeRdb::RdbStore &store, const std::string &tableName);
27     bool IsColumnExists(NativeRdb::RdbStore &store, const std::string &tableName, const std::string &columnName);
28     int32_t DropAllTriggers(NativeRdb::RdbStore &store, const std::string &tableName);
29     int32_t DropAllUniqueIndex(NativeRdb::RdbStore &store, const std::string &tableName);
30 
31 private:
32     std::vector<std::string> GetAllTriggers(NativeRdb::RdbStore &store, const std::string &tableName);
33     std::vector<std::string> GetAllUniqueIndex(NativeRdb::RdbStore &store, const std::string &tableName);
34 
35 private:
36     const std::string SQL_PRAGMA_TABLE_INFO_QUERY = "\
37         SELECT \
38             name, \
39             type, \
40             [notnull], \
41             dflt_value, \
42             pk \
43         FROM \
44             pragma_table_info(?) \
45         WHERE name = ?;";
46     const std::string SQL_SQLITE_MASTER_QUERY = "\
47         SELECT \
48             type, \
49             name, \
50             tbl_name \
51         FROM sqlite_master \
52         WHERE type='table' AND \
53             tbl_name = ?;";
54     const std::string SQL_SQLITE_MASTER_QUERY_TRIGGER = "\
55         SELECT \
56             type, \
57             name, \
58             tbl_name \
59         FROM sqlite_master \
60         WHERE \
61             type='trigger' AND \
62             tbl_name = ?;";
63     const std::string SQL_SQLITE_MASTER_QUERY_UNIQUE_INDEX = "\
64         SELECT \
65             type, \
66             name, \
67             tbl_name \
68         FROM sqlite_master \
69         WHERE \
70             tbl_name = ? AND \
71             type = 'index' AND \
72             sql LIKE 'CREATE UNIQUE INDEX %';";
73 };
74 }  // namespace DataTransfer
75 }  // namespace OHOS::Media
76 #endif  // OHOS_MEDIA_DATATRANSFER_DB_UPGRADE_UTILS_H