extractMetadataInternal parses a SQL string and extracts tables, columns, and functions.
(sql string)
| 124 | |
| 125 | // extractMetadataInternal parses a SQL string and extracts tables, columns, and functions. |
| 126 | func extractMetadataInternal(sql string) (map[string]any, error) { |
| 127 | if sql == "" { |
| 128 | return nil, fmt.Errorf("parameter 'sql' is required and must not be empty") |
| 129 | } |
| 130 | |
| 131 | tree, err := gosqlx.Parse(sql) |
| 132 | if err != nil { |
| 133 | return nil, fmt.Errorf("parse failed: %w", err) |
| 134 | } |
| 135 | |
| 136 | meta := gosqlx.ExtractMetadata(tree) |
| 137 | |
| 138 | tables := meta.Tables |
| 139 | if tables == nil { |
| 140 | tables = []string{} |
| 141 | } |
| 142 | columns := meta.Columns |
| 143 | if columns == nil { |
| 144 | columns = []string{} |
| 145 | } |
| 146 | functions := meta.Functions |
| 147 | if functions == nil { |
| 148 | functions = []string{} |
| 149 | } |
| 150 | |
| 151 | return map[string]any{ |
| 152 | "tables": tables, |
| 153 | "columns": columns, |
| 154 | "functions": functions, |
| 155 | }, nil |
| 156 | } |
| 157 | |
| 158 | // securityScanInternal scans a SQL string for injection patterns and other threats. |
| 159 | func securityScanInternal(sql string) (map[string]any, error) { |