Your Question

  • I want to change all the SQL's table names with a fixed prefix before the process. I don't know how to do this with the *gorm.DB object like this https://gorm.io/zh_CN/docs/hooks.html#%E4%BF%AE%E6%94%B9%E5%BD%93%E5%89%8D%E6%93%8D%E4%BD%9C
  • Is there any example or document can help me to resolve this problem?
  • Or any document about the clause introduction?

What I Want

  • Now I am using the TiDB sqlparser github.com/pingcap/tidb/pkg/parser to modify the SQL, for example:
type MirrorTableModifier struct {
    tblNameMap map[string]string
}

func (m *MirrorTableModifier) Enter(in ast.Node) (ast.Node, bool) {
    switch node := in.(type) {
    case *ast.TableName:
        if modifyName, ok := m.tblNameMap[node.Name.String()]; ok {
            node.Name = model.NewCIStr(modifyName)  // modify table name here
        }
    }
    return in, false
}

func (m *MirrorTableModifier) Leave(in ast.Node) (ast.Node, bool) {
    return in, true
}
  • So how can I use this sqlparser together with gorm callback?