MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / parseUnpivotClause

Method parseUnpivotClause

pkg/sql/parser/pivot.go:193–266  ·  view source on GitHub ↗

parseUnpivotClause parses UNPIVOT (value_col FOR name_col IN (columns)). The current token must be the UNPIVOT keyword.

()

Source from the content-addressed store, hash-verified

191// parseUnpivotClause parses UNPIVOT (value_col FOR name_col IN (columns)).
192// The current token must be the UNPIVOT keyword.
193func (p *Parser) parseUnpivotClause() (*ast.UnpivotClause, error) {
194 pos := p.currentLocation()
195 p.advance() // consume UNPIVOT
196
197 if !p.isType(models.TokenTypeLParen) {
198 return nil, p.expectedError("( after UNPIVOT")
199 }
200 p.advance() // consume (
201
202 // Parse value column name
203 if !p.isIdentifier() {
204 return nil, p.expectedError("value column name in UNPIVOT")
205 }
206 valueCol := p.currentToken.Token.Value
207 p.advance()
208
209 // Expect FOR keyword
210 if !p.isType(models.TokenTypeFor) {
211 return nil, p.expectedError("FOR in UNPIVOT clause")
212 }
213 p.advance() // consume FOR
214
215 // Parse name column
216 if !p.isIdentifier() {
217 return nil, p.expectedError("name column after FOR in UNPIVOT")
218 }
219 nameCol := p.currentToken.Token.Value
220 p.advance()
221
222 // Expect IN keyword
223 if !p.isType(models.TokenTypeIn) {
224 return nil, p.expectedError("IN in UNPIVOT clause")
225 }
226 p.advance() // consume IN
227
228 // Expect opening parenthesis for column list
229 if !p.isType(models.TokenTypeLParen) {
230 return nil, p.expectedError("( after IN in UNPIVOT")
231 }
232 p.advance() // consume (
233
234 // Parse IN columns
235 var cols []string
236 for !p.isType(models.TokenTypeRParen) && !p.isType(models.TokenTypeEOF) {
237 if !p.isIdentifier() {
238 return nil, p.expectedError("column name in UNPIVOT IN list")
239 }
240 cols = append(cols, renderQuotedIdent(p.currentToken.Token))
241 p.advance()
242 if p.isType(models.TokenTypeComma) {
243 p.advance()
244 }
245 }
246
247 if len(cols) == 0 {
248 return nil, p.expectedError("at least one column in UNPIVOT IN list")
249 }
250 if !p.isType(models.TokenTypeRParen) {

Callers 2

parseJoinedTableRefMethod · 0.95

Calls 6

currentLocationMethod · 0.95
advanceMethod · 0.95
isTypeMethod · 0.95
expectedErrorMethod · 0.95
isIdentifierMethod · 0.95
renderQuotedIdentFunction · 0.85

Tested by

no test coverage detected