Skip to content

Commit 1904a0e

Browse files
committed
Added parameter tryColumnType
1 parent fd19691 commit 1904a0e

File tree

13 files changed

+134
-24
lines changed

13 files changed

+134
-24
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- param tryColumnType, will try to format column by type.
13+
1014
## [1.0.1] - 2024-07-16
1115

1216
### Added

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Tool export query in CSV and XLS / XLSX format
2424
<query id="Q001" sql="SELECT * FROM test_export" outputFormat="csv" csvSeparator=";" outputFile="target/catalog_test_001.csv"/>
2525
<query id="Q002" sql="SELECT * FROM test_export" outputFormat="csv" outputFile="target/catalog_test_002.csv"/>
2626
<query id="Q003" sql="SELECT * FROM test_export" outputFormat="html" outputFile="target/catalog_test_003.html" createPath="1"/>
27-
<query id="Q004" sql="SELECT * FROM test_export" outputFormat="xls" outputFile="target/catalog_test_004.xls" xlsResize="1"/>
27+
<query id="Q004" sql="SELECT * FROM test_export" outputFormat="xls" outputFile="target/catalog_test_004.xls" xlsResize="1" tryColumnType="1"/>
2828
<query id="Q005" sql="SELECT * FROM test_export" outputFormat="xlsx" outputFile="target/catalog_test_004.xlsx" xlsTemplate="src/test/resources/template/test_template.xlsx" />
2929
</query-catalog>
3030

src/main/java/org/fugerit/java/query/export/catalog/QueryConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,7 @@ public class QueryConfig extends BasicIdConfigType {
2929
@Getter @Setter private String xlsTemplate;
3030

3131
@Getter @Setter private String createPath;
32+
33+
@Getter @Setter private String tryColumnType;
3234

3335
}

src/main/java/org/fugerit/java/query/export/catalog/QueryConfigCatalog.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ public static void handle( Connection conn, QueryConfig queryConfig ) throws IOE
9696
} else {
9797
throw new ConfigRuntimeException( "Unsuppoorted format : "+format );
9898
}
99+
if ( StringUtils.isNotEmpty( queryConfig.getTryColumnType() ) ) {
100+
config.setTryColumnType( BooleanUtils.isTrue( queryConfig.getTryColumnType() ) );
101+
}
99102
config.getParams().putAll( params );
100103
QueryExportFacade.export(config);
101104
}

src/main/java/org/fugerit/java/query/export/facade/QueryExportConfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.sql.Connection;
55
import java.util.Properties;
66

7+
import lombok.Getter;
8+
import lombok.Setter;
79
import org.fugerit.java.query.export.meta.BasicObjectFormat;
810

911
public class QueryExportConfig {
@@ -47,6 +49,7 @@ public QueryExportConfig(String format, char separator, OutputStream output, Con
4749
this.query = query;
4850
this.exportHeader = exportHeader;
4951
this.params = new Properties();
52+
this.tryColumnType = false;
5053
}
5154

5255
private String format;
@@ -126,5 +129,14 @@ public BasicObjectFormat getObjectFormat() {
126129
public void setObjectFormat(BasicObjectFormat objectFormat) {
127130
this.objectFormat = objectFormat;
128131
}
132+
133+
/*
134+
* if set to true, by default :
135+
* string - is unchanged
136+
* number - dependent on outout format, for excel : #,###
137+
* date - dependent on outout format, for excel : m/d/yy h:mm
138+
*/
139+
@Getter @Setter
140+
private boolean tryColumnType;
129141

130142
}

src/main/java/org/fugerit/java/query/export/facade/QueryExportFacade.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ private QueryExportFacade() {}
3232
public static final String ARG_XLS_RESIZE = "xls-resize";
3333

3434
public static final String ARG_XLS_RESIZE_DEFAULT = "false";
35-
35+
3636
public static boolean registerHandler( String type ) {
3737
boolean ok = false;
3838
try {
39+
3940
QueryExportHandler handler = (QueryExportHandler)ClassHelper.newInstance( type );
4041
setGet( null, handler );
4142
ok = true;

src/main/java/org/fugerit/java/query/export/facade/format/QueryExportHandlerXLSBase.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
import java.io.FileInputStream;
55
import java.io.IOException;
66
import java.io.InputStream;
7+
import java.text.DecimalFormat;
8+
import java.text.NumberFormat;
79
import java.util.Iterator;
810

9-
import org.apache.poi.ss.usermodel.Cell;
10-
import org.apache.poi.ss.usermodel.Row;
11-
import org.apache.poi.ss.usermodel.Sheet;
12-
import org.apache.poi.ss.usermodel.Workbook;
11+
import org.apache.poi.ss.usermodel.*;
1312
import org.fugerit.java.core.function.SafeFunction;
1413
import org.fugerit.java.core.lang.helpers.BooleanUtils;
1514
import org.fugerit.java.query.export.facade.QueryExportConfig;
@@ -34,14 +33,30 @@ protected QueryExportHandlerXLSBase(String format) {
3433
super(format);
3534
}
3635

37-
private void addRow( Iterator<MetaField> currentRecord, Sheet sheet, int index ) {
36+
private void addRow( Workbook workbook, Iterator<MetaField> currentRecord, Sheet sheet, int index, QueryExportConfig config ) {
3837
int col = 0;
3938
Row row = sheet.createRow( index );
4039
while ( currentRecord.hasNext() ) {
4140
MetaField field = currentRecord.next();
42-
String value = field.getStringValue();
4341
Cell cell = row.createCell( col );
44-
cell.setCellValue( value );
42+
if ( config.isTryColumnType() && field.getType() != MetaField.TYPE_STRING ) {
43+
if ( field.getType() == MetaField.TYPE_DATE ) {
44+
DataFormatter formatter = new DataFormatter();
45+
DataFormat format = workbook.createDataFormat();
46+
CellStyle style = workbook.createCellStyle();
47+
style.setDataFormat( format.getFormat( "m/d/yy h:mm" ) );
48+
cell.setCellValue( field.getStringValue() );
49+
cell.setCellStyle(style);
50+
} else if ( field.getType() == MetaField.TYPE_NUMBER ) {
51+
DataFormat format = workbook.createDataFormat();
52+
CellStyle style = workbook.createCellStyle();
53+
style.setDataFormat( format.getFormat( "#,###" ) );
54+
cell.setCellValue( field.getNumberValue() );
55+
cell.setCellStyle(style);
56+
}
57+
} else {
58+
cell.setCellValue( field.getStringValue() );
59+
}
4560
col++;
4661
}
4762
}
@@ -71,7 +86,7 @@ public int export( QueryExportConfig config, MetaResult meta ) {
7186
if ( xlsTemplate == null ) {
7287
sheet = workbook.createSheet();
7388
if ( meta.hasHeader() ) {
74-
addRow( meta.headerIterator() , sheet, 0 );
89+
addRow( workbook, meta.headerIterator() , sheet, 0, config );
7590
}
7691
} else {
7792
sheet = workbook.getSheetAt( 0 );
@@ -80,7 +95,7 @@ public int export( QueryExportConfig config, MetaResult meta ) {
8095
Iterator<MetaRecord> itRec = meta.recordIterator();
8196
while ( itRec.hasNext() ) {
8297
MetaRecord currentRecord = itRec.next();
83-
addRow( currentRecord.fieldIterator() , sheet, index );
98+
addRow( workbook, currentRecord.fieldIterator() , sheet, index, config );
8499
index++;
85100
}
86101
if ( BooleanUtils.isTrue( config.getParams().getProperty( QueryExportFacade.ARG_XLS_RESIZE ) ) ) {
Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
11
package org.fugerit.java.query.export.meta;
22

3+
import lombok.AccessLevel;
34
import lombok.AllArgsConstructor;
45
import lombok.Getter;
56

6-
@AllArgsConstructor
7+
import java.sql.Timestamp;
8+
9+
@AllArgsConstructor(access = AccessLevel.PRIVATE)
710
public class BasicMetaField implements MetaField {
811

9-
@Getter private String stringValue;
12+
public BasicMetaField(String stringValue) {
13+
this( TYPE_STRING, stringValue, null, null );
14+
}
15+
16+
public BasicMetaField(String stringValue, Long longValue) {
17+
this( TYPE_NUMBER, stringValue, longValue, null );
18+
}
19+
20+
public BasicMetaField(String stringValue, Timestamp timestampValue) {
21+
this( TYPE_DATE, stringValue, null, timestampValue );
22+
}
23+
24+
@Getter
25+
private int type;
26+
27+
@Getter
28+
private String stringValue;
29+
30+
@Getter
31+
private Long numberValue;
32+
33+
@Getter
34+
private Timestamp timestampValue;
1035

1136
}

src/main/java/org/fugerit/java/query/export/meta/BasicMetaRSE.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import java.sql.ResultSet;
44
import java.sql.ResultSetMetaData;
55
import java.sql.SQLException;
6+
import java.sql.Timestamp;
67
import java.util.ArrayList;
8+
import java.util.Date;
79
import java.util.List;
810

911
import org.fugerit.java.core.db.dao.RSExtractor;
@@ -59,8 +61,13 @@ public MetaRecord extractNext(ResultSet rs) throws SQLException {
5961
Object current = rs.getObject( k+1 );
6062
SafeFunction.apply( () -> {
6163
String value = this.getFormat().format( current );
62-
MetaField field = new BasicMetaField( value );
63-
fields.add( field );
64+
if ( current instanceof Number ) {
65+
fields.add( new BasicMetaField( value, ((Number)current).longValue() ) );
66+
} else if ( current instanceof Date) {
67+
fields.add( new BasicMetaField( value, new Timestamp( ((Date)current).getTime() ) ) );
68+
} else {
69+
fields.add( new BasicMetaField( value ) );
70+
}
6471
} );
6572
}
6673
return new BasicMetaRecord( fields );
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
11
package org.fugerit.java.query.export.meta;
22

3+
import org.fugerit.java.core.cfg.ConfigRuntimeException;
4+
5+
import java.sql.Timestamp;
6+
37
public interface MetaField {
48

5-
public String getStringValue();
6-
9+
public static final int TYPE_STRING = 1;
10+
11+
public static final int TYPE_NUMBER = 2;
12+
13+
public static final int TYPE_DATE = 3;
14+
15+
String getStringValue();
16+
17+
default int getType() {
18+
return TYPE_STRING;
19+
}
20+
21+
default Long getNumberValue() {
22+
if ( Boolean.TRUE ) {
23+
throw new ConfigRuntimeException( "Number value unsupported" );
24+
}
25+
return null;
26+
}
27+
28+
default Timestamp getTimestampValue() {
29+
if ( Boolean.TRUE ) {
30+
throw new ConfigRuntimeException( "Timestamp value unsupported" );
31+
}
32+
return null;
33+
}
34+
735
}

0 commit comments

Comments
 (0)