Merge branch '2.3' into develop
This commit is contained in:
@ -1668,19 +1668,23 @@ const char* gw_dcb_state2string(dcb_state_t state)
|
||||
*/
|
||||
void dcb_printf(DCB* dcb, const char* fmt, ...)
|
||||
{
|
||||
GWBUF* buf;
|
||||
va_list args;
|
||||
|
||||
if ((buf = gwbuf_alloc(10240)) == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
va_start(args, fmt);
|
||||
vsnprintf((char*)GWBUF_DATA(buf), 10240, fmt, args);
|
||||
int n = vsnprintf(nullptr, 0, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
buf->end = (void*)((char*)GWBUF_DATA(buf) + strlen((char*)GWBUF_DATA(buf)));
|
||||
dcb->func.write(dcb, buf);
|
||||
GWBUF* buf = gwbuf_alloc(n + 1);
|
||||
|
||||
if (buf)
|
||||
{
|
||||
va_start(args, fmt);
|
||||
vsnprintf((char*)GWBUF_DATA(buf), n + 1, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
// Remove the trailing null character
|
||||
GWBUF_RTRIM(buf, 1);
|
||||
dcb->func.write(dcb, buf);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -16,6 +16,7 @@
|
||||
|
||||
#include <numeric>
|
||||
|
||||
#include <maxscale/alloc.h>
|
||||
#include <maxscale/resultset.hh>
|
||||
#include <maxscale/buffer.hh>
|
||||
#include <maxscale/dcb.hh>
|
||||
@ -206,3 +207,24 @@ void ResultSet::write(DCB* dcb)
|
||||
|
||||
mysql_send_eof(dcb, seqno);
|
||||
}
|
||||
|
||||
void ResultSet::write_as_json(DCB* dcb)
|
||||
{
|
||||
json_t* arr = json_array();
|
||||
|
||||
for (const auto& row : m_rows)
|
||||
{
|
||||
json_t* obj = json_object();
|
||||
|
||||
for (size_t i = 0; i < row.size(); i++)
|
||||
{
|
||||
json_object_set_new(obj, m_columns[i].c_str(), json_string(row[i].c_str()));
|
||||
}
|
||||
|
||||
json_array_append_new(arr, obj);
|
||||
}
|
||||
|
||||
char* js = json_dumps(arr, JSON_INDENT(4));
|
||||
dcb_printf(dcb, "%s", js);
|
||||
MXS_FREE(js);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user