Search in sources :

Example 1 with SyntaxError

use of com.datastax.driver.core.exceptions.SyntaxError in project newts by OpenNMS.

the class SchemaManager method create.

public void create(Schema schema, boolean ifNotExists, boolean printOnly) throws IOException {
    checkNotNull(schema, "schema argument");
    InputStream stream = checkNotNull(schema.getInputStream(), "schema input stream");
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
    String line, scrubbed;
    StringBuilder statement = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        scrubbed = scrub(line);
        statement.append(scrubbed);
        if (scrubbed.endsWith(";")) {
            // Substitute the actual keyspace name for any KEYSPACE macros.
            String queryString = statement.toString().replace(KEYSPACE_PLACEHOLDER, m_keyspace);
            queryString = queryString.replace(REPLICATION_FACTOR_PLACEHOLDER, Integer.toString(m_replicationFactor));
            if (printOnly) {
                System.err.println(queryString);
            } else {
                try {
                    m_session.execute(queryString);
                } catch (AlreadyExistsException e) {
                    if (ifNotExists) {
                        System.err.printf("%s; Nothing here to do%n", e.getLocalizedMessage());
                    } else {
                        throw e;
                    }
                } catch (SyntaxError e) {
                    System.out.printf("ERROR: %s (query: \"%s\").%n", e.getLocalizedMessage(), queryString);
                    throw e;
                }
            }
            statement = new StringBuilder();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) AlreadyExistsException(com.datastax.driver.core.exceptions.AlreadyExistsException) SyntaxError(com.datastax.driver.core.exceptions.SyntaxError) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader)

Example 2 with SyntaxError

use of com.datastax.driver.core.exceptions.SyntaxError in project cassandra by apache.

the class PreparedStatementsTest method prepareAndExecuteWithCustomExpressions.

@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());
    }
}
Also used : SyntaxError(com.datastax.driver.core.exceptions.SyntaxError) PreparedStatement(com.datastax.driver.core.PreparedStatement) Test(org.junit.Test)

Aggregations

SyntaxError (com.datastax.driver.core.exceptions.SyntaxError)2 PreparedStatement (com.datastax.driver.core.PreparedStatement)1 AlreadyExistsException (com.datastax.driver.core.exceptions.AlreadyExistsException)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Test (org.junit.Test)1