-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathpg_query_deparse.c
64 lines (51 loc) · 1.37 KB
/
pg_query_deparse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "pg_query.h"
#include "pg_query_internal.h"
#include "pg_query_readfuncs.h"
#include "postgres_deparse.h"
PgQueryDeparseResult pg_query_deparse_protobuf(PgQueryProtobuf parse_tree)
{
PgQueryDeparseResult result = {0};
StringInfoData str;
MemoryContext ctx;
List *stmts;
ListCell *lc;
ctx = pg_query_enter_memory_context();
PG_TRY();
{
stmts = pg_query_protobuf_to_nodes(parse_tree);
initStringInfo(&str);
foreach(lc, stmts) {
deparseRawStmt(&str, castNode(RawStmt, lfirst(lc)));
if (lnext(stmts, lc))
appendStringInfoString(&str, "; ");
}
result.query = strdup(str.data);
}
PG_CATCH();
{
ErrorData* error_data;
PgQueryError* error;
MemoryContextSwitchTo(ctx);
error_data = CopyErrorData();
// Note: This is intentionally malloc so exiting the memory context doesn't free this
error = malloc(sizeof(PgQueryError));
error->message = strdup(error_data->message);
error->filename = strdup(error_data->filename);
error->funcname = strdup(error_data->funcname);
error->context = NULL;
error->lineno = error_data->lineno;
error->cursorpos = error_data->cursorpos;
result.error = error;
FlushErrorState();
}
PG_END_TRY();
pg_query_exit_memory_context(ctx);
return result;
}
void pg_query_free_deparse_result(PgQueryDeparseResult result)
{
if (result.error) {
pg_query_free_error(result.error);
}
free(result.query);
}