MCPcopy
hub / github.com/turbot/steampipe / ResolveQueryAndArgsFromSQLString

Function ResolveQueryAndArgsFromSQLString

pkg/query/init_data.go:154–182  ·  view source on GitHub ↗

ResolveQueryAndArgsFromSQLString attempts to resolve 'arg' to a query and query args

(sqlString string)

Source from the content-addressed store, hash-verified

152
153// ResolveQueryAndArgsFromSQLString attempts to resolve 'arg' to a query and query args
154func 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
185func getQueryFromFile(input string) (*modconfig.ResolvedQuery, bool, error) {

Callers 2

getQueryMethod · 0.92
getQueriesFromArgsFunction · 0.85

Calls 3

ShowWarningFunction · 0.92
getQueryFromFileFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected