Search in sources :

Example 26 with SqlResult

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();
}
Also used : SqlResult(com.hazelcast.sql.SqlResult) Test(org.junit.Test)

Example 27 with SqlResult

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();
    }
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) SqlResult(com.hazelcast.sql.SqlResult) IOException(java.io.IOException) Test(org.junit.Test)

Example 28 with SqlResult

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;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SqlRow(com.hazelcast.sql.SqlRow) SqlStatement(com.hazelcast.sql.SqlStatement) SqlResult(com.hazelcast.sql.SqlResult) HashSet(java.util.HashSet)

Example 29 with SqlResult

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);
    }
}
Also used : SqlResult(com.hazelcast.sql.SqlResult) EndOfFileException(org.jline.reader.EndOfFileException) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) UserInterruptException(org.jline.reader.UserInterruptException) History(org.jline.reader.History) IOError(java.io.IOError) LineReader(org.jline.reader.LineReader) PrintWriter(java.io.PrintWriter)

Example 30 with SqlResult

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);
    }
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) SqlResult(com.hazelcast.sql.SqlResult) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) SqlRowMetadata(com.hazelcast.sql.SqlRowMetadata) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException) EndOfFileException(org.jline.reader.EndOfFileException) UserInterruptException(org.jline.reader.UserInterruptException) PrintWriter(java.io.PrintWriter) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException)

Aggregations

SqlResult (com.hazelcast.sql.SqlResult)60 Test (org.junit.Test)38 SqlRow (com.hazelcast.sql.SqlRow)31 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)23 QuickTest (com.hazelcast.test.annotation.QuickTest)23 SqlStatement (com.hazelcast.sql.SqlStatement)14 HazelcastInstance (com.hazelcast.core.HazelcastInstance)13 ArrayList (java.util.ArrayList)9 Job (com.hazelcast.jet.Job)8 HazelcastSqlException (com.hazelcast.sql.HazelcastSqlException)8 SqlService (com.hazelcast.sql.SqlService)8 JobConfig (com.hazelcast.jet.config.JobConfig)7 JetSqlRow (com.hazelcast.sql.impl.row.JetSqlRow)6 HashMap (java.util.HashMap)5 SqlRowMetadata (com.hazelcast.sql.SqlRowMetadata)4 List (java.util.List)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 TimeoutException (java.util.concurrent.TimeoutException)4 ClientConfig (com.hazelcast.client.config.ClientConfig)3 IndexConfig (com.hazelcast.config.IndexConfig)3