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 20namespace OHOS { 21namespace Security { 22namespace AccessToken { 23namespace { 24static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "Statement"}; 25} 26 27Statement::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 34Statement::~Statement() 35{ 36 sqlite3_finalize(statement_); 37 statement_ = nullptr; 38} 39 40void 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 47void 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 54void 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 61int32_t Statement::GetColumnInt(const int32_t column) const 62{ 63 return sqlite3_column_int(statement_, column); 64} 65 66int64_t Statement::GetColumnInt64(const int32_t column) const 67{ 68 return sqlite3_column_int64(statement_, column); 69} 70 71std::string Statement::GetColumnString(const int32_t column) const 72{ 73 return std::string(reinterpret_cast<const char*>(sqlite3_column_text(statement_, column))); 74} 75 76std::string Statement::GetColumnName(const int32_t column) const 77{ 78 return sqlite3_column_name(statement_, column); 79} 80 81Statement::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 98int32_t Statement::GetParameterIndex(const std::string& name) const 99{ 100 return sqlite3_bind_parameter_index(statement_, name.c_str()); 101} 102 103void 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 115int32_t Statement::Reset() 116{ 117 return sqlite3_reset(statement_); 118} 119 120int32_t Statement::GetColumnCount() const 121{ 122 return sqlite3_column_count(statement_); 123} 124 125VariantValue 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