protected void checkCassandraException(Exception e) { _mexceptions.incr(); if (e instanceof AlreadyExistsException || e instanceof AuthenticationException || e instanceof DriverException || e instanceof DriverInternalError || e instanceof InvalidConfigurationInQueryException || e instanceof InvalidQueryException || e instanceof InvalidTypeException || e instanceof QueryExecutionException || e instanceof QueryTimeoutException || e instanceof QueryValidationException || e instanceof ReadTimeoutException || e instanceof SyntaxError || e instanceof TraceRetrievalException || e instanceof TruncateException || e instanceof UnauthorizedException || e instanceof UnavailableException || e instanceof ReadTimeoutException || e instanceof WriteTimeoutException) { throw new ReportedFailedException(e); } else { throw new RuntimeException(e); } }
public void execute(InputStream commandStream) throws IOException { byte[] commandBuffer = new byte[commandStream.available()]; IOUtils.readFully(commandStream, commandBuffer); Cluster cluster = cassandraClusterBuilderMaker.create().addContactPoint(node).build(); Session session = cluster.connect(keyspace); List<String> commands = Arrays.asList(new String(commandBuffer, "UTF-8").split(";")); for(String command : commands){ String commandLine = command.trim().replaceAll("^-- .*", ""); if(!commandLine.isEmpty()){ command = commandLine + ";"; LOG.info("Execute:\n" + command); try { session.execute(command); } catch (SyntaxError e) { LOG.error("Command failed with " + e.getMessage()); throw e; } } } }
@Test public void testShouldReturnSyntaxError() throws Exception { String message = "pc load letter"; server.prime(when(query).then(syntaxError(message))); thrown.expect(SyntaxError.class); thrown.expectMessage(message); query(); }
@Test public void testShouldReturnSyntaxErrorOnPrepare() throws Exception { String message = "this syntax is no good"; server.prime(when(query).then(syntaxError(message)).applyToPrepare()); thrown.expect(SyntaxError.class); thrown.expectMessage(message); prepare(); }
@Test public void testShouldNotReturnSyntaxErrorOnPrepare() throws Exception { String message = "this syntax is no good"; server.prime(when(query).then(syntaxError(message)).ignoreOnPrepare()); // should not throw error here. prepare(); thrown.expect(SyntaxError.class); thrown.expectMessage(message); prepareAndQuery(); }
@Test public void testShouldNotReturnSyntaxErrorOnPrepareByDefault() throws Exception { String message = "this syntax is no good"; server.prime(when(query).then(syntaxError(message))); // should not throw error here. prepare(); thrown.expect(SyntaxError.class); thrown.expectMessage(message); prepareAndQuery(); }
@Test public void prepareAndExecuteWithCustomExpressions() throws Throwable { session.execute(dropKsStatement); session.execute(createKsStatement); String table = "custom_expr_test"; String index = "custom_index"; session.execute(String.format("CREATE TABLE IF NOT EXISTS %s.%s (id int PRIMARY KEY, cid int, val text);", KEYSPACE, table)); session.execute(String.format("CREATE CUSTOM INDEX %s ON %s.%s(val) USING '%s'", index, KEYSPACE, table, StubIndex.class.getName())); session.execute(String.format("INSERT INTO %s.%s(id, cid, val) VALUES (0, 0, 'test')", KEYSPACE, table)); PreparedStatement prepared1 = session.prepare(String.format("SELECT * FROM %s.%s WHERE expr(%s, 'foo')", KEYSPACE, table, index)); assertEquals(1, session.execute(prepared1.bind()).all().size()); PreparedStatement prepared2 = session.prepare(String.format("SELECT * FROM %s.%s WHERE expr(%s, ?)", KEYSPACE, table, index)); assertEquals(1, session.execute(prepared2.bind("foo bar baz")).all().size()); try { session.prepare(String.format("SELECT * FROM %s.%s WHERE expr(?, 'foo bar baz')", KEYSPACE, table)); fail("Expected syntax exception, but none was thrown"); } catch(SyntaxError e) { assertEquals("Bind variables cannot be used for index names", e.getMessage()); } }