Skip to content

Commit 303f8b5

Browse files
committed
feat: support for multiline ref blocks and improved sqlx parser
1 parent ccd3d61 commit 303f8b5

File tree

3 files changed

+52
-19
lines changed

3 files changed

+52
-19
lines changed

assets/.sqlfluff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ quoted_identifiers_policy = none
127127
# e.g. 2 = `gcp_project_id.my_dataset.my_table`
128128
# NOTE: when using formatdataform cli it automatically generates multiple lines for you (defaulted to 30 atm)
129129
[sqlfluff:templater:placeholder]
130-
param_regex = \$\{self\(\)\}|\${ref\(\"\d*([\w]+)"\)\}|\${ref\(\'\d*([\w]+)'\)\}|\$\{[^}]*\}
130+
param_regex = (?s)\$\{\s*self\s*\(\s*\)\s*\}|\$\{\s*ref\s*\(\s*(\"?\w+\"?|\{[^}]+\})\s*\)\s*\}|\$\{\s*ref\s*\(\s*\'\s*\d*\s*([\w]+)\s*\'\s*\)\s*\}|\$\{\s*[^}]*\s*\}
131131
1 = my_table_or_replacement
132132
2 = my_table_or_replacement
133133
3 = my_table_or_replacement

cmd/sqlfluff_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ quoted_identifiers_policy = none
130130
131131
132132
[sqlfluff:templater:placeholder]
133-
param_regex = \$\{self\(\)\}|\${ref\(\"\d*([\w]+)"\)\}|\${ref\(\'\d*([\w]+)'\)\}|\$\{[^}]*\}`)
133+
param_regex = (?s)\$\{\s*self\s*\(\s*\)\s*\}|\$\{\s*ref\s*\(\s*(\"?\w+\"?|\{[^}]+\})\s*\)\s*\}|\$\{\s*ref\s*\(\s*\'\s*\d*\s*([\w]+)\s*\'\s*\)\s*\}|\$\{\s*[^}]*\s*\}`)
134134
for j := 1; j < 30; j++ {
135135
sqlfluffConfig += fmt.Sprintf(`
136136
%d = %s`, j, "my_table_or_replacement")

cmd/sqlx_parser.go

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import (
77
"strings"
88
)
99

10+
func countCurlyBraces(line string) (int, int) {
11+
openBraces := strings.Count(line, "{")
12+
closedBraces := strings.Count(line, "}")
13+
return openBraces, closedBraces
14+
}
15+
1016
type ConfigBlockMeta struct {
1117
exsists bool
1218
startOfConfigBlock int
@@ -67,8 +73,8 @@ func sqlxParser(filepath string) (sqlxParserMeta, error) {
6773
var sqlBlockExsists = false
6874
var sqlBlockContent = ""
6975

70-
var isInInnerMajorBlock = false
71-
var innerMajorBlockCount = 0
76+
var openBracesCount = 0;
77+
var closedBracesCount = 0;
7278

7379
var currentBlock = ""
7480
var currentBlockContent = ""
@@ -85,35 +91,65 @@ func sqlxParser(filepath string) (sqlxParserMeta, error) {
8591
for scanner.Scan() {
8692
i++
8793
var lineContents = scanner.Text() + "\n"
94+
openBraces, closedBraces := countCurlyBraces(lineContents)
95+
openBracesCount += openBraces
96+
closedBracesCount += closedBraces
8897

8998
if strings.Contains(lineContents, "config {") {
9099
inMajorBlock = true
91100
currentBlock = "config"
92101
startOfConfigBlock = i
93102
currentBlockContent += lineContents
103+
104+
if (openBracesCount == closedBracesCount) && inMajorBlock {
105+
configBlockContent = currentBlockContent
106+
endOfConfigBlock = i
107+
configBlockExsists = true
108+
currentBlock = ""
109+
currentBlockContent = ""
110+
}
111+
94112
} else if strings.Contains(lineContents, "post_operations {") && !inMajorBlock {
95113
startOfpostOperationsBlock = i
96114
inMajorBlock = true
97115
currentBlock = "post_operations"
98116
currentBlockContent += lineContents
117+
118+
if (openBracesCount == closedBracesCount) && inMajorBlock {
119+
endOfpostOperationsBlock = i
120+
postOpsBlockMeta := PostOpsBlockMeta{
121+
exsists: true,
122+
startOfpostOperationsBlock: startOfpostOperationsBlock,
123+
endOfpostOperationsBlock: endOfpostOperationsBlock,
124+
postOpsBlockContent: currentBlockContent,
125+
}
126+
postOpsBlocksMeta = append(postOpsBlocksMeta, postOpsBlockMeta)
127+
currentBlock = ""
128+
currentBlockContent = ""
129+
inMajorBlock = false
130+
}
99131
} else if strings.Contains(lineContents, "pre_operations {") && !inMajorBlock {
100132
inMajorBlock = true
101133
currentBlock = "pre_operations"
102134
startOfPreOperationsBlock = i
103135
currentBlockContent += lineContents
104-
} else if strings.Contains(lineContents, "{") && inMajorBlock {
105-
if strings.Contains(lineContents, "}") {
106-
currentBlockContent += lineContents
107-
continue
108-
} else {
109-
isInInnerMajorBlock = true
110-
innerMajorBlockCount += 1
111-
}
112-
currentBlockContent += lineContents
113-
} else if strings.Contains(lineContents, "}") && isInInnerMajorBlock && innerMajorBlockCount >= 1 && inMajorBlock {
114-
innerMajorBlockCount -= 1
136+
137+
if (openBracesCount == closedBracesCount) && inMajorBlock {
138+
endOfPreOperationsBlock = i
139+
preOpsBlockMeta := PreOpsBlockMeta{
140+
exsists: true,
141+
startOfPreOperationsBlock: startOfPreOperationsBlock,
142+
endOfPreOperationsBlock: endOfPreOperationsBlock,
143+
preOpsBlockContent: currentBlockContent,
144+
}
145+
preOpsBlocksMeta = append(preOpsBlocksMeta, preOpsBlockMeta)
146+
currentBlock = ""
147+
currentBlockContent = ""
148+
inMajorBlock = false
149+
}
150+
} else if inMajorBlock && (openBracesCount != closedBracesCount) {
115151
currentBlockContent += lineContents
116-
} else if strings.Contains(lineContents, "}") && innerMajorBlockCount == 0 && inMajorBlock {
152+
} else if inMajorBlock && (openBracesCount == closedBracesCount) {
117153
if currentBlock == "config" {
118154
currentBlockContent += lineContents
119155
configBlockContent = currentBlockContent
@@ -147,9 +183,6 @@ func sqlxParser(filepath string) (sqlxParserMeta, error) {
147183
currentBlockContent = ""
148184
}
149185
inMajorBlock = false
150-
} else if strings.Contains(lineContents, "}") && isInInnerMajorBlock && innerMajorBlockCount >= 1 && !inMajorBlock {
151-
innerMajorBlockCount -= 1
152-
currentBlockContent += lineContents
153186
} else if lineContents != "\n" && !inMajorBlock {
154187
if startOfSqlBlock == 0 {
155188
startOfSqlBlock = i

0 commit comments

Comments
 (0)