[#6309] fixed fields extraction for view query with multi-level comments
This commit is contained in:
parent
2317695011
commit
b8ea953059
16
core/view.go
16
core/view.go
|
@ -437,9 +437,11 @@ func getQueryTableInfo(app App, selectQuery string) ([]*TableInfoRow, error) {
|
||||||
// Raw query identifiers parser
|
// Raw query identifiers parser
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
|
|
||||||
var joinReplaceRegex = regexp.MustCompile(`(?im)\s+(full\s+outer\s+join|left\s+outer\s+join|right\s+outer\s+join|full\s+join|cross\s+join|inner\s+join|outer\s+join|left\s+join|right\s+join|join)\s+?`)
|
var (
|
||||||
var discardReplaceRegex = regexp.MustCompile(`(?im)\s+(where|group\s+by|having|order|limit|with)\s+?`)
|
joinReplaceRegex = regexp.MustCompile(`(?im)\s+(full\s+outer\s+join|left\s+outer\s+join|right\s+outer\s+join|full\s+join|cross\s+join|inner\s+join|outer\s+join|left\s+join|right\s+join|join)\s+?`)
|
||||||
var commentsReplaceRegex = regexp.MustCompile(`(?m)(\/\*[\s\S]+\*\/)|(--.+$)`)
|
discardReplaceRegex = regexp.MustCompile(`(?im)\s+(where|group\s+by|having|order|limit|with)\s+?`)
|
||||||
|
commentsReplaceRegex = regexp.MustCompile(`(?m)(\/\*[\s\S]+\*\/)|(--.+$)`)
|
||||||
|
)
|
||||||
|
|
||||||
type identifier struct {
|
type identifier struct {
|
||||||
original string
|
original string
|
||||||
|
@ -453,9 +455,9 @@ type identifiersParser struct {
|
||||||
|
|
||||||
func (p *identifiersParser) parse(selectQuery string) error {
|
func (p *identifiersParser) parse(selectQuery string) error {
|
||||||
str := strings.Trim(strings.TrimSpace(selectQuery), ";")
|
str := strings.Trim(strings.TrimSpace(selectQuery), ";")
|
||||||
str = joinReplaceRegex.ReplaceAllString(str, " _join_ ")
|
|
||||||
str = discardReplaceRegex.ReplaceAllString(str, " _discard_ ")
|
|
||||||
str = commentsReplaceRegex.ReplaceAllString(str, "")
|
str = commentsReplaceRegex.ReplaceAllString(str, "")
|
||||||
|
str = joinReplaceRegex.ReplaceAllString(str, " __pb_join__ ")
|
||||||
|
str = discardReplaceRegex.ReplaceAllString(str, " __pb_discard__ ")
|
||||||
|
|
||||||
tk := tokenizer.NewFromString(str)
|
tk := tokenizer.NewFromString(str)
|
||||||
tk.Separators(',', ' ', '\n', '\t')
|
tk.Separators(',', ' ', '\n', '\t')
|
||||||
|
@ -490,7 +492,7 @@ func (p *identifiersParser) parse(selectQuery string) error {
|
||||||
skip = false
|
skip = false
|
||||||
partType = "from"
|
partType = "from"
|
||||||
activeBuilder = &fromParts
|
activeBuilder = &fromParts
|
||||||
case "_join_":
|
case "__pb_join__":
|
||||||
skip = false
|
skip = false
|
||||||
|
|
||||||
// the previous part was also a join
|
// the previous part was also a join
|
||||||
|
@ -500,7 +502,7 @@ func (p *identifiersParser) parse(selectQuery string) error {
|
||||||
|
|
||||||
partType = "join"
|
partType = "join"
|
||||||
activeBuilder = &joinParts
|
activeBuilder = &joinParts
|
||||||
case "_discard_":
|
case "__pb_discard__":
|
||||||
// skip following tokens
|
// skip following tokens
|
||||||
skip = true
|
skip = true
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -246,11 +246,21 @@ func TestCreateViewFields(t *testing.T) {
|
||||||
`
|
`
|
||||||
select
|
select
|
||||||
-- test single line
|
-- test single line
|
||||||
id,
|
demo1.id,
|
||||||
text,
|
demo1.text,
|
||||||
/* multi
|
/* multi
|
||||||
line comment */
|
line comment */
|
||||||
url, created, updated from demo1
|
demo1.url, demo1.created, demo2.updated from demo1
|
||||||
|
-- comment before join
|
||||||
|
join demo2 ON (
|
||||||
|
-- comment inside join
|
||||||
|
demo2.id = demo1.id
|
||||||
|
)
|
||||||
|
-- comment before where
|
||||||
|
where (
|
||||||
|
-- comment inside where
|
||||||
|
demo2.id = demo1.id
|
||||||
|
)
|
||||||
`,
|
`,
|
||||||
false,
|
false,
|
||||||
map[string]string{
|
map[string]string{
|
||||||
|
|
Loading…
Reference in New Issue