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 int index,const std::string & text)40 void Statement::Bind(const int 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 int index,int value)47 void Statement::Bind(const int index, int value)
48 {
49 if (sqlite3_bind_int(statement_, index, value) != SQLITE_OK) {
50 ACCESSTOKEN_LOG_ERROR(LABEL, "Cannot bind int, errorMsg: %{public}s", sqlite3_errmsg(db_));
51 }
52 }
53
GetColumnInt(const int column) const54 int Statement::GetColumnInt(const int column) const
55 {
56 return sqlite3_column_int(statement_, column);
57 }
58
GetColumnString(const int column) const59 std::string Statement::GetColumnString(const int column) const
60 {
61 return std::string(reinterpret_cast<const char*>(sqlite3_column_text(statement_, column)));
62 }
63
GetColumnName(const int column) const64 std::string Statement::GetColumnName(const int column) const
65 {
66 return sqlite3_column_name(statement_, column);
67 }
68
Step()69 Statement::State Statement::Step()
70 {
71 int ret = sqlite3_step(statement_);
72 switch (ret) {
73 case SQLITE_ROW:
74 return Statement::State::ROW;
75 case SQLITE_DONE:
76 return Statement::State::DONE;
77 case SQLITE_BUSY:
78 return Statement::State::BUSY;
79 case SQLITE_MISUSE:
80 return Statement::State::MISUSE;
81 default:
82 return Statement::State::UNKNOWN;
83 }
84 }
85
GetParameterIndex(const std::string & name) const86 int Statement::GetParameterIndex(const std::string& name) const
87 {
88 return sqlite3_bind_parameter_index(statement_, name.c_str());
89 }
90
Bind(const std::string & tableColumnName,const VariantValue & value)91 void Statement::Bind(const std::string& tableColumnName, const VariantValue& value)
92 {
93 int index = GetParameterIndex(":" + tableColumnName);
94 if (value.GetType() == ValueType::TYPE_STRING) {
95 Bind(index, value.GetString());
96 } else if (value.GetType() == ValueType::TYPE_INT) {
97 Bind(index, value.GetInt());
98 }
99 }
100
Reset()101 int Statement::Reset()
102 {
103 return sqlite3_reset(statement_);
104 }
105
GetColumnCount() const106 int Statement::GetColumnCount() const
107 {
108 return sqlite3_column_count(statement_);
109 }
110
GetValue(const int column) const111 VariantValue Statement::GetValue(const int column) const
112 {
113 int type = sqlite3_column_type(statement_, column);
114 switch (type) {
115 case SQLITE_INTEGER:
116 return VariantValue(GetColumnInt(column));
117 case SQLITE_TEXT:
118 return VariantValue(GetColumnString(column));
119 default:
120 return VariantValue();
121 }
122 }
123 } // namespace AccessToken
124 } // namespace Security
125 } // namespace OHOS
126