View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * @author Leo Przybylski (przybyls@arizona.edu)
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       * Gets the value of connection
86       *
87       * @return the value of connection
88       */
89      public final Connection getConnection() {
90          return this.connection;
91      }
92      
93      /**
94       * Sets the value of connection
95       *
96       * @param argConnection Value to assign to this.connection
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 }