From c5ebb42f2cd0cd6d0306b3adf18a5df7c4257eb9 Mon Sep 17 00:00:00 2001 From: lukasmartinelli Date: Tue, 23 Aug 2016 09:50:45 +0200 Subject: [PATCH 1/2] Better PostgreSQL identifier check #26 --- .travis.yml | 1 + postgres.go | 21 ++++++++------------- postgres_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 13 deletions(-) create mode 100644 postgres_test.go diff --git a/.travis.yml b/.travis.yml index 6d61df1..293e08d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,3 +16,4 @@ install: - ./download_samples.sh script: - go install && ./test.sh + - go test diff --git a/postgres.go b/postgres.go index 9067a89..e196a1f 100644 --- a/postgres.go +++ b/postgres.go @@ -3,7 +3,9 @@ package main import ( "database/sql" "fmt" + "log" "math/rand" + "regexp" "strconv" "strings" @@ -53,24 +55,17 @@ func postgresify(identifier string) string { "-": "_", ",": "_", "#": "_", - - "[": "", - "]": "", - "{": "", - "}": "", - "(": "", - ")": "", - "?": "", - "!": "", - "$": "", - "%": "", - "*": "", - "\"": "", } for oldString, newString := range replacements { str = strings.Replace(str, oldString, newString, -1) } + reg, err := regexp.Compile("[^A-Za-z0-9_]+") + if err != nil { + log.Fatal(err) + } + str = reg.ReplaceAllString(str, "") + if len(str) == 0 { str = fmt.Sprintf("_col%d", rand.Intn(10000)) } else { diff --git a/postgres_test.go b/postgres_test.go new file mode 100644 index 0000000..cb3ca41 --- /dev/null +++ b/postgres_test.go @@ -0,0 +1,24 @@ +package main + +import "testing" + +type testpair struct { + columnName string + sanitizedName string +} + +var tests = []testpair{ + {"Starting Date & Time", "starting_date__time"}, + {"[$MYCOLUMN]", "mycolumn"}, + {"({colname?!})", "colname"}, + {"m4 * 4 / 3", "m4__4___3"}, +} + +func TestPostgresify(t *testing.T) { + for _, pair := range tests { + str := postgresify(pair.columnName) + if str != pair.sanitizedName { + t.Error("Invalid PostgreSQL identifier expected ", pair.sanitizedName, "got ", str) + } + } +} From 031674fc59834f9d36d62416ad85101ba56d969c Mon Sep 17 00:00:00 2001 From: lukasmartinelli Date: Tue, 23 Aug 2016 10:08:13 +0200 Subject: [PATCH 2/2] Return error in CLI funcs --- pgfutter.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pgfutter.go b/pgfutter.go index 468abac..a9cc7d5 100644 --- a/pgfutter.go +++ b/pgfutter.go @@ -92,7 +92,7 @@ func main() { { Name: "json", Usage: "Import newline-delimited JSON objects into database", - Action: func(c *cli.Context) { + Action: func(c *cli.Context) error { cli.CommandHelpTemplate = strings.Replace(cli.CommandHelpTemplate, "[arguments...]", "", -1) filename := c.Args().First() @@ -103,13 +103,13 @@ func main() { connStr := parseConnStr(c) err := importJSON(filename, connStr, schema, tableName, ignoreErrors) - exitOnError(err) + return err }, }, { Name: "jsonobj", Usage: "Import single JSON object into database", - Action: func(c *cli.Context) { + Action: func(c *cli.Context) error { cli.CommandHelpTemplate = strings.Replace(cli.CommandHelpTemplate, "[arguments...]", "", -1) filename := c.Args().First() @@ -119,7 +119,7 @@ func main() { connStr := parseConnStr(c) err := importJSONObject(filename, connStr, schema, tableName) - exitOnError(err) + return err }, }, { @@ -140,7 +140,7 @@ func main() { Usage: "field delimiter", }, }, - Action: func(c *cli.Context) { + Action: func(c *cli.Context) error { cli.CommandHelpTemplate = strings.Replace(cli.CommandHelpTemplate, "[arguments...]", "", -1) filename := c.Args().First() @@ -155,7 +155,7 @@ func main() { connStr := parseConnStr(c) err := importCSV(filename, connStr, schema, tableName, ignoreErrors, skipHeader, fields, delimiter) - exitOnError(err) + return err }, }, }