Skip to content

Commit

Permalink
table name from filename
Browse files Browse the repository at this point in the history
  • Loading branch information
untoldone committed Jan 2, 2016
1 parent e3cb62c commit 476ce07
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions csvschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"path"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -67,6 +68,10 @@ func main() {
os.Exit(1)
}

_, filename := path.Split(os.Args[1])
fileParts := strings.Split(filename, ".")
tablename := strings.Join(fileParts[:len(fileParts)-1], ".")

csvReader := csv.NewReader(fileReader)
if err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -132,12 +137,12 @@ func main() {
})
}

fmt.Println("CREATE TABLE sample (")
fmt.Println("CREATE TABLE \"" + tablename + "\" (")
for i, field := range fields {
if field.FieldType == "character varying" {
fmt.Printf(" " + field.FieldName + " " + field.FieldType + " (" + strconv.Itoa(field.MaxLength*2) + ")")
fmt.Printf(" \"" + field.FieldName + "\" " + field.FieldType + " (" + strconv.Itoa(field.MaxLength*2) + ")")
} else {
fmt.Printf(" " + field.FieldName + " " + field.FieldType)
fmt.Printf(" \"" + field.FieldName + "\" " + field.FieldType)
}

if i < len(fields)-1 {
Expand Down

0 comments on commit 476ce07

Please sign in to comment.