-
Notifications
You must be signed in to change notification settings - Fork 0
WIP pretty printing support #3
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
base: dev/PG-151
Are you sure you want to change the base?
Changes from all commits
b524b4c
91b8595
af01afa
d62732a
ed954dd
ec1d489
626f876
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -94,6 +94,10 @@ | |||||||||||||||||||||||||||
| ((pretty) ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) \ | ||||||||||||||||||||||||||||
| : PRETTYFLAG_INDENT) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| #define GET_DDL_PRETTY_FLAGS(pretty) \ | ||||||||||||||||||||||||||||
| ((pretty) ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) \ | ||||||||||||||||||||||||||||
| : 0) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
Comment on lines
+97
to
+100
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like adding the GET_DDL_PRETTY_FLAGS, but maybe it's just me. If you have a strong feeling, let it be as it is |
||||||||||||||||||||||||||||
| /* Default line length for pretty-print wrapping: 0 means wrap always */ | ||||||||||||||||||||||||||||
| #define WRAP_COLUMN_DEFAULT 0 | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -546,6 +550,11 @@ static void get_json_table_nested_columns(TableFunc *tf, JsonTablePlan *plan, | |||||||||||||||||||||||||||
| deparse_context *context, | ||||||||||||||||||||||||||||
| bool showimplicit, | ||||||||||||||||||||||||||||
| bool needcomma); | ||||||||||||||||||||||||||||
| static void get_formatted_string(StringInfo buf, | ||||||||||||||||||||||||||||
| int prettyFlags, | ||||||||||||||||||||||||||||
| int noOfTabChars, | ||||||||||||||||||||||||||||
| const char *fmt,...) pg_attribute_printf(4, 5); | ||||||||||||||||||||||||||||
| static char *pg_get_domain_ddl_worker(Oid domain_oid, int prettyFlags); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| #define only_marker(rte) ((rte)->inh ? "" : "ONLY ") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -13739,6 +13748,37 @@ get_range_partbound_string(List *bound_datums) | |||||||||||||||||||||||||||
| return buf->data; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||
| * get_formatted_string | ||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||
| * Return a formatted version of the string. | ||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||
| * pretty - If pretty is true, the output includes tabs (\t) and newlines (\n). | ||||||||||||||||||||||||||||
| * noOfTabChars - indent with specified no of tabs. | ||||||||||||||||||||||||||||
| * fmt - printf-style format string used by appendStringInfoVA. | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| static void | ||||||||||||||||||||||||||||
| get_formatted_string(StringInfo buf, int prettyFlags, int noOfTabChars, const char *fmt,...) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| va_list args; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (prettyFlags & PRETTYFLAG_INDENT) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| appendStringInfoChar(buf, '\n'); | ||||||||||||||||||||||||||||
| /* Indent with tabs */ | ||||||||||||||||||||||||||||
| for (int i = 0; i < noOfTabChars; i++) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| appendStringInfoChar(buf, '\t'); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||
| appendStringInfoChar(buf, ' '); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| va_start(args, fmt); | ||||||||||||||||||||||||||||
| appendStringInfoVA(buf, fmt, args); | ||||||||||||||||||||||||||||
| va_end(args); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||
| * Helper function to scan domain constraints | ||||||||||||||||||||||||||||
|
|
@@ -13787,7 +13827,7 @@ scan_domain_constraints(Oid domain_oid, List **validcons, List **invalidcons) | |||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| static void | ||||||||||||||||||||||||||||
| build_create_domain_statement(StringInfo buf, Form_pg_type typForm, | ||||||||||||||||||||||||||||
| Node *defaultExpr, List *validConstraints) | ||||||||||||||||||||||||||||
| Node *defaultExpr, List *validConstraints, int prettyFlags) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| HeapTuple baseTypeTuple; | ||||||||||||||||||||||||||||
| Form_pg_type baseTypeForm; | ||||||||||||||||||||||||||||
|
|
@@ -13814,16 +13854,17 @@ build_create_domain_statement(StringInfo buf, Form_pg_type typForm, | |||||||||||||||||||||||||||
| /* Only add COLLATE if domain's collation differs from base type's */ | ||||||||||||||||||||||||||||
| if (typForm->typcollation != baseCollation) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| appendStringInfo(buf, " COLLATE %s", | ||||||||||||||||||||||||||||
| generate_collation_name(typForm->typcollation)); | ||||||||||||||||||||||||||||
| get_formatted_string(buf, prettyFlags, 1, "COLLATE %s", | ||||||||||||||||||||||||||||
| generate_collation_name(typForm->typcollation)); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /* Add default value if present */ | ||||||||||||||||||||||||||||
| if (defaultExpr != NULL) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| char *defaultValue = deparse_expression_pretty(defaultExpr, NIL, false, false, 0, 0); | ||||||||||||||||||||||||||||
| appendStringInfo(buf, " DEFAULT %s", defaultValue); | ||||||||||||||||||||||||||||
| char *defaultValue = deparse_expression_pretty(defaultExpr, NIL, false, false, prettyFlags, 0); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| get_formatted_string(buf, prettyFlags, 1, "DEFAULT %s", defaultValue); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /* Add valid constraints */ | ||||||||||||||||||||||||||||
|
|
@@ -13837,14 +13878,14 @@ build_create_domain_statement(StringInfo buf, Form_pg_type typForm, | |||||||||||||||||||||||||||
| /* Look up the constraint info */ | ||||||||||||||||||||||||||||
| constraintTup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(constraintOid)); | ||||||||||||||||||||||||||||
| if (!HeapTupleIsValid(constraintTup)) | ||||||||||||||||||||||||||||
| continue; /* constraint was dropped concurrently */ | ||||||||||||||||||||||||||||
| continue; /* constraint was dropped concurrently */ | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| con = (Form_pg_constraint) GETSTRUCT(constraintTup); | ||||||||||||||||||||||||||||
| constraintDef = pg_get_constraintdef_worker(constraintOid, false, PRETTYFLAG_PAREN, true); | ||||||||||||||||||||||||||||
| constraintDef = pg_get_constraintdef_worker(constraintOid, false, prettyFlags, true); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| appendStringInfo(buf, " CONSTRAINT %s %s", | ||||||||||||||||||||||||||||
| quote_identifier(NameStr(con->conname)), | ||||||||||||||||||||||||||||
| constraintDef); | ||||||||||||||||||||||||||||
| get_formatted_string(buf, prettyFlags, 1, "CONSTRAINT %s", | ||||||||||||||||||||||||||||
| quote_identifier(NameStr(con->conname))); | ||||||||||||||||||||||||||||
| get_formatted_string(buf, prettyFlags, 2, "%s", constraintDef); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| ReleaseSysCache(constraintTup); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
@@ -13856,28 +13897,45 @@ build_create_domain_statement(StringInfo buf, Form_pg_type typForm, | |||||||||||||||||||||||||||
| * Helper function to add ALTER DOMAIN statements for invalid constraints | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| static void | ||||||||||||||||||||||||||||
| add_alter_domain_statements(StringInfo buf, List *invalidConstraints) | ||||||||||||||||||||||||||||
| add_alter_domain_statements(StringInfo buf, List *invalidConstraints, int prettyFlags) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| ListCell *lc; | ||||||||||||||||||||||||||||
| ListCell *lc; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| foreach(lc, invalidConstraints) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| Oid constraintOid = lfirst_oid(lc); | ||||||||||||||||||||||||||||
| char *alterStmt = pg_get_constraintdef_worker(constraintOid, true, PRETTYFLAG_PAREN, true); | ||||||||||||||||||||||||||||
| Oid constraintOid = lfirst_oid(lc); | ||||||||||||||||||||||||||||
| char *alterStmt = pg_get_constraintdef_worker(constraintOid, true, prettyFlags, true); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (alterStmt) | ||||||||||||||||||||||||||||
| appendStringInfo(buf, "\n%s;", alterStmt); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||
| * pg_get_domain_ddl - Get CREATE DOMAIN statement for a domain | ||||||||||||||||||||||||||||
| * pg_get_domain_ddl_ext - Get CREATE DOMAIN statement for a domain with pretty-print option | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| Datum | ||||||||||||||||||||||||||||
| pg_get_domain_ddl(PG_FUNCTION_ARGS) | ||||||||||||||||||||||||||||
| pg_get_domain_ddl_ext(PG_FUNCTION_ARGS) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| StringInfoData buf; | ||||||||||||||||||||||||||||
| Oid domain_oid = PG_GETARG_OID(0); | ||||||||||||||||||||||||||||
| bool pretty = PG_GETARG_BOOL(1); | ||||||||||||||||||||||||||||
| char *res; | ||||||||||||||||||||||||||||
| int prettyFlags; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| prettyFlags = GET_DDL_PRETTY_FLAGS(pretty); | ||||||||||||||||||||||||||||
|
Comment on lines
+13921
to
+13925
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I propose this, instead of the GET_DDL_PRETTY_FLAGS, but it's your choice. |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| res = pg_get_domain_ddl_worker(domain_oid, prettyFlags); | ||||||||||||||||||||||||||||
| if (res == NULL) | ||||||||||||||||||||||||||||
| PG_RETURN_NULL(); | ||||||||||||||||||||||||||||
| PG_RETURN_TEXT_P(string_to_text(res)); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| static char * | ||||||||||||||||||||||||||||
| pg_get_domain_ddl_worker(Oid domain_oid, int prettyFlags) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| StringInfoData buf; | ||||||||||||||||||||||||||||
| HeapTuple typeTuple; | ||||||||||||||||||||||||||||
| Form_pg_type typForm; | ||||||||||||||||||||||||||||
| Node *defaultExpr; | ||||||||||||||||||||||||||||
|
|
@@ -13887,7 +13945,7 @@ pg_get_domain_ddl(PG_FUNCTION_ARGS) | |||||||||||||||||||||||||||
| /* Look up the domain in pg_type */ | ||||||||||||||||||||||||||||
| typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(domain_oid)); | ||||||||||||||||||||||||||||
| if (!HeapTupleIsValid(typeTuple)) | ||||||||||||||||||||||||||||
| PG_RETURN_NULL(); | ||||||||||||||||||||||||||||
| return NULL; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| typForm = (Form_pg_type) GETSTRUCT(typeTuple); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -13899,16 +13957,16 @@ pg_get_domain_ddl(PG_FUNCTION_ARGS) | |||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /* Build the DDL statement */ | ||||||||||||||||||||||||||||
| initStringInfo(&buf); | ||||||||||||||||||||||||||||
| build_create_domain_statement(&buf, typForm, defaultExpr, validConstraints); | ||||||||||||||||||||||||||||
| build_create_domain_statement(&buf, typForm, defaultExpr, validConstraints, prettyFlags); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /* Add ALTER DOMAIN statements for invalid constraints */ | ||||||||||||||||||||||||||||
| if (list_length(invalidConstraints) > 0) | ||||||||||||||||||||||||||||
| add_alter_domain_statements(&buf, invalidConstraints); | ||||||||||||||||||||||||||||
| add_alter_domain_statements(&buf, invalidConstraints, prettyFlags); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /* Cleanup */ | ||||||||||||||||||||||||||||
| list_free(validConstraints); | ||||||||||||||||||||||||||||
| list_free(invalidConstraints); | ||||||||||||||||||||||||||||
| ReleaseSysCache(typeTuple); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| PG_RETURN_TEXT_P(cstring_to_text(buf.data)); | ||||||||||||||||||||||||||||
| return buf.data; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8515,9 +8515,9 @@ | |||||||||||
| { oid => '2508', descr => 'constraint description with pretty-print option', | ||||||||||||
| proname => 'pg_get_constraintdef', provolatile => 's', prorettype => 'text', | ||||||||||||
| proargtypes => 'oid bool', prosrc => 'pg_get_constraintdef_ext' }, | ||||||||||||
| { oid => '8024', descr => 'get CREATE statement for DOMAIN', | ||||||||||||
| { oid => '8024', descr => 'get CREATE statement for DOMAIN with pretty option', | ||||||||||||
| proname => 'pg_get_domain_ddl', prorettype => 'text', | ||||||||||||
| proargtypes => 'regtype', prosrc => 'pg_get_domain_ddl' }, | ||||||||||||
| proargtypes => 'regtype bool', prosrc => 'pg_get_domain_ddl_ext' }, | ||||||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this instead of adding the create function, better to have all teh changes in one place, maybe... |
||||||||||||
| { oid => '2509', | ||||||||||||
| descr => 'deparse an encoded expression with pretty-print option', | ||||||||||||
| proname => 'pg_get_expr', provolatile => 's', prorettype => 'text', | ||||||||||||
|
|
||||||||||||
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.
Why do we need this?
Isn't it better to have it in pg_proc.dat, also the one with a one-argument signature?