1fc0b0055Sopenharmony_ci/* 2fc0b0055Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3fc0b0055Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fc0b0055Sopenharmony_ci * you may not use this file except in compliance with the License. 5fc0b0055Sopenharmony_ci * You may obtain a copy of the License at 6fc0b0055Sopenharmony_ci * 7fc0b0055Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fc0b0055Sopenharmony_ci * 9fc0b0055Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fc0b0055Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fc0b0055Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fc0b0055Sopenharmony_ci * See the License for the specific language governing permissions and 13fc0b0055Sopenharmony_ci * limitations under the License. 14fc0b0055Sopenharmony_ci */ 15fc0b0055Sopenharmony_ci 16fc0b0055Sopenharmony_ci#include "statement.h" 17fc0b0055Sopenharmony_ci 18fc0b0055Sopenharmony_ci#include "accesstoken_log.h" 19fc0b0055Sopenharmony_ci 20fc0b0055Sopenharmony_cinamespace OHOS { 21fc0b0055Sopenharmony_cinamespace Security { 22fc0b0055Sopenharmony_cinamespace AccessToken { 23fc0b0055Sopenharmony_cinamespace { 24fc0b0055Sopenharmony_cistatic constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "Statement"}; 25fc0b0055Sopenharmony_ci} 26fc0b0055Sopenharmony_ci 27fc0b0055Sopenharmony_ciStatement::Statement(sqlite3* db, const std::string& sql) : db_(db), sql_(sql) 28fc0b0055Sopenharmony_ci{ 29fc0b0055Sopenharmony_ci if (sqlite3_prepare_v2(db, sql.c_str(), sql.size(), &statement_, nullptr) != SQLITE_OK) { 30fc0b0055Sopenharmony_ci ACCESSTOKEN_LOG_ERROR(LABEL, "Cannot prepare, errorMsg: %{public}s", sqlite3_errmsg(db_)); 31fc0b0055Sopenharmony_ci } 32fc0b0055Sopenharmony_ci} 33fc0b0055Sopenharmony_ci 34fc0b0055Sopenharmony_ciStatement::~Statement() 35fc0b0055Sopenharmony_ci{ 36fc0b0055Sopenharmony_ci sqlite3_finalize(statement_); 37fc0b0055Sopenharmony_ci statement_ = nullptr; 38fc0b0055Sopenharmony_ci} 39fc0b0055Sopenharmony_ci 40fc0b0055Sopenharmony_civoid Statement::Bind(const int32_t index, const std::string& text) 41fc0b0055Sopenharmony_ci{ 42fc0b0055Sopenharmony_ci if (sqlite3_bind_text(statement_, index, text.c_str(), text.size(), SQLITE_TRANSIENT) != SQLITE_OK) { 43fc0b0055Sopenharmony_ci ACCESSTOKEN_LOG_ERROR(LABEL, "Cannot bind string, errorMsg: %{public}s", sqlite3_errmsg(db_)); 44fc0b0055Sopenharmony_ci } 45fc0b0055Sopenharmony_ci} 46fc0b0055Sopenharmony_ci 47fc0b0055Sopenharmony_civoid Statement::Bind(const int32_t index, int32_t value) 48fc0b0055Sopenharmony_ci{ 49fc0b0055Sopenharmony_ci if (sqlite3_bind_int(statement_, index, value) != SQLITE_OK) { 50fc0b0055Sopenharmony_ci ACCESSTOKEN_LOG_ERROR(LABEL, "Cannot bind int32_t, errorMsg: %{public}s", sqlite3_errmsg(db_)); 51fc0b0055Sopenharmony_ci } 52fc0b0055Sopenharmony_ci} 53fc0b0055Sopenharmony_ci 54fc0b0055Sopenharmony_civoid Statement::Bind(const int32_t index, int64_t value) 55fc0b0055Sopenharmony_ci{ 56fc0b0055Sopenharmony_ci if (sqlite3_bind_int64(statement_, index, value) != SQLITE_OK) { 57fc0b0055Sopenharmony_ci ACCESSTOKEN_LOG_ERROR(LABEL, "Cannot bind int64_t, errorMsg: %{public}s", sqlite3_errmsg(db_)); 58fc0b0055Sopenharmony_ci } 59fc0b0055Sopenharmony_ci} 60fc0b0055Sopenharmony_ci 61fc0b0055Sopenharmony_ciint32_t Statement::GetColumnInt(const int32_t column) const 62fc0b0055Sopenharmony_ci{ 63fc0b0055Sopenharmony_ci return sqlite3_column_int(statement_, column); 64fc0b0055Sopenharmony_ci} 65fc0b0055Sopenharmony_ci 66fc0b0055Sopenharmony_ciint64_t Statement::GetColumnInt64(const int32_t column) const 67fc0b0055Sopenharmony_ci{ 68fc0b0055Sopenharmony_ci return sqlite3_column_int64(statement_, column); 69fc0b0055Sopenharmony_ci} 70fc0b0055Sopenharmony_ci 71fc0b0055Sopenharmony_cistd::string Statement::GetColumnString(const int32_t column) const 72fc0b0055Sopenharmony_ci{ 73fc0b0055Sopenharmony_ci return std::string(reinterpret_cast<const char*>(sqlite3_column_text(statement_, column))); 74fc0b0055Sopenharmony_ci} 75fc0b0055Sopenharmony_ci 76fc0b0055Sopenharmony_cistd::string Statement::GetColumnName(const int32_t column) const 77fc0b0055Sopenharmony_ci{ 78fc0b0055Sopenharmony_ci return sqlite3_column_name(statement_, column); 79fc0b0055Sopenharmony_ci} 80fc0b0055Sopenharmony_ci 81fc0b0055Sopenharmony_ciStatement::State Statement::Step() 82fc0b0055Sopenharmony_ci{ 83fc0b0055Sopenharmony_ci int32_t ret = sqlite3_step(statement_); 84fc0b0055Sopenharmony_ci switch (ret) { 85fc0b0055Sopenharmony_ci case SQLITE_ROW: 86fc0b0055Sopenharmony_ci return Statement::State::ROW; 87fc0b0055Sopenharmony_ci case SQLITE_DONE: 88fc0b0055Sopenharmony_ci return Statement::State::DONE; 89fc0b0055Sopenharmony_ci case SQLITE_BUSY: 90fc0b0055Sopenharmony_ci return Statement::State::BUSY; 91fc0b0055Sopenharmony_ci case SQLITE_MISUSE: 92fc0b0055Sopenharmony_ci return Statement::State::MISUSE; 93fc0b0055Sopenharmony_ci default: 94fc0b0055Sopenharmony_ci return Statement::State::UNKNOWN; 95fc0b0055Sopenharmony_ci } 96fc0b0055Sopenharmony_ci} 97fc0b0055Sopenharmony_ci 98fc0b0055Sopenharmony_ciint32_t Statement::GetParameterIndex(const std::string& name) const 99fc0b0055Sopenharmony_ci{ 100fc0b0055Sopenharmony_ci return sqlite3_bind_parameter_index(statement_, name.c_str()); 101fc0b0055Sopenharmony_ci} 102fc0b0055Sopenharmony_ci 103fc0b0055Sopenharmony_civoid Statement::Bind(const std::string& tableColumnName, const VariantValue& value) 104fc0b0055Sopenharmony_ci{ 105fc0b0055Sopenharmony_ci int32_t index = GetParameterIndex(":" + tableColumnName); 106fc0b0055Sopenharmony_ci if (value.GetType() == ValueType::TYPE_STRING) { 107fc0b0055Sopenharmony_ci Bind(index, value.GetString()); 108fc0b0055Sopenharmony_ci } else if (value.GetType() == ValueType::TYPE_INT) { 109fc0b0055Sopenharmony_ci Bind(index, value.GetInt()); 110fc0b0055Sopenharmony_ci } else if (value.GetType() == ValueType::TYPE_INT64) { 111fc0b0055Sopenharmony_ci Bind(index, value.GetInt64()); 112fc0b0055Sopenharmony_ci } 113fc0b0055Sopenharmony_ci} 114fc0b0055Sopenharmony_ci 115fc0b0055Sopenharmony_ciint32_t Statement::Reset() 116fc0b0055Sopenharmony_ci{ 117fc0b0055Sopenharmony_ci return sqlite3_reset(statement_); 118fc0b0055Sopenharmony_ci} 119fc0b0055Sopenharmony_ci 120fc0b0055Sopenharmony_ciint32_t Statement::GetColumnCount() const 121fc0b0055Sopenharmony_ci{ 122fc0b0055Sopenharmony_ci return sqlite3_column_count(statement_); 123fc0b0055Sopenharmony_ci} 124fc0b0055Sopenharmony_ci 125fc0b0055Sopenharmony_ciVariantValue Statement::GetValue(const int32_t column, const bool flagInt64) const 126fc0b0055Sopenharmony_ci{ 127fc0b0055Sopenharmony_ci int32_t type = sqlite3_column_type(statement_, column); 128fc0b0055Sopenharmony_ci switch (type) { 129fc0b0055Sopenharmony_ci case SQLITE_INTEGER: 130fc0b0055Sopenharmony_ci if (flagInt64) { 131fc0b0055Sopenharmony_ci return VariantValue(GetColumnInt64(column)); 132fc0b0055Sopenharmony_ci } else { 133fc0b0055Sopenharmony_ci return VariantValue(GetColumnInt(column)); 134fc0b0055Sopenharmony_ci } 135fc0b0055Sopenharmony_ci case SQLITE_TEXT: 136fc0b0055Sopenharmony_ci return VariantValue(GetColumnString(column)); 137fc0b0055Sopenharmony_ci default: 138fc0b0055Sopenharmony_ci return VariantValue(); 139fc0b0055Sopenharmony_ci } 140fc0b0055Sopenharmony_ci} 141fc0b0055Sopenharmony_ci} // namespace AccessToken 142fc0b0055Sopenharmony_ci} // namespace Security 143fc0b0055Sopenharmony_ci} // namespace OHOS 144