1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
package net.sourceforge.addam.impexp; |
23 | |
|
24 | |
|
25 | |
import net.sourceforge.addam.impexp.csv.CSVPrinter; |
26 | |
import net.sourceforge.addam.util.DatabaseMetaDataUtil; |
27 | |
import net.sourceforge.addam.util.TableUtil; |
28 | |
|
29 | |
import org.apache.tools.ant.BuildException; |
30 | |
import org.apache.tools.ant.taskdefs.JDBCTask; |
31 | |
|
32 | |
import java.io.File; |
33 | |
import java.io.FileWriter; |
34 | |
import java.io.IOException; |
35 | |
import java.io.Writer; |
36 | |
import java.sql.*; |
37 | |
import java.util.ArrayList; |
38 | |
import java.util.Iterator; |
39 | |
import java.util.List; |
40 | |
|
41 | 0 | public class ExportDataTask extends JDBCTask { |
42 | 0 | private final List<Tables> includes = new ArrayList<Tables>(); |
43 | 0 | private final List<Tables> excludes = new ArrayList<Tables>(); |
44 | |
protected File dir; |
45 | |
private String schema; |
46 | |
private String catalog; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public void execute() throws BuildException { |
55 | |
|
56 | 0 | for (String tableName : getTableNames()) { |
57 | 0 | Connection connection = null; |
58 | |
try { |
59 | 0 | connection = this.getConnection(); |
60 | 0 | this.exportTable(tableName, connection); |
61 | 0 | } finally { |
62 | 0 | if (connection != null) |
63 | |
try { |
64 | 0 | connection.close(); |
65 | 0 | } catch (SQLException e) { |
66 | |
|
67 | 0 | } |
68 | 0 | } |
69 | 0 | } |
70 | 0 | } |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public void exportTable(String tableName, Connection connection) throws BuildException { |
79 | 0 | File toFile = new File(dir, TableUtil.getFileName(tableName)); |
80 | 0 | log("extracting " + tableName + " to file " + toFile); |
81 | 0 | int count = 0; |
82 | 0 | Writer writer = null; |
83 | |
try { |
84 | 0 | writer = new FileWriter(toFile); |
85 | 0 | CSVPrinter printer = new CSVPrinter(writer); |
86 | |
|
87 | 0 | Statement stmt = connection.createStatement(); |
88 | 0 | ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName + " ORDER BY 1"); |
89 | 0 | ResultSetMetaData rsmd = rs.getMetaData(); |
90 | 0 | int numCols = rsmd.getColumnCount(); |
91 | 0 | String line[] = new String[numCols]; |
92 | |
|
93 | |
|
94 | 0 | for (int i = 1; i <= numCols; i++) { |
95 | 0 | line[i - 1] = rsmd.getColumnName(i); |
96 | |
} |
97 | 0 | printer.writeRecord(line); |
98 | |
|
99 | |
|
100 | 0 | while (rs.next()) { |
101 | 0 | for (int i = 1; i <= numCols; i++) { |
102 | 0 | String className = rsmd.getColumnClassName(i); |
103 | 0 | DataCodec codec = DataCodec.getCodec(className); |
104 | |
Object value; |
105 | 0 | if (className.equals("java.sql.Timestamp")) { |
106 | 0 | value = rs.getTimestamp(i); |
107 | 0 | } else if (className.equals("java.lang.String")) { |
108 | 0 | value = rs.getString(i); |
109 | 0 | } else { |
110 | 0 | value = rs.getObject(i); |
111 | |
} |
112 | |
|
113 | 0 | String o = codec.encode(value); |
114 | 0 | line[i - 1] = (o == null) ? "" : o; |
115 | |
} |
116 | 0 | printer.writeRecord(line); |
117 | 0 | count++; |
118 | 0 | } |
119 | 0 | } catch (BuildException e) { |
120 | 0 | throw e; |
121 | 0 | } catch (Exception e) { |
122 | 0 | e.printStackTrace(); |
123 | 0 | throw new BuildException(e); |
124 | 0 | } finally { |
125 | 0 | try { |
126 | 0 | if (writer != null) { |
127 | 0 | writer.close(); |
128 | |
} |
129 | 0 | } catch (IOException e) { |
130 | 0 | log(e.toString()); |
131 | 0 | } |
132 | 0 | } |
133 | 0 | log(" ... " + count + " records"); |
134 | 0 | } |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public void setIncludeFile(File tableFile) { |
142 | 0 | Tables tables = new Tables(); |
143 | 0 | tables.setFile(tableFile); |
144 | 0 | includes.add(tables); |
145 | 0 | } |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
public void setExcludeFile(File tableFile) { |
153 | 0 | Tables tables = new Tables(); |
154 | 0 | tables.setFile(tableFile); |
155 | 0 | excludes.add(tables); |
156 | 0 | } |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
public void setIncludesPattern(String pattern) { |
162 | 0 | Tables tables = new Tables(); |
163 | 0 | tables.setPattern(pattern); |
164 | 0 | includes.add(tables); |
165 | 0 | } |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
public void setExcludesPattern(String pattern) { |
171 | 0 | Tables tables = new Tables(); |
172 | 0 | tables.setPattern(pattern); |
173 | 0 | excludes.add(tables); |
174 | 0 | } |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
public void setIncludes(String names) { |
180 | 0 | Tables tables = new Tables(); |
181 | 0 | tables.setNames(names); |
182 | 0 | includes.add(tables); |
183 | 0 | } |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
public void setExcludes(String names) { |
189 | 0 | Tables tables = new Tables(); |
190 | 0 | tables.setNames(names); |
191 | 0 | excludes.add(tables); |
192 | 0 | } |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
public void addIncludeTables(Tables tables) { |
198 | 0 | includes.add(tables); |
199 | 0 | } |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
public void addExcludeTables(Tables tables) { |
205 | 0 | excludes.add(tables); |
206 | 0 | } |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
public void setDir(File tableDir) { |
212 | 0 | dir = tableDir; |
213 | 0 | } |
214 | |
|
215 | |
public void setSchema(String schema) { |
216 | 0 | this.schema = schema; |
217 | 0 | } |
218 | |
|
219 | |
public void setCatalog(String catalog) { |
220 | 0 | this.catalog = catalog; |
221 | 0 | } |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
protected List<String> getTableNames() { |
229 | |
List<String> tableNamesFromDB; |
230 | |
|
231 | 0 | Connection connection = null; |
232 | |
try { |
233 | 0 | connection = this.getConnection(); |
234 | 0 | tableNamesFromDB = DatabaseMetaDataUtil.getTables(catalog,schema,connection); |
235 | 0 | } catch (SQLException e) { |
236 | 0 | throw new BuildException(e); |
237 | 0 | } finally { |
238 | 0 | if (connection != null) |
239 | |
try { |
240 | 0 | connection.close(); |
241 | 0 | } catch (SQLException e) { |
242 | |
|
243 | 0 | } |
244 | 0 | } |
245 | |
|
246 | 0 | return Tables.narrow(tableNamesFromDB,includes,excludes); |
247 | |
} |
248 | |
} |