SqlCompleter { public int completeName(String buffer, int cursor, List<InterpreterCompletion> candidates, Map<String, String> aliases) { CursorArgument cursorArgument = parseCursorArgument(buffer, cursor); String schema; String table; String column; if (cursorArgument.getSchema() == null) { List<CharSequence> keywordsCandidates = new ArrayList(); List<CharSequence> schemaCandidates = new ArrayList<>(); int keywordsRes = completeKeyword(buffer, cursor, keywordsCandidates); int schemaRes = completeSchema(buffer, cursor, schemaCandidates); addCompletions(candidates, keywordsCandidates, CompletionType.keyword.name()); addCompletions(candidates, schemaCandidates, CompletionType.schema.name()); return NumberUtils.max(new int[]{keywordsRes, schemaRes}); } else { schema = cursorArgument.getSchema(); if (aliases.containsKey(schema)) { String alias = aliases.get(schema); int pointPos = alias.indexOf('.'); schema = alias.substring(0, pointPos); table = alias.substring(pointPos + 1); column = cursorArgument.getColumn(); List<CharSequence> columnCandidates = new ArrayList(); int columnRes = completeColumn(schema, table, column, cursorArgument.getCursorPosition(), columnCandidates); addCompletions(candidates, columnCandidates, CompletionType.column.name()); } else if (cursorArgument.getTable() != null && cursorArgument.getColumn() == null) { List<CharSequence> tableCandidates = new ArrayList(); table = cursorArgument.getTable(); int tableRes = completeTable(schema, table, cursorArgument.getCursorPosition(), tableCandidates); addCompletions(candidates, tableCandidates, CompletionType.table.name()); return tableRes; } else { List<CharSequence> columnCandidates = new ArrayList(); table = cursorArgument.getTable(); column = cursorArgument.getColumn(); int columnRes = completeColumn(schema, table, column, cursorArgument.getCursorPosition(), columnCandidates); addCompletions(candidates, columnCandidates, CompletionType.column.name()); } } return -1; } SqlCompleter(int ttlInSeconds); int complete(String buffer, int cursor, List<InterpreterCompletion> candidates); static Set<String> getSqlKeywordsCompletions(DatabaseMetaData meta); void createOrUpdateFromConnection(Connection connection, String schemaFiltersString,
      String buffer, int cursor); void initKeywords(Set<String> keywords); void initSchemas(Set<String> schemas); void initTables(String schema, Set<String> tables); void initColumns(String schemaTable, Set<String> columns); Map<String, String> findAliasesInSQL(String[] sqlArguments); int completeName(String buffer, int cursor, List<InterpreterCompletion> candidates,
                          Map<String, String> aliases);  }
@Test public void testCompleteName_Empty() { String buffer = ""; int cursor = 0; List<InterpreterCompletion> candidates = new ArrayList<>(); Map<String, String> aliases = new HashMap<>(); sqlCompleter.completeName(buffer, cursor, candidates, aliases); assertEquals(9, candidates.size()); assertTrue(candidates.contains(new InterpreterCompletion("prod_dds", "prod_dds", CompletionType.schema.name()))); assertTrue(candidates.contains(new InterpreterCompletion("prod_emart", "prod_emart", CompletionType.schema.name()))); assertTrue(candidates.contains(new InterpreterCompletion("SUM", "SUM", CompletionType.keyword.name()))); assertTrue(candidates.contains(new InterpreterCompletion("SUBSTRING", "SUBSTRING", CompletionType.keyword.name()))); assertTrue(candidates.contains(new InterpreterCompletion("SUBCLASS_ORIGIN", "SUBCLASS_ORIGIN", CompletionType.keyword.name()))); assertTrue(candidates.contains(new InterpreterCompletion("SELECT", "SELECT", CompletionType.keyword.name()))); assertTrue(candidates.contains(new InterpreterCompletion("ORDER", "ORDER", CompletionType.keyword.name()))); assertTrue(candidates.contains(new InterpreterCompletion("LIMIT", "LIMIT", CompletionType.keyword.name()))); assertTrue(candidates.contains(new InterpreterCompletion("FROM", "FROM", CompletionType.keyword.name()))); }
@Test public void testCompleteName_SimpleSchema() { String buffer = "prod_"; int cursor = 3; List<InterpreterCompletion> candidates = new ArrayList<>(); Map<String, String> aliases = new HashMap<>(); sqlCompleter.completeName(buffer, cursor, candidates, aliases); assertEquals(2, candidates.size()); assertTrue(candidates.contains(new InterpreterCompletion("prod_dds", "prod_dds", CompletionType.schema.name()))); assertTrue(candidates.contains(new InterpreterCompletion("prod_emart", "prod_emart", CompletionType.schema.name()))); }
@Test public void testCompleteName_SimpleTable() { String buffer = "prod_dds.fin"; int cursor = 11; List<InterpreterCompletion> candidates = new ArrayList<>(); Map<String, String> aliases = new HashMap<>(); sqlCompleter.completeName(buffer, cursor, candidates, aliases); assertEquals(1, candidates.size()); assertTrue(candidates.contains( new InterpreterCompletion("financial_account", "financial_account", CompletionType.table.name()))); }
@Test public void testCompleteName_SimpleColumn() { String buffer = "prod_dds.financial_account.acc"; int cursor = 30; List<InterpreterCompletion> candidates = new ArrayList<>(); Map<String, String> aliases = new HashMap<>(); sqlCompleter.completeName(buffer, cursor, candidates, aliases); assertEquals(2, candidates.size()); assertTrue(candidates.contains(new InterpreterCompletion("account_rk", "account_rk", CompletionType.column.name()))); assertTrue(candidates.contains(new InterpreterCompletion("account_id", "account_id", CompletionType.column.name()))); }
@Test public void testCompleteName_WithAlias() { String buffer = "a.acc"; int cursor = 4; List<InterpreterCompletion> candidates = new ArrayList<>(); Map<String, String> aliases = new HashMap<>(); aliases.put("a", "prod_dds.financial_account"); sqlCompleter.completeName(buffer, cursor, candidates, aliases); assertEquals(2, candidates.size()); assertTrue(candidates.contains(new InterpreterCompletion("account_rk", "account_rk", CompletionType.column.name()))); assertTrue(candidates.contains(new InterpreterCompletion("account_id", "account_id", CompletionType.column.name()))); }
@Test public void testCompleteName_WithAliasAndPoint() { String buffer = "a."; int cursor = 2; List<InterpreterCompletion> candidates = new ArrayList<>(); Map<String, String> aliases = new HashMap<>(); aliases.put("a", "prod_dds.financial_account"); sqlCompleter.completeName(buffer, cursor, candidates, aliases); assertEquals(2, candidates.size()); assertTrue(candidates.contains(new InterpreterCompletion("account_rk", "account_rk", CompletionType.column.name()))); assertTrue(candidates.contains(new InterpreterCompletion("account_id", "account_id", CompletionType.column.name()))); }