--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-user+unsubscribe@lists.datastax.com.
--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-us...@lists.datastax.com.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-user+unsubscribe@lists.datastax.com.
--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-user+unsubscribe@lists.datastax.com.
package com.hello.world;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CommonTokenStream;
import org.apache.cassandra.cql3.CqlLexer;
import org.apache.cassandra.cql3.CqlParser;
import org.apache.cassandra.cql3.statements.CreateTableStatement;
import org.apache.cassandra.cql3.statements.ParsedStatement;
public class Test {
public static void main(String[] args) throws Exception {
String stmt = "create table if not exists test_keyspace.my_table (field1 text, field2 int, field3 set<ascii>, field4 map<ascii, text>, primary key (field1) );";
ANTLRStringStream stringStream = new ANTLRStringStream(stmt);
CqlLexer cqlLexer = new CqlLexer(stringStream);
CommonTokenStream token = new CommonTokenStream(cqlLexer);
CqlParser parser = new CqlParser(token);
ParsedStatement query = parser.query();
if (query.getClass().getDeclaringClass() == CreateTableStatement.class) {
CreateTableStatement.RawStatement cts = (CreateTableStatement.RawStatement) query;
System.out.println(cts.keyspace());
System.out.println(cts.columnFamily());
ParsedStatement.Prepared prepared = cts.prepare();
CreateTableStatement cts2 = (CreateTableStatement) prepared.statement;
cts2.getCFMetaData()
.getColumnMetadata()
.values()
.stream()
.forEach(cd -> System.out.println(cd));
}
}
}
protected static String[] splitStatementsUsingCqlLexer(String statements) {
ANTLRStringStream stream = new ANTLRStringStream(statements);
CqlLexer lexer = new CqlLexer(stream);
ArrayList<String> statementList = new ArrayList<String>();
StringBuffer currentStatement = new StringBuffer();
boolean inComment;
// Not the prettiest code i ever wrote, but it gets the job done.
for (Token token = lexer.nextToken(); token.getType() != Token.EOF; token = lexer.nextToken()) {
if (token.getText().equals(";")) {
// when we meet a ; terminate current statement and prepare the next
currentStatement.append(";");
statementList.add(currentStatement.toString());
currentStatement = new StringBuffer();
} else if (token.getType() == CqlLexer.STRING_LITERAL) {
// If we meet a string we should quote it and escape any enclosed ' as ''
currentStatement.append("'");
// TODO: There must be a cassandra util method somewhere that escapes a string for sql
currentStatement.append(token.getText().replaceAll("'", "''"));
currentStatement.append("'");
} else {
currentStatement.append(token.getText());
}
}
if (currentStatement.length() > 0 && currentStatement.toString().trim().length() > 0) {
statementList.add(currentStatement.toString());
}
return statementList.toArray(new String[statementList.size()]);
}
Best regards Jens
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-us...@lists.datastax.com.
--With best wishes, Alex Ott
http://alexott.net/
Twitter: alexott_en (English), alexott (Russian)
Skype: alex.ott
--With best wishes, Alex Ott
http://alexott.net/
Twitter: alexott_en (English), alexott (Russian)
Skype: alex.ott
--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-us...@lists.datastax.com.