Ejemplo n.º 1
0
func TestWriterInterface(t *testing.T) {
	t.Parallel()

	var iface Writer
	iface = thiscsv.NewWriter(new(bytes.Buffer))
	iface = thiscsv.NewDialectWriter(new(bytes.Buffer), thiscsv.Dialect{})
	iface = oldcsv.NewWriter(new(bytes.Buffer))

	// To get rid of compile-time warning that this variable is not used.
	iface.Flush()
}
Ejemplo n.º 2
0
func Example_flag() {
	builder := dialect.FromCommandLine()

	flag.Parse()

	dialect, err := builder.Dialect()
	if err != nil {
		panic(err)
	}

	reader := csv.NewDialectWriter(os.Stdout, *dialect)
	reader.Write([]string{"Hello", "World"})
	reader.Flush()

	// Output:
	// Hello	World
}
Ejemplo n.º 3
0
func Example_flagSet() {
	fset := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
	builder := dialect.FromFlagSet(fset)

	fset.Parse([]string{})

	dialect, err := builder.Dialect()
	if err != nil {
		panic(err)
	}

	reader := csv.NewDialectWriter(os.Stdout, *dialect)
	reader.Write([]string{"Hello", "World"})
	reader.Flush()

	// Output:
	// Hello	World
}
Ejemplo n.º 4
0
func dumpTable(table string, db queryable, outputDir string, compressOut, skipHeader bool, csvDialect *csv.Dialect) error {
	fname := outputDir + "/" + table + ".csv"
	if compressOut {
		fname = fname + ".gz"
	}

	f, err := os.Create(fname)
	if err != nil {
		return err
	}
	defer f.Close()

	var out io.Writer
	if compressOut {
		gzout := gzip.NewWriter(f)
		defer gzout.Close()
		out = gzout
	} else {
		out = f
	}

	w := csv.NewDialectWriter(out, *csvDialect)

	rows, err := db.Query("SELECT * FROM " + table) // Couldn't get placeholder expansion to work here
	if err != nil {
		return err
	}

	columns, err := rows.Columns()
	if err != nil {
		panic(err.Error())
	}
	if !skipHeader {
		err = w.Write(columns) // Header
		if err != nil {
			return err
		}
	}

	for rows.Next() {
		// Shamelessly ripped (and modified) from http://play.golang.org/p/jxza3pbqq9

		// Create interface set
		values := make([]interface{}, len(columns))
		scanArgs := make([]interface{}, len(values))
		for i := range values {
			scanArgs[i] = &values[i]
		}

		// Scan for arbitrary values
		err = rows.Scan(scanArgs...)
		if err != nil {
			return err
		}

		// Print data
		csvData := make([]string, 0, len(values))
		for _, value := range values {
			switch value.(type) {
			default:
				s := fmt.Sprintf("%s", value)
				csvData = append(csvData, string(s))
			}
		}
		err = w.Write(csvData)
		if err != nil {
			return err
		}
	}

	w.Flush()
	err = w.Error()
	if err != nil {
		return err
	}

	return nil
}