Skip to content

Commit ee36913

Browse files
authored
Version 0.4.0 (#4)
1 parent 9a56c66 commit ee36913

File tree

4 files changed

+111
-18
lines changed

4 files changed

+111
-18
lines changed

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,62 @@ Tool export query in CSV and XLS / XLSX format
1111
![Java runtime version](https://img.shields.io/badge/run%20on-java%208+-%23113366.svg?style=for-the-badge&logo=openjdk&logoColor=white)
1212
![Java build version](https://img.shields.io/badge/build%20on-java%2011+-%23ED8B00.svg?style=for-the-badge&logo=openjdk&logoColor=white)
1313
![Apache Maven](https://img.shields.io/badge/Apache%20Maven-3.9.0+-C71A36?style=for-the-badge&logo=Apache%20Maven&logoColor=white)
14+
15+
## 1 Quickstart
16+
17+
### 1.1 Create a sample query catalog
18+
19+
```
20+
<query-catalog-config>
21+
22+
<query-catalog id="main-catalog">
23+
<query id="Q001" sql="SELECT * FROM test_export" outputFormat="csv" csvSeparator=";" outputFile="target/catalog_test_001.csv"/>
24+
<query id="Q002" sql="SELECT * FROM test_export" outputFormat="csv" outputFile="target/catalog_test_002.csv"/>
25+
<query id="Q003" sql="SELECT * FROM test_export" outputFormat="html" outputFile="target/catalog_test_003.html" createPath="1"/>
26+
<query id="Q004" sql="SELECT * FROM test_export" outputFormat="xls" outputFile="target/catalog_test_004.xls" xlsResize="1"/>
27+
<query id="Q005" sql="SELECT * FROM test_export" outputFormat="xlsx" outputFile="target/catalog_test_004.xlsx" xlsTemplate="src/test/resources/template/test_template.xlsx" />
28+
</query-catalog>
29+
30+
</query-catalog-config>
31+
```
32+
33+
### 1.2 Load and use the catalog
34+
35+
```
36+
QueryConfigCatalog catalog = QueryConfigCatalog.loadQueryConfigCatalogSafe( "cl://sample/query-catalog-sample.xml" );
37+
try ( Connection conn = ... ) {
38+
catalog.handle( conn , "main-catalog", "Q001");
39+
}
40+
```
41+
42+
## 2 Formats
43+
44+
### 2.1 HTML format
45+
46+
HTML format is handled with core I/O API, no dependency needed.
47+
48+
### 2.2 CSV format
49+
50+
CSV Format needs *OpenCSV* dependency, which is automatically included by default when importing query-export-tool dependency.
51+
52+
If needed it can be added in explicit way :
53+
54+
```
55+
<dependency>
56+
<groupId>com.opencsv</groupId>
57+
<artifactId>opencsv</artifactId>
58+
<version>${opencsv-version}</version>
59+
</dependency>
60+
```
61+
62+
### 2.3 XLS/XLSX formats
63+
64+
XLS/XLSX Formats needs *Apache POI* dependency, which is *NOT* automatically included by default when importing query-export-tool dependency :
65+
66+
```
67+
<dependency>
68+
<groupId>org.apache.poi</groupId>
69+
<artifactId>poi-ooxml</artifactId>
70+
<version>${poi-version}</version>
71+
</dependency>
72+
```

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
import java.io.File;
44
import java.io.FileOutputStream;
55
import java.io.IOException;
6+
import java.io.InputStream;
67
import java.sql.Connection;
78
import java.util.Properties;
89

910
import org.fugerit.java.core.cfg.ConfigRuntimeException;
1011
import org.fugerit.java.core.cfg.xml.CustomListCatalogConfig;
12+
import org.fugerit.java.core.cfg.xml.GenericListCatalogConfig;
13+
import org.fugerit.java.core.function.SafeFunction;
1114
import org.fugerit.java.core.io.helper.HelperIOException;
15+
import org.fugerit.java.core.io.helper.StreamHelper;
1216
import org.fugerit.java.core.lang.helpers.BooleanUtils;
1317
import org.fugerit.java.core.lang.helpers.StringUtils;
1418
import org.fugerit.java.core.util.collection.ListMapStringKey;
@@ -30,6 +34,29 @@ public QueryConfigCatalog() {
3034
this.getGeneralProps().setProperty( ATT_TYPE , QueryConfig.class.getName() );
3135
}
3236

37+
public static QueryConfigCatalog loadQueryConfigCatalogSafe( String path ) {
38+
return SafeFunction.get( () -> {
39+
QueryConfigCatalog catalog = new QueryConfigCatalog();
40+
try ( InputStream is = StreamHelper.resolveStream( path ) ) {
41+
GenericListCatalogConfig.load( is ,catalog );
42+
}
43+
return catalog;
44+
} );
45+
}
46+
47+
public void handle( Connection conn, String catalogId, String queryId ) throws IOException {
48+
log.info( "handle catalogId : {}, queryId : {}", catalogId, queryId );
49+
ListMapStringKey<QueryConfig> catalog = this.getListMap( catalogId );
50+
if ( catalog == null ) {
51+
throw new IOException( "Catalog not found : "+catalogId );
52+
}
53+
QueryConfig queryConfig = catalog.get( queryId );
54+
if ( queryConfig == null ) {
55+
throw new IOException( "Query not found : "+queryId+" in catalog : "+catalogId );
56+
}
57+
handle(conn, queryConfig);
58+
}
59+
3360
public static void handle( Connection conn, QueryConfig queryConfig ) throws IOException {
3461
HelperIOException.apply( () -> {
3562
QueryExportConfig config = null;

src/test/java/test/org/fugerit/java/query/export/tool/TestCatalog.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
import java.io.File;
44
import java.io.IOException;
5-
import java.io.InputStream;
5+
import java.sql.Connection;
6+
import java.sql.SQLException;
67

78
import org.fugerit.java.core.cfg.ConfigRuntimeException;
8-
import org.fugerit.java.core.cfg.xml.GenericListCatalogConfig;
99
import org.fugerit.java.core.function.SafeFunction;
10-
import org.fugerit.java.core.lang.helpers.ClassHelper;
1110
import org.fugerit.java.query.export.catalog.QueryConfig;
1211
import org.fugerit.java.query.export.catalog.QueryConfigCatalog;
1312
import org.junit.Assert;
@@ -19,20 +18,24 @@ private boolean testWorkerSingle( QueryConfig queryConfig ) {
1918
return SafeFunction.get( () -> {
2019
logger.info( "test start" );
2120
File outputFile = new File( queryConfig.getOutputFile() );
22-
QueryConfigCatalog.handle(getConnection(), queryConfig);
21+
try ( Connection conn = getConnection() ) {
22+
QueryConfigCatalog.handle( conn, queryConfig);
23+
}
2324
return outputFile.exists();
2425
} );
2526
}
2627

2728
@Test
28-
public void testCatalog() throws IOException {
29-
QueryConfigCatalog catalog = new QueryConfigCatalog();
30-
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( "sample/query-catalog-sample.xml" ) ) {
31-
GenericListCatalogConfig.load(is, catalog);
32-
catalog.getListMap( "sample-catalog" ).stream().forEach( c -> Assert.assertTrue( this.testWorkerSingle(c) ) );
33-
catalog.getListMap( "fail-catalog" ).stream().forEach(
34-
c -> Assert.assertThrows( ConfigRuntimeException.class, () -> this.testWorkerSingle(c) )
35-
);
29+
public void testCatalog() throws IOException, SQLException {
30+
QueryConfigCatalog catalog = QueryConfigCatalog.loadQueryConfigCatalogSafe( "cl://sample/query-catalog-sample.xml" );
31+
catalog.getListMap( "sample-catalog" ).stream().forEach( c -> Assert.assertTrue( this.testWorkerSingle(c) ) );
32+
catalog.getListMap( "fail-catalog" ).stream().forEach(
33+
c -> Assert.assertThrows( ConfigRuntimeException.class, () -> this.testWorkerSingle(c) ) );
34+
// test specific
35+
try ( Connection conn = getConnection() ) {
36+
catalog.handle( conn , "main-catalog", "Q010");
37+
Assert.assertThrows( IOException.class , () -> catalog.handle(conn, "main-catalog", "not-exists" ) );
38+
Assert.assertThrows( IOException.class , () -> catalog.handle(conn, "not-exists", "not-exists" ) );
3639
}
3740
}
3841

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
<query-catalog-config>
22

33
<query-catalog id="sample-catalog">
4-
<query sql="SELECT * FROM test_export" outputFormat="csv" csvSeparator=";" outputFile="target/catalog_test_001.csv"/>
5-
<query sql="SELECT * FROM test_export" outputFormat="csv" outputFile="target/catalog_test_002.csv"/>
6-
<query sql="SELECT * FROM test_export" outputFormat="html" outputFile="target/catalog_test_003.html" createPath="1"/>
7-
<query sql="SELECT * FROM test_export" outputFormat="xls" outputFile="target/catalog_test_004.xls" xlsResize="1"/>
8-
<query sql="SELECT * FROM test_export" outputFormat="xlsx" outputFile="target/catalog_test_004.xlsx" xlsTemplate="src/test/resources/template/test_template.xlsx" />
4+
<query id="Q001" sql="SELECT * FROM test_export" outputFormat="csv" csvSeparator=";" outputFile="target/catalog_test_001.csv"/>
5+
<query id="Q002" sql="SELECT * FROM test_export" outputFormat="csv" outputFile="target/catalog_test_002.csv"/>
6+
<query id="Q003" sql="SELECT * FROM test_export" outputFormat="html" outputFile="target/catalog_test_003.html" createPath="1"/>
7+
<query id="Q004" sql="SELECT * FROM test_export" outputFormat="xls" outputFile="target/catalog_test_004.xls" xlsResize="1"/>
8+
<query id="Q005" sql="SELECT * FROM test_export" outputFormat="xlsx" outputFile="target/catalog_test_004.xlsx" xlsTemplate="src/test/resources/template/test_template.xlsx" />
99
</query-catalog>
1010

1111
<query-catalog id="fail-catalog">
12-
<query sql="SELECT * FROM test_export" outputFormat="xxx" outputFile="target/catalog_test_fail.xlsx"/>
12+
<query id="QEX1" sql="SELECT * FROM test_export" outputFormat="xxx" outputFile="target/catalog_test_fail.xlsx"/>
13+
</query-catalog>
14+
15+
<query-catalog id="main-catalog">
16+
<query id="Q010" sql="SELECT * FROM test_export" outputFormat="csv" outputFile="target/catalog_test_010.csv"/>
1317
</query-catalog>
1418

1519
</query-catalog-config>

0 commit comments

Comments
 (0)