From cedd8c16df560536c1252e0a1e04552e4194fab3 Mon Sep 17 00:00:00 2001 From: Nico Grashoff Date: Sun, 5 Feb 2023 14:58:24 +0100 Subject: [PATCH 1/2] Reformat smart column test table --- cmd/sqlcup/main_test.go | 73 +++++------------------------------------ 1 file changed, 8 insertions(+), 65 deletions(-) diff --git a/cmd/sqlcup/main_test.go b/cmd/sqlcup/main_test.go index 1ec39d6..79dfa18 100644 --- a/cmd/sqlcup/main_test.go +++ b/cmd/sqlcup/main_test.go @@ -9,71 +9,14 @@ var smartColTests = map[string]struct { col column err error }{ - "@id": { - col: column{ - Name: "id", - Type: "INTEGER", - Constraint: "PRIMARY KEY", - ID: true, - }, - }, - "col_id@id": { - col: column{ - Name: "col_id", - Type: "INTEGER", - Constraint: "PRIMARY KEY", - ID: true, - }, - }, - "primary_key@text@id": { - col: column{ - Name: "primary_key", - Type: "TEXT", - Constraint: "NOT NULL PRIMARY KEY", - ID: true, - }, - }, - "col@text": { - col: column{ - Name: "col", - Type: "TEXT", - Constraint: "NOT NULL", - ID: false, - }, - }, - "col@text@null": { - col: column{ - Name: "col", - Type: "TEXT", - Constraint: "", - ID: false, - }, - }, - "col@text@unique": { - col: column{ - Name: "col", - Type: "TEXT", - Constraint: "NOT NULL UNIQUE", - ID: false, - }, - }, - "col@int": { - col: column{ - Name: "col", - Type: "INTEGER", - Constraint: "NOT NULL", - ID: false, - }, - }, - "col@datetime": { - col: column{ - Name: "col", - Type: "DATETIME", - Constraint: "NOT NULL", - ID: false, - }, - err: nil, - }, + "@id": {col: column{Name: "id", Type: "INTEGER", Constraint: "PRIMARY KEY", ID: true}}, + "col_id@id": {col: column{Name: "col_id", Type: "INTEGER", Constraint: "PRIMARY KEY", ID: true}}, + "primary_key@text@id": {col: column{Name: "primary_key", Type: "TEXT", Constraint: "NOT NULL PRIMARY KEY", ID: true}}, + "col@text": {col: column{Name: "col", Type: "TEXT", Constraint: "NOT NULL", ID: false}}, + "col@text@null": {col: column{Name: "col", Type: "TEXT", Constraint: "", ID: false}}, + "col@text@unique": {col: column{Name: "col", Type: "TEXT", Constraint: "NOT NULL UNIQUE", ID: false}}, + "col@int": {col: column{Name: "col", Type: "INTEGER", Constraint: "NOT NULL", ID: false}}, + "col@datetime": {col: column{Name: "col", Type: "DATETIME", Constraint: "NOT NULL", ID: false}, err: nil}, } func TestParseSmartColumnDefinition(t *testing.T) { From af4d9b3578adf2f4cac56b7afa9f6446930a23ad Mon Sep 17 00:00:00 2001 From: Nico Grashoff Date: Sun, 5 Feb 2023 16:53:25 +0100 Subject: [PATCH 2/2] Convert to upper camel case. Previous, we only capitalized the names which worked great unless they contained underscores. Was became `ListZipcode_imports` now becomes `ListZipcodeImports` which improves function and type names generates by sqlc. --- README.md | 6 +++--- cmd/sqlcup/main.go | 17 +++++++++++++++-- cmd/sqlcup/usage.txt | 4 ++-- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 288c6e4..6165600 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ## Installation ``` -$ go install github.com/ngrash/sqlcup/cmd/sqlcup@v0.4.1 +$ go install github.com/ngrash/sqlcup/cmd/sqlcup@v0.4.2 ``` ## Usage @@ -19,8 +19,8 @@ Synopsis: Description: sqlcup prints SQL statements to stdout. The argument must be - of the form /. sqlcup capitalizes those names - where necessary. + of the form /. sqlcup converts those names to + upper camel case where necessary. Each column argument given to sqlcup defines a database column and must be either a or a : diff --git a/cmd/sqlcup/main.go b/cmd/sqlcup/main.go index d84ef99..8ab53bd 100644 --- a/cmd/sqlcup/main.go +++ b/cmd/sqlcup/main.go @@ -259,8 +259,8 @@ func parseScaffoldCommandArgs(args []string) (*scaffoldCommandArgs, error) { sca := &scaffoldCommandArgs{ Table: tableParts[1], - SingularEntity: capitalize(tableParts[0]), - PluralEntity: capitalize(tableParts[1]), + SingularEntity: upperCamelCase(tableParts[0]), + PluralEntity: upperCamelCase(tableParts[1]), NoExistsClause: *noExistsClauseFlag, NoReturningClause: *noReturningClauseFlag, OrderBy: *orderByFlag, @@ -446,6 +446,19 @@ func writeUpdateQuery(w io.Writer, args *scaffoldCommandArgs) { } } +// upperCamelCase converts a string like "zipcode_imports" to "ZipcodeImports". +func upperCamelCase(s string) string { + parts := strings.Split(s, "_") + if len(parts) == 1 { + return capitalize(s) + } + b := strings.Builder{} + for _, p := range parts { + b.WriteString(capitalize(p)) + } + return b.String() +} + func capitalize(s string) string { r := []rune(s) r[0] = unicode.ToUpper(r[0]) diff --git a/cmd/sqlcup/usage.txt b/cmd/sqlcup/usage.txt index 13ff8b5..e36aaf4 100644 --- a/cmd/sqlcup/usage.txt +++ b/cmd/sqlcup/usage.txt @@ -5,8 +5,8 @@ Synopsis: Description: sqlcup prints SQL statements to stdout. The argument must be - of the form /. sqlcup capitalizes those names - where necessary. + of the form /. sqlcup converts those names to + upper camel case where necessary. Each column argument given to sqlcup defines a database column and must be either a or a :