Skip to content

Added separator config to support TSV and Fixed Value formatting. #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion csv2sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var csvFileName string
var keepOrigCols bool
var debugSwitch bool
var helpMe bool
var separatorVal string

// init() function - always runs before main() - used here to set-up required flags variables
// from the command line parameters provided by the user when they run the app
Expand All @@ -56,6 +57,7 @@ func init() {
// IntVar; StringVar; BoolVar all required: variable, cmd line flag, initial value, description used by flag.Usage() on error / help
flag.StringVar(&tableName, "t", "", "\tUSE: '-t tablename' where tablename is the name of the SQLite table to hold your CSV file data [MANDATORY]")
flag.StringVar(&csvFileName, "f", "", "\tUSE: '-f filename.csv' where filename.csv is the name and path to a CSV file that contains your data for conversion [MANDATORY]")
flag.StringVar(&separatorVal, "s", ",", "\tUSE: for tab '-s=\"\\t\" or '-s=\",\"' [OPTIONAL]")
flag.BoolVar(&keepOrigCols, "k", false, "\tUSE: '-k=true' to keep original csv header fields as the SQL table column names")
flag.BoolVar(&debugSwitch, "d", false, "\tUSE: '-d=true' to include additional debug output when run")
flag.BoolVar(&helpMe, "h", false, "\tUSE: '-h' to provide more detailed help on using this program")
Expand Down Expand Up @@ -227,6 +229,16 @@ func main() {
// TODO : is there an error from this to check?
reader := csv.NewReader(file)

// TSV support
if separatorVal != "" {
if debugSwitch {
fmt.Printf("Using separator: %s\n", separatorVal)
}
if separatorVal == "\\t" {
reader.Comma = '\t'
}
}

//-------------------------------------------------------------------------
// open and prepare the SQL output file
//-------------------------------------------------------------------------
Expand Down Expand Up @@ -326,7 +338,7 @@ func main() {
if len(record[i]) == 0 || record[i] == "NULL" {
strbuffer.WriteString("NULL")
} else {
strbuffer.WriteString("\"" + record[i] + "\"")
strbuffer.WriteString("\"" + strings.Replace(record[i], "\"", "\"\"", -1) + "\"") /* escape " with "" */
}
// if we have not reached the last record yet - add a coma also to the output
if i < len(record)-1 {
Expand Down