1 /*
2  * Copyright (c) 2021 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 #ifndef NATIVE_RDB_SQLITE_SQL_BUILDER_H
17 #define NATIVE_RDB_SQLITE_SQL_BUILDER_H
18 
19 #include <map>
20 #include <sstream>
21 #include <string>
22 #include <vector>
23 
24 #include "rdb_store.h"
25 namespace OHOS {
26 namespace NativeRdb {
27 class SqliteSqlBuilder {
28 public:
29     using RefValue = std::reference_wrapper<ValueObject>;
30     using BatchRefSqls = std::vector<std::pair<std::string, std::vector<std::vector<RefValue>>>>;
31     SqliteSqlBuilder();
32     ~SqliteSqlBuilder();
33     static std::string BuildDeleteString(const std::string &tableName, const std::string &index,
34         const std::string &whereClause, const std::string &group, const std::string &order, int limit, int offset);
35     static std::string BuildUpdateString(const ValuesBucket &values, const std::string &tableName,
36         const std::vector<std::string> &whereArgs, const std::string &index, const std::string &whereClause,
37         const std::string &group, const std::string &order, int limit, int offset, std::vector<ValueObject> &bindArgs,
38         ConflictResolution conflictResolution);
39     static int BuildQueryString(bool distinct, const std::string &table, const std::string &joinClause,
40         const std::vector<std::string> &columns, const std::string &whereClause, const std::string &groupBy,
41         const std::string &indexName, const std::string &orderBy, const int &limit,
42         const int &offset, std::string &outSql);
43     static std::string BuildCountString(const std::string &tableName, const std::string &index,
44         const std::string &whereClause, const std::string &group, const std::string &order, int limit, int offset);
45     static std::string BuildSqlStringFromPredicates(const std::string &index, const std::string &joinClause,
46         const std::string &whereClause, const std::string &group, const std::string &order, int limit, int offset);
47     static std::string BuildSqlStringFromPredicatesNoWhere(const std::string &index, const std::string &whereClause,
48         const std::string &group, const std::string &order, int limit, int offset);
49     static std::string BuildQueryString(const AbsRdbPredicates &predicates, const std::vector<std::string> &columns);
50     static std::string BuildCountString(const AbsRdbPredicates &predicates);
51     static std::string BuildSqlStringFromPredicates(const AbsPredicates &predicates);
52     static std::string BuildCursorQueryString(const AbsRdbPredicates &predicates,
53         const std::vector<std::string> &columns, const std::string &logTable, const std::pair<bool, bool> &queryStatus);
54     static std::string BuildLockRowQueryString(
55         const AbsRdbPredicates &predicates, const std::vector<std::string> &columns, const std::string &logTable);
56     static std::string GetSqlArgs(size_t size);
57 
58     static BatchRefSqls GenerateSqls(const std::string &table, const ValuesBuckets &buckets, int limit);
59     static void UpdateAssetStatus(const ValueObject &value, int32_t status);
60 private:
61     static BatchRefSqls MakeExecuteSqls(const std::string &sql, const std::vector<RefValue> &args, int fieldSize,
62         int limit);
63     static void AppendClause(std::string &builder, const std::string &name,
64         const std::string &clause, const std::string &table = "");
65     static void AppendColumns(
66         std::string &builder, const std::vector<std::string> &columns, const std::string &table = "");
67 
68     static constexpr const char *SHARING_RESOURCE = "sharing_resource";
69     static constexpr uint32_t EXPANSION = 2;
70     static ValueObject nullObject_;
71     static std::reference_wrapper<ValueObject> nullRef_;
72     static std::string HandleTable(const std::string &tableName);
73 };
74 } // namespace NativeRdb
75 } // namespace OHOS
76 #endif
77