| 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.table; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
|
| 10 |
import org.h2.engine.SessionLocal; |
| 11 |
import org.h2.index.Index; |
| 12 |
import org.h2.index.IndexType; |
| 13 |
import org.h2.index.MetaIndex; |
| 14 |
import org.h2.message.DbException; |
| 15 |
import org.h2.result.Row; |
| 16 |
import org.h2.result.SearchRow; |
| 17 |
import org.h2.schema.Schema; |
| 18 |
import org.h2.util.StringUtils; |
| 19 |
import org.h2.value.TypeInfo; |
| 20 |
import org.h2.value.Value; |
| 21 |
import org.h2.value.ValueNull; |
| 22 |
import org.h2.value.ValueVarchar; |
| 23 |
import org.h2.value.ValueVarcharIgnoreCase; |
| 24 |
|
| 25 |
/** |
| 26 |
* This class is responsible to build the database meta data pseudo tables. |
| 27 |
*/ |
| 28 |
public abstract class MetaTable extends Table { |
| 29 |
|
| 30 |
/** |
| 31 |
* The approximate number of rows of a meta table. |
| 32 |
*/ |
| 33 |
public static final long ROW_COUNT_APPROXIMATION = 1000; |
| 34 |
|
| 35 |
/** |
| 36 |
* The table type. |
| 37 |
*/ |
| 38 |
protected final int type; |
| 39 |
|
| 40 |
/** |
| 41 |
* The indexed column. |
| 42 |
*/ |
| 43 |
protected int indexColumn; |
| 44 |
|
| 45 |
/** |
| 46 |
* The index for this table. |
| 47 |
*/ |
| 48 |
protected MetaIndex metaIndex; |
| 49 |
|
| 50 |
/** |
| 51 |
* Create a new metadata table. |
| 52 |
* |
| 53 |
* @param schema the schema |
| 54 |
* @param id the object id |
| 55 |
* @param type the meta table type |
| 56 |
*/ |
| 57 |
protected MetaTable(Schema schema, int id, int type) { |
| 58 |
// tableName will be set later |
| 59 |
super(schema, id, null, true, true); |
| 60 |
this.type = type; |
| 61 |
} |
| 62 |
|
| 63 |
protected final void setMetaTableName(String upperName) { |
| 64 |
setObjectName(database.sysIdentifier(upperName)); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Creates a column with the specified name and character string data type. |
| 69 |
* |
| 70 |
* @param name |
| 71 |
* the uppercase column name |
| 72 |
* @return the column |
| 73 |
*/ |
| 74 |
final Column column(String name) { |
| 75 |
return new Column(database.sysIdentifier(name), |
| 76 |
database.getSettings().caseInsensitiveIdentifiers ? TypeInfo.TYPE_VARCHAR_IGNORECASE |
| 77 |
: TypeInfo.TYPE_VARCHAR); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Creates a column with the specified name and data type. |
| 82 |
* |
| 83 |
* @param name |
| 84 |
* the uppercase column name |
| 85 |
* @param type |
| 86 |
* the data type |
| 87 |
* @return the column |
| 88 |
*/ |
| 89 |
protected final Column column(String name, TypeInfo type) { |
| 90 |
return new Column(database.sysIdentifier(name), type); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public final String getCreateSQL() { |
| 95 |
return null; |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public final Index addIndex(SessionLocal session, String indexName, int indexId, IndexColumn[] cols, |
| 100 |
int uniqueColumnCount, IndexType indexType, boolean create, String indexComment) { |
| 101 |
throw DbException.getUnsupportedException("META"); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* If needed, convert the identifier to lower case. |
| 106 |
* |
| 107 |
* @param s the identifier to convert |
| 108 |
* @return the converted identifier |
| 109 |
*/ |
| 110 |
protected final String identifier(String s) { |
| 111 |
if (database.getSettings().databaseToLower) { |
| 112 |
s = s == null ? null : StringUtils.toLowerEnglish(s); |
| 113 |
} |
| 114 |
return s; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Checks index conditions. |
| 119 |
* |
| 120 |
* @param session the session |
| 121 |
* @param value the value |
| 122 |
* @param indexFrom the lower bound of value, or {@code null} |
| 123 |
* @param indexTo the higher bound of value, or {@code null} |
| 124 |
* @return whether row should be included into result |
| 125 |
*/ |
| 126 |
protected final boolean checkIndex(SessionLocal session, String value, Value indexFrom, Value indexTo) { |
| 127 |
if (value == null || (indexFrom == null && indexTo == null)) { |
| 128 |
return true; |
| 129 |
} |
| 130 |
Value v; |
| 131 |
if (database.getSettings().caseInsensitiveIdentifiers) { |
| 132 |
v = ValueVarcharIgnoreCase.get(value); |
| 133 |
} else { |
| 134 |
v = ValueVarchar.get(value); |
| 135 |
} |
| 136 |
if (indexFrom != null && session.compare(v, indexFrom) < 0) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
if (indexTo != null && session.compare(v, indexTo) > 0) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Check whether to hide the table. Tables are never hidden in the system |
| 147 |
* session. |
| 148 |
* |
| 149 |
* @param table the table |
| 150 |
* @param session the session |
| 151 |
* @return whether the table is hidden |
| 152 |
*/ |
| 153 |
protected final boolean hideTable(Table table, SessionLocal session) { |
| 154 |
return table.isHidden() && session != database.getSystemSession(); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Generate the data for the given metadata table using the given first and |
| 159 |
* last row filters. |
| 160 |
* |
| 161 |
* @param session the session |
| 162 |
* @param first the first row to return |
| 163 |
* @param last the last row to return |
| 164 |
* @return the generated rows |
| 165 |
*/ |
| 166 |
public abstract ArrayList<Row> generateRows(SessionLocal session, SearchRow first, SearchRow last); |
| 167 |
|
| 168 |
@Override |
| 169 |
public boolean isInsertable() { |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
public final void removeRow(SessionLocal session, Row row) { |
| 175 |
throw DbException.getUnsupportedException("META"); |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public final void addRow(SessionLocal session, Row row) { |
| 180 |
throw DbException.getUnsupportedException("META"); |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public final void removeChildrenAndResources(SessionLocal session) { |
| 185 |
throw DbException.getUnsupportedException("META"); |
| 186 |
} |
| 187 |
|
| 188 |
@Override |
| 189 |
public final void close(SessionLocal session) { |
| 190 |
// nothing to do |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Add a row to a list. |
| 195 |
* |
| 196 |
* @param session the session |
| 197 |
* @param rows the original row list |
| 198 |
* @param stringsOrValues the values, or strings |
| 199 |
*/ |
| 200 |
protected final void add(SessionLocal session, ArrayList<Row> rows, Object... stringsOrValues) { |
| 201 |
Value[] values = new Value[stringsOrValues.length]; |
| 202 |
for (int i = 0; i < stringsOrValues.length; i++) { |
| 203 |
Object s = stringsOrValues[i]; |
| 204 |
Value v = s == null ? ValueNull.INSTANCE : s instanceof String ? ValueVarchar.get((String) s) : (Value) s; |
| 205 |
values[i] = columns[i].convert(session, v); |
| 206 |
} |
| 207 |
rows.add(Row.get(values, 1, rows.size())); |
| 208 |
} |
| 209 |
|
| 210 |
@Override |
| 211 |
public final void checkRename() { |
| 212 |
throw DbException.getUnsupportedException("META"); |
| 213 |
} |
| 214 |
|
| 215 |
@Override |
| 216 |
public final void checkSupportAlter() { |
| 217 |
throw DbException.getUnsupportedException("META"); |
| 218 |
} |
| 219 |
|
| 220 |
@Override |
| 221 |
public final long truncate(SessionLocal session) { |
| 222 |
throw DbException.getUnsupportedException("META"); |
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public long getRowCount(SessionLocal session) { |
| 227 |
throw DbException.getInternalError(toString()); |
| 228 |
} |
| 229 |
|
| 230 |
@Override |
| 231 |
public boolean canGetRowCount(SessionLocal session) { |
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
@Override |
| 236 |
public final boolean canDrop() { |
| 237 |
return false; |
| 238 |
} |
| 239 |
|
| 240 |
@Override |
| 241 |
public final TableType getTableType() { |
| 242 |
return TableType.SYSTEM_TABLE; |
| 243 |
} |
| 244 |
|
| 245 |
@Override |
| 246 |
public final Index getScanIndex(SessionLocal session) { |
| 247 |
return new MetaIndex(this, IndexColumn.wrap(columns), true); |
| 248 |
} |
| 249 |
|
| 250 |
@Override |
| 251 |
public final ArrayList<Index> getIndexes() { |
| 252 |
ArrayList<Index> list = new ArrayList<>(2); |
| 253 |
if (metaIndex == null) { |
| 254 |
return list; |
| 255 |
} |
| 256 |
list.add(new MetaIndex(this, IndexColumn.wrap(columns), true)); |
| 257 |
// TODO re-use the index |
| 258 |
list.add(metaIndex); |
| 259 |
return list; |
| 260 |
} |
| 261 |
|
| 262 |
@Override |
| 263 |
public long getRowCountApproximation(SessionLocal session) { |
| 264 |
return ROW_COUNT_APPROXIMATION; |
| 265 |
} |
| 266 |
|
| 267 |
@Override |
| 268 |
public final boolean isDeterministic() { |
| 269 |
return true; |
| 270 |
} |
| 271 |
|
| 272 |
@Override |
| 273 |
public final boolean canReference() { |
| 274 |
return false; |
| 275 |
} |
| 276 |
|
| 277 |
} |