Skip to content

Commit 662efcd

Browse files
Added abstraction level
1 parent e8061e8 commit 662efcd

File tree

9 files changed

+265
-16
lines changed

9 files changed

+265
-16
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,28 @@
55

66
public class QueryExportConfig {
77

8+
public static final boolean DEFAULT_EXPORT_HEADER = true;
9+
810
public static QueryExportConfig newConfigCSV( OutputStream output, Connection conn, String query ) {
9-
return new QueryExportConfig( QueryExportFacade.FORMAT_CSV , QueryExportFacade.CSV_SEPARATOR_DEF, output, conn, query );
11+
return new QueryExportConfig( QueryExportFacade.FORMAT_CSV , QueryExportFacade.CSV_SEPARATOR_DEF, output, conn, query, DEFAULT_EXPORT_HEADER );
1012
}
1113

1214
public static QueryExportConfig newConfigCSV( OutputStream output, Connection conn, String query, char separator ) {
13-
return new QueryExportConfig( QueryExportFacade.FORMAT_CSV , separator, output, conn, query );
15+
return new QueryExportConfig( QueryExportFacade.FORMAT_CSV , separator, output, conn, query, DEFAULT_EXPORT_HEADER );
1416
}
1517

1618
public QueryExportConfig(String format, char separator, OutputStream output, Connection conn, String query) {
19+
this( format, separator, output, conn, query, true );
20+
}
21+
22+
public QueryExportConfig(String format, char separator, OutputStream output, Connection conn, String query, boolean exportHeader) {
1723
super();
1824
this.format = format;
1925
this.separator = separator;
2026
this.output = output;
2127
this.conn = conn;
2228
this.query = query;
29+
this.exportHeader = exportHeader;
2330
}
2431

2532
private String format;
@@ -31,6 +38,8 @@ public QueryExportConfig(String format, char separator, OutputStream output, Con
3138
private Connection conn;
3239

3340
private String query;
41+
42+
private boolean exportHeader;
3443

3544
public String getFormat() {
3645
return format;
@@ -69,5 +78,13 @@ public OutputStream getOutput() {
6978
public void setOutput(OutputStream output) {
7079
this.output = output;
7180
}
81+
82+
public boolean isExportHeader() {
83+
return exportHeader;
84+
}
85+
86+
public void setExportHeader(boolean exportHeader) {
87+
this.exportHeader = exportHeader;
88+
}
7289

7390
}

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

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
import java.sql.ResultSet;
77
import java.sql.ResultSetMetaData;
88
import java.sql.Statement;
9+
import java.util.Iterator;
910

11+
import org.fugerit.java.query.export.meta.BasicMetaRSE;
12+
import org.fugerit.java.query.export.meta.BasicMetaResult;
13+
import org.fugerit.java.query.export.meta.MetaField;
14+
import org.fugerit.java.query.export.meta.MetaRecord;
15+
import org.fugerit.java.query.export.meta.MetaResult;
1016
import org.slf4j.Logger;
1117
import org.slf4j.LoggerFactory;
1218

@@ -25,8 +31,27 @@ public static int export( QueryExportConfig config ) throws Exception {
2531
Statement stm = config.getConn().createStatement();
2632
logger.info( "sql : "+config.getQuery() );
2733
ResultSet rs = stm.executeQuery( config.getQuery() );
28-
int count = 0;
29-
ResultSetMetaData rsmd = rs.getMetaData();
34+
MetaResult meta = new BasicMetaResult( BasicMetaRSE.newInstanceAllToString( rs.getMetaData() ) , rs );
35+
export( config, meta );
36+
int count = meta.close();
37+
stm.close();
38+
logger.info( "record count "+count );
39+
return res;
40+
}
41+
42+
private static void writeRecordCSV( String[] line, Iterator<MetaField> itField, CSVWriter csvwriter ) throws Exception {
43+
int k = 0;
44+
while ( itField.hasNext() ) {
45+
MetaField field = itField.next();
46+
line[k] = field.getStringValue();
47+
k++;
48+
}
49+
csvwriter.writeNext( line );
50+
}
51+
52+
public static int export( QueryExportConfig config, MetaResult meta ) throws Exception {
53+
int res = 0;
54+
ResultSetMetaData rsmd = meta.getMetaData();
3055
if ( FORMAT_CSV.equalsIgnoreCase( config.getFormat() ) ) {
3156
char separator = config.getSeparator();
3257
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( config.getOutput() , Charset.forName( "UTF-8" ) ) );
@@ -36,23 +61,18 @@ public static int export( QueryExportConfig config ) throws Exception {
3661
CSVWriter.NO_ESCAPE_CHARACTER,
3762
CSVWriter.DEFAULT_LINE_END);
3863
String[] line = new String[ rsmd.getColumnCount() ];
39-
for ( int k=0; k<rsmd.getColumnCount(); k++ ) {
40-
line[k] = rsmd.getColumnLabel( k+1 );
64+
if ( meta.hasHeader() ) {
65+
writeRecordCSV(line, meta.headerIterator(), csvwriter);
4166
}
42-
csvwriter.writeNext( line );
43-
while ( rs.next() ) {
44-
for ( int k=0; k<rsmd.getColumnCount(); k++ ) {
45-
line[k] = rs.getString( k+1 );
46-
}
47-
csvwriter.writeNext( line );
48-
count++;
67+
Iterator<MetaRecord> itRec = meta.recordIterator();
68+
while ( itRec.hasNext() ) {
69+
MetaRecord record = itRec.next();
70+
Iterator<MetaField> itField = record.fieldIterator();
71+
writeRecordCSV(line, itField, csvwriter);
4972
}
5073
csvwriter.flush();
5174
csvwriter.close();
5275
}
53-
rs.close();
54-
stm.close();
55-
logger.info( "record count "+count );
5676
return res;
5777
}
5878

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.fugerit.java.query.export.meta;
2+
3+
public class BasicMetaField implements MetaField {
4+
5+
public BasicMetaField(String stringValue) {
6+
super();
7+
this.stringValue = stringValue;
8+
}
9+
10+
private String stringValue;
11+
12+
public String getStringValue() {
13+
return stringValue;
14+
}
15+
16+
public void setStringValue(String stringValue) {
17+
this.stringValue = stringValue;
18+
}
19+
20+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.fugerit.java.query.export.meta;
2+
3+
import java.sql.ResultSet;
4+
import java.sql.ResultSetMetaData;
5+
import java.sql.SQLException;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
import org.fugerit.java.core.db.dao.RSExtractor;
10+
11+
public abstract class BasicMetaRSE implements RSExtractor<MetaRecord> {
12+
13+
private ResultSetMetaData rsmd;
14+
15+
@Override
16+
public abstract MetaRecord extractNext(ResultSet rs) throws SQLException;
17+
18+
public void init( ResultSetMetaData rsmd ) throws Exception {
19+
this.rsmd = rsmd;
20+
}
21+
22+
public void destroy( ) throws Exception {
23+
this.rsmd = null;
24+
}
25+
26+
protected ResultSetMetaData getRsmd() {
27+
return rsmd;
28+
}
29+
30+
public static BasicMetaRSE newInstanceAllToString( ResultSetMetaData rsmd ) throws Exception {
31+
BasicMetaRSEAllToString rse = new BasicMetaRSEAllToString();
32+
rse.init( rsmd );
33+
return rse;
34+
35+
}
36+
37+
}
38+
39+
class BasicMetaRSEAllToString extends BasicMetaRSE {
40+
41+
@Override
42+
public MetaRecord extractNext(ResultSet rs) throws SQLException {
43+
List<MetaField> fields = new ArrayList<MetaField>();
44+
for ( int k=0; k<this.getRsmd().getColumnCount(); k++ ) {
45+
Object current = rs.getObject( k+1 );
46+
String value = null;
47+
if ( current != null ) {
48+
value = String.valueOf( current );
49+
}
50+
MetaField field = new BasicMetaField( value );
51+
fields.add( field );
52+
}
53+
return new BasicMetaRecord( fields );
54+
}
55+
56+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.fugerit.java.query.export.meta;
2+
3+
import java.util.Collection;
4+
import java.util.Iterator;
5+
6+
public class BasicMetaRecord implements MetaRecord {
7+
8+
private Collection<MetaField> fields;
9+
10+
@Override
11+
public Iterator<MetaField> fieldIterator() {
12+
return this.fields.iterator();
13+
}
14+
15+
public BasicMetaRecord(Collection<MetaField> fields) {
16+
super();
17+
this.fields = fields;
18+
}
19+
20+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.fugerit.java.query.export.meta;
2+
3+
import java.sql.ResultSet;
4+
import java.sql.ResultSetMetaData;
5+
import java.sql.SQLException;
6+
import java.util.ArrayList;
7+
import java.util.Iterator;
8+
import java.util.List;
9+
10+
public class BasicMetaResult implements MetaResult {
11+
12+
public BasicMetaResult( BasicMetaRSE rse, ResultSet rs) {
13+
super();
14+
this.rse = rse;
15+
this.rs = rs;
16+
}
17+
18+
private BasicMetaRSE rse;
19+
20+
private ResultSet rs;
21+
22+
private int count;
23+
24+
@Override
25+
public boolean hasHeader() throws Exception {
26+
return true;
27+
}
28+
29+
@Override
30+
public Iterator<MetaField> headerIterator() throws Exception {
31+
List<MetaField> list = new ArrayList<MetaField>();
32+
ResultSetMetaData rsmd = this.rs.getMetaData();
33+
for ( int k=0; k<rsmd.getColumnCount(); k++ ) {
34+
list.add( new BasicMetaField( rsmd.getColumnLabel( k+1 ) ) );
35+
}
36+
return list.iterator();
37+
}
38+
39+
@Override
40+
public ResultSetMetaData getMetaData() throws SQLException {
41+
return this.rs.getMetaData();
42+
}
43+
44+
@Override
45+
public Iterator<MetaRecord> recordIterator() throws Exception {
46+
return new Iterator<MetaRecord>() {
47+
@Override
48+
public boolean hasNext() {
49+
boolean next = false;
50+
try {
51+
next = rs.next();
52+
} catch (SQLException e) {
53+
throw new RuntimeException( e );
54+
}
55+
return next;
56+
}
57+
@Override
58+
public MetaRecord next() {
59+
MetaRecord record = null;
60+
try {
61+
record = rse.extractNext( rs );
62+
count++;
63+
} catch (SQLException e) {
64+
throw new RuntimeException( e );
65+
}
66+
return record;
67+
}
68+
};
69+
}
70+
71+
@Override
72+
public int close() throws Exception {
73+
this.rs.close();
74+
this.rs = null;
75+
this.rse.destroy();
76+
this.rse = null;
77+
return this.count;
78+
}
79+
80+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.fugerit.java.query.export.meta;
2+
3+
public interface MetaField {
4+
5+
public String getStringValue();
6+
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.fugerit.java.query.export.meta;
2+
3+
import java.util.Iterator;
4+
5+
public interface MetaRecord {
6+
7+
public Iterator<MetaField> fieldIterator();
8+
9+
10+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.fugerit.java.query.export.meta;
2+
3+
import java.sql.ResultSetMetaData;
4+
import java.sql.SQLException;
5+
import java.util.Iterator;
6+
7+
public interface MetaResult {
8+
9+
public Iterator<MetaRecord> recordIterator() throws Exception;
10+
11+
public ResultSetMetaData getMetaData() throws SQLException;
12+
13+
public boolean hasHeader() throws Exception;
14+
15+
public Iterator<MetaField> headerIterator() throws Exception;
16+
17+
public int close() throws Exception;
18+
19+
}

0 commit comments

Comments
 (0)