From bc3f01fd2e85f44da826a3a3148bdf011ae3e9a7 Mon Sep 17 00:00:00 2001 From: Tianzhou Date: Mon, 1 Dec 2025 20:23:55 -0800 Subject: [PATCH] chore: refine quotation --- ir/quote.go | 6 ++--- ir/quote_test.go | 63 ------------------------------------------------ 2 files changed, 3 insertions(+), 66 deletions(-) diff --git a/ir/quote.go b/ir/quote.go index 1fc91805..5ab7154d 100644 --- a/ir/quote.go +++ b/ir/quote.go @@ -117,8 +117,8 @@ var reservedWords = map[string]bool{ "within": true, } -// NeedsQuoting checks if an identifier needs to be quoted -func NeedsQuoting(identifier string) bool { +// needsQuoting checks if an identifier needs to be quoted +func needsQuoting(identifier string) bool { if identifier == "" { return false } @@ -150,7 +150,7 @@ func NeedsQuoting(identifier string) bool { // QuoteIdentifier adds quotes to an identifier if needed func QuoteIdentifier(identifier string) string { - if NeedsQuoting(identifier) { + if needsQuoting(identifier) { return `"` + identifier + `"` } return identifier diff --git a/ir/quote_test.go b/ir/quote_test.go index c090e074..8b972886 100644 --- a/ir/quote_test.go +++ b/ir/quote_test.go @@ -5,56 +5,6 @@ import ( "testing" ) -func TestNeedsQuoting(t *testing.T) { - type testCase struct { - name string - identifier string - expected bool - } - tests := []testCase{ - {"simple lowercase", "users", false}, - {"reserved word", "user", true}, - {"limit keyword", "limit", true}, - {"bigint type", "bigint", true}, - {"boolean type", "boolean", true}, - {"update command", "update", true}, - {"delete command", "delete", true}, - {"insert command", "insert", true}, - {"returning clause", "returning", true}, - {"offset keyword", "offset", true}, - {"inner join", "inner", true}, - {"outer join", "outer", true}, - {"camelCase", "firstName", true}, - {"UPPERCASE", "USERS", true}, - {"MixedCase", "MyApp", true}, - {"with underscore", "user_name", false}, - {"starts with underscore", "_private", false}, - {"starts with number", "1table", true}, - {"contains dash", "user-table", true}, - {"empty string", "", false}, - } - - // adding all keywords as test cases to ensure all values are checked, there may be some duplicates - // in the tests above to ensure that there is also the opportunity to visually inspect a subset - // of the test cases. - for reservedWord := range reservedWords { - tests = append(tests, testCase{ - name: fmt.Sprintf("reserved word: %q", reservedWord), - identifier: reservedWord, - expected: true, - }) - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - result := NeedsQuoting(tt.identifier) - if result != tt.expected { - t.Errorf("NeedsQuoting(%q) = %v; want %v", tt.identifier, result, tt.expected) - } - }) - } -} - func TestQuoteIdentifier(t *testing.T) { type testCase struct { name string @@ -64,19 +14,6 @@ func TestQuoteIdentifier(t *testing.T) { tests := []testCase{ {"simple lowercase", "users", "users"}, - {"reserved word", "user", `"user"`}, - {"limit keyword", "limit", `"limit"`}, - {"bigint type", "bigint", `"bigint"`}, - {"boolean type", "boolean", `"boolean"`}, - {"update command", "update", `"update"`}, - {"delete command", "delete", `"delete"`}, - {"insert command", "insert", `"insert"`}, - {"returning clause", "returning", `"returning"`}, - {"offset keyword", "offset", `"offset"`}, - {"inner join", "inner", `"inner"`}, - {"outer join", "outer", `"outer"`}, - {"for clause", "for", `"for"`}, - {"filter keyword", "filter", `"filter"`}, {"camelCase", "firstName", `"firstName"`}, {"UPPERCASE", "USERS", `"USERS"`}, {"MixedCase", "MyApp", `"MyApp"`},