ClickHouse/dbms/tests/integration/test_mysql_protocol/clients/golang/main.go

98 lines
2.0 KiB
Go
Raw Normal View History

2019-04-07 10:29:30 +00:00
package main
import (
"database/sql"
"flag"
"fmt"
_ "github.com/go-sql-driver/mysql"
"log"
"os"
)
func main() {
host := flag.String("host", "localhost", "mysql server address")
port := flag.Uint("port", 3306, "mysql server port")
user := flag.String("user", "", "username")
password := flag.String("password", "", "password")
database := flag.String("database", "", "database to authenticate against")
flag.Parse()
logger := log.New(os.Stderr, "", 0)
dataSource := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?allowCleartextPasswords=1", *user, *password, *host, *port, *database)
2019-04-07 10:32:52 +00:00
db, err := sql.Open("mysql", dataSource)
if err != nil {
2019-04-07 10:29:30 +00:00
logger.Fatal(err)
2019-04-07 10:32:52 +00:00
}
2019-04-07 10:29:30 +00:00
defer db.Close()
2019-04-08 08:49:21 +00:00
runQuery := func(query string, processRows func(*sql.Rows) error) {
2019-04-07 10:29:30 +00:00
rows, err := db.Query(query)
if err != nil {
logger.Fatal(err)
}
columns, err := rows.Columns()
fmt.Println("Columns:")
for _, name := range columns {
fmt.Println(name)
}
columnsTypes, err := rows.ColumnTypes()
fmt.Println("Column types:")
for _, column := range columnsTypes {
fmt.Printf("%s %s\n", column.Name(), column.DatabaseTypeName())
}
fmt.Println("Result:")
2019-04-08 08:49:21 +00:00
err = processRows(rows)
if err != nil {
logger.Fatal(err)
}
2019-04-07 10:29:30 +00:00
err = rows.Err()
if err != nil {
logger.Fatal(err)
}
err = rows.Close()
if err != nil {
logger.Fatal(err)
}
err = rows.Close()
if err != nil {
logger.Fatal(err)
}
}
2019-04-08 08:49:21 +00:00
processRows := func(rows *sql.Rows) error {
2019-04-07 10:29:30 +00:00
var x int
for rows.Next() {
err := rows.Scan(&x)
if err != nil {
2019-04-08 08:49:21 +00:00
return err
2019-04-07 10:29:30 +00:00
}
fmt.Println(x)
}
2019-04-08 08:49:21 +00:00
return nil
2019-04-07 10:29:30 +00:00
}
runQuery("select number as a from system.numbers limit 2", processRows)
2019-04-08 08:49:21 +00:00
processRows = func(rows *sql.Rows) error {
2019-04-07 10:29:30 +00:00
var name string
var a int
for rows.Next() {
err := rows.Scan(&name, &a)
if err != nil {
2019-04-08 08:49:21 +00:00
return err
2019-04-07 10:29:30 +00:00
}
fmt.Println(name, a)
}
2019-04-08 08:49:21 +00:00
return nil
2019-04-07 10:29:30 +00:00
}
2019-04-08 08:49:21 +00:00
runQuery("select name, 1 as a from system.tables where name == 'tables'", processRows)
2019-04-07 10:29:30 +00:00
runQuery("select 'тест' as a, 1 as b", processRows)
}