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_D("%{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         if (stmt != nullptr) {
40             sqlite3_finalize(stmt);
41         }
42         return errCode;
43     }
44     Finalize(); // finalize the old
45     sqlCmd_ = newSql;
46     stmtHandle_ = stmt;
47     columnCount_ = sqlite3_column_count(stmtHandle_);
48     return SQLITE_OK;
49 }
50 
Finalize()51 void NetStatsSqliteStatement::Finalize()
52 {
53     if (stmtHandle_ == nullptr) {
54         return;
55     }
56 
57     sqlite3_finalize(stmtHandle_);
58     stmtHandle_ = nullptr;
59     sqlCmd_ = "";
60     columnCount_ = 0;
61 }
62 
BindInt32(int32_t index,int32_t value) const63 int32_t NetStatsSqliteStatement::BindInt32(int32_t index, int32_t value) const
64 {
65     return sqlite3_bind_int(stmtHandle_, index, value);
66 }
67 
BindInt64(int32_t index,int64_t value) const68 int32_t NetStatsSqliteStatement::BindInt64(int32_t index, int64_t value) const
69 {
70     return sqlite3_bind_int64(stmtHandle_, index, value);
71 }
72 
BindText(int32_t index,std::string value) const73 int32_t NetStatsSqliteStatement::BindText(int32_t index, std::string value) const
74 {
75     return sqlite3_bind_text(stmtHandle_, index, value.c_str(), -1, SQLITE_STATIC);
76 }
77 
ResetStatementAndClearBindings() const78 void NetStatsSqliteStatement::ResetStatementAndClearBindings() const
79 {
80     if (stmtHandle_ == nullptr) {
81         return;
82     }
83 
84     int32_t errCode = sqlite3_reset(stmtHandle_);
85     if (sqlite3_reset(stmtHandle_) != SQLITE_OK) {
86         NETMGR_LOG_E("Reset statement failed. %{public}d", errCode);
87         return;
88     }
89 
90     errCode = sqlite3_clear_bindings(stmtHandle_);
91     if (errCode != SQLITE_OK) {
92         NETMGR_LOG_E("Reset clear bindings failed. %{public}d", errCode);
93     }
94 }
95 
Step()96 int32_t NetStatsSqliteStatement::Step()
97 {
98     return sqlite3_step(stmtHandle_);
99 }
100 
GetColumnString(int32_t index,std::string & value) const101 int32_t NetStatsSqliteStatement::GetColumnString(int32_t index, std::string &value) const
102 {
103     if (stmtHandle_ == nullptr || index >= columnCount_) {
104         NETMGR_LOG_E("Get column string failed");
105         return SQLITE_ERROR;
106     }
107 
108     int32_t type = sqlite3_column_type(stmtHandle_, index);
109     if (type != SQLITE_TEXT) {
110         NETMGR_LOG_E("Get column string failed type is not text");
111         return SQLITE_ERROR;
112     }
113     auto val = reinterpret_cast<const char *>(sqlite3_column_text(stmtHandle_, index));
114     value = (val == nullptr) ? "" : std::string(val, sqlite3_column_bytes(stmtHandle_, index));
115     return val == nullptr ? SQLITE_ERROR : SQLITE_OK;
116 }
117 
GetColumnLong(int32_t index,uint64_t & value) const118 int32_t NetStatsSqliteStatement::GetColumnLong(int32_t index, uint64_t &value) const
119 {
120     if (stmtHandle_ == nullptr || index >= columnCount_) {
121         NETMGR_LOG_E("Get column long failed");
122         return SQLITE_ERROR;
123     }
124     int32_t type = sqlite3_column_type(stmtHandle_, index);
125     if (type != SQLITE_INTEGER) {
126         NETMGR_LOG_E("Get column long failed type is not interger");
127         return SQLITE_ERROR;
128     }
129     value = static_cast<uint64_t>(sqlite3_column_int64(stmtHandle_, index));
130     return SQLITE_OK;
131 }
132 
GetColumnInt(int32_t index,uint32_t & value) const133 int32_t NetStatsSqliteStatement::GetColumnInt(int32_t index, uint32_t &value) const
134 {
135     if (stmtHandle_ == nullptr || index >= columnCount_) {
136         NETMGR_LOG_E("Get column int failed");
137         return SQLITE_ERROR;
138     }
139     int32_t type = sqlite3_column_type(stmtHandle_, index);
140     if (type != SQLITE_INTEGER) {
141         NETMGR_LOG_E("Get column int failed type is not interger");
142         return SQLITE_ERROR;
143     }
144     value = static_cast<uint64_t>(sqlite3_column_int(stmtHandle_, index));
145     return SQLITE_OK;
146 }
147 } // namespace NetManagerStandard
148 } // namespace OHOS
149