Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion crates/pgls_pretty_print/src/nodes/comment_stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,14 @@ pub(super) fn emit_comment_stmt(e: &mut EventEmitter, n: &CommentStmt) {
e.space();
super::emit_node(object, e);
} else {
super::emit_node(object, e);
// For most object types (TABLE, INDEX, SEQUENCE, VIEW, etc.), the object
// is a List of String nodes representing a schema-qualified name that must
// be dot-separated (e.g., public.my_table), not comma-separated.
if let Some(pgls_query::NodeEnum::List(list)) = object.node.as_ref() {
emit_dot_separated_list(e, &list.items);
} else {
super::emit_node(object, e);
}
}
}

Expand Down
53 changes: 49 additions & 4 deletions crates/pgls_pretty_print/src/nodes/create_extension_stmt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use super::node_list::emit_comma_separated_list;
use crate::{
TokenKind,
emitter::{EventEmitter, GroupKind, LineType},
};
use pgls_query::protobuf::CreateExtensionStmt;
use pgls_query::{NodeEnum, protobuf::CreateExtensionStmt};

pub(super) fn emit_create_extension_stmt(e: &mut EventEmitter, n: &CreateExtensionStmt) {
e.group_start(GroupKind::CreateExtensionStmt);
Expand All @@ -27,11 +26,57 @@ pub(super) fn emit_create_extension_stmt(e: &mut EventEmitter, n: &CreateExtensi
if !n.options.is_empty() {
e.line(LineType::SoftOrSpace);
e.token(TokenKind::WITH_KW);
e.space();
emit_comma_separated_list(e, &n.options, super::emit_node);

for opt in &n.options {
let def = assert_node_variant!(DefElem, opt);
e.space();
emit_extension_option(e, def);
}
}

e.token(TokenKind::SEMICOLON);

e.group_end();
}

/// Emit a CREATE EXTENSION option.
/// Options use keyword syntax (no equals sign):
/// SCHEMA schema_name
/// VERSION 'version'
/// CASCADE
fn emit_extension_option(e: &mut EventEmitter, def: &pgls_query::protobuf::DefElem) {
match def.defname.as_str() {
"schema" => {
e.token(TokenKind::SCHEMA_KW);
if let Some(ref arg) = def.arg {
e.space();
// Schema name is a String node - emit_node dispatches to
// emit_string which uses emit_identifier_maybe_quoted
super::emit_node(arg, e);
}
}
"new_version" => {
e.token(TokenKind::VERSION_KW);
if let Some(ref arg) = def.arg {
e.space();
// Version must be a quoted string literal
if let Some(NodeEnum::String(s)) = &arg.node {
super::emit_string_literal(e, s);
} else {
super::emit_node(arg, e);
}
}
}
"cascade" => {
e.token(TokenKind::CASCADE_KW);
}
_ => {
// Fallback for unknown options
super::string::emit_keyword(e, &def.defname);
if let Some(ref arg) = def.arg {
e.space();
super::emit_node(arg, e);
}
}
}
}
22 changes: 22 additions & 0 deletions crates/pgls_pretty_print/src/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,28 @@ fn normalize_a_indirection(node: &mut NodeEnum) {
}
}
}
NodeEnum::CreateStmt(stmt) => {
// Recurse into table elements (ColumnDef, Constraint, etc.)
for elt in &mut stmt.table_elts {
if let Some(ref mut n) = elt.node {
normalize_a_indirection(n);
}
}
}
NodeEnum::ColumnDef(cd) => {
// Recurse into column constraints
for constraint in &mut cd.constraints {
if let Some(ref mut n) = constraint.node {
normalize_a_indirection(n);
}
}
// Recurse into default value expression
if let Some(ref mut raw) = cd.raw_default
&& let Some(ref mut n) = raw.node
{
normalize_a_indirection(n);
}
}
NodeEnum::RuleStmt(stmt) => {
for action in &mut stmt.actions {
if let Some(ref mut n) = action.node {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
COMMENT ON TABLE public.api_token IS 'Listing of known API tokens';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
COMMENT ON TABLE "public"."event" IS 'An event is created everytime something interesting happens';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE EXTENSION vector WITH SCHEMA extensions;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE EXTENSION IF NOT EXISTS pg_stat_statements WITH SCHEMA extensions;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE TABLE t (
"type" "public"."my_enum" NOT NULL,
"target_column" "text",
"from_value" "text",
"to_value" "text",
CONSTRAINT "c2" CHECK (
("type" <> 'es_value_changed'::"public"."my_enum") OR
(
("target_column" = 'config'::"text") AND
(("from_value" IS NULL) AND ("to_value" IS NULL))
) OR
(
(("from_value" IS NOT NULL) AND ("to_value" IS NOT NULL)) AND
("target_column" IN ('type', 'name'))
)
)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/pgls_pretty_print/tests/tests.rs
input_file: crates/pgls_pretty_print/tests/data/single/comment_stmt_1.sql
---
comment on table public.api_token is 'Listing of known API tokens';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/pgls_pretty_print/tests/tests.rs
input_file: crates/pgls_pretty_print/tests/data/single/comment_stmt_1.sql
---
comment on table public.api_token is 'Listing of known API tokens';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/pgls_pretty_print/tests/tests.rs
input_file: crates/pgls_pretty_print/tests/data/single/comment_stmt_2.sql
---
comment on table public.event is 'An event is created everytime something interesting happens';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/pgls_pretty_print/tests/tests.rs
input_file: crates/pgls_pretty_print/tests/data/single/comment_stmt_2.sql
---
comment on table public.event is 'An event is created everytime something interesting happens';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/pgls_pretty_print/tests/tests.rs
input_file: crates/pgls_pretty_print/tests/data/single/create_extension_stmt_1.sql
---
create extension vector with schema extensions;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/pgls_pretty_print/tests/tests.rs
input_file: crates/pgls_pretty_print/tests/data/single/create_extension_stmt_1.sql
---
create extension vector with schema extensions;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/pgls_pretty_print/tests/tests.rs
input_file: crates/pgls_pretty_print/tests/data/single/create_extension_stmt_2.sql
---
create extension if not exists pg_stat_statements with schema extensions;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/pgls_pretty_print/tests/tests.rs
input_file: crates/pgls_pretty_print/tests/data/single/create_extension_stmt_2.sql
---
create extension if not exists pg_stat_statements with schema extensions;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: crates/pgls_pretty_print/tests/tests.rs
input_file: crates/pgls_pretty_print/tests/data/single/create_stmt_cast_check_0.sql
---
create table t (
type public.my_enum not null,
target_column text,
from_value text,
to_value text,
constraint "c2" check (type <> cast('es_value_changed' as public.my_enum) or
target_column = cast('config' as text) and
from_value is null and to_value is null or
from_value is not null and
to_value is not null and
target_column in ('type', 'name'))
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/pgls_pretty_print/tests/tests.rs
input_file: crates/pgls_pretty_print/tests/data/single/create_stmt_cast_check_0.sql
---
create table t (
type public.my_enum not null,
target_column text,
from_value text,
to_value text,
constraint "c2" check (type <>
cast('es_value_changed'
as public.my_enum) or
target_column = cast('config' as text) and
from_value is null and to_value is null or
from_value is not null and
to_value is not null and
target_column in ('type', 'name'))
);
Loading