001 /* 002 @license.text@ 003 */ 004 package biz.hammurapi.sqlc; 005 006 import java.util.ArrayList; 007 import java.util.Collection; 008 import java.util.Iterator; 009 010 import org.apache.tools.ant.BuildException; 011 012 import biz.hammurapi.sql.metadata.ColumnDescriptor; 013 import biz.hammurapi.sql.metadata.KeyEntry; 014 import biz.hammurapi.sql.metadata.Metadata.TableAcceptor; 015 016 /** 017 * @author Pavel Vlasov 018 * @version $Revision: 1.9 $ 019 */ 020 public class TableEntry implements TableAcceptor, ColumnHolder { 021 private String catalog; 022 private String schema; 023 private String table; 024 025 /** 026 * Catalog. Any catalog matches if not set 027 * @ant.non-required 028 * @param catalog 029 */ 030 public void setCatalog(String catalog) { 031 this.catalog = catalog; 032 } 033 034 /** 035 * Schema. Any schema matches if not set 036 * @ant.non-required 037 * @param catalog 038 */ 039 public void setSchema(String schema) { 040 this.schema = schema; 041 } 042 /** 043 * Table. Any table matches if not set 044 * @ant.non-required 045 * @param catalog 046 */ 047 public void setTable(String table) { 048 this.table = table; 049 } 050 051 public boolean accept(String catalog, String schema, String table) { 052 return (this.catalog==null || this.catalog.equals(catalog)) 053 && (this.schema==null || this.schema.equals(schema)) 054 && (this.table==null || this.table.equals(table)); 055 } 056 057 /** 058 * Column type. Overrides type reported by database. 059 * @ant.non-required 060 * @param paramType 061 */ 062 public void addConfiguredColumn(ColumnType colType) { 063 if (colType.getName()==null) { 064 throw new BuildException("Column name is not set for column type"); 065 } 066 if (!colType.isSkip() && colType.getType()==null) { 067 throw new BuildException("Column type is not set for column "+colType.getName()); 068 } 069 colTypes.add(colType); 070 } 071 072 private Collection colTypes=new ArrayList(); 073 074 /** 075 * @ant.ignore 076 * @param nigs 077 */ 078 public void setColTypes(NamedInterfaceGeneratingStatement nigs) { 079 Iterator it=colTypes.iterator(); 080 while (it.hasNext()) { 081 ColumnType ct=(ColumnType) it.next(); 082 nigs.setColumnType(ct.getName(), ct.getType()); 083 } 084 } 085 086 public String getColType(KeyEntry ke) { 087 return getColType(ke.getColumnName()); 088 } 089 090 091 /** 092 * @param columnName 093 * @return 094 */ 095 private String getColType(String columnName) { 096 Iterator it=colTypes.iterator(); 097 while (it.hasNext()) { 098 ColumnType ct=(ColumnType) it.next(); 099 if (columnName.equalsIgnoreCase(ct.getName())) { 100 return ct.getType(); 101 } 102 } 103 return null; 104 } 105 106 public String getColType(ColumnDescriptor cd) { 107 return getColType(cd.getDbName()); 108 } 109 110 protected boolean isSkipColumn(ColumnDescriptor cd) { 111 Iterator it=colTypes.iterator(); 112 while (it.hasNext()) { 113 ColumnType ct=(ColumnType) it.next(); 114 if (cd.getDbName().equalsIgnoreCase(ct.getName())) { 115 return ct.isSkip(); 116 } 117 } 118 return false; 119 } 120 121 private boolean generateMutators=true; 122 private boolean paramPerColumnInsert=true; 123 private boolean qualifiedTableName; 124 private boolean qualifiedMethodName; 125 126 /** 127 * Indicates whether table name shall include Catalog and Schema names 128 * @return 129 */ 130 public boolean isQualifiedTableName() { 131 return qualifiedTableName; 132 } 133 134 /** 135 * Indicates whether table name shall include Catalog and Schema names 136 * @return 137 */ 138 public boolean isQualifiedMethodName() { 139 return qualifiedMethodName; 140 } 141 142 /** 143 * Indicate where to qualify table name with schema name. 144 * Valid values are - 'none', 'table', 'method', and 'both' 145 * @ant.non-required 146 * @param qualifiedName 147 */ 148 public void setQualifiedName(String qualifiedName) { 149 if ("none".equals(qualifiedName)) { 150 qualifiedTableName=false; 151 qualifiedMethodName=false; 152 } else if ("table".equals(qualifiedName)) { 153 qualifiedTableName=true; 154 qualifiedMethodName=false; 155 } else if ("method".equals(qualifiedName)) { 156 qualifiedTableName=false; 157 qualifiedMethodName=true; 158 } else if ("both".equals(qualifiedName)) { 159 qualifiedTableName=true; 160 qualifiedMethodName=true; 161 } else { 162 throw new BuildException("Invalid value: '"+"' expected 'none', 'table', 'method', or 'both'"); 163 } 164 } 165 166 /** 167 * If 'true' (default) then mutators (setters) are generated along with 168 * accessors (getters) in row interfaces. 169 * @ant.non-required 170 * @param generateMutator 171 */ 172 public void setGenerateMutators(boolean generateMutators) { 173 this.generateMutators = generateMutators; 174 } 175 /** 176 * @return 177 */ 178 public boolean getGenerateMutators() { 179 return generateMutators; 180 } 181 182 /** 183 * If 'true' (default) insert() method is generated for table 184 * with all columns listed as method parameters. For tables with big number 185 * of columns it is better off to set this attribute to false and use 186 * insert(<<I>RowInterface</I>>) method. 187 * @ant.non-required 188 * @param paramPerColumnInsert 189 * @return 190 */ 191 public void setParamPerColumnInsert(boolean paramPerColumnInsert) { 192 this.paramPerColumnInsert=paramPerColumnInsert; 193 } 194 195 /** 196 * @return Returns the paramPerColumnInsertUpdate. 197 */ 198 public boolean isParamPerColumnInsert() { 199 return paramPerColumnInsert; 200 } 201 202 protected StatementCompilerTask task; 203 204 void setTask(StatementCompilerTask task) { 205 this.task=task; 206 } 207 208 StatementCompilerTask getTask() { 209 return task; 210 } 211 212 /** 213 * @return 214 */ 215 boolean getUseSqlTypes() { 216 if (task==null) { 217 throw new BuildException("Task is null"); 218 } 219 220 if (useSqlTypes==null) { 221 return task.getUseSqlTypes()==null ? false : task.getUseSqlTypes().booleanValue(); 222 } 223 224 return useSqlTypes.booleanValue(); 225 } 226 227 private Boolean useSqlTypes; 228 229 /** 230 * If true then generated classes will use setObject(int, Object, int) method 231 * instead of setObject(int, Object) to set parameters of object type. 232 * Inherits value from task. Default value is true. 233 * @ant.non-required 234 * @param useSqlTypes 235 */ 236 public void setUseSqlTypes(boolean useSqlTypes) { 237 this.useSqlTypes = useSqlTypes ? Boolean.TRUE : Boolean.FALSE; 238 } 239 240 private String smartBase; 241 242 /** 243 * @ant.ignore 244 * @return Returns the smartBase. 245 */ 246 String getSmartBase() { 247 return smartBase == null ? getTask().getSmartBase() : smartBase; 248 } 249 250 /** 251 * Base class (fully qualified name) for generated smart implementation. Default is biz.hammurapi.sql.DatabaseObject 252 * The base class is supposed to have same constructors and methods as DatabaseObject. The idea is 253 * to subclass DatabaseObject to introduce additional qualities like audit, metrics, security, ... whithout 254 * changing class' contract. If this value is not set explicitly it gets inherited from the task. 255 * @ant.non-required 256 * @param smartBase The smartBase to set. 257 */ 258 public void setSmartBase(String smartBase) { 259 this.smartBase = smartBase; 260 } 261 262 }