1 /* 2 * Copyright (c) 2022 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 SQLITE_RELATIONAL_UTILS_H 17 #define SQLITE_RELATIONAL_UTILS_H 18 19 #include <vector> 20 #include "cloud/cloud_store_types.h" 21 #include "data_value.h" 22 #include "sqlite_import.h" 23 #include "sqlite_single_ver_relational_storage_executor.h" 24 #include "table_info.h" 25 26 namespace DistributedDB { 27 class SQLiteRelationalUtils { 28 public: 29 static int GetDataValueByType(sqlite3_stmt *statement, int cid, DataValue &value); 30 31 static std::vector<DataValue> GetSelectValues(sqlite3_stmt *stmt); 32 33 static int GetCloudValueByType(sqlite3_stmt *statement, int type, int cid, Type &cloudValue); 34 35 static void CalCloudValueLen(Type &cloudValue, uint32_t &totalSize); 36 37 static int BindStatementByType(sqlite3_stmt *statement, int cid, Type &typeVal); 38 39 static int GetSelectVBucket(sqlite3_stmt *stmt, VBucket &bucket); 40 41 static bool GetDbFileName(sqlite3 *db, std::string &fileName); 42 43 static int SelectServerObserver(sqlite3 *db, const std::string &tableName, bool isChanged); 44 45 static void AddUpgradeSqlToList(const TableInfo &tableInfo, 46 const std::vector<std::pair<std::string, std::string>> &fieldList, std::vector<std::string> &sqlList); 47 48 static int AnalysisTrackerTable(sqlite3 *db, const TrackerTable &trackerTable, TableInfo &tableInfo); 49 50 static int QueryCount(sqlite3 *db, const std::string &tableName, int64_t &count); 51 52 static int GetCursor(sqlite3 *db, const std::string &tableName, uint64_t &cursor); 53 54 static int CheckDistributedSchemaValid(const RelationalSchemaObject &schemaObj, const DistributedSchema &schema, 55 bool isForceUpgrade, SQLiteSingleVerRelationalStorageExecutor *executor); 56 57 static DistributedSchema FilterRepeatDefine(const DistributedSchema &schema); 58 59 static DistributedTable FilterRepeatDefine(const DistributedTable &table); 60 61 static int GetLogData(sqlite3_stmt *logStatement, LogInfo &logInfo); 62 63 static int GetLogInfoPre(sqlite3_stmt *queryStmt, DistributedTableMode mode, const DataItem &dataItem, 64 LogInfo &logInfoGet); 65 66 static int OperateDataStatus(sqlite3 *db, const std::vector<std::string> &tables, uint64_t virtualTime); 67 68 static int GetMetaLocalTimeOffset(sqlite3 *db, int64_t &timeOffset); 69 70 static std::pair<int, std::string> GetCurrentVirtualTime(sqlite3 *db); 71 72 static int CreateRelationalMetaTable(sqlite3 *db); 73 74 static int GetKvData(sqlite3 *db, bool isMemory, const Key &key, Value &value); 75 static int PutKvData(sqlite3 *db, bool isMemory, const Key &key, const Value &value); 76 77 static int InitCursorToMeta(sqlite3 *db, bool isMemory, const std::string &tableName); 78 static int InitKnowledgeTableTypeToMeta(sqlite3 *db, bool isMemory, const std::string &tableName); 79 static int SetLogTriggerStatus(sqlite3 *db, bool status); 80 81 struct GenLogParam { 82 sqlite3 *db = nullptr; 83 bool isMemory = false; 84 bool isTrackerTable = false; 85 }; 86 87 static int GeneTimeStrForLog(const TableInfo &tableInfo, GenLogParam ¶m, std::string &timeStr); 88 89 static int GeneLogInfoForExistedData(const std::string &identity, const TableInfo &tableInfo, 90 std::unique_ptr<SqliteLogTableManager> &logMgrPtr, GenLogParam ¶m); 91 92 static int GetExistedDataTimeOffset(sqlite3 *db, const std::string &tableName, bool isMem, int64_t &timeOffset); 93 94 static std::string GetExtendValue(const TrackerTable &trackerTable); 95 96 static int CleanTrackerData(sqlite3 *db, const std::string &tableName, int64_t cursor, bool isOnlyTrackTable); 97 98 static std::pair<int, bool> CheckExistDirtyLog(sqlite3 *db, const std::string &oriTable); 99 100 static int CleanDirtyLog(sqlite3 *db, const std::string &oriTable, const RelationalSchemaObject &obj); 101 102 static std::pair<int, bool> ExecuteCheckSql(sqlite3 *db, const std::string &sql); 103 104 static int ExecuteSql(sqlite3 *db, const std::string &sql, 105 const std::function<void(sqlite3_stmt *stmt)> &checkFunc); 106 107 static int ExecuteListAction(const std::vector<std::function<int()>> &actions); 108 109 static int GetLocalLogInfo(const RelationalSyncDataInserter &inserter, const DataItem &dataItem, 110 DistributedTableMode mode, LogInfo &logInfoGet, SaveSyncDataStmt &saveStmt); 111 112 static int GetLocalLog(const DataItem &dataItem, const RelationalSyncDataInserter &inserter, sqlite3_stmt *stmt, 113 LogInfo &logInfo); 114 115 static std::pair<int, VBucket> GetDistributedPk(const DataItem &dataItem, 116 const RelationalSyncDataInserter &inserter); 117 118 static int BindDistributedPk(sqlite3_stmt *stmt, const RelationalSyncDataInserter &inserter, 119 VBucket &distributedPk); 120 121 static int BindOneField(sqlite3_stmt *stmt, int bindIdx, const FieldInfo &fieldInfo, VBucket &distributedPk); 122 private: 123 static int BindExtendStatementByType(sqlite3_stmt *statement, int cid, Type &typeVal); 124 125 static int GetTypeValByStatement(sqlite3_stmt *stmt, int cid, Type &typeVal); 126 static int GetBlobByStatement(sqlite3_stmt *stmt, int cid, Type &typeVal); 127 128 static int UpdateLocalDataModifyTime(sqlite3 *db, const std::string &table, const std::string &modifyTime); 129 }; 130 } // namespace DistributedDB 131 #endif // SQLITE_RELATIONAL_UTILS_H 132