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 #include "sql_util.h"
16 
17 namespace OHOS {
18 namespace HiviewDFX {
19 namespace SqlUtil {
GenerateCreateSql(const std::string & table,const std::vector<std::pair<std::string,std::string>> & fields)20 std::string GenerateCreateSql(const std::string& table,
21     const std::vector<std::pair<std::string, std::string>>& fields)
22 {
23     std::string sql;
24     sql += "CREATE TABLE IF NOT EXISTS ";
25     sql += table;
26     sql += "(";
27     sql += "id INTEGER PRIMARY KEY AUTOINCREMENT";  // default field: id
28     for (auto field : fields) {
29         sql += ", ";
30         sql += field.first;
31         sql += " ";
32         sql += field.second;
33     }
34     sql += ")";
35     return sql;
36 }
37 
GenerateExistSql(const std::string & table)38 std::string GenerateExistSql(const std::string& table)
39 {
40     std::string sql = "SELECT name FROM sqlite_sequence WHERE name = '" + table + "'";
41     return sql;
42 }
43 
GenerateDropSql(const std::string & table)44 std::string GenerateDropSql(const std::string& table)
45 {
46     std::string sql;
47     sql += "DROP TABLE IF EXISTS ";
48     sql += table;
49     return sql;
50 }
51 } // namespace SqlUtil
52 } // namespace HiviewDFX
53 } // namespace OHOS
54