GetSqlTableFromContext returns the table from the context. Uses the context's current database if an empty database name is provided. Returns nil if no table was found.
(ctx *sql.Context, databaseName string, tableName doltdb.TableName)
| 209 | // GetSqlTableFromContext returns the table from the context. Uses the context's current database if an empty database |
| 210 | // name is provided. Returns nil if no table was found. |
| 211 | func GetSqlTableFromContext(ctx *sql.Context, databaseName string, tableName doltdb.TableName) (sql.Table, error) { |
| 212 | db, err := GetSqlDatabaseFromContext(ctx, databaseName) |
| 213 | if err != nil || db == nil { |
| 214 | return nil, err |
| 215 | } |
| 216 | schemaDb, ok := db.(sql.SchemaDatabase) |
| 217 | if !ok { |
| 218 | // Fairly confident that Dolt only has database implementations that inherit sql.SchemaDatabase, so only GMS |
| 219 | // databases may fail here (like information_schema). In this scenario, we expect that no schema will be passed, |
| 220 | // so we'll special-case it here. |
| 221 | if len(tableName.Schema) == 0 { |
| 222 | tbl, ok, err := db.GetTableInsensitive(ctx, tableName.Name) |
| 223 | if err != nil || !ok { |
| 224 | return nil, err |
| 225 | } |
| 226 | return tbl, nil |
| 227 | } |
| 228 | return nil, nil |
| 229 | } |
| 230 | |
| 231 | var searchPath []string |
| 232 | if len(tableName.Schema) == 0 { |
| 233 | // If a schema was not provided, then we'll use the search path |
| 234 | searchPath, err = SearchPath(ctx) |
| 235 | if err != nil { |
| 236 | return nil, err |
| 237 | } |
| 238 | } else { |
| 239 | // A specific schema is given, so we'll only use that one for the search path |
| 240 | searchPath = []string{tableName.Schema} |
| 241 | } |
| 242 | |
| 243 | for _, schema := range searchPath { |
| 244 | db, ok, err = schemaDb.GetSchema(ctx, schema) |
| 245 | if err != nil { |
| 246 | return nil, err |
| 247 | } |
| 248 | if !ok { |
| 249 | continue |
| 250 | } |
| 251 | tbl, ok, err := db.GetTableInsensitive(ctx, tableName.Name) |
| 252 | if err != nil { |
| 253 | return nil, err |
| 254 | } |
| 255 | if !ok { |
| 256 | continue |
| 257 | } |
| 258 | return tbl, nil |
| 259 | } |
| 260 | return nil, nil |
| 261 | } |
| 262 | |
| 263 | // SearchPath returns the effective schema search path for the current session |
| 264 | func SearchPath(ctx *sql.Context) ([]string, error) { |
no test coverage detected