亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 SQL 查詢中獲取表名

從 SQL 查詢中獲取表名

Go
不負相思意 2023-06-01 15:05:43
我正在使用golang SQL 解析器從實際的 SQL 查詢字符串中獲取查詢相關信息。我可以使用以下代碼找到查詢類型:queryType?:=?sqlparser.StmtType(sqlparser.Preview(sql)) fmt.Println(queryType)但我不確定如何從 sql 查詢中獲取實際的表名。文檔也不清楚。我從解析函數中得到的唯一信息是一條語句有人可以指導我如何使用 golang sqlparser 獲取此信息嗎?
查看完整描述

3 回答

?
烙印99

TA貢獻1829條經驗 獲得超13個贊

Statement要獲取所有表名,您必須將它們從返回的 by中拉出Parse,可能使用反射。如果您運行以下代碼:


stmt, _ := sqlparser.Parse("insert into my_table set my_column=1")

fmt.Printf("%#v\n", stmt)

你得到輸出(為了便于閱讀而縮進):


&sqlparser.Insert{

    Action:"insert", 

    Comments:sqlparser.Comments(nil), 

    Ignore:"", 

    Table:sqlparser.TableName{

        Name:sqlparser.TableIdent{v:"my_table"}, 

        Qualifier:sqlparser.TableIdent{v:""}

    }, 

    Partitions:sqlparser.Partitions(nil), 

    Columns:sqlparser.Columns{sqlparser.ColIdent{_:[0]struct { _ []uint8 }{}, val:"my_column", lowered:""}}, 

    Rows:sqlparser.Values{sqlparser.ValTuple{(*sqlparser.SQLVal)(0xc00000a0c0)}}, 

    OnDup:sqlparser.OnDup(nil)

}

如您所見,它包含一個類型的(子)字段TableIdent,其中包含語句中請求的表。


查看完整回答
反對 回復 2023-06-01
?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

原始片段還返回了表的別名。例如


select * from my_table as mt join other_table using(my_key)

original snippet returns: [my_table, mt, other_table]

new snippet returns:? ? ? [my_table, other_table]

rob74 的原始片段:play.golang.org/p/B31wr2w1AL8


package main


import (

? ? "fmt"

? ? "github.com/xwb1989/sqlparser"

? ? "reflect"

)


func main() {

? ? stmt, _ := sqlparser.Parse("select * from my_table as mt join other_table using(my_key)")

? ? var tables []string

? ? tables = getTableNames(reflect.Indirect(reflect.ValueOf(stmt)), tables, 0, false)

? ? fmt.Printf("%s", tables)

}


func getTableNames(v reflect.Value, tables []string, level int, isTable bool) []string {

? ? switch v.Kind() {

? ? case reflect.Struct:

? ? ? ? if v.Type().Name() == "TableIdent" {

? ? ? ? ? ? // if this is a TableIdent struct, extract the table name

? ? ? ? ? ? tableName := v.FieldByName("v").String()

? ? ? ? ? ? if tableName != "" && isTable{

? ? ? ? ? ? ? ? tables = append(tables, tableName)

? ? ? ? ? ? }

? ? ? ? } else {

? ? ? ? ? ? // otherwise enumerate all fields of the struct and process further

? ? ? ? ? ? for i := 0; i < v.NumField(); i++ {

? ? ? ? ? ? ? ? tables = getTableNames(reflect.Indirect(v.Field(i)), tables, level+1, isTable)

? ? ? ? ? ? }

? ? ? ? }

? ? case reflect.Array, reflect.Slice:

? ? ? ? for i := 0; i < v.Len(); i++ {

? ? ? ? ? ? // enumerate all elements of an array/slice and process further

? ? ? ? ? ? tables = getTableNames(reflect.Indirect(v.Index(i)), tables, level+1, isTable)

? ? ? ? }

? ? case reflect.Interface:

? ? ? ? if v.Type().Name() == "SimpleTableExpr" {

? ? ? ? ? ? isTable = true

? ? ? ? }

? ? ? ? // get the actual object that satisfies an interface and process further

? ? ? ? tables = getTableNames(reflect.Indirect(reflect.ValueOf(v.Interface())), tables, level+1, isTable)

? ? }


? ? return tables

}


查看完整回答
反對 回復 2023-06-01
?
白衣染霜花

TA貢獻1796條經驗 獲得超10個贊

我為 SQL 查詢編寫了一些字符串操作庫來獲取表名:


queryString := sqlstr.NewQueryString(`SELECT column_name(s)

FROM table1

LEFT JOIN table2

ON table1.column_name = table2.column_name;`)


tableNames := queryString.TableNames()


fmt.Println(tableNames)


// Output:

// [table1 table2]


查看完整回答
反對 回復 2023-06-01
  • 3 回答
  • 0 關注
  • 313 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號