MCPcopy Create free account
hub / github.com/auxten/postgresql-parser / ColNamesInSelect

Function ColNamesInSelect

pkg/walk/walker.go:255–284  ·  view source on GitHub ↗

ColNamesInSelect finds all referred variables in a Select Statement. (variables = sub-expressions, placeholders, indexed vars, etc.) Implementation limits: 1. Table with AS is not normalized. 2. Columns referred from outer query are not translated.

(sql string)

Source from the content-addressed store, hash-verified

253// 1. Table with AS is not normalized.
254// 2. Columns referred from outer query are not translated.
255func ColNamesInSelect(sql string) (referredCols ReferredCols, err error) {
256 referredCols = make(ReferredCols, 0)
257
258 w := &AstWalker{
259 Fn: func(ctx interface{}, node interface{}) (stop bool) {
260 rCols := ctx.(ReferredCols)
261 if isColumn(node) {
262 nodeName := fmt.Sprint(node)
263 // just drop the "table." part
264 tableCols := strings.Split(nodeName, ".")
265 colName := tableCols[len(tableCols)-1]
266 rCols[colName] = 1
267 }
268 return false
269 },
270 }
271 stmts, err := parser.Parse(sql)
272 if err != nil {
273 return
274 }
275
276 _, err = w.Walk(stmts, referredCols)
277 if err != nil {
278 return
279 }
280 for _, col := range w.UnknownNodes {
281 log.Printf("unhandled column type %T", col)
282 }
283 return
284}
285
286func AllColsContained(set ReferredCols, cols []string) bool {
287 if cols == nil {

Callers 2

TestParserFunction · 0.85

Calls 4

WalkMethod · 0.95
ParseFunction · 0.92
isColumnFunction · 0.85
PrintfMethod · 0.80

Tested by 2

TestParserFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…