You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

352 lines
12 KiB

package com.seaweedfs.jdbc;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.*;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
/**
* PreparedStatement implementation for SeaweedFS JDBC
*/
public class SeaweedFSPreparedStatement extends SeaweedFSStatement implements PreparedStatement {
private final String originalSql;
private final Map<Integer, Object> parameters = new HashMap<>();
public SeaweedFSPreparedStatement(SeaweedFSConnection connection, String sql) {
super(connection);
this.originalSql = sql;
}
@Override
public ResultSet executeQuery() throws SQLException {
return executeQuery(buildSqlWithParameters());
}
@Override
public int executeUpdate() throws SQLException {
return executeUpdate(buildSqlWithParameters());
}
@Override
public void setNull(int parameterIndex, int sqlType) throws SQLException {
checkClosed();
parameters.put(parameterIndex, null);
}
@Override
public void setBoolean(int parameterIndex, boolean x) throws SQLException {
checkClosed();
parameters.put(parameterIndex, x);
}
@Override
public void setByte(int parameterIndex, byte x) throws SQLException {
checkClosed();
parameters.put(parameterIndex, x);
}
@Override
public void setShort(int parameterIndex, short x) throws SQLException {
checkClosed();
parameters.put(parameterIndex, x);
}
@Override
public void setInt(int parameterIndex, int x) throws SQLException {
checkClosed();
parameters.put(parameterIndex, x);
}
@Override
public void setLong(int parameterIndex, long x) throws SQLException {
checkClosed();
parameters.put(parameterIndex, x);
}
@Override
public void setFloat(int parameterIndex, float x) throws SQLException {
checkClosed();
parameters.put(parameterIndex, x);
}
@Override
public void setDouble(int parameterIndex, double x) throws SQLException {
checkClosed();
parameters.put(parameterIndex, x);
}
@Override
public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
checkClosed();
parameters.put(parameterIndex, x);
}
@Override
public void setString(int parameterIndex, String x) throws SQLException {
checkClosed();
parameters.put(parameterIndex, x);
}
@Override
public void setBytes(int parameterIndex, byte[] x) throws SQLException {
checkClosed();
parameters.put(parameterIndex, x);
}
@Override
public void setDate(int parameterIndex, Date x) throws SQLException {
checkClosed();
parameters.put(parameterIndex, x);
}
@Override
public void setTime(int parameterIndex, Time x) throws SQLException {
checkClosed();
parameters.put(parameterIndex, x);
}
@Override
public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
checkClosed();
parameters.put(parameterIndex, x);
}
@Override
public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException {
throw new SQLFeatureNotSupportedException("ASCII streams are not supported");
}
@Override
public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException {
throw new SQLFeatureNotSupportedException("Unicode streams are not supported");
}
@Override
public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException {
throw new SQLFeatureNotSupportedException("Binary streams are not supported");
}
@Override
public void clearParameters() throws SQLException {
checkClosed();
parameters.clear();
}
@Override
public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {
setObject(parameterIndex, x);
}
@Override
public void setObject(int parameterIndex, Object x) throws SQLException {
checkClosed();
parameters.put(parameterIndex, x);
}
@Override
public boolean execute() throws SQLException {
return execute(buildSqlWithParameters());
}
@Override
public void addBatch() throws SQLException {
checkClosed();
addBatch(buildSqlWithParameters());
}
@Override
public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException {
throw new SQLFeatureNotSupportedException("Character streams are not supported");
}
@Override
public void setRef(int parameterIndex, Ref x) throws SQLException {
throw new SQLFeatureNotSupportedException("Ref objects are not supported");
}
@Override
public void setBlob(int parameterIndex, Blob x) throws SQLException {
throw new SQLFeatureNotSupportedException("Blob objects are not supported");
}
@Override
public void setClob(int parameterIndex, Clob x) throws SQLException {
throw new SQLFeatureNotSupportedException("Clob objects are not supported");
}
@Override
public void setArray(int parameterIndex, Array x) throws SQLException {
throw new SQLFeatureNotSupportedException("Array objects are not supported");
}
@Override
public ResultSetMetaData getMetaData() throws SQLException {
throw new SQLFeatureNotSupportedException("Prepared statement metadata is not supported");
}
@Override
public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException {
setDate(parameterIndex, x);
}
@Override
public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException {
setTime(parameterIndex, x);
}
@Override
public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException {
setTimestamp(parameterIndex, x);
}
@Override
public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException {
setNull(parameterIndex, sqlType);
}
@Override
public void setURL(int parameterIndex, URL x) throws SQLException {
checkClosed();
parameters.put(parameterIndex, x.toString());
}
@Override
public ParameterMetaData getParameterMetaData() throws SQLException {
throw new SQLFeatureNotSupportedException("Parameter metadata is not supported");
}
@Override
public void setRowId(int parameterIndex, RowId x) throws SQLException {
throw new SQLFeatureNotSupportedException("RowId objects are not supported");
}
@Override
public void setNString(int parameterIndex, String value) throws SQLException {
setString(parameterIndex, value);
}
@Override
public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("NCharacter streams are not supported");
}
@Override
public void setNClob(int parameterIndex, NClob value) throws SQLException {
throw new SQLFeatureNotSupportedException("NClob objects are not supported");
}
@Override
public void setClob(int parameterIndex, Reader reader, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("Clob objects are not supported");
}
@Override
public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("Blob objects are not supported");
}
@Override
public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("NClob objects are not supported");
}
@Override
public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException {
throw new SQLFeatureNotSupportedException("SQLXML objects are not supported");
}
@Override
public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength) throws SQLException {
setObject(parameterIndex, x);
}
@Override
public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("ASCII streams are not supported");
}
@Override
public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("Binary streams are not supported");
}
@Override
public void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("Character streams are not supported");
}
@Override
public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException {
throw new SQLFeatureNotSupportedException("ASCII streams are not supported");
}
@Override
public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException {
throw new SQLFeatureNotSupportedException("Binary streams are not supported");
}
@Override
public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException {
throw new SQLFeatureNotSupportedException("Character streams are not supported");
}
@Override
public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException {
throw new SQLFeatureNotSupportedException("NCharacter streams are not supported");
}
@Override
public void setClob(int parameterIndex, Reader reader) throws SQLException {
throw new SQLFeatureNotSupportedException("Clob objects are not supported");
}
@Override
public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException {
throw new SQLFeatureNotSupportedException("Blob objects are not supported");
}
@Override
public void setNClob(int parameterIndex, Reader reader) throws SQLException {
throw new SQLFeatureNotSupportedException("NClob objects are not supported");
}
/**
* Build the final SQL string by replacing parameter placeholders with actual values
*/
private String buildSqlWithParameters() throws SQLException {
String sql = originalSql;
// Simple parameter substitution (not SQL-injection safe, but good enough for demo)
// In a production implementation, you would use proper parameter binding
for (Map.Entry<Integer, Object> entry : parameters.entrySet()) {
String placeholder = "\\?"; // Find first ? placeholder
String replacement;
Object value = entry.getValue();
if (value == null) {
replacement = "NULL";
} else if (value instanceof String) {
// Escape single quotes and wrap in quotes
replacement = "'" + value.toString().replace("'", "''") + "'";
} else if (value instanceof Number || value instanceof Boolean) {
replacement = value.toString();
} else if (value instanceof Date) {
replacement = "'" + value.toString() + "'";
} else if (value instanceof Timestamp) {
replacement = "'" + value.toString() + "'";
} else {
replacement = "'" + value.toString().replace("'", "''") + "'";
}
// Replace the first occurrence of ?
sql = sql.replaceFirst(placeholder, replacement);
}
return sql;
}
}