| 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.bnf.context; |
| 7 |
|
| 8 |
import java.util.HashMap; |
| 9 |
import java.util.HashSet; |
| 10 |
|
| 11 |
import org.h2.bnf.Bnf; |
| 12 |
import org.h2.bnf.BnfVisitor; |
| 13 |
import org.h2.bnf.Rule; |
| 14 |
import org.h2.bnf.RuleElement; |
| 15 |
import org.h2.bnf.RuleHead; |
| 16 |
import org.h2.bnf.RuleList; |
| 17 |
import org.h2.bnf.Sentence; |
| 18 |
import org.h2.message.DbException; |
| 19 |
import org.h2.util.ParserUtil; |
| 20 |
import org.h2.util.StringUtils; |
| 21 |
|
| 22 |
/** |
| 23 |
* A BNF terminal rule that is linked to the database context information. |
| 24 |
* This class is used by the H2 Console, to support auto-complete. |
| 25 |
*/ |
| 26 |
public class DbContextRule implements Rule { |
| 27 |
|
| 28 |
public static final int COLUMN = 0, TABLE = 1, TABLE_ALIAS = 2; |
| 29 |
public static final int NEW_TABLE_ALIAS = 3; |
| 30 |
public static final int COLUMN_ALIAS = 4, SCHEMA = 5, PROCEDURE = 6; |
| 31 |
|
| 32 |
private final DbContents contents; |
| 33 |
private final int type; |
| 34 |
|
| 35 |
private String columnType; |
| 36 |
|
| 37 |
/** |
| 38 |
* BNF terminal rule Constructor |
| 39 |
* @param contents Extract rule from this component |
| 40 |
* @param type Rule type, one of |
| 41 |
* {@link DbContextRule#COLUMN}, |
| 42 |
* {@link DbContextRule#TABLE}, |
| 43 |
* {@link DbContextRule#TABLE_ALIAS}, |
| 44 |
* {@link DbContextRule#NEW_TABLE_ALIAS}, |
| 45 |
* {@link DbContextRule#COLUMN_ALIAS}, |
| 46 |
* {@link DbContextRule#SCHEMA} |
| 47 |
*/ |
| 48 |
public DbContextRule(DbContents contents, int type) { |
| 49 |
this.contents = contents; |
| 50 |
this.type = type; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @param columnType COLUMN Auto completion can be filtered by column type |
| 55 |
*/ |
| 56 |
public void setColumnType(String columnType) { |
| 57 |
this.columnType = columnType; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public void setLinks(HashMap<String, RuleHead> ruleMap) { |
| 62 |
// nothing to do |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public void accept(BnfVisitor visitor) { |
| 67 |
// nothing to do |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public boolean autoComplete(Sentence sentence) { |
| 72 |
String query = sentence.getQuery(), s = query; |
| 73 |
String up = sentence.getQueryUpper(); |
| 74 |
switch (type) { |
| 75 |
case SCHEMA: { |
| 76 |
DbSchema[] schemas = contents.getSchemas(); |
| 77 |
String best = null; |
| 78 |
DbSchema bestSchema = null; |
| 79 |
for (DbSchema schema: schemas) { |
| 80 |
String name = StringUtils.toUpperEnglish(schema.name); |
| 81 |
if (up.startsWith(name)) { |
| 82 |
if (best == null || name.length() > best.length()) { |
| 83 |
best = name; |
| 84 |
bestSchema = schema; |
| 85 |
} |
| 86 |
} else if (s.length() == 0 || name.startsWith(up)) { |
| 87 |
if (s.length() < name.length()) { |
| 88 |
sentence.add(name, name.substring(s.length()), type); |
| 89 |
sentence.add(schema.quotedName + ".", |
| 90 |
schema.quotedName.substring(s.length()) + ".", |
| 91 |
Sentence.CONTEXT); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
if (best != null) { |
| 96 |
sentence.setLastMatchedSchema(bestSchema); |
| 97 |
s = s.substring(best.length()); |
| 98 |
} |
| 99 |
break; |
| 100 |
} |
| 101 |
case TABLE: { |
| 102 |
DbSchema schema = sentence.getLastMatchedSchema(); |
| 103 |
if (schema == null) { |
| 104 |
schema = contents.getDefaultSchema(); |
| 105 |
} |
| 106 |
DbTableOrView[] tables = schema.getTables(); |
| 107 |
String best = null; |
| 108 |
DbTableOrView bestTable = null; |
| 109 |
for (DbTableOrView table : tables) { |
| 110 |
String compare = up; |
| 111 |
String name = StringUtils.toUpperEnglish(table.getName()); |
| 112 |
if (table.getQuotedName().length() > name.length()) { |
| 113 |
name = table.getQuotedName(); |
| 114 |
compare = query; |
| 115 |
} |
| 116 |
if (compare.startsWith(name)) { |
| 117 |
if (best == null || name.length() > best.length()) { |
| 118 |
best = name; |
| 119 |
bestTable = table; |
| 120 |
} |
| 121 |
} else if (s.length() == 0 || name.startsWith(compare)) { |
| 122 |
if (s.length() < name.length()) { |
| 123 |
sentence.add(table.getQuotedName(), |
| 124 |
table.getQuotedName().substring(s.length()), |
| 125 |
Sentence.CONTEXT); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
if (best != null) { |
| 130 |
sentence.setLastMatchedTable(bestTable); |
| 131 |
sentence.addTable(bestTable); |
| 132 |
s = s.substring(best.length()); |
| 133 |
} |
| 134 |
break; |
| 135 |
} |
| 136 |
case NEW_TABLE_ALIAS: |
| 137 |
s = autoCompleteTableAlias(sentence, true); |
| 138 |
break; |
| 139 |
case TABLE_ALIAS: |
| 140 |
s = autoCompleteTableAlias(sentence, false); |
| 141 |
break; |
| 142 |
case COLUMN_ALIAS: { |
| 143 |
int i = 0; |
| 144 |
if (query.indexOf(' ') < 0) { |
| 145 |
break; |
| 146 |
} |
| 147 |
for (; i < up.length(); i++) { |
| 148 |
char ch = up.charAt(i); |
| 149 |
if (ch != '_' && !Character.isLetterOrDigit(ch)) { |
| 150 |
break; |
| 151 |
} |
| 152 |
} |
| 153 |
if (i == 0) { |
| 154 |
break; |
| 155 |
} |
| 156 |
String alias = up.substring(0, i); |
| 157 |
if (ParserUtil.isKeyword(alias, false)) { |
| 158 |
break; |
| 159 |
} |
| 160 |
s = s.substring(alias.length()); |
| 161 |
break; |
| 162 |
} |
| 163 |
case COLUMN: { |
| 164 |
HashSet<DbTableOrView> set = sentence.getTables(); |
| 165 |
String best = null; |
| 166 |
DbTableOrView last = sentence.getLastMatchedTable(); |
| 167 |
if (last != null && last.getColumns() != null) { |
| 168 |
for (DbColumn column : last.getColumns()) { |
| 169 |
String compare = up; |
| 170 |
String name = StringUtils.toUpperEnglish(column.getName()); |
| 171 |
if (column.getQuotedName().length() > name.length()) { |
| 172 |
name = column.getQuotedName(); |
| 173 |
compare = query; |
| 174 |
} |
| 175 |
if (compare.startsWith(name) && testColumnType(column)) { |
| 176 |
String b = s.substring(name.length()); |
| 177 |
if (best == null || b.length() < best.length()) { |
| 178 |
best = b; |
| 179 |
} else if (s.length() == 0 || name.startsWith(compare)) { |
| 180 |
if (s.length() < name.length()) { |
| 181 |
sentence.add(column.getName(), |
| 182 |
column.getName().substring(s.length()), |
| 183 |
Sentence.CONTEXT); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
for (DbSchema schema : contents.getSchemas()) { |
| 190 |
for (DbTableOrView table : schema.getTables()) { |
| 191 |
if (table != last && set != null && !set.contains(table)) { |
| 192 |
continue; |
| 193 |
} |
| 194 |
if (table == null || table.getColumns() == null) { |
| 195 |
continue; |
| 196 |
} |
| 197 |
for (DbColumn column : table.getColumns()) { |
| 198 |
String name = StringUtils.toUpperEnglish(column |
| 199 |
.getName()); |
| 200 |
if (testColumnType(column)) { |
| 201 |
if (up.startsWith(name)) { |
| 202 |
String b = s.substring(name.length()); |
| 203 |
if (best == null || b.length() < best.length()) { |
| 204 |
best = b; |
| 205 |
} |
| 206 |
} else if (s.length() == 0 || name.startsWith(up)) { |
| 207 |
if (s.length() < name.length()) { |
| 208 |
sentence.add(column.getName(), |
| 209 |
column.getName().substring(s.length()), |
| 210 |
Sentence.CONTEXT); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
if (best != null) { |
| 218 |
s = best; |
| 219 |
} |
| 220 |
break; |
| 221 |
} |
| 222 |
case PROCEDURE: |
| 223 |
autoCompleteProcedure(sentence); |
| 224 |
break; |
| 225 |
default: |
| 226 |
throw DbException.getInternalError("type=" + type); |
| 227 |
} |
| 228 |
if (!s.equals(query)) { |
| 229 |
while (Bnf.startWithSpace(s)) { |
| 230 |
s = s.substring(1); |
| 231 |
} |
| 232 |
sentence.setQuery(s); |
| 233 |
return true; |
| 234 |
} |
| 235 |
return false; |
| 236 |
} |
| 237 |
|
| 238 |
private boolean testColumnType(DbColumn column) { |
| 239 |
if (columnType == null) { |
| 240 |
return true; |
| 241 |
} |
| 242 |
String type = column.getDataType(); |
| 243 |
if (columnType.contains("CHAR") || columnType.contains("CLOB")) { |
| 244 |
return type.contains("CHAR") || type.contains("CLOB"); |
| 245 |
} |
| 246 |
if (columnType.contains("BINARY") || columnType.contains("BLOB")) { |
| 247 |
return type.contains("BINARY") || type.contains("BLOB"); |
| 248 |
} |
| 249 |
return type.contains(columnType); |
| 250 |
} |
| 251 |
|
| 252 |
private void autoCompleteProcedure(Sentence sentence) { |
| 253 |
DbSchema schema = sentence.getLastMatchedSchema(); |
| 254 |
if (schema == null) { |
| 255 |
schema = contents.getDefaultSchema(); |
| 256 |
} |
| 257 |
String incompleteSentence = sentence.getQueryUpper(); |
| 258 |
String incompleteFunctionName = incompleteSentence; |
| 259 |
int bracketIndex = incompleteSentence.indexOf('('); |
| 260 |
if (bracketIndex != -1) { |
| 261 |
incompleteFunctionName = StringUtils.trimSubstring(incompleteSentence, 0, bracketIndex); |
| 262 |
} |
| 263 |
|
| 264 |
// Common elements |
| 265 |
RuleElement openBracket = new RuleElement("(", "Function"); |
| 266 |
RuleElement closeBracket = new RuleElement(")", "Function"); |
| 267 |
RuleElement comma = new RuleElement(",", "Function"); |
| 268 |
|
| 269 |
// Fetch all elements |
| 270 |
for (DbProcedure procedure : schema.getProcedures()) { |
| 271 |
final String procName = procedure.getName(); |
| 272 |
if (procName.startsWith(incompleteFunctionName)) { |
| 273 |
// That's it, build a RuleList from this function |
| 274 |
RuleElement procedureElement = new RuleElement(procName, |
| 275 |
"Function"); |
| 276 |
RuleList rl = new RuleList(procedureElement, openBracket, false); |
| 277 |
// Go further only if the user use open bracket |
| 278 |
if (incompleteSentence.contains("(")) { |
| 279 |
for (DbColumn parameter : procedure.getParameters()) { |
| 280 |
if (parameter.getPosition() > 1) { |
| 281 |
rl = new RuleList(rl, comma, false); |
| 282 |
} |
| 283 |
DbContextRule columnRule = new DbContextRule(contents, |
| 284 |
COLUMN); |
| 285 |
String parameterType = parameter.getDataType(); |
| 286 |
// Remove precision |
| 287 |
if (parameterType.contains("(")) { |
| 288 |
parameterType = parameterType.substring(0, |
| 289 |
parameterType.indexOf('(')); |
| 290 |
} |
| 291 |
columnRule.setColumnType(parameterType); |
| 292 |
rl = new RuleList(rl, columnRule, false); |
| 293 |
} |
| 294 |
rl = new RuleList(rl, closeBracket , false); |
| 295 |
} |
| 296 |
rl.autoComplete(sentence); |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
private static String autoCompleteTableAlias(Sentence sentence, |
| 302 |
boolean newAlias) { |
| 303 |
String s = sentence.getQuery(); |
| 304 |
String up = sentence.getQueryUpper(); |
| 305 |
int i = 0; |
| 306 |
for (; i < up.length(); i++) { |
| 307 |
char ch = up.charAt(i); |
| 308 |
if (ch != '_' && !Character.isLetterOrDigit(ch)) { |
| 309 |
break; |
| 310 |
} |
| 311 |
} |
| 312 |
if (i == 0) { |
| 313 |
return s; |
| 314 |
} |
| 315 |
String alias = up.substring(0, i); |
| 316 |
if ("SET".equals(alias) || ParserUtil.isKeyword(alias, false)) { |
| 317 |
return s; |
| 318 |
} |
| 319 |
if (newAlias) { |
| 320 |
sentence.addAlias(alias, sentence.getLastTable()); |
| 321 |
} |
| 322 |
HashMap<String, DbTableOrView> map = sentence.getAliases(); |
| 323 |
if ((map != null && map.containsKey(alias)) || |
| 324 |
(sentence.getLastTable() == null)) { |
| 325 |
if (newAlias && s.length() == alias.length()) { |
| 326 |
return s; |
| 327 |
} |
| 328 |
s = s.substring(alias.length()); |
| 329 |
if (s.length() == 0) { |
| 330 |
sentence.add(alias + ".", ".", Sentence.CONTEXT); |
| 331 |
} |
| 332 |
return s; |
| 333 |
} |
| 334 |
HashSet<DbTableOrView> tables = sentence.getTables(); |
| 335 |
if (tables != null) { |
| 336 |
String best = null; |
| 337 |
for (DbTableOrView table : tables) { |
| 338 |
String tableName = |
| 339 |
StringUtils.toUpperEnglish(table.getName()); |
| 340 |
if (alias.startsWith(tableName) && |
| 341 |
(best == null || tableName.length() > best.length())) { |
| 342 |
sentence.setLastMatchedTable(table); |
| 343 |
best = tableName; |
| 344 |
} else if (s.length() == 0 || tableName.startsWith(alias)) { |
| 345 |
sentence.add(tableName + ".", |
| 346 |
tableName.substring(s.length()) + ".", |
| 347 |
Sentence.CONTEXT); |
| 348 |
} |
| 349 |
} |
| 350 |
if (best != null) { |
| 351 |
s = s.substring(best.length()); |
| 352 |
if (s.length() == 0) { |
| 353 |
sentence.add(alias + ".", ".", Sentence.CONTEXT); |
| 354 |
} |
| 355 |
return s; |
| 356 |
} |
| 357 |
} |
| 358 |
return s; |
| 359 |
} |
| 360 |
|
| 361 |
} |