Exemplo n.º 1
0
func main() {
	flag.Parse()
	args := flag.Args()

	if len(args) == 0 {
		flag.Usage()
		os.Exit(1)
	}

	conn, err := db.Open(*driver, *server)
	if err != nil {
		log.Fatalf("client error: %v", err)
	}

	now := time.Now()

	if isDml(args[0]) {
		r, err := conn.Exec(args[0], bindvars)
		if err != nil {
			log.Fatalf("exec failed: %v", err)
		}

		n, err := r.RowsAffected()

		log.Println("Total time:", time.Now().Sub(now), "Rows affected:", n)
	}
}
Exemplo n.º 2
0
func main() {
	flag.Parse()
	args := flag.Args()
	if len(args) == 0 {
		flag.Usage()
		os.Exit(1)
	}

	conn, err := db.Open(*driver, *server)
	if err != nil {
		log.Fatalf("%v", err)
	}

	now := time.Now()
	if isDml(args[0]) {
		r, err := conn.Exec(args[0], bindvars)
		if err != nil {
			log.Fatalf("exec failed: %v", err)
		}

		n, err := r.RowsAffected()

		log.Println("Total time:", time.Now().Sub(now), "Rows affected:", n)
	} else {
		// launch the query
		r, err := conn.Exec(args[0], bindvars)
		if err != nil {
			log.Fatalf("%v", err)
		}

		// get the headers
		cols := r.Columns()
		if err != nil {
			log.Fatalf("%v", err)
		}

		n, err := r.RowsAffected()
		line := "Index:"
		// print the header
		if *verbose && n > 0 {
			for _, field := range cols {
				line += "\t" + field
			}
			log.Println(line)
		}

		// get the rows
		rowIndex := 0
		for row := r.Next(); row != nil; row = r.Next() {
			// print the line if needed
			if *verbose {
				line := fmt.Sprintf("%-10d", rowIndex)
				for _, value := range row {
					if value != nil {
						switch value.(type) {
						case []byte:
							line += fmt.Sprintf("\t%s", value)
						default:
							line += fmt.Sprintf("\t%v", value)
						}
					} else {
						line += "\t"
					}
				}
				log.Println(line)
			}
			rowIndex++
		}
		if err := r.Err(); err != nil {
			log.Fatalf("Error %v\n", err)
		}
		log.Println("Total time:", time.Now().Sub(now), "Row count:", rowIndex)
	}
}