Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(parser): support character type (#3185)
rebelice1aa5de6f936b
diff --git a/plugin/parser/ast/character.go b/plugin/parser/ast/character.gonew file mode 100644index 0000000000..f6c50ee093--- /dev/null+++ b/plugin/parser/ast/character.go@@ -0,0 +1,24 @@+package ast++import (+ "fmt"+ "strings"+)++var (+ _ DataType = (*Character)(nil)+)++// Character is the struct for the character.+type Character struct {+ characterType++ // the storage size(characters, not bytes)+ Size int+}++// EquivalentType implements the DataType interface.+func (c *Character) EquivalentType(tp string) bool {+ tp = strings.ToLower(tp)+ return tp == fmt.Sprintf("char(%d)", c.Size) || tp == fmt.Sprintf("character(%d)", c.Size)+}diff --git a/plugin/parser/ast/character_varying.go b/plugin/parser/ast/character_varying.gonew file mode 100644index 0000000000..29c87214e2--- /dev/null+++ b/plugin/parser/ast/character_varying.go@@ -0,0 +1,24 @@+package ast++import (+ "fmt"+ "strings"+)++var (+ _ DataType = (*CharacterVarying)(nil)+)++// CharacterVarying is the struct for the character varying(varchar).+type CharacterVarying struct {+ characterType++ // the storage size(characters, not bytes)+ Size int+}++// EquivalentType implements the DataType interface.+func (c *CharacterVarying) EquivalentType(tp string) bool {+ tp = strings.ToLower(tp)+ return tp == fmt.Sprintf("varchar(%d)", c.Size) || tp == fmt.Sprintf("character varying(%d)", c.Size)+}diff --git a/plugin/parser/ast/data_type.go b/plugin/parser/ast/data_type.goindex a1157285c4..b6b7a912ef 100644--- a/plugin/parser/ast/data_type.go+++ b/plugin/parser/ast/data_type.go@@ -35,3 +35,16 @@ type numericType struct {}func (*numericType) numericTypeInterface() {}++// CharacterType is the interface for character type.+type CharacterType interface {+ DataType++ characterTypeInterface()+}++type characterType struct {+ dataType+}++func (*characterType) characterTypeInterface() {}diff --git a/plugin/parser/ast/text.go b/plugin/parser/ast/text.gonew file mode 100644index 0000000000..5eeac50465--- /dev/null+++ b/plugin/parser/ast/text.go@@ -0,0 +1,20 @@+package ast++import (+ "strings"+)++var (+ _ DataType = (*Text)(nil)+)++// Text is the struct for the character.+type Text struct {+ characterType+}++// EquivalentType implements the DataType interface.+func (*Text) EquivalentType(tp string) bool {+ tp = strings.ToLower(tp)+ return tp == "text"+}diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.goindex 3c5ce7c98d..fd24a14707 100644--- a/plugin/parser/engine/pg/convert.go+++ b/plugin/parser/engine/pg/convert.go@@ -861,6 +861,12 @@ func convertDataType(tp *pgquery.TypeName) ast.DataType {return &ast.Serial{Size: size}case s == "numeric":return convertToDecimal(tp.Typmods)+ case s == "bpchar":+ return convertToCharacter(tp.Typmods)+ case s == "varchar":+ return convertToVarchar(tp.Typmods)+ case s == "text":+ return &ast.Text{}}}return convertToUnconvertedDataType(tp)@@ -902,6 +908,28 @@ func convertToDecimal(typmods []*pgquery.Node) ast.DataType {}}+func convertToVarchar(typmods []*pgquery.Node) ast.DataType {+ if len(typmods) != 1 {+ return &ast.UnconvertedDataType{}+ }+ size, ok := convertToInteger(typmods[0])+ if !ok {+ return &ast.UnconvertedDataType{}+ }+ return &ast.CharacterVarying{Size: size}+}++func convertToCharacter(typmods []*pgquery.Node) ast.DataType {+ if len(typmods) != 1 {+ return &ast.UnconvertedDataType{}+ }+ size, ok := convertToInteger(typmods[0])+ if !ok {+ return &ast.UnconvertedDataType{}+ }+ return &ast.Character{Size: size}+}+func convertToInteger(in *pgquery.Node) (int, bool) {aConst, ok := in.Node.(*pgquery.Node_AConst)if !ok {diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.goindex 355c39cc18..f3f7c2a884 100644--- a/plugin/parser/engine/pg/convert_test.go+++ b/plugin/parser/engine/pg/convert_test.go@@ -50,6 +50,63 @@ func runTests(t *testing.T, tests []testData) {func TestPGConvertCreateTableStmt(t *testing.T) {tests := []testData{+ {+ stmt: `+ CREATE TABLE tech_book(+ a char(20),+ b character(30),+ c varchar(330),+ d character varying(400),+ e text+ )+ `,+ want: []ast.Node{+ &ast.CreateTableStmt{+ IfNotExists: false,+ Name: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },+ ColumnList: []*ast.ColumnDef{+ {+ ColumnName: "a",+ Type: &ast.Character{Size: 20},+ },+ {+ ColumnName: "b",+ Type: &ast.Character{Size: 30},+ },+ {+ ColumnName: "c",+ Type: &ast.CharacterVarying{Size: 330},+ },+ {+ ColumnName: "d",+ Type: &ast.CharacterVarying{Size: 400},+ },+ {+ ColumnName: "e",+ Type: &ast.Text{},+ },+ },+ },+ },+ statementList: []parser.SingleSQL{+ {+ Text: `CREATE TABLE tech_book(+ a char(20),+ b character(30),+ c varchar(330),+ d character varying(400),+ e text+ )`,+ LastLine: 8,+ },+ },+ columnLine: [][]int{+ {3, 4, 5, 6, 7},+ },+ },{stmt: `CREATE TABLE tech_book(a smallint,