55#include < cmath>
66#include < ctime>
77#include < iostream>
8+ #include < optional>
89#include < map>
910#include < sqlite3.h>
1011#include < sstream>
@@ -143,8 +144,8 @@ SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::str
143144 std::string column_name;
144145 ColumnType column_declared_type;
145146 SQLiteQueryResultRow row;
146- auto results = std::make_unique<SQLiteQueryResults>() ;
147- auto metadata = new SQLiteQueryTableMetadata () ;
147+ SQLiteQueryResults results;
148+ std::optional<SQLiteQueryTableMetadata> metadata = std:: nullopt ;
148149
149150 while (isConsuming) {
150151 result = sqlite3_step (statement);
@@ -192,7 +193,7 @@ SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::str
192193 }
193194 i++;
194195 }
195- results-> push_back (std::move (row));
196+ results. push_back (std::move (row));
196197 break ;
197198 case SQLITE_DONE:
198199 i = 0 ;
@@ -202,6 +203,10 @@ SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::str
202203 const char * tp = sqlite3_column_decltype (statement, i);
203204 column_declared_type = mapSQLiteTypeToColumnType (tp);
204205 auto columnMeta = SQLiteQueryColumnMetadata (std::move (column_name), std::move (column_declared_type), i);
206+
207+ if (!metadata) {
208+ metadata = std::make_optional<SQLiteQueryTableMetadata>();
209+ }
205210 metadata->insert ({column_name, columnMeta});
206211 i++;
207212 }
@@ -221,16 +226,11 @@ SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::str
221226
222227 int rowsAffected = sqlite3_changes (db);
223228 long long latestInsertRowId = sqlite3_last_insert_rowid (db);
224- auto meta = std::make_unique<std::optional<SQLiteQueryTableMetadata>>(
225- metadata && metadata->size () > 0
226- ? std::make_optional (std::move (*metadata))
227- : std::nullopt
228- );
229229 return {
230230 .rowsAffected = rowsAffected,
231231 .insertId = static_cast <double >(latestInsertRowId),
232232 .results = std::move (results),
233- .metadata = std::move (meta )
233+ .metadata = std::move (metadata )
234234 };
235235}
236236
0 commit comments