16 package ai.heavy.jdbc;
18 import org.apache.thrift.TException;
19 import org.slf4j.LoggerFactory;
21 import java.sql.Connection;
22 import java.sql.ResultSet;
23 import java.sql.SQLException;
24 import java.sql.SQLWarning;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
28 import ai.heavy.thrift.server.Heavy;
29 import ai.heavy.thrift.server.TDBException;
30 import ai.heavy.thrift.server.TQueryResult;
33 final static org.slf4j.Logger
logger = LoggerFactory.getLogger(HeavyAIStatement.class);
48 client = connection.client;
53 Pattern.compile(
"select top\\s+([0-9]+)\\s+", Pattern.CASE_INSENSITIVE);
64 String[] tokens = sql.toLowerCase().
split(
" ", 3);
65 if (tokens[0].equals(
"select")) {
66 if (sql.toLowerCase().contains(
"limit")) {
72 Matcher matcher = top_pattern.matcher(sql);
76 maxRows = Integer.parseInt(matcher.group(1));
77 sql = top_pattern.matcher(sql).replaceAll(
"select ");
80 sql = sql +
" LIMIT " +
maxRows;
81 logger.debug(
"Added LIMIT of " +
maxRows);
86 logger.debug(
"Before HeavyAIEscapeParser [" + sql +
"]");
89 String afterEscapeParseSQL = HeavyAIEscapeParser.parse(sql);
91 logger.debug(
"After HeavyAIEscapeParser [" + afterSimpleParse +
"]");
93 sqlResult = client.sql_execute(
session, afterSimpleParse +
";",
true, null, -1, -1);
94 }
catch (TDBException ex) {
95 throw new SQLException(
97 }
catch (TException ex) {
98 throw new SQLException(
107 public void cancel() throws SQLException {
111 alternate_connection = connection.getAlternateConnection();
114 }
catch (TDBException ttE) {
115 throw new SQLException(
"Thrift transport connection failed - "
118 }
catch (TException tE) {
119 throw new SQLException(
124 alternate_connection.closeConnection();
133 if (sql.trim().substring(0, 6).compareToIgnoreCase(
"CREATE") == 0) {
134 sql = sql.replace(
'"',
' ');
137 }
catch (TDBException ex) {
138 throw new SQLException(
"Query failed : sql was '" + sql +
"' "
141 }
catch (TException ex) {
142 throw new SQLException(
152 public void close() throws SQLException {
161 throw new UnsupportedOperationException(
"Not supported yet,"
162 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
163 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
164 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
169 throw new UnsupportedOperationException(
"Not supported yet,"
170 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
171 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
172 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
187 throws SQLException {
198 throws SQLException {
199 return (
int) sqlResult.execution_time_ms;
204 throws SQLException {
205 SQLWarning warning =
new SQLWarning(
206 "Query timeouts are not supported. Substituting a value of zero.");
210 rootWarning.setNextWarning(warning);
226 throw new UnsupportedOperationException(
"Not supported yet,"
227 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
228 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
229 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
233 public boolean execute(String sql)
throws SQLException {
265 throws SQLException {
266 throw new UnsupportedOperationException(
"Not supported yet,"
267 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
268 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
269 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
274 return ResultSet.FETCH_FORWARD;
279 SQLWarning warning =
new SQLWarning(
280 "Query FetchSize are not supported. Substituting a value of zero.");
284 rootWarning.setNextWarning(warning);
290 throw new UnsupportedOperationException(
"Not supported yet,"
291 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
292 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
293 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
298 throw new UnsupportedOperationException(
"Not supported yet,"
299 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
300 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
301 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
306 throw new UnsupportedOperationException(
"Not supported yet,"
307 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
308 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
309 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
313 public void addBatch(String sql)
throws SQLException {
314 throw new UnsupportedOperationException(
"Not supported yet,"
315 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
316 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
317 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
322 throw new UnsupportedOperationException(
"Not supported yet,"
323 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
324 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
325 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
330 throw new UnsupportedOperationException(
"Not supported yet,"
331 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
332 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
333 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
343 throws SQLException {
344 throw new UnsupportedOperationException(
"Not supported yet,"
345 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
346 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
347 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
352 throw new UnsupportedOperationException(
"Not supported yet,"
353 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
354 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
355 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
360 throws SQLException {
361 throw new UnsupportedOperationException(
"Not supported yet,"
362 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
363 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
364 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
369 throws SQLException {
370 throw new UnsupportedOperationException(
"Not supported yet,"
371 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
372 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
373 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
378 throws SQLException {
379 throw new UnsupportedOperationException(
"Not supported yet,"
380 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
381 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
382 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
386 public boolean execute(String sql,
int autoGeneratedKeys)
387 throws SQLException {
388 throw new UnsupportedOperationException(
"Not supported yet,"
389 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
390 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
391 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
395 public boolean execute(String sql,
int[] columnIndexes)
396 throws SQLException {
397 throw new UnsupportedOperationException(
"Not supported yet,"
398 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
399 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
400 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
404 public boolean execute(String sql, String[] columnNames)
405 throws SQLException {
406 throw new UnsupportedOperationException(
"Not supported yet,"
407 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
408 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
409 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
414 throw new UnsupportedOperationException(
"Not supported yet,"
415 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
416 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
417 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
427 throws SQLException {
428 throw new UnsupportedOperationException(
"Not supported yet,"
429 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
430 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
431 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
436 throw new UnsupportedOperationException(
"Not supported yet,"
437 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
438 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
439 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
444 throw new UnsupportedOperationException(
"Not supported yet,"
445 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
446 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
447 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
452 throw new UnsupportedOperationException(
"Not supported yet,"
453 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
454 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
455 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
459 public <T>
T unwrap(Class<T> iface)
throws SQLException {
460 throw new UnsupportedOperationException(
"Not supported yet,"
461 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
462 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
463 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
468 throws SQLException {
469 throw new UnsupportedOperationException(
"Not supported yet,"
470 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
471 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
472 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
475 private static final Pattern
QUARTER = Pattern.compile(
476 "\\sQUARTER\\(([^\\{]*?)", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
477 private static final Pattern
DAYOFYEAR = Pattern.compile(
478 "\\sDAYOFYEAR\\(([^\\{]*?)", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
479 private static final Pattern
DAYOFWEEK = Pattern.compile(
480 "\\sDAYOFWEEK\\(([^\\{]*?)", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
481 private static final Pattern
WEEK = Pattern.compile(
482 "\\sWEEK\\(([^\\{]*?)", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
503 Pattern.compile(
"(?<![\\w.])CURRENT_DATE(?:\\(\\))?(?![\\w.])",
504 Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
514 sql = QUARTER.matcher(sql).replaceAll(
" EXTRACT(QUARTER FROM $1");
515 }
while (!sql.equals(start));
519 sql = DAYOFYEAR.matcher(sql).replaceAll(
" EXTRACT(DOY FROM $1");
520 }
while (!sql.equals(start));
524 sql = DAYOFWEEK.matcher(sql).replaceAll(
" EXTRACT(ISODOW FROM $1");
525 }
while (!sql.equals(start));
529 sql = WEEK.matcher(sql).replaceAll(
" EXTRACT(WEEK FROM $1");
530 }
while (!sql.equals(start));
534 sql = CURRENTDATE.matcher(sql).replaceAll(
" cast(now() as date) ");
535 }
while (!sql.equals(start));
542 throw new SQLException(
"Statement is closed.");
int getResultSetHoldability()
boolean execute(String sql, int[] columnIndexes)
void setMaxFieldSize(int max)
public< T > T unwrap(Class< T > iface)
static final Pattern QUARTER
int executeUpdate(String sql)
boolean isWrapperFor(Class<?> iface)
int executeUpdate(String sql, String[] columnNames)
static final Pattern DAYOFYEAR
static String simplisticDateTransform(String sql)
boolean isCloseOnCompletion()
Connection getConnection()
boolean execute(String sql, String[] columnNames)
static final Pattern WEEK
void setFetchDirection(int direction)
void setPoolable(boolean poolable)
int getResultSetConcurrency()
boolean execute(String sql, int autoGeneratedKeys)
static final Pattern CURRENTDATE
void addBatch(String sql)
int executeUpdate(String sql, int[] columnIndexes)
static Pattern top_pattern
ResultSet getGeneratedKeys()
HeavyAIStatement(String tsession, HeavyAIConnection tconnection)
void setFetchSize(int rows)
static String getExceptionDetail(Exception ex)
static final org.slf4j.Logger logger
boolean getMoreResults(int current)
ResultSet executeQuery(String sql)
boolean execute(String sql)
int getQueryInternalExecuteTime()
static final Pattern DAYOFWEEK
void setQueryTimeout(int seconds)
HeavyAIConnection connection
void setEscapeProcessing(boolean enable)
void setCursorName(String name)
int executeUpdate(String sql, int autoGeneratedKeys)