| 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.index; |
| 7 |
|
| 8 |
import java.sql.PreparedStatement; |
| 9 |
import java.sql.ResultSet; |
| 10 |
import java.util.ArrayList; |
| 11 |
|
| 12 |
import org.h2.command.query.AllColumnsForPlan; |
| 13 |
import org.h2.engine.Constants; |
| 14 |
import org.h2.engine.SessionLocal; |
| 15 |
import org.h2.message.DbException; |
| 16 |
import org.h2.result.Row; |
| 17 |
import org.h2.result.SearchRow; |
| 18 |
import org.h2.result.SortOrder; |
| 19 |
import org.h2.table.Column; |
| 20 |
import org.h2.table.IndexColumn; |
| 21 |
import org.h2.table.TableFilter; |
| 22 |
import org.h2.table.TableLink; |
| 23 |
import org.h2.util.Utils; |
| 24 |
import org.h2.value.TypeInfo; |
| 25 |
import org.h2.value.Value; |
| 26 |
import org.h2.value.ValueNull; |
| 27 |
|
| 28 |
/** |
| 29 |
* A linked index is a index for a linked (remote) table. |
| 30 |
* It is backed by an index on the remote table which is accessed over JDBC. |
| 31 |
*/ |
| 32 |
public class LinkedIndex extends Index { |
| 33 |
|
| 34 |
private final TableLink link; |
| 35 |
private final String targetTableName; |
| 36 |
private long rowCount; |
| 37 |
|
| 38 |
private final int sqlFlags = QUOTE_ONLY_WHEN_REQUIRED; |
| 39 |
|
| 40 |
public LinkedIndex(TableLink table, int id, IndexColumn[] columns, int uniqueColumnCount, IndexType indexType) { |
| 41 |
super(table, id, null, columns, uniqueColumnCount, indexType); |
| 42 |
link = table; |
| 43 |
targetTableName = link.getQualifiedTable(); |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public String getCreateSQL() { |
| 48 |
return null; |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public void close(SessionLocal session) { |
| 53 |
// nothing to do |
| 54 |
} |
| 55 |
|
| 56 |
private static boolean isNull(Value v) { |
| 57 |
return v == null || v == ValueNull.INSTANCE; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public void add(SessionLocal session, Row row) { |
| 62 |
ArrayList<Value> params = Utils.newSmallArrayList(); |
| 63 |
StringBuilder buff = new StringBuilder("INSERT INTO "); |
| 64 |
buff.append(targetTableName).append(" VALUES("); |
| 65 |
for (int i = 0; i < row.getColumnCount(); i++) { |
| 66 |
Value v = row.getValue(i); |
| 67 |
if (i > 0) { |
| 68 |
buff.append(", "); |
| 69 |
} |
| 70 |
if (v == null) { |
| 71 |
buff.append("DEFAULT"); |
| 72 |
} else if (isNull(v)) { |
| 73 |
buff.append("NULL"); |
| 74 |
} else { |
| 75 |
buff.append('?'); |
| 76 |
params.add(v); |
| 77 |
} |
| 78 |
} |
| 79 |
buff.append(')'); |
| 80 |
String sql = buff.toString(); |
| 81 |
try { |
| 82 |
link.execute(sql, params, true, session); |
| 83 |
rowCount++; |
| 84 |
} catch (Exception e) { |
| 85 |
throw TableLink.wrapException(sql, e); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public Cursor find(SessionLocal session, SearchRow first, SearchRow last) { |
| 91 |
ArrayList<Value> params = Utils.newSmallArrayList(); |
| 92 |
StringBuilder builder = new StringBuilder("SELECT * FROM ").append(targetTableName).append(" T"); |
| 93 |
boolean f = false; |
| 94 |
for (int i = 0; first != null && i < first.getColumnCount(); i++) { |
| 95 |
Value v = first.getValue(i); |
| 96 |
if (v != null) { |
| 97 |
builder.append(f ? " AND " : " WHERE "); |
| 98 |
f = true; |
| 99 |
Column col = table.getColumn(i); |
| 100 |
addColumnName(builder, col); |
| 101 |
if (v == ValueNull.INSTANCE) { |
| 102 |
builder.append(" IS NULL"); |
| 103 |
} else { |
| 104 |
builder.append(">="); |
| 105 |
addParameter(builder, col); |
| 106 |
params.add(v); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
for (int i = 0; last != null && i < last.getColumnCount(); i++) { |
| 111 |
Value v = last.getValue(i); |
| 112 |
if (v != null) { |
| 113 |
builder.append(f ? " AND " : " WHERE "); |
| 114 |
f = true; |
| 115 |
Column col = table.getColumn(i); |
| 116 |
addColumnName(builder, col); |
| 117 |
if (v == ValueNull.INSTANCE) { |
| 118 |
builder.append(" IS NULL"); |
| 119 |
} else { |
| 120 |
builder.append("<="); |
| 121 |
addParameter(builder, col); |
| 122 |
params.add(v); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
String sql = builder.toString(); |
| 127 |
try { |
| 128 |
PreparedStatement prep = link.execute(sql, params, false, session); |
| 129 |
ResultSet rs = prep.getResultSet(); |
| 130 |
return new LinkedCursor(link, rs, session, sql, prep); |
| 131 |
} catch (Exception e) { |
| 132 |
throw TableLink.wrapException(sql, e); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
private void addColumnName(StringBuilder builder, Column col) { |
| 137 |
String identifierQuoteString = link.getIdentifierQuoteString(); |
| 138 |
String name = col.getName(); |
| 139 |
if (identifierQuoteString == null || identifierQuoteString.isEmpty() || identifierQuoteString.equals(" ")) { |
| 140 |
builder.append(name); |
| 141 |
} else if (identifierQuoteString.equals("\"")) { |
| 142 |
/* |
| 143 |
* StringUtils.quoteIdentifier() can produce Unicode identifiers, |
| 144 |
* but target DBMS isn't required to support them |
| 145 |
*/ |
| 146 |
builder.append('"'); |
| 147 |
int i = name.indexOf('"'); |
| 148 |
if (i < 0) { |
| 149 |
builder.append(name); |
| 150 |
} else { |
| 151 |
builder.append(name, 0, ++i).append('"'); |
| 152 |
for (int l = name.length(); i < l; i++) { |
| 153 |
char c = name.charAt(i); |
| 154 |
if (c == '"') { |
| 155 |
builder.append('"'); |
| 156 |
} |
| 157 |
builder.append(c); |
| 158 |
} |
| 159 |
} |
| 160 |
builder.append('"'); |
| 161 |
} else { |
| 162 |
builder.append(identifierQuoteString).append(name).append(identifierQuoteString); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
private void addParameter(StringBuilder builder, Column col) { |
| 167 |
TypeInfo type = col.getType(); |
| 168 |
if (type.getValueType() == Value.CHAR && link.isOracle()) { |
| 169 |
// workaround for Oracle |
| 170 |
// create table test(id int primary key, name char(15)); |
| 171 |
// insert into test values(1, 'Hello') |
| 172 |
// select * from test where name = ? -- where ? = "Hello" > no rows |
| 173 |
builder.append("CAST(? AS CHAR(").append(type.getPrecision()).append("))"); |
| 174 |
} else { |
| 175 |
builder.append('?'); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
@Override |
| 180 |
public double getCost(SessionLocal session, int[] masks, |
| 181 |
TableFilter[] filters, int filter, SortOrder sortOrder, |
| 182 |
AllColumnsForPlan allColumnsSet) { |
| 183 |
return 100 + getCostRangeIndex(masks, rowCount + |
| 184 |
Constants.COST_ROW_OFFSET, filters, filter, sortOrder, false, allColumnsSet); |
| 185 |
} |
| 186 |
|
| 187 |
@Override |
| 188 |
public void remove(SessionLocal session) { |
| 189 |
// nothing to do |
| 190 |
} |
| 191 |
|
| 192 |
@Override |
| 193 |
public void truncate(SessionLocal session) { |
| 194 |
// nothing to do |
| 195 |
} |
| 196 |
|
| 197 |
@Override |
| 198 |
public void checkRename() { |
| 199 |
throw DbException.getUnsupportedException("LINKED"); |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public boolean needRebuild() { |
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
@Override |
| 208 |
public void remove(SessionLocal session, Row row) { |
| 209 |
ArrayList<Value> params = Utils.newSmallArrayList(); |
| 210 |
StringBuilder builder = new StringBuilder("DELETE FROM ").append(targetTableName).append(" WHERE "); |
| 211 |
for (int i = 0; i < row.getColumnCount(); i++) { |
| 212 |
if (i > 0) { |
| 213 |
builder.append("AND "); |
| 214 |
} |
| 215 |
Column col = table.getColumn(i); |
| 216 |
addColumnName(builder, col); |
| 217 |
Value v = row.getValue(i); |
| 218 |
if (isNull(v)) { |
| 219 |
builder.append(" IS NULL "); |
| 220 |
} else { |
| 221 |
builder.append('='); |
| 222 |
addParameter(builder, col); |
| 223 |
params.add(v); |
| 224 |
builder.append(' '); |
| 225 |
} |
| 226 |
} |
| 227 |
String sql = builder.toString(); |
| 228 |
try { |
| 229 |
PreparedStatement prep = link.execute(sql, params, false, session); |
| 230 |
int count = prep.executeUpdate(); |
| 231 |
link.reusePreparedStatement(prep, sql); |
| 232 |
rowCount -= count; |
| 233 |
} catch (Exception e) { |
| 234 |
throw TableLink.wrapException(sql, e); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Update a row using a UPDATE statement. This method is to be called if the |
| 240 |
* emit updates option is enabled. |
| 241 |
* |
| 242 |
* @param oldRow the old data |
| 243 |
* @param newRow the new data |
| 244 |
* @param session the session |
| 245 |
*/ |
| 246 |
public void update(Row oldRow, Row newRow, SessionLocal session) { |
| 247 |
ArrayList<Value> params = Utils.newSmallArrayList(); |
| 248 |
StringBuilder builder = new StringBuilder("UPDATE ").append(targetTableName).append(" SET "); |
| 249 |
for (int i = 0; i < newRow.getColumnCount(); i++) { |
| 250 |
if (i > 0) { |
| 251 |
builder.append(", "); |
| 252 |
} |
| 253 |
table.getColumn(i).getSQL(builder, sqlFlags).append('='); |
| 254 |
Value v = newRow.getValue(i); |
| 255 |
if (v == null) { |
| 256 |
builder.append("DEFAULT"); |
| 257 |
} else { |
| 258 |
builder.append('?'); |
| 259 |
params.add(v); |
| 260 |
} |
| 261 |
} |
| 262 |
builder.append(" WHERE "); |
| 263 |
for (int i = 0; i < oldRow.getColumnCount(); i++) { |
| 264 |
Column col = table.getColumn(i); |
| 265 |
if (i > 0) { |
| 266 |
builder.append(" AND "); |
| 267 |
} |
| 268 |
addColumnName(builder, col); |
| 269 |
Value v = oldRow.getValue(i); |
| 270 |
if (isNull(v)) { |
| 271 |
builder.append(" IS NULL"); |
| 272 |
} else { |
| 273 |
builder.append('='); |
| 274 |
params.add(v); |
| 275 |
addParameter(builder, col); |
| 276 |
} |
| 277 |
} |
| 278 |
String sql = builder.toString(); |
| 279 |
try { |
| 280 |
link.execute(sql, params, true, session); |
| 281 |
} catch (Exception e) { |
| 282 |
throw TableLink.wrapException(sql, e); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
@Override |
| 287 |
public long getRowCount(SessionLocal session) { |
| 288 |
return rowCount; |
| 289 |
} |
| 290 |
|
| 291 |
@Override |
| 292 |
public long getRowCountApproximation(SessionLocal session) { |
| 293 |
return rowCount; |
| 294 |
} |
| 295 |
|
| 296 |
} |