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