Skip to content

Commit

Permalink
fix: Myriad string formatting changes (#2558)
Browse files Browse the repository at this point in the history
  • Loading branch information
verygoodsoftwarenotvirus committed Aug 1, 2023
1 parent 4a18cc2 commit e91a9ab
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 29 deletions.
2 changes: 1 addition & 1 deletion internal/codegen/golang/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TagsToString(tags map[string]string) string {
}
tagParts := make([]string, 0, len(tags))
for key, val := range tags {
tagParts = append(tagParts, fmt.Sprintf("%s:\"%s\"", key, val))
tagParts = append(tagParts, fmt.Sprintf("%s:%q", key, val))
}
sort.Strings(tagParts)
return strings.Join(tagParts, " ")
Expand Down
4 changes: 2 additions & 2 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ type ImportSpec struct {

func (s ImportSpec) String() string {
if s.ID != "" {
return fmt.Sprintf("%s \"%s\"", s.ID, s.Path)
return fmt.Sprintf("%s %q", s.ID, s.Path)
} else {
return fmt.Sprintf("\"%s\"", s.Path)
return fmt.Sprintf("%q", s.Path)
}
}

Expand Down
8 changes: 4 additions & 4 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,14 +639,14 @@ func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef)
if found == 0 {
return nil, &sqlerr.Error{
Code: "42703",
Message: fmt.Sprintf("column \"%s\" does not exist", name),
Message: fmt.Sprintf("column %q does not exist", name),
Location: res.Location,
}
}
if found > 1 {
return nil, &sqlerr.Error{
Code: "42703",
Message: fmt.Sprintf("column reference \"%s\" is ambiguous", name),
Message: fmt.Sprintf("column reference %q is ambiguous", name),
Location: res.Location,
}
}
Expand Down Expand Up @@ -702,14 +702,14 @@ func findColumnForRef(ref *ast.ColumnRef, tables []*Table, targetList *ast.List)
if found == 0 {
return &sqlerr.Error{
Code: "42703",
Message: fmt.Sprintf("column reference \"%s\" not found", name),
Message: fmt.Sprintf("column reference %q not found", name),
Location: ref.Location,
}
}
if found > 1 {
return &sqlerr.Error{
Code: "42703",
Message: fmt.Sprintf("column reference \"%s\" is ambiguous", name),
Message: fmt.Sprintf("column reference %q is ambiguous", name),
Location: ref.Location,
}
}
Expand Down
14 changes: 7 additions & 7 deletions internal/compiler/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
if !located {
return nil, &sqlerr.Error{
Code: "42703",
Message: fmt.Sprintf("table alias \"%s\" does not exist", alias),
Message: fmt.Sprintf("table alias %q does not exist", alias),
Location: node.Location,
}
}
Expand Down Expand Up @@ -231,14 +231,14 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
if found == 0 {
return nil, &sqlerr.Error{
Code: "42703",
Message: fmt.Sprintf("column \"%s\" does not exist", key),
Message: fmt.Sprintf("column %q does not exist", key),
Location: node.Location,
}
}
if found > 1 {
return nil, &sqlerr.Error{
Code: "42703",
Message: fmt.Sprintf("column reference \"%s\" is ambiguous", key),
Message: fmt.Sprintf("column reference %q is ambiguous", key),
Location: node.Location,
}
}
Expand Down Expand Up @@ -471,7 +471,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
} else {
return nil, &sqlerr.Error{
Code: "42703",
Message: fmt.Sprintf("column \"%s\" does not exist", key),
Message: fmt.Sprintf("column %q does not exist", key),
Location: n.Location,
}
}
Expand Down Expand Up @@ -587,20 +587,20 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
if found == 0 {
return nil, &sqlerr.Error{
Code: "42703",
Message: fmt.Sprintf("396: column \"%s\" does not exist", key),
Message: fmt.Sprintf("396: column %q does not exist", key),
Location: location,
}
}
if found > 1 {
return nil, &sqlerr.Error{
Code: "42703",
Message: fmt.Sprintf("in same name column reference \"%s\" is ambiguous", key),
Message: fmt.Sprintf("in same name column reference %q is ambiguous", key),
Location: location,
}
}

default:
fmt.Printf("unsupported reference type: %T", n)
fmt.Printf("unsupported reference type: %T\n", n)
}
}
return a, nil
Expand Down
4 changes: 2 additions & 2 deletions internal/sql/catalog/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
seen[col.Name] = notNull || col.IsNotNull
if a, ok := coltype[col.Name]; ok {
if !sameType(&a, &col.Type) {
return fmt.Errorf("column \"%s\" has a type conflict", col.Name)
return fmt.Errorf("column %q has a type conflict", col.Name)
}
}
continue
Expand Down Expand Up @@ -298,7 +298,7 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
seen[col.Colname] = notNull || col.IsNotNull
if a, ok := coltype[col.Colname]; ok {
if !sameType(&a, col.TypeName) {
return fmt.Errorf("column \"%s\" has a type conflict", col.Colname)
return fmt.Errorf("column %q has a type conflict", col.Colname)
}
}
continue
Expand Down
20 changes: 10 additions & 10 deletions internal/sql/sqlerr/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,77 +35,77 @@ func ColumnExists(rel, col string) *Error {
return &Error{
Err: Exists,
Code: "42701",
Message: fmt.Sprintf("column \"%s\" of relation \"%s\"", col, rel),
Message: fmt.Sprintf("column %q of relation %q", col, rel),
}
}

func ColumnNotFound(rel, col string) *Error {
return &Error{
Err: NotFound,
Code: "42703",
Message: fmt.Sprintf("column \"%s\" of relation \"%s\"", col, rel),
Message: fmt.Sprintf("column %q of relation %q", col, rel),
}
}

func RelationExists(rel string) *Error {
return &Error{
Err: Exists,
Code: "42P07",
Message: fmt.Sprintf("relation \"%s\"", rel),
Message: fmt.Sprintf("relation %q", rel),
}
}

func RelationNotFound(rel string) *Error {
return &Error{
Err: NotFound,
Code: "42P01",
Message: fmt.Sprintf("relation \"%s\"", rel),
Message: fmt.Sprintf("relation %q", rel),
}
}

func SchemaExists(name string) *Error {
return &Error{
Err: Exists,
Code: "42P06",
Message: fmt.Sprintf("schema \"%s\"", name),
Message: fmt.Sprintf("schema %q", name),
}
}

func SchemaNotFound(sch string) *Error {
return &Error{
Err: NotFound,
Code: "3F000",
Message: fmt.Sprintf("schema \"%s\"", sch),
Message: fmt.Sprintf("schema %q", sch),
}
}

func TypeExists(typ string) *Error {
return &Error{
Err: Exists,
Code: "42710",
Message: fmt.Sprintf("type \"%s\"", typ),
Message: fmt.Sprintf("type %q", typ),
}
}

func TypeNotFound(typ string) *Error {
return &Error{
Err: NotFound,
Code: "42704",
Message: fmt.Sprintf("type \"%s\"", typ),
Message: fmt.Sprintf("type %q", typ),
}
}

func FunctionNotFound(fun string) *Error {
return &Error{
Err: NotFound,
Code: "42704",
Message: fmt.Sprintf("function \"%s\"", fun),
Message: fmt.Sprintf("function %q", fun),
}
}

func FunctionNotUnique(fn string) *Error {
return &Error{
Err: NotUnique,
Message: fmt.Sprintf("function name \"%s\"", fn),
Message: fmt.Sprintf("function name %q", fn),
}
}
5 changes: 2 additions & 3 deletions internal/tools/sqlc-pg-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strings"
"text/template"

pgx "github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4"
)

// https://dba.stackexchange.com/questions/255412/how-to-select-functions-that-belong-in-a-given-extension-in-postgresql
Expand Down Expand Up @@ -308,8 +308,7 @@ func run(ctx context.Context) error {
funcName += strings.Title(part)
}

_, err := conn.Exec(ctx, fmt.Sprintf("CREATE EXTENSION IF NOT EXISTS \"%s\"", extension))
if err != nil {
if _, err := conn.Exec(ctx, fmt.Sprintf("CREATE EXTENSION IF NOT EXISTS %q", extension)); err != nil {
return fmt.Errorf("error creating %s: %s", extension, err)
}

Expand Down

0 comments on commit e91a9ab

Please sign in to comment.