Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TEIID-2734 adding native support to cassandra
- Loading branch information
Showing
4 changed files
with
178 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...src/main/java/org/teiid/translator/cassandra/execution/CassandraDirectQueryExecution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* See the COPYRIGHT.txt file distributed with this work for information | ||
* regarding copyright ownership. Some portions may be licensed | ||
* to Red Hat, Inc. under one or more contributor license agreements. | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
* 02110-1301 USA. | ||
*/ | ||
|
||
package org.teiid.translator.cassandra.execution; | ||
|
||
import java.util.List; | ||
|
||
import org.teiid.language.Argument; | ||
import org.teiid.language.Command; | ||
import org.teiid.language.Literal; | ||
import org.teiid.language.visitor.SQLStringVisitor; | ||
import org.teiid.translator.ExecutionContext; | ||
import org.teiid.translator.ProcedureExecution; | ||
import org.teiid.translator.TranslatorException; | ||
import org.teiid.translator.cassandra.CassandraConnection; | ||
|
||
public class CassandraDirectQueryExecution extends CassandraQueryExecution implements ProcedureExecution { | ||
|
||
private String cql; | ||
private List<Argument> arguments; | ||
|
||
public CassandraDirectQueryExecution(String cql, List<Argument> arguments, Command command, CassandraConnection connection, ExecutionContext context){ | ||
super(command, connection, context); | ||
this.arguments = arguments; | ||
this.cql = cql; | ||
} | ||
|
||
@Override | ||
public void execute() throws TranslatorException { | ||
StringBuilder buffer = new StringBuilder(); | ||
SQLStringVisitor.parseNativeQueryParts(cql, arguments, buffer, new SQLStringVisitor.Substitutor() { | ||
|
||
@Override | ||
public void substitute(Argument arg, StringBuilder builder, int index) { | ||
Literal argumentValue = arg.getArgumentValue(); | ||
builder.append(argumentValue); | ||
} | ||
}); | ||
String source_cql = buffer.toString(); | ||
execute(source_cql); | ||
} | ||
|
||
@Override | ||
public List<?> getOutputParameterValues() throws TranslatorException { | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...ranslator-cassandra/src/test/java/org/teiid/translator/cassandra/TestNativeCassandra.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* See the COPYRIGHT.txt file distributed with this work for information | ||
* regarding copyright ownership. Some portions may be licensed | ||
* to Red Hat, Inc. under one or more contributor license agreements. | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
* 02110-1301 USA. | ||
*/ | ||
|
||
package org.teiid.translator.cassandra; | ||
|
||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import org.teiid.cdk.api.TranslationUtility; | ||
import org.teiid.cdk.unittest.FakeTranslationFactory; | ||
import org.teiid.language.Command; | ||
import org.teiid.metadata.RuntimeMetadata; | ||
import org.teiid.query.metadata.TransformationMetadata; | ||
import org.teiid.query.unittest.RealMetadataFactory; | ||
import org.teiid.translator.Execution; | ||
import org.teiid.translator.ExecutionContext; | ||
import org.teiid.translator.TranslatorException; | ||
|
||
@SuppressWarnings("nls") | ||
public class TestNativeCassandra { | ||
|
||
@Test public void testDirect() throws TranslatorException { | ||
CassandraExecutionFactory cef = new CassandraExecutionFactory(); | ||
cef.setSupportsNativeQueries(true); | ||
|
||
String input = "call native('select $1', 'a')"; | ||
|
||
TranslationUtility util = FakeTranslationFactory.getInstance().getExampleTranslationUtility(); | ||
Command command = util.parseCommand(input); | ||
ExecutionContext ec = Mockito.mock(ExecutionContext.class); | ||
RuntimeMetadata rm = Mockito.mock(RuntimeMetadata.class); | ||
CassandraConnection connection = Mockito.mock(CassandraConnection.class); | ||
|
||
Execution execution = cef.createExecution(command, ec, rm, connection); | ||
execution.execute(); | ||
|
||
Mockito.verify(connection).executeQuery("select 'a'"); | ||
} | ||
|
||
@Test public void testNativeQuery() throws Exception { | ||
CassandraExecutionFactory cef = new CassandraExecutionFactory(); | ||
cef.setSupportsNativeQueries(true); | ||
|
||
String input = "call proc('a', 1)"; | ||
|
||
TransformationMetadata metadata = RealMetadataFactory.fromDDL("create foreign procedure proc (in x string, in y integer) options (\"teiid_rel:native-query\" 'delete from $1 where $2')", "x", "y"); | ||
TranslationUtility util = new TranslationUtility(metadata); | ||
Command command = util.parseCommand(input); | ||
ExecutionContext ec = Mockito.mock(ExecutionContext.class); | ||
RuntimeMetadata rm = Mockito.mock(RuntimeMetadata.class); | ||
CassandraConnection connection = Mockito.mock(CassandraConnection.class); | ||
|
||
Execution execution = cef.createExecution(command, ec, rm, connection); | ||
execution.execute(); | ||
|
||
Mockito.verify(connection).executeQuery("delete from 'a' where 1"); | ||
} | ||
|
||
} |