• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include "security_guard_log.h"
18 
19 namespace OHOS {
20 namespace Security::SecurityGuard {
21 
Statement(sqlite3 * db,const std::string & sql)22 Statement::Statement(sqlite3* db, const std::string &sql) : db_(db), sql_(sql)
23 {
24     if (sqlite3_prepare_v2(db, sql.c_str(), sql.size(), &statement_, nullptr) != SQLITE_OK) {
25         SGLOGE("Cannot prepare, errorMsg: %{public}s", sqlite3_errmsg(db_));
26     }
27 }
28 
~Statement()29 Statement::~Statement()
30 {
31     sqlite3_finalize(statement_);
32     statement_ = nullptr;
33 }
34 
Bind(const int32_t index,const std::string & text)35 void Statement::Bind(const int32_t index, const std::string &text)
36 {
37     if (index < 0) {
38         SGLOGE("input index < 0");
39         return;
40     }
41 
42     if (sqlite3_bind_text(statement_, index, text.c_str(), text.size(), SQLITE_TRANSIENT) != SQLITE_OK) {
43         SGLOGE("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 (index < 0) {
50         SGLOGE("input index < 0");
51         return;
52     }
53 
54     if (sqlite3_bind_int(statement_, index, value) != SQLITE_OK) {
55         SGLOGE("Cannot bind int32_t, errorMsg: %{public}s", sqlite3_errmsg(db_));
56     }
57 }
58 
Bind(const int32_t index,int64_t value)59 void Statement::Bind(const int32_t index, int64_t value)
60 {
61     if (index < 0) {
62         SGLOGE("input index < 0");
63         return;
64     }
65 
66     if (sqlite3_bind_int64(statement_, index, value) != SQLITE_OK) {
67         SGLOGE("Cannot bind int64_t, errorMsg: %{public}s", sqlite3_errmsg(db_));
68     }
69 }
70 
GetColumnInt(const int32_t column) const71 int32_t Statement::GetColumnInt(const int32_t column) const
72 {
73     return sqlite3_column_int(statement_, column);
74 }
75 
GetColumnInt64(const int32_t column) const76 int64_t Statement::GetColumnInt64(const int32_t column) const
77 {
78     return sqlite3_column_int64(statement_, column);
79 }
80 
GetColumnString(const int32_t column) const81 std::string Statement::GetColumnString(const int32_t column) const
82 {
83     return std::string(reinterpret_cast<const char*>(sqlite3_column_text(statement_, column)));
84 }
85 
GetColumnName(const int32_t column) const86 std::string Statement::GetColumnName(const int32_t column) const
87 {
88     return sqlite3_column_name(statement_, column);
89 }
90 
Step()91 Statement::State Statement::Step()
92 {
93     int32_t ret = sqlite3_step(statement_);
94     switch (ret) {
95         case SQLITE_ROW:
96             return Statement::State::ROW;
97         case SQLITE_DONE:
98             return Statement::State::DONE;
99         case SQLITE_BUSY:
100             return Statement::State::BUSY;
101         case SQLITE_MISUSE:
102             return Statement::State::MISUSE;
103         default:
104             return Statement::State::UNKNOWN;
105     }
106 }
107 
GetParameterIndex(const std::string & name) const108 int32_t Statement::GetParameterIndex(const std::string &name) const
109 {
110     return sqlite3_bind_parameter_index(statement_, name.c_str());
111 }
112 
Bind(const std::string & tableColumnName,const VariantValue & value)113 void Statement::Bind(const std::string &tableColumnName, const VariantValue &value)
114 {
115     int32_t index = GetParameterIndex(":" + tableColumnName);
116     if (value.GetType() == ValueType::TYPE_STRING) {
117         Bind(index, value.GetString());
118     } else if (value.GetType() == ValueType::TYPE_INT) {
119         Bind(index, value.GetInt());
120     } else if (value.GetType() == ValueType::TYPE_INT64) {
121         Bind(index, value.GetInt64());
122     }
123 }
124 
Reset()125 int32_t Statement::Reset()
126 {
127     return sqlite3_reset(statement_);
128 }
129 
GetColumnCount() const130 int32_t Statement::GetColumnCount() const
131 {
132     return sqlite3_column_count(statement_);
133 }
134 
GetValue(const int32_t column,const bool flagInt64) const135 VariantValue Statement::GetValue(const int32_t column, const bool flagInt64) const
136 {
137     int32_t type = sqlite3_column_type(statement_, column);
138     switch (type) {
139         case SQLITE_INTEGER:
140             if (flagInt64) {
141                 return VariantValue(GetColumnInt64(column));
142             } else {
143                 return VariantValue(GetColumnInt(column));
144             }
145         case SQLITE_TEXT:
146             return VariantValue(GetColumnString(column));
147         default:
148             return VariantValue();
149     }
150 }
151 } // namespace Security::SecurityGuard
152 } // namespace OHOS
153