| 2204 | } |
| 2205 | |
| 2206 | func resolveDataSourceID(ctx context.Context, instance *store.InstanceMessage, dataSourceID string, statement string, allowAdminDataSource bool) (string, error) { |
| 2207 | if dataSourceID != "" { |
| 2208 | return dataSourceID, nil |
| 2209 | } |
| 2210 | |
| 2211 | var adminDataSourceID string |
| 2212 | var readOnlyDataSourceID string |
| 2213 | readOnlyCount := 0 |
| 2214 | for _, dataSource := range instance.Metadata.GetDataSources() { |
| 2215 | switch dataSource.GetType() { |
| 2216 | case storepb.DataSourceType_ADMIN: |
| 2217 | adminDataSourceID = dataSource.GetId() |
| 2218 | case storepb.DataSourceType_READ_ONLY: |
| 2219 | readOnlyCount++ |
| 2220 | readOnlyDataSourceID = dataSource.GetId() |
| 2221 | default: |
| 2222 | } |
| 2223 | } |
| 2224 | |
| 2225 | if allowAdminDataSource && adminDataSourceID != "" && requiresAdminDataSource(ctx, instance.Metadata.GetEngine(), statement) { |
| 2226 | return adminDataSourceID, nil |
| 2227 | } |
| 2228 | |
| 2229 | switch { |
| 2230 | case readOnlyCount == 1: |
| 2231 | return readOnlyDataSourceID, nil |
| 2232 | case readOnlyCount > 1: |
| 2233 | return "", connect.NewError(connect.CodeFailedPrecondition, errors.New("instance has multiple read-only data sources, please specify data_source_id explicitly")) |
| 2234 | case adminDataSourceID != "": |
| 2235 | return adminDataSourceID, nil |
| 2236 | default: |
| 2237 | return "", connect.NewError(connect.CodeFailedPrecondition, errors.New("instance has no admin data source")) |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | func requiresAdminDataSource(ctx context.Context, engine storepb.Engine, statement string) bool { |
| 2242 | readOnly, _, err := parserbase.ValidateSQLForEditor(engine, statement) |