1 /*
2 * Copyright (c) 2021-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 "statement.h"
17
18 #include "accesstoken_common_log.h"
19
20 namespace OHOS {
21 namespace Security {
22 namespace AccessToken {
23
Statement(sqlite3 * db,const std::string & sql)24 Statement::Statement(sqlite3* db, const std::string& sql) : db_(db), sql_(sql)
25 {
26 if (sqlite3_prepare_v2(db, sql.c_str(), sql.size(), &statement_, nullptr) != SQLITE_OK) {
27 LOGE(ATM_DOMAIN, ATM_TAG, "Cannot prepare, errorMsg: %{public}s", sqlite3_errmsg(db_));
28 }
29 }
30
~Statement()31 Statement::~Statement()
32 {
33 sqlite3_finalize(statement_);
34 statement_ = nullptr;
35 }
36
Bind(const int32_t index,const std::string & text)37 void Statement::Bind(const int32_t index, const std::string& text)
38 {
39 if (sqlite3_bind_text(statement_, index, text.c_str(), text.size(), SQLITE_TRANSIENT) != SQLITE_OK) {
40 LOGE(ATM_DOMAIN, ATM_TAG, "Cannot bind string, errorMsg: %{public}s", sqlite3_errmsg(db_));
41 }
42 }
43
Bind(const int32_t index,int32_t value)44 void Statement::Bind(const int32_t index, int32_t value)
45 {
46 if (sqlite3_bind_int(statement_, index, value) != SQLITE_OK) {
47 LOGE(ATM_DOMAIN, ATM_TAG, "Cannot bind int32_t, errorMsg: %{public}s", sqlite3_errmsg(db_));
48 }
49 }
50
Bind(const int32_t index,int64_t value)51 void Statement::Bind(const int32_t index, int64_t value)
52 {
53 if (sqlite3_bind_int64(statement_, index, value) != SQLITE_OK) {
54 LOGE(ATM_DOMAIN, ATM_TAG, "Cannot bind int64_t, errorMsg: %{public}s", sqlite3_errmsg(db_));
55 }
56 }
57
GetColumnInt(const int32_t column) const58 int32_t Statement::GetColumnInt(const int32_t column) const
59 {
60 return sqlite3_column_int(statement_, column);
61 }
62
GetColumnInt64(const int32_t column) const63 int64_t Statement::GetColumnInt64(const int32_t column) const
64 {
65 return sqlite3_column_int64(statement_, column);
66 }
67
GetColumnString(const int32_t column) const68 std::string Statement::GetColumnString(const int32_t column) const
69 {
70 return std::string(reinterpret_cast<const char*>(sqlite3_column_text(statement_, column)));
71 }
72
GetColumnName(const int32_t column) const73 std::string Statement::GetColumnName(const int32_t column) const
74 {
75 return sqlite3_column_name(statement_, column);
76 }
77
Step()78 Statement::State Statement::Step()
79 {
80 int32_t ret = sqlite3_step(statement_);
81 switch (ret) {
82 case SQLITE_ROW:
83 return Statement::State::ROW;
84 case SQLITE_DONE:
85 return Statement::State::DONE;
86 case SQLITE_BUSY:
87 return Statement::State::BUSY;
88 case SQLITE_MISUSE:
89 return Statement::State::MISUSE;
90 default:
91 return Statement::State::UNKNOWN;
92 }
93 }
94
GetParameterIndex(const std::string & name) const95 int32_t Statement::GetParameterIndex(const std::string& name) const
96 {
97 return sqlite3_bind_parameter_index(statement_, name.c_str());
98 }
99
Bind(const std::string & tableColumnName,const VariantValue & value)100 void Statement::Bind(const std::string& tableColumnName, const VariantValue& value)
101 {
102 int32_t index = GetParameterIndex(":" + tableColumnName);
103 if (value.GetType() == ValueType::TYPE_STRING) {
104 Bind(index, value.GetString());
105 } else if (value.GetType() == ValueType::TYPE_INT) {
106 Bind(index, value.GetInt());
107 } else if (value.GetType() == ValueType::TYPE_INT64) {
108 Bind(index, value.GetInt64());
109 }
110 }
111
Reset()112 int32_t Statement::Reset()
113 {
114 return sqlite3_reset(statement_);
115 }
116
GetColumnCount() const117 int32_t Statement::GetColumnCount() const
118 {
119 return sqlite3_column_count(statement_);
120 }
121
GetValue(const int32_t column,const bool flagInt64) const122 VariantValue Statement::GetValue(const int32_t column, const bool flagInt64) const
123 {
124 int32_t type = sqlite3_column_type(statement_, column);
125 switch (type) {
126 case SQLITE_INTEGER:
127 if (flagInt64) {
128 return VariantValue(GetColumnInt64(column));
129 } else {
130 return VariantValue(GetColumnInt(column));
131 }
132 case SQLITE_TEXT:
133 return VariantValue(GetColumnString(column));
134 default:
135 return VariantValue();
136 }
137 }
138 } // namespace AccessToken
139 } // namespace Security
140 } // namespace OHOS
141