Skip to content

Commit d0c521d

Browse files
committed
add deparse sequence options list
1 parent 5033a99 commit d0c521d

File tree

6 files changed

+87
-1
lines changed

6 files changed

+87
-1
lines changed

parser/include/pg_query.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ PgQueryDeparseResult pg_query_deparse_protobuf(PgQueryProtobuf parse_tree);
121121
PgQueryDeparseResult pg_query_deparse_expr_protobuf(PgQueryProtobuf parse_tree);
122122
PgQueryDeparseResult pg_query_deparse_typename_protobuf(PgQueryProtobuf parse_tree);
123123
PgQueryDeparseResult pg_query_deparse_reloptions_protobuf(PgQueryProtobuf buf);
124+
PgQueryDeparseResult pg_query_deparse_parenthesized_seq_opt_list_protobuf(PgQueryProtobuf buf);
124125

125126
void pg_query_free_normalize_result(PgQueryNormalizeResult result);
126127
void pg_query_free_scan_result(PgQueryScanResult result);

parser/parser.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ PgQueryDeparseResult pg_query_deparse_reloptions_protobuf_direct_args(void* data
4040
return pg_query_deparse_reloptions_protobuf(p);
4141
}
4242
43+
// Avoid complexities dealing with C structs in Go
44+
PgQueryDeparseResult pg_query_deparse_parenthesized_seq_opt_list(void* data, unsigned int len) {
45+
PgQueryProtobuf p;
46+
p.data = (char *) data;
47+
p.len = len;
48+
return pg_query_deparse_parenthesized_seq_opt_list_protobuf(p);
49+
}
50+
4351
// Avoid inconsistent type behaviour in xxhash library
4452
uint64_t pg_query_hash_xxh3_64(void *data, size_t len, size_t seed) {
4553
return XXH3_64bits_withSeed(data, len, seed);
@@ -216,6 +224,24 @@ func DeparseRelOptionsFromProtobuf(input []byte) (result string, err error) {
216224
return
217225
}
218226

227+
func DeparseParenthesizedSeqOptList(input []byte) (result string, err error) {
228+
inputC := C.CBytes(input)
229+
defer C.free(inputC)
230+
231+
resultC := C.pg_query_deparse_reloptions_protobuf_direct_args(inputC, C.uint(len(input)))
232+
233+
defer C.pg_query_free_deparse_result(resultC)
234+
235+
if resultC.error != nil {
236+
err = newPgQueryError(resultC.error)
237+
return
238+
}
239+
240+
result = C.GoString(resultC.query)
241+
242+
return
243+
}
244+
219245
// ParsePlPgSqlToJSON - Parses the given PL/pgSQL function statement into a parse tree (JSON format)
220246
func ParsePlPgSqlToJSON(input string) (result string, err error) {
221247
inputC := C.CString(input)

parser/pg_query_deparse.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,52 @@ PgQueryDeparseResult pg_query_deparse_reloptions_protobuf(PgQueryProtobuf buf)
192192
return result;
193193
}
194194

195+
PgQueryDeparseResult pg_query_deparse_parenthesized_seq_opt_list_protobuf(PgQueryProtobuf buf)
196+
{
197+
PgQueryDeparseResult result = {0};
198+
StringInfoData str;
199+
MemoryContext ctx;
200+
List *list;
201+
202+
ctx = pg_query_enter_memory_context();
203+
204+
PG_TRY();
205+
{
206+
list = pg_query_protobuf_to_list(buf);
207+
208+
initStringInfo(&str);
209+
210+
deparseOptParenthesizedSeqOptList(&str, list);
211+
212+
result.query = strdup(str.data);
213+
}
214+
PG_CATCH();
215+
{
216+
ErrorData* error_data;
217+
PgQueryError* error;
218+
219+
MemoryContextSwitchTo(ctx);
220+
error_data = CopyErrorData();
221+
222+
// Note: This is intentionally malloc so exiting the memory context doesn't free this
223+
error = malloc(sizeof(PgQueryError));
224+
error->message = strdup(error_data->message);
225+
error->filename = strdup(error_data->filename);
226+
error->funcname = strdup(error_data->funcname);
227+
error->context = NULL;
228+
error->lineno = error_data->lineno;
229+
error->cursorpos = error_data->cursorpos;
230+
231+
result.error = error;
232+
FlushErrorState();
233+
}
234+
PG_END_TRY();
235+
236+
pg_query_exit_memory_context(ctx);
237+
238+
return result;
239+
}
240+
195241
void pg_query_free_deparse_result(PgQueryDeparseResult result)
196242
{
197243
if (result.error) {

parser/postgres_deparse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ static void deparseOptSeqOptList(StringInfo str, List *options)
678678
}
679679

680680
// "OptParenthesizedSeqOptList" in gram.y
681-
static void deparseOptParenthesizedSeqOptList(StringInfo str, List *options)
681+
void deparseOptParenthesizedSeqOptList(StringInfo str, List *options)
682682
{
683683
if (list_length(options) > 0)
684684
{

parser/postgres_deparse.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ extern void deparseRawStmt(StringInfo str, RawStmt *raw_stmt);
88
extern void deparseExpr(StringInfo str, Node *node);
99
extern void deparseTypeName(StringInfo str, TypeName *type_name);
1010
extern void deparseRelOptions(StringInfo str, List *l);
11+
extern void deparseOptParenthesizedSeqOptList(StringInfo str, List *l);
1112

1213
#endif

pg_query.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ func DeparseRelOptions(relOptions []*Node) (output string, err error) {
8080
return
8181
}
8282

83+
func DeparseParenthesizedSeqOptList(relOptions []*Node) (output string, err error) {
84+
list := MakeListNode(relOptions)
85+
86+
protobufNode, err := proto.Marshal(list.GetList())
87+
if err != nil {
88+
return
89+
}
90+
91+
output, err = parser.DeparseParenthesizedSeqOptList(protobufNode)
92+
return
93+
}
94+
8395
// ParsePlPgSqlToJSON - Parses the given PL/pgSQL function statement into a parse tree (JSON format)
8496
func ParsePlPgSqlToJSON(input string) (result string, err error) {
8597
return parser.ParsePlPgSqlToJSON(input)

0 commit comments

Comments
 (0)