use of com.hazelcast.sql.SqlResult in project hazelcast by hazelcast.
the class SqlMappingTest method when_mappingIsDeclared_then_itIsAvailable.
@Test
public void when_mappingIsDeclared_then_itIsAvailable() {
// given
String name = randomName();
createMapping(name, Integer.class, String.class);
// when
SqlResult queryResult = sqlService.execute("SELECT __key, this FROM public." + name);
// then
assertThat(queryResult.updateCount()).isEqualTo(-1);
assertThat(queryResult.iterator()).isExhausted();
}
use of com.hazelcast.sql.SqlResult in project hazelcast by hazelcast.
the class SqlClientCursorCleanupTest method testExceptionOnExecute.
@Test
public void testExceptionOnExecute() {
IMap<Integer, Person> map = member.getMap(MAP_NAME);
map.put(0, new Person());
map.put(1, new Person());
fail = true;
try {
SqlResult result = client.getSql().execute(statement());
for (SqlRow ignore : result) {
// No-op.
}
fail("Must fail");
} catch (Exception e) {
assertNoState();
}
}
use of com.hazelcast.sql.SqlResult in project hazelcast by hazelcast.
the class SqlIndexAbstractTest method sqlKeys.
private Set<Integer> sqlKeys(boolean withIndex, String sql, List<Object> params) {
SqlStatement query = new SqlStatement(sql);
if (!params.isEmpty()) {
query.setParameters(params);
}
Set<Integer> keys = new HashSet<>();
try (SqlResult result = instance().getSql().execute(query)) {
for (SqlRow row : result) {
keys.add(row.getObject(0));
}
}
return keys;
}
use of com.hazelcast.sql.SqlResult in project hazelcast by hazelcast.
the class SqlConsole method run.
public static void run(HazelcastInstance hzClient) {
LineReader reader = LineReaderBuilder.builder().parser(new MultilineParser()).variable(LineReader.SECONDARY_PROMPT_PATTERN, new AttributedStringBuilder().style(AttributedStyle.BOLD.foreground(SECONDARY_COLOR)).append("%M%P > ").toAnsi()).variable(LineReader.INDENTATION, 2).option(LineReader.Option.DISABLE_EVENT_EXPANSION, true).appName("hazelcast-sql").build();
AtomicReference<SqlResult> activeSqlResult = new AtomicReference<>();
reader.getTerminal().handle(Terminal.Signal.INT, signal -> {
SqlResult r = activeSqlResult.get();
if (r != null) {
r.close();
}
});
PrintWriter writer = reader.getTerminal().writer();
writer.println(sqlStartingPrompt(hzClient));
writer.flush();
for (; ; ) {
String command;
try {
command = reader.readLine(new AttributedStringBuilder().style(AttributedStyle.DEFAULT.foreground(SECONDARY_COLOR)).append("sql> ").toAnsi()).trim();
} catch (EndOfFileException | IOError e) {
// Ctrl+D, and kill signals result in exit
writer.println(Constants.EXIT_PROMPT);
writer.flush();
break;
} catch (UserInterruptException e) {
// Ctrl+C cancels the not-yet-submitted query
continue;
}
command = command.trim();
if (command.length() > 0 && command.charAt(command.length() - 1) == ';') {
command = command.substring(0, command.length() - 1).trim();
} else if (command.lastIndexOf(";") >= 0) {
String errorPrompt = new AttributedStringBuilder().style(AttributedStyle.BOLD.foreground(PRIMARY_COLOR)).append("There are non-whitespace characters after the semicolon").toAnsi();
writer.println(errorPrompt);
writer.flush();
continue;
}
if ("".equals(command)) {
continue;
}
if (equalsIgnoreCase("clear", command)) {
reader.getTerminal().puts(InfoCmp.Capability.clear_screen);
continue;
}
if (equalsIgnoreCase("help", command)) {
writer.println(helpPrompt());
writer.flush();
continue;
}
if (equalsIgnoreCase("history", command)) {
History hist = reader.getHistory();
ListIterator<History.Entry> iterator = hist.iterator();
while (iterator.hasNext()) {
History.Entry entry = iterator.next();
if (iterator.hasNext()) {
String entryLine = new AttributedStringBuilder().style(AttributedStyle.BOLD.foreground(PRIMARY_COLOR)).append(String.valueOf(entry.index() + 1)).append(" - ").append(entry.line()).toAnsi();
writer.println(entryLine);
writer.flush();
} else {
// remove the "history" command from the history
iterator.remove();
hist.resetIndex();
}
}
continue;
}
if (equalsIgnoreCase("exit", command)) {
writer.println(Constants.EXIT_PROMPT);
writer.flush();
break;
}
executeSqlCmd(hzClient, command, reader.getTerminal(), activeSqlResult);
}
}
use of com.hazelcast.sql.SqlResult in project hazelcast by hazelcast.
the class SqlConsole method executeSqlCmd.
private static void executeSqlCmd(HazelcastInstance hz, String command, Terminal terminal, AtomicReference<SqlResult> activeSqlResult) {
PrintWriter out = terminal.writer();
try (SqlResult sqlResult = hz.getSql().execute(command)) {
activeSqlResult.set(sqlResult);
// if it's a result with an update count, just print it
if (sqlResult.updateCount() != -1) {
String message = new AttributedStringBuilder().style(AttributedStyle.BOLD.foreground(PRIMARY_COLOR)).append("OK").toAnsi();
out.println(message);
return;
}
SqlRowMetadata rowMetadata = sqlResult.getRowMetadata();
int[] colWidths = determineColumnWidths(rowMetadata);
Alignment[] alignments = determineAlignments(rowMetadata);
// this is a result with rows. Print the header and rows, watch for concurrent cancellation
printMetadataInfo(rowMetadata, colWidths, alignments, out);
int rowCount = 0;
for (SqlRow row : sqlResult) {
rowCount++;
printRow(row, colWidths, alignments, out);
}
// bottom line after all the rows
printSeparatorLine(sqlResult.getRowMetadata().getColumnCount(), colWidths, out);
String message = new AttributedStringBuilder().style(AttributedStyle.BOLD.foreground(PRIMARY_COLOR)).append(String.valueOf(rowCount)).append(" row(s) selected").toAnsi();
out.println(message);
} catch (HazelcastSqlException e) {
// the query failed to execute with HazelcastSqlException
String errorPrompt = new AttributedStringBuilder().style(AttributedStyle.BOLD.foreground(PRIMARY_COLOR)).append(e.getMessage()).toAnsi();
out.println(errorPrompt);
} catch (Exception e) {
// the query failed to execute with an unexpected exception
String unexpectedErrorPrompt = new AttributedStringBuilder().style(AttributedStyle.BOLD.foreground(PRIMARY_COLOR)).append("Encountered an unexpected exception while executing the query:\n").append(e.getMessage()).toAnsi();
out.println(unexpectedErrorPrompt);
e.printStackTrace(out);
}
}
Aggregations