Skip to content

Commit a36198f

Browse files
AndrewDikennytm
andauthored
export: add CsvNullValue Cli usage (pingcap#58)
* add CsvNullValue Cli usage * Update cmd/dumpling/main.go Co-Authored-By: kennytm <[email protected]> Co-authored-by: kennytm <[email protected]>
1 parent 37d83d2 commit a36198f

File tree

7 files changed

+15
-11
lines changed

7 files changed

+15
-11
lines changed

cmd/dumpling/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var (
4545
noHeader bool
4646
noSchemas bool
4747
noData bool
48+
csvNullValue string
4849

4950
escapeBackslash bool
5051

@@ -86,6 +87,7 @@ func init() {
8687
rootCmd.PersistentFlags().BoolVar(&noHeader, "no-header", false, "whether not to dump CSV table header")
8788
rootCmd.PersistentFlags().BoolVarP(&noSchemas, "no-schemas", "m", false, "Do not dump table schemas with the data")
8889
rootCmd.PersistentFlags().BoolVarP(&noData, "no-data", "d", false, "Do not dump table data")
90+
rootCmd.PersistentFlags().StringVar(&csvNullValue, "csv-null-value", "\\N", "The null value used when export to csv")
8991
}
9092

9193
func run() {
@@ -112,6 +114,7 @@ func run() {
112114
conf.NoHeader = noHeader
113115
conf.NoSchemas = noSchemas
114116
conf.NoData = noData
117+
conf.CsvNullValue = csvNullValue
115118

116119
err := export.Dump(conf)
117120
if err != nil {

v4/export/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type Config struct {
3434
NoHeader bool
3535
NoSchemas bool
3636
NoData bool
37+
CsvNullValue string
3738

3839
BlackWhiteList BWListConf
3940
Rows uint64
@@ -67,6 +68,7 @@ func DefaultConfig() *Config {
6768
NoHeader: false,
6869
NoSchemas: false,
6970
NoData: false,
71+
CsvNullValue: "\\N",
7072
}
7173
}
7274

v4/export/ir.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type RowReceiverStringer interface {
3838

3939
type Stringer interface {
4040
WriteToBuffer(*bytes.Buffer, bool)
41-
WriteToBufferInCsv(*bytes.Buffer, bool)
41+
WriteToBufferInCsv(*bytes.Buffer, bool, string)
4242
}
4343

4444
type RowReceiver interface {

v4/export/sql_type.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
var colTypeRowReceiverMap = map[string]func() RowReceiverStringer{}
1010

1111
var nullValue = "NULL"
12-
var csvNullValue = "\\N"
1312
var quotationMark byte = '\''
1413
var doubleQuotationMark byte = '"'
1514
var quotationMarkNotQuote = []byte{quotationMark}
@@ -148,9 +147,9 @@ func (r RowReceiverArr) WriteToBuffer(bf *bytes.Buffer, escapeBackslash bool) {
148147
bf.WriteByte(')')
149148
}
150149

151-
func (r RowReceiverArr) WriteToBufferInCsv(bf *bytes.Buffer, escapeBackslash bool) {
150+
func (r RowReceiverArr) WriteToBufferInCsv(bf *bytes.Buffer, escapeBackslash bool, csvNullValue string) {
152151
for i, receiver := range r {
153-
receiver.WriteToBufferInCsv(bf, escapeBackslash)
152+
receiver.WriteToBufferInCsv(bf, escapeBackslash, csvNullValue)
154153
if i != len(r)-1 {
155154
bf.WriteByte(',')
156155
}
@@ -169,7 +168,7 @@ func (s SQLTypeNumber) WriteToBuffer(bf *bytes.Buffer, _ bool) {
169168
}
170169
}
171170

172-
func (s SQLTypeNumber) WriteToBufferInCsv(bf *bytes.Buffer, _ bool) {
171+
func (s SQLTypeNumber) WriteToBufferInCsv(bf *bytes.Buffer, _ bool, csvNullValue string) {
173172
if s.RawBytes != nil {
174173
bf.Write(s.RawBytes)
175174
} else {
@@ -201,7 +200,7 @@ func (s *SQLTypeString) WriteToBuffer(bf *bytes.Buffer, escapeBackslash bool) {
201200
}
202201
}
203202

204-
func (s *SQLTypeString) WriteToBufferInCsv(bf *bytes.Buffer, escapeBackslash bool) {
203+
func (s *SQLTypeString) WriteToBufferInCsv(bf *bytes.Buffer, escapeBackslash bool, csvNullValue string) {
205204
if s.RawBytes != nil {
206205
bf.WriteByte(doubleQuotationMark)
207206
escape(s.RawBytes, bf, escapeBackslash)
@@ -226,7 +225,7 @@ func (s *SQLTypeBytes) WriteToBuffer(bf *bytes.Buffer, _ bool) {
226225
fmt.Fprintf(bf, "x'%x'", s.RawBytes)
227226
}
228227

229-
func (s *SQLTypeBytes) WriteToBufferInCsv(bf *bytes.Buffer, _ bool) {
228+
func (s *SQLTypeBytes) WriteToBufferInCsv(bf *bytes.Buffer, _ bool, csvNullValue string) {
230229
if s.RawBytes != nil {
231230
bf.WriteByte(doubleQuotationMark)
232231
bf.Write(s.RawBytes)

v4/export/writer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (f *CsvWriter) WriteTableData(ctx context.Context, ir TableDataIR) error {
123123
for {
124124
filePath := path.Join(f.cfg.OutputDirPath, fileName)
125125
fileWriter, tearDown := buildInterceptFileWriter(filePath)
126-
err := WriteInsertInCsv(chunksIter, fileWriter, f.cfg.NoHeader)
126+
err := WriteInsertInCsv(chunksIter, fileWriter, f.cfg.NoHeader, f.cfg.CsvNullValue)
127127
tearDown()
128128
if err != nil {
129129
return err

v4/export/writer_util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func WriteInsert(tblIR TableDataIR, w io.Writer) error {
182182
return wp.Error()
183183
}
184184

185-
func WriteInsertInCsv(tblIR TableDataIR, w io.Writer, noHeader bool) error {
185+
func WriteInsertInCsv(tblIR TableDataIR, w io.Writer, noHeader bool, csvNullValue string) error {
186186
fileRowIter := tblIR.Rows()
187187
if !fileRowIter.HasNext() {
188188
return nil
@@ -234,7 +234,7 @@ func WriteInsertInCsv(tblIR TableDataIR, w io.Writer, noHeader bool) error {
234234
return err
235235
}
236236

237-
row.WriteToBufferInCsv(bf, escapeBackSlash)
237+
row.WriteToBufferInCsv(bf, escapeBackSlash, csvNullValue)
238238
counter += 1
239239

240240
if bf.Len() >= lengthLimit {

v4/export/writer_util_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (s *testUtilSuite) TestWriteInsertInCsv(c *C) {
8282
tableIR := newMockTableIR("test", "employee", data, nil, colTypes)
8383
bf := &bytes.Buffer{}
8484

85-
err := WriteInsertInCsv(tableIR, bf, true)
85+
err := WriteInsertInCsv(tableIR, bf, true, "\\N")
8686
c.Assert(err, IsNil)
8787
expected := "1,\"male\",\"[email protected]\",\"020-1234\",\\N\n" +
8888
"2,\"female\",\"[email protected]\",\"020-1253\",\"healthy\"\n" +

0 commit comments

Comments
 (0)