Skip to content

Commit d55960f

Browse files
committed
Initial import
1 parent 50222e0 commit d55960f

File tree

9 files changed

+474
-0
lines changed

9 files changed

+474
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/target/
2+
/.classpath
3+
/.project
4+
/.settings/

pom.xml

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>org.fugerit.java</groupId>
6+
<artifactId>query-export-tool</artifactId>
7+
8+
<version>0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>query-export-tool</name>
12+
<description>Tool export query in CSV (and in the furutre other) formats</description>
13+
<url>http://www.fugerit.org/java/</url>
14+
15+
<scm>
16+
<connection>scm:git:git://github.com/fugerit-org/query-export-tool.git</connection>
17+
<developerConnection>scm:git:ssh://github.com/fugerit-org/query-export-tool.git</developerConnection>
18+
<url>http://github.com/fugerit-org/query-export-tool/tree/master</url>
19+
</scm>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
</properties>
24+
25+
<licenses>
26+
<license>
27+
<name>Apache License, Version 2.0</name>
28+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
29+
<distribution>repo</distribution>
30+
</license>
31+
</licenses>
32+
33+
<developers>
34+
<developer>
35+
<name>Matteo a.k.a. Fugerit</name>
36+
<email>m@fugerit.org</email>
37+
<organization>Fugerit</organization>
38+
<organizationUrl>http://www.fugerit.org</organizationUrl>
39+
</developer>
40+
<developer>
41+
<name>Daneel</name>
42+
<email>d@fugerit.org</email>
43+
<organization>Fugerit</organization>
44+
<organizationUrl>http://www.fugerit.org</organizationUrl>
45+
</developer>
46+
</developers>
47+
48+
49+
50+
<distributionManagement>
51+
<snapshotRepository>
52+
<id>ossrh</id>
53+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
54+
</snapshotRepository>
55+
</distributionManagement>
56+
57+
<dependencies>
58+
59+
<dependency>
60+
<groupId>org.fugerit.java</groupId>
61+
<artifactId>fj-core</artifactId>
62+
<version>0.2.4.6</version>
63+
</dependency>
64+
65+
<!-- java core dependancies -->
66+
<dependency>
67+
<groupId>maven</groupId>
68+
<artifactId>maven-java-plugin</artifactId>
69+
<version>1.6</version>
70+
</dependency>
71+
<dependency>
72+
<groupId>javax</groupId>
73+
<artifactId>javaee-api</artifactId>
74+
<version>6.0</version>
75+
<scope>provided</scope>
76+
</dependency>
77+
78+
<dependency>
79+
<groupId>com.opencsv</groupId>
80+
<artifactId>opencsv</artifactId>
81+
<version>4.6</version>
82+
</dependency>
83+
84+
<!-- for testing -->
85+
<dependency>
86+
<groupId>junit</groupId>
87+
<artifactId>junit</artifactId>
88+
<version>4.10</version>
89+
<scope>test</scope>
90+
<exclusions>
91+
<exclusion>
92+
<groupId>org.hamcrest</groupId>
93+
<artifactId>hamcrest-core</artifactId>
94+
</exclusion>
95+
</exclusions>
96+
</dependency>
97+
98+
</dependencies>
99+
100+
<build>
101+
102+
<plugins>
103+
104+
<plugin>
105+
<groupId>org.apache.maven.plugins</groupId>
106+
<artifactId>maven-source-plugin</artifactId>
107+
<version>3.0.1</version>
108+
<executions>
109+
<execution>
110+
<id>attach-sources</id>
111+
<goals>
112+
<goal>jar</goal>
113+
</goals>
114+
</execution>
115+
</executions>
116+
</plugin>
117+
<plugin>
118+
<groupId>org.apache.maven.plugins</groupId>
119+
<artifactId>maven-javadoc-plugin</artifactId>
120+
<version>2.10.4</version>
121+
<configuration>
122+
<stylesheetfile>src/main/javadoc/stylesheet.css</stylesheetfile>
123+
</configuration>
124+
<executions>
125+
<execution>
126+
<id>attach-javadocs</id>
127+
<goals>
128+
<goal>jar</goal>
129+
</goals>
130+
</execution>
131+
</executions>
132+
</plugin>
133+
134+
</plugins>
135+
136+
</build>
137+
138+
<profiles>
139+
140+
<profile>
141+
<id>doRelease</id>
142+
<build>
143+
<plugins>
144+
145+
146+
<plugin>
147+
<groupId>org.sonatype.plugins</groupId>
148+
<artifactId>nexus-staging-maven-plugin</artifactId>
149+
<version>1.6.8</version>
150+
<extensions>true</extensions>
151+
<configuration>
152+
<serverId>ossrh</serverId>
153+
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
154+
<autoReleaseAfterClose>true</autoReleaseAfterClose>
155+
</configuration>
156+
</plugin>
157+
158+
<plugin>
159+
<groupId>org.apache.maven.plugins</groupId>
160+
<artifactId>maven-gpg-plugin</artifactId>
161+
<version>1.5</version>
162+
<executions>
163+
<execution>
164+
<id>sign-artifacts</id>
165+
<phase>verify</phase>
166+
<goals>
167+
<goal>sign</goal>
168+
</goals>
169+
</execution>
170+
</executions>
171+
</plugin>
172+
173+
</plugins>
174+
</build>
175+
</profile>
176+
177+
<profile>
178+
<id>singlepackage</id>
179+
<build>
180+
<plugins>
181+
<plugin>
182+
<groupId>org.apache.maven.plugins</groupId>
183+
<artifactId>maven-jar-plugin</artifactId>
184+
<version>3.0.2</version>
185+
<configuration>
186+
<archive>
187+
<manifest>
188+
<addClasspath>true</addClasspath>
189+
<mainClass>org.fugerit.java.github.issue.export.GithubIssueExportMain</mainClass>
190+
</manifest>
191+
</archive>
192+
</configuration>
193+
</plugin>
194+
<plugin>
195+
<groupId>org.apache.maven.plugins</groupId>
196+
<artifactId>maven-shade-plugin</artifactId>
197+
<version>3.1.0</version>
198+
<executions>
199+
<execution>
200+
<phase>package</phase>
201+
<goals>
202+
<goal>shade</goal>
203+
</goals>
204+
</execution>
205+
</executions>
206+
<configuration>
207+
<finalName>dist-${project.artifactId}-${project.version}</finalName>
208+
</configuration>
209+
</plugin>
210+
</plugins>
211+
</build>
212+
</profile>
213+
214+
<profile>
215+
<id>jsonschema2pojo</id>
216+
<build>
217+
<plugins>
218+
<plugin>
219+
<!-- Genera gli oggetti di modello java per degli oggetti JSON : http://www.jsonschema2pojo.org/
220+
https://github.com/joelittlejohn/jsonschema2pojo/wiki/Getting-Started -->
221+
<groupId>org.jsonschema2pojo</groupId>
222+
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
223+
<version>0.4.33</version>
224+
<configuration>
225+
<sourceType>json</sourceType>
226+
<sourceDirectory>${project.basedir}/src/main/resources/pojo-config/json-source</sourceDirectory>
227+
<targetPackage>org.fugerit.java.github.issue.export.model</targetPackage>
228+
<!-- <sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
229+
<targetPackage>it.sogei.anpr.subentro.bpm.client.pojo</targetPackage> -->
230+
</configuration>
231+
<executions>
232+
<execution>
233+
<goals>
234+
<goal>generate</goal>
235+
</goals>
236+
</execution>
237+
</executions>
238+
</plugin>
239+
</plugins>
240+
</build>
241+
</profile>
242+
243+
</profiles>
244+
245+
<reporting>
246+
<plugins>
247+
<plugin>
248+
<groupId>org.apache.maven.plugins</groupId>
249+
<artifactId>maven-changelog-plugin</artifactId>
250+
<version>2.3</version>
251+
</plugin>
252+
</plugins>
253+
</reporting>
254+
255+
<organization>
256+
<url>http://www.fugerit.org</url>
257+
<name>Fugerit</name>
258+
</organization>
259+
260+
</project>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package org.fugerit.java.query.export.tool;
2+
3+
import java.io.BufferedWriter;
4+
import java.nio.charset.StandardCharsets;
5+
import java.nio.file.Files;
6+
import java.nio.file.Paths;
7+
import java.sql.Connection;
8+
import java.sql.ResultSet;
9+
import java.sql.ResultSetMetaData;
10+
import java.sql.Statement;
11+
import java.util.Properties;
12+
13+
import org.fugerit.java.core.cli.ArgUtils;
14+
import org.fugerit.java.core.db.connect.ConnectionFactory;
15+
import org.fugerit.java.core.db.connect.ConnectionFactoryImpl;
16+
import org.fugerit.java.core.io.FileIO;
17+
import org.fugerit.java.core.util.PropsIO;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
21+
import com.opencsv.CSVWriter;
22+
23+
public class QueryExportToolMain {
24+
25+
public static final String ARG_DB_CONFIG = "db-config";
26+
27+
public static final String ARG_QUERY_FILE = "query-file";
28+
29+
public static final String ARG_OUTPUT_FILE = "output-file";
30+
31+
public static final String ARG_OUTPUT_FORMAT = "output-format";
32+
public static final String ARG_OUTPUT_FORMAT_CSV = "csv";
33+
public static final String ARG_OUTPUT_FORMAT_DEFAULT = ARG_OUTPUT_FORMAT_CSV;
34+
35+
public static final String ARG_CSV_SEPARATOR = "csv-separator";
36+
public static final String ARG_CSV_SEPARATOR_DEF = ",";
37+
38+
protected static final Logger logger = LoggerFactory.getLogger(QueryExportToolMain.class);
39+
40+
public static void main( String[] args ) {
41+
Properties params = ArgUtils.getArgs( args );
42+
try {
43+
logger.info( "params: "+params );
44+
String dbConfig = params.getProperty( ARG_DB_CONFIG );
45+
String queryFile = params.getProperty( ARG_QUERY_FILE );
46+
String outputFile = params.getProperty( ARG_OUTPUT_FILE );
47+
String outputFormat = params.getProperty( ARG_OUTPUT_FORMAT, ARG_OUTPUT_FORMAT_DEFAULT );
48+
logger.info( "param : "+ARG_DB_CONFIG+"="+dbConfig );
49+
logger.info( "param : "+ARG_QUERY_FILE+"="+queryFile );
50+
logger.info( "param : "+ARG_OUTPUT_FILE+"="+outputFile );
51+
logger.info( "param : "+ARG_OUTPUT_FORMAT+"="+outputFormat );
52+
if ( queryFile == null || outputFile == null ) {
53+
throw new Exception( "Missing required arguments "+ARG_OUTPUT_FILE+", "+ARG_QUERY_FILE );
54+
} else {
55+
Properties props = PropsIO.loadFromFile( dbConfig );
56+
ConnectionFactory cf = ConnectionFactoryImpl.newInstance( props );
57+
Connection conn = cf.getConnection();
58+
try {
59+
60+
61+
Statement stm = conn.createStatement();
62+
String sql = FileIO.readString( queryFile );
63+
logger.info( "sql : "+sql );
64+
ResultSet rs = stm.executeQuery( sql );
65+
int count = 0;
66+
ResultSetMetaData rsmd = rs.getMetaData();
67+
if ( ARG_OUTPUT_FORMAT_CSV.equalsIgnoreCase( outputFormat ) ) {
68+
String separator = params.getProperty( ARG_CSV_SEPARATOR, ARG_CSV_SEPARATOR_DEF );
69+
BufferedWriter writer = Files.newBufferedWriter( Paths.get( outputFile ), StandardCharsets.UTF_8 );
70+
CSVWriter csvwriter = new CSVWriter( writer,
71+
separator.charAt( 0 ),
72+
'"',
73+
CSVWriter.NO_ESCAPE_CHARACTER,
74+
CSVWriter.DEFAULT_LINE_END);
75+
String[] line = new String[ rsmd.getColumnCount() ];
76+
for ( int k=0; k<rsmd.getColumnCount(); k++ ) {
77+
line[k] = rsmd.getColumnLabel( k+1 );
78+
}
79+
csvwriter.writeNext( line );
80+
while ( rs.next() ) {
81+
for ( int k=0; k<rsmd.getColumnCount(); k++ ) {
82+
line[k] = rs.getString( k+1 );
83+
}
84+
csvwriter.writeNext( line );
85+
count++;
86+
}
87+
csvwriter.flush();
88+
csvwriter.close();
89+
}
90+
91+
rs.close();
92+
stm.close();
93+
logger.info( "record count "+count );
94+
} catch (Exception e) {
95+
throw e;
96+
} finally {
97+
if ( conn != null ) {
98+
conn.close();
99+
}
100+
}
101+
}
102+
} catch (Exception e) {
103+
logger.error( e.getMessage(), e );
104+
}
105+
}
106+
107+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.fugerit.java.query.export.tool.data;
2+
3+
public interface FieldHandler {
4+
5+
public Object getData();
6+
7+
public int getSQLType();
8+
9+
}

0 commit comments

Comments
 (0)