Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lichunzhu committed Jul 7, 2020
1 parent 273c82f commit 7eb9742
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion v4/export/ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type RowReceiverStringer interface {

type Stringer interface {
WriteToBuffer(*bytes.Buffer, bool)
WriteToBufferInCsv(*bytes.Buffer, bool, csvOption)
WriteToBufferInCsv(*bytes.Buffer, bool, *csvOption)
}

type RowReceiver interface {
Expand Down
10 changes: 5 additions & 5 deletions v4/export/sql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (r RowReceiverArr) WriteToBuffer(bf *bytes.Buffer, escapeBackslash bool) {
bf.WriteByte(')')
}

func (r RowReceiverArr) WriteToBufferInCsv(bf *bytes.Buffer, escapeBackslash bool, opt csvOption) {
func (r RowReceiverArr) WriteToBufferInCsv(bf *bytes.Buffer, escapeBackslash bool, opt *csvOption) {
for i, receiver := range r {
receiver.WriteToBufferInCsv(bf, escapeBackslash, opt)
if i != len(r)-1 {
Expand All @@ -174,7 +174,7 @@ func (s SQLTypeNumber) WriteToBuffer(bf *bytes.Buffer, _ bool) {
}
}

func (s SQLTypeNumber) WriteToBufferInCsv(bf *bytes.Buffer, _ bool, opt csvOption) {
func (s SQLTypeNumber) WriteToBufferInCsv(bf *bytes.Buffer, _ bool, opt *csvOption) {
if s.RawBytes != nil {
bf.Write(s.RawBytes)
} else {
Expand Down Expand Up @@ -206,10 +206,10 @@ func (s *SQLTypeString) WriteToBuffer(bf *bytes.Buffer, escapeBackslash bool) {
}
}

func (s *SQLTypeString) WriteToBufferInCsv(bf *bytes.Buffer, escapeBackslash bool, opt csvOption) {
func (s *SQLTypeString) WriteToBufferInCsv(bf *bytes.Buffer, escapeBackslash bool, opt *csvOption) {
if s.RawBytes != nil {
bf.Write(opt.delimiter)
escape(s.RawBytes, bf, getEscapeQuotation(escapeBackslash, opt.separator))
escape(s.RawBytes, bf, getEscapeQuotation(escapeBackslash, opt.delimiter))
bf.Write(opt.delimiter)
} else {
bf.WriteString(opt.nullValue)
Expand All @@ -231,7 +231,7 @@ func (s *SQLTypeBytes) WriteToBuffer(bf *bytes.Buffer, _ bool) {
fmt.Fprintf(bf, "x'%x'", s.RawBytes)
}

func (s *SQLTypeBytes) WriteToBufferInCsv(bf *bytes.Buffer, _ bool, opt csvOption) {
func (s *SQLTypeBytes) WriteToBufferInCsv(bf *bytes.Buffer, _ bool, opt *csvOption) {
if s.RawBytes != nil {
bf.Write(opt.delimiter)
bf.Write(s.RawBytes)
Expand Down
14 changes: 1 addition & 13 deletions v4/export/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (m *mockTableIR) EscapeBackSlash() bool {
return m.escapeBackSlash
}

func newMockTableIR(databaseName, tableName string, data [][]driver.Value, specialComments, colTypes []string) TableDataIR {
func newMockTableIR(databaseName, tableName string, data [][]driver.Value, specialComments, colTypes []string) *mockTableIR {
return &mockTableIR{
dbName: databaseName,
tblName: tableName,
Expand All @@ -158,15 +158,3 @@ func newMockTableIR(databaseName, tableName string, data [][]driver.Value, speci
colTypes: colTypes,
}
}

func newMockTableIRWithError(databaseName, tableName string, data [][]driver.Value, specialComments, colTypes []string, err error) TableDataIR {
return &mockTableIR{
dbName: databaseName,
tblName: tableName,
data: data,
specCmt: specialComments,
selectedField: "*",
colTypes: colTypes,
rowErr: err,
}
}
2 changes: 1 addition & 1 deletion v4/export/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (f *CsvWriter) WriteTableData(ctx context.Context, ir TableDataIR) error {
chunksIter := buildChunksIter(ir, f.cfg.FileSize, f.cfg.StatementSize)
defer chunksIter.Rows().Close()

opt := csvOption{
opt := &csvOption{
nullValue: f.cfg.CsvNullValue,
separator: []byte(f.cfg.CsvSeparator),
delimiter: []byte(f.cfg.CsvDelimiter),
Expand Down
10 changes: 5 additions & 5 deletions v4/export/writer_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func WriteInsert(pCtx context.Context, tblIR TableDataIR, w io.Writer) error {
return wp.Error()
}

func WriteInsertInCsv(pCtx context.Context, tblIR TableDataIR, w io.Writer, noHeader bool, opt csvOption) error {
func WriteInsertInCsv(pCtx context.Context, tblIR TableDataIR, w io.Writer, noHeader bool, opt *csvOption) error {
fileRowIter := tblIR.Rows()
if !fileRowIter.HasNext() {
return nil
Expand Down Expand Up @@ -223,11 +223,11 @@ func WriteInsertInCsv(pCtx context.Context, tblIR TableDataIR, w io.Writer, noHe

if !noHeader && len(tblIR.ColumnNames()) != 0 {
for i, col := range tblIR.ColumnNames() {
bf.Write(opt.separator)
escape([]byte(col), bf, getEscapeQuotation(escapeBackSlash, opt.separator))
bf.Write(opt.separator)
bf.Write(opt.delimiter)
escape([]byte(col), bf, getEscapeQuotation(escapeBackSlash, opt.delimiter))
bf.Write(opt.delimiter)
if i != len(tblIR.ColumnTypes())-1 {
bf.WriteByte(',')
bf.Write(opt.separator)
}
}
bf.WriteByte('\n')
Expand Down
22 changes: 20 additions & 2 deletions v4/export/writer_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ func (s *testUtilSuite) TestWriteInsertReturnsError(c *C) {
}
// row errors at last line
rowErr := errors.New("mock row error")
tableIR := newMockTableIRWithError("test", "employee", data, specCmts, colTypes, rowErr)
tableIR := newMockTableIR("test", "employee", data, specCmts, colTypes)
tableIR.rowErr = rowErr
bf := &bytes.Buffer{}

err := WriteInsert(context.Background(), tableIR, bf)
Expand All @@ -112,7 +113,8 @@ func (s *testUtilSuite) TestWriteInsertInCsv(c *C) {
tableIR := newMockTableIR("test", "employee", data, nil, colTypes)
bf := &bytes.Buffer{}

opt := csvOption{separator: []byte(","), delimiter: doubleQuotationMark, nullValue: "\\N"}
// test nullValue
opt := &csvOption{separator: []byte(","), delimiter: doubleQuotationMark, nullValue: "\\N"}
err := WriteInsertInCsv(context.Background(), tableIR, bf, true, opt)
c.Assert(err, IsNil)
expected := "1,\"male\",\"[email protected]\",\"020-1234\",\\N\n" +
Expand All @@ -121,6 +123,7 @@ func (s *testUtilSuite) TestWriteInsertInCsv(c *C) {
"4,\"female\",\"[email protected]\",\"020-1235\",\"healthy\"\n"
c.Assert(bf.String(), Equals, expected)

// test delimiter
bf.Reset()
opt.delimiter = quotationMark
err = WriteInsertInCsv(context.Background(), tableIR, bf, true, opt)
Expand All @@ -131,6 +134,7 @@ func (s *testUtilSuite) TestWriteInsertInCsv(c *C) {
"4,'female','[email protected]','020-1235','healthy'\n"
c.Assert(bf.String(), Equals, expected)

// test separator
bf.Reset()
opt.separator = []byte(";")
err = WriteInsertInCsv(context.Background(), tableIR, bf, true, opt)
Expand All @@ -140,6 +144,20 @@ func (s *testUtilSuite) TestWriteInsertInCsv(c *C) {
"3;'male';'[email protected]';'020-1256';'healthy'\n" +
"4;'female';'[email protected]';'020-1235';'healthy'\n"
c.Assert(bf.String(), Equals, expected)

// test delimiter that included in values
bf.Reset()
opt.separator = []byte(",")
opt.delimiter = []byte("ma")
tableIR.colNames = []string{"id", "gender", "email", "phone_number", "status"}
err = WriteInsertInCsv(context.Background(), tableIR, bf, false, opt)
c.Assert(err, IsNil)
expected = "maidma,magenderma,maemamailma,maphone_numberma,mastatusma\n" +
"1,mamamalema,[email protected],ma020-1234ma,\\N\n" +
"2,mafemamalema,[email protected],ma020-1253ma,mahealthyma\n" +
"3,mamamalema,[email protected],ma020-1256ma,mahealthyma\n" +
"4,mafemamalema,[email protected],ma020-1235ma,mahealthyma\n"
c.Assert(bf.String(), Equals, expected)
}

func (s *testUtilSuite) TestSQLDataTypes(c *C) {
Expand Down

0 comments on commit 7eb9742

Please sign in to comment.