ZEPPELIN-70: Add property to control the number of SQL results shown in the display.

This commit is contained in:
tzolov 2015-08-05 01:52:30 +02:00
parent 880bde0971
commit 886eab5c59
2 changed files with 50 additions and 6 deletions

View file

@ -43,6 +43,7 @@ import org.slf4j.LoggerFactory;
* <li>{@code postgresql.user} - JDBC user name..</li>
* <li>{@code postgresql.password} - JDBC password..</li>
* <li>{@code postgresql.driver.name} - JDBC driver name.</li>
* <li>{@code postgresql.max.result} - Max number of SQL result to display.</li>
* </ul>
*
* <p>
@ -70,11 +71,13 @@ public class PostgreSqlInterpreter extends Interpreter {
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 POSTGRESQL_SERVER_URL = "postgresql.url";
static final String POSTGRESQL_SERVER_USER = "postgresql.user";
static final String POSTGRESQL_SERVER_PASSWORD = "postgresql.password";
static final String POSTGRESQL_SERVER_DRIVER_NAME = "postgresql.driver.name";
static final String POSTGRESQL_SERVER_MAX_RESULT = "postgresql.max.result";
static {
Interpreter.register(
@ -87,12 +90,14 @@ public class PostgreSqlInterpreter extends Interpreter {
.add(POSTGRESQL_SERVER_PASSWORD, DEFAULT_JDBC_USER_PASSWORD,
"The PostgreSQL user password")
.add(POSTGRESQL_SERVER_DRIVER_NAME, DEFAULT_JDBC_DRIVER_NAME, "JDBC Driver Name")
.build());
.add(POSTGRESQL_SERVER_MAX_RESULT, DEFAULT_MAX_RESULT,
"Max number of SQL result to display.").build());
}
private Connection jdbcConnection;
private Statement currentStatement;
private Exception exceptionOnConnect;
private int maxResult;
public PostgreSqlInterpreter(Properties property) {
super(property);
@ -109,6 +114,7 @@ public class PostgreSqlInterpreter extends Interpreter {
String url = getProperty(POSTGRESQL_SERVER_URL);
String user = getProperty(POSTGRESQL_SERVER_USER);
String password = getProperty(POSTGRESQL_SERVER_PASSWORD);
maxResult = Integer.valueOf(getProperty(POSTGRESQL_SERVER_MAX_RESULT));
Class.forName(driverName);
@ -176,7 +182,8 @@ public class PostgreSqlInterpreter extends Interpreter {
}
msg.append(NEWLINE);
while (resultSet.next()) {
int displayRowCount = 0;
while (resultSet.next() && displayRowCount < getMaxResult()) {
for (int i = 1; i < md.getColumnCount() + 1; i++) {
msg.append(replaceReservedChars(isTableType, resultSet.getString(i)));
if (i != md.getColumnCount()) {
@ -184,6 +191,7 @@ public class PostgreSqlInterpreter extends Interpreter {
}
}
msg.append(NEWLINE);
displayRowCount++;
}
} else {
// Response contains either an update count or there are no results.
@ -256,6 +264,10 @@ public class PostgreSqlInterpreter extends Interpreter {
return null;
}
public int getMaxResult() {
return maxResult;
}
// Test only method
protected Connection getJdbcConnection() {
return jdbcConnection;

View file

@ -53,16 +53,19 @@ public class PostgreSqlInterpreterTest extends BasicJDBCTestCaseAdapter {
when(psqlInterpreter.getJdbcConnection()).thenReturn(connection);
}
@Test
public void testDefaultProperties() throws SQLException {
PostgreSqlInterpreter psqlInterpreter = new PostgreSqlInterpreter(new Properties());
PostgreSqlInterpreter psqlInterpreter = new PostgreSqlInterpreter(new Properties());
assertEquals(DEFAULT_JDBC_DRIVER_NAME, psqlInterpreter.getProperty(POSTGRESQL_SERVER_DRIVER_NAME));
assertEquals(DEFAULT_JDBC_DRIVER_NAME,
psqlInterpreter.getProperty(POSTGRESQL_SERVER_DRIVER_NAME));
assertEquals(DEFAULT_JDBC_URL, psqlInterpreter.getProperty(POSTGRESQL_SERVER_URL));
assertEquals(DEFAULT_JDBC_USER_NAME, psqlInterpreter.getProperty(POSTGRESQL_SERVER_USER));
assertEquals(DEFAULT_JDBC_USER_PASSWORD, psqlInterpreter.getProperty(POSTGRESQL_SERVER_PASSWORD));
assertEquals(DEFAULT_JDBC_USER_PASSWORD,
psqlInterpreter.getProperty(POSTGRESQL_SERVER_PASSWORD));
assertEquals(DEFAULT_MAX_RESULT, psqlInterpreter.getProperty(POSTGRESQL_SERVER_MAX_RESULT));
}
@Test
@ -99,6 +102,8 @@ public class PostgreSqlInterpreterTest extends BasicJDBCTestCaseAdapter {
@Test
public void testSelectQuery() throws SQLException {
when(psqlInterpreter.getMaxResult()).thenReturn(1000);
String sqlQuery = "select * from t";
result.addColumn("col1", new String[] {"val11", "val12"});
@ -115,9 +120,32 @@ public class PostgreSqlInterpreterTest extends BasicJDBCTestCaseAdapter {
verifyAllStatementsClosed();
}
@Test
public void testSelectQueryMaxResult() throws SQLException {
when(psqlInterpreter.getMaxResult()).thenReturn(1);
String sqlQuery = "select * from t";
result.addColumn("col1", new String[] {"val11", "val12"});
result.addColumn("col2", new String[] {"val21", "val22"});
InterpreterResult interpreterResult = psqlInterpreter.interpret(sqlQuery, null);
assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code());
assertEquals(InterpreterResult.Type.TABLE, interpreterResult.type());
assertEquals("col1\tcol2\nval11\tval21\n", interpreterResult.message());
verifySQLStatementExecuted(sqlQuery);
verifyAllResultSetsClosed();
verifyAllStatementsClosed();
}
@Test
public void testSelectQueryWithSpecialCharacters() throws SQLException {
when(psqlInterpreter.getMaxResult()).thenReturn(1000);
String sqlQuery = "select * from t";
result.addColumn("co\tl1", new String[] {"val11", "va\tl1\n2"});
@ -137,6 +165,8 @@ public class PostgreSqlInterpreterTest extends BasicJDBCTestCaseAdapter {
@Test
public void testExplainQuery() throws SQLException {
when(psqlInterpreter.getMaxResult()).thenReturn(1000);
String sqlQuery = "explain select * from t";
result.addColumn("col1", new String[] {"val11", "val12"});
@ -155,6 +185,8 @@ public class PostgreSqlInterpreterTest extends BasicJDBCTestCaseAdapter {
@Test
public void testExplainQueryWithSpecialCharachters() throws SQLException {
when(psqlInterpreter.getMaxResult()).thenReturn(1000);
String sqlQuery = "explain select * from t";
result.addColumn("co\tl\n1", new String[] {"va\nl11", "va\tl\n12"});