use of io.crate.action.sql.SQLOperations 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.SQLOperations 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.SQLOperations in project crate by crate.
the class PostgresWireProtocolTest method testDescribeStatementMessage.
@Test
public void testDescribeStatementMessage() 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");
ClientMessages.sendParseMessage(buffer, "S1", "select ? in (1, 2, 3)", new int[0]);
channel.writeInbound(buffer);
channel.releaseInbound();
// we're not interested in the startup, parse, or bind replies
channel.flushOutbound();
channel.releaseOutbound();
channel.outboundMessages().clear();
}
{
// try the describe statement variant
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendDescribeMessage(buffer, ClientMessages.DescribeType.STATEMENT, "S1");
channel.writeInbound(buffer);
channel.releaseInbound();
// we should get back a ParameterDescription message
channel.flushOutbound();
ByteBuf response = channel.readOutbound();
try {
assertThat(response.readByte(), is((byte) 't'));
assertThat(response.readInt(), is(10));
assertThat(response.readShort(), is((short) 1));
assertThat(response.readInt(), is(PGTypes.get(DataTypes.INTEGER).oid()));
} finally {
response.release();
}
// we should get back a RowDescription message
response = channel.readOutbound();
try {
assertThat(response.readByte(), is((byte) 'T'));
assertThat(response.readInt(), is(46));
assertThat(response.readShort(), is((short) 1));
assertThat(PostgresWireProtocol.readCString(response), is("($1 = ANY([1, 2, 3]))"));
assertThat(response.readInt(), is(0));
assertThat(response.readShort(), is((short) 0));
assertThat(response.readInt(), is(PGTypes.get(DataTypes.BOOLEAN).oid()));
assertThat(response.readShort(), is(PGTypes.get(DataTypes.BOOLEAN).typeLen()));
assertThat(response.readInt(), is(PGTypes.get(DataTypes.LONG).typeMod()));
assertThat(response.readShort(), is((short) 0));
} finally {
response.release();
}
}
}
use of io.crate.action.sql.SQLOperations in project crate by crate.
the class PostgresWireProtocolTest method testDescribePortalMessage.
@Test
public void testDescribePortalMessage() 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");
ClientMessages.sendParseMessage(buffer, "S1", "select ? in (1, 2, 3)", new int[] { PGTypes.get(DataTypes.INTEGER).oid() });
ClientMessages.sendBindMessage(buffer, "P1", "S1", Collections.singletonList(1));
channel.writeInbound(buffer);
channel.releaseInbound();
// we're not interested in the startup, parse, or bind replies
channel.flushOutbound();
channel.releaseOutbound();
channel.outboundMessages().clear();
}
{
// try portal describe message
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendDescribeMessage(buffer, ClientMessages.DescribeType.PORTAL, "P1");
channel.writeInbound(buffer);
channel.releaseInbound();
// we should get back a RowDescription message
channel.flushOutbound();
ByteBuf response = channel.readOutbound();
try {
assertThat(response.readByte(), is((byte) 'T'));
assertThat(response.readInt(), is(46));
assertThat(response.readShort(), is((short) 1));
assertThat(PostgresWireProtocol.readCString(response), is("($1 = ANY([1, 2, 3]))"));
assertThat(response.readInt(), is(0));
assertThat(response.readShort(), is((short) 0));
assertThat(response.readInt(), is(PGTypes.get(DataTypes.BOOLEAN).oid()));
assertThat(response.readShort(), is(PGTypes.get(DataTypes.BOOLEAN).typeLen()));
assertThat(response.readInt(), is(PGTypes.get(DataTypes.LONG).typeMod()));
assertThat(response.readShort(), is((short) 0));
} finally {
response.release();
}
}
}
use of io.crate.action.sql.SQLOperations 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