forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.go
41 lines (36 loc) · 1.08 KB
/
analyzer.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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sqlparser
// analyzer.go contains utility analysis functions.
// TODO(sougou): Move some generic functions out of execution.go
// and router.go into this file.
import "fmt"
// GetDBName parses the specified DML and returns the
// db name if it was used to qualify the table name.
// It returns an error if parsing fails or if the statement
// is not a DML.
func GetDBName(sql string) (string, error) {
rootNode, err := Parse(sql)
if err != nil {
return "", err
}
switch rootNode.Type {
case INSERT:
return extractDBName(rootNode.At(INSERT_TABLE_OFFSET)), nil
case UPDATE:
return extractDBName(rootNode.At(UPDATE_TABLE_OFFSET)), nil
case DELETE:
return extractDBName(rootNode.At(DELETE_TABLE_OFFSET)), nil
}
return "", fmt.Errorf("statement '%s' is not a dml", sql)
}
func extractDBName(node *Node) string {
if node.Type != '.' {
return ""
}
if node.At(0).Type != ID {
return ""
}
return string(node.At(0).Value)
}