From 0937960ca350989d3537493a6d2a9ac6ebb0ca79 Mon Sep 17 00:00:00 2001 From: Brandon Roehl Date: Mon, 18 Mar 2019 08:42:35 -0500 Subject: [PATCH 1/4] Switch statment when reading --- dump.go | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/dump.go b/dump.go index 4b8469e..fbfab1b 100644 --- a/dump.go +++ b/dump.go @@ -100,6 +100,8 @@ const footerTmpl = `/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; -- Dump completed on {{ .CompleteTime }} ` +const nullType = "NULL" + // Dump data using struct func (data *Data) Dump() error { meta := metaData{ @@ -309,27 +311,30 @@ func (data *Data) createTableValues(name string) (string, error) { for key, value := range values { if value == nil { - dataStrings[key] = "NULL" - } else if s, ok := value.(*sql.NullString); ok { - if s.Valid { - dataStrings[key] = "'" + sanitize(s.String) + "'" - } else { - dataStrings[key] = "NULL" - } - } else if s, ok := value.(*sql.NullInt64); ok { - if s.Valid { - dataStrings[key] = fmt.Sprintf("%d", s.Int64) - } else { - dataStrings[key] = "NULL" - } - } else if s, ok := value.(*sql.RawBytes); ok { - if len(*s) == 0 { - dataStrings[key] = "NULL" - } else { - dataStrings[key] = "_binary '" + sanitize(string(*s)) + "'" - } + dataStrings[key] = nullType } else { - dataStrings[key] = fmt.Sprint("'", value, "'") + switch s := value.(type) { + case *sql.NullString: + if s.Valid { + dataStrings[key] = "'" + sanitize(s.String) + "'" + } else { + dataStrings[key] = nullType + } + case *sql.NullInt64: + if s.Valid { + dataStrings[key] = fmt.Sprintf("%d", s.Int64) + } else { + dataStrings[key] = nullType + } + case *sql.RawBytes: + if len(*s) == 0 { + dataStrings[key] = nullType + } else { + dataStrings[key] = "_binary '" + sanitize(string(*s)) + "'" + } + default: + dataStrings[key] = fmt.Sprint("'", value, "'") + } } } From 9823f56968b29d8d2dcebdfb079ae5075538a5da Mon Sep 17 00:00:00 2001 From: Brandon Roehl Date: Mon, 18 Mar 2019 09:21:02 -0500 Subject: [PATCH 2/4] All in the templates --- dump.go | 21 ++++++++++++--------- dump_test.go | 2 +- mysqldump_test.go | 2 +- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/dump.go b/dump.go index fbfab1b..35a10d8 100644 --- a/dump.go +++ b/dump.go @@ -35,7 +35,7 @@ type Data struct { type table struct { Name string SQL string - Values string + Values []string } type metaData struct { @@ -81,7 +81,10 @@ DROP TABLE IF EXISTS {{ .Name }}; LOCK TABLES {{ .Name }} WRITE; /*!40000 ALTER TABLE {{ .Name }} DISABLE KEYS */; {{- if .Values }} -INSERT INTO {{ .Name }} VALUES {{ .Values }}; +INSERT INTO {{ .Name }} VALUES +{{- range $index, $element := .Values -}} +{{- if $index }},{{ else }} {{ end -}}{{ $element }} +{{- end -}}; {{- end }} /*!40000 ALTER TABLE {{ .Name }} ENABLE KEYS */; UNLOCK TABLES; @@ -262,25 +265,25 @@ func (data *Data) createTableSQL(name string) (string, error) { return tableSQL.String, nil } -func (data *Data) createTableValues(name string) (string, error) { +func (data *Data) createTableValues(name string) ([]string, error) { rows, err := data.Connection.Query("SELECT * FROM `" + name + "`") if err != nil { - return "", err + return nil, err } defer rows.Close() columns, err := rows.Columns() if err != nil { - return "", err + return nil, err } if len(columns) == 0 { - return "", errors.New("No columns in table " + name + ".") + return nil, errors.New("No columns in table " + name + ".") } dataText := make([]string, 0) tt, err := rows.ColumnTypes() if err != nil { - return "", err + return nil, err } types := make([]reflect.Type, len(tt)) @@ -304,7 +307,7 @@ func (data *Data) createTableValues(name string) (string, error) { } for rows.Next() { if err := rows.Scan(values...); err != nil { - return "", err + return dataText, err } dataStrings := make([]string, len(columns)) @@ -341,5 +344,5 @@ func (data *Data) createTableValues(name string) (string, error) { dataText = append(dataText, "("+strings.Join(dataStrings, ",")+")") } - return strings.Join(dataText, ","), rows.Err() + return dataText, rows.Err() } diff --git a/dump_test.go b/dump_test.go index 91ff4f6..1b9b09d 100644 --- a/dump_test.go +++ b/dump_test.go @@ -286,7 +286,7 @@ func TestCreateTableOk(t *testing.T) { expectedResult := &table{ Name: "`Test_Table`", SQL: "CREATE TABLE 'Test_Table' (`id` int(11) NOT NULL AUTO_INCREMENT,`s` char(60) DEFAULT NULL, PRIMARY KEY (`id`))ENGINE=InnoDB DEFAULT CHARSET=latin1", - Values: "('1',NULL,'Test Name 1'),('2','test2@test.de','Test Name 2')", + Values: []string{"('1',NULL,'Test Name 1')", "('2','test2@test.de','Test Name 2')"}, } if !reflect.DeepEqual(result, expectedResult) { diff --git a/mysqldump_test.go b/mysqldump_test.go index 0270255..eed34f3 100644 --- a/mysqldump_test.go +++ b/mysqldump_test.go @@ -96,6 +96,6 @@ UNLOCK TABLES; ` if !reflect.DeepEqual(result, expected) { - t.Fatalf("expected %#v, got %#v", expected, result) + t.Fatalf("expected \n%#v, got \n%#v", expected, result) } } From 63aca4f8a34f9b8c1b43d4bf1c1a0a4e3b3b1d76 Mon Sep 17 00:00:00 2001 From: Brandon Roehl Date: Mon, 18 Mar 2019 09:29:34 -0500 Subject: [PATCH 3/4] Fix test type --- dump_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dump_test.go b/dump_test.go index 1b9b09d..b4441ac 100644 --- a/dump_test.go +++ b/dump_test.go @@ -208,7 +208,7 @@ func TestCreateTableValuesOk(t *testing.T) { t.Errorf("there were unfulfilled expections: %s", err) } - expectedResult := "('1','test@test.de','Test Name 1'),('2','test2@test.de','Test Name 2')" + expectedResult := []string{"('1','test@test.de','Test Name 1')", "('2','test2@test.de','Test Name 2')"} if !reflect.DeepEqual(result, expectedResult) { t.Fatalf("expected %#v, got %#v", expectedResult, result) @@ -244,7 +244,7 @@ func TestCreateTableValuesNil(t *testing.T) { t.Errorf("there were unfulfilled expections: %s", err) } - expectedResult := "('1',NULL,'Test Name 1'),('2','test2@test.de','Test Name 2'),('3','','Test Name 3')" + expectedResult := []string{"('1',NULL,'Test Name 1')", "('2','test2@test.de','Test Name 2')", "('3','','Test Name 3')"} if !reflect.DeepEqual(result, expectedResult) { t.Fatalf("expected %#v, got %#v", expectedResult, result) From 6cb5948e03725b434e8704f61538509af16f3a0d Mon Sep 17 00:00:00 2001 From: Brandon Roehl Date: Mon, 18 Mar 2019 16:03:10 -0500 Subject: [PATCH 4/4] Version bump --- dump.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dump.go b/dump.go index 35a10d8..44ab13e 100644 --- a/dump.go +++ b/dump.go @@ -44,7 +44,7 @@ type metaData struct { CompleteTime string } -const version = "0.3.4" +const version = "0.3.5" const headerTmpl = `-- Go SQL Dump {{ .DumpVersion }} --