forked from teiid/teiid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySQL5ExecutionFactory.java
180 lines (155 loc) · 6.4 KB
/
MySQL5ExecutionFactory.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
/*
* 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.jdbc.mysql;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.teiid.language.Expression;
import org.teiid.language.Function;
import org.teiid.language.Literal;
import org.teiid.language.SQLConstants.NonReserved;
import org.teiid.translator.SourceSystemFunctions;
import org.teiid.translator.Translator;
import org.teiid.translator.TranslatorException;
import org.teiid.translator.TypeFacility;
import org.teiid.translator.jdbc.FunctionModifier;
import org.teiid.translator.jdbc.Version;
@Translator(name="mysql5", description="A translator for open source MySQL5 Database")
public class MySQL5ExecutionFactory extends MySQLExecutionFactory {
public static final Version FIVE_6 = Version.getVersion("5.6"); //$NON-NLS-1$
@Override
public void start() throws TranslatorException {
super.start();
registerFunctionModifier(SourceSystemFunctions.CHAR, new FunctionModifier() {
@Override
public List<?> translate(Function function) {
return Arrays.asList("char(", function.getParameters().get(0), " USING ASCII)"); //$NON-NLS-1$ //$NON-NLS-2$
}
});
registerFunctionModifier(SourceSystemFunctions.TIMESTAMPADD, new FunctionModifier() {
@Override
public List<?> translate(Function function) {
Literal intervalType = (Literal)function.getParameters().get(0);
String interval = ((String)intervalType.getValue()).toUpperCase();
if (interval.equals(NonReserved.SQL_TSI_FRAC_SECOND)) {
intervalType.setValue("MICROSECOND"); //$NON-NLS-1$
Expression[] args = new Expression[] {function.getParameters().get(1), getLanguageFactory().createLiteral(1000, TypeFacility.RUNTIME_TYPES.INTEGER)};
function.getParameters().set(1, getLanguageFactory().createFunction("/", args, TypeFacility.RUNTIME_TYPES.INTEGER)); //$NON-NLS-1$
}
return null;
}
});
addPushDownFunction("mysql", "timestampdiff", TypeFacility.RUNTIME_NAMES.INTEGER, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.TIMESTAMP, TypeFacility.RUNTIME_NAMES.TIMESTAMP); //$NON-NLS-1$ //$NON-NLS-2$
registerFunctionModifier(SourceSystemFunctions.TIMESTAMPDIFF, new FunctionModifier() {
@Override
public List<?> translate(Function function) {
Literal intervalType = (Literal)function.getParameters().get(0);
String interval = ((String)intervalType.getValue()).toUpperCase();
if (interval.equals(NonReserved.SQL_TSI_FRAC_SECOND)) {
intervalType.setValue("MICROSECOND"); //$NON-NLS-1$
return Arrays.asList(function, " * 1000"); //$NON-NLS-1$
}
return null;
}
});
}
@Override
public List<String> getSupportedFunctions() {
List<String> supportedFunctions = new ArrayList<String>();
supportedFunctions.addAll(super.getSupportedFunctions());
supportedFunctions.add(SourceSystemFunctions.TIMESTAMPADD);
//mysql rounds down even when crossing a date part
//supportedFunctions.add(SourceSystemFunctions.TIMESTAMPDIFF);
if (getVersion().compareTo(FIVE_6) >= 0) {
supportedFunctions.add(SourceSystemFunctions.ST_INTERSECTS);
supportedFunctions.add(SourceSystemFunctions.ST_CONTAINS);
supportedFunctions.add(SourceSystemFunctions.ST_CROSSES);
supportedFunctions.add(SourceSystemFunctions.ST_DISJOINT);
supportedFunctions.add(SourceSystemFunctions.ST_DISTANCE);
supportedFunctions.add(SourceSystemFunctions.ST_OVERLAPS);
supportedFunctions.add(SourceSystemFunctions.ST_TOUCHES);
}
return supportedFunctions;
}
@Override
protected boolean usesDatabaseVersion() {
return true;
}
@Override
public boolean supportsInlineViews() {
return true;
}
@Override
public boolean supportsAggregatesEnhancedNumeric() {
return true;
}
@Override
public boolean supportsLikeRegex() {
return true;
}
@Override
public String getLikeRegexString() {
return "REGEXP"; //$NON-NLS-1$
}
@Override
public Object retrieveValue(ResultSet results, int columnIndex,
Class<?> expectedType) throws SQLException {
Object result = super.retrieveValue(results, columnIndex, expectedType);
if (expectedType == TypeFacility.RUNTIME_TYPES.STRING && (result instanceof Blob || result instanceof byte[])) {
return results.getString(columnIndex);
}
return result;
}
@Override
public Object retrieveValue(CallableStatement results, int parameterIndex,
Class<?> expectedType) throws SQLException {
Object result = super.retrieveValue(results, parameterIndex, expectedType);
if (expectedType == TypeFacility.RUNTIME_TYPES.STRING && (result instanceof Blob || result instanceof byte[])) {
return results.getString(parameterIndex);
}
return result;
}
@Override
public String getHibernateDialectClassName() {
return "org.hibernate.dialect.MySQL5Dialect"; //$NON-NLS-1$
}
@Override
public boolean supportsGroupByRollup() {
return true;
}
@Override
public boolean useWithRollup() {
return true;
}
@Override
public boolean supportsOrderByWithExtendedGrouping() {
return false;
}
@Override
public boolean supportsFunctionsInGroupBy() {
return true;
}
}