示例#1
0
文件: csvdump.go 项目: xingskycn/go
func main() {
	var (
		where   string
		columns []string
	)

	flagConnect := flag.String("connect", os.Getenv("BRUNO_ID"), "user/passw@sid to connect to")
	flagDateFormat := flag.String("date", dateFormat, "date format, in Go notation")
	flag.Parse()
	dateFormat = *flagDateFormat
	dEnd = `"` + strings.NewReplacer(
		"2006", "9999",
		"01", "12",
		"02", "31",
		"15", "23",
		"04", "59",
		"05", "59",
	).Replace(dateFormat) + `"`
	if flag.NArg() > 1 {
		where = flag.Arg(1)
		if flag.NArg() > 2 {
			columns = flag.Args()[2:]
		}
	}
	_ = ora.GetDrv()
	//ora.Log = lg15.Log
	//lg15.Log.SetHandler(log15.StderrHandler)
	db, err := sql.Open("ora", *flagConnect)
	if err != nil {
		log.Printf("error connecting to %s: %v", *flagConnect, err)
		os.Exit(2)
	}
	qry := getQuery(flag.Arg(0), where, columns)
	err = dump(os.Stdout, dber.SqlDBer{DB: db}, qry)
	_ = db.Close()
	if err != nil {
		log.Printf("error dumping: %s", err)
		os.Exit(1)
	}
	os.Exit(0)
}
示例#2
0
func TestDescribeQuery(t *testing.T) {
	_ = ora.GetDrv()
	db, err := sql.Open("ora", *flagConnect)
	if err != nil {
		t.Fatalf("cannot connect to %q: %v", *flagConnect, err)
	}
	defer db.Close()
	dbr := dber.SqlDBer{db}
	cols, err := DescribeQuery(dbr, "SELECT * FROM user_objects")
	if err != nil {
		t.Errorf("DescribeQuery: %v", err)
	}
	t.Logf("cols=%v", cols)
	awaited := []Column{
		{"", "OBJECT_NAME", 1, 128, 0, 0, true, 32, 1},
		{"", "SUBOBJECT_NAME", 1, 30, 0, 0, true, 32, 1},
		{"", "OBJECT_ID", 2, 22, 0, -127, true, 0, 0},
		{"", "DATA_OBJECT_ID", 2, 22, 0, -127, true, 0, 0},
		{"", "OBJECT_TYPE", 1, 19, 0, 0, true, 32, 1},
		{"", "CREATED", 12, 7, 0, 0, true, 0, 0},
		{"", "LAST_DDL_TIME", 12, 7, 0, 0, true, 0, 0},
		{"", "TIMESTAMP", 1, 19, 0, 0, true, 32, 1},
		{"", "STATUS", 1, 7, 0, 0, true, 32, 1},
		{"", "TEMPORARY", 1, 1, 0, 0, true, 32, 1},
		{"", "GENERATED", 1, 1, 0, 0, true, 32, 1},
		{"", "SECONDARY", 1, 1, 0, 0, true, 32, 1},
	}
	ver, err := GetVersion(dbr)
	if err != nil {
		t.Errorf("get version: %v", err)
	} else if ver.Major >= 11 {
		awaited = append(awaited,
			Column{"", "NAMESPACE", 2, 22, 0, -127, true, 0, 0},
			Column{"", "EDITION_NAME", 1, 30, 0, 0, true, 32, 1},
		)
	}
	if !reflect.DeepEqual(cols, awaited) {
		t.Errorf("Mismatch: \n\tgot %#v,\n\tawaited %#v", cols, awaited)
	}
}