Skip to content

Commit

Permalink
fix: add error prefix for detecting table, column name (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent authored Jan 3, 2024
2 parents 0af79f6 + bacc499 commit bb2c803
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 71 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ linters:
- exhaustruct # https://github.com/GaijinEntertainment/go-exhaustruct
- exhaustivestruct # https://github.com/mbilski/exhaustivestruct
- gci # unnecessary
- goconst # unnecessary
- godox # unnecessary
- golint # deprecated https://github.com/golang/lint
- gomnd # https://github.com/tommy-muehle/go-mnd
Expand Down
12 changes: 6 additions & 6 deletions exp/database/sql/ddl/cockroachdb/ddl_index_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ func (s *CreateIndexStmt) String() string {
}
str += "CREATE "
if s.Unique {
str += "UNIQUE " //nolint:goconst
str += "UNIQUE "
}
str += "INDEX " //nolint:goconst
str += "INDEX "
if s.IfNotExists {
str += "IF NOT EXISTS " //nolint:goconst
str += "IF NOT EXISTS "
}
str += s.Name.String() + " ON " + s.TableName.String() + " (" + stringz.JoinStringers(", ", s.Columns...) + ");\n"
return str
Expand All @@ -46,11 +46,11 @@ func (s *CreateIndexStmt) String() string {
func (s *CreateIndexStmt) StringForDiff() string {
str := "CREATE "
if s.Unique {
str += "UNIQUE " //nolint:goconst
str += "UNIQUE "
}
str += "INDEX " //nolint:goconst
str += "INDEX "
if s.IfNotExists {
str += "IF NOT EXISTS " //nolint:goconst
str += "IF NOT EXISTS "
}
str += s.Name.StringForDiff() + " ON " + s.TableName.StringForDiff() + " ("
for i, c := range s.Columns {
Expand Down
2 changes: 1 addition & 1 deletion exp/database/sql/ddl/cockroachdb/ddl_index_drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (s *DropIndexStmt) String() string {
}
str += "DROP INDEX "
if s.IfExists {
str += "IF EXISTS " //nolint:goconst
str += "IF EXISTS "
}
str += s.Name.String() + ";\n"
return str
Expand Down
8 changes: 4 additions & 4 deletions exp/database/sql/ddl/cockroachdb/ddl_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (c *PrimaryKeyConstraint) GoString() string { return internal.GoString(*c)
func (c *PrimaryKeyConstraint) String() string {
var str string
if c.Name != nil {
str += "CONSTRAINT " + c.Name.String() + " " //nolint:goconst
str += "CONSTRAINT " + c.Name.String() + " "
}
str += "PRIMARY KEY"
str += " (" + stringz.JoinStringers(", ", c.Columns...) + ")"
Expand All @@ -64,7 +64,7 @@ func (c *PrimaryKeyConstraint) String() string {
func (c *PrimaryKeyConstraint) StringForDiff() string {
var str string
if c.Name != nil {
str += "CONSTRAINT " + c.Name.StringForDiff() + " " //nolint:goconst
str += "CONSTRAINT " + c.Name.StringForDiff() + " "
}
str += "PRIMARY KEY"
str += " ("
Expand Down Expand Up @@ -188,7 +188,7 @@ func (c *CheckConstraint) String() string {
if c.Name != nil {
str += "CONSTRAINT " + c.Name.String() + " "
}
str += "CHECK" //nolint:goconst
str += "CHECK"
str += " (" + stringz.JoinStringers(" ", c.Expr...) + ")"
return str
}
Expand Down Expand Up @@ -310,7 +310,7 @@ func (d *Default) String() string {
return ""
}
if d.Value != nil {
return "DEFAULT " + d.Value.String() //nolint:goconst
return "DEFAULT " + d.Value.String()
}
return ""
}
Expand Down
18 changes: 9 additions & 9 deletions exp/database/sql/ddl/cockroachdb/lexar.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ const (

// DATA TYPE.
TOKEN_BOOL TokenType = "BOOL" //diff:ignore-line-postgres-cockroach
TOKEN_INT2 TokenType = "INT2"
TOKEN_INT4 TokenType = "INT4"
TOKEN_INT8 TokenType = "INT8"
TOKEN_INT2 TokenType = "INT2" //diff:ignore-line-postgres-cockroach
TOKEN_INT4 TokenType = "INT4" //diff:ignore-line-postgres-cockroach
TOKEN_INT8 TokenType = "INT8" //diff:ignore-line-postgres-cockroach
TOKEN_DECIMAL TokenType = "DECIMAL"
TOKEN_NUMERIC TokenType = "NUMERIC"
TOKEN_REAL TokenType = "REAL"
Expand Down Expand Up @@ -164,12 +164,12 @@ func lookupIdent(ident string) TokenType {
return TOKEN_TO
case "BOOLEAN", "BOOL": //diff:ignore-line-postgres-cockroach
return TOKEN_BOOL //diff:ignore-line-postgres-cockroach
case "INT2", "SMALLINT":
return TOKEN_INT2
case "INT4", "INTEGER", "INT":
return TOKEN_INT4
case "INT8", "BIGINT":
return TOKEN_INT8
case "INT2", "SMALLINT": //diff:ignore-line-postgres-cockroach
return TOKEN_INT2 //diff:ignore-line-postgres-cockroach
case "INT4", "INTEGER", "INT": //diff:ignore-line-postgres-cockroach
return TOKEN_INT4 //diff:ignore-line-postgres-cockroach
case "INT8", "BIGINT": //diff:ignore-line-postgres-cockroach
return TOKEN_INT8 //diff:ignore-line-postgres-cockroach
case "DECIMAL":
return TOKEN_DECIMAL
case "NUMERIC":
Expand Down
33 changes: 18 additions & 15 deletions exp/database/sql/ddl/cockroachdb/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,12 @@ func (p *Parser) parseCreateTableStmt() (*CreateTableStmt, error) {
}

createTableStmt.Name = NewObjectName(p.currentToken.Literal.Str)
errFmtPrefix := fmt.Sprintf("table_name=%s: ", createTableStmt.Name.StringForDiff())

p.nextToken() // current = (

if err := p.checkCurrentToken(TOKEN_OPEN_PAREN); err != nil {
return nil, errorz.Errorf("checkCurrentToken: %w", err)
return nil, errorz.Errorf(errFmtPrefix+"checkCurrentToken: %w", err)
}

p.nextToken() // current = column_name
Expand All @@ -152,7 +153,7 @@ LabelColumns:
case p.currentToken.Type == TOKEN_IDENT:
column, constraints, err := p.parseColumn(createTableStmt.Name.Name)
if err != nil {
return nil, errorz.Errorf("parseColumn: %w", err)
return nil, errorz.Errorf(errFmtPrefix+"parseColumn: %w", err)
}
createTableStmt.Columns = append(createTableStmt.Columns, column)
if len(constraints) > 0 {
Expand All @@ -163,7 +164,7 @@ LabelColumns:
case isConstraint(p.currentToken.Type):
constraint, err := p.parseTableConstraint(createTableStmt.Name.Name)
if err != nil {
return nil, errorz.Errorf("parseConstraint: %w", err)
return nil, errorz.Errorf(errFmtPrefix+"parseConstraint: %w", err)
}
createTableStmt.Constraints = createTableStmt.Constraints.Append(constraint)
case p.currentToken.Type == TOKEN_COMMA:
Expand All @@ -174,10 +175,10 @@ LabelColumns:
case TOKEN_SEMICOLON, TOKEN_EOF:
break LabelColumns
default:
return nil, errorz.Errorf("peekToken=%#v: %w", p.peekToken, ddl.ErrUnexpectedToken)
return nil, errorz.Errorf(errFmtPrefix+"peekToken=%#v: %w", p.peekToken, ddl.ErrUnexpectedToken)
}
default:
return nil, errorz.Errorf("currentToken=%#v: %w", p.currentToken, ddl.ErrUnexpectedToken)
return nil, errorz.Errorf(errFmtPrefix+"currentToken=%#v: %w", p.currentToken, ddl.ErrUnexpectedToken)
}
}

Expand Down Expand Up @@ -212,30 +213,31 @@ func (p *Parser) parseCreateIndexStmt() (*CreateIndexStmt, error) {
}

createIndexStmt.Name = NewObjectName(p.currentToken.Literal.Str)
errFmtPrefix := fmt.Sprintf("index_name=%s: ", createIndexStmt.Name.StringForDiff())

p.nextToken() // current = ON

if err := p.checkCurrentToken(TOKEN_ON); err != nil {
return nil, errorz.Errorf("checkCurrentToken: %w", err)
return nil, errorz.Errorf(errFmtPrefix+"checkCurrentToken: %w", err)
}

p.nextToken() // current = table_name

if err := p.checkCurrentToken(TOKEN_IDENT); err != nil {
return nil, errorz.Errorf("checkCurrentToken: %w", err)
return nil, errorz.Errorf(errFmtPrefix+"checkCurrentToken: %w", err)
}

createIndexStmt.TableName = NewObjectName(p.currentToken.Literal.Str)

p.nextToken() // current = (

if err := p.checkCurrentToken(TOKEN_OPEN_PAREN); err != nil {
return nil, errorz.Errorf("checkCurrentToken: %w", err)
return nil, errorz.Errorf(errFmtPrefix+"checkCurrentToken: %w", err)
}

idents, err := p.parseColumnIdents()
if err != nil {
return nil, errorz.Errorf("parseColumnIdents: %w", err)
return nil, errorz.Errorf(errFmtPrefix+"parseColumnIdents: %w", err)
}

createIndexStmt.Columns = idents
Expand All @@ -253,14 +255,15 @@ func (p *Parser) parseColumn(tableName *Ident) (*Column, []Constraint, error) {
}

column.Name = NewRawIdent(p.currentToken.Literal.Str)
errFmtPrefix := fmt.Sprintf("column_name=%s: ", column.Name.StringForDiff())

p.nextToken() // current = DATA_TYPE

switch { //nolint:exhaustive
case isDataType(p.currentToken.Type):
dataType, err := p.parseDataType()
if err != nil {
return nil, nil, errorz.Errorf("parseDataType: %w", err)
return nil, nil, errorz.Errorf(errFmtPrefix+"parseDataType: %w", err)
}
column.DataType = dataType

Expand All @@ -270,7 +273,7 @@ func (p *Parser) parseColumn(tableName *Ident) (*Column, []Constraint, error) {
switch p.currentToken.Type { //nolint:exhaustive
case TOKEN_NOT:
if err := p.checkPeekToken(TOKEN_NULL); err != nil {
return nil, nil, errorz.Errorf("checkPeekToken: %w", err)
return nil, nil, errorz.Errorf(errFmtPrefix+"checkPeekToken: %w", err)
}
p.nextToken() // current = NULL
column.NotNull = true
Expand All @@ -280,7 +283,7 @@ func (p *Parser) parseColumn(tableName *Ident) (*Column, []Constraint, error) {
p.nextToken() // current = DEFAULT
def, err := p.parseColumnDefault()
if err != nil {
return nil, nil, errorz.Errorf("parseColumnDefault: %w", err)
return nil, nil, errorz.Errorf(errFmtPrefix+"parseColumnDefault: %w", err)
}
column.Default = def
continue
Expand All @@ -293,15 +296,15 @@ func (p *Parser) parseColumn(tableName *Ident) (*Column, []Constraint, error) {

cs, err := p.parseColumnConstraints(tableName, column)
if err != nil {
return nil, nil, errorz.Errorf("parseColumnConstraints: %w", err)
return nil, nil, errorz.Errorf(errFmtPrefix+"parseColumnConstraints: %w", err)
}
if len(cs) > 0 {
for _, c := range cs {
constraints = constraints.Append(c)
}
}
default:
return nil, nil, errorz.Errorf("currentToken=%#v: %w", p.currentToken, ddl.ErrUnexpectedToken)
return nil, nil, errorz.Errorf(errFmtPrefix+"currentToken=%#v: %w", p.currentToken, ddl.ErrUnexpectedToken)
}

return column, constraints, nil
Expand Down Expand Up @@ -691,7 +694,7 @@ func isReservedValue(tokenType TokenType) bool {
func isDataType(tokenType TokenType) bool {
switch tokenType { //nolint:exhaustive
case TOKEN_BOOL, //diff:ignore-line-postgres-cockroach
TOKEN_INT2, TOKEN_INT4, TOKEN_INT8,
TOKEN_INT2, TOKEN_INT4, TOKEN_INT8, //diff:ignore-line-postgres-cockroach
TOKEN_DECIMAL, TOKEN_NUMERIC,
TOKEN_REAL, TOKEN_DOUBLE, /* TOKEN_PRECISION, */
TOKEN_SMALLSERIAL, TOKEN_SERIAL, TOKEN_BIGSERIAL,
Expand Down
12 changes: 6 additions & 6 deletions exp/database/sql/ddl/postgres/ddl_index_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ func (s *CreateIndexStmt) String() string {
}
str += "CREATE "
if s.Unique {
str += "UNIQUE " //nolint:goconst
str += "UNIQUE "
}
str += "INDEX " //nolint:goconst
str += "INDEX "
if s.IfNotExists {
str += "IF NOT EXISTS " //nolint:goconst
str += "IF NOT EXISTS "
}
str += s.Name.String() + " ON " + s.TableName.String() + " (" + stringz.JoinStringers(", ", s.Columns...) + ");\n"
return str
Expand All @@ -46,11 +46,11 @@ func (s *CreateIndexStmt) String() string {
func (s *CreateIndexStmt) StringForDiff() string {
str := "CREATE "
if s.Unique {
str += "UNIQUE " //nolint:goconst
str += "UNIQUE "
}
str += "INDEX " //nolint:goconst
str += "INDEX "
if s.IfNotExists {
str += "IF NOT EXISTS " //nolint:goconst
str += "IF NOT EXISTS "
}
str += s.Name.StringForDiff() + " ON " + s.TableName.StringForDiff() + " ("
for i, c := range s.Columns {
Expand Down
2 changes: 1 addition & 1 deletion exp/database/sql/ddl/postgres/ddl_index_drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (s *DropIndexStmt) String() string {
}
str += "DROP INDEX "
if s.IfExists {
str += "IF EXISTS " //nolint:goconst
str += "IF EXISTS "
}
str += s.Name.String() + ";\n"
return str
Expand Down
8 changes: 4 additions & 4 deletions exp/database/sql/ddl/postgres/ddl_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *PrimaryKeyConstraint) GoString() string { return internal.GoString(*c)
func (c *PrimaryKeyConstraint) String() string {
var str string
if c.Name != nil {
str += "CONSTRAINT " + c.Name.String() + " " //nolint:goconst
str += "CONSTRAINT " + c.Name.String() + " "
}
str += "PRIMARY KEY"
str += " (" + stringz.JoinStringers(", ", c.Columns...) + ")"
Expand All @@ -52,7 +52,7 @@ func (c *PrimaryKeyConstraint) String() string {
func (c *PrimaryKeyConstraint) StringForDiff() string {
var str string
if c.Name != nil {
str += "CONSTRAINT " + c.Name.StringForDiff() + " " //nolint:goconst
str += "CONSTRAINT " + c.Name.StringForDiff() + " "
}
str += "PRIMARY KEY"
str += " ("
Expand Down Expand Up @@ -171,7 +171,7 @@ func (c *CheckConstraint) String() string {
if c.Name != nil {
str += "CONSTRAINT " + c.Name.String() + " "
}
str += "CHECK" //nolint:goconst
str += "CHECK"
str += " (" + stringz.JoinStringers(" ", c.Expr...) + ")"
return str
}
Expand Down Expand Up @@ -292,7 +292,7 @@ func (d *Default) String() string {
return ""
}
if d.Value != nil {
return "DEFAULT " + d.Value.String() //nolint:goconst
return "DEFAULT " + d.Value.String()
}
return ""
}
Expand Down
20 changes: 10 additions & 10 deletions exp/database/sql/ddl/postgres/lexar.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ const (
TOKEN_TO TokenType = "TO"

// DATA TYPE.
TOKEN_BOOLEAN TokenType = "BOOLEAN" //diff:ignore-line-postgres-cockroach
TOKEN_SMALLINT TokenType = "SMALLINT"
TOKEN_INTEGER TokenType = "INTEGER"
TOKEN_BIGINT TokenType = "BIGINT"
TOKEN_BOOLEAN TokenType = "BOOLEAN" //diff:ignore-line-postgres-cockroach
TOKEN_SMALLINT TokenType = "SMALLINT" //diff:ignore-line-postgres-cockroach
TOKEN_INTEGER TokenType = "INTEGER" //diff:ignore-line-postgres-cockroach
TOKEN_BIGINT TokenType = "BIGINT" //diff:ignore-line-postgres-cockroach
TOKEN_DECIMAL TokenType = "DECIMAL"
TOKEN_NUMERIC TokenType = "NUMERIC"
TOKEN_REAL TokenType = "REAL"
Expand Down Expand Up @@ -163,12 +163,12 @@ func lookupIdent(ident string) TokenType {
return TOKEN_TO
case "BOOLEAN": //diff:ignore-line-postgres-cockroach
return TOKEN_BOOLEAN //diff:ignore-line-postgres-cockroach
case "SMALLINT":
return TOKEN_SMALLINT
case "INTEGER", "INT":
return TOKEN_INTEGER
case "BIGINT":
return TOKEN_BIGINT
case "SMALLINT": //diff:ignore-line-postgres-cockroach
return TOKEN_SMALLINT //diff:ignore-line-postgres-cockroach
case "INTEGER", "INT": //diff:ignore-line-postgres-cockroach
return TOKEN_INTEGER //diff:ignore-line-postgres-cockroach
case "BIGINT": //diff:ignore-line-postgres-cockroach
return TOKEN_BIGINT //diff:ignore-line-postgres-cockroach
case "DECIMAL":
return TOKEN_DECIMAL
case "NUMERIC":
Expand Down
Loading

0 comments on commit bb2c803

Please sign in to comment.