Search in sources :

Example 1 with OutputContainer

use of com.twosigma.beakerx.jvm.object.OutputContainer in project beakerx by twosigma.

the class OutputContainerSerializerTest method setUp.

@Before
public void setUp() throws Exception {
    KernelManager.register(new KernelTest());
    outputContainer = new OutputContainer();
}
Also used : KernelTest(com.twosigma.beakerx.KernelTest) OutputContainer(com.twosigma.beakerx.jvm.object.OutputContainer) Before(org.junit.Before)

Example 2 with OutputContainer

use of com.twosigma.beakerx.jvm.object.OutputContainer in project beakerx by twosigma.

the class OutputContainerDeserializerTest method deserialize_resultObjectHasItems.

@Test
public void deserialize_resultObjectHasItems() throws Exception {
    // given
    ObjectMapper mapper = new ObjectMapper();
    JsonNode actualObj = mapper.readTree(json);
    OutputContainerDeserializer deserializer = new OutputContainerDeserializer(new BasicObjectSerializer());
    // when
    OutputContainer outputContainer = (OutputContainer) deserializer.deserialize(actualObj, mapper);
    // then
    Assertions.assertThat(outputContainer).isNotNull();
    Assertions.assertThat(outputContainer.getItems()).isNotEmpty();
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) OutputContainer(com.twosigma.beakerx.jvm.object.OutputContainer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with OutputContainer

use of com.twosigma.beakerx.jvm.object.OutputContainer in project beakerx by twosigma.

the class QueryExecutor method executeQuery.

public synchronized Object executeQuery(String script, NamespaceClient namespaceClient, ConnectionStringHolder defaultConnectionString, Map<String, ConnectionStringHolder> namedConnectionString) throws SQLException, IOException, ReadVariableException {
    BeakerParser beakerParser = new BeakerParser(script, namespaceClient, defaultConnectionString, namedConnectionString, jdbcClient);
    BasicDataSource ds = jdbcClient.getDataSource(beakerParser.getDbURI().getActualConnectionString());
    Properties info = null;
    if (beakerParser.getDbURI().getUser() != null && !beakerParser.getDbURI().getUser().isEmpty()) {
        if (info == null) {
            info = new Properties();
        }
        info.put("user", beakerParser.getDbURI().getUser());
    }
    if (beakerParser.getDbURI().getPassword() != null && !beakerParser.getDbURI().getPassword().isEmpty()) {
        if (info == null) {
            info = new Properties();
        }
        info.put("password", beakerParser.getDbURI().getPassword());
    }
    boolean isConnectionExeption = true;
    // Workaround for "h2database" : do not work correctly with empty or null "Properties"
    try (Connection connection = info != null ? ds.getDriver().connect(beakerParser.getDbURI().getActualConnectionString(), info) : ds.getConnection()) {
        this.connection = connection;
        connection.setAutoCommit(false);
        List<Object> resultsForOutputCell = new ArrayList<>();
        Map<String, List<Object>> resultsForNamspace = new HashMap<>();
        for (BeakerParseResult queryLine : beakerParser.getResults()) {
            BeakerInputVar basicIterationArray = null;
            for (BeakerInputVar parameter : queryLine.getInputVars()) {
                if (parameter.isAll()) {
                    basicIterationArray = parameter;
                    if (parameter.getErrorMessage() != null)
                        throw new ReadVariableException(parameter.getErrorMessage());
                    // ToDo make recursively iteration over several arrays
                    break;
                }
            }
            try {
                if (basicIterationArray != null) {
                    int l;
                    Object obj;
                    try {
                        obj = namespaceClient.get(basicIterationArray.objectName);
                    } catch (Exception e) {
                        throw new ReadVariableException(basicIterationArray.objectName, e);
                    }
                    if (obj instanceof List) {
                        l = ((List) obj).size();
                    } else if (obj.getClass().isArray()) {
                        l = Array.getLength(obj);
                    } else
                        break;
                    for (int i = 0; i < l; i++) {
                        QueryResult queryResult = executeQuery(i, queryLine, connection, namespaceClient);
                        adoptResult(queryLine, queryResult, resultsForOutputCell, resultsForNamspace);
                    }
                } else {
                    QueryResult queryResult = executeQuery(-1, queryLine, connection, namespaceClient);
                    adoptResult(queryLine, queryResult, resultsForOutputCell, resultsForNamspace);
                }
            } catch (Exception e) {
                isConnectionExeption = false;
                throw e;
            }
        }
        connection.commit();
        for (String output : resultsForNamspace.keySet()) {
            if (resultsForNamspace.get(output).size() > 1) {
                OutputContainer outputContainer = new OutputContainer(resultsForNamspace.get(output));
                namespaceClient.set(output, outputContainer);
            } else if (!resultsForNamspace.get(output).isEmpty()) {
                namespaceClient.set(output, resultsForNamspace.get(output).get(0));
            } else {
                namespaceClient.set(output, null);
            }
        }
        if (resultsForOutputCell.size() > 1) {
            OutputContainer outputContainer = new OutputContainer(resultsForOutputCell);
            return outputContainer;
        } else if (!resultsForOutputCell.isEmpty()) {
            return resultsForOutputCell.get(0);
        } else {
            return OutputCell.HIDDEN;
        }
    } catch (Exception e) {
        if (beakerParser.getDbURI() != null && isConnectionExeption) {
            beakerParser.getDbURI().setShowDialog(true);
        }
        throw e;
    } finally {
        statement = null;
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) OutputContainer(com.twosigma.beakerx.jvm.object.OutputContainer) Properties(java.util.Properties) IOException(java.io.IOException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) List(java.util.List) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource)

Example 4 with OutputContainer

use of com.twosigma.beakerx.jvm.object.OutputContainer in project beakerx by twosigma.

the class OutputContainerDeserializer method deserialize.

@Override
public Object deserialize(JsonNode n, ObjectMapper mapper) {
    Object o = null;
    try {
        List<Object> items = null;
        List<String> labels = null;
        if (n.has("items")) {
            JsonNode nn = n.get("items");
            if (nn.isArray()) {
                items = new ArrayList<>();
                for (JsonNode no : nn) {
                    items.add(parent.deserialize(no, mapper));
                }
            }
        }
        if (n.has("labels")) {
            JsonNode nn = n.get("labels");
            if (nn.isArray()) {
                labels = new ArrayList<>();
                for (JsonNode no : nn) {
                    labels.add(no.asText());
                }
            }
        }
        o = createOutputContainer(items, labels);
        if (n.has("layout")) {
            Object layout = parent.deserialize(n.get("layout"), mapper);
            ((OutputContainer) o).setLayoutManager((OutputContainerLayoutManager) layout);
        }
    } catch (Exception e) {
        OutputContainer.LOGGER.error("exception deserializing OutputContainer ", e);
    }
    return o;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) OutputContainer(com.twosigma.beakerx.jvm.object.OutputContainer)

Aggregations

OutputContainer (com.twosigma.beakerx.jvm.object.OutputContainer)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 KernelTest (com.twosigma.beakerx.KernelTest)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Properties (java.util.Properties)1 BasicDataSource (org.apache.commons.dbcp2.BasicDataSource)1 Before (org.junit.Before)1 Test (org.junit.Test)1