17 package org.apache.calcite.prepare;
19 import org.apache.calcite.sql.advise.SqlAdvisor;
20 import org.apache.calcite.sql.parser.SqlParser;
21 import org.apache.calcite.sql.validate.SqlMoniker;
22 import org.apache.calcite.sql.validate.SqlMonikerImpl;
23 import org.apache.calcite.sql.validate.SqlMonikerType;
25 import java.util.ArrayList;
26 import java.util.List;
31 super(validator, parserConfig);
32 this.permissionsAwareValidator = validator;
38 int wordStart = cursor;
39 boolean quoted =
false;
40 while (wordStart > 0 && Character.isJavaIdentifierPart(sql.charAt(wordStart - 1))) {
43 if ((wordStart > 0) && (sql.charAt(wordStart - 1) ==
'"')) {
49 return java.util.Collections.emptyList();
55 while (wordEnd < sql.length()
56 && Character.isJavaIdentifierPart(sql.charAt(wordEnd))) {
59 if (quoted && (wordEnd < sql.length()) && (sql.charAt(wordEnd) ==
'"')) {
65 String word = replaced[0] = sql.substring(wordStart, cursor);
66 if (wordStart < wordEnd) {
67 sql = sql.substring(0, wordStart) + sql.substring(wordEnd, sql.length());
72 List<SqlMoniker> completionHints =
76 return new ArrayList<>();
83 final List<SqlMoniker>
result;
84 if (word.length() > 0) {
85 result =
new java.util.ArrayList<SqlMoniker>();
88 word = word.substring(1);
89 for (SqlMoniker hint : completionHints) {
90 String cname = hint.toString();
91 if (cname.startsWith(word)) {
97 for (SqlMoniker hint : completionHints) {
98 String cname = hint.toString();
99 if ((cname.length() >= word.length())
100 && cname.substring(0, word.length()).equalsIgnoreCase(word)) {
106 result = completionHints;
113 final List<SqlMoniker> completionHints) {
114 List<SqlMoniker> strippedCompletionHints =
new ArrayList<>();
115 for (
final SqlMoniker hint : completionHints) {
116 if (hint.getType() == SqlMonikerType.TABLE
117 && hint.getFullyQualifiedNames().size() == 2) {
118 final String tableName = hint.getFullyQualifiedNames().
get(1);
119 strippedCompletionHints.add(
new SqlMonikerImpl(tableName, SqlMonikerType.TABLE));
121 strippedCompletionHints.add(hint);
124 return strippedCompletionHints;
128 final List<SqlMoniker> completionHints) {
129 List<SqlMoniker> completionHintsWithPermissions =
new ArrayList<>();
130 for (
final SqlMoniker hint : completionHints) {
131 if (hint.getType() == SqlMonikerType.TABLE) {
133 assert hint.getFullyQualifiedNames().size() == 1;
138 completionHintsWithPermissions.add(hint);
141 completionHintsWithPermissions.add(hint);
144 return completionHintsWithPermissions;
HeavyDBSqlAdvisor(HeavyDBSqlAdvisorValidator validator, SqlParser.Config parserConfig)
List< SqlMoniker > getCompletionHints(String sql, int cursor, String[] replaced)
List< SqlMoniker > applyPermissionsToTableHints(final List< SqlMoniker > completionHints)
HeavyDBSqlAdvisorValidator permissionsAwareValidator
boolean tableViolatesPermissions(final String tableName)
static List< SqlMoniker > stripDatabaseFromTableHints(final List< SqlMoniker > completionHints)
boolean hasViolatedTablePermissions()