Skip to content

Instantly share code, notes, and snippets.

@greenlion
Created May 2, 2019 00:15
Show Gist options
  • Save greenlion/86703b5f75606bdd3d90f2e2336980c6 to your computer and use it in GitHub Desktop.
Save greenlion/86703b5f75606bdd3d90f2e2336980c6 to your computer and use it in GitHub Desktop.
package main
import (
"database/sql"
"fmt"
"github.com/greenlion/go-myquery/myquery"
"github.com/pborman/getopt/v2"
"time"
)
// Declare flags against existing variables.
type tSettings struct {
/* MySQL connection parameters (note user must have SELECT on P_S) */
host string
port string
socket string
user string
password string
refresh_interval time.Duration
show_help bool
}
var (
/* program settings
TODO: support using settings from a my.cnf [client] section
*/
settings tSettings
/* number of defined views */
view_count int
/* views are numbered consecutively from zero */
current_view int
/* each view has a set of rows */
views map[string][]string
)
func init() {
//views = make([]*tRowVal)
settings.host = "127.0.0.1"
settings.port = "3306"
settings.socket = ""
settings.user = "root"
settings.password = ""
settings.refresh_interval = time.Millisecond * 1000
getopt.FlagLong(&settings.show_help, "help", '?', "show help").SetOptional()
getopt.FlagLong(&settings.host, "host", 'h', "hostname").SetOptional()
getopt.FlagLong(&settings.user, "user", 'u', "username").SetOptional()
getopt.FlagLong(&settings.port, "port", 'P', "port").SetOptional()
getopt.FlagLong(&settings.socket, "socket", 's', "socket file").SetOptional()
getopt.FlagLong(&settings.password, "password", 'p', "password").SetOptional()
}
func main() {
getopt.Parse()
conn, err := myquery.Connect(settings.host, settings.user, settings.password, settings.port, "performance_schema")
if err != nil {
panic(err)
}
defer conn.Close()
sys_tables := get_sys_tables(conn)
fmt.Println(sys_tables)
}
func get_sys_tables(conn *sql.DB) []string {
tables := make([]string, 0)
/* connection to the database */
stmt, err := myquery.Query(conn, "select table_name from information_schema.tables where table_schema = 'sys'")
if err != nil {
panic(err)
}
for {
row, err := myquery.Fetch(stmt)
if err != nil {
panic(err)
}
if row == nil {
break
}
tables = append(tables, row["table_name"])
}
return tables
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment