mirror of
https://github.com/nxshock/csv2db.git
synced 2024-11-27 03:31:00 +05:00
Add WithoutHeader and Silent options
This commit is contained in:
parent
3606f38890
commit
3f2a2e2b44
17
main.go
17
main.go
@ -73,6 +73,12 @@ var app = &cli.App{
|
||||
Name: "unknowncolumnnames",
|
||||
Usage: "insert to table with unknown column names",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "withoutheader",
|
||||
Usage: "file does not contains a header"},
|
||||
&cli.BoolFlag{
|
||||
Name: "silent",
|
||||
Usage: "silens progress output"},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
if len(strings.Split(c.String("table"), ".")) != 2 {
|
||||
@ -146,7 +152,9 @@ func processReader(c *cli.Context, r io.Reader) error {
|
||||
reader.Comma = []rune(c.String("comma"))[0]
|
||||
}
|
||||
|
||||
header, err := reader.Read()
|
||||
var header []string
|
||||
if !c.Bool("withoutheader") {
|
||||
header, err = reader.Read()
|
||||
if err != nil {
|
||||
return fmt.Errorf("read header: %v", err)
|
||||
}
|
||||
@ -165,6 +173,7 @@ func processReader(c *cli.Context, r io.Reader) error {
|
||||
headerList += `"`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var neededHeader []string
|
||||
|
||||
@ -207,7 +216,7 @@ func processReader(c *cli.Context, r io.Reader) error {
|
||||
}
|
||||
|
||||
if c.Bool("create") {
|
||||
err = createTable(tx, c.String("table"), header, c.String("fields"), c.Bool("overwrite"))
|
||||
err = createTable(tx, c.String("table"), neededHeader, c.String("fields"), c.Bool("overwrite"))
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
return fmt.Errorf("create table: %v", err)
|
||||
@ -225,8 +234,10 @@ func processReader(c *cli.Context, r io.Reader) error {
|
||||
n := 0
|
||||
for {
|
||||
if n%100000 == 0 {
|
||||
if !c.Bool("silent") {
|
||||
fmt.Fprintf(os.Stderr, "Processed %d records...\r", n)
|
||||
}
|
||||
}
|
||||
|
||||
record, err := reader.Read()
|
||||
if err == io.EOF {
|
||||
@ -274,7 +285,9 @@ func processReader(c *cli.Context, r io.Reader) error {
|
||||
_ = tx.Rollback()
|
||||
return fmt.Errorf("calc rows affected: %v", err)
|
||||
}
|
||||
if !c.Bool("silent") {
|
||||
fmt.Fprintf(os.Stderr, "Processed %d records. \n", rowsAffected)
|
||||
}
|
||||
|
||||
err = stmt.Close()
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user