src.main.java.com.aceql.client.jdbc.AceQLPreparedStatement Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aceql-http-client-sdk Show documentation
Show all versions of aceql-http-client-sdk Show documentation
The AceQL Java Client SDK allows to wrap the AceQL HTTP APIs and eliminate the tedious works of handling communications errors and parsing JSON results.
Android and Java Desktop application developers can access remote SQL databases and/or SQL databases in the cloud by simply including standard JDBC calls in their code, just like they would for a local database.
/*
* This file is part of AceQL Client SDK.
* AceQL Client SDK: Remote JDBC access over HTTP with AceQL HTTP.
* Copyright (C) 2020, KawanSoft SAS
* (http://www.kawansoft.com). All rights reserved.
*
* Licensed 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 com.aceql.client.jdbc;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement;
import org.kawanfw.driver.util.FrameworkFileUtil;
import com.aceql.client.jdbc.http.AceQLHttpApi;
import com.aceql.client.jdbc.util.AceQLTypes;
import com.aceql.client.jdbc.util.json.PrepStatementParametersBuilder;
import com.aceql.client.jdbc.util.json.SqlParameter;
import com.aceql.client.jdbc.util.json.StreamResultAnalyzer;
/**
* @author Nicolas de Pomereu
*
*/
class AceQLPreparedStatement extends AbstractPreparedStatement implements PreparedStatement {
private static boolean DEBUG = false;
private AceQLConnection aceQLConnection = null;
private String sql = null;
private List localResultSetFiles = new ArrayList();
private List localInputStreams = new ArrayList();
private List localBlobIds = new ArrayList();
private List localLengths = new ArrayList();
/** The Http instance that does all Http stuff */
private AceQLHttpApi aceQLHttpApi = null;
protected PrepStatementParametersBuilder builder = new PrepStatementParametersBuilder();
/** is set to true if CallableStatement */
protected boolean isStoredProcedure = false;
/**
* Constructor
*
* @param aceQLConnection
* the Connection to the the remote database
* @param sql
* an SQL statement that may contain one or more '?' IN parameter
* placeholders
*/
public AceQLPreparedStatement(AceQLConnection aceQLConnection, String sql) throws SQLException {
super(sql);
this.aceQLConnection = aceQLConnection;
this.aceQLHttpApi = aceQLConnection.aceQLHttpApi;
this.sql = sql;
}
/*
* (non-Javadoc)
*
* @see org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#setNull(int,
* int)
*/
@Override
public void setNull(int parameterIndex, int sqlType) throws SQLException {
builder.setInParameter(parameterIndex, AceQLTypes.TYPE_NULL + sqlType, null);
}
/*
* (non-Javadoc)
*
* @see org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#setBoolean
* (int, boolean)
*/
@Override
public void setBoolean(int parameterIndex, boolean x) throws SQLException {
// builder.setParameter(parameterIndex, AceQLTypes.BIT, new
// Boolean(x).toString());
builder.setInParameter(parameterIndex, AceQLTypes.BIT, "" + x);
}
/*
* (non-Javadoc)
*
* @see
* org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#setShort(int,
* short)
*/
@Override
public void setShort(int parameterIndex, short x) throws SQLException {
// builder.setParameter(parameterIndex, AceQLTypes.TINYINT, new
// Short(x).toString());
builder.setInParameter(parameterIndex, AceQLTypes.TINYINT, "" + x);
}
/*
* (non-Javadoc)
*
* @see org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#setInt(int,
* int)
*/
@Override
public void setInt(int parameterIndex, int x) throws SQLException {
// builder.setParameter(parameterIndex, AceQLTypes.INTEGER, new
// Integer(x).toString());
builder.setInParameter(parameterIndex, AceQLTypes.INTEGER, "" + x);
}
/*
* (non-Javadoc)
*
* @see org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#setLong(int,
* long)
*/
@Override
public void setLong(int parameterIndex, long x) throws SQLException {
// builder.setParameter(parameterIndex, AceQLTypes.BIGINT, new
// Long(x).toString());
builder.setInParameter(parameterIndex, AceQLTypes.BIGINT, "" + x);
}
/*
* (non-Javadoc)
*
* @see
* org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#setFloat(int,
* float)
*/
@Override
public void setFloat(int parameterIndex, float x) throws SQLException {
// builder.setParameter(parameterIndex, AceQLTypes.REAL, new
// Float(x).toString());
builder.setInParameter(parameterIndex, AceQLTypes.REAL, "" + x);
}
/*
* (non-Javadoc)
*
* @see org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#setDouble
* (int, double)
*/
@Override
public void setDouble(int parameterIndex, double x) throws SQLException {
// builder.setParameter(parameterIndex, AceQLTypes.DOUBLE_PRECISION, new
// Double(x).toString());
builder.setInParameter(parameterIndex, AceQLTypes.DOUBLE_PRECISION, "" + x);
}
/*
* (non-Javadoc)
*
* @see
* org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#setBigDecimal
* (int, java.math.BigDecimal)
*/
@Override
public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
builder.setInParameter(parameterIndex, AceQLTypes.DOUBLE_PRECISION, x.toString());
}
/*
* (non-Javadoc)
*
* @see org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#setString
* (int, java.lang.String)
*/
@Override
public void setString(int parameterIndex, String x) throws SQLException {
builder.setInParameter(parameterIndex, AceQLTypes.VARCHAR, x);
}
/*
* (non-Javadoc)
*
* @see org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#setDate(int,
* java.sql.Date)
*/
@Override
public void setDate(int parameterIndex, Date x) throws SQLException {
// builder.setParameter(parameterIndex, AceQLTypes.DATE, new
// Long(x.getTime()).toString());
builder.setInParameter(parameterIndex, AceQLTypes.DATE, "" + x.getTime());
}
/*
* (non-Javadoc)
*
* @see org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#setTime(int,
* java.sql.Time)
*/
@Override
public void setTime(int parameterIndex, Time x) throws SQLException {
// builder.setParameter(parameterIndex, AceQLTypes.TIME, new
// Long(x.getTime()).toString());
builder.setInParameter(parameterIndex, AceQLTypes.TIME, "" + x.getTime());
}
/*
* (non-Javadoc)
*
* @see org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#setTimestamp
* (int, java.sql.Timestamp)
*/
@Override
public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
// builder.setParameter(parameterIndex, AceQLTypes.TIMESTAMP, new
// Long(x.getTime()).toString());
builder.setInParameter(parameterIndex, AceQLTypes.TIMESTAMP, "" + x.getTime());
}
/*
* (non-Javadoc)
*
* @see
* org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#setArray(int,
* java.sql.Array)
*/
// @Override
// public void setArray(int iParam, Array x) throws SQLException {
// // TODO Auto-generated method stub
// super.setArray(iParam, x);
// }
/*
* (non-Javadoc)
*
* @see org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#setURL(int,
* java.net.URL)
*/
@Override
public void setURL(int parameterIndex, URL x) throws SQLException {
builder.setInParameter(parameterIndex, AceQLTypes.URL, x.toString());
}
/*
* (non-Javadoc)
*
* @see org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#
* setBinaryStream( int, java.io.InputStream, int)
*/
@Override
public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException {
setBinaryStream(parameterIndex, x, (long) length);
}
/*
* (non-Javadoc)
*
* @see org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#
* setBinaryStream( int, java.io.InputStream)
*/
@Override
public void setBinaryStream(int parameterIndex, InputStream inputStream) throws SQLException {
setBinaryStream(parameterIndex, inputStream, (long) 0);
}
/*
* (non-Javadoc)
*
* @see org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#
* setBinaryStream (int, java.io.InputStream, long)
*/
@Override
public void setBinaryStream(int parameterIndex, InputStream inputStream, long length) throws SQLException {
if (inputStream != null) {
String blobId = buildBlobIdFile().getName();
builder.setInParameter(parameterIndex, AceQLTypes.BLOB, blobId);
localInputStreams.add(inputStream);
localBlobIds.add(blobId);
localLengths.add(length);
} else {
builder.setInParameter(parameterIndex, AceQLTypes.BLOB, null);
}
}
private static File buildBlobIdFile() {
File file = new File(FrameworkFileUtil.getKawansoftTempDir() + File.separator + "pc-blob-out-"
+ FrameworkFileUtil.getUniqueId() + ".txt");
return file;
}
/*
* (non-Javadoc)
*
* @see
* org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#executeUpdate ()
*/
@Override
public int executeUpdate() throws SQLException {
long totalLength = 0;
for (Long length : localLengths) {
totalLength += length;
}
for (int i = 0; i < localInputStreams.size(); i++) {
InputStream in = localInputStreams.get(i);
String blobId = localBlobIds.get(i);
aceQLHttpApi.blobUpload(blobId, in, totalLength);
}
boolean isPreparedStatement = true;
Map statementParameters = builder.getHttpFormattedStatementParameters();
Map callableOutParameters = builder.getCallableOutParameters();
return aceQLHttpApi.executeUpdate(sql, isPreparedStatement, isStoredProcedure, statementParameters,
callableOutParameters);
}
/*
* (non-Javadoc)
*
* @see org.kawanfw.driver.jdbc.abstracts.AbstractPreparedStatement#executeQuery
* ()
*/
@Override
public ResultSet executeQuery() throws SQLException {
try {
File file = AceQLStatement.buildtResultSetFile();
this.localResultSetFiles.add(file);
aceQLHttpApi.trace("file: " + file);
aceQLHttpApi.trace("gzipResult: " + aceQLHttpApi.isGzipResult());
boolean isPreparedStatement = true;
Map statementParameters = builder.getHttpFormattedStatementParameters();
try (InputStream in = aceQLHttpApi.executeQuery(sql, isPreparedStatement, isStoredProcedure,
statementParameters); OutputStream out = new BufferedOutputStream(new FileOutputStream(file));) {
if (in != null) {
// Do not use resource try {} ==> We don't want to create an
// empty file
InputStream inFinal = AceQLStatement.getFinalInputStream(in, aceQLHttpApi.isGzipResult());
IOUtils.copy(inFinal, out);
}
}
int httpStatusCode = aceQLHttpApi.getHttpStatusCode();
StreamResultAnalyzer streamResultAnalyzer = new StreamResultAnalyzer(file, httpStatusCode,
aceQLHttpApi.getHttpStatusMessage());
if (!streamResultAnalyzer.isStatusOk()) {
throw new AceQLException(streamResultAnalyzer.getErrorMessage(), streamResultAnalyzer.getErrorId(),
null, streamResultAnalyzer.getStackTrace(), httpStatusCode);
}
if (isStoredProcedure) {
Map callableOutParameters = builder.getCallableOutParameters();
debug("callableOutParameters: " + callableOutParameters);
updateOutParameters(streamResultAnalyzer, callableOutParameters);
}
/*
if (DEBUG) {
String fileContent = FileUtils.readFileToString(file, Charset.forName("UTF-8"));
debug(fileContent);
}
*/
int rowCount = streamResultAnalyzer.getRowCount();
AceQLResultSet aceQLResultSet = new AceQLResultSet(file, this, rowCount);
return aceQLResultSet;
} catch (Exception e) {
if (e instanceof AceQLException) {
throw (AceQLException) e;
} else {
throw new AceQLException(e.getMessage(), 0, e, null, aceQLHttpApi.getHttpStatusCode());
}
}
}
private void updateOutParameters(StreamResultAnalyzer streamResultAnalyzer,
Map callableOutParameters) throws SQLException {
// Immediate return in case no parameters
if (callableOutParameters == null || callableOutParameters.isEmpty()) {
return;
}
Map parametersOutPerIndexAfterExecute = streamResultAnalyzer.getParametersOutPerIndex();
debug("parametersOutPerIndexAfterExecute: " + parametersOutPerIndexAfterExecute);
// Immediate return in case no parameters. This can not happen if
// callableOutParameters is not empty
if (parametersOutPerIndexAfterExecute == null || parametersOutPerIndexAfterExecute.isEmpty()) {
throw new AceQLException("No stored procedure out parameters returned by AceQL Server", 4, null, null,
HttpURLConnection.HTTP_OK);
}
for (Integer key : callableOutParameters.keySet()) {
if (parametersOutPerIndexAfterExecute.containsKey(key)) {
SqlParameter sqlParameter = callableOutParameters.get(key);
SqlParameter sqlParameterNew = new SqlParameter(key, sqlParameter.getParameterType(),
parametersOutPerIndexAfterExecute.get(key));
// Put back new value
callableOutParameters.put(key, sqlParameterNew);
}
}
debug("callableOutParameters after execute: " + callableOutParameters);
}
/*
* (non-Javadoc)
*
* @see org.kawanfw.driver.jdbc.abstracts.AbstractStatement#getConnection()
*/
@Override
public Connection getConnection() throws SQLException {
return this.aceQLConnection;
}
/*
* (non-Javadoc)
*
* @see org.kawanfw.driver.jdbc.abstracts.AbstractStatement#close()
*/
@Override
public void close() throws SQLException {
for (File file : localResultSetFiles) {
file.delete();
}
}
private void debug(String s) {
if (DEBUG) {
System.out.println(new java.util.Date() + " " + s);
}
}
}