17 package org.apache.calcite.prepare;
19 import org.apache.calcite.sql.advise.SqlAdvisor;
20 import org.apache.calcite.sql.validate.SqlMoniker;
21 import org.apache.calcite.sql.validate.SqlMonikerImpl;
22 import org.apache.calcite.sql.validate.SqlMonikerType;
24 import java.util.ArrayList;
25 import java.util.List;
30 this.permissionsAwareValidator = validator;
36 int wordStart = cursor;
37 boolean quoted =
false;
38 while (wordStart > 0 && Character.isJavaIdentifierPart(sql.charAt(wordStart - 1))) {
41 if ((wordStart > 0) && (sql.charAt(wordStart - 1) ==
'"')) {
47 return java.util.Collections.emptyList();
53 while (wordEnd < sql.length()
54 && Character.isJavaIdentifierPart(sql.charAt(wordEnd))) {
57 if (quoted && (wordEnd < sql.length()) && (sql.charAt(wordEnd) ==
'"')) {
63 String word = replaced[0] = sql.substring(wordStart, cursor);
64 if (wordStart < wordEnd) {
65 sql = sql.substring(0, wordStart) + sql.substring(wordEnd, sql.length());
70 List<SqlMoniker> completionHints =
74 return new ArrayList<>();
81 final List<SqlMoniker>
result;
82 if (word.length() > 0) {
83 result =
new java.util.ArrayList<SqlMoniker>();
86 word = word.substring(1);
87 for (SqlMoniker hint : completionHints) {
88 String cname = hint.toString();
89 if (cname.startsWith(word)) {
95 for (SqlMoniker hint : completionHints) {
96 String cname = hint.toString();
97 if ((cname.length() >= word.length())
98 && cname.substring(0, word.length()).equalsIgnoreCase(word)) {
104 result = completionHints;
111 final List<SqlMoniker> completionHints) {
112 List<SqlMoniker> strippedCompletionHints =
new ArrayList<>();
113 for (
final SqlMoniker hint : completionHints) {
114 if (hint.getType() == SqlMonikerType.TABLE
115 && hint.getFullyQualifiedNames().size() == 2) {
116 final String tableName = hint.getFullyQualifiedNames().
get(1);
117 strippedCompletionHints.add(
new SqlMonikerImpl(tableName, SqlMonikerType.TABLE));
119 strippedCompletionHints.add(hint);
122 return strippedCompletionHints;
126 final List<SqlMoniker> completionHints) {
127 List<SqlMoniker> completionHintsWithPermissions =
new ArrayList<>();
128 for (
final SqlMoniker hint : completionHints) {
129 if (hint.getType() == SqlMonikerType.TABLE) {
131 assert hint.getFullyQualifiedNames().size() == 1;
136 completionHintsWithPermissions.add(hint);
139 completionHintsWithPermissions.add(hint);
142 return completionHintsWithPermissions;
MapDSqlAdvisor(MapDSqlAdvisorValidator validator)
static List< SqlMoniker > stripDatabaseFromTableHints(final List< SqlMoniker > completionHints)
boolean tableViolatesPermissions(final String tableName)
boolean hasViolatedTablePermissions()
int64_t const int32_t sz assert(dest)
List< SqlMoniker > applyPermissionsToTableHints(final List< SqlMoniker > completionHints)
MapDSqlAdvisorValidator permissionsAwareValidator
List< SqlMoniker > getCompletionHints(String sql, int cursor, String[] replaced)