Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the memory usage by leaving values as the arrays they are #1

Merged
merged 4 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 38 additions & 30 deletions dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Data struct {
type table struct {
Name string
SQL string
Values string
Values []string
}

type metaData struct {
Expand All @@ -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 }}
--
Expand Down Expand Up @@ -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;
Expand All @@ -100,6 +103,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{
Expand Down Expand Up @@ -260,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))
Expand All @@ -302,39 +307,42 @@ 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))

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, "'")
}
}
}

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

return strings.Join(dataText, ","), rows.Err()
return dataText, rows.Err()
}
6 changes: 3 additions & 3 deletions dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func TestCreateTableValuesOk(t *testing.T) {
t.Errorf("there were unfulfilled expections: %s", err)
}

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

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

expectedResult := "('1',NULL,'Test Name 1'),('2','[email protected]','Test Name 2'),('3','','Test Name 3')"
expectedResult := []string{"('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 @@ -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','[email protected]','Test Name 2')",
Values: []string{"('1',NULL,'Test Name 1')", "('2','[email protected]','Test Name 2')"},
}

if !reflect.DeepEqual(result, expectedResult) {
Expand Down
2 changes: 1 addition & 1 deletion mysqldump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}