Skip to content

Commit

Permalink
Merge pull request JamesStewy#11 from christianruhstaller/bugfix-nil
Browse files Browse the repository at this point in the history
Add handling for nil values
  • Loading branch information
JamesStewy authored Jun 24, 2018
2 parents e653992 + b9e7e65 commit d04ec8e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
6 changes: 4 additions & 2 deletions dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,13 @@ func createTableValues(db *sql.DB, name string) (string, error) {

for key, value := range data {
if value != nil && value.Valid {
dataStrings[key] = value.String
dataStrings[key] = "'" + value.String + "'"
} else {
dataStrings[key] = "null"
}
}

data_text = append(data_text, "('"+strings.Join(dataStrings, "','")+"')")
data_text = append(data_text, "("+strings.Join(dataStrings, ",")+")")
}

return strings.Join(data_text, ","), rows.Err()
Expand Down
9 changes: 5 additions & 4 deletions dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ func TestCreateTableValuesNil(t *testing.T) {

rows := sqlmock.NewRows([]string{"id", "email", "name"}).
AddRow(1, nil, "Test Name 1").
AddRow(2, "[email protected]", "Test Name 2")
AddRow(2, "[email protected]", "Test Name 2").
AddRow(3, "", "Test Name 3")

mock.ExpectQuery("^SELECT (.+) FROM test$").WillReturnRows(rows)

Expand All @@ -189,7 +190,7 @@ func TestCreateTableValuesNil(t *testing.T) {
t.Errorf("there were unfulfilled expections: %s", err)
}

expectedResult := "('1','','Test Name 1'),('2','[email protected]','Test Name 2')"
expectedResult := "('1',null,'Test Name 1'),('2','[email protected]','Test Name 2'),('3','','Test Name 3')"

if !reflect.DeepEqual(result, expectedResult) {
t.Fatalf("expected %#v, got %#v", expectedResult, result)
Expand Down Expand Up @@ -227,7 +228,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','','Test Name 1'),('2','[email protected]','Test Name 2')",
Values: "('1',null,'Test Name 1'),('2','[email protected]','Test Name 2')",
}

if !reflect.DeepEqual(result, expectedResult) {
Expand Down Expand Up @@ -323,7 +324,7 @@ CREATE TABLE 'Test_Table' (\id\ int(11) NOT NULL AUTO_INCREMENT,\email\ char(60)
LOCK TABLES Test_Table WRITE;
/*!40000 ALTER TABLE Test_Table DISABLE KEYS */;
INSERT INTO Test_Table VALUES ('1','','Test Name 1'),('2','[email protected]','Test Name 2');
INSERT INTO Test_Table VALUES ('1',null,'Test Name 1'),('2','[email protected]','Test Name 2');
/*!40000 ALTER TABLE Test_Table ENABLE KEYS */;
UNLOCK TABLES;
Expand Down

0 comments on commit d04ec8e

Please sign in to comment.