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

Special character issue in value fixed #22

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Go MYSQL Dump
Create MYSQL dumps in Go without the `mysqldump` CLI as a dependancy.
If you wish, you can only get the data of the tables you want.
```
first parameter get all data
second parameter get data of specified tables only

dumper.Dump(true,[]string{"tableName1"})
```

### Simple Example
```go
Expand Down Expand Up @@ -53,3 +60,4 @@ func main() {

[![GoDoc](https://godoc.org/github.com/JamesStewy/go-mysqldump?status.svg)](https://godoc.org/github.com/JamesStewy/go-mysqldump)
[![Build Status](https://travis-ci.org/JamesStewy/go-mysqldump.svg?branch=master)](https://travis-ci.org/JamesStewy/go-mysqldump)

42 changes: 28 additions & 14 deletions dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"os"
"path"
"regexp"
"strings"
"text/template"
"time"
Expand All @@ -25,7 +26,7 @@ type dump struct {

const version = "0.2.2"

const tmpl = `-- Go SQL Dump {{ .DumpVersion }}
const tmplWithData = `-- Go SQL Dump {{ .DumpVersion }}
--
-- ------------------------------------------------------
-- Server version {{ .ServerVersion }}
Expand Down Expand Up @@ -56,19 +57,26 @@ DROP TABLE IF EXISTS {{ .Name }};
-- Dumping data for table {{ .Name }}
--

LOCK TABLES {{ .Name }} WRITE;
/*!40000 ALTER TABLE {{ .Name }} DISABLE KEYS */;
BEGIN;
{{ if .Values }}
INSERT INTO {{ .Name }} VALUES {{ .Values }};
{{ end }}
/*!40000 ALTER TABLE {{ .Name }} ENABLE KEYS */;
UNLOCK TABLES;
COMMIT;
{{ end }}
-- Dump completed on {{ .CompleteTime }}
`

func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}

// Creates a MYSQL Dump based on the options supplied through the dumper.
func (d *Dumper) Dump() (string, error) {
func (d *Dumper) Dump(withData bool, tableNames []string) (string, error) {
name := time.Now().Format(d.format)
p := path.Join(d.dir, name+".sql")

Expand Down Expand Up @@ -104,18 +112,16 @@ func (d *Dumper) Dump() (string, error) {

// Get sql for each table
for _, name := range tables {
if t, err := createTable(d.db, name); err == nil {
if t, err := createTable(d.db, name, withData, tableNames); err == nil {
data.Tables = append(data.Tables, t)
} else {
return p, err
}
}

// Set complete time
data.CompleteTime = time.Now().String()

// Write dump to file
t, err := template.New("mysqldump").Parse(tmpl)
t, err := template.New("mysqldump").Parse(tmplWithData)
if err != nil {
return p, err
}
Expand Down Expand Up @@ -155,16 +161,19 @@ func getServerVersion(db *sql.DB) (string, error) {
return server_version.String, nil
}

func createTable(db *sql.DB, name string) (*table, error) {
func createTable(db *sql.DB, name string, withData bool, tableNames []string) (*table, error) {
var err error
t := &table{Name: name}

if t.SQL, err = createTableSQL(db, name); err != nil {
return nil, err
}

if t.Values, err = createTableValues(db, name); err != nil {
return nil, err
if withData || contains(tableNames, name) {
t.Values, err = createTableValues(db, name)
if err != nil {
return nil, err
}
}

return t, nil
Expand Down Expand Up @@ -226,7 +235,7 @@ 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] = "'" + valueReplace(value.String) + "'"
} else {
dataStrings[key] = "null"
}
Expand All @@ -237,3 +246,8 @@ func createTableValues(db *sql.DB, name string) (string, error) {

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

func valueReplace(value string) string {
re := regexp.MustCompile(`["']`)
return re.ReplaceAllString(value, "\\${0}${1}")
}