Search in sources :

Example 21 with Value

use of org.neo4j.driver.Value in project neo4j by neo4j.

the class BoltStateHandler method changePassword.

public void changePassword(@Nonnull ConnectionConfig connectionConfig) {
    if (!connectionConfig.passwordChangeRequired()) {
        return;
    }
    if (isConnected()) {
        silentDisconnect();
    }
    final AuthToken authToken = AuthTokens.basic(connectionConfig.username(), connectionConfig.password());
    try {
        driver = getDriver(connectionConfig, authToken);
        activeDatabaseNameAsSetByUser = SYSTEM_DB_NAME;
        // Supply empty command, so that we do not run ping.
        reconnect(SYSTEM_DB_NAME, SYSTEM_DB_NAME, () -> {
        });
        try {
            String command = "ALTER CURRENT USER SET PASSWORD FROM $o TO $n";
            Value parameters = Values.parameters("o", connectionConfig.password(), "n", connectionConfig.newPassword());
            Result run = session.run(command, parameters);
            run.consume();
        } catch (Neo4jException e) {
            if (isPasswordChangeRequiredException(e)) {
                // In < 4.0 versions use legacy method.
                String oldCommand = "CALL dbms.security.changePassword($n)";
                Value oldParameters = Values.parameters("n", connectionConfig.newPassword());
                Result run = session.run(oldCommand, oldParameters);
                run.consume();
            } else {
                throw e;
            }
        }
        // If successful, use the new password when reconnecting
        connectionConfig.setPassword(connectionConfig.newPassword());
        connectionConfig.setNewPassword(null);
        silentDisconnect();
    } catch (Throwable t) {
        try {
            silentDisconnect();
        } catch (Exception e) {
            t.addSuppressed(e);
        }
        if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        }
        // we cannot get that since we supply an empty command.
        throw new RuntimeException(t);
    }
}
Also used : Value(org.neo4j.driver.Value) AuthToken(org.neo4j.driver.AuthToken) Neo4jException(org.neo4j.driver.exceptions.Neo4jException) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) Versions.isPasswordChangeRequiredException(org.neo4j.shell.util.Versions.isPasswordChangeRequiredException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ClientException(org.neo4j.driver.exceptions.ClientException) Neo4jException(org.neo4j.driver.exceptions.Neo4jException) CommandException(org.neo4j.shell.exception.CommandException) Result(org.neo4j.driver.Result)

Example 22 with Value

use of org.neo4j.driver.Value in project neo4j by neo4j.

the class CypherShellTest method setParamShouldAddParam.

@Test
public void setParamShouldAddParam() throws ParameterException, CommandException {
    Value value = mock(Value.class);
    Record recordMock = mock(Record.class);
    BoltResult boltResult = mock(ListBoltResult.class);
    when(mockedBoltStateHandler.runCypher(anyString(), anyMap())).thenReturn(Optional.of(boltResult));
    when(boltResult.getRecords()).thenReturn(asList(recordMock));
    when(recordMock.get("bob")).thenReturn(value);
    when(value.asObject()).thenReturn("99");
    assertTrue(offlineTestShell.getParameterMap().allParameterValues().isEmpty());
    Object result = offlineTestShell.getParameterMap().setParameter("`bob`", "99");
    assertEquals(99L, result);
    assertEquals(99L, offlineTestShell.getParameterMap().allParameterValues().get("bob"));
}
Also used : Value(org.neo4j.driver.Value) Record(org.neo4j.driver.Record) Mockito.anyObject(org.mockito.Mockito.anyObject) BoltResult(org.neo4j.shell.state.BoltResult) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Test(org.junit.Test)

Example 23 with Value

use of org.neo4j.driver.Value in project neo4j by neo4j.

the class OutputFormatterTest method buildOperator.

private Value buildOperator(String operator, long dbHits, long rows, Value child) {
    Map<String, Value> operatorMap = new HashMap<>();
    operatorMap.put("operatorType", Values.value(operator));
    operatorMap.put("dbHits", Values.value(dbHits));
    operatorMap.put("rows", Values.value(rows));
    if (child != null) {
        operatorMap.put("children", new ListValue(child));
    }
    return new MapValue(operatorMap);
}
Also used : HashMap(java.util.HashMap) ListValue(org.neo4j.driver.internal.value.ListValue) MapValue(org.neo4j.driver.internal.value.MapValue) Value(org.neo4j.driver.Value) ListValue(org.neo4j.driver.internal.value.ListValue) MapValue(org.neo4j.driver.internal.value.MapValue)

Example 24 with Value

use of org.neo4j.driver.Value in project neo4j by neo4j.

the class OutputFormatter method info.

@Nonnull
static Map<String, Value> info(@Nonnull ResultSummary summary) {
    Map<String, Value> result = new LinkedHashMap<>();
    if (!summary.hasPlan()) {
        return result;
    }
    Plan plan = summary.plan();
    result.put("Plan", Values.value(summary.hasProfile() ? "PROFILE" : "EXPLAIN"));
    result.put("Statement", Values.value(summary.queryType().name()));
    Map<String, Value> arguments = plan.arguments();
    Value emptyString = Values.value("");
    Value questionMark = Values.value("?");
    for (String key : INFO_SUMMARY) {
        Value value = arguments.getOrDefault(key, arguments.getOrDefault(key.toLowerCase(), emptyString));
        result.put(key, value);
    }
    result.put("Time", Values.value(summary.resultAvailableAfter(MILLISECONDS) + summary.resultConsumedAfter(MILLISECONDS)));
    if (summary.hasProfile()) {
        result.put("DbHits", Values.value(collectHits(summary.profile())));
    }
    if (summary.hasProfile()) {
        result.put("Rows", Values.value(summary.profile().records()));
    }
    if (summary.hasProfile()) {
        result.put("Memory (Bytes)", arguments.getOrDefault("GlobalMemory", questionMark));
    }
    return result;
}
Also used : DurationValue(org.neo4j.values.storable.DurationValue) Value(org.neo4j.driver.Value) Plan(org.neo4j.driver.summary.Plan) ProfiledPlan(org.neo4j.driver.summary.ProfiledPlan) LinkedHashMap(java.util.LinkedHashMap) Nonnull(javax.annotation.Nonnull)

Example 25 with Value

use of org.neo4j.driver.Value in project neo4j by neo4j.

the class TableOutputFormatter method formatInfo.

@Override
@Nonnull
public String formatInfo(@Nonnull ResultSummary summary) {
    Map<String, Value> info = OutputFormatter.info(summary);
    if (info.isEmpty()) {
        return "";
    }
    String[] columns = info.keySet().toArray(new String[0]);
    StringBuilder sb = new StringBuilder();
    Record record = new InternalRecord(asList(columns), info.values().toArray(new Value[0]));
    formatResultAndCountRows(columns, Collections.singletonList(record).iterator(), line -> sb.append(line).append(OutputFormatter.NEWLINE));
    return sb.toString();
}
Also used : InternalRecord(org.neo4j.driver.internal.InternalRecord) Value(org.neo4j.driver.Value) InternalRecord(org.neo4j.driver.internal.InternalRecord) Record(org.neo4j.driver.Record) Nonnull(javax.annotation.Nonnull)

Aggregations

Value (org.neo4j.driver.Value)37 Test (org.junit.Test)29 ListBoltResult (org.neo4j.shell.state.ListBoltResult)22 Record (org.neo4j.driver.Record)21 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)16 BoltResult (org.neo4j.shell.state.BoltResult)15 ResultSummary (org.neo4j.driver.summary.ResultSummary)14 HashMap (java.util.HashMap)12 StringContains.containsString (org.hamcrest.core.StringContains.containsString)9 DurationValue (org.neo4j.driver.internal.value.DurationValue)9 NodeValue (org.neo4j.driver.internal.value.NodeValue)9 PathValue (org.neo4j.driver.internal.value.PathValue)9 PointValue (org.neo4j.driver.internal.value.PointValue)9 RelationshipValue (org.neo4j.driver.internal.value.RelationshipValue)9 InternalRecord (org.neo4j.driver.internal.InternalRecord)8 Plan (org.neo4j.driver.summary.Plan)7 Node (org.neo4j.driver.types.Node)7 Relationship (org.neo4j.driver.types.Relationship)7 ProfiledPlan (org.neo4j.driver.summary.ProfiledPlan)6 FloatValue (org.neo4j.driver.internal.value.FloatValue)5