json get
此函数将单个对象导出到输出流。
运行时会调用应用程序定义的处理程序来管理输出流。
请注意,只有在将 -json
标志传递给 mcocomp 实用程序时,才会生成此函数。 应用程序有责任以适合流式传输二进制数据的模式打开输出流,并确保目的地有足够的空间来容纳对象。
MCO_RET classname_json_get(
/*IN*/ classname *handle,
void *stream_handle,
mco_stream_write output_stream_writer
);
参数
handle
classname *
类句柄类型变量的地址。
stream_handle
void
输出流句柄。
output_stream_writer
mco_stream_write
运行时用于格式化输出的处理函数。
返回
MCO_S_OK
数据库数据导出成功。
MCO_E_WRITE_STREAM
写入输出流错误。
示例
/* Stream writer function matching the mco_stream_write prototype */
mco_size_sig_t file_writer(void *stream_handle /* FILE * */, const void *from, mco_size_t nbytes)
{
return fwrite(from, 1, nbytes, (FILE *)stream_handle);
}
/* Function to export a single object */
void export_obj()
{
mco_trans_h t;
mco_cursor_t cur;
<classname> obj;
FILE *f = fopen("obj.json", "wb");
/* Use the cursor to locate an object */
mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t);
<classname>_<indexname>_index_cursor(t, &cur);
mco_cursor_first(t, &cur);
<classname>_from_cursor(t, &cur, &obj);
/* Export the object */
<classname>_json_get(&obj, f, file_writer);
mco_cursor_close(t, &cur);
mco_trans_rollback(t);
fclose(f);
}