| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.schema; |
| 7 |
|
| 8 |
import java.util.Collections; |
| 9 |
import java.util.HashMap; |
| 10 |
import java.util.Map; |
| 11 |
|
| 12 |
import org.h2.engine.Constants; |
| 13 |
import org.h2.engine.Database; |
| 14 |
import org.h2.engine.SessionLocal; |
| 15 |
import org.h2.engine.User; |
| 16 |
import org.h2.table.InformationSchemaTable; |
| 17 |
import org.h2.table.InformationSchemaTableLegacy; |
| 18 |
import org.h2.table.Table; |
| 19 |
|
| 20 |
/** |
| 21 |
* Information schema. |
| 22 |
*/ |
| 23 |
public final class InformationSchema extends MetaSchema { |
| 24 |
|
| 25 |
private volatile HashMap<String, Table> newTables; |
| 26 |
|
| 27 |
private volatile HashMap<String, Table> oldTables; |
| 28 |
|
| 29 |
/** |
| 30 |
* Creates new instance of information schema. |
| 31 |
* |
| 32 |
* @param database |
| 33 |
* the database |
| 34 |
* @param owner |
| 35 |
* the owner of the schema (system user) |
| 36 |
*/ |
| 37 |
public InformationSchema(Database database, User owner) { |
| 38 |
super(database, Constants.INFORMATION_SCHEMA_ID, database.sysIdentifier("INFORMATION_SCHEMA"), owner); |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
protected Map<String, Table> getMap(SessionLocal session) { |
| 43 |
if (session == null) { |
| 44 |
return Collections.emptyMap(); |
| 45 |
} |
| 46 |
boolean old = session.isOldInformationSchema(); |
| 47 |
HashMap<String, Table> map = old ? oldTables : newTables; |
| 48 |
if (map == null) { |
| 49 |
map = fillMap(old); |
| 50 |
} |
| 51 |
return map; |
| 52 |
} |
| 53 |
|
| 54 |
private synchronized HashMap<String, Table> fillMap(boolean old) { |
| 55 |
HashMap<String, Table> map = old ? oldTables : newTables; |
| 56 |
if (map == null) { |
| 57 |
map = database.newStringMap(64); |
| 58 |
if (old) { |
| 59 |
for (int type = 0; type < InformationSchemaTableLegacy.META_TABLE_TYPE_COUNT; type++) { |
| 60 |
InformationSchemaTableLegacy table = new InformationSchemaTableLegacy(this, |
| 61 |
Constants.INFORMATION_SCHEMA_ID - type, type); |
| 62 |
map.put(table.getName(), table); |
| 63 |
} |
| 64 |
oldTables = map; |
| 65 |
} else { |
| 66 |
for (int type = 0; type < InformationSchemaTable.META_TABLE_TYPE_COUNT; type++) { |
| 67 |
InformationSchemaTable table = new InformationSchemaTable(this, |
| 68 |
Constants.INFORMATION_SCHEMA_ID - type, type); |
| 69 |
map.put(table.getName(), table); |
| 70 |
} |
| 71 |
newTables = map; |
| 72 |
} |
| 73 |
} |
| 74 |
return map; |
| 75 |
} |
| 76 |
|
| 77 |
} |