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 int BuildQueryString(bool distinct, const std::string &table, const std::string &joinClause, 34 const std::vector<std::string> &columns, const std::string &whereClause, const std::string &groupBy, 35 const std::string &indexName, const std::string &orderBy, int limit, int offset, std::string &outSql); 36 static std::string BuildQueryString(const AbsRdbPredicates &predicates, const std::vector<std::string> &columns); 37 static std::string BuildCountString(const AbsRdbPredicates &predicates); 38 static std::string BuildClauseFromPredicates(const AbsRdbPredicates &predicates); 39 static std::string BuildCursorQueryString(const AbsRdbPredicates &predicates, 40 const std::vector<std::string> &columns, const std::string &logTable, const std::pair<bool, bool> &queryStatus); 41 static std::string BuildLockRowQueryString( 42 const AbsRdbPredicates &predicates, const std::vector<std::string> &columns, const std::string &logTable); 43 static std::string GetSqlArgs(size_t size); 44 45 static void AppendReturning(std::string &sql, const std::vector<std::string> &fields); 46 47 static BatchRefSqls GenerateSqls(const std::string &table, const ValuesBuckets &buckets, int limit, 48 ConflictResolution resolution = ConflictResolution::ON_CONFLICT_REPLACE); 49 static void UpdateAssetStatus(const ValueObject &value, int32_t status); 50 51 private: 52 static BatchRefSqls MakeExecuteSqls( 53 const std::string &sql, const std::vector<RefValue> &args, int fieldSize, int limit); 54 static void AppendClause( 55 std::string &builder, const std::string &name, const std::string &clause, const std::string &table = ""); 56 static void AppendColumns( 57 std::string &builder, const std::vector<std::string> &columns, const std::string &table = ""); 58 static void AppendLimitAndOffset(std::string &builder, int limit, int offset); 59 static std::string GetSelectClause(const std::vector<std::string> &columns, bool IsDistinct, 60 const std::string &ast, const std::string &table = ""); 61 static constexpr const char *SHARING_RESOURCE = "sharing_resource"; 62 static constexpr uint32_t EXPANSION = 2; 63 static ValueObject nullObject_; 64 static std::reference_wrapper<ValueObject> nullRef_; 65 }; 66 } // namespace NativeRdb 67 } // namespace OHOS 68 #endif 69