-
Notifications
You must be signed in to change notification settings - Fork 209
Move deparser to dedicated file without pg_query dependencies #192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lfittl
merged 1 commit into
pganalyze:15-latest
from
yrashk:separate-deparser-into-dedicated-unit
Jun 10, 2023
Merged
Move deparser to dedicated file without pg_query dependencies #192
lfittl
merged 1 commit into
pganalyze:15-latest
from
yrashk:separate-deparser-into-dedicated-unit
Jun 10, 2023
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
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.
d065186 to
fbd9abc
Compare
Contributor
Author
|
Rebased |
Contributor
Author
|
Bump |
lfittl
approved these changes
Jun 10, 2023
Member
lfittl
left a comment
There was a problem hiding this 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);
-}
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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