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