use of io.crate.action.sql.Session in project crate by crate.
the class PostgresWireProtocolTest method testSessionCloseOnTerminationMessage.
@Test
public void testSessionCloseOnTerminationMessage() throws Exception {
SQLOperations sqlOperations = mock(SQLOperations.class);
Session session = mock(Session.class);
when(sqlOperations.createSession(any(String.class), any(User.class))).thenReturn(session);
PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendStartupMessage(buffer, "doc");
ClientMessages.sendTermination(buffer);
channel.writeInbound(buffer);
channel.releaseInbound();
verify(session, times(1)).close();
}
use of io.crate.action.sql.Session in project crate by crate.
the class PostgresWireProtocolTest method submitQueriesThroughSimpleQueryMode.
private void submitQueriesThroughSimpleQueryMode(String statements, @Nullable Throwable failure) {
SQLOperations sqlOperations = Mockito.mock(SQLOperations.class);
Session session = mock(Session.class);
SessionContext sessionContext = new SessionContext(User.CRATE_USER);
when(session.sessionContext()).thenReturn(sessionContext);
when(sqlOperations.createSession(any(String.class), any(User.class))).thenReturn(session);
DescribeResult describeResult = mock(DescribeResult.class);
when(describeResult.getFields()).thenReturn(null);
when(session.describe(anyChar(), anyString())).thenReturn(describeResult);
when(session.transactionState()).thenReturn(TransactionState.IDLE);
PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionCtx -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
if (failure != null) {
when(session.sync()).thenAnswer(invocationOnMock -> {
Messages.sendErrorResponse(channel, AccessControl.DISABLED, failure);
return CompletableFuture.failedFuture(failure);
});
} else {
when(session.sync()).thenReturn(CompletableFuture.completedFuture(null));
}
sendStartupMessage(channel);
readAuthenticationOK(channel);
skipParameterMessages(channel);
readReadyForQueryMessage(channel);
ByteBuf query = Unpooled.buffer();
try {
// the actual statements don't have to be valid as they are not executed
Messages.writeCString(query, statements.getBytes(StandardCharsets.UTF_8));
ctx.handleSimpleQuery(query, new DelayableWriteChannel(channel));
} finally {
query.release();
}
}
use of io.crate.action.sql.Session in project crate by crate.
the class BatchPortalTest method testEachStatementReceivesCorrectParams.
@Test
public void testEachStatementReceivesCorrectParams() throws Throwable {
SQLExecutor sqlExecutor = SQLExecutor.builder(clusterService).addTable("create table t1 (x int)").build();
Plan insertPlan = new Plan() {
@Override
public StatementType type() {
return StatementType.INSERT;
}
@Override
public void executeOrFail(DependencyCarrier executor, PlannerContext plannerContext, RowConsumer consumer, Row params, SubQueryResults subQueryResults) {
consumer.accept(InMemoryBatchIterator.of(params, null), null);
}
};
Planner planner = new Planner(Settings.EMPTY, clusterService, sqlExecutor.nodeCtx, new TableStats(), null, null, sqlExecutor.schemas(), new StubUserManager(), mock(SessionSettingRegistry.class)) {
@Override
public Plan plan(AnalyzedStatement analyzedStatement, PlannerContext plannerContext) {
return insertPlan;
}
};
DependencyCarrier executor = mock(DependencyCarrier.class, Answers.RETURNS_MOCKS);
Session session = new Session(sqlExecutor.nodeCtx, sqlExecutor.analyzer, planner, new JobsLogs(() -> false), false, executor, AccessControl.DISABLED, SessionContext.systemSessionContext());
session.parse("S_1", "insert into t1(x) values(1)", Collections.emptyList());
session.bind("Portal", "S_1", Collections.emptyList(), null);
final ArrayList<Object[]> s1Rows = new ArrayList<>();
session.execute("Portal", 0, new BaseResultReceiver() {
@Override
public void setNextRow(Row row) {
s1Rows.add(row.materialize());
}
});
session.parse("S_2", "insert into t1(x) values(?)", Collections.emptyList());
session.bind("Portal", "S_2", Collections.singletonList(2), null);
final ArrayList<Object[]> s2Rows = new ArrayList<>();
session.execute("Portal", 0, new BaseResultReceiver() {
@Override
public void setNextRow(Row row) {
s2Rows.add(row.materialize());
}
});
session.sync().get(5, TimeUnit.SECONDS);
assertThat(s1Rows, contains(emptyArray()));
assertThat(s2Rows, contains(arrayContaining(is(2))));
}
use of io.crate.action.sql.Session in project crate by crate.
the class PostgresWireProtocolTest method testBindMessageCanBeReadIfTypeForParamsIsUnknown.
@Test
public void testBindMessageCanBeReadIfTypeForParamsIsUnknown() throws Exception {
PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendStartupMessage(buffer, "doc");
// no type hints for parameters
ClientMessages.sendParseMessage(buffer, "S1", "select ?, ?", new int[0]);
List<Object> params = Arrays.asList(10, 20);
ClientMessages.sendBindMessage(buffer, "P1", "S1", params);
channel.writeInbound(buffer);
channel.releaseInbound();
Session session = sessions.get(0);
// If the query can be retrieved via portalName it means bind worked
assertThat(session.getQuery("P1"), is("select ?, ?"));
}
use of io.crate.action.sql.Session in project crate by crate.
the class PostgresWireProtocolTest method prepare.
@Before
public void prepare() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table users (name text not null)").build();
sqlOperations = new SQLOperations(e.nodeCtx, e.analyzer, e.planner, () -> mock(DependencyCarrier.class), new JobsLogs(() -> true), Settings.EMPTY, clusterService, USER_MANAGER_PROVIDER) {
@Override
public Session createSession(@Nullable String defaultSchema, @Nullable User user) {
Session session = super.createSession(defaultSchema, user);
sessions.add(session);
return session;
}
};
}
Aggregations