Skip to content

Commit f61ba94

Browse files
authored
Merge pull request #1 from BrandonRoehl/master
Reduce the memory usage by leaving values as the arrays they are
2 parents d22e068 + 6cb5948 commit f61ba94

File tree

3 files changed

+42
-34
lines changed

3 files changed

+42
-34
lines changed

dump.go

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type Data struct {
3535
type table struct {
3636
Name string
3737
SQL string
38-
Values string
38+
Values []string
3939
}
4040

4141
type metaData struct {
@@ -44,7 +44,7 @@ type metaData struct {
4444
CompleteTime string
4545
}
4646

47-
const version = "0.3.4"
47+
const version = "0.3.5"
4848

4949
const headerTmpl = `-- Go SQL Dump {{ .DumpVersion }}
5050
--
@@ -81,7 +81,10 @@ DROP TABLE IF EXISTS {{ .Name }};
8181
LOCK TABLES {{ .Name }} WRITE;
8282
/*!40000 ALTER TABLE {{ .Name }} DISABLE KEYS */;
8383
{{- if .Values }}
84-
INSERT INTO {{ .Name }} VALUES {{ .Values }};
84+
INSERT INTO {{ .Name }} VALUES
85+
{{- range $index, $element := .Values -}}
86+
{{- if $index }},{{ else }} {{ end -}}{{ $element }}
87+
{{- end -}};
8588
{{- end }}
8689
/*!40000 ALTER TABLE {{ .Name }} ENABLE KEYS */;
8790
UNLOCK TABLES;
@@ -100,6 +103,8 @@ const footerTmpl = `/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
100103
-- Dump completed on {{ .CompleteTime }}
101104
`
102105

106+
const nullType = "NULL"
107+
103108
// Dump data using struct
104109
func (data *Data) Dump() error {
105110
meta := metaData{
@@ -260,25 +265,25 @@ func (data *Data) createTableSQL(name string) (string, error) {
260265
return tableSQL.String, nil
261266
}
262267

263-
func (data *Data) createTableValues(name string) (string, error) {
268+
func (data *Data) createTableValues(name string) ([]string, error) {
264269
rows, err := data.Connection.Query("SELECT * FROM `" + name + "`")
265270
if err != nil {
266-
return "", err
271+
return nil, err
267272
}
268273
defer rows.Close()
269274

270275
columns, err := rows.Columns()
271276
if err != nil {
272-
return "", err
277+
return nil, err
273278
}
274279
if len(columns) == 0 {
275-
return "", errors.New("No columns in table " + name + ".")
280+
return nil, errors.New("No columns in table " + name + ".")
276281
}
277282

278283
dataText := make([]string, 0)
279284
tt, err := rows.ColumnTypes()
280285
if err != nil {
281-
return "", err
286+
return nil, err
282287
}
283288

284289
types := make([]reflect.Type, len(tt))
@@ -302,39 +307,42 @@ func (data *Data) createTableValues(name string) (string, error) {
302307
}
303308
for rows.Next() {
304309
if err := rows.Scan(values...); err != nil {
305-
return "", err
310+
return dataText, err
306311
}
307312

308313
dataStrings := make([]string, len(columns))
309314

310315
for key, value := range values {
311316
if value == nil {
312-
dataStrings[key] = "NULL"
313-
} else if s, ok := value.(*sql.NullString); ok {
314-
if s.Valid {
315-
dataStrings[key] = "'" + sanitize(s.String) + "'"
316-
} else {
317-
dataStrings[key] = "NULL"
318-
}
319-
} else if s, ok := value.(*sql.NullInt64); ok {
320-
if s.Valid {
321-
dataStrings[key] = fmt.Sprintf("%d", s.Int64)
322-
} else {
323-
dataStrings[key] = "NULL"
324-
}
325-
} else if s, ok := value.(*sql.RawBytes); ok {
326-
if len(*s) == 0 {
327-
dataStrings[key] = "NULL"
328-
} else {
329-
dataStrings[key] = "_binary '" + sanitize(string(*s)) + "'"
330-
}
317+
dataStrings[key] = nullType
331318
} else {
332-
dataStrings[key] = fmt.Sprint("'", value, "'")
319+
switch s := value.(type) {
320+
case *sql.NullString:
321+
if s.Valid {
322+
dataStrings[key] = "'" + sanitize(s.String) + "'"
323+
} else {
324+
dataStrings[key] = nullType
325+
}
326+
case *sql.NullInt64:
327+
if s.Valid {
328+
dataStrings[key] = fmt.Sprintf("%d", s.Int64)
329+
} else {
330+
dataStrings[key] = nullType
331+
}
332+
case *sql.RawBytes:
333+
if len(*s) == 0 {
334+
dataStrings[key] = nullType
335+
} else {
336+
dataStrings[key] = "_binary '" + sanitize(string(*s)) + "'"
337+
}
338+
default:
339+
dataStrings[key] = fmt.Sprint("'", value, "'")
340+
}
333341
}
334342
}
335343

336344
dataText = append(dataText, "("+strings.Join(dataStrings, ",")+")")
337345
}
338346

339-
return strings.Join(dataText, ","), rows.Err()
347+
return dataText, rows.Err()
340348
}

dump_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func TestCreateTableValuesOk(t *testing.T) {
208208
t.Errorf("there were unfulfilled expections: %s", err)
209209
}
210210

211-
expectedResult := "('1','[email protected]','Test Name 1'),('2','[email protected]','Test Name 2')"
211+
expectedResult := []string{"('1','[email protected]','Test Name 1')", "('2','[email protected]','Test Name 2')"}
212212

213213
if !reflect.DeepEqual(result, expectedResult) {
214214
t.Fatalf("expected %#v, got %#v", expectedResult, result)
@@ -244,7 +244,7 @@ func TestCreateTableValuesNil(t *testing.T) {
244244
t.Errorf("there were unfulfilled expections: %s", err)
245245
}
246246

247-
expectedResult := "('1',NULL,'Test Name 1'),('2','[email protected]','Test Name 2'),('3','','Test Name 3')"
247+
expectedResult := []string{"('1',NULL,'Test Name 1')", "('2','[email protected]','Test Name 2')", "('3','','Test Name 3')"}
248248

249249
if !reflect.DeepEqual(result, expectedResult) {
250250
t.Fatalf("expected %#v, got %#v", expectedResult, result)
@@ -286,7 +286,7 @@ func TestCreateTableOk(t *testing.T) {
286286
expectedResult := &table{
287287
Name: "`Test_Table`",
288288
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",
289-
Values: "('1',NULL,'Test Name 1'),('2','[email protected]','Test Name 2')",
289+
Values: []string{"('1',NULL,'Test Name 1')", "('2','[email protected]','Test Name 2')"},
290290
}
291291

292292
if !reflect.DeepEqual(result, expectedResult) {

mysqldump_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,6 @@ UNLOCK TABLES;
9696
`
9797

9898
if !reflect.DeepEqual(result, expected) {
99-
t.Fatalf("expected %#v, got %#v", expected, result)
99+
t.Fatalf("expected \n%#v, got \n%#v", expected, result)
100100
}
101101
}

0 commit comments

Comments
 (0)