Commit 3cdc7271 authored by R.W.Majeed's avatar R.W.Majeed

output SQL with virtual JDBC connection

parent ace2a33f
package de.sekmi.histream.i2b2.ont;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.sql.SQLException;
import java.util.Map;
import java.util.function.Consumer;
import de.sekmi.histream.i2b2.sql.VirtualConnection;
import de.sekmi.histream.ontology.Ontology;
import de.sekmi.histream.ontology.OntologyException;
public class SQLGenerator implements Closeable{
private Import imp;
private BufferedWriter metaWriter;
private BufferedWriter dataWriter;
VirtualConnection metaCon;
VirtualConnection dataCon;
public SQLGenerator(BufferedWriter metaWriter, BufferedWriter dataWriter, Map<String,String> config) throws IOException, SQLException{
this.metaWriter = metaWriter;
this.dataWriter = dataWriter;
this.metaCon = new VirtualConnection(this::writeMetaSQL);
this.dataCon = new VirtualConnection(this::writeDataSQL);
this.imp = new Import(metaCon, dataCon, config);
}
public SQLGenerator(Path metaSQL, Path dataSQL, Map<String,String> config, boolean overwrite) throws IOException, SQLException{
this(Files.newBufferedWriter(metaSQL, StandardCharsets.UTF_8, StandardOpenOption.WRITE, StandardOpenOption.CREATE),//, overwrite?StandardOpenOption.TRUNCATE_EXISTING:StandardOpenOption.CREATE_NEW),
Files.newBufferedWriter(dataSQL, StandardCharsets.UTF_8, StandardOpenOption.WRITE, StandardOpenOption.CREATE),//, overwrite?StandardOpenOption.TRUNCATE_EXISTING:StandardOpenOption.CREATE_NEW),
config);
}
private void writeMetaSQL(String sql){
try {
metaWriter.write(sql);
metaWriter.write(';');
metaWriter.newLine();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private void writeDataSQL(String sql){
try {
dataWriter.write(sql);
dataWriter.write(';');
dataWriter.newLine();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public void setWarningHandler(Consumer<String> handler){
imp.setWarningHandler(handler);
}
public void writeOntologySQL(Ontology ontology) throws SQLException, OntologyException{
imp.setOntology(ontology);
imp.processOntology();
}
@Override
public void close() throws IOException {
metaWriter.close();
dataWriter.close();
}
}
package de.sekmi.histream.i2b2.sql;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
public class VirtualConnection implements Connection{
private Consumer<String> action;
public VirtualConnection(Consumer<String> sqlAction){
this.action = sqlAction;
}
void output(String sql){
this.action.accept(sql);
}
@Override
public boolean isWrapperFor(Class<?> arg0) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public <T> T unwrap(Class<T> arg0) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void abort(Executor executor) throws SQLException {
}
@Override
public void clearWarnings() throws SQLException {
}
@Override
public void close() throws SQLException {
// nothing to do
}
@Override
public void commit() throws SQLException {
}
@Override
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public Blob createBlob() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public Clob createClob() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public NClob createNClob() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public SQLXML createSQLXML() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public Statement createStatement() throws SQLException {
return new VirtualStatement(this);
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
return createStatement();
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
return createStatement();
}
@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public boolean getAutoCommit() throws SQLException {
return false;
}
@Override
public String getCatalog() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public Properties getClientInfo() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public String getClientInfo(String name) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public int getHoldability() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public DatabaseMetaData getMetaData() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public int getNetworkTimeout() throws SQLException {
return 0;
}
@Override
public String getSchema() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public int getTransactionIsolation() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public Map<String, Class<?>> getTypeMap() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public SQLWarning getWarnings() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public boolean isClosed() throws SQLException {
return false;
}
@Override
public boolean isReadOnly() throws SQLException {
return false;
}
@Override
public boolean isValid(int timeout) throws SQLException {
return true;
}
@Override
public String nativeSQL(String sql) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public CallableStatement prepareCall(String sql) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
return new VirtualPreparedStatement(sql, this);
}
@Override
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
return prepareStatement(sql);
}
@Override
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
return prepareStatement(sql);
}
@Override
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
return prepareStatement(sql);
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
return prepareStatement(sql);
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
return prepareStatement(sql);
}
@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
}
@Override
public void rollback() throws SQLException {
}
@Override
public void rollback(Savepoint savepoint) throws SQLException {
}
@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
}
@Override
public void setCatalog(String catalog) throws SQLException {
}
@Override
public void setClientInfo(Properties properties) throws SQLClientInfoException {
}
@Override
public void setClientInfo(String name, String value) throws SQLClientInfoException {
}
@Override
public void setHoldability(int holdability) throws SQLException {
}
@Override
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
}
@Override
public void setReadOnly(boolean readOnly) throws SQLException {
}
@Override
public Savepoint setSavepoint() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public Savepoint setSavepoint(String name) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setSchema(String schema) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setTransactionIsolation(int level) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
throw new UnsupportedOperationException();
}
}
package de.sekmi.histream.i2b2.sql;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.NClob;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Calendar;
public class VirtualPreparedStatement extends VirtualStatement implements PreparedStatement {
public String[] sqlFragments;
public String[] sqlValues;
public VirtualPreparedStatement(String sql, VirtualConnection connection) {
super(connection);
sqlFragments = sql.split("\\?");
// if the SQL ends with ?, split will not produce a trailing empty fragment
// we need to add that manually
if( sql.endsWith("?") ){
sqlFragments = Arrays.copyOf(sqlFragments, sqlFragments.length+1);
sqlFragments[sqlFragments.length-1] = "";
}
sqlValues = new String[sqlFragments.length - 1];
}
@Override
public void addBatch() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void clearParameters() throws SQLException {
Arrays.fill(sqlValues, null);
}
@Override
public boolean execute() throws SQLException {
executeUpdate();
return false;
}
@Override
public ResultSet executeQuery() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public int executeUpdate() throws SQLException {
// build SQL
StringBuilder sb = new StringBuilder();
for( int i=0; i<sqlValues.length; i++ ){
sb.append(sqlFragments[i]);
if( sqlValues[i] == null ){
sb.append("NULL");
}else{
sb.append(sqlValues[i]);
}
}
// append last fragment
sb.append(sqlFragments[sqlValues.length]);
// execute
execute(sb.toString());
// always affect one virtual row
return 1;
}
@Override
public ResultSetMetaData getMetaData() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public ParameterMetaData getParameterMetaData() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setArray(int arg0, Array arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setAsciiStream(int arg0, InputStream arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setAsciiStream(int arg0, InputStream arg1, int arg2) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setAsciiStream(int arg0, InputStream arg1, long arg2) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setBigDecimal(int index, BigDecimal value) throws SQLException {
setObject(index, value);
}
@Override
public void setBinaryStream(int arg0, InputStream arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setBinaryStream(int arg0, InputStream arg1, int arg2) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setBinaryStream(int arg0, InputStream arg1, long arg2) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setBlob(int arg0, Blob arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setBlob(int arg0, InputStream arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setBlob(int arg0, InputStream arg1, long arg2) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setBoolean(int index, boolean value) throws SQLException {
setObject(index, value);
}
@Override
public void setByte(int arg0, byte arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setBytes(int arg0, byte[] arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setCharacterStream(int arg0, Reader arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setCharacterStream(int arg0, Reader arg1, int arg2) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setCharacterStream(int arg0, Reader arg1, long arg2) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setClob(int arg0, Clob arg1) throws SQLException {
try( Reader r = arg1.getCharacterStream() ){
setClob(arg0, r);
} catch (IOException e) {
throw new SQLException(e);
}
}
@Override
public void setClob(int arg0, Reader arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setClob(int arg0, Reader arg1, long arg2) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setDate(int arg0, Date arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setDate(int arg0, Date arg1, Calendar arg2) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setDouble(int arg0, double arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setFloat(int arg0, float arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setInt(int index, int value) throws SQLException {
setObject(index,value);
}
@Override
public void setLong(int index, long value) throws SQLException {
setObject(index,value);
}
@Override
public void setNCharacterStream(int arg0, Reader arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setNCharacterStream(int arg0, Reader arg1, long arg2) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setNClob(int arg0, NClob arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setNClob(int arg0, Reader arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setNClob(int arg0, Reader arg1, long arg2) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setNString(int arg0, String arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setNull(int index, int arg1) throws SQLException {
setObject(index, null);
}
@Override
public void setNull(int arg0, int arg1, String arg2) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setObject(int index, Object value) throws SQLException {
String str;
if( value == null ){
str = null;
}else if( value.getClass() == Boolean.class ){
str = ((Boolean)value)?"TRUE":"FALSE";
}else if( value instanceof Number ){
str = value.toString();
}else{
str = "'"+escapeString(value.toString())+"'";
}
sqlValues[index-1] = str;
}
@Override
public void setObject(int arg0, Object arg1, int arg2) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setObject(int arg0, Object arg1, int arg2, int arg3) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setRef(int arg0, Ref arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setRowId(int arg0, RowId arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setSQLXML(int arg0, SQLXML arg1) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setShort(int index, short value) throws SQLException {
setObject(index,value);
}
@Override
public void setString(int index, String value) throws SQLException {
setObject(index,value);
}