/
OdbcUtils.java
144 lines (119 loc) · 4.89 KB
/
OdbcUtils.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.internal.processors.odbc;
import org.apache.ignite.IgniteException;
/**
* Various ODBC utility methods.
*/
public class OdbcUtils {
/** Latest version. */
public static final SqlListenerProtocolVersion VER_LATEST = SqlListenerProtocolVersion.VER_2_0_0;
/**
* Add quotation marks at the beginning and end of the string.
*
* @param str Input string.
* @return String surrounded with quotation marks.
*/
public static String addQuotationMarksIfNeeded(String str) {
if (str != null && !str.isEmpty() && !(str.startsWith("\"") && str.endsWith("\"")))
return "\"" + str + "\"";
return str;
}
/**
* Remove quotation marks at the beginning and end of the string if present.
*
* @param str Input string.
* @return String without leading and trailing quotation marks.
*/
public static String removeQuotationMarksIfNeeded(String str) {
if (str != null && str.startsWith("\"") && str.endsWith("\""))
return str.substring(1, str.length() - 1);
return str;
}
/**
* Private constructor.
*/
private OdbcUtils() {
// No-op.
}
/**
* Lookup Ignite data type corresponding to specific ODBC data type
*
* @param odbcDataType ODBC data type identifier
* @return Ignite data type name
*/
public static String getIgniteTypeFromOdbcType(String odbcDataType) {
assert odbcDataType != null;
switch (odbcDataType.toUpperCase()) {
case OdbcTypes.SQL_BIGINT:
return SqlListenerDataTypes.BIGINT;
case OdbcTypes.SQL_BINARY:
case OdbcTypes.SQL_LONGVARBINARY:
case OdbcTypes.SQL_VARBINARY:
return SqlListenerDataTypes.BINARY;
case OdbcTypes.SQL_BIT:
return SqlListenerDataTypes.BIT;
case OdbcTypes.SQL_CHAR:
return SqlListenerDataTypes.CHAR;
case OdbcTypes.SQL_DECIMAL:
case OdbcTypes.SQL_NUMERIC:
return SqlListenerDataTypes.DECIMAL;
case OdbcTypes.SQL_LONGVARCHAR:
case OdbcTypes.SQL_VARCHAR:
case OdbcTypes.SQL_WCHAR:
case OdbcTypes.SQL_WLONGVARCHAR:
case OdbcTypes.SQL_WVARCHAR:
return SqlListenerDataTypes.VARCHAR;
case OdbcTypes.SQL_DOUBLE:
case OdbcTypes.SQL_FLOAT:
return SqlListenerDataTypes.DOUBLE;
case OdbcTypes.SQL_REAL:
return SqlListenerDataTypes.REAL;
case OdbcTypes.SQL_GUID:
return SqlListenerDataTypes.UUID;
case OdbcTypes.SQL_SMALLINT:
return SqlListenerDataTypes.SMALLINT;
case OdbcTypes.SQL_INTEGER:
return SqlListenerDataTypes.INTEGER;
case OdbcTypes.SQL_DATE:
return SqlListenerDataTypes.DATE;
case OdbcTypes.SQL_TIME:
return SqlListenerDataTypes.TIME;
case OdbcTypes.SQL_TIMESTAMP:
return SqlListenerDataTypes.TIMESTAMP;
case OdbcTypes.SQL_TINYINT:
return SqlListenerDataTypes.TINYINT;
//No support for interval types
case OdbcTypes.SQL_INTERVAL_SECOND:
case OdbcTypes.SQL_INTERVAL_MINUTE:
case OdbcTypes.SQL_INTERVAL_HOUR:
case OdbcTypes.SQL_INTERVAL_DAY:
case OdbcTypes.SQL_INTERVAL_MONTH:
case OdbcTypes.SQL_INTERVAL_YEAR:
case OdbcTypes.SQL_INTERVAL_YEAR_TO_MONTH:
case OdbcTypes.SQL_INTERVAL_HOUR_TO_MINUTE:
case OdbcTypes.SQL_INTERVAL_HOUR_TO_SECOND:
case OdbcTypes.SQL_INTERVAL_MINUTE_TO_SECOND:
case OdbcTypes.SQL_INTERVAL_DAY_TO_HOUR:
case OdbcTypes.SQL_INTERVAL_DAY_TO_MINUTE:
case OdbcTypes.SQL_INTERVAL_DAY_TO_SECOND:
throw new IgniteException("Unsupported ODBC data type '" + odbcDataType + "'");
default:
throw new IgniteException("Invalid ODBC data type '" + odbcDataType + "'");
}
}
}