Skip to content

Commit 1185546

Browse files
committed
Serializers into JSON for MySQL API objects
1 parent 0fde16f commit 1185546

File tree

4 files changed

+82
-23
lines changed

4 files changed

+82
-23
lines changed

src/replica/mysql/DatabaseMySQL.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "lsst/log/Log.h"
4444

4545
using namespace std;
46+
using json = nlohmann::json;
4647

4748
namespace {
4849

@@ -495,6 +496,30 @@ void Connection::exportField(ProtocolResponseSqlField* ptr, size_t idx) const {
495496
ptr->set_type(field.type);
496497
}
497498

499+
json Connection::fieldsToJson() const {
500+
_assertQueryContext();
501+
502+
json result;
503+
for (size_t i = 0; i < _numFields; ++i) {
504+
auto&& field = _fields[i];
505+
json f;
506+
f["name"] = field.name;
507+
f["org_name"] = field.org_name;
508+
f["table"] = field.table;
509+
f["org_table"] = field.org_table;
510+
f["db"] = field.db;
511+
f["catalog"] = field.catalog;
512+
f["def"] = field.def;
513+
f["length"] = field.length;
514+
f["max_length"] = field.max_length;
515+
f["flags"] = field.flags;
516+
f["decimals"] = field.decimals;
517+
f["type"] = field.type;
518+
result.push_back(move(f));
519+
}
520+
return result;
521+
}
522+
498523
bool Connection::next(Row& row) {
499524
string const context = "Connection[" + to_string(_id) + "]::" + string(__func__) +
500525
"(_inTransaction=" + to_string(_inTransaction ? 1 : 0) + ") ";

src/replica/mysql/DatabaseMySQL.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252

5353
// Third party headers
5454
#include <mysql/mysql.h>
55+
#include "nlohmann/json.hpp"
5556

5657
// Qserv headers
5758
#include "replica/mysql/DatabaseMySQLExceptions.h"
@@ -422,6 +423,20 @@ class Connection : public std::enable_shared_from_this<Connection> {
422423
*/
423424
void exportField(ProtocolResponseSqlField* ptr, size_t idx) const;
424425

426+
/**
427+
* Convert the current result set into a JSON object.
428+
*
429+
* @note The method can be called only upon a successful completion of a query
430+
* which has a result set. Otherwise it will throw an exception.
431+
*
432+
* @see Connection::hasResult
433+
*
434+
* @return a JSON object representing the current result set
435+
* @throw std::logic_error if no SQL statement has ever been executed, or
436+
* if the last query failed.
437+
*/
438+
nlohmann::json fieldsToJson() const;
439+
425440
/**
426441
* Move the iterator to the next (first) row of the current result set
427442
* and if the iterator is not beyond the last row then initialize an object

src/replica/mysql/DatabaseMySQLRow.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030

3131
// Qserv headers
3232
#include "replica/proto/protocol.pb.h"
33+
#include "util/String.h"
3334

3435
// LSST headers
3536
#include "lsst/log/Log.h"
3637

3738
using namespace std;
39+
using json = nlohmann::json;
3840

3941
namespace {
4042

@@ -204,4 +206,28 @@ void Row::exportRow(ProtocolResponseSqlRow* ptr) const {
204206
}
205207
}
206208

209+
json Row::toJson() const {
210+
string const context = "Row::" + string(__func__) + " ";
211+
if (not isValid()) {
212+
throw logic_error(context + "the object is not valid");
213+
}
214+
json result = json::object();
215+
result["cells"] = json::array();
216+
result["nulls"] = json::array();
217+
json& cellsJson = result["cells"];
218+
json& nullsJson = result["nulls"];
219+
for (Cell const& cell : _index2cell) {
220+
char const* ptr = cell.first;
221+
size_t const length = cell.second;
222+
if (nullptr == ptr) {
223+
cellsJson.push_back(string());
224+
nullsJson.push_back(1);
225+
} else {
226+
cellsJson.push_back(util::String::toHex(ptr, length));
227+
nullsJson.push_back(0);
228+
}
229+
}
230+
return result;
231+
}
232+
207233
} // namespace lsst::qserv::replica::database::mysql

src/replica/mysql/DatabaseMySQLRow.h

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#include <string>
3838
#include <vector>
3939

40+
// Third party headers
41+
#include "nlohmann/json.hpp"
42+
4043
// Qserv headers
4144
#include "replica/mysql/DatabaseMySQLExceptions.h"
4245

@@ -110,10 +113,7 @@ class Row {
110113
*/
111114
Row();
112115

113-
/// Copy constructor
114116
Row(Row const& rhs) = default;
115-
116-
/// The Assignment operator
117117
Row& operator=(Row const& rhs) = default;
118118

119119
~Row() = default;
@@ -133,8 +133,6 @@ class Row {
133133
// There are two ways to access the values: either by a relative
134134
// index of a column in a result set, or by the name of the column.
135135
// The second method has some extra (though, minor) overhead.
136-
//
137-
// @see class Row
138136

139137
template <typename T>
140138
T getAs(size_t columnIdx) const {
@@ -204,41 +202,36 @@ class Row {
204202
// Other types
205203

206204
bool get(size_t columnIdx, bool& value) const;
207-
208205
bool get(std::string const& columnName, bool& value) const;
209206

210207
/**
211-
* @return
212-
* reference to the data cell for the column
213-
*
214-
* @param columnIdx
215-
* the index of a column
208+
* @param columnIdx the index of a column
209+
* @return reference to the data cell for the column
216210
*/
217211
Cell const& getDataCell(size_t columnIdx) const;
218212

219213
/**
220-
* @return
221-
* reference to the data cell for the column
222-
*
223-
* @param columnName
224-
* the name of a column
214+
* @param columnName the name of a column
215+
* @return reference to the data cell for the column
225216
*/
226217
Cell const& getDataCell(std::string const& columnName) const;
227218

228219
/**
229220
* Fill a Protobuf object representing a row.
230-
*
231-
* @param ptr
232-
* a valid pointer to the Protobuf object to be populated.
233-
*
234-
* @param std::invalid_argument
235-
* if the input pointer is 0
221+
* @param ptr a valid pointer to the Protobuf object to be populated.
222+
* @param std::invalid_argument if the input pointer is 0
236223
*/
237224
void exportRow(ProtocolResponseSqlRow* ptr) const;
238225

226+
/**
227+
* Convert the current row into a JSON object.
228+
* @return a JSON object representing the current row
229+
*/
230+
nlohmann::json toJson() const;
231+
239232
private:
240233
/**
241-
* Mapping column names to the indexes
234+
* Mapping column names to the indexes
242235
*
243236
* @note
244237
* If the pointer is set to 'nullptr' then the object is not

0 commit comments

Comments
 (0)