1 /* 2 * Copyright (c) 2024 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 #ifndef RDB_DATA_GENERATE_H 16 #define RDB_DATA_GENERATE_H 17 18 #include "cloud/cloud_store_types.h" 19 #include "relational_virtual_device.h" 20 #include "sqlite_utils.h" 21 #include "virtual_cloud_db.h" 22 namespace DistributedDBUnitTest { 23 struct UtFieldInfo { 24 DistributedDB::Field field; 25 bool isAutoIncrement = false; 26 }; 27 28 struct UtTableSchemaInfo { 29 std::string name; 30 std::string sharedTableName; 31 std::vector<UtFieldInfo> fieldInfo; 32 }; 33 34 struct UtDateBaseSchemaInfo { 35 std::vector<UtTableSchemaInfo> tablesInfo; 36 }; 37 class RDBDataGenerator { 38 public: 39 static int InitDatabase(const DistributedDB::DataBaseSchema &schema, sqlite3 &db); 40 41 static int InitTable(const DistributedDB::TableSchema &table, bool notNullWithStr, sqlite3 &db); 42 43 static int InitTable(const DistributedDB::TableSchema &table, bool notNullWithStr, bool isAutoIncrement, 44 sqlite3 &db); 45 46 static DistributedDB::DBStatus InsertCloudDBData(int64_t begin, int64_t count, int64_t gidStart, 47 const DistributedDB::DataBaseSchema &schema, 48 const std::shared_ptr<DistributedDB::VirtualCloudDb> &virtualCloudDb); 49 50 static std::pair<std::vector<DistributedDB::VBucket>, std::vector<DistributedDB::VBucket>> GenerateDataRecords( 51 int64_t begin, int64_t count, int64_t gidStart, const std::vector<DistributedDB::Field> &fields); 52 53 static int InsertLocalDBData(int64_t begin, int64_t count, sqlite3 *db, 54 const DistributedDB::DataBaseSchema &schema); 55 56 static int InsertLocalDBData(int64_t begin, int64_t count, sqlite3 *db, 57 const DistributedDB::TableSchema &schema); 58 59 static int UpsertLocalDBData(int64_t begin, int64_t count, sqlite3 *db, 60 const DistributedDB::TableSchema &schema); 61 62 static int UpdateLocalDBData(int64_t begin, int64_t count, sqlite3 *db, 63 const DistributedDB::TableSchema &schema); 64 65 static int InsertVirtualLocalDBData(int64_t begin, int64_t count, DistributedDB::RelationalVirtualDevice *device, 66 const DistributedDB::TableSchema &schema, const bool isDelete = false); 67 68 static DistributedDB::DistributedSchema ParseSchema(const DistributedDB::DataBaseSchema &schema, 69 bool syncOnlyPk = false); 70 71 static int PrepareVirtualDeviceEnv(const std::string &tableName, sqlite3 *db, 72 DistributedDB::RelationalVirtualDevice *device); 73 74 static int PrepareVirtualDeviceEnv(const std::string &scanTable, const std::string &expectTable, sqlite3 *db, 75 DistributedDB::RelationalVirtualDevice *device); 76 77 static DistributedDB::TableSchema FlipTableSchema(const DistributedDB::TableSchema &origin); 78 79 static int InitDatabaseWithSchemaInfo(const UtDateBaseSchemaInfo &schemaInfo, sqlite3 &db); 80 81 static int InitTableWithSchemaInfo(const UtTableSchemaInfo &tableInfo, sqlite3 &db); 82 private: 83 static std::string GetTypeText(int type); 84 85 static void FillColValueByType(int64_t index, const DistributedDB::Field &field, DistributedDB::VBucket &vBucket); 86 87 static DistributedDB::Type GetColValueByType(int64_t index, const DistributedDB::Field &field); 88 89 static DistributedDB::Asset GenerateAsset(int64_t index, const DistributedDB::Field &field); 90 91 static DistributedDB::Assets GenerateAssets(int64_t index, const DistributedDB::Field &field); 92 93 static int BindOneRowStmt(int64_t index, sqlite3_stmt *stmt, int cid, bool withoutPk, 94 const std::vector<DistributedDB::Field> &fields); 95 96 static int BindOneRowUpdateStmt(int64_t index, sqlite3_stmt *stmt, int cid, 97 const std::vector<DistributedDB::Field> &fields); 98 99 static int BindOneColStmt(int cid, sqlite3_stmt *stmt, const DistributedDB::Type &type); 100 101 static std::string GetUpsertSQL(const DistributedDB::TableSchema &schema); 102 103 static std::string GetUpdateSQL(const DistributedDB::TableSchema &schema); 104 105 static void FillTypeIntoDataValue(const DistributedDB::Field &field, const DistributedDB::Type &type, 106 DistributedDB::VirtualRowData &virtualRow); 107 static DistributedDB::Bytes GenerateBytes(int64_t index); 108 }; 109 } 110 #endif // RDB_DATA_GENERATE_H 111