allDatabasesWithNames returns the current database(s) and their catalog and schema names.
(ctx *sql.Context, cat sql.Catalog, privCheck bool)
| 23 | |
| 24 | // allDatabasesWithNames returns the current database(s) and their catalog and schema names. |
| 25 | func allDatabasesWithNames(ctx *sql.Context, cat sql.Catalog, privCheck bool) ([]information_schema.DbWithNames, error) { |
| 26 | var dbs []information_schema.DbWithNames |
| 27 | |
| 28 | currentDB := ctx.GetCurrentDatabase() |
| 29 | currentRevDB, _ := doltdb.SplitRevisionDbName(currentDB) |
| 30 | |
| 31 | allDbs := cat.AllDatabases(ctx) |
| 32 | for _, db := range allDbs { |
| 33 | // Always unwrap PrivilegedDatabase to reach the underlying SchemaDatabase implementation. |
| 34 | // PrivilegedDatabase does not implement sql.SchemaDatabase; unwrapping is required for |
| 35 | // schema iteration regardless of privilege-check mode. |
| 36 | if privDatabase, ok := db.(mysql_db.PrivilegedDatabase); ok { |
| 37 | db = privDatabase.Unwrap() |
| 38 | } |
| 39 | |
| 40 | sdb, ok := db.(sql.SchemaDatabase) |
| 41 | if ok { |
| 42 | var dbsForSchema []information_schema.DbWithNames |
| 43 | schemas, err := sdb.AllSchemas(ctx) |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | |
| 48 | for _, schema := range schemas { |
| 49 | dbName := db.Name() |
| 50 | revDb, _ := doltdb.SplitRevisionDbName(dbName) |
| 51 | // Add database it is the current database/revision database and if SchemaName exists |
| 52 | if schema.SchemaName() != "" && (dbName == currentDB || revDb == currentRevDB) { |
| 53 | dbsForSchema = append(dbsForSchema, information_schema.DbWithNames{ |
| 54 | Database: schema, |
| 55 | CatalogName: schema.Name(), |
| 56 | SchemaName: schema.SchemaName(), |
| 57 | }) |
| 58 | } |
| 59 | } |
| 60 | dbs = append(dbs, dbsForSchema...) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return dbs, nil |
| 65 | } |
no test coverage detected