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 #include "net_stats_sqlite_statement.h"
17
18 #include "net_manager_constants.h"
19 #include "net_mgr_log_wrapper.h"
20 #include "net_stats_constants.h"
21
22 namespace OHOS {
23 namespace NetManagerStandard {
24
~NetStatsSqliteStatement()25 NetStatsSqliteStatement::~NetStatsSqliteStatement()
26 {
27 Finalize();
28 }
29
Prepare(sqlite3 * dbHandle,const std::string & newSql)30 int32_t NetStatsSqliteStatement::Prepare(sqlite3 *dbHandle, const std::string &newSql)
31 {
32 if (sqlCmd_.compare(newSql) == 0) {
33 NETMGR_LOG_I("%{public}s is already prepared", newSql.c_str());
34 return SQLITE_OK;
35 }
36 sqlite3_stmt *stmt = nullptr;
37 int32_t errCode = sqlite3_prepare_v2(dbHandle, newSql.c_str(), newSql.length(), &stmt, nullptr);
38 if (errCode != SQLITE_OK) {
39 NETMGR_LOG_E("Prepare failed err = %{public}d", errCode);
40 if (stmt != nullptr) {
41 sqlite3_finalize(stmt);
42 }
43 return errCode;
44 }
45 Finalize(); // finalize the old
46 sqlCmd_ = newSql;
47 stmtHandle_ = stmt;
48 columnCount_ = sqlite3_column_count(stmtHandle_);
49 return SQLITE_OK;
50 }
51
Finalize()52 void NetStatsSqliteStatement::Finalize()
53 {
54 if (stmtHandle_ == nullptr) {
55 return;
56 }
57
58 sqlite3_finalize(stmtHandle_);
59 stmtHandle_ = nullptr;
60 sqlCmd_ = "";
61 columnCount_ = 0;
62 }
63
BindInt32(int32_t index,int32_t value) const64 int32_t NetStatsSqliteStatement::BindInt32(int32_t index, int32_t value) const
65 {
66 return sqlite3_bind_int(stmtHandle_, index, value);
67 }
68
BindInt64(int32_t index,int64_t value) const69 int32_t NetStatsSqliteStatement::BindInt64(int32_t index, int64_t value) const
70 {
71 return sqlite3_bind_int64(stmtHandle_, index, value);
72 }
73
BindText(int32_t index,std::string value) const74 int32_t NetStatsSqliteStatement::BindText(int32_t index, std::string value) const
75 {
76 return sqlite3_bind_text(stmtHandle_, index, value.c_str(), -1, SQLITE_STATIC);
77 }
78
ResetStatementAndClearBindings() const79 void NetStatsSqliteStatement::ResetStatementAndClearBindings() const
80 {
81 if (stmtHandle_ == nullptr) {
82 return;
83 }
84
85 int32_t errCode = sqlite3_reset(stmtHandle_);
86 if (sqlite3_reset(stmtHandle_) != SQLITE_OK) {
87 NETMGR_LOG_E("Reset statement failed. %{public}d", errCode);
88 return;
89 }
90
91 errCode = sqlite3_clear_bindings(stmtHandle_);
92 if (errCode != SQLITE_OK) {
93 NETMGR_LOG_E("Reset clear bindings failed. %{public}d", errCode);
94 }
95 }
96
Step()97 int32_t NetStatsSqliteStatement::Step()
98 {
99 return sqlite3_step(stmtHandle_);
100 }
101
GetColumnString(int32_t index,std::string & value) const102 int32_t NetStatsSqliteStatement::GetColumnString(int32_t index, std::string &value) const
103 {
104 if (stmtHandle_ == nullptr || index >= columnCount_) {
105 NETMGR_LOG_E("Get column string failed");
106 return SQLITE_ERROR;
107 }
108
109 int32_t type = sqlite3_column_type(stmtHandle_, index);
110 if (type != SQLITE_TEXT) {
111 NETMGR_LOG_E("Get column string failed type is not text");
112 return SQLITE_ERROR;
113 }
114 auto val = reinterpret_cast<const char *>(sqlite3_column_text(stmtHandle_, index));
115 value = (val == nullptr) ? "" : std::string(val, sqlite3_column_bytes(stmtHandle_, index));
116 return val == nullptr ? SQLITE_ERROR : SQLITE_OK;
117 }
118
GetColumnLong(int32_t index,uint64_t & value) const119 int32_t NetStatsSqliteStatement::GetColumnLong(int32_t index, uint64_t &value) const
120 {
121 if (stmtHandle_ == nullptr || index >= columnCount_) {
122 NETMGR_LOG_E("Get column long failed");
123 return SQLITE_ERROR;
124 }
125 int32_t type = sqlite3_column_type(stmtHandle_, index);
126 if (type != SQLITE_INTEGER) {
127 NETMGR_LOG_E("Get column long failed type is not interger");
128 return SQLITE_ERROR;
129 }
130 value = static_cast<uint64_t>(sqlite3_column_int64(stmtHandle_, index));
131 return SQLITE_OK;
132 }
133
GetColumnInt(int32_t index,uint32_t & value) const134 int32_t NetStatsSqliteStatement::GetColumnInt(int32_t index, uint32_t &value) const
135 {
136 if (stmtHandle_ == nullptr || index >= columnCount_) {
137 NETMGR_LOG_E("Get column int failed");
138 return SQLITE_ERROR;
139 }
140 int32_t type = sqlite3_column_type(stmtHandle_, index);
141 if (type != SQLITE_INTEGER) {
142 NETMGR_LOG_E("Get column int failed type is not interger");
143 return SQLITE_ERROR;
144 }
145 value = static_cast<uint64_t>(sqlite3_column_int(stmtHandle_, index));
146 return SQLITE_OK;
147 }
148 } // namespace NetManagerStandard
149 } // namespace OHOS
150