• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "rdb_data_generator.h"
17 
18 #include "distributeddb_tools_unit_test.h"
19 #include "log_print.h"
20 
21 namespace DistributedDBUnitTest {
22 using namespace DistributedDB;
GetTypeText(int type)23 std::string RDBDataGenerator::GetTypeText(int type)
24 {
25     switch (type) {
26         case DistributedDB::TYPE_INDEX<int64_t>:
27             return "INTEGER";
28         case DistributedDB::TYPE_INDEX<std::string>:
29             return "TEXT";
30         case DistributedDB::TYPE_INDEX<DistributedDB::Assets>:
31             return "ASSETS";
32         case DistributedDB::TYPE_INDEX<DistributedDB::Asset>:
33             return "ASSET";
34         case DistributedDB::TYPE_INDEX<double>:
35             return "DOUBLE";
36         case DistributedDB::TYPE_INDEX<Bytes>:
37             return "BLOB";
38         default:
39             return "";
40     }
41 }
42 
InitDatabaseWithSchemaInfo(const UtDateBaseSchemaInfo & schemaInfo,sqlite3 & db)43 int RDBDataGenerator::InitDatabaseWithSchemaInfo(const UtDateBaseSchemaInfo &schemaInfo, sqlite3 &db)
44 {
45     int errCode = RelationalTestUtils::ExecSql(&db, "PRAGMA journal_mode=WAL;");
46     if (errCode != SQLITE_OK) {
47         LOGE("[RDBDataGenerator] Execute sql failed %d", errCode);
48         return errCode;
49     }
50     for (const auto &tableInfo : schemaInfo.tablesInfo) {
51         errCode = InitTableWithSchemaInfo(tableInfo, db);
52         if (errCode != SQLITE_OK) {
53             LOGE("[RDBDataGenerator] Init table failed %d, %s", errCode, tableInfo.name.c_str());
54             break;
55         }
56     }
57     return errCode;
58 }
59 
InitTableWithSchemaInfo(const UtTableSchemaInfo & tableInfo,sqlite3 & db)60 int RDBDataGenerator::InitTableWithSchemaInfo(const UtTableSchemaInfo &tableInfo, sqlite3 &db)
61 {
62     std::string sql = "CREATE TABLE IF NOT EXISTS " + tableInfo.name + "(";
63     for (const auto &fieldInfo : tableInfo.fieldInfo) {
64         sql += "'" + fieldInfo.field.colName + "' " + GetTypeText(fieldInfo.field.type);
65         if (fieldInfo.field.primary) {
66             sql += " PRIMARY KEY";
67             if (fieldInfo.isAutoIncrement) {
68                 sql += " AUTOINCREMENT";
69             }
70         }
71         if (!fieldInfo.field.nullable) {
72             sql += " NOT NULL ON CONFLICT IGNORE";
73         }
74         sql += ",";
75     }
76     sql.pop_back();
77     sql += ");";
78     int errCode = RelationalTestUtils::ExecSql(&db, sql);
79     if (errCode != SQLITE_OK) {
80         LOGE("[RDBDataGenerator] Execute sql failed %d, sql is %s", errCode, sql.c_str());
81     }
82     return errCode;
83 }
84 }