Exemplo n.º 1
0
func main() {
	defer exit.Recover()
	defer logutil.Flush()

	flag.Parse()
	args := flag.Args()

	if len(args) == 0 {
		flag.Usage()
		exit.Return(1)
	}

	c := vitessdriver.Configuration{
		Protocol:   *vtgateconn.VtgateProtocol,
		Address:    *server,
		Keyspace:   *keyspace,
		Shard:      *shard,
		TabletType: *tabletType,
		Timeout:    *timeout,
		Streaming:  *streaming,
	}
	db, err := vitessdriver.OpenWithConfiguration(c)
	if err != nil {
		log.Errorf("client error: %v", err)
		exit.Return(1)
	}

	log.Infof("Sending the query...")
	startTime := time.Now()

	// handle dml
	if isDml(args[0]) {
		tx, err := db.Begin()
		if err != nil {
			log.Errorf("begin failed: %v", err)
			exit.Return(1)
		}

		result, err := db.Exec(args[0], []interface{}(*bindVariables)...)
		if err != nil {
			log.Errorf("exec failed: %v", err)
			exit.Return(1)
		}

		err = tx.Commit()
		if err != nil {
			log.Errorf("commit failed: %v", err)
			exit.Return(1)
		}

		rowsAffected, err := result.RowsAffected()
		lastInsertID, err := result.LastInsertId()
		log.Infof("Total time: %v / Row affected: %v / Last Insert Id: %v", time.Since(startTime), rowsAffected, lastInsertID)
	} else {
		// launch the query
		rows, err := db.Query(args[0], []interface{}(*bindVariables)...)
		if err != nil {
			log.Errorf("client error: %v", err)
			exit.Return(1)
		}
		defer rows.Close()

		// get the headers
		var qr results
		cols, err := rows.Columns()
		if err != nil {
			log.Errorf("client error: %v", err)
			exit.Return(1)
		}
		qr.Fields = cols

		// get the rows
		for rows.Next() {
			row := make([]interface{}, len(cols))
			for i := range row {
				var col string
				row[i] = &col
			}
			if err := rows.Scan(row...); err != nil {
				log.Errorf("client error: %v", err)
				exit.Return(1)
			}

			// unpack []*string into []string
			vals := make([]string, 0, len(row))
			for _, value := range row {
				vals = append(vals, *(value.(*string)))
			}
			qr.Rows = append(qr.Rows, vals)
		}
		if err := rows.Err(); err != nil {
			log.Errorf("Error %v\n", err)
			exit.Return(1)
		}

		if *jsonOutput {
			data, err := json.MarshalIndent(qr, "", "  ")
			if err != nil {
				log.Errorf("cannot marshal data: %v", err)
				exit.Return(1)
			}
			fmt.Print(string(data))
		} else {
			printTable(qr, time.Since(startTime))
		}
	}
}
Exemplo n.º 2
0
func main() {
	defer exit.Recover()
	defer logutil.Flush()

	flag.Parse()
	args := flag.Args()

	if len(args) == 0 {
		flag.Usage()
		exit.Return(1)
	}

	c := vitessdriver.Configuration{
		Protocol:   *vtgateconn.VtgateProtocol,
		Address:    *server,
		Keyspace:   *keyspace,
		Shard:      *shard,
		TabletType: *tabletType,
		Timeout:    *timeout,
		Streaming:  *streaming,
	}
	db, err := vitessdriver.OpenWithConfiguration(c)
	if err != nil {
		log.Errorf("client error: %v", err)
		exit.Return(1)
	}

	log.Infof("Sending the query...")
	now := time.Now()

	// handle dml
	if isDml(args[0]) {
		tx, err := db.Begin()
		if err != nil {
			log.Errorf("begin failed: %v", err)
			exit.Return(1)
		}

		result, err := db.Exec(args[0], []interface{}(*bindVariables)...)
		if err != nil {
			log.Errorf("exec failed: %v", err)
			exit.Return(1)
		}

		err = tx.Commit()
		if err != nil {
			log.Errorf("commit failed: %v", err)
			exit.Return(1)
		}

		rowsAffected, err := result.RowsAffected()
		lastInsertID, err := result.LastInsertId()
		log.Infof("Total time: %v / Row affected: %v / Last Insert Id: %v", time.Now().Sub(now), rowsAffected, lastInsertID)
	} else {

		// launch the query
		rows, err := db.Query(args[0], []interface{}(*bindVariables)...)
		if err != nil {
			log.Errorf("client error: %v", err)
			exit.Return(1)
		}
		defer rows.Close()

		// print the headers
		cols, err := rows.Columns()
		if err != nil {
			log.Errorf("client error: %v", err)
			exit.Return(1)
		}
		line := "Index"
		for _, field := range cols {
			line += "\t" + field
		}
		fmt.Printf("%s\n", line)

		// get the rows
		rowIndex := 0
		for rows.Next() {
			row := make([]interface{}, len(cols))
			for i := range row {
				var col string
				row[i] = &col
			}
			if err := rows.Scan(row...); err != nil {
				log.Errorf("client error: %v", err)
				exit.Return(1)
			}

			// print the line
			line := fmt.Sprintf("%d", rowIndex)
			for _, value := range row {
				line += fmt.Sprintf("\t%v", *(value.(*string)))
			}
			fmt.Printf("%s\n", line)
			rowIndex++
		}
		if err := rows.Err(); err != nil {
			log.Errorf("Error %v\n", err)
			exit.Return(1)
		}
		log.Infof("Total time: %v / Row count: %v", time.Now().Sub(now), rowIndex)
	}
}