001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.codegen.oracle8;
042:
043: import java.util.ArrayList;
044: import java.util.Iterator;
045: import java.util.List;
046:
047: import org.apache.velocity.VelocityContext;
048: import org.netbeans.modules.sql.framework.codegen.AbstractDB;
049: import org.netbeans.modules.sql.framework.codegen.ColumnIdentifier;
050: import org.netbeans.modules.sql.framework.codegen.StatementContext;
051: import org.netbeans.modules.sql.framework.codegen.TemplateBuilder;
052: import org.netbeans.modules.sql.framework.codegen.base.BaseStatements;
053: import org.netbeans.modules.sql.framework.model.SQLConstants;
054: import org.netbeans.modules.sql.framework.model.SourceTable;
055: import org.netbeans.modules.sql.framework.model.TargetColumn;
056: import org.netbeans.modules.sql.framework.model.TargetTable;
057:
058: import com.sun.sql.framework.exception.BaseException;
059: import com.sun.sql.framework.jdbc.SQLPart;
060: import com.sun.sql.framework.utils.StringUtil;
061:
062: /**
063: * @author Jonathan Giron
064: * @version $Revision$
065: */
066: public class Oracle8Statements extends BaseStatements {
067: public Oracle8Statements(AbstractDB database) {
068: super (database);
069: }
070:
071: public SQLPart getSelectStatement(SourceTable sourceTable,
072: StatementContext context) throws BaseException {
073: if (context == null) {
074: context = new StatementContext();
075: }
076: VelocityContext vContext = new VelocityContext();
077:
078: List sourceColumnIdentifiers = this .createSourceIdentifierList(
079: sourceTable, context);
080: vContext
081: .put("sourceColumnIdentifiers", sourceColumnIdentifiers);
082:
083: vContext.put("aliasColumns", Boolean.valueOf(context
084: .isUseSourceColumnAliasName()));
085: vContext.put("distinct", Boolean.valueOf(sourceTable
086: .isSelectDistinct()));
087:
088: vContext.put("selectAliasName", "");
089: String tableName = this .genFactory.generate(sourceTable,
090: context);
091: vContext.put("fromContent", tableName);
092:
093: // Add extraction conditions only if user did not set extraction flag to full.
094: vContext.put("condition", "");
095: vContext.put("useWhere", Boolean.FALSE);
096:
097: List whereList = (List) context
098: .getClientProperty(StatementContext.WHERE_CONDITION_LIST);
099:
100: if (!"full".equalsIgnoreCase(sourceTable.getExtractionType())) { //NOI18N
101: if (whereList == null) {
102: whereList = new ArrayList(3);
103: context.putClientProperty(
104: StatementContext.WHERE_CONDITION_LIST,
105: whereList);
106: }
107:
108: List sourceList = new ArrayList(1);
109: sourceList.add(sourceTable);
110: String condition = getSourceWhereCondition(sourceList,
111: context);
112: if (condition != null && !condition.equals("")) {
113: whereList.add(condition);
114: vContext.put("useWhere", Boolean.TRUE);
115: vContext.put(StatementContext.WHERE_CONDITION_LIST,
116: whereList);
117: }
118: }
119:
120: // Add limit constraint if a limit value is set.
121: Object prop = context.getClientProperty("limit");
122: if (prop != null) {
123: if (whereList == null) {
124: whereList = new ArrayList(3);
125: context.putClientProperty(
126: StatementContext.WHERE_CONDITION_LIST,
127: whereList);
128: }
129:
130: setSelectLimit(context, vContext);
131: vContext.put("useWhere", Boolean.TRUE);
132: vContext.put(StatementContext.WHERE_CONDITION_LIST,
133: whereList);
134: }
135:
136: String result = TemplateBuilder.generateSql(this .db
137: .getTemplateFileName("select"), vContext); // NOI18N
138: return createSQLPart(result, SQLPart.STMT_SELECT); // NOI18N
139: }
140:
141: public SQLPart getSelectStatement(TargetTable targetTable,
142: StatementContext context) throws BaseException {
143: if (context == null) {
144: context = new StatementContext();
145: }
146: VelocityContext vContext = new VelocityContext();
147:
148: List sourceColumnIdentifiers = new ArrayList();
149: Iterator it = targetTable.getColumnList().iterator();
150: while (it.hasNext()) {
151: TargetColumn column = (TargetColumn) it.next();
152: String sql = this .genFactory.generate(column, context);
153: ColumnIdentifier cId = new ColumnIdentifier(column
154: .getDisplayName(), sql);
155: sourceColumnIdentifiers.add(cId);
156: }
157: vContext
158: .put("sourceColumnIdentifiers", sourceColumnIdentifiers);
159:
160: vContext.put("aliasColumns", Boolean.FALSE);
161: vContext.put("distinct", super
162: .areDistinctRowsRequired(targetTable));
163:
164: vContext.put("selectAliasName", "");
165: vContext.put("fromContent", this .genFactory.generate(
166: targetTable, context));
167: vContext.put("useWhere", Boolean.FALSE);
168:
169: List whereList = (List) context
170: .getClientProperty(StatementContext.WHERE_CONDITION_LIST);
171: if (whereList == null) {
172: whereList = new ArrayList(3);
173: context.putClientProperty(
174: StatementContext.WHERE_CONDITION_LIST, whereList);
175: }
176: setSelectLimit(context, vContext);
177:
178: String notInSql = (String) vContext.get("notInSql");
179: if (!StringUtil.isNullString(notInSql)) {
180: whereList.add(notInSql);
181: }
182:
183: String integritySql = (String) vContext.get("integritySql");
184: if (!StringUtil.isNullString(integritySql)) {
185: whereList.add(integritySql);
186: }
187:
188: if (whereList.size() != 0) {
189: vContext.put("useWhere", Boolean.TRUE);
190: vContext.put("isJoin", Boolean.TRUE);
191: vContext.put("whereList", whereList);
192: }
193:
194: String result = TemplateBuilder.generateSql(this .db
195: .getTemplateFileName("select"), vContext); // NOI18N
196: return createSQLPart(result, SQLPart.STMT_SELECT); // NOI18N
197: }
198:
199: protected void populateContextForInsertSelect(
200: TargetTable targetTable, StatementContext context,
201: VelocityContext vContext) throws BaseException {
202: StatementContext localContext = new StatementContext();
203: if (context != null) {
204: localContext.putAll(context);
205: }
206: // TODO: consolidate condition in whereList at template level.
207: // currently we are adding "condition" and "whereList" both
208: // in context.
209: List whereList = (List) context
210: .getClientProperty(StatementContext.WHERE_CONDITION_LIST);
211: if (whereList == null) {
212: whereList = new ArrayList(3);
213: localContext.putClientProperty(
214: StatementContext.WHERE_CONDITION_LIST, whereList);
215: }
216:
217: // SET CONTEXT TO USE TARGET TABLE ALIAS NAME IN FROM CLAUSE
218: // AND ALSO ALIAS WILL BE PREPENDED IN COLUMN NAME
219: localContext.putClientProperty(
220: StatementContext.USE_SOURCE_TABLE_ALIAS_NAME,
221: Boolean.TRUE);
222:
223: String targetTableName = this .genFactory.generate(targetTable,
224: localContext);
225: vContext.put("targetTable", targetTableName);
226:
227: // Use the Table Qualification flag to suppress column prefix
228: localContext.setSuppressingTablePrefixForTargetColumn(true);
229:
230: List targetColumnIdentifiers = this .createTargetIdentifierList(
231: targetTable, localContext);
232: vContext
233: .put("targetColumnIdentifiers", targetColumnIdentifiers);
234:
235: //START SELECT
236: List sourceColumnIdentifiers = this .createSourceIdentifierList(
237: targetTable, localContext);
238: vContext
239: .put("sourceColumnIdentifiers", sourceColumnIdentifiers);
240:
241: vContext.put("aliasColumns", Boolean.FALSE);
242: vContext.put("distinct", super
243: .areDistinctRowsRequired(targetTable));
244:
245: vContext.put("selectAliasName", "");
246: vContext.put("nestedIndent", "");
247: // END SELECT
248:
249: // START WHERE
250: vContext.put("condition", "");
251: vContext.put("notInSql", "");
252: vContext.put("integritySql", "");
253: vContext.put("isJoin", Boolean.TRUE);
254:
255: // NOTE: to build the where clauses/join conditions, allow target columns to use
256: // table aliases
257: localContext.putClientProperty(
258: StatementContext.USE_TARGET_TABLE_ALIAS_NAME,
259: Boolean.TRUE);
260: localContext.setSuppressingTablePrefixForTargetColumn(false);
261:
262: // TODO: If filter has been applied already, don't apply again.
263: String condition = getWhereCondition(targetTable, localContext);
264: if (condition != null && !condition.equals("")) {
265: vContext.put("useWhere", Boolean.TRUE);
266: vContext.put("condition", condition);
267:
268: // Move various where clauses from independent VelocityContext mappings to
269: // whereList.
270: whereList.add(condition);
271: }
272:
273: String integritySql = appendSQLForIntegrityCheck(targetTable,
274: localContext);
275: if (integritySql != null && !integritySql.trim().equals("")) {
276: vContext.put("useWhere", Boolean.TRUE);
277: vContext.put("integritySql", integritySql);
278: vContext
279: .put("fromContent",
280: getFromStatementContentForTarget(
281: targetTable,
282: SQLConstants.RIGHT_OUTER_JOIN,
283: localContext));
284:
285: whereList.add(integritySql);
286: } else {
287: vContext.put("fromContent", getFromStatementContent(
288: targetTable, localContext));
289: }
290: // END WHERE
291:
292: if (whereList.size() != 0) {
293: vContext.put("useWhere", Boolean.TRUE);
294: vContext.put("whereList", whereList);
295: }
296:
297: SourceTable[] srcTables = (SourceTable[]) targetTable
298: .getSourceTableList().toArray(new SourceTable[0]);
299: for (SourceTable srcTable : srcTables) {
300: populateContextForGroupByAndHaving(srcTable, localContext,
301: vContext);
302: }
303:
304: populateContextForGroupByAndHaving(targetTable, localContext,
305: vContext);
306: populateContextForGroupByAndHaving(targetTable.getJoinView(),
307: localContext, vContext);
308: }
309:
310: protected void populateContextForUpdate(TargetTable targetTable,
311: StatementContext context, VelocityContext vContext)
312: throws BaseException {
313: StatementContext localContext = new StatementContext();
314: if (context != null) {
315: localContext.putAll(context);
316: }
317: localContext.putClientProperty(
318: StatementContext.USE_TARGET_TABLE_ALIAS_NAME,
319: Boolean.TRUE);
320: super .populateContextForUpdate(targetTable, localContext,
321: vContext);
322: }
323:
324: private void setSelectLimit(StatementContext context,
325: VelocityContext vContext) {
326: Object prop = context.getClientProperty("limit");
327:
328: List whereList = (List) context
329: .getClientProperty(StatementContext.WHERE_CONDITION_LIST);
330: if (prop != null && whereList != null) {
331: vContext.put("limit", prop.toString());
332: String result = TemplateBuilder.generateSql(this .db
333: .getTemplateFileName("limit"), vContext); // NOI18N
334:
335: // Add limit as another predicate in the where clause
336: whereList.add(result);
337: }
338: }
339: }
|