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