• 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 #define LOG_TAG "TransDB"
16 #include "trans_db.h"
17 
18 #include "gdb_errors.h"
19 #include "db_trace.h"
20 #include "gdb_utils.h"
21 #include "logger.h"
22 
23 namespace OHOS::DistributedDataAip {
24 
25 constexpr int32_t MAX_GQL_LEN = 1024 * 1024;
26 
TransDB(std::shared_ptr<Connection> connection)27 TransDB::TransDB(std::shared_ptr<Connection> connection) : conn_(connection)
28 {
29 }
30 
QueryGql(const std::string & gql)31 std::pair<int32_t, std::shared_ptr<Result>> TransDB::QueryGql(const std::string &gql)
32 {
33     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
34     if (gql.empty() || gql.length() > MAX_GQL_LEN) {
35         LOG_ERROR("Gql is empty or length is too long.");
36         return { E_INVALID_ARGS, std::make_shared<FullResult>() };
37     }
38     if (GdbUtils::IsTransactionGql(gql)) {
39         LOG_ERROR("Transaction related statements are not supported.");
40         return { E_INVALID_ARGS, std::make_shared<FullResult>() };
41     }
42 
43     auto [errCode, statement] = GetStatement(gql);
44     if (errCode != E_OK || statement == nullptr) {
45         return { errCode, std::make_shared<FullResult>() };
46     }
47 
48     auto result = std::make_shared<FullResult>();
49     errCode = result->InitData(statement);
50     if (errCode != E_OK) {
51         LOG_ERROR("Get FullResult failed, errCode=%{public}d", errCode);
52         return { errCode, std::make_shared<FullResult>() };
53     }
54 
55     return { errCode, result };
56 }
57 
ExecuteGql(const std::string & gql)58 std::pair<int32_t, std::shared_ptr<Result>> TransDB::ExecuteGql(const std::string &gql)
59 {
60     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
61     if (gql.empty() || gql.length() > MAX_GQL_LEN) {
62         LOG_ERROR("Gql is empty or length is too long.");
63         return { E_INVALID_ARGS, std::make_shared<FullResult>() };
64     }
65     if (GdbUtils::IsTransactionGql(gql)) {
66         LOG_ERROR("Transaction related statements are not supported.");
67         return { E_INVALID_ARGS, std::make_shared<FullResult>() };
68     }
69 
70     auto [errCode, statement] = GetStatement(gql);
71     if (errCode != E_OK || statement == nullptr) {
72         return { errCode, std::make_shared<FullResult>() };
73     }
74 
75     errCode = statement->Step();
76     if (errCode != E_OK) {
77         LOG_ERROR("Step statement failed, errCode=%{public}d", errCode);
78     }
79     return { errCode, std::make_shared<FullResult>() };
80 }
81 
GetStatement(const std::string & gql) const82 std::pair<int32_t, std::shared_ptr<Statement>> TransDB::GetStatement(const std::string &gql) const
83 {
84     auto connection = conn_.lock();
85     if (connection == nullptr) {
86         return { E_GRD_DB_INSTANCE_ABNORMAL, nullptr };
87     }
88     return connection->CreateStatement(gql, connection);
89 }
90 
CreateTransaction()91 std::pair<int32_t, std::shared_ptr<Transaction>> TransDB::CreateTransaction()
92 {
93     return { E_NOT_SUPPORT, nullptr };
94 }
95 
Close()96 int32_t TransDB::Close()
97 {
98     return E_NOT_SUPPORT;
99 }
100 } // namespace OHOS::DistributedDataAip