Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(spanner/spansql): add backticks when name contains a hypen #6621

Merged
merged 1 commit into from Sep 7, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions spanner/spannertest/db_test.go
Expand Up @@ -714,3 +714,17 @@ func TestForeignKeyAddAndAlterConstraint(t *testing.T) {
}
}
}

func TestAddBackQuoteForHypen(t *testing.T) {
ddl, err := spansql.ParseDDL("filename", "ALTER DATABASE `test-db` SET OPTIONS (optimizer_version=4, version_retention_period = '7d', enable_key_visualizer=true)")
if err != nil {
t.Fatalf("%s: Bad DDL", err)
}

got := ddl.List[0].SQL()
want := "ALTER DATABASE `test-db` SET OPTIONS (optimizer_version=4, version_retention_period='7d', enable_key_visualizer=true)"

if !reflect.DeepEqual(got, want) {
t.Errorf("Generated SQL statement incorrect.\n got %v\nwant %v", got, want)
}
}
3 changes: 2 additions & 1 deletion spanner/spansql/sql.go
Expand Up @@ -701,7 +701,8 @@ func (id ID) addSQL(sb *strings.Builder) {

// TODO: If there are non-letters/numbers/underscores then this also needs quoting.

if IsKeyword(string(id)) {
// Naming Convention: Must be enclosed in backticks (`) if it's a reserved keyword or contains a hyphen.
if IsKeyword(string(id)) || strings.Contains(string(id), "-") {
// TODO: Escaping may be needed here.
sb.WriteString("`")
sb.WriteString(string(id))
Expand Down