@ -6,6 +6,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet ;
import java.sql.ResultSet ;
import java.sql.ResultSetMetaData ;
import java.sql.ResultSetMetaData ;
import java.sql.SQLException ;
import java.sql.SQLException ;
import java.sql.SQLSyntaxErrorException ;
import java.sql.SQLWarning ;
import java.sql.SQLWarning ;
import java.sql.Statement ;
import java.sql.Statement ;
import java.sql.Types ;
import java.sql.Types ;
@ -19,6 +20,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import at.compax.tools.sql.model.QueryParameter ;
import at.compax.tools.sql.model.QueryParameter ;
import at.compax.tools.sql.model.QueryResult ;
import at.compax.tools.sql.model.QueryResult ;
import at.compax.tools.sql.model.QueryResult.ColumnDefinition ;
import at.compax.tools.sql.model.QueryResult.QueryResultBuilder ;
import at.compax.tools.sql.model.QueryResult.QueryResultBuilder ;
import at.compax.tools.sql.model.QueryResult.Row ;
import at.compax.tools.sql.model.QueryResult.Row ;
import at.compax.tools.sql.model.QueryResult.Row.Column ;
import at.compax.tools.sql.model.QueryResult.Row.Column ;
@ -91,8 +93,10 @@ public class SqlExecutor {
} else if ( object instanceof Long ) {
} else if ( object instanceof Long ) {
stmt . setLong ( parameterIndex , ( Long ) object ) ;
stmt . setLong ( parameterIndex , ( Long ) object ) ;
} else {
} else {
String message = "Unhandled paramter type: " + object . getClass ( ) . getName ( ) ;
stmt . setString ( parameterIndex , ( String ) object ) ;
throw new IllegalArgumentException ( message ) ;
String message = String . format ( "Unhandled paramter type <%s>. Defaulting to String" ,
object . getClass ( ) . getName ( ) ) ;
log . warn ( message ) ;
}
}
} else {
} else {
log . warn ( "parameter is null" ) ;
log . warn ( "parameter is null" ) ;
@ -123,15 +127,18 @@ public class SqlExecutor {
List < Row > rows = new Vector < Row > ( ) ;
List < Row > rows = new Vector < Row > ( ) ;
while ( rs . next ( ) ) {
while ( rs . next ( ) ) {
ResultSetMetaData metaData = rs . getMetaData ( ) ;
if ( rs . isFirst ( ) ) {
queryResultBuilder . columnDefinitions ( getColumnDefinitions ( metaData ) ) ;
}
RowBuilder rowBuilder = Row . builder ( ) ;
RowBuilder rowBuilder = Row . builder ( ) ;
List < Column > columns = new Vector < Column > ( ) ;
List < Column > columns = new Vector < Column > ( ) ;
ResultSetMetaData metaData = rs . getMetaData ( ) ;
int columnCount = metaData . getColumnCount ( ) ;
int columnCount = metaData . getColumnCount ( ) ;
for ( int columnIndex = 1 ; columnIndex < = columnCount ; columnIndex + + ) {
for ( int columnIndex = 1 ; columnIndex < = columnCount ; columnIndex + + ) {
ColumnBuilder columnBuilder = Column . builder ( ) ;
ColumnBuilder columnBuilder = Column . builder ( ) ;
columnBuilder . name ( metaData . getColumnLabel ( columnIndex ) ) ;
int columnType = metaData . getColumnType ( columnIndex ) ;
int columnType = metaData . getColumnType ( columnIndex ) ;
if ( columnType = = Types . VARCHAR ) {
if ( columnType = = Types . VARCHAR ) {
@ -139,8 +146,8 @@ public class SqlExecutor {
} else if ( columnType = = Types . NUMERIC ) {
} else if ( columnType = = Types . NUMERIC ) {
columnBuilder . value ( rs . getLong ( columnIndex ) ) ;
columnBuilder . value ( rs . getLong ( columnIndex ) ) ;
} else {
} else {
String message = "Unhandled column type type: " + columnType ;
String message = String . format ( "Unhandled column type <%s>. Defaulting to String" , rs . getMetaData ( ) . getColumnTypeName ( columnIndex ) ) ;
throw new IllegalArgumentExceptio n( message ) ;
log . war n( message ) ;
}
}
columns . add ( columnBuilder . build ( ) ) ;
columns . add ( columnBuilder . build ( ) ) ;
@ -153,6 +160,21 @@ public class SqlExecutor {
return queryResultBuilder . rows ( rows ) . build ( ) ;
return queryResultBuilder . rows ( rows ) . build ( ) ;
}
}
private static Vector < ColumnDefinition > getColumnDefinitions ( ResultSetMetaData metaData ) throws SQLException {
Vector < ColumnDefinition > columnDefinitions = new Vector < ColumnDefinition > ( ) ;
int columnCount = metaData . getColumnCount ( ) ;
for ( int columnIndex = 1 ; columnIndex < = columnCount ; columnIndex + + ) {
columnDefinitions . add ( ColumnDefinition . builder ( ) //
. index ( columnIndex ) //
. name ( metaData . getColumnLabel ( columnIndex ) ) //
. type ( metaData . getColumnTypeName ( columnIndex ) ) //
. build ( ) ) ;
}
return columnDefinitions ;
}
private static void handleUserErrors ( Connection conn , String sql , Statement stmt )
private static void handleUserErrors ( Connection conn , String sql , Statement stmt )
throws SQLException , SqlExecutionException {
throws SQLException , SqlExecutionException {
SQLWarning warnings = stmt . getWarnings ( ) ;
SQLWarning warnings = stmt . getWarnings ( ) ;
@ -165,6 +187,7 @@ public class SqlExecutor {
}
}
private static List < UserError > parseUserErrors ( Connection conn ) throws SQLException {
private static List < UserError > parseUserErrors ( Connection conn ) throws SQLException {
log . debug ( "Retrieving user errors" ) ;
String userErrorsSql = "select name, type, sequence, line, position, text, attribute, message_number from user_errors where type <> 'JAVA CLASS' order by sequence" ;
String userErrorsSql = "select name, type, sequence, line, position, text, attribute, message_number from user_errors where type <> 'JAVA CLASS' order by sequence" ;
boolean first = true ;
boolean first = true ;