Пример #1
0
func ListObjects(t *testing.T) {
	conn, err := connect.GetConnection("")
	if err != nil {
		t.Errorf("error connectiong: %s", err)
		t.FailNow()
		return
	}
	defer conn.Close()

	qry := "SELECT owner, object_name, object_id FROM all_objects WHERE ROWNUM < 20"
	log.Printf(`executing "%s"`, qry)
	rows, err := conn.Query(qry)
	if err != nil {
		t.Logf(`error with %q: %s`, qry, err)
		t.FailNow()
		return
	}
	var (
		owner, objectName string
		objectID          int
	)
	for rows.Next() {
		if err = rows.Scan(&owner, &objectName, &objectID); err != nil {
			t.Errorf("error fetching: %s", err)
			break
		}
		t.Logf(`row: "%s";"%s";%d`, owner, objectName, objectID)
	}
}
Пример #2
0
func dump(w io.Writer, qry string) error {
	db, err := connect.GetConnection("")
	if err != nil {
		return errgo.Notef(err, "connect to database")
	}
	defer db.Close()
	columns, err := GetColumns(db, qry)
	if err != nil {
		return errgo.Notef(err, "get column converters", err)
	}
	log.Printf("columns: %#v", columns)
	row := make([]interface{}, len(columns))
	rowP := make([]interface{}, len(row))
	for i := range row {
		rowP[i] = &row[i]
	}

	rows, err := db.Query(qry)
	if err != nil {
		return errgo.Newf("error executing %q: %s", qry, err)
	}
	defer rows.Close()

	bw := bufio.NewWriterSize(w, 65536)
	defer bw.Flush()
	for i, col := range columns {
		if i > 0 {
			bw.Write([]byte{';'})
		}
		bw.Write([]byte{'"'})
		bw.WriteString(col.Name)
		bw.Write([]byte{'"'})
	}
	bw.Write([]byte{'\n'})
	n := 0
	for rows.Next() {
		if err = rows.Scan(rowP...); err != nil {
			return errgo.Notef(err, "scan %d. row", n+1)
		}
		for i, data := range row {
			if i > 0 {
				bw.Write([]byte{';'})
			}
			if data == nil {
				continue
			}
			bw.WriteString(columns[i].String(data))
		}
		bw.Write([]byte{'\n'})
		n++
	}
	log.Printf("written %d rows.", n)
	err = rows.Err()
	if err != nil {
		return err
	}
	return nil
}