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)
| 253 | // 1. Table with AS is not normalized. |
| 254 | // 2. Columns referred from outer query are not translated. |
| 255 | func 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 | |
| 286 | func AllColsContained(set ReferredCols, cols []string) bool { |
| 287 | if cols == nil { |
searching dependent graphs…