1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.rsmart.kuali.tools.ant.tasks;
18
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22 import java.sql.Connection;
23 import java.sql.DatabaseMetaData;
24 import java.sql.ResultSet;
25 import java.util.HashSet;
26 import java.util.Set;
27 import java.util.Vector;
28
29 import org.apache.tools.ant.BuildException;
30
31
32
33
34
35 public class TableSet {
36 private Vector<Include> includes;
37 private Set<String> tables;
38 private Connection connection;
39 private String schema;
40
41 public TableSet() {
42 includes = new Vector();
43 tables = new HashSet();
44 }
45
46
47 public Include createInclude() {
48 Include retval = new Include();
49 includes.add(retval);
50 return retval;
51 }
52
53 public void execute() {
54 try {
55 DatabaseMetaData metadata = getConnection().getMetaData();
56 System.out.printf("Looking for tables in schema %s\n", getSchema());
57 ResultSet rs = metadata.getTables(null, getSchema(), null, new String[] {"TABLE"});
58
59 while (rs.next()) {
60 String tableName = rs.getString("TABLE_NAME");
61 if (isTableNameValid(tableName)) {
62 tables.add(tableName);
63 }
64 }
65 }
66 catch (Exception e) {
67 throw new BuildException("Exception when getting table names", e);
68 }
69 }
70
71 private boolean isTableNameValid(String tableName) {
72 boolean retval = includes.size() == 0;
73 for (Include include : includes) {
74 retval |= Pattern.compile(include.getRegex()).matcher(tableName).matches();
75 }
76 return retval;
77 }
78
79 public Set<String> getTables() {
80 return tables;
81 }
82
83
84
85
86
87
88
89 public final Connection getConnection() {
90 return this.connection;
91 }
92
93
94
95
96
97
98 public final void setConnection(final Connection argConnection) {
99 this.connection = argConnection;
100 }
101
102
103 public void setSchema(String schema) {
104 this.schema = schema;
105 }
106
107 public String getSchema() {
108 return schema;
109 }
110 }