GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 72.2% 78 / 0 / 108
Functions: 85.7% 12 / 0 / 14
Branches: 53.1% 60 / 0 / 113

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 17497 PreparedStatement::~PreparedStatement() {
12
1/2
✓ Branch 0 taken 17497 times.
✗ Branch 1 not taken.
17497 if (m_statement) {
13 17497 sqlite3_finalize(m_statement);
14 }
15
16
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 17494 times.
17497 if (m_transaction) {
17 3 sqlite3_exec(m_db, "COMMIT", nullptr, nullptr, nullptr);
18 }
19 17497 }
20
21 60219 bool PreparedStatement::bind(int position, const std::string& t_str) {
22
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 60219 times.
60219 if (sqlite3_bind_text(m_statement, position, t_str.c_str(), t_str.size(), SQLITE_TRANSIENT) != SQLITE_OK) {
23 ✗ return false;
24 }
25 60219 return true;
26 }
27
28 52772 bool PreparedStatement::bind(int position, int val) {
29
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 52772 times.
52772 if (sqlite3_bind_int(m_statement, position, val) != SQLITE_OK) {
30 ✗ return false;
31 }
32 52772 return true;
33 }
34
35 8784 bool PreparedStatement::bind(int position, unsigned int val) {
36
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8784 times.
8784 if (sqlite3_bind_int(m_statement, position, static_cast<int>(val)) != SQLITE_OK) {
37 ✗ return false;
38 }
39 8784 return true;
40 }
41
42 61 bool PreparedStatement::bind(int position, double val) {
43
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
61 if (sqlite3_bind_double(m_statement, position, val) != SQLITE_OK) {
44 ✗ return false;
45 }
46 61 return true;
47 }
48
49 8805 int PreparedStatement::execute() {
50 8805 int code = sqlite3_step(m_statement);
51 8805 sqlite3_reset(m_statement);
52 8805 return code;
53 }
54
55 17344 boost::optional<double> PreparedStatement::execAndReturnFirstDouble() const {
56 17344 boost::optional<double> value;
57
1/2
✓ Branch 0 taken 17344 times.
✗ Branch 1 not taken.
17344 if (m_db) {
58
1/1
✓ Branch 1 taken 17344 times.
17344 int code = sqlite3_step(m_statement);
59
2/2
✓ Branch 0 taken 1552 times.
✓ Branch 1 taken 15792 times.
17344 if (code == SQLITE_ROW) {
60
2/2
✓ Branch 1 taken 1552 times.
✓ Branch 4 taken 1552 times.
1552 value = sqlite3_column_double(m_statement, 0);
61 }
62 }
63 17344 return value;
64 }
65
66 130 boost::optional<int> PreparedStatement::execAndReturnFirstInt() const {
67 130 boost::optional<int> value;
68
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 if (m_db) {
69
1/1
✓ Branch 1 taken 130 times.
130 int code = sqlite3_step(m_statement);
70
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 if (code == SQLITE_ROW) {
71
2/2
✓ Branch 1 taken 130 times.
✓ Branch 4 taken 130 times.
130 value = sqlite3_column_int(m_statement, 0);
72 }
73 }
74 130 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 1 boost::optional<std::vector<std::string>> PreparedStatement::execAndReturnVectorOfString() const {
144 1 boost::optional<std::string> value;
145 1 boost::optional<std::vector<std::string>> valueVector;
146
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (m_db) {
147 1 int code = SQLITE_OK;
148
149
5/8
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
2 while ((code != SQLITE_DONE) && (code != SQLITE_BUSY) && (code != SQLITE_ERROR) && (code != SQLITE_MISUSE)) //loop until SQLITE_DONE
150 {
151
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 if (!valueVector) {
152
1/1
✓ Branch 2 taken 1 time.
1 valueVector = std::vector<std::string>();
153 }
154
155
1/1
✓ Branch 1 taken 1 time.
1 code = sqlite3_step(m_statement);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 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 1 code = SQLITE_DONE;
162 }
163
164 } // end loop
165 }
166 2 return valueVector;
167 1 }
168
169 17498 PreparedStatement::PreparedStatement(PreparedStatement::InternalConstructor, const std::string& t_stmt, sqlite3* t_db, bool t_transaction)
170 17498 : m_db(t_db), m_statement(nullptr), m_transaction(t_transaction) {
171
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 17495 times.
17498 if (m_transaction) {
172 3 sqlite3_exec(m_db, "BEGIN", nullptr, nullptr, nullptr);
173 }
174
175 17498 int code = sqlite3_prepare_v2(m_db, t_stmt.c_str(), t_stmt.size(), &m_statement, nullptr);
176
177
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 17497 times.
17498 if (!m_statement) {
178
1/1
✓ Branch 1 taken 1 time.
1 int extendedErrorCode = sqlite3_extended_errcode(m_db);
179
1/1
✓ Branch 1 taken 1 time.
1 const char* err = sqlite3_errmsg(m_db);
180
1/1
✓ Branch 1 taken 1 time.
1 std::string errMsg = err;
181
4/4
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
2 throw std::runtime_error("Error creating prepared statement: " + t_stmt + " with error code " + std::to_string(code) + ", extended code "
182
4/4
✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 12 taken 1 time.
3 + std::to_string(extendedErrorCode) + ", errmsg: " + errMsg);
183 1 }
184 17497 }
185
186 17160 int PreparedStatement::get_sqlite3_bind_parameter_count(sqlite3_stmt* statement) {
187 17160 return sqlite3_bind_parameter_count(statement);
188 }
189 } // namespace openstudio
190