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
38 changes: 38 additions & 0 deletions internal/compiler/compile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package compiler

import (
"testing"

"github.com/kyleconroy/sqlc/internal/config"
)

func TestQuoteIdent(t *testing.T) {
type test struct {
engine config.Engine
in string
want string
}
tests := []test{
{config.EnginePostgreSQL, "age", "age"},
{config.EnginePostgreSQL, "Age", `"Age"`},
{config.EnginePostgreSQL, "CamelCase", `"CamelCase"`},
{config.EngineMySQL, "CamelCase", "CamelCase"},
// keywords
{config.EnginePostgreSQL, "select", `"select"`},
{config.EngineMySQL, "select", "`select`"},
}

for _, spec := range tests {
compiler := NewCompiler(config.SQL{
Engine: spec.engine,
}, config.CombinedSettings{})

t.Run(spec.in, func(t *testing.T) {
got := compiler.quoteIdent(spec.in)
if got != spec.want {
t.Error("quoteIdent: engine " + string(spec.engine) + " failed for " + spec.in + ", want " + spec.want + ", got " + got)
}
})
}

}
6 changes: 6 additions & 0 deletions internal/compiler/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ func (c *Compiler) quoteIdent(ident string) string {
return "\"" + ident + "\""
}
}
if c.conf.Engine == config.EnginePostgreSQL {
// camelCase means the column is also camelCase
if strings.ToLower(ident) != ident {
return "\"" + ident + "\""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 This will solve the problem where columns are in camel case and should not break anything.

}
}
return ident
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CREATE TABLE users (
id integer NOT NULL PRIMARY KEY,
ID integer NOT NULL PRIMARY KEY,
first_name varchar(255) NOT NULL,
last_name varchar(255),
age integer NOT NULL
"Age" integer NOT NULL
);