Impl GetDbNames, GetDbTalbes and ExecDbQuery

This commit is contained in:
Changhua
2023-02-17 22:06:40 +08:00
parent 33a5ed4033
commit 95ce4578bf
7 changed files with 277 additions and 56 deletions
+17
View File
@@ -2,6 +2,7 @@
#include <map>
#include <string>
#include <vector>
using namespace std;
@@ -16,3 +17,19 @@ typedef struct {
string province;
string city;
} RpcContact_t;
typedef vector<string> DbNames_t;
typedef struct {
string name;
string sql;
} DbTable_t;
typedef vector<DbTable_t> DbTables_t;
typedef struct {
int32_t type;
string column;
vector<uint8_t> content;
} DbField_t;
typedef vector<DbField_t> DbRow_t;
typedef vector<DbRow_t> DbRows_t;
+118
View File
@@ -23,6 +23,7 @@ bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void *const *a
const char *str = (const char *)*arg;
if (!pb_encode_tag_for_field(stream, field)) {
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(stream));
return false;
}
@@ -35,11 +36,24 @@ bool decode_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
size_t len = stream->bytes_left;
str->resize(len);
if (!pb_read(stream, (uint8_t *)str->data(), len)) {
LOG_ERROR("Decoding failed: {}", PB_GET_ERROR(stream));
return false;
}
return true;
}
bool encode_bytes(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
{
vector<uint8_t> *v = (vector<uint8_t> *)*arg;
if (!pb_encode_tag_for_field(stream, field)) {
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(stream));
return false;
}
return pb_encode_string(stream, (uint8_t *)v->data(), v->size());
}
bool encode_types(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
{
MsgTypes_t *m = (MsgTypes_t *)*arg;
@@ -51,10 +65,12 @@ bool encode_types(pb_ostream_t *stream, const pb_field_t *field, void *const *ar
message.value.arg = (void *)it->second.c_str();
if (!pb_encode_tag_for_field(stream, field)) {
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(stream));
return false;
}
if (!pb_encode_submessage(stream, MsgTypes_TypesEntry_fields, &message)) {
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(stream));
return false;
}
}
@@ -90,10 +106,112 @@ bool encode_contacts(pb_ostream_t *stream, const pb_field_t *field, void *const
message.gender = (*it).gender;
if (!pb_encode_tag_for_field(stream, field)) {
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(stream));
return false;
}
if (!pb_encode_submessage(stream, RpcContact_fields, &message)) {
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(stream));
return false;
}
}
return true;
}
bool encode_dbnames(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
{
vector<string> *v = (vector<string> *)*arg;
DbNames message = DbNames_init_default;
for (auto it = v->begin(); it != v->end(); it++) {
message.names.funcs.encode = &encode_string;
message.names.arg = (void *)(*it).c_str();
if (!pb_encode_tag_for_field(stream, field)) {
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(stream));
return false;
}
if (!pb_encode_submessage(stream, DbNames_fields, &message)) {
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(stream));
return false;
}
}
return true;
}
bool encode_tables(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
{
DbTables_t *v = (DbTables_t *)*arg;
DbTable message = DbTable_init_default;
for (auto it = v->begin(); it != v->end(); it++) {
message.name.funcs.encode = &encode_string;
message.name.arg = (void *)(*it).name.c_str();
message.sql.funcs.encode = &encode_string;
message.sql.arg = (void *)(*it).sql.c_str();
if (!pb_encode_tag_for_field(stream, field)) {
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(stream));
return false;
}
if (!pb_encode_submessage(stream, DbTable_fields, &message)) {
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(stream));
return false;
}
}
return true;
}
static bool encode_fields(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
{
DbRow_t *v = (DbRow_t *)*arg;
DbField message = DbField_init_default;
for (auto it = v->begin(); it != v->end(); it++) {
message.type = (*it).type;
message.column.arg = (void *)(*it).column.c_str();
message.column.funcs.encode = &encode_string;
message.content.arg = (void *)&(*it).content;
message.content.funcs.encode = &encode_bytes;
if (!pb_encode_tag_for_field(stream, field)) {
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(stream));
return false;
}
if (!pb_encode_submessage(stream, DbField_fields, &message)) {
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(stream));
return false;
}
}
return true;
}
bool encode_rows(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
{
DbRows_t *v = (DbRows_t *)*arg;
DbRow message = DbRow_init_default;
for (auto it = v->begin(); it != v->end(); it++) {
message.fields.arg = (void *)&(*it);
message.fields.funcs.encode = &encode_fields;
if (!pb_encode_tag_for_field(stream, field)) {
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(stream));
return false;
}
if (!pb_encode_submessage(stream, DbRow_fields, &message)) {
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(stream));
return false;
}
}
+3
View File
@@ -8,3 +8,6 @@ bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void *const *a
bool decode_string(pb_istream_t *stream, const pb_field_t *field, void **arg);
bool encode_types(pb_ostream_t *stream, const pb_field_t *field, void *const *arg);
bool encode_contacts(pb_ostream_t *stream, const pb_field_t *field, void *const *arg);
bool encode_dbnames(pb_ostream_t *stream, const pb_field_t *field, void *const *arg);
bool encode_tables(pb_ostream_t *stream, const pb_field_t *field, void *const *arg);
bool encode_rows(pb_ostream_t *stream, const pb_field_t *field, void *const *arg);
+4
View File
@@ -3,3 +3,7 @@
* fallback_type:FT_POINTER
MsgTypes* fallback_type:FT_CALLBACK
RpcContact* fallback_type:FT_CALLBACK
DbNames* fallback_type:FT_CALLBACK
DbTable* fallback_type:FT_CALLBACK
DbField* fallback_type:FT_CALLBACK
DbRow* fallback_type:FT_CALLBACK