MCPcopy
hub / github.com/mathaou/termdbms / Convert

Function Convert

tuiutil/csv2sql.go:48–174  ·  view source on GitHub ↗
(csvFileName, tableName string, keepOrigCols bool)

Source from the content-addressed store, hash-verified

46}
47
48func Convert(csvFileName, tableName string, keepOrigCols bool) string {
49 // check we have a table name and csv file to work with - otherwise abort
50 if csvFileName == "" || tableName == "" {
51 return ""
52 }
53
54 // open the CSV file - name provided via command line input - handle 'file'
55 file, err := os.Open(csvFileName)
56 // error - if we have one exit as CSV file not right
57 if err != nil {
58 fmt.Printf("ERROR: %s\n", err)
59 os.Exit(-3)
60 }
61 // now file is open - defer the close of CSV file handle until we return
62 defer file.Close()
63 // connect a CSV reader to the file handle - which is the actual opened
64 // CSV file
65 // TODO : is there an error from this to check?
66 reader := csv.NewReader(file)
67
68 sqlOutFile := SQLFileName(csvFileName)
69
70 // open the new file using the name we obtained above - handle 'filesql'
71 filesql, err := os.Create(sqlOutFile)
72 // error - if we have one when trying open & create the new file
73 if err != nil {
74 return ""
75 }
76 // now new file is open - defer the close of the file handle until we return
77 defer filesql.Close()
78 // attach the opened new sql file handle to a buffered file writer
79 // the buffered file writer has the handle 'sqlFileBuffer'
80 sqlFileBuffer := bufio.NewWriter(filesql)
81
82 //-------------------------------------------------------------------------
83 // prepare to read the each line of the CSV file - and write out to the SQl
84 //-------------------------------------------------------------------------
85 // track the number of lines in the csv file
86 lineCount := 0
87
88 // create a buffer to hold each line of the SQL file as we build it
89 // handle to this buffer is called 'strbuffer'
90 var strbuffer bytes.Buffer
91
92 // START - processing of each line in the CSV input file
93 //-------------------------------------------------------------------------
94 // loop through the csv file until EOF - or until we hit an error in parsing it.
95 // Data is read in for each line of the csv file and held in the variable
96 // 'record'. Build a string for each line - wrapped with the SQL and
97 // then output to the SQL file writer in its completed new form
98 //-------------------------------------------------------------------------
99 for {
100 record, err := reader.Read()
101
102 // if we hit end of file (EOF) or another unexpected error
103 if err == io.EOF {
104 break
105 } else if err != nil {

Callers 1

mainFunction · 0.85

Calls 4

SQLFileNameFunction · 0.85
cleanHeaderFunction · 0.85
ResetMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected