forked from JamesStewy/go-mysqldump
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmysql.go
124 lines (104 loc) · 2.96 KB
/
mysql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package sqldump
import (
"database/sql"
"errors"
"text/template"
"time"
)
const mytpl = `-- Go SQL Dump {{ .DumpVersion }}
--
-- ------------------------------------------------------
-- Server version {{ .ServerVersion }}
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
{{range .Tables}}
--
-- Table structure for table {{ .Name }}
--
DROP TABLE IF EXISTS {{ .Name }};
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
{{ .SQL }};
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table {{ .Name }}
--
LOCK TABLES {{ .Name }} WRITE;
/*!40000 ALTER TABLE {{ .Name }} DISABLE KEYS */;
{{ if .Values }}
INSERT INTO {{ .Name }} VALUES {{ .Values }};
{{ end }}
/*!40000 ALTER TABLE {{ .Name }} ENABLE KEYS */;
UNLOCK TABLES;
{{ end }}
-- Dump completed on {{ .CompleteTime }}
`
// DumpMySQL to file.
func (d *Dumper) DumpMySQL(data dump, list ...string) error {
// Get sql for each desired table
for _, name := range list {
if t, err := d.createMySQLTable(name); err == nil {
data.Tables = append(data.Tables, t)
} else {
return err
}
}
// Set complete time
data.CompleteTime = time.Now().String()
// Write dump to file
t, err := template.New("mysqldump").Parse(mytpl)
if err != nil {
return err
}
return t.Execute(data.file, data)
}
func (d *Dumper) getMySQLTables() ([]string, error) {
tables := make([]string, 0)
// Get table list
rows, err := d.db.Query("SHOW TABLES")
if err != nil {
return tables, err
}
defer rows.Close()
// Read result
for rows.Next() {
var table sql.NullString
if err := rows.Scan(&table); err != nil {
return tables, err
}
tables = append(tables, table.String)
}
return tables, rows.Err()
}
func (d *Dumper) createMySQLTable(name string) (*table, error) {
var err error
t := &table{Name: name}
if t.SQL, err = d.createMySQLTableSQL(name); err != nil {
return nil, err
}
if t.Values, err = d.createTableValues(name, 0, 0); err != nil {
return nil, err
}
return t, nil
}
func (d *Dumper) createMySQLTableSQL(name string) (string, error) {
// Get table creation SQL
var table_return sql.NullString
var table_sql sql.NullString
err := d.db.QueryRow("SHOW CREATE TABLE "+name).Scan(&table_return, &table_sql)
if err != nil {
return "", err
}
if table_return.String != name {
return "", errors.New("Returned table is not the same as requested table")
}
return table_sql.String, nil
}