Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: modify the deparse format as pg_dump (#3186)

rebeliceb423a3933921
diff --git a/plugin/parser/engine/pg/deparse.go b/plugin/parser/engine/pg/deparse.go
index b9ceff5529..0ff301d99b 100644
--- a/plugin/parser/engine/pg/deparse.go
+++ b/plugin/parser/engine/pg/deparse.go
@@ -43,20 +43,29 @@ func deparseCreateTable(context parser.DeparseContext, in *ast.CreateTableStmt,
}
if len(in.ColumnList) != 0 {
- if _, err := buf.WriteString("("); err != nil {
+ if _, err := buf.WriteString(" ("); err != nil {
return err
}
}
+ columnContext := parser.DeparseContext{
+ IndentLevel: context.IndentLevel + 1,
+ }
for i, column := range in.ColumnList {
if i != 0 {
- if _, err := buf.WriteString(", "); err != nil {
+ if _, err := buf.WriteString(","); err != nil {
return err
}
}
- if err := deparseColumnDef(context, column, buf); err != nil {
+ if _, err := buf.WriteString("\n"); err != nil {
+ return err
+ }
+ if err := deparseColumnDef(columnContext, column, buf); err != nil {
return err
}
}
+ if _, err := buf.WriteString("\n"); err != nil {
+ return err
+ }
if len(in.ColumnList) != 0 {
if _, err := buf.WriteString(")"); err != nil {
return err
@@ -67,6 +76,11 @@ func deparseCreateTable(context parser.DeparseContext, in *ast.CreateTableStmt,
}
func deparseColumnDef(context parser.DeparseContext, in *ast.ColumnDef, buf *strings.Builder) error {
+ for i := 0; i < context.IndentLevel; i++ {
+ if _, err := buf.WriteString(parser.DeparseIndentString); err != nil {
+ return err
+ }
+ }
if err := writeSurrounding(buf, in.ColumnName, "\""); err != nil {
return err
}
@@ -133,14 +147,24 @@ func deparseTableDef(_ parser.DeparseContext, in *ast.TableDef, buf *strings.Bui
func deparseDataType(_ parser.DeparseContext, in ast.DataType, buf *strings.Builder) error {
switch node := in.(type) {
case *ast.Integer:
- if _, err := buf.WriteString("INT"); err != nil {
- return err
- }
- if _, err := buf.WriteString(strconv.Itoa(node.Size)); err != nil {
- return err
+ switch node.Size {
+ case 8:
+ if _, err := buf.WriteString("bigint"); err != nil {
+ return err
+ }
+ case 4:
+ if _, err := buf.WriteString("integer"); err != nil {
+ return err
+ }
+ case 2:
+ if _, err := buf.WriteString("smallint"); err != nil {
+ return err
+ }
+ default:
+ return errors.Errorf("failed to deparse integer with %d size", node.Size)
}
case *ast.Decimal:
- if _, err := buf.WriteString("DECIMAL"); err != nil {
+ if _, err := buf.WriteString("numeric"); err != nil {
return err
}
if node.Precision != 0 {
@@ -165,18 +189,34 @@ func deparseDataType(_ parser.DeparseContext, in ast.DataType, buf *strings.Buil
}
}
case *ast.Float:
- if _, err := buf.WriteString("FLOAT"); err != nil {
- return err
- }
- if _, err := buf.WriteString(strconv.Itoa(node.Size)); err != nil {
- return err
+ switch node.Size {
+ case 8:
+ if _, err := buf.WriteString("double precision"); err != nil {
+ return err
+ }
+ case 4:
+ if _, err := buf.WriteString("real"); err != nil {
+ return err
+ }
+ default:
+ return errors.Errorf("failed to deparse float with %d size", node.Size)
}
case *ast.Serial:
- if _, err := buf.WriteString("SERIAL"); err != nil {
- return err
- }
- if _, err := buf.WriteString(strconv.Itoa(node.Size)); err != nil {
- return err
+ switch node.Size {
+ case 8:
+ if _, err := buf.WriteString("bigserial"); err != nil {
+ return err
+ }
+ case 4:
+ if _, err := buf.WriteString("serial"); err != nil {
+ return err
+ }
+ case 2:
+ if _, err := buf.WriteString("smallserial"); err != nil {
+ return err
+ }
+ default:
+ return errors.Errorf("failed to deparse serial with %d size", node.Size)
}
case *ast.UnconvertedDataType:
var nameList []string
diff --git a/plugin/parser/engine/pg/deparse_test.go b/plugin/parser/engine/pg/deparse_test.go
index 1fbc1c1be8..553c603438 100644
--- a/plugin/parser/engine/pg/deparse_test.go
+++ b/plugin/parser/engine/pg/deparse_test.go
@@ -50,31 +50,34 @@ func TestCreateTable(t *testing.T) {
r serial2,
s serial4,
t decimal)`,
- want: `CREATE TABLE "tech_book"(` +
- `"a" INT2, ` +
- `"b" INT4, ` +
- `"c" INT8, ` +
- `"d" DECIMAL(10, 2), ` +
- `"e" DECIMAL(4), ` +
- `"f" FLOAT4, ` +
- `"g" FLOAT8, ` +
- `"h" SERIAL2, ` +
- `"i" SERIAL4, ` +
- `"j" SERIAL8, ` +
- `"k" INT8, ` +
- `"l" SERIAL8, ` +
- `"m" FLOAT8, ` +
- `"n" INT4, ` +
- `"o" INT4, ` +
- `"p" FLOAT4, ` +
- `"q" INT2, ` +
- `"r" SERIAL2, ` +
- `"s" SERIAL4, ` +
- `"t" DECIMAL)`,
+ want: `CREATE TABLE "tech_book" (
+ "a" smallint,
+ "b" integer,
+ "c" bigint,
+ "d" numeric(10, 2),
+ "e" numeric(4),
+ "f" real,
+ "g" double precision,
+ "h" smallserial,
+ "i" serial,
+ "j" bigserial,
+ "k" bigint,
+ "l" bigserial,
+ "m" double precision,
+ "n" integer,
+ "o" integer,
+ "p" real,
+ "q" smallint,
+ "r" smallserial,
+ "s" serial,
+ "t" numeric
+)`,
},
{
stmt: `create table "TechBook"(a "user defined data type")`,
- want: `CREATE TABLE "TechBook"("a" "user defined data type")`,
+ want: `CREATE TABLE "TechBook" (
+ "a" "user defined data type"
+)`,
},
}
diff --git a/plugin/parser/parser.go b/plugin/parser/parser.go
index e24b6fbb98..c305a3da88 100644
--- a/plugin/parser/parser.go
+++ b/plugin/parser/parser.go
@@ -19,6 +19,9 @@ const (
Postgres EngineType = "POSTGRES"
// TiDB is the engine type for TiDB.
TiDB EngineType = "TIDB"
+
+ // DeparseIndentString is the string for each indent level.
+ DeparseIndentString = " "
)
// ParseContext is the context for parsing.
@@ -27,6 +30,9 @@ type ParseContext struct {
// DeparseContext is the contxt for restoring.
type DeparseContext struct {
+ // IndentLevel is indent level for current line.
+ // The parser deparses statements with the indent level for pretty format.
+ IndentLevel int
}
// Parser is the interface for parser.