matchDatabases applies tiered matching (exact > case-insensitive > substring) and returns the resolved result, an ambiguous result, or a not-found error.
(databases []databaseEntry, database, instance, project string)
| 129 | // matchDatabases applies tiered matching (exact > case-insensitive > substring) and |
| 130 | // returns the resolved result, an ambiguous result, or a not-found error. |
| 131 | func matchDatabases(databases []databaseEntry, database, instance, project string) (*resolvedDatabase, error) { |
| 132 | matches := matchExact(databases, database) |
| 133 | if len(matches) == 0 { |
| 134 | matches = matchCaseInsensitive(databases, database) |
| 135 | } |
| 136 | if len(matches) == 0 { |
| 137 | matches = matchSubstring(databases, database) |
| 138 | } |
| 139 | |
| 140 | if len(matches) == 0 { |
| 141 | suggestion := "check the database name or use search_api to list available databases" |
| 142 | if instance != "" || project != "" { |
| 143 | suggestion = "try without instance/project filters, or use search_api to list available databases" |
| 144 | } |
| 145 | return nil, &toolError{ |
| 146 | Code: "DATABASE_NOT_FOUND", |
| 147 | Message: fmt.Sprintf("no database matching %q", database), |
| 148 | Suggestion: suggestion, |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if len(matches) > 1 { |
| 153 | return buildAmbiguousResult(matches), nil |
| 154 | } |
| 155 | |
| 156 | db := matches[0] |
| 157 | return &resolvedDatabase{ |
| 158 | resourceName: db.Name, |
| 159 | dataSourceID: selectDataSource(db.InstanceResource.DataSources), |
| 160 | engine: db.InstanceResource.Engine, |
| 161 | project: db.Project, |
| 162 | }, nil |
| 163 | } |
| 164 | |
| 165 | // buildAmbiguousResult constructs a resolvedDatabase with multiple candidates. |
| 166 | func buildAmbiguousResult(matches []databaseEntry) *resolvedDatabase { |