| 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.Collections; |
| 9 |
import java.util.List; |
| 10 |
import org.h2.command.ddl.CreateTableData; |
| 11 |
import org.h2.engine.Database; |
| 12 |
import org.h2.index.IndexType; |
| 13 |
import org.h2.result.SearchRow; |
| 14 |
import org.h2.result.SortOrder; |
| 15 |
import org.h2.util.StringUtils; |
| 16 |
import org.h2.value.Value; |
| 17 |
|
| 18 |
/** |
| 19 |
* The base class of a regular table, or a user defined table. |
| 20 |
* |
| 21 |
* @author Thomas Mueller |
| 22 |
* @author Sergi Vladykin |
| 23 |
*/ |
| 24 |
public abstract class TableBase extends Table { |
| 25 |
|
| 26 |
/** |
| 27 |
* The table engine used (null for regular tables). |
| 28 |
*/ |
| 29 |
private final String tableEngine; |
| 30 |
/** Provided table parameters */ |
| 31 |
private final List<String> tableEngineParams; |
| 32 |
|
| 33 |
private final boolean globalTemporary; |
| 34 |
|
| 35 |
/** |
| 36 |
* Returns main index column if index is an primary key index and has only |
| 37 |
* one column with _ROWID_ compatible data type. |
| 38 |
* |
| 39 |
* @param indexType type of an index |
| 40 |
* @param cols columns of the index |
| 41 |
* @return main index column or {@link SearchRow#ROWID_INDEX} |
| 42 |
*/ |
| 43 |
public static int getMainIndexColumn(IndexType indexType, IndexColumn[] cols) { |
| 44 |
if (!indexType.isPrimaryKey() || cols.length != 1) { |
| 45 |
return SearchRow.ROWID_INDEX; |
| 46 |
} |
| 47 |
IndexColumn first = cols[0]; |
| 48 |
if ((first.sortType & SortOrder.DESCENDING) != 0) { |
| 49 |
return SearchRow.ROWID_INDEX; |
| 50 |
} |
| 51 |
switch (first.column.getType().getValueType()) { |
| 52 |
case Value.TINYINT: |
| 53 |
case Value.SMALLINT: |
| 54 |
case Value.INTEGER: |
| 55 |
case Value.BIGINT: |
| 56 |
return first.column.getColumnId(); |
| 57 |
default: |
| 58 |
return SearchRow.ROWID_INDEX; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
public TableBase(CreateTableData data) { |
| 63 |
super(data.schema, data.id, data.tableName, |
| 64 |
data.persistIndexes, data.persistData); |
| 65 |
this.tableEngine = data.tableEngine; |
| 66 |
this.globalTemporary = data.globalTemporary; |
| 67 |
if (data.tableEngineParams != null) { |
| 68 |
this.tableEngineParams = data.tableEngineParams; |
| 69 |
} else { |
| 70 |
this.tableEngineParams = Collections.emptyList(); |
| 71 |
} |
| 72 |
setTemporary(data.temporary); |
| 73 |
setColumns(data.columns.toArray(new Column[0])); |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public String getDropSQL() { |
| 78 |
StringBuilder builder = new StringBuilder("DROP TABLE IF EXISTS "); |
| 79 |
getSQL(builder, DEFAULT_SQL_FLAGS).append(" CASCADE"); |
| 80 |
return builder.toString(); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public String getCreateSQLForMeta() { |
| 85 |
return getCreateSQL(true); |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public String getCreateSQL() { |
| 90 |
return getCreateSQL(false); |
| 91 |
} |
| 92 |
|
| 93 |
private String getCreateSQL(boolean forMeta) { |
| 94 |
Database db = getDatabase(); |
| 95 |
if (db == null) { |
| 96 |
// closed |
| 97 |
return null; |
| 98 |
} |
| 99 |
StringBuilder buff = new StringBuilder("CREATE "); |
| 100 |
if (isTemporary()) { |
| 101 |
if (isGlobalTemporary()) { |
| 102 |
buff.append("GLOBAL "); |
| 103 |
} else { |
| 104 |
buff.append("LOCAL "); |
| 105 |
} |
| 106 |
buff.append("TEMPORARY "); |
| 107 |
} else if (isPersistIndexes()) { |
| 108 |
buff.append("CACHED "); |
| 109 |
} else { |
| 110 |
buff.append("MEMORY "); |
| 111 |
} |
| 112 |
buff.append("TABLE "); |
| 113 |
if (isHidden) { |
| 114 |
buff.append("IF NOT EXISTS "); |
| 115 |
} |
| 116 |
getSQL(buff, DEFAULT_SQL_FLAGS); |
| 117 |
if (comment != null) { |
| 118 |
buff.append(" COMMENT "); |
| 119 |
StringUtils.quoteStringSQL(buff, comment); |
| 120 |
} |
| 121 |
buff.append("(\n "); |
| 122 |
for (int i = 0, l = columns.length; i < l; i++) { |
| 123 |
if (i > 0) { |
| 124 |
buff.append(",\n "); |
| 125 |
} |
| 126 |
buff.append(columns[i].getCreateSQL(forMeta)); |
| 127 |
} |
| 128 |
buff.append("\n)"); |
| 129 |
if (tableEngine != null) { |
| 130 |
String d = db.getSettings().defaultTableEngine; |
| 131 |
if (d == null || !tableEngine.endsWith(d)) { |
| 132 |
buff.append("\nENGINE "); |
| 133 |
StringUtils.quoteIdentifier(buff, tableEngine); |
| 134 |
} |
| 135 |
} |
| 136 |
if (!tableEngineParams.isEmpty()) { |
| 137 |
buff.append("\nWITH "); |
| 138 |
for (int i = 0, l = tableEngineParams.size(); i < l; i++) { |
| 139 |
if (i > 0) { |
| 140 |
buff.append(", "); |
| 141 |
} |
| 142 |
StringUtils.quoteIdentifier(buff, tableEngineParams.get(i)); |
| 143 |
} |
| 144 |
} |
| 145 |
if (!isPersistIndexes() && !isPersistData()) { |
| 146 |
buff.append("\nNOT PERSISTENT"); |
| 147 |
} |
| 148 |
if (isHidden) { |
| 149 |
buff.append("\nHIDDEN"); |
| 150 |
} |
| 151 |
return buff.toString(); |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public boolean isGlobalTemporary() { |
| 156 |
return globalTemporary; |
| 157 |
} |
| 158 |
|
| 159 |
} |