Skip to content

Commit

Permalink
dynamic column use go-sql-driver
Browse files Browse the repository at this point in the history
  • Loading branch information
martianzhang committed Dec 20, 2018
1 parent ad1b1e0 commit 3faa8cd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
8 changes: 7 additions & 1 deletion database/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -967,8 +967,14 @@ func ParseExplainResult(res QueryResult, formatType int) (exp *ExplainInfo, err
}
cols, err := res.Rows.Columns()
common.LogIfError(err, "")
var colByPass []byte
for _, col := range cols {
explainFields = append(explainFields, fields[col])
if _, ok := fields[col]; ok {
explainFields = append(explainFields, fields[col])
} else {
common.Log.Debug("ParseExplainResult by pass column %s", col)
explainFields = append(explainFields, &colByPass)
}
}

// 补全 ExplainRows
Expand Down
68 changes: 47 additions & 21 deletions database/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,14 @@ func (db *Connector) ShowTableStatus(tableName string) (*TableStatInfo, error) {
}
cols, err := res.Rows.Columns()
common.LogIfError(err, "")
var colByPass []byte
for _, col := range cols {
statusFields = append(statusFields, fields[col])
if _, ok := fields[col]; ok {
statusFields = append(statusFields, fields[col])
} else {
common.Log.Debug("ShowTableStatus by pass column %s", col)
statusFields = append(statusFields, &colByPass)
}
}
// 获取值
for res.Rows.Next() {
Expand All @@ -161,7 +167,7 @@ type TableIndexInfo struct {
Rows []TableIndexRow
}

// TableIndexRow 用以存放show index之后获取的每一条index信息
// TableIndexRow 用以存放show index 之后获取的每一条 index 信息
type TableIndexRow struct {
Table string // 表名
NonUnique int // 0:unique key,1:not unique
Expand Down Expand Up @@ -224,8 +230,14 @@ func (db *Connector) ShowIndex(tableName string) (*TableIndexInfo, error) {
}
cols, err := res.Rows.Columns()
common.LogIfError(err, "")
var colByPass []byte
for _, col := range cols {
indexFields = append(indexFields, fields[col])
if _, ok := fields[col]; ok {
indexFields = append(indexFields, fields[col])
} else {
common.Log.Debug("ShowIndex by pass column %s", col)
indexFields = append(indexFields, &colByPass)
}
}
// 获取值
for res.Rows.Next() {
Expand Down Expand Up @@ -348,10 +360,15 @@ func (db *Connector) ShowColumns(tableName string) (*TableDesc, error) {
}
cols, err := res.Rows.Columns()
common.LogIfError(err, "")
var colByPass []byte
for _, col := range cols {
columnFields = append(columnFields, fields[col])
if _, ok := fields[col]; ok {
columnFields = append(columnFields, fields[col])
} else {
common.Log.Debug("ShowColumns by pass column %s", col)
columnFields = append(columnFields, &colByPass)
}
}

// 获取值
for res.Rows.Next() {
res.Rows.Scan(columnFields...)
Expand All @@ -371,34 +388,43 @@ func (td TableDesc) Columns() []string {

// showCreate show create
func (db *Connector) showCreate(createType, name string) (string, error) {
// 执行 show create table|database
// createType = [table|database|view]
// SHOW CREATE DATABASE db_name
// SHOW CREATE EVENT event_name
// SHOW CREATE FUNCTION func_name
// SHOW CREATE PROCEDURE proc_name
// SHOW CREATE TABLE tbl_name
// SHOW CREATE TRIGGER trigger_name
// SHOW CREATE VIEW view_name
res, err := db.Query(fmt.Sprintf("show create %s `%s`", createType, name))
if err != nil {
return "", err
}

// columns info
var colByPass []byte
var create string
createFields := make([]interface{}, 0)
fields := map[string]interface{}{
"Create View": &create,
"Create Table": &create,
"Create Database": &create,
"Table": &colByPass,
"Database": &colByPass,
"View": &colByPass,
"character_set_client": &colByPass,
"collation_connection": &colByPass,
"Create View": &create,
"Create Table": &create,
"Create Database": &create,
"Create Event": &create,
"Statement": &create, // show create trigger
"Create Function": &create,
"Create Procedure": &create,
}
cols, err := res.Rows.Columns()
common.LogIfError(err, "")
var colByPass []byte
for _, col := range cols {
createFields = append(createFields, fields[col])
if _, ok := fields[col]; ok {
createFields = append(createFields, fields[col])
} else {
common.Log.Debug("showCreate by pass column %s", col)
createFields = append(createFields, &colByPass)
}
}

// 获取 CREATE TABLE|VIEW|DATABASE 语句
// 获取 CREATE 语句
for res.Rows.Next() {
res.Rows.Scan(createFields...)
}
Expand Down Expand Up @@ -494,9 +520,9 @@ func (db *Connector) FindColumn(name, dbName string, tables ...string) ([]*commo
col.Collation = string(collation)
// 填充字符集和排序规则
if col.Character == "" {
// 当从`INFORMATION_SCHEMA`.`COLUMNS`表中查询不到相关列的character和collation的信息时
// 认为该列使用的character和collation与其所处的表一致
// 由于`INFORMATION_SCHEMA`.`TABLES`表中未找到表的character,所以从按照MySQL中collation的规则从中截取character
// 当从 `INFORMATION_SCHEMA`.`COLUMNS` 表中查询不到相关列的 character 和 collation 的信息时
// 认为该列使用的 character 和 collation 与其所处的表一致
// 由于 `INFORMATION_SCHEMA`.`TABLES` 表中未找到表的 character,所以从按照 MySQL 中 collation 的规则从中截取 character

sql = fmt.Sprintf("SELECT `t`.`TABLE_COLLATION` FROM `INFORMATION_SCHEMA`.`TABLES` AS `t` "+
"WHERE `t`.`TABLE_NAME`='%s' AND `t`.`TABLE_SCHEMA` = '%s'", col.Table, col.DB)
Expand Down

0 comments on commit 3faa8cd

Please sign in to comment.