mirror of
https://github.com/zhufuyi/sponge.git
synced 2025-10-05 16:57:07 +08:00
feat: implement sponge commands
This commit is contained in:
36
pkg/sql2code/parser/mysql.go
Normal file
36
pkg/sql2code/parser/mysql.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql" //nolint
|
||||
)
|
||||
|
||||
// GetTableInfo get table info from mysql
|
||||
func GetTableInfo(dsn, tableName string) (string, error) {
|
||||
db, err := sql.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("connect mysql 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
|
||||
}
|
Reference in New Issue
Block a user