/
CassandraExecutionFactory.java
192 lines (161 loc) · 6.23 KB
/
CassandraExecutionFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* 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 java.util.List;
import javax.resource.cci.ConnectionFactory;
import org.teiid.core.BundleUtil;
import org.teiid.language.Argument;
import org.teiid.language.Call;
import org.teiid.language.Command;
import org.teiid.language.QueryExpression;
import org.teiid.language.visitor.SQLStringVisitor;
import org.teiid.logging.LogConstants;
import org.teiid.logging.LogManager;
import org.teiid.metadata.RuntimeMetadata;
import org.teiid.translator.ExecutionContext;
import org.teiid.translator.ExecutionFactory;
import org.teiid.translator.MetadataProcessor;
import org.teiid.translator.ProcedureExecution;
import org.teiid.translator.ResultSetExecution;
import org.teiid.translator.Translator;
import org.teiid.translator.TranslatorException;
import org.teiid.translator.UpdateExecution;
import com.datastax.driver.core.VersionNumber;
@Translator(name = "cassandra", description = "A translator for Cassandra NoSql database")
public class CassandraExecutionFactory extends ExecutionFactory<ConnectionFactory, CassandraConnection> {
private static final VersionNumber DEFAULT_VERSION = VersionNumber.parse("1.2.0"); //$NON-NLS-1$
private static final VersionNumber V2 = VersionNumber.parse("2.0.0"); //$NON-NLS-1$
private static final VersionNumber V2_2 = VersionNumber.parse("2.2.0"); //$NON-NLS-1$
public static final BundleUtil UTIL = BundleUtil.getBundleUtil(CassandraExecutionFactory.class);
public static enum Event implements BundleUtil.Event {
TEIID22000
}
private VersionNumber version;
@Override
public void start() throws TranslatorException {
super.start();
setTransactionSupport(TransactionSupport.NONE);
LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Cassandra ExecutionFactory Started"); //$NON-NLS-1$
}
@Override
public ResultSetExecution createResultSetExecution(QueryExpression command,
ExecutionContext executionContext, RuntimeMetadata metadata,
CassandraConnection connection) throws TranslatorException {
return new CassandraQueryExecution(command, connection, executionContext);
}
@Override
public UpdateExecution createUpdateExecution(Command command,
ExecutionContext executionContext, RuntimeMetadata metadata,
CassandraConnection connection) throws TranslatorException {
return new CassandraUpdateExecution(command, executionContext, metadata, connection);
}
@Override
public ProcedureExecution createProcedureExecution(Call command,
ExecutionContext executionContext, RuntimeMetadata metadata,
CassandraConnection connection) throws TranslatorException {
String nativeQuery = command.getMetadataObject().getProperty(SQLStringVisitor.TEIID_NATIVE_QUERY, false);
if (nativeQuery != null) {
return new CassandraDirectQueryExecution(nativeQuery, command.getArguments(), command, connection, executionContext, false);
}
throw new TranslatorException("Missing native-query extension metadata."); //$NON-NLS-1$
}
@Override
public ProcedureExecution createDirectExecution(List<Argument> arguments,
Command command, ExecutionContext executionContext,
RuntimeMetadata metadata, CassandraConnection connection)
throws TranslatorException {
return new CassandraDirectQueryExecution((String) arguments.get(0).getArgumentValue().getValue(), arguments.subList(1, arguments.size()), command, connection, executionContext, true);
}
@Override
public MetadataProcessor<CassandraConnection> getMetadataProcessor(){
return new CassandraMetadataProcessor();
}
@Override
public boolean supportsOrderBy() {
// Order by is allowed in very restrictive case when this is used as
// compound primary key's second column where it is defined partioned key
return false;
}
@Override
public boolean supportsAggregatesCountStar() {
return true;
}
@Override
public boolean supportsCompareCriteriaEquals() {
return true;
}
@Override
public boolean supportsCompareCriteriaOrdered() {
return true;
}
@Override
public boolean supportsInCriteria() {
return true;
}
@Override
public boolean supportsRowLimit() {
return true;
}
@Override
public boolean supportsBulkUpdate() {
return version.compareTo(V2) >= 0;
}
@Override
public boolean supportsBatchedUpdates() {
return version.compareTo(V2) >= 0;
}
@Override
public boolean supportsAggregatesSum() {
return version.compareTo(V2_2) >= 0;
}
@Override
public boolean supportsAggregatesAvg() {
return version.compareTo(V2_2) >= 0;
}
@Override
public boolean supportsAggregatesMin() {
return version.compareTo(V2_2) >= 0;
}
@Override
public boolean supportsAggregatesMax() {
return version.compareTo(V2_2) >= 0;
}
@Override
public boolean returnsSingleUpdateCount() {
return true;
}
@Override
public void initCapabilities(CassandraConnection connection)
throws TranslatorException {
if (connection == null) {
return;
}
this.version = connection.getVersion();
if (this.version == null) {
this.version = DEFAULT_VERSION;
}
}
@Override
public boolean isSourceRequiredForCapabilities() {
return true;
}
}