@@ -3,6 +3,7 @@ package mysqldump
3
3
import (
4
4
"database/sql"
5
5
"errors"
6
+ "fmt"
6
7
"io"
7
8
"strings"
8
9
"text/template"
@@ -70,10 +71,15 @@ UNLOCK TABLES;
70
71
-- Dump completed on {{ .CompleteTime }}
71
72
`
72
73
73
- // Creates a MYSQL Dump based on the options supplied through the dumper.
74
- func (d * Dumper ) Dump (w io.Writer , db string , tableName string ) error {
74
+ // Dump dumps one or more tables from a database into a writer
75
+ func (d * Dumper ) Dump (w io.Writer , db string , tables ... string ) error {
75
76
var err error
76
77
78
+ // Use the database
79
+ if _ , err = d .db .Exec ("USE " + db ); err != nil {
80
+ return fmt .Errorf ("use database: %w" , err )
81
+ }
82
+
77
83
data := dumpData {
78
84
DumpVersion : version ,
79
85
Tables : make ([]* tableData , 0 ),
@@ -85,10 +91,12 @@ func (d *Dumper) Dump(w io.Writer, db string, tableName string) error {
85
91
}
86
92
87
93
// Get sql for each table
88
- if t , err := createTable (d .db , tableName ); err == nil {
89
- data .Tables = append (data .Tables , t )
90
- } else {
91
- return err
94
+ for _ , t := range tables {
95
+ if t , err := createTable (d .db , t ); err == nil {
96
+ data .Tables = append (data .Tables , t )
97
+ } else {
98
+ return err
99
+ }
92
100
}
93
101
94
102
// Set complete time
@@ -102,7 +110,23 @@ func (d *Dumper) Dump(w io.Writer, db string, tableName string) error {
102
110
return t .Execute (w , data )
103
111
}
104
112
105
- func (d * Dumper ) GetTables () ([]string , error ) {
113
+ // DumpAllTables dumps all tables in a database into a writer
114
+ func (d * Dumper ) DumpAllTables (w io.Writer , db string ) error {
115
+ // Use the database
116
+ if _ , err := d .db .Exec ("USE " + db ); err != nil {
117
+ return fmt .Errorf ("use database: %w" , err )
118
+ }
119
+
120
+ // List tables in the database
121
+ tables , err := d .getTables ()
122
+ if err != nil {
123
+ return fmt .Errorf ("list tables: %w" , err )
124
+ }
125
+
126
+ return d .Dump (w , db , tables ... )
127
+ }
128
+
129
+ func (d * Dumper ) getTables () ([]string , error ) {
106
130
tables := make ([]string , 0 )
107
131
108
132
// Get table list
0 commit comments