@@ -17,22 +17,12 @@ limitations under the License.
1717package pipelines
1818
1919import (
20- "bytes"
21- "fmt"
22- "github.com/360EntSecGroup-Skylar/excelize"
2320 log "github.com/cihub/seelog"
2421 "github.com/infinitbyte/framework/core/pipeline"
2522 "github.com/infinitbyte/framework/core/util"
26- "strings"
2723)
2824
2925var fileName pipeline.ParaKey = "file_name"
30- var rowFormat pipeline.ParaKey = "row_format"
31- var sheetName pipeline.ParaKey = "sheet_name"
32- var columnName pipeline.ParaKey = "column_name"
33- var dataFromIndex pipeline.ParaKey = "data_start_from_index"
34-
35- var sqlKey pipeline.ParaKey = "sql"
3626
3727type ReadCsvJoint struct {
3828 pipeline.Parameters
@@ -44,70 +34,15 @@ func (joint ReadCsvJoint) Name() string {
4434
4535func (joint ReadCsvJoint ) Process (c * pipeline.Context ) error {
4636
47- log .Debug (joint .Data )
48- log .Debug (c .Data )
49- templates := joint .MustGetStringArray (rowFormat )
50- log .Debug ("row templates: " , templates )
51-
52- xlsx , err := excelize .OpenFile (joint .MustGetString (fileName ))
37+ excelBytes , err := util .FileGetContent (joint .MustGetString (fileName ))
5338 if err != nil {
5439 log .Error (err )
40+ panic (err )
5541 return err
5642 }
57- sheetMap := xlsx .GetSheetMap ()
58- log .Debug ("sheets: " , sheetMap )
59-
60- colNames := joint .MustGetStringArray (columnName )
61- dataOffset := joint .MustGetInt (dataFromIndex )
62-
63- rows := xlsx .GetRows (joint .MustGetString (sheetName ))
64-
65- var sqlBuffer bytes.Buffer
66- for offset , row := range rows {
67- if offset < dataOffset {
68- log .Debugf ("%v < data offset: %v, ignore" , offset , dataOffset )
69- continue
70- }
71-
72- colMap := map [string ]string {}
73-
74- hit := false
75- for k , colCell := range row {
76- if colCell != "" {
77- hit = true
78- }
79- colName := colNames [k ]
80- colMap [colName ] = colCell
81- log .Trace ("row:" , offset , ": " , colName , "-" , k , "-" , colCell )
82- }
83-
84- //ignore empty row
85- if ! hit {
86- continue
87- }
88-
89- for _ , x := range templates {
90- line := x
91- log .Debug ("template:" , line )
92- for k , v := range colMap {
93- log .Debug (fmt .Sprintf ("<{%v: }>" , k ), "," , formatString (v ))
94- line = strings .Replace (line , fmt .Sprintf ("<{%v: }>" , k ), formatString (v ), - 1 )
95- }
96- log .Debug (line )
97- sqlBuffer .WriteString (line )
98- }
99- }
100-
101- c .Set (sqlKey , sqlBuffer .String ())
10243
103- log . Trace ( sqlBuffer . String () )
44+ c . Set ( excelBytesKey , excelBytes )
10445
10546 return nil
10647}
10748
108- func formatString (str string ) string {
109- str = strings .Replace (str , "\" " , "" , - 1 )
110- str = strings .Replace (str , "'" , "" , - 1 )
111- str = fmt .Sprintf ("'%s'" , util .TrimSpaces (str ))
112- return str
113- }
0 commit comments