Skip to content

Commit 0623c3a

Browse files
excel driver probes cells belonging to columns in order to recognize their common value type; columns are nullable now
1 parent 097aaf2 commit 0623c3a

10 files changed

+364
-73
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ publishing {
5050
maven(MavenPublication) {
5151
groupId = 'de.inetsoftware'
5252
artifactId = 'exceljconnect'
53-
version = '1.2'
53+
version = '1.3'
5454
from components.java
5555
pom {
5656
name = 'Excel driver for Java'

src/com/inet/excel/ExcelDatabaseMetaData.java

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Objects;
2929

3030
import com.inet.excel.parser.ExcelParser;
31+
import com.inet.excel.parser.ValueType;
3132

3233
/** Implementation of {@link ResultSetMetaData} for {@link ExcelConnection}
3334
*/
@@ -50,6 +51,48 @@ public ExcelDatabaseMetaData( ExcelParser parser ) {
5051
this.parser = parser;
5152
}
5253

54+
/** Returns constant value that identifies the generic SQL type matching specified {@link ValueType}.
55+
* @param type type to return constant value for. Must not be null.
56+
* @return constant value that identifies the generic SQL type matching specified {@link ValueType}.
57+
*/
58+
static int getDataType( ValueType type ) {
59+
switch( type ) {
60+
case DATE:
61+
return Types.DATE;
62+
case NUMBER:
63+
return Types.NUMERIC;
64+
case TIME:
65+
return Types.TIME;
66+
case TIMESTAMP:
67+
return Types.TIMESTAMP;
68+
case VARCHAR:
69+
return Types.VARCHAR;
70+
default:
71+
return Types.JAVA_OBJECT;
72+
}
73+
}
74+
75+
/** Returns name that identifies the generic SQL type matching specified {@link ValueType}.
76+
* @param type type to return constant value for. Must not be null.
77+
* @return name that identifies the generic SQL type matching specified {@link ValueType}.
78+
*/
79+
static String getDataTypeName( ValueType type ) {
80+
switch( type ) {
81+
case DATE:
82+
return "DATE";
83+
case NUMBER:
84+
return "NUMERIC";
85+
case TIME:
86+
return "TIME";
87+
case TIMESTAMP:
88+
return "TIMESTAMP";
89+
case VARCHAR:
90+
return "VARCHAR";
91+
default:
92+
return "JAVA_OBJECT";
93+
}
94+
}
95+
5396
/**
5497
* {@inheritDoc}
5598
*/
@@ -1094,8 +1137,11 @@ public ResultSet getProcedureColumns( String catalog, String schemaPattern, Stri
10941137
continue;
10951138
}
10961139

1140+
List<ValueType> columnTypes = parser.getColumnTypes( sheetName );
10971141
int colIndex = 0;
10981142
for( String colName : parser.getColumnNames( sheetName ) ) {
1143+
ValueType valueType = columnTypes.get( colIndex );
1144+
10991145
colIndex++; // indexing starts with 1
11001146

11011147
List<Object> row = new ArrayList<>();
@@ -1104,20 +1150,20 @@ public ResultSet getProcedureColumns( String catalog, String schemaPattern, Stri
11041150
row.add( sheetName );
11051151
row.add( colName );
11061152
row.add( Integer.valueOf( DatabaseMetaData.procedureColumnResult ) );
1107-
row.add( Integer.valueOf( Types.JAVA_OBJECT ) );
1108-
row.add( "JAVA_OBJECT" );
1153+
row.add( Integer.valueOf( getDataType( valueType ) ) );
1154+
row.add( getDataTypeName( valueType ) );
11091155
row.add( Integer.valueOf( 0 ) );
11101156
row.add( Integer.valueOf( COLUMN_SIZE_IN_BYTES ) );
11111157
row.add( Integer.valueOf( COLUMN_SIZE_IN_BYTES ) );
11121158
row.add( null );
1113-
row.add( Integer.valueOf( DatabaseMetaData.procedureNoNulls ) );
1159+
row.add( Integer.valueOf( DatabaseMetaData.procedureNullable ) );
11141160
row.add( "" );
11151161
row.add( "" );
11161162
row.add( null );
11171163
row.add( null );
11181164
row.add( Integer.valueOf( COLUMN_SIZE_IN_BYTES ) );
11191165
row.add( Integer.valueOf( colIndex ) );
1120-
row.add( "NO" );
1166+
row.add( "YES" );
11211167
row.add( sheetName );
11221168

11231169
allRows.add( row );

src/com/inet/excel/ExcelDriver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class ExcelDriver implements Driver {
3535
public static final String URL_PREFIX = "jdbc:inetexcel:";
3636
public static final String DRIVER_NAME = "inetexcel";
3737
public static final int MAJOR_VERSION = 1;
38-
public static final int MINOR_VERSION = 2;
38+
public static final int MINOR_VERSION = 3;
3939

4040
/** Throws exception indicating that requested operation is not supported.
4141
* @throws SQLException exception indicating that requested operation is not supported.

src/com/inet/excel/ExcelSheetResultSet.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121

2222
import com.inet.excel.parser.ExcelParser;
23+
import com.inet.excel.parser.ValueType;
2324

2425
/** Class for result set used to retrieve data of the sheet from Excel document.
2526
*/
@@ -50,7 +51,8 @@ public ExcelSheetResultSet( ExcelParser parser, String sheetName, int maxRowsPer
5051
this.parser = parser;
5152
this.sheetName = sheetName;
5253
this.maxRowsPerBatch = maxRowsPerBatch;
53-
this.metaData = new ExcelSheetResultSetMetaData( parser.getFileName(), sheetName, getColumnNames() );
54+
List<ValueType> columnTypes = parser.getColumnTypes( sheetName );
55+
this.metaData = new ExcelSheetResultSetMetaData( parser.getFileName(), sheetName, getColumnNames(), columnTypes );
5456
this.rowCount = parser.getRowCount( sheetName );
5557
this.currentRowIndex = -1;
5658
this.currentBatchIndex = -1;

src/com/inet/excel/ExcelSheetResultSetMetaData.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,25 @@
2020
import java.sql.Types;
2121
import java.util.List;
2222

23+
import com.inet.excel.parser.ValueType;
24+
2325
/** Implementation of {@link ResultSetMetaData} for {@link ExcelSheetResultSet}.
2426
*/
2527
public class ExcelSheetResultSetMetaData implements ResultSetMetaData {
2628

2729
private String fileName;
2830
private String sheetName;
2931
private List<String> columnNames;
32+
private List<ValueType> columnTypes;
3033

3134
/** Constructor of the class.
3235
* @param fileName file name of the Excel document.
3336
* @param sheetName name of the sheet from Excel document.
3437
* @param columnNames list of column names.
38+
* @param columnTypes list of column types.
3539
* @throws IllegalArgumentException if any of given arguments is null.
3640
*/
37-
public ExcelSheetResultSetMetaData( String fileName, String sheetName, List<String> columnNames ) {
41+
public ExcelSheetResultSetMetaData( String fileName, String sheetName, List<String> columnNames, List<ValueType> columnTypes ) {
3842
if( fileName == null ) {
3943
throw new IllegalArgumentException( "file name must not be null" );
4044
}
@@ -44,9 +48,13 @@ public ExcelSheetResultSetMetaData( String fileName, String sheetName, List<Stri
4448
if( columnNames == null ) {
4549
throw new IllegalArgumentException( "list of column names must not be null" );
4650
}
51+
if( columnTypes == null ) {
52+
throw new IllegalArgumentException( "list of column types must not be null" );
53+
}
4754
this.fileName = fileName;
4855
this.sheetName = sheetName;
4956
this.columnNames = columnNames;
57+
this.columnTypes = columnTypes;
5058
}
5159

5260
/**
@@ -112,7 +120,7 @@ public boolean isCurrency( int column ) throws SQLException {
112120
*/
113121
@Override
114122
public int isNullable( int column ) throws SQLException {
115-
return columnNoNulls;
123+
return columnNullable;
116124
}
117125

118126
/**
@@ -192,15 +200,17 @@ public String getCatalogName( int column ) throws SQLException {
192200
*/
193201
@Override
194202
public int getColumnType( int column ) throws SQLException {
195-
return Types.JAVA_OBJECT;
203+
ValueType valueType = columnTypes.get( column - 1 );
204+
return ExcelDatabaseMetaData.getDataType( valueType );
196205
}
197206

198207
/**
199208
* {@inheritDoc}
200209
*/
201210
@Override
202211
public String getColumnTypeName( int column ) throws SQLException {
203-
return "JAVA_OBJECT";
212+
ValueType valueType = columnTypes.get( column - 1 );
213+
return ExcelDatabaseMetaData.getDataTypeName( valueType );
204214
}
205215

206216
/**
@@ -232,6 +242,19 @@ public boolean isDefinitelyWritable( int column ) throws SQLException {
232242
*/
233243
@Override
234244
public String getColumnClassName( int column ) throws SQLException {
235-
return String.class.getName(); //TODO
245+
ValueType valueType = columnTypes.get( column - 1 );
246+
switch( valueType ) {
247+
case DATE:
248+
return java.util.Date.class.getName();
249+
case NUMBER:
250+
return Double.class.getName();
251+
case TIME:
252+
return java.sql.Time.class.getName();
253+
case TIMESTAMP:
254+
return java.sql.Timestamp.class.getName();
255+
case VARCHAR:
256+
default:
257+
return String.class.getName();
258+
}
236259
}
237260
}

0 commit comments

Comments
 (0)