github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/internal/pkg/service/output-print.go (about) 1 package service 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strings" 7 8 "github.com/easysoft/zendata/internal/pkg/helper" 9 10 consts "github.com/easysoft/zendata/internal/pkg/const" 11 logUtils "github.com/easysoft/zendata/pkg/utils/log" 12 "github.com/easysoft/zendata/pkg/utils/vari" 13 ) 14 15 type PrintService struct { 16 PlaceholderService *PlaceholderService `inject:""` 17 } 18 19 func (s *PrintService) PrintLines() (lines []interface{}) { 20 var sqlHeader string 21 22 if vari.GlobalVars.OutputFormat == consts.FormatText { 23 s.PrintTextHeader() 24 25 } else if vari.GlobalVars.OutputFormat == consts.FormatSql { 26 sqlHeader = s.getInsertSqlHeader() 27 if vari.GlobalVars.DBDsn != "" { 28 lines = append(lines, sqlHeader) 29 } 30 31 } else if vari.GlobalVars.OutputFormat == consts.FormatJson { 32 //s.PrintJsonHeader() 33 34 } else if vari.GlobalVars.OutputFormat == consts.FormatXml { 35 //s.PrintXmlHeader() 36 } 37 38 return 39 } 40 41 func (s *PrintService) PrintTextHeader() { 42 if !vari.GlobalVars.Human { 43 return 44 } 45 headerLine := "" 46 for idx, field := range vari.GlobalVars.ExportFields { 47 headerLine += field 48 if idx < len(vari.GlobalVars.ExportFields)-1 { 49 headerLine += "\t" 50 } 51 } 52 53 logUtils.PrintLine(headerLine + "\n") 54 } 55 56 // return "Table> (<column1, column2,...)" 57 func (s *PrintService) getInsertSqlHeader() string { 58 fieldNames := make([]string, 0) 59 for _, f := range vari.GlobalVars.ExportFields { 60 if vari.GlobalVars.DBType == consts.DBTypeSqlServer { 61 f = "[" + helper.EscapeColumnOfSqlServer(f) + "]" 62 } else if vari.GlobalVars.DBType == consts.DBTypeOracle { 63 f = `"` + f + `"` 64 } else { 65 f = "`" + helper.EscapeColumnOfMysql(f) + "`" 66 //vari.GenVars.DBType == consts.DBTypeMysql { 67 } 68 69 fieldNames = append(fieldNames, f) 70 } 71 72 var ret string 73 switch vari.GlobalVars.DBType { 74 case consts.DBTypeSqlServer: 75 ret = fmt.Sprintf("[%s] (%s)", vari.GlobalVars.Table, strings.Join(fieldNames, ", ")) 76 case consts.DBTypeOracle: 77 ret = fmt.Sprintf(`"%s" (%s)`, vari.GlobalVars.Table, strings.Join(fieldNames, ", ")) 78 // case consts.DBTypeMysql: 79 default: 80 ret = fmt.Sprintf("`%s` (%s)", vari.GlobalVars.Table, strings.Join(fieldNames, ", ")) 81 } 82 83 return ret 84 } 85 86 func (s *PrintService) rowToJson(cols []string, fieldsToExport []string) string { 87 rowMap := map[string]string{} 88 for j, col := range cols { 89 rowMap[fieldsToExport[j]] = col 90 } 91 92 jsonObj, _ := json.MarshalIndent(rowMap, "", "\t") 93 respJson := string(jsonObj) 94 95 respJson = strings.ReplaceAll("\t"+respJson, "\n", "\n\t") 96 97 return respJson 98 } 99 100 // @return "" 101 func (s *PrintService) genSqlLine(sqlheader string, values []string, dbtype string) string { 102 var tmp string 103 switch dbtype { 104 case consts.DBTypeSqlServer: 105 tmp = "INSERT INTO " + sqlheader + " VALUES (" + strings.Join(values, ",") + "); GO" 106 default: 107 // consts.DBTypeMysql 108 // consts.DBTypeOracle: 109 tmp = "INSERT INTO " + sqlheader + " VALUES (" + strings.Join(values, ",") + ");" 110 } 111 112 return tmp 113 } 114 115 func (s *PrintService) genJsonLine(i int, row []string, length int, fields []string) string { 116 temp := s.rowToJson(row, fields) 117 if i < length-1 { 118 temp = temp + ", " 119 } else { 120 temp = temp + "\n]" 121 } 122 123 return temp 124 } 125 126 func (s *PrintService) getXmlLine(i int, mp map[string]string, length int) string { 127 str := "" 128 j := 0 129 for key, val := range mp { 130 str += fmt.Sprintf(" <%s>%s</%s>", key, val, key) 131 if j != len(mp)-1 { 132 str = str + "\n" 133 } 134 135 j++ 136 } 137 138 text := fmt.Sprintf(" <row>\n%s\n </row>", str) 139 if i == length-1 { 140 text = text + "\n</testdata>" 141 } 142 return text 143 }