org.fugerit.java.tool.sql.ExportQuery Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fj-tool Show documentation
Show all versions of fj-tool Show documentation
Fugerit Java Tool Library for JDK 1.8 and up
The newest version!
/**
*
*/
package org.fugerit.java.tool.sql;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Properties;
import org.fugerit.java.core.db.connect.ConnectionFactory;
import org.fugerit.java.core.db.connect.ConnectionFactoryImpl;
import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.lang.helpers.StringUtils;
import org.fugerit.java.tool.ToolHandlerHelper;
import lombok.extern.slf4j.Slf4j;
/**
* @author mttfranci
*
*/
@Slf4j
public class ExportQuery extends ToolHandlerHelper {
public static final String ARG_QUERY = "query";
public static final String ARG_OUTPUT = "output";
public static final String ARG_FORMAT = "format";
public static final String ARG_FORMAT_DEFAULT = "html";
private void openFile( PrintWriter pw, String format ) {
log.trace( "format : {}", format );
pw.println( "" );
}
private void closeFile( PrintWriter pw, String format ) {
log.trace( "format : {}", format );
pw.println( "
" );
}
private void addRecord( PrintWriter pw, String[] currentRecord, boolean header ) {
pw.println( "" );
String openTag = "";
String closeTag = " ";
if ( header ) {
openTag = "";
closeTag = " ";
}
for ( int k=0; k" );
}
private ConnectionFactory getCf( Properties params, ClassLoader cl ) {
return SafeFunction.get( () -> ConnectionFactoryImpl.newInstance( params, null, cl ) );
}
/* (non-Javadoc)
* @see org.fugerit.java.tool.ToolHandlerHelper#handleWorker(java.util.Properties)
*/
@Override
public int handleWorker(Properties params) {
ClassLoader cl = this.getClassLoader( params );
ConnectionFactory cf = this.getCf(params, cl);
String output = params.getProperty( ARG_OUTPUT );
return SafeFunction.get( () -> {
try (
PrintWriter pw = new PrintWriter( new OutputStreamWriter( new FileOutputStream( output ) ) );
Connection conn = cf.getConnection();
Statement stm = conn.createStatement() ) {
String sql = params.getProperty( ARG_QUERY );
String format = params.getProperty( ARG_FORMAT, ARG_FORMAT_DEFAULT );
openFile( pw, format );
try ( ResultSet rs = stm.executeQuery( sql ) ) {
ResultSetMetaData rsmd = rs.getMetaData();
String[] head = new String[rsmd.getColumnCount()];
for ( int k=0; k
© 2015 - 2024 Weber Informatics LLC | Privacy Policy