src/utilities/sql/PreparedStatement.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "PreparedStatement.hpp" | ||
| 7 | #include <OpenStudio.hxx> | ||
| 8 | #include <sqlite3.h> | ||
| 9 | |||
| 10 | namespace openstudio { | ||
| 11 | 17286 | PreparedStatement::~PreparedStatement() { | |
| 12 |
1/2✓ Branch 0 taken 17286 times.
✗ Branch 1 not taken.
|
17286 | if (m_statement) { |
| 13 | 17286 | sqlite3_finalize(m_statement); | |
| 14 | } | ||
| 15 | |||
| 16 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17286 times.
|
17286 | if (m_transaction) { |
| 17 | ✗ | sqlite3_exec(m_db, "COMMIT", nullptr, nullptr, nullptr); | |
| 18 | } | ||
| 19 | 17286 | } | |
| 20 | |||
| 21 | 51430 | bool PreparedStatement::bind(int position, const std::string& t_str) { | |
| 22 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 51430 times.
|
51430 | if (sqlite3_bind_text(m_statement, position, t_str.c_str(), t_str.size(), SQLITE_TRANSIENT) != SQLITE_OK) { |
| 23 | ✗ | return false; | |
| 24 | } | ||
| 25 | 51430 | return true; | |
| 26 | } | ||
| 27 | |||
| 28 | 4 | bool PreparedStatement::bind(int position, int val) { | |
| 29 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if (sqlite3_bind_int(m_statement, position, val) != SQLITE_OK) { |
| 30 | ✗ | return false; | |
| 31 | } | ||
| 32 | 4 | return true; | |
| 33 | } | ||
| 34 | |||
| 35 | ✗ | bool PreparedStatement::bind(int position, unsigned int val) { | |
| 36 | ✗ | if (sqlite3_bind_int(m_statement, position, static_cast<int>(val)) != SQLITE_OK) { | |
| 37 | ✗ | return false; | |
| 38 | } | ||
| 39 | ✗ | return true; | |
| 40 | } | ||
| 41 | |||
| 42 | ✗ | bool PreparedStatement::bind(int position, double val) { | |
| 43 | ✗ | if (sqlite3_bind_double(m_statement, position, val) != SQLITE_OK) { | |
| 44 | ✗ | return false; | |
| 45 | } | ||
| 46 | ✗ | return true; | |
| 47 | } | ||
| 48 | |||
| 49 | ✗ | int PreparedStatement::execute() { | |
| 50 | ✗ | int code = sqlite3_step(m_statement); | |
| 51 | ✗ | sqlite3_reset(m_statement); | |
| 52 | ✗ | return code; | |
| 53 | } | ||
| 54 | |||
| 55 | 17200 | boost::optional<double> PreparedStatement::execAndReturnFirstDouble() const { | |
| 56 | 17200 | boost::optional<double> value; | |
| 57 |
1/2✓ Branch 0 taken 17200 times.
✗ Branch 1 not taken.
|
17200 | if (m_db) { |
| 58 |
1/1✓ Branch 1 taken 17200 times.
|
17200 | int code = sqlite3_step(m_statement); |
| 59 |
2/2✓ Branch 0 taken 1408 times.
✓ Branch 1 taken 15792 times.
|
17200 | if (code == SQLITE_ROW) { |
| 60 |
2/2✓ Branch 1 taken 1408 times.
✓ Branch 4 taken 1408 times.
|
1408 | value = sqlite3_column_double(m_statement, 0); |
| 61 | } | ||
| 62 | } | ||
| 63 | 17200 | return value; | |
| 64 | } | ||
| 65 | |||
| 66 | 82 | boost::optional<int> PreparedStatement::execAndReturnFirstInt() const { | |
| 67 | 82 | boost::optional<int> value; | |
| 68 |
1/2✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
|
82 | if (m_db) { |
| 69 |
1/1✓ Branch 1 taken 82 times.
|
82 | int code = sqlite3_step(m_statement); |
| 70 |
1/2✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
|
82 | if (code == SQLITE_ROW) { |
| 71 |
2/2✓ Branch 1 taken 82 times.
✓ Branch 4 taken 82 times.
|
82 | value = sqlite3_column_int(m_statement, 0); |
| 72 | } | ||
| 73 | } | ||
| 74 | 82 | return value; | |
| 75 | } | ||
| 76 | |||
| 77 | ✗ | boost::optional<std::string> PreparedStatement::execAndReturnFirstString() const { | |
| 78 | ✗ | boost::optional<std::string> value; | |
| 79 | ✗ | if (m_db) { | |
| 80 | ✗ | int code = sqlite3_step(m_statement); | |
| 81 | ✗ | if (code == SQLITE_ROW) { | |
| 82 | ✗ | value = columnText(sqlite3_column_text(m_statement, 0)); | |
| 83 | } | ||
| 84 | } | ||
| 85 | ✗ | return value; | |
| 86 | ✗ | } | |
| 87 | |||
| 88 | 4 | boost::optional<std::vector<double>> PreparedStatement::execAndReturnVectorOfDouble() const { | |
| 89 | |||
| 90 | 4 | boost::optional<double> value; | |
| 91 | 4 | boost::optional<std::vector<double>> valueVector; | |
| 92 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (m_db) { |
| 93 | 4 | int code = SQLITE_OK; | |
| 94 | |||
| 95 |
5/8✓ Branch 0 taken 40 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 40 times.
✗ Branch 7 not taken.
|
44 | while ((code != SQLITE_DONE) && (code != SQLITE_BUSY) && (code != SQLITE_ERROR) && (code != SQLITE_MISUSE)) //loop until SQLITE_DONE |
| 96 | { | ||
| 97 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 36 times.
|
40 | if (!valueVector) { |
| 98 |
1/1✓ Branch 2 taken 4 times.
|
4 | valueVector = std::vector<double>(); |
| 99 | } | ||
| 100 | |||
| 101 |
1/1✓ Branch 1 taken 40 times.
|
40 | code = sqlite3_step(m_statement); |
| 102 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 4 times.
|
40 | if (code == SQLITE_ROW) { |
| 103 |
2/2✓ Branch 1 taken 36 times.
✓ Branch 4 taken 36 times.
|
36 | value = sqlite3_column_double(m_statement, 0); |
| 104 |
3/3✓ Branch 1 taken 36 times.
✓ Branch 4 taken 36 times.
✓ Branch 7 taken 36 times.
|
36 | valueVector->push_back(*value); |
| 105 | } else // i didn't get a row. something is wrong so set the exit condition. | ||
| 106 | { // should never get here since i test for all documented return states above | ||
| 107 | 4 | code = SQLITE_DONE; | |
| 108 | } | ||
| 109 | |||
| 110 | } // end loop | ||
| 111 | } | ||
| 112 | |||
| 113 | 8 | return valueVector; | |
| 114 | ✗ | } | |
| 115 | |||
| 116 | ✗ | boost::optional<std::vector<int>> PreparedStatement::execAndReturnVectorOfInt() const { | |
| 117 | ✗ | boost::optional<int> value; | |
| 118 | ✗ | boost::optional<std::vector<int>> valueVector; | |
| 119 | ✗ | if (m_db) { | |
| 120 | ✗ | int code = SQLITE_OK; | |
| 121 | |||
| 122 | ✗ | while ((code != SQLITE_DONE) && (code != SQLITE_BUSY) && (code != SQLITE_ERROR) && (code != SQLITE_MISUSE)) //loop until SQLITE_DONE | |
| 123 | { | ||
| 124 | ✗ | if (!valueVector) { | |
| 125 | ✗ | valueVector = std::vector<int>(); | |
| 126 | } | ||
| 127 | |||
| 128 | ✗ | code = sqlite3_step(m_statement); | |
| 129 | ✗ | if (code == SQLITE_ROW) { | |
| 130 | ✗ | value = sqlite3_column_int(m_statement, 0); | |
| 131 | ✗ | valueVector->push_back(*value); | |
| 132 | } else // i didn't get a row. something is wrong so set the exit condition. | ||
| 133 | { // should never get here since i test for all documented return states above | ||
| 134 | ✗ | code = SQLITE_DONE; | |
| 135 | } | ||
| 136 | |||
| 137 | } // end loop | ||
| 138 | } | ||
| 139 | |||
| 140 | ✗ | return valueVector; | |
| 141 | ✗ | } | |
| 142 | |||
| 143 | ✗ | boost::optional<std::vector<std::string>> PreparedStatement::execAndReturnVectorOfString() const { | |
| 144 | ✗ | boost::optional<std::string> value; | |
| 145 | ✗ | boost::optional<std::vector<std::string>> valueVector; | |
| 146 | ✗ | if (m_db) { | |
| 147 | ✗ | int code = SQLITE_OK; | |
| 148 | |||
| 149 | ✗ | while ((code != SQLITE_DONE) && (code != SQLITE_BUSY) && (code != SQLITE_ERROR) && (code != SQLITE_MISUSE)) //loop until SQLITE_DONE | |
| 150 | { | ||
| 151 | ✗ | if (!valueVector) { | |
| 152 | ✗ | valueVector = std::vector<std::string>(); | |
| 153 | } | ||
| 154 | |||
| 155 | ✗ | code = sqlite3_step(m_statement); | |
| 156 | ✗ | if (code == SQLITE_ROW) { | |
| 157 | ✗ | value = columnText(sqlite3_column_text(m_statement, 0)); | |
| 158 | ✗ | valueVector->push_back(*value); | |
| 159 | } else // i didn't get a row. something is wrong so set the exit condition. | ||
| 160 | { // should never get here since i test for all documented return states above | ||
| 161 | ✗ | code = SQLITE_DONE; | |
| 162 | } | ||
| 163 | |||
| 164 | } // end loop | ||
| 165 | } | ||
| 166 | ✗ | return valueVector; | |
| 167 | ✗ | } | |
| 168 | |||
| 169 | 17286 | PreparedStatement::PreparedStatement(PreparedStatement::InternalConstructor, const std::string& t_stmt, sqlite3* t_db, bool t_transaction) | |
| 170 | 17286 | : m_db(t_db), m_statement(nullptr), m_transaction(t_transaction) { | |
| 171 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17286 times.
|
17286 | if (m_transaction) { |
| 172 | ✗ | sqlite3_exec(m_db, "BEGIN", nullptr, nullptr, nullptr); | |
| 173 | } | ||
| 174 | |||
| 175 | 17286 | int code = sqlite3_prepare_v2(m_db, t_stmt.c_str(), t_stmt.size(), &m_statement, nullptr); | |
| 176 | |||
| 177 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17286 times.
|
17286 | if (!m_statement) { |
| 178 | ✗ | int extendedErrorCode = sqlite3_extended_errcode(m_db); | |
| 179 | ✗ | const char* err = sqlite3_errmsg(m_db); | |
| 180 | ✗ | std::string errMsg = err; | |
| 181 | ✗ | throw std::runtime_error("Error creating prepared statement: " + t_stmt + " with error code " + std::to_string(code) + ", extended code " | |
| 182 | ✗ | + std::to_string(extendedErrorCode) + ", errmsg: " + errMsg); | |
| 183 | ✗ | } | |
| 184 | 17286 | } | |
| 185 | |||
| 186 | 17156 | int PreparedStatement::get_sqlite3_bind_parameter_count(sqlite3_stmt* statement) { | |
| 187 | 17156 | return sqlite3_bind_parameter_count(statement); | |
| 188 | } | ||
| 189 | } // namespace openstudio | ||
| 190 |