-
Notifications
You must be signed in to change notification settings - Fork 98
/
mysql.go
42 lines (34 loc) · 928 Bytes
/
mysql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package parser
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql" //nolint
)
// GetMysqlTableInfo get table info from mysql
func GetMysqlTableInfo(dsn, tableName string) (string, error) {
db, err := sql.Open("mysql", dsn)
if err != nil {
return "", fmt.Errorf("GetMysqlTableInfo error, %v", err)
}
defer db.Close() //nolint
rows, err := db.Query("SHOW CREATE TABLE " + tableName)
if err != nil {
return "", fmt.Errorf("query show create table error, %v", err)
}
defer rows.Close() //nolint
if !rows.Next() {
return "", fmt.Errorf("not found found table '%s'", tableName)
}
var table string
var info string
err = rows.Scan(&table, &info)
if err != nil {
return "", err
}
return info, nil
}
// GetTableInfo get table info from mysql
// Deprecated: replaced by GetMysqlTableInfo
func GetTableInfo(dsn, tableName string) (string, error) {
return GetMysqlTableInfo(dsn, tableName)
}