mirror of
https://github.com/apache/zeppelin
synced 2026-05-24 09:38:26 +00:00
add multiple connections for interpreter
This commit is contained in:
parent
a06718cca5
commit
fe92f895f1
9 changed files with 207 additions and 586 deletions
|
|
@ -68,7 +68,10 @@ if [[ -d "${ZEPPELIN_HOME}/zeppelin-server/target/classes" ]]; then
|
|||
fi
|
||||
|
||||
# Add jdbc connector jar
|
||||
# ZEPPELIN_CLASSPATH+=":${ZEPPELIN_HOME}/jdbc connector jar"
|
||||
ZEPPELIN_CLASSPATH+=":${ZEPPELIN_HOME}/jdbc/jars/mysql-connector-java-5.1.35-bin.jar"
|
||||
ZEPPELIN_CLASSPATH+=":${ZEPPELIN_HOME}/jdbc/jars/postgresql-9.4-1205-jdbc41.jar"
|
||||
ZEPPELIN_CLASSPATH+=":${ZEPPELIN_HOME}/jdbc/jars/mariadb-java-client-1.2.3.jar"
|
||||
ZEPPELIN_CLASSPATH+=":${ZEPPELIN_HOME}/jdbc/jars/RedshiftJDBC41-1.1.10.1010.jar"
|
||||
|
||||
addJarInDir "${ZEPPELIN_HOME}"
|
||||
addJarInDir "${ZEPPELIN_HOME}/lib"
|
||||
|
|
|
|||
|
|
@ -105,7 +105,7 @@
|
|||
|
||||
<property>
|
||||
<name>zeppelin.interpreters</name>
|
||||
<value>org.apache.zeppelin.spark.SparkInterpreter,org.apache.zeppelin.spark.PySparkInterpreter,org.apache.zeppelin.spark.SparkSqlInterpreter,org.apache.zeppelin.spark.DepInterpreter,org.apache.zeppelin.markdown.Markdown,org.apache.zeppelin.angular.AngularInterpreter,org.apache.zeppelin.shell.ShellInterpreter,org.apache.zeppelin.hive.HiveInterpreter,org.apache.zeppelin.tajo.TajoInterpreter,org.apache.zeppelin.flink.FlinkInterpreter,org.apache.zeppelin.lens.LensInterpreter,org.apache.zeppelin.ignite.IgniteInterpreter,org.apache.zeppelin.ignite.IgniteSqlInterpreter,org.apache.zeppelin.cassandra.CassandraInterpreter,org.apache.zeppelin.geode.GeodeOqlInterpreter,org.apache.zeppelin.postgresql.PostgreSqlInterpreter,org.apache.zeppelin.phoenix.PhoenixInterpreter,org.apache.zeppelin.kylin.KylinInterpreter,org.apache.zeppelin.elasticsearch.ElasticsearchInterpreter,org.apache.zeppelin.scalding.ScaldingInterpreter</value>
|
||||
<value>org.apache.zeppelin.spark.SparkInterpreter,org.apache.zeppelin.spark.PySparkInterpreter,org.apache.zeppelin.spark.SparkSqlInterpreter,org.apache.zeppelin.spark.DepInterpreter,org.apache.zeppelin.markdown.Markdown,org.apache.zeppelin.angular.AngularInterpreter,org.apache.zeppelin.shell.ShellInterpreter,org.apache.zeppelin.hive.HiveInterpreter,org.apache.zeppelin.tajo.TajoInterpreter,org.apache.zeppelin.flink.FlinkInterpreter,org.apache.zeppelin.lens.LensInterpreter,org.apache.zeppelin.ignite.IgniteInterpreter,org.apache.zeppelin.ignite.IgniteSqlInterpreter,org.apache.zeppelin.cassandra.CassandraInterpreter,org.apache.zeppelin.geode.GeodeOqlInterpreter,org.apache.zeppelin.jdbc.JDBCInterpreter,org.apache.zeppelin.phoenix.PhoenixInterpreter,org.apache.zeppelin.kylin.KylinInterpreter,org.apache.zeppelin.elasticsearch.ElasticsearchInterpreter,org.apache.zeppelin.scalding.ScaldingInterpreter</value>
|
||||
<description>Comma separated interpreter configurations. First interpreter become a default</description>
|
||||
</property>
|
||||
|
||||
|
|
|
|||
|
|
@ -33,9 +33,6 @@
|
|||
<name>Zeppelin: JDBC interpreter</name>
|
||||
<url>http://www.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<postgresql.version>9.4-1201-jdbc41</postgresql.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
|
@ -46,12 +43,6 @@
|
|||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>${postgresql.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
|
|
|
|||
|
|
@ -24,7 +24,10 @@ import java.sql.ResultSetMetaData;
|
|||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
|
|
@ -70,49 +73,55 @@ import com.google.common.collect.Sets.SetView;
|
|||
public class JDBCInterpreter extends Interpreter {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(JDBCInterpreter.class);
|
||||
|
||||
static final String COMMON_KEY = "common";
|
||||
static final String MAX_LINE_KEY = "max_count";
|
||||
static final String MAX_LINE_DEFAULT = "1000";
|
||||
|
||||
private static final char WhITESPACE = ' ';
|
||||
static final String DEFAULT_KEY = "default";
|
||||
static final String DRIVER_KEY = "driver";
|
||||
static final String URL_KEY = "url";
|
||||
static final String USER_KEY = "user";
|
||||
static final String PASSWORD_KEY = "password";
|
||||
static final String DOT = ".";
|
||||
|
||||
private static final char WHITESPACE = ' ';
|
||||
private static final char NEWLINE = '\n';
|
||||
private static final char TAB = '\t';
|
||||
private static final String TABLE_MAGIC_TAG = "%table ";
|
||||
private static final String EXPLAIN_PREDICATE = "EXPLAIN ";
|
||||
private static final String UPDATE_COUNT_HEADER = "Update Count";
|
||||
|
||||
static final String DEFAULT_JDBC_URL = "jdbc:postgresql://localhost:5432/";
|
||||
static final String DEFAULT_JDBC_USER_PASSWORD = "";
|
||||
static final String DEFAULT_JDBC_USER_NAME = "gpadmin";
|
||||
static final String DEFAULT_JDBC_DRIVER_NAME = "org.postgresql.Driver";
|
||||
static final String DEFAULT_MAX_RESULT = "1000";
|
||||
static final String COMMON_MAX_LINE = COMMON_KEY + DOT + MAX_LINE_KEY;
|
||||
|
||||
static final String DEFAULT_DRIVER = DEFAULT_KEY + DOT + DRIVER_KEY;
|
||||
static final String DEFAULT_URL = DEFAULT_KEY + DOT + URL_KEY;
|
||||
static final String DEFAULT_USER = DEFAULT_KEY + DOT + USER_KEY;
|
||||
static final String DEFAULT_PASSWORD = DEFAULT_KEY + DOT + PASSWORD_KEY;
|
||||
|
||||
static final String JDBC_SERVER_URL = "jdbc.url";
|
||||
static final String JDBC_SERVER_USER = "jdbc.user";
|
||||
static final String JDBC_SERVER_PASSWORD = "jdbc.password";
|
||||
static final String JDBC_SERVER_DRIVER_NAME = "jdbc.driver.name";
|
||||
static final String JDBC_SERVER_MAX_RESULT = "jdbc.max.result";
|
||||
static final String EMPTY_COLUMN_VALUE = "";
|
||||
|
||||
private final HashMap<String, Properties> propertiesMap;
|
||||
private final Map<String, Statement> paragraphIdStatementMap;
|
||||
|
||||
private final Map<String, ArrayList<Connection>> propertyKeyUnusedConnectionListMap;
|
||||
private final Map<String, Connection> paragraphIdConnectionMap;
|
||||
|
||||
static {
|
||||
Interpreter.register(
|
||||
"sql",
|
||||
"jdbc",
|
||||
JDBCInterpreter.class.getName(),
|
||||
new InterpreterPropertyBuilder()
|
||||
.add(JDBC_SERVER_URL, DEFAULT_JDBC_URL, "The URL for JDBC.")
|
||||
.add(JDBC_SERVER_USER, DEFAULT_JDBC_USER_NAME, "The JDBC user name")
|
||||
.add(JDBC_SERVER_PASSWORD, DEFAULT_JDBC_USER_PASSWORD,
|
||||
.add(DEFAULT_URL, "jdbc:postgresql://localhost:5432/", "The URL for JDBC.")
|
||||
.add(DEFAULT_USER, "gpadmin", "The JDBC user name")
|
||||
.add(DEFAULT_PASSWORD, "",
|
||||
"The JDBC user password")
|
||||
.add(JDBC_SERVER_DRIVER_NAME, DEFAULT_JDBC_DRIVER_NAME, "JDBC Driver Name")
|
||||
.add(JDBC_SERVER_MAX_RESULT, DEFAULT_MAX_RESULT,
|
||||
.add(DEFAULT_DRIVER, "org.postgresql.Driver", "JDBC Driver Name")
|
||||
.add(COMMON_MAX_LINE, MAX_LINE_DEFAULT,
|
||||
"Max number of SQL result to display.").build());
|
||||
}
|
||||
|
||||
private Connection jdbcConnection;
|
||||
private Statement currentStatement;
|
||||
private Exception exceptionOnConnect;
|
||||
private int maxResult;
|
||||
|
||||
private SqlCompleter sqlCompleter;
|
||||
|
||||
private static final Function<CharSequence, String> sequenceToStringTransformer =
|
||||
new Function<CharSequence, String>() {
|
||||
public String apply(CharSequence seq) {
|
||||
|
|
@ -124,82 +133,147 @@ public class JDBCInterpreter extends Interpreter {
|
|||
|
||||
public JDBCInterpreter(Properties property) {
|
||||
super(property);
|
||||
propertiesMap = new HashMap<>();
|
||||
propertyKeyUnusedConnectionListMap = new HashMap<>();
|
||||
paragraphIdStatementMap = new HashMap<>();
|
||||
paragraphIdConnectionMap = new HashMap<>();
|
||||
}
|
||||
|
||||
public HashMap<String, Properties> getPropertiesMap() {
|
||||
return propertiesMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void open() {
|
||||
|
||||
logger.info("Open jdbc connection!");
|
||||
|
||||
// Ensure that no previous connections are left open.
|
||||
close();
|
||||
|
||||
try {
|
||||
String driverName = getProperty(JDBC_SERVER_DRIVER_NAME);
|
||||
String url = getProperty(JDBC_SERVER_URL);
|
||||
String user = getProperty(JDBC_SERVER_USER);
|
||||
String password = getProperty(JDBC_SERVER_PASSWORD);
|
||||
maxResult = Integer.valueOf(getProperty(JDBC_SERVER_MAX_RESULT));
|
||||
|
||||
Class.forName(driverName);
|
||||
|
||||
jdbcConnection = DriverManager.getConnection(url, user, password);
|
||||
|
||||
sqlCompleter = createSqlCompleter(jdbcConnection);
|
||||
|
||||
exceptionOnConnect = null;
|
||||
logger.info("Successfully created jdbc connection");
|
||||
|
||||
} catch (ClassNotFoundException | SQLException e) {
|
||||
logger.error("Cannot open connection", e);
|
||||
exceptionOnConnect = e;
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
private SqlCompleter createSqlCompleter(Connection jdbcConnection) {
|
||||
|
||||
SqlCompleter completer = null;
|
||||
try {
|
||||
Set<String> keywordsCompletions = SqlCompleter.getSqlKeywordsCompletions(jdbcConnection);
|
||||
Set<String> dataModelCompletions =
|
||||
SqlCompleter.getDataModelMetadataCompletions(jdbcConnection);
|
||||
SetView<String> allCompletions = Sets.union(keywordsCompletions, dataModelCompletions);
|
||||
completer = new SqlCompleter(allCompletions, dataModelCompletions);
|
||||
|
||||
} catch (IOException | SQLException e) {
|
||||
logger.error("Cannot create SQL completer", e);
|
||||
for (String propertyKey : property.stringPropertyNames()) {
|
||||
logger.debug("propertyKey: {}", propertyKey);
|
||||
String[] keyValue = propertyKey.split("\\.", 2);
|
||||
if (2 == keyValue.length) {
|
||||
logger.info("key: {}, value: {}", keyValue[0], keyValue[1]);
|
||||
Properties prefixProperties;
|
||||
if (propertiesMap.containsKey(keyValue[0])) {
|
||||
prefixProperties = propertiesMap.get(keyValue[0]);
|
||||
} else {
|
||||
prefixProperties = new Properties();
|
||||
propertiesMap.put(keyValue[0], prefixProperties);
|
||||
}
|
||||
prefixProperties.put(keyValue[1], property.getProperty(propertyKey));
|
||||
}
|
||||
}
|
||||
|
||||
return completer;
|
||||
Set<String> removeKeySet = new HashSet<>();
|
||||
for (String key : propertiesMap.keySet()) {
|
||||
if (!COMMON_KEY.equals(key)) {
|
||||
Properties properties = propertiesMap.get(key);
|
||||
if (!properties.containsKey(DRIVER_KEY) || !properties.containsKey(URL_KEY)) {
|
||||
logger.error("{} will be ignored. {}.{} and {}.{} is mandatory.",
|
||||
key, DRIVER_KEY, key, key, URL_KEY);
|
||||
removeKeySet.add(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (String key : removeKeySet) {
|
||||
propertiesMap.remove(key);
|
||||
}
|
||||
|
||||
logger.debug("propertiesMap: {}", propertiesMap);
|
||||
}
|
||||
|
||||
public Connection getConnection(String propertyKey) throws ClassNotFoundException, SQLException {
|
||||
Connection connection = null;
|
||||
if (propertyKeyUnusedConnectionListMap.containsKey(propertyKey)) {
|
||||
ArrayList<Connection> connectionList = propertyKeyUnusedConnectionListMap.get(propertyKey);
|
||||
if (0 != connectionList.size()) {
|
||||
connection = propertyKeyUnusedConnectionListMap.get(propertyKey).remove(0);
|
||||
if (null != connection && connection.isClosed()) {
|
||||
connection.close();
|
||||
connection = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (null == connection) {
|
||||
Properties properties = propertiesMap.get(propertyKey);
|
||||
logger.info(properties.getProperty(DRIVER_KEY));
|
||||
Class.forName(properties.getProperty(DRIVER_KEY));
|
||||
String url = properties.getProperty(URL_KEY);
|
||||
String user = properties.getProperty(USER_KEY);
|
||||
String password = properties.getProperty(PASSWORD_KEY);
|
||||
if (null != user && null != password) {
|
||||
connection = DriverManager.getConnection(url, user, password);
|
||||
} else {
|
||||
connection = DriverManager.getConnection(url, properties);
|
||||
}
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
public Statement getStatement(String propertyKey, String paragraphId)
|
||||
throws SQLException, ClassNotFoundException {
|
||||
Connection connection;
|
||||
if (paragraphIdConnectionMap.containsKey(paragraphId)) {
|
||||
// Never enter for now.
|
||||
connection = paragraphIdConnectionMap.get(paragraphId);
|
||||
} else {
|
||||
connection = getConnection(propertyKey);
|
||||
}
|
||||
|
||||
Statement statement = connection.createStatement();
|
||||
if (isStatementClosed(statement)) {
|
||||
connection = getConnection(propertyKey);
|
||||
statement = connection.createStatement();
|
||||
}
|
||||
paragraphIdConnectionMap.put(paragraphId, connection);
|
||||
paragraphIdStatementMap.put(paragraphId, statement);
|
||||
|
||||
return statement;
|
||||
}
|
||||
|
||||
private boolean isStatementClosed(Statement statement) {
|
||||
try {
|
||||
return statement.isClosed();
|
||||
} catch (Throwable t) {
|
||||
logger.debug("{} doesn't support isClosed method", statement);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
|
||||
logger.info("Close jdbc connection!");
|
||||
|
||||
try {
|
||||
if (getJdbcConnection() != null) {
|
||||
getJdbcConnection().close();
|
||||
for (List<Connection> connectionList : propertyKeyUnusedConnectionListMap.values()) {
|
||||
for (Connection c : connectionList) {
|
||||
c.close();
|
||||
}
|
||||
}
|
||||
|
||||
for (Statement statement : paragraphIdStatementMap.values()) {
|
||||
statement.close();
|
||||
}
|
||||
paragraphIdStatementMap.clear();
|
||||
|
||||
for (Connection connection : paragraphIdConnectionMap.values()) {
|
||||
connection.close();
|
||||
}
|
||||
paragraphIdConnectionMap.clear();
|
||||
|
||||
} catch (SQLException e) {
|
||||
logger.error("Cannot close connection", e);
|
||||
} finally {
|
||||
exceptionOnConnect = null;
|
||||
logger.error("Error while closing...", e);
|
||||
}
|
||||
}
|
||||
|
||||
private InterpreterResult executeSql(String sql) {
|
||||
private InterpreterResult executeSql(String propertyKey, String sql,
|
||||
InterpreterContext interpreterContext) {
|
||||
|
||||
String paragraphId = interpreterContext.getParagraphId();
|
||||
|
||||
try {
|
||||
|
||||
if (exceptionOnConnect != null) {
|
||||
return new InterpreterResult(Code.ERROR, exceptionOnConnect.getMessage());
|
||||
}
|
||||
|
||||
currentStatement = getJdbcConnection().createStatement();
|
||||
|
||||
currentStatement.setMaxRows(maxResult);
|
||||
Statement statement = getStatement(propertyKey, paragraphId);
|
||||
logger.info("antes");
|
||||
statement.setMaxRows(getMaxResult());
|
||||
logger.info("paso");
|
||||
|
||||
StringBuilder msg = null;
|
||||
boolean isTableType = false;
|
||||
|
|
@ -214,10 +288,10 @@ public class JDBCInterpreter extends Interpreter {
|
|||
ResultSet resultSet = null;
|
||||
try {
|
||||
|
||||
boolean isResultSetAvailable = currentStatement.execute(sql);
|
||||
boolean isResultSetAvailable = statement.execute(sql);
|
||||
|
||||
if (isResultSetAvailable) {
|
||||
resultSet = currentStatement.getResultSet();
|
||||
resultSet = statement.getResultSet();
|
||||
|
||||
ResultSetMetaData md = resultSet.getMetaData();
|
||||
|
||||
|
|
@ -242,24 +316,18 @@ public class JDBCInterpreter extends Interpreter {
|
|||
}
|
||||
} else {
|
||||
// Response contains either an update count or there are no results.
|
||||
int updateCount = currentStatement.getUpdateCount();
|
||||
int updateCount = statement.getUpdateCount();
|
||||
msg.append(UPDATE_COUNT_HEADER).append(NEWLINE);
|
||||
msg.append(updateCount).append(NEWLINE);
|
||||
|
||||
// In case of update event (e.g. isResultSetAvailable = false) update the completion
|
||||
// meta-data.
|
||||
if (sqlCompleter != null) {
|
||||
sqlCompleter.updateDataModelMetaData(getJdbcConnection());
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
if (resultSet != null) {
|
||||
resultSet.close();
|
||||
}
|
||||
currentStatement.close();
|
||||
statement.close();
|
||||
} finally {
|
||||
currentStatement = null;
|
||||
statement = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -268,6 +336,9 @@ public class JDBCInterpreter extends Interpreter {
|
|||
} catch (SQLException ex) {
|
||||
logger.error("Cannot run " + sql, ex);
|
||||
return new InterpreterResult(Code.ERROR, ex.getMessage());
|
||||
} catch (ClassNotFoundException e) {
|
||||
logger.error("Cannot run " + sql, e);
|
||||
return new InterpreterResult(Code.ERROR, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -278,13 +349,25 @@ public class JDBCInterpreter extends Interpreter {
|
|||
if (str == null) {
|
||||
return EMPTY_COLUMN_VALUE;
|
||||
}
|
||||
return (!isTableResponseType) ? str : str.replace(TAB, WhITESPACE).replace(NEWLINE, WhITESPACE);
|
||||
return (!isTableResponseType) ? str : str.replace(TAB, WHITESPACE).replace(NEWLINE, WHITESPACE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
|
||||
logger.info("Run SQL command '{}'", cmd);
|
||||
return executeSql(cmd);
|
||||
String propertyKey = getPropertyKey(cmd);
|
||||
|
||||
if (null != propertyKey) {
|
||||
cmd = cmd.substring(propertyKey.length() + 2);
|
||||
} else {
|
||||
propertyKey = DEFAULT_KEY;
|
||||
}
|
||||
|
||||
cmd = cmd.trim();
|
||||
|
||||
logger.info("PropertyKey: {}, SQL command: '{}'", propertyKey, cmd);
|
||||
|
||||
return executeSql(propertyKey, cmd, contextInterpreter);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -292,16 +375,28 @@ public class JDBCInterpreter extends Interpreter {
|
|||
|
||||
logger.info("Cancel current query statement.");
|
||||
|
||||
if (currentStatement != null) {
|
||||
try {
|
||||
currentStatement.cancel();
|
||||
} catch (SQLException ex) {
|
||||
} finally {
|
||||
currentStatement = null;
|
||||
}
|
||||
String paragraphId = context.getParagraphId();
|
||||
try {
|
||||
paragraphIdStatementMap.get(paragraphId).cancel();
|
||||
} catch (SQLException e) {
|
||||
logger.error("Error while cancelling...", e);
|
||||
}
|
||||
}
|
||||
|
||||
public String getPropertyKey(String cmd) {
|
||||
int firstLineIndex = cmd.indexOf("\n");
|
||||
if (-1 == firstLineIndex) {
|
||||
firstLineIndex = cmd.length();
|
||||
}
|
||||
int configStartIndex = cmd.indexOf("(");
|
||||
int configLastIndex = cmd.indexOf(")");
|
||||
if (configStartIndex != -1 && configLastIndex != -1
|
||||
&& configLastIndex < firstLineIndex && configLastIndex < firstLineIndex) {
|
||||
return cmd.substring(configStartIndex + 1, configLastIndex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FormType getFormType() {
|
||||
return FormType.SIMPLE;
|
||||
|
|
@ -320,21 +415,12 @@ public class JDBCInterpreter extends Interpreter {
|
|||
|
||||
@Override
|
||||
public List<String> completion(String buf, int cursor) {
|
||||
|
||||
List<CharSequence> candidates = new ArrayList<CharSequence>();
|
||||
if (sqlCompleter != null && sqlCompleter.complete(buf, cursor, candidates) >= 0) {
|
||||
return Lists.transform(candidates, sequenceToStringTransformer);
|
||||
} else {
|
||||
return NO_COMPLETION;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getMaxResult() {
|
||||
return maxResult;
|
||||
}
|
||||
|
||||
// Test only method
|
||||
protected Connection getJdbcConnection() {
|
||||
return jdbcConnection;
|
||||
return Integer.valueOf(
|
||||
propertiesMap.get(COMMON_KEY).getProperty(MAX_LINE_KEY, MAX_LINE_DEFAULT));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,250 +0,0 @@
|
|||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
|
||||
* agreements. See the NOTICE file distributed with this work for additional information regarding
|
||||
* copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the License. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
||||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
package org.apache.zeppelin.jdbc;
|
||||
|
||||
/*
|
||||
* This source file is based on code taken from SQLLine 1.0.2 See SQLLine notice in LICENSE
|
||||
*/
|
||||
import static org.apache.commons.lang.StringUtils.isBlank;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.TreeSet;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import jline.console.completer.ArgumentCompleter.ArgumentList;
|
||||
import jline.console.completer.ArgumentCompleter.WhitespaceArgumentDelimiter;
|
||||
import jline.console.completer.StringsCompleter;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.google.common.collect.Sets.SetView;
|
||||
|
||||
/**
|
||||
* SQL auto complete functionality for the PostgreSqlInterpreter.
|
||||
*/
|
||||
public class SqlCompleter extends StringsCompleter {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(SqlCompleter.class);
|
||||
|
||||
/**
|
||||
* Delimiter that can split SQL statement in keyword list
|
||||
*/
|
||||
private WhitespaceArgumentDelimiter sqlDelimiter = new WhitespaceArgumentDelimiter() {
|
||||
|
||||
private Pattern pattern = Pattern.compile("[\\.:;,]");
|
||||
|
||||
@Override
|
||||
public boolean isDelimiterChar(CharSequence buffer, int pos) {
|
||||
return pattern.matcher("" + buffer.charAt(pos)).matches()
|
||||
|| super.isDelimiterChar(buffer, pos);
|
||||
}
|
||||
};
|
||||
|
||||
private Set<String> modelCompletions = new HashSet<String>();
|
||||
|
||||
public SqlCompleter(Set<String> allCompletions, Set<String> dataModelCompletions) {
|
||||
super(allCompletions);
|
||||
this.modelCompletions = dataModelCompletions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int complete(String buffer, int cursor, List<CharSequence> candidates) {
|
||||
|
||||
if (isBlank(buffer) || (cursor > buffer.length() + 1)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// The delimiter breaks the buffer into separate words (arguments), separated by the
|
||||
// white spaces.
|
||||
ArgumentList argumentList = sqlDelimiter.delimit(buffer, cursor);
|
||||
String argument = argumentList.getCursorArgument();
|
||||
// cursor in the selected argument
|
||||
int argumentPosition = argumentList.getArgumentPosition();
|
||||
|
||||
if (isBlank(argument)) {
|
||||
int argumentsCount = argumentList.getArguments().length;
|
||||
if (argumentsCount <= 0 || ((buffer.length() + 2) < cursor)
|
||||
|| sqlDelimiter.isDelimiterChar(buffer, cursor - 2)) {
|
||||
return -1;
|
||||
}
|
||||
argument = argumentList.getArguments()[argumentsCount - 1];
|
||||
argumentPosition = argument.length();
|
||||
}
|
||||
|
||||
int complete = super.complete(argument, argumentPosition, candidates);
|
||||
|
||||
logger.debug("complete:" + complete + ", size:" + candidates.size());
|
||||
|
||||
return complete;
|
||||
}
|
||||
|
||||
public void updateDataModelMetaData(Connection connection) {
|
||||
|
||||
try {
|
||||
Set<String> newModelCompletions = getDataModelMetadataCompletions(connection);
|
||||
logger.debug("New model metadata is:" + Joiner.on(',').join(newModelCompletions));
|
||||
|
||||
// Sets.difference(set1, set2) - returned set contains all elements that are contained by set1
|
||||
// and not contained by set2. set2 may also contain elements not present in set1; these are
|
||||
// simply ignored.
|
||||
SetView<String> removedCompletions = Sets.difference(modelCompletions, newModelCompletions);
|
||||
logger.debug("Removed Model Completions: " + Joiner.on(',').join(removedCompletions));
|
||||
this.getStrings().removeAll(removedCompletions);
|
||||
|
||||
SetView<String> newCompletions = Sets.difference(newModelCompletions, modelCompletions);
|
||||
logger.debug("New Completions: " + Joiner.on(',').join(newCompletions));
|
||||
this.getStrings().addAll(newCompletions);
|
||||
|
||||
modelCompletions = newModelCompletions;
|
||||
|
||||
} catch (SQLException e) {
|
||||
logger.error("Failed to update the metadata conmpletions", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Set<String> getSqlKeywordsCompletions(Connection connection) throws IOException,
|
||||
SQLException {
|
||||
|
||||
// Add the default SQL completions
|
||||
String keywords =
|
||||
new BufferedReader(new InputStreamReader(
|
||||
SqlCompleter.class.getResourceAsStream("/ansi.sql.keywords"))).readLine();
|
||||
|
||||
DatabaseMetaData metaData = connection.getMetaData();
|
||||
|
||||
// Add the driver specific SQL completions
|
||||
String driverSpecificKeywords =
|
||||
"/" + metaData.getDriverName().replace(" ", "-").toLowerCase() + "-sql.keywords";
|
||||
|
||||
logger.info("JDBC DriverName:" + driverSpecificKeywords);
|
||||
|
||||
if (SqlCompleter.class.getResource(driverSpecificKeywords) != null) {
|
||||
String driverKeywords =
|
||||
new BufferedReader(new InputStreamReader(
|
||||
SqlCompleter.class.getResourceAsStream(driverSpecificKeywords))).readLine();
|
||||
keywords += "," + driverKeywords.toUpperCase();
|
||||
}
|
||||
|
||||
Set<String> completions = new TreeSet<String>();
|
||||
|
||||
|
||||
// Add the keywords from the current JDBC connection
|
||||
try {
|
||||
keywords += "," + metaData.getSQLKeywords();
|
||||
} catch (Exception e) {
|
||||
logger.debug("fail to get SQL key words from database metadata: " + e, e);
|
||||
}
|
||||
try {
|
||||
keywords += "," + metaData.getStringFunctions();
|
||||
} catch (Exception e) {
|
||||
logger.debug("fail to get string function names from database metadata: " + e, e);
|
||||
}
|
||||
try {
|
||||
keywords += "," + metaData.getNumericFunctions();
|
||||
} catch (Exception e) {
|
||||
logger.debug("fail to get numeric function names from database metadata: " + e, e);
|
||||
}
|
||||
try {
|
||||
keywords += "," + metaData.getSystemFunctions();
|
||||
} catch (Exception e) {
|
||||
logger.debug("fail to get system function names from database metadata: " + e, e);
|
||||
}
|
||||
try {
|
||||
keywords += "," + metaData.getTimeDateFunctions();
|
||||
} catch (Exception e) {
|
||||
logger.debug("fail to get time date function names from database metadata: " + e, e);
|
||||
}
|
||||
|
||||
// Also allow lower-case versions of all the keywords
|
||||
keywords += "," + keywords.toLowerCase();
|
||||
|
||||
StringTokenizer tok = new StringTokenizer(keywords, ", ");
|
||||
while (tok.hasMoreTokens()) {
|
||||
completions.add(tok.nextToken());
|
||||
}
|
||||
|
||||
return completions;
|
||||
}
|
||||
|
||||
public static Set<String> getDataModelMetadataCompletions(Connection connection)
|
||||
throws SQLException {
|
||||
Set<String> completions = new TreeSet<String>();
|
||||
getColumnNames(connection.getMetaData(), completions);
|
||||
getSchemaNames(connection.getMetaData(), completions);
|
||||
return completions;
|
||||
}
|
||||
|
||||
private static void getColumnNames(DatabaseMetaData meta, Set<String> names) throws SQLException {
|
||||
|
||||
try {
|
||||
ResultSet columns = meta.getColumns(meta.getConnection().getCatalog(), null, "%", "%");
|
||||
try {
|
||||
|
||||
while (columns.next()) {
|
||||
// Add the following strings: (1) column name, (2) table name
|
||||
String name = columns.getString("TABLE_NAME");
|
||||
if (!isBlank(name)) {
|
||||
names.add(name);
|
||||
names.add(columns.getString("COLUMN_NAME"));
|
||||
// names.add(columns.getString("TABLE_NAME") + "." + columns.getString("COLUMN_NAME"));
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
columns.close();
|
||||
}
|
||||
|
||||
logger.debug(Joiner.on(',').join(names));
|
||||
} catch (Throwable t) {
|
||||
logger.error("Failed to retrieve the column name", t);
|
||||
}
|
||||
}
|
||||
|
||||
private static void getSchemaNames(DatabaseMetaData meta, Set<String> names) throws SQLException {
|
||||
|
||||
try {
|
||||
ResultSet schemas = meta.getSchemas();
|
||||
try {
|
||||
while (schemas.next()) {
|
||||
String schemaName = schemas.getString("TABLE_SCHEM");
|
||||
if (!isBlank(schemaName)) {
|
||||
names.add(schemaName + ".");
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
schemas.close();
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
logger.error("Failed to retrieve the column name", t);
|
||||
}
|
||||
}
|
||||
|
||||
// test purpose only
|
||||
WhitespaceArgumentDelimiter getSqlDelimiter() {
|
||||
return this.sqlDelimiter;
|
||||
}
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
ABSOLUTE,ACTION,ADD,ALL,ALLOCATE,ALTER,AND,ANY,ARE,AS,ASC,ASSERTION,AT,AUTHORIZATION,AVG,BEGIN,BETWEEN,BIT,BIT_LENGTH,BOTH,BY,CASCADE,CASCADED,CASE,CAST,CATALOG,CHAR,CHARACTER,CHAR_LENGTH,CHARACTER_LENGTH,CHECK,CLOSE,CLUSTER,COALESCE,COLLATE,COLLATION,COLUMN,COMMIT,CONNECT,CONNECTION,CONSTRAINT,CONSTRAINTS,CONTINUE,CONVERT,CORRESPONDING,COUNT,CREATE,CROSS,CURRENT,CURRENT_DATE,CURRENT_TIME,CURRENT_TIMESTAMP,CURRENT_USER,CURSOR,DATE,DAY,DEALLOCATE,DEC,DECIMAL,DECLARE,DEFAULT,DEFERRABLE,DEFERRED,DELETE,DESC,DESCRIBE,DESCRIPTOR,DIAGNOSTICS,DISCONNECT,DISTINCT,DOMAIN,DOUBLE,DROP,ELSE,END,END-EXEC,ESCAPE,EXCEPT,EXCEPTION,EXEC,EXECUTE,EXISTS,EXTERNAL,EXTRACT,FALSE,FETCH,FIRST,FLOAT,FOR,FOREIGN,FOUND,FROM,FULL,GET,GLOBAL,GO,GOTO,GRANT,GROUP,HAVING,HOUR,IDENTITY,IMMEDIATE,IN,INDICATOR,INITIALLY,INNER,INPUT,INSENSITIVE,INSERT,INT,INTEGER,INTERSECT,INTERVAL,INTO,IS,ISOLATION,JOIN,KEY,LANGUAGE,LAST,LEADING,LEFT,LEVEL,LIKE,LOCAL,LOWER,MATCH,MAX,MIN,MINUTE,MODULE,MONTH,NAMES,NATIONAL,NATURAL,NCHAR,NEXT,NO,NOT,NULL,NULLIF,NUMERIC,OCTET_LENGTH,OF,ON,ONLY,OPEN,OPTION,OR,ORDER,OUTER,OUTPUT,OVERLAPS,OVERWRITE,PAD,PARTIAL,PARTITION,POSITION,PRECISION,PREPARE,PRESERVE,PRIMARY,PRIOR,PRIVILEGES,PROCEDURE,PUBLIC,READ,REAL,REFERENCES,RELATIVE,RESTRICT,REVOKE,RIGHT,ROLLBACK,ROWS,SCHEMA,SCROLL,SECOND,SECTION,SELECT,SESSION,SESSION_USER,SET,SIZE,SMALLINT,SOME,SPACE,SQL,SQLCODE,SQLERROR,SQLSTATE,SUBSTRING,SUM,SYSTEM_USER,TABLE,TEMPORARY,THEN,TIME,TIMESTAMP,TIMEZONE_HOUR,TIMEZONE_MINUTE,TO,TRAILING,TRANSACTION,TRANSLATE,TRANSLATION,TRIM,TRUE,UNION,UNIQUE,UNKNOWN,UPDATE,UPPER,USAGE,USER,USING,VALUE,VALUES,VARCHAR,VARYING,VIEW,WHEN,WHENEVER,WHERE,WITH,WORK,WRITE,YEAR,ZONE,ADA,C,CATALOG_NAME,CHARACTER_SET_CATALOG,CHARACTER_SET_NAME,CHARACTER_SET_SCHEMA,CLASS_ORIGIN,COBOL,COLLATION_CATALOG,COLLATION_NAME,COLLATION_SCHEMA,COLUMN_NAME,COMMAND_FUNCTION,COMMITTED,CONDITION_NUMBER,CONNECTION_NAME,CONSTRAINT_CATALOG,CONSTRAINT_NAME,CONSTRAINT_SCHEMA,CURSOR_NAME,DATA,DATETIME_INTERVAL_CODE,DATETIME_INTERVAL_PRECISION,DYNAMIC_FUNCTION,FORTRAN,LENGTH,MESSAGE_LENGTH,MESSAGE_OCTET_LENGTH,MESSAGE_TEXT,MORE,MUMPS,NAME,NULLABLE,NUMBER,PASCAL,PLI,REPEATABLE,RETURNED_LENGTH,RETURNED_OCTET_LENGTH,RETURNED_SQLSTATE,ROW_COUNT,SCALE,SCHEMA_NAME,SERIALIZABLE,SERVER_NAME,SUBCLASS_ORIGIN,TABLE_NAME,TYPE,UNCOMMITTED,UNNAMED,LIMIT
|
||||
File diff suppressed because one or more lines are too long
|
|
@ -14,16 +14,6 @@
|
|||
*/
|
||||
package org.apache.zeppelin.jdbc;
|
||||
|
||||
import static org.apache.zeppelin.jdbc.JDBCInterpreter.DEFAULT_JDBC_DRIVER_NAME;
|
||||
import static org.apache.zeppelin.jdbc.JDBCInterpreter.DEFAULT_JDBC_URL;
|
||||
import static org.apache.zeppelin.jdbc.JDBCInterpreter.DEFAULT_JDBC_USER_NAME;
|
||||
import static org.apache.zeppelin.jdbc.JDBCInterpreter.DEFAULT_JDBC_USER_PASSWORD;
|
||||
import static org.apache.zeppelin.jdbc.JDBCInterpreter.DEFAULT_MAX_RESULT;
|
||||
import static org.apache.zeppelin.jdbc.JDBCInterpreter.JDBC_SERVER_DRIVER_NAME;
|
||||
import static org.apache.zeppelin.jdbc.JDBCInterpreter.JDBC_SERVER_MAX_RESULT;
|
||||
import static org.apache.zeppelin.jdbc.JDBCInterpreter.JDBC_SERVER_PASSWORD;
|
||||
import static org.apache.zeppelin.jdbc.JDBCInterpreter.JDBC_SERVER_URL;
|
||||
import static org.apache.zeppelin.jdbc.JDBCInterpreter.JDBC_SERVER_USER;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
|
@ -36,7 +26,8 @@ import java.util.Properties;
|
|||
|
||||
import org.apache.zeppelin.interpreter.InterpreterResult;
|
||||
import org.apache.zeppelin.jdbc.JDBCInterpreter;
|
||||
import org.junit.Before;
|
||||
import org.junit.After;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mockrunner.jdbc.BasicJDBCTestCaseAdapter;
|
||||
|
|
@ -52,7 +43,7 @@ public class JDBCInterpreterTest extends BasicJDBCTestCaseAdapter {
|
|||
private JDBCInterpreter jdbcInterpreter = null;
|
||||
private MockResultSet result = null;
|
||||
|
||||
@Before
|
||||
@BeforeClass
|
||||
public void beforeTest() {
|
||||
MockConnection connection = getJDBCMockObjectFactory().getMockConnection();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,198 +0,0 @@
|
|||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
|
||||
* agreements. See the NOTICE file distributed with this work for additional information regarding
|
||||
* copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the License. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
||||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
package org.apache.zeppelin.jdbc;
|
||||
|
||||
import static com.google.common.collect.Sets.newHashSet;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import jline.console.completer.Completer;
|
||||
|
||||
import org.apache.zeppelin.jdbc.SqlCompleter;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.mockrunner.jdbc.BasicJDBCTestCaseAdapter;
|
||||
|
||||
public class SqlCompleterTest extends BasicJDBCTestCaseAdapter {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(SqlCompleterTest.class);
|
||||
|
||||
private final static Set<String> EMPTY = new HashSet<String>();
|
||||
|
||||
private CompleterTester tester;
|
||||
|
||||
private SqlCompleter sqlCompleter;
|
||||
|
||||
@Before
|
||||
public void beforeTest() throws IOException, SQLException {
|
||||
Set<String> keywordsCompletions =
|
||||
SqlCompleter.getSqlKeywordsCompletions(getJDBCMockObjectFactory().getMockConnection());
|
||||
Set<String> dataModelCompletions =
|
||||
SqlCompleter
|
||||
.getDataModelMetadataCompletions(getJDBCMockObjectFactory().getMockConnection());
|
||||
|
||||
sqlCompleter =
|
||||
new SqlCompleter(Sets.union(keywordsCompletions, dataModelCompletions),
|
||||
dataModelCompletions);
|
||||
tester = new CompleterTester(sqlCompleter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAfterBufferEnd() {
|
||||
String buffer = "ORDER";
|
||||
// Up to 2 white spaces after the buffer end, the completer still uses the last argument
|
||||
tester.buffer(buffer).from(0).to(buffer.length() + 1).expect(newHashSet("ORDER ")).test();
|
||||
// 2 white spaces or more behind the buffer end the completer returns empty result
|
||||
tester.buffer(buffer).from(buffer.length() + 2).to(buffer.length() + 5).expect(EMPTY).test();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEdges() {
|
||||
String buffer = " ORDER ";
|
||||
tester.buffer(buffer).from(0).to(8).expect(newHashSet("ORDER ")).test();
|
||||
tester.buffer(buffer).from(9).to(15).expect(EMPTY).test();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleWords() {
|
||||
String buffer = " SELE fro LIM";
|
||||
tester.buffer(buffer).from(0).to(6).expect(newHashSet("SELECT ")).test();
|
||||
tester.buffer(buffer).from(7).to(11).expect(newHashSet("from ")).test();
|
||||
tester.buffer(buffer).from(12).to(19).expect(newHashSet("LIMIT ")).test();
|
||||
tester.buffer(buffer).from(20).to(24).expect(EMPTY).test();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiLineBuffer() {
|
||||
String buffer = " \n SELE \n fro";
|
||||
tester.buffer(buffer).from(0).to(7).expect(newHashSet("SELECT ")).test();
|
||||
tester.buffer(buffer).from(8).to(14).expect(newHashSet("from ")).test();
|
||||
tester.buffer(buffer).from(15).to(17).expect(EMPTY).test();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleCompletionSuggestions() {
|
||||
String buffer = " SU";
|
||||
tester.buffer(buffer).from(0).to(5).expect(newHashSet("SUBCLASS_ORIGIN", "SUM", "SUBSTRING"))
|
||||
.test();
|
||||
tester.buffer(buffer).from(6).to(7).expect(EMPTY).test();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDotDelimiter() {
|
||||
String buffer = " order.select ";
|
||||
tester.buffer(buffer).from(4).to(7).expect(newHashSet("order ")).test();
|
||||
tester.buffer(buffer).from(8).to(15).expect(newHashSet("select ")).test();
|
||||
tester.buffer(buffer).from(16).to(17).expect(EMPTY).test();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSqlDelimiterCharacters() {
|
||||
assertTrue(sqlCompleter.getSqlDelimiter().isDelimiterChar("r.", 1));
|
||||
assertTrue(sqlCompleter.getSqlDelimiter().isDelimiterChar("SS;", 2));
|
||||
assertTrue(sqlCompleter.getSqlDelimiter().isDelimiterChar(":", 0));
|
||||
assertTrue(sqlCompleter.getSqlDelimiter().isDelimiterChar("ttt,", 3));
|
||||
}
|
||||
|
||||
public class CompleterTester {
|
||||
|
||||
private Completer completer;
|
||||
|
||||
private String buffer;
|
||||
private int fromCursor;
|
||||
private int toCursor;
|
||||
private Set<String> expectedCompletions;
|
||||
|
||||
public CompleterTester(Completer completer) {
|
||||
this.completer = completer;
|
||||
}
|
||||
|
||||
public CompleterTester buffer(String buffer) {
|
||||
this.buffer = buffer;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CompleterTester from(int fromCursor) {
|
||||
this.fromCursor = fromCursor;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CompleterTester to(int toCursor) {
|
||||
this.toCursor = toCursor;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CompleterTester expect(Set<String> expectedCompletions) {
|
||||
this.expectedCompletions = expectedCompletions;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void test() {
|
||||
for (int c = fromCursor; c <= toCursor; c++) {
|
||||
expectedCompletions(buffer, c, expectedCompletions);
|
||||
}
|
||||
}
|
||||
|
||||
private void expectedCompletions(String buffer, int cursor, Set<String> expected) {
|
||||
|
||||
ArrayList<CharSequence> candidates = new ArrayList<CharSequence>();
|
||||
|
||||
completer.complete(buffer, cursor, candidates);
|
||||
|
||||
String explain = explain(buffer, cursor, candidates);
|
||||
|
||||
logger.info(explain);
|
||||
|
||||
assertEquals("Buffer [" + buffer.replace(" ", ".") + "] and Cursor[" + cursor + "] "
|
||||
+ explain, expected, newHashSet(candidates));
|
||||
}
|
||||
|
||||
private String explain(String buffer, int cursor, ArrayList<CharSequence> candidates) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
||||
for (int i = 0; i <= Math.max(cursor, buffer.length()); i++) {
|
||||
if (i == cursor) {
|
||||
sb.append("(");
|
||||
}
|
||||
if (i >= buffer.length()) {
|
||||
sb.append("_");
|
||||
} else {
|
||||
if (Character.isWhitespace(buffer.charAt(i))) {
|
||||
sb.append(".");
|
||||
} else {
|
||||
sb.append(buffer.charAt(i));
|
||||
}
|
||||
}
|
||||
if (i == cursor) {
|
||||
sb.append(")");
|
||||
}
|
||||
}
|
||||
sb.append(" >> [").append(Joiner.on(",").join(candidates)).append("]");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue