Example #1
0
File: load.go Project: gwenn/yacr
// http://download.geonames.org/export/dump/allCountries.zip
func main() {
	flag.Parse()
	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	r := yacr.NewReader(os.Stdin, '\t', false, false)
	w := yacr.NewWriter(os.Stdout, '\t', false)
	for r.Scan() && w.Write(r.Bytes()) {
		if r.EndOfRecord() {
			w.EndOfRecord()
		}
	}
	w.Flush()
	if err := r.Err(); err != nil {
		fmt.Fprintln(os.Stderr, err)
	}
	if err := w.Err(); err != nil {
		fmt.Fprintln(os.Stderr, err)
	}
}
Example #2
0
func Example() {
	r := yacr.NewReader(os.Stdin, '\t', false, false)
	w := yacr.NewWriter(os.Stdout, '\t', false)

	for r.Scan() && w.Write(r.Bytes()) {
		if r.EndOfRecord() {
			w.EndOfRecord()
		}
	}
	w.Flush()
	if err := r.Err(); err != nil {
		fmt.Fprintln(os.Stderr, err)
	}
	if err := w.Err(); err != nil {
		fmt.Fprintln(os.Stderr, err)
	}
}
Example #3
0
func TestExportTableToCSV(t *testing.T) {
	db := open(t)
	defer checkClose(db, t)
	createTable(db, t)
	err := db.FastExec(`INSERT INTO test (float_num, int_num, a_string) VALUES (1.23, 0, 'qu"ote'), (NULL, 1, "new
line"), (3.33, 2, 'test')`)
	checkNoError(t, err, "error while inserting data: %s")

	var b bytes.Buffer
	w := yacr.NewWriter(&b, ',', true)
	err = db.ExportTableToCSV("", "test", "", true, w)
	checkNoError(t, err, "error while exporting CSV file: %s")
	assert.Equal(t, `id,float_num,int_num,a_string
1,1.23,0,"qu""ote"
2,,1,"new
line"
3,3.33,2,test
`, b.String())
}
Example #4
0
func TestExportToCSV(t *testing.T) {
	db := open(t)
	defer checkClose(db, t)
	createTable(db, t)
	err := db.FastExec(`INSERT INTO test (float_num, int_num, a_string) VALUES (1.23, 0, 'qu"ote'), (NULL, 1, "new
line"), (3.33, 2, 'test')`)
	checkNoError(t, err, "error while inserting data: %s")

	var b bytes.Buffer
	w := yacr.NewWriter(&b, ',', true)
	s, err := db.Prepare("SELECT float_num, int_num, a_string FROM test where id > ?", 0)
	checkNoError(t, err, "error while preparing stmt: %s")
	defer checkFinalize(s, t)

	err = s.ExportToCSV("", false, w)
	checkNoError(t, err, "error while exporting CSV file: %s")
	assert.Equal(t, `1.23,0,"qu""ote"
,1,"new
line"
3.33,2,test
`, b.String())
}
Example #5
0
func makeWriter(wr io.Writer, c *config) *yacr.Writer {
	writer := yacr.NewWriter(wr, c.sep, false /*TODO c.quoted */)
	return writer
}