• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class RDBDataGenerator {
24 public:
25     static int InitDatabase(const DistributedDB::DataBaseSchema &schema, sqlite3 &db);
26 
27     static int InitTable(const DistributedDB::TableSchema &table, bool notNullWithStr, sqlite3 &db);
28 
29     static int InitTable(const DistributedDB::TableSchema &table, bool notNullWithStr, bool isAutoIncrement,
30         sqlite3 &db);
31 
32     static DistributedDB::DBStatus InsertCloudDBData(int64_t begin, int64_t count, int64_t gidStart,
33         const DistributedDB::DataBaseSchema &schema,
34         const std::shared_ptr<DistributedDB::VirtualCloudDb> &virtualCloudDb);
35 
36     static std::pair<std::vector<DistributedDB::VBucket>, std::vector<DistributedDB::VBucket>> GenerateDataRecords(
37         int64_t begin, int64_t count, int64_t gidStart, const std::vector<DistributedDB::Field> &fields);
38 
39     static int InsertLocalDBData(int64_t begin, int64_t count, sqlite3 *db,
40         const DistributedDB::DataBaseSchema &schema);
41 
42     static int InsertLocalDBData(int64_t begin, int64_t count, sqlite3 *db,
43         const DistributedDB::TableSchema &schema);
44 
45     static int UpsertLocalDBData(int64_t begin, int64_t count, sqlite3 *db,
46         const DistributedDB::TableSchema &schema);
47 
48     static int UpdateLocalDBData(int64_t begin, int64_t count, sqlite3 *db,
49         const DistributedDB::TableSchema &schema);
50 
51     static int InsertVirtualLocalDBData(int64_t begin, int64_t count, DistributedDB::RelationalVirtualDevice *device,
52         const DistributedDB::TableSchema &schema);
53 
54     static DistributedDB::DistributedSchema ParseSchema(const DistributedDB::DataBaseSchema &schema,
55         bool syncOnlyPk = false);
56 
57     static int PrepareVirtualDeviceEnv(const std::string &tableName, sqlite3 *db,
58         DistributedDB::RelationalVirtualDevice *device);
59 
60     static int PrepareVirtualDeviceEnv(const std::string &scanTable, const std::string &expectTable, sqlite3 *db,
61         DistributedDB::RelationalVirtualDevice *device);
62 
63     static DistributedDB::TableSchema FlipTableSchema(const DistributedDB::TableSchema &origin);
64 private:
65     static std::string GetTypeText(int type);
66 
67     static void FillColValueByType(int64_t index, const DistributedDB::Field &field, DistributedDB::VBucket &vBucket);
68 
69     static DistributedDB::Type GetColValueByType(int64_t index, const DistributedDB::Field &field);
70 
71     static DistributedDB::Asset GenerateAsset(int64_t index, const DistributedDB::Field &field);
72 
73     static DistributedDB::Assets GenerateAssets(int64_t index, const DistributedDB::Field &field);
74 
75     static int BindOneRowStmt(int64_t index, sqlite3_stmt *stmt, int cid, bool withoutPk,
76         const std::vector<DistributedDB::Field> &fields);
77 
78     static int BindOneRowUpdateStmt(int64_t index, sqlite3_stmt *stmt, int cid,
79         const std::vector<DistributedDB::Field> &fields);
80 
81     static int BindOneColStmt(int cid, sqlite3_stmt *stmt, const DistributedDB::Type &type);
82 
83     static std::string GetUpsertSQL(const DistributedDB::TableSchema &schema);
84 
85     static std::string GetUpdateSQL(const DistributedDB::TableSchema &schema);
86 
87     static void FillTypeIntoDataValue(const DistributedDB::Field &field, const DistributedDB::Type &type,
88         DistributedDB::VirtualRowData &virtualRow);
89 };
90 }
91 #endif // RDB_DATA_GENERATE_H
92