Skip to content

Commit d04ec8e

Browse files
authored
Merge pull request #11 from christianruhstaller/bugfix-nil
Add handling for nil values
2 parents e653992 + b9e7e65 commit d04ec8e

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

dump.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,13 @@ func createTableValues(db *sql.DB, name string) (string, error) {
226226

227227
for key, value := range data {
228228
if value != nil && value.Valid {
229-
dataStrings[key] = value.String
229+
dataStrings[key] = "'" + value.String + "'"
230+
} else {
231+
dataStrings[key] = "null"
230232
}
231233
}
232234

233-
data_text = append(data_text, "('"+strings.Join(dataStrings, "','")+"')")
235+
data_text = append(data_text, "("+strings.Join(dataStrings, ",")+")")
234236
}
235237

236238
return strings.Join(data_text, ","), rows.Err()

dump_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ func TestCreateTableValuesNil(t *testing.T) {
175175

176176
rows := sqlmock.NewRows([]string{"id", "email", "name"}).
177177
AddRow(1, nil, "Test Name 1").
178-
AddRow(2, "[email protected]", "Test Name 2")
178+
AddRow(2, "[email protected]", "Test Name 2").
179+
AddRow(3, "", "Test Name 3")
179180

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

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

192-
expectedResult := "('1','','Test Name 1'),('2','[email protected]','Test Name 2')"
193+
expectedResult := "('1',null,'Test Name 1'),('2','[email protected]','Test Name 2'),('3','','Test Name 3')"
193194

194195
if !reflect.DeepEqual(result, expectedResult) {
195196
t.Fatalf("expected %#v, got %#v", expectedResult, result)
@@ -227,7 +228,7 @@ func TestCreateTableOk(t *testing.T) {
227228
expectedResult := &table{
228229
Name: "Test_Table",
229230
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",
230-
Values: "('1','','Test Name 1'),('2','[email protected]','Test Name 2')",
231+
Values: "('1',null,'Test Name 1'),('2','[email protected]','Test Name 2')",
231232
}
232233

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

0 commit comments

Comments
 (0)