001/* 002 * Copyright 2005-2007 The Kuali Foundation 003 * 004 * 005 * Licensed under the Educational Community License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.opensource.org/licenses/ecl2.php 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.kualigan.tools.ant.tasks; 018 019import java.util.regex.Matcher; 020import java.util.regex.Pattern; 021 022import java.sql.Connection; 023import java.sql.DatabaseMetaData; 024import java.sql.ResultSet; 025import java.util.HashSet; 026import java.util.Set; 027import java.util.Vector; 028 029import org.apache.tools.ant.BuildException; 030 031/** 032 * 033 * @author Leo Przybylski (przybyls@arizona.edu) 034 */ 035public class TableSet { 036 private Vector<Include> includes; 037 private Set<String> tables; 038 private Connection connection; 039 private String schema; 040 041 public TableSet() { 042 includes = new Vector(); 043 tables = new HashSet(); 044 } 045 046 047 public Include createInclude() { 048 Include retval = new Include(); 049 includes.add(retval); 050 return retval; 051 } 052 053 public void execute() { 054 try { 055 DatabaseMetaData metadata = getConnection().getMetaData(); 056 System.out.printf("Looking for tables in schema %s\n", getSchema()); 057 ResultSet rs = metadata.getTables(null, getSchema(), null, new String[] {"TABLE"}); 058 059 while (rs.next()) { 060 String tableName = rs.getString("TABLE_NAME"); 061 if (isTableNameValid(tableName)) { 062 tables.add(tableName); 063 } 064 } 065 } 066 catch (Exception e) { 067 throw new BuildException("Exception when getting table names", e); 068 } 069 } 070 071 private boolean isTableNameValid(String tableName) { 072 boolean retval = includes.size() == 0; 073 for (Include include : includes) { 074 retval |= Pattern.compile(include.getRegex()).matcher(tableName).matches(); 075 } 076 return retval; 077 } 078 079 public Set<String> getTables() { 080 return tables; 081 } 082 083 084 /** 085 * Gets the value of connection 086 * 087 * @return the value of connection 088 */ 089 public final Connection getConnection() { 090 return this.connection; 091 } 092 093 /** 094 * Sets the value of connection 095 * 096 * @param argConnection Value to assign to this.connection 097 */ 098 public final void setConnection(final Connection argConnection) { 099 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}