Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
180 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ nohup.log | |
run.bat | ||
stations.ini | ||
result.sql | ||
result.csv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type CsvWriter struct { | ||
fw FileWriter | ||
dstFile string | ||
dstDir string | ||
delimiter string | ||
} | ||
|
||
func NewCsvWriter(fw FileWriter, dstFile, dstDir, delimiter string) *CsvWriter { | ||
return &CsvWriter{ | ||
fw, | ||
dstFile, | ||
dstDir, | ||
delimiter, | ||
} | ||
} | ||
|
||
func (w *CsvWriter) WriteDDL(tableName string, ddl string) (err error) { | ||
return | ||
} | ||
|
||
func (w *CsvWriter) WriteRows(tableName string, columns []string, rows []*map[string]interface{}) (err error) { | ||
f, err := w.fw.getFileHandler(w.getFilename(tableName)) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
columnsNames := make([]string, 0) | ||
for _, column := range columns { | ||
columnsNames = append(columnsNames, escapeCsvString(column)) | ||
} | ||
_, err = f.WriteString(strings.Join(columnsNames, w.delimiter) + "\r\n") | ||
if err != nil { | ||
return fmt.Errorf("Error at writing header to file: %s", err) | ||
} | ||
for _, row := range rows { | ||
values := make([]string, 0) | ||
for _, field := range columns { | ||
v := (*row)[field] | ||
value := "" | ||
switch typedValue := v.(type) { | ||
case int: | ||
value = fmt.Sprintf("%d", typedValue) | ||
break | ||
case int64: | ||
value = fmt.Sprintf("%d", typedValue) | ||
break | ||
case float64: | ||
value = fmt.Sprintf("%f", typedValue) | ||
break | ||
case string: | ||
value = escapeCsvString(typedValue) | ||
break | ||
case []uint8: | ||
value = escapeCsvString(fmt.Sprintf("%s", typedValue)) | ||
case nil: | ||
value = "NULL" | ||
default: | ||
value = "UNDEFINED" | ||
} | ||
values = append(values, value) | ||
} | ||
_, err = f.WriteString(strings.Join(values, w.delimiter) + "\r\n") | ||
if err != nil { | ||
return fmt.Errorf("Error at writing rows to file: %s", err) | ||
} | ||
} | ||
return | ||
} | ||
|
||
func (w *CsvWriter) getFilename(tableName string) (filename string) { | ||
if w.dstDir != "" { | ||
return w.dstDir + "/" + tableName + ".csv" | ||
} | ||
return w.dstFile | ||
} | ||
|
||
func escapeCsvString(str string) string { | ||
str = strings.Replace(str, "\"", "\"\"", -1) | ||
return "\"" + str + "\"" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestCsvWriterWriteRows(t *testing.T) { | ||
fw := NewTestFileWriter() | ||
writer := NewCsvWriter(fw, "result.csv", "", ",") | ||
rows := make([]*map[string]interface{}, 0) | ||
rows = append(rows, &map[string]interface{}{ | ||
"name": "one", | ||
"title": "t\"wo", | ||
"id": int(123), | ||
"value": int64(456), | ||
"amount": 1.23, | ||
"chars": []uint8{0x26, 0x23, 0x29}, | ||
"nulled": nil, | ||
"strange": uintptr(1), | ||
}) | ||
rows = append(rows, &map[string]interface{}{ | ||
"name": "four", | ||
"title": "five", | ||
"id": int(789), | ||
"value": int64(345), | ||
"amount": 2.23, | ||
"chars": []uint8{0x23, 0x23, 0x29}, | ||
"nulled": nil, | ||
"strange": uintptr(1), | ||
}) | ||
|
||
writer.WriteRows("some_table", []string{"name", "title", "id", "value", "amount", "chars", "nulled", "strange"}, rows) | ||
result := fw.getContents("result.csv") | ||
expected := "\"name\",\"title\",\"id\",\"value\",\"amount\",\"chars\",\"nulled\",\"strange\"\r\n" | ||
expected += "\"one\",\"t\"\"wo\",123,456,1.230000,\"&#)\",NULL,UNDEFINED\r\n" | ||
expected += "\"four\",\"five\",789,345,2.230000,\"##)\",NULL,UNDEFINED\r\n" | ||
if expected != result { | ||
t.Errorf("Expected:\n%sGot:\n%s", expected, result) | ||
} | ||
} | ||
|
||
func TestCsvWriterWriteDDL(t *testing.T) { | ||
fw := NewTestFileWriter() | ||
writer := NewCsvWriter(fw, "result.csv", "", ",") | ||
err := writer.WriteDDL("some_table", "") | ||
if err != nil { | ||
t.Errorf("Unexpected error: ", err) | ||
} | ||
} | ||
|
||
func TestCsvWriterWriteRowsFileWriteError(t *testing.T) { | ||
fw := &TestFileErrorWriter{} | ||
writer := NewCsvWriter(fw, "result.csv", "", ",") | ||
rows := make([]*map[string]interface{}, 0) | ||
rows = append(rows, &map[string]interface{}{"name": "one"}) | ||
err := writer.WriteRows("some_table", []string{"name"}, rows) | ||
if err == nil { | ||
t.Errorf("Expected file writer error, but got nil ") | ||
} | ||
} | ||
|
||
func TestCsvWriterWriteRowsFileHandlerError(t *testing.T) { | ||
fw := &TestFileHandlerErrorWriter{} | ||
writer := NewCsvWriter(fw, "result.csv", "", ",") | ||
rows := make([]*map[string]interface{}, 0) | ||
rows = append(rows, &map[string]interface{}{"name": "one"}) | ||
err := writer.WriteRows("some_table", []string{"name"}, rows) | ||
if err == nil { | ||
t.Errorf("Expected write error, but got nil ") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters