ResolveQueryAndArgsFromSQLString attempts to resolve 'arg' to a query and query args
(sqlString string)
| 152 | |
| 153 | // ResolveQueryAndArgsFromSQLString attempts to resolve 'arg' to a query and query args |
| 154 | func ResolveQueryAndArgsFromSQLString(sqlString string) (*modconfig.ResolvedQuery, error) { |
| 155 | var err error |
| 156 | |
| 157 | // 2) is this a file |
| 158 | // get absolute filename |
| 159 | filePath, err := filepath.Abs(sqlString) |
| 160 | if err != nil { |
| 161 | return nil, fmt.Errorf("%s", err.Error()) |
| 162 | } |
| 163 | fileQuery, fileExists, err := getQueryFromFile(filePath) |
| 164 | if err != nil { |
| 165 | return nil, fmt.Errorf("%s", err.Error()) |
| 166 | } |
| 167 | if fileExists { |
| 168 | if fileQuery.ExecuteSQL == "" { |
| 169 | error_helpers.ShowWarning(fmt.Sprintf("file '%s' does not contain any data", filePath)) |
| 170 | // (just return the empty query - it will be filtered above) |
| 171 | } |
| 172 | return fileQuery, nil |
| 173 | } |
| 174 | // the argument cannot be resolved as an existing file |
| 175 | // if it has a sql suffix (i.e we believe the user meant to specify a file) return a file not found error |
| 176 | if strings.HasSuffix(strings.ToLower(sqlString), ".sql") { |
| 177 | return nil, fmt.Errorf("file '%s' does not exist", filePath) |
| 178 | } |
| 179 | |
| 180 | // 2) just use the query string as is and assume it is valid SQL |
| 181 | return &modconfig.ResolvedQuery{RawSQL: sqlString, ExecuteSQL: sqlString}, nil |
| 182 | } |
| 183 | |
| 184 | // try to treat the input string as a file name and if it exists, return its contents |
| 185 | func getQueryFromFile(input string) (*modconfig.ResolvedQuery, bool, error) { |
no test coverage detected