Skip to content

Conversation

@yrashk
Copy link
Contributor

@yrashk yrashk commented May 26, 2023

This enables external projects that are interested to work with a raw parsetree to use the libpg_query deparser, without pulling in the main pg_query headers.

Fixes #180.


Supersedes #183

@lfittl
Copy link
Member

lfittl commented Jun 6, 2023

@yrashk Sorry for the delay (last week got busier than expected with pgcon) -- if you could rebase this one more time (just merged a smaller change from #193 that was conflicting), I promise that we'll get this done now :)

This enables external projects that are interested to work with a raw
parsetree to use the libpg_query deparser, without pulling in the
main pg_query headers.

Fixes pganalyze#180.
@yrashk yrashk force-pushed the separate-deparser-into-dedicated-unit branch from d065186 to fbd9abc Compare June 6, 2023 19:00
@yrashk
Copy link
Contributor Author

yrashk commented Jun 6, 2023

Rebased

@yrashk
Copy link
Contributor Author

yrashk commented Jun 8, 2023

Bump

Copy link
Member

@lfittl lfittl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Thanks for rebasing!

For future reference, here is the diff from the old src/pg_query_deparse.c to the new src/postgres_deparse.c:

lfittl@starfish libpg_query % git diff 15-latest:./src/pg_query_deparse.c ./src/postgres_deparse.c
diff --git a/src/pg_query_deparse.c b/src/postgres_deparse.c
index f37e5a0..8687a9d 100644
--- a/src/pg_query_deparse.c
+++ b/src/postgres_deparse.c
@@ -1,7 +1,4 @@
-#include "pg_query.h"
-#include "pg_query_internal.h"
-#include "pg_query_readfuncs.h"
-
+#include "postgres.h"
 #include "catalog/index.h"
 #include "catalog/pg_am.h"
 #include "catalog/pg_attribute.h"
@@ -141,7 +138,7 @@ static void deparseSelectStmt(StringInfo str, SelectStmt *stmt);
 static void deparseIntoClause(StringInfo str, IntoClause *into_clause);
 static void deparseRangeVar(StringInfo str, RangeVar *range_var, DeparseNodeContext context);
 static void deparseResTarget(StringInfo str, ResTarget *res_target, DeparseNodeContext context);
-static void deparseRawStmt(StringInfo str, RawStmt *raw_stmt);
+void deparseRawStmt(StringInfo str, RawStmt *raw_stmt);
 static void deparseAlias(StringInfo str, Alias *alias);
 static void deparseWindowDef(StringInfo str, WindowDef* window_def);
 static void deparseColumnRef(StringInfo str, ColumnRef* column_ref);
@@ -2380,11 +2377,11 @@ static void deparseRangeVar(StringInfo str, RangeVar *range_var, DeparseNodeCont
        removeTrailingSpace(str);
 }
 
-static void deparseRawStmt(StringInfo str, RawStmt *raw_stmt)
+void deparseRawStmt(StringInfo str, RawStmt *raw_stmt)
 {
        if (raw_stmt->stmt == NULL)
                elog(ERROR, "deparse error in deparseRawStmt: RawStmt with empty Stmt");
-       
+
        deparseStmt(str, raw_stmt->stmt);
 }
 
@@ -10648,62 +10645,3 @@ static void deparseStmt(StringInfo str, Node *node)
                        elog(ERROR, "deparse: unsupported top-level node type: %u", nodeTag(node));
        }
 }
-
-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);
-}

@lfittl lfittl merged commit ed0fd87 into pganalyze:15-latest Jun 10, 2023
@lfittl lfittl mentioned this pull request Jun 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Making query deparsing indepenently usable

2 participants