@@ -22,14 +22,19 @@ import (
2222 log "github.com/cihub/seelog"
2323 "github.com/infinitbyte/framework/core/pipeline"
2424 "strings"
25+ "github.com/infinitbyte/framework/core/util"
2526)
2627
28+ var fileName pipeline.ParaKey = "file_name"
2729var rowFormat pipeline.ParaKey = "row_format"
2830var sheetName pipeline.ParaKey = "sheet_name"
2931var columnName pipeline.ParaKey = "column_name"
3032var dataFromIndex pipeline.ParaKey = "data_start_from_index"
3133
34+ var sqlKey pipeline.ParaKey = "sql"
35+
3236type ReadCsvJoint struct {
37+ pipeline.Parameters
3338}
3439
3540func (joint ReadCsvJoint ) Name () string {
@@ -38,22 +43,25 @@ func (joint ReadCsvJoint) Name() string {
3843
3944func (joint ReadCsvJoint ) Process (c * pipeline.Context ) error {
4045
46+ log .Debug (joint .Data )
4147 log .Debug (c .Data )
42- templates := c .MustGetStringArray (rowFormat )
48+ templates := joint .MustGetStringArray (rowFormat )
4349 log .Debug ("row templates: " , templates )
4450
45- xlsx , err := excelize .OpenFile ("../test/Book1.xlsx" )
51+ xlsx , err := excelize .OpenFile (joint . MustGetString ( fileName ) )
4652 if err != nil {
4753 log .Error (err )
4854 return err
4955 }
5056 sheetMap := xlsx .GetSheetMap ()
5157 log .Debug ("sheets: " , sheetMap )
5258
53- colNames := c .MustGetStringArray (columnName )
54- dataOffset := c .MustGetInt (dataFromIndex )
59+ colNames := joint .MustGetStringArray (columnName )
60+ dataOffset := joint .MustGetInt (dataFromIndex )
61+
62+ rows := xlsx .GetRows (joint .MustGetString (sheetName ))
5563
56- rows := xlsx . GetRows ( c . MustGetString ( sheetName ))
64+ sql := ""
5765 for offset , row := range rows {
5866 if offset < dataOffset {
5967 log .Debugf ("%v < data offset: %v, ignore" , offset , dataOffset )
@@ -62,28 +70,45 @@ func (joint ReadCsvJoint) Process(c *pipeline.Context) error {
6270
6371 colMap := map [string ]string {}
6472
73+ hit := false
6574 for k , colCell := range row {
75+ if colCell != "" {
76+ hit = true
77+ }
6678 colName := colNames [k ]
6779 colMap [colName ] = colCell
68- log .Debug ("row:" ,offset , ": " , colName , "-" , k , "-" , colCell )
80+ log .Trace ("row:" , offset , ": " , colName , "-" , k , "-" , colCell )
81+ }
82+
83+ //ignore empty row
84+ if ! hit {
85+ continue
6986 }
7087
7188 for _ , x := range templates {
72- line := x
73- log .Debug ("template:" ,line )
89+ line := x
90+ //line:=templates
91+ log .Debug ("template:" , line )
7492 for k , v := range colMap {
75- log .Debug (fmt .Sprintf ("<{%v: }>" ,k ),"," , formatString (v ))
93+ log .Debug (fmt .Sprintf ("<{%v: }>" , k ), "," , formatString (v ))
7694 line = strings .Replace (line , fmt .Sprintf ("<{%v: }>" , k ), formatString (v ), - 1 )
7795 }
7896 log .Debug (line )
97+ sql = sql + line
98+ log .Debug ("sql:" ,sql )
7999 }
80100 }
81101
102+ c .Set (sqlKey , sql )
103+
104+ log .Info (sql )
105+
82106 return nil
83107}
84108
85109func formatString (str string ) string {
86110 str = strings .Replace (str , "\" " , "" , - 1 )
87- str = fmt .Sprintf ("'%s'" ,str )
111+ str = strings .Replace (str , "'" , "" , - 1 )
112+ str = fmt .Sprintf ("'%s'" , util .TrimSpaces (str ))
88113 return str
89114}
0 commit comments