001: /*
002: * Copyright 2002 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: OracleQueryStatement.java,v 1.7 2003/08/11 16:01:52 pierreg0 Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import javax.jdo.JDOFatalInternalException;
014: import java.util.Iterator;
015:
016: class OracleQueryStatement extends QueryStatement {
017: /**
018: * The system property that selects the "linguistic definition" to be used
019: * for native language sorting of String fields. This is the string
020: * "com.triactive.jdo.oracle.nlsSortOrder". The default value is "LATIN".
021: * A value of "BINARY" disables native language sorting.
022: */
023: public static final String NLS_SORT_ORDER_PROPERTY = "com.triactive.jdo.oracle.nlsSortOrder";
024:
025: private final String nlsSortOrder = System.getProperty(
026: NLS_SORT_ORDER_PROPERTY, "LATIN").toUpperCase();
027:
028: public OracleQueryStatement(Table initialTable) {
029: super (initialTable);
030: }
031:
032: public OracleQueryStatement(Table initialTable,
033: SQLIdentifier initialRangeVar) {
034: super (initialTable, initialRangeVar);
035: }
036:
037: public void setOrdering(SQLExpression[] exprs, boolean[] descending) {
038: assertNotFrozen();
039:
040: boolean needsSelect = dba.includeOrderByColumnsInSelect();
041:
042: orderByList = new StatementText();
043:
044: for (int i = 0; i < exprs.length; ++i) {
045: if (i > 0)
046: orderByList.append(',');
047:
048: if (exprs[i] instanceof CharacterExpression
049: && !nlsSortOrder.equals("BINARY"))
050: orderByList.append("NLSSORT(").append(exprs[i]).append(
051: ", 'NLS_SORT = ").append(nlsSortOrder).append(
052: "')");
053: else {
054: String exprText = exprs[i].toStatementText().toString();
055:
056: if (exprs[i] instanceof BooleanExpression) {
057: if (false == (exprs[i] instanceof BooleanCharColumnExpression)) {
058: throw new JDOFatalInternalException(
059: "OracleAdapter only supports boolean char expressions.");
060: }
061:
062: exprText = stripTruthTest((BooleanCharColumnExpression) exprs[i]);
063: }
064:
065: orderByList.append(exprText);
066: }
067:
068: if (descending[i])
069: orderByList.append(" DESC");
070:
071: if (needsSelect) {
072: Iterator j = exprs[i].toStatementText()
073: .getReferencedColumns().iterator();
074:
075: while (j.hasNext()) {
076: String columnRef = j.next().toString();
077:
078: if (!selected.contains(columnRef))
079: selected.add(columnRef);
080: }
081: }
082: }
083: }
084:
085: /**
086: * Return the StatementText as a String with the truth test stripped off.
087: * This is a hack to account for the fact that Oracle does't support boolean
088: * expressions in ORDER BY clauses.
089: * @param expr The BooleanCharColumnExpression to strip to truth test from.
090: * @return The StatementText as a String with the truth test stripped off.
091: */
092: private static String stripTruthTest(
093: BooleanCharColumnExpression expr) {
094: String text = expr.toStatementText().toString();
095:
096: if (null != text) {
097: text = text.substring(0, text.lastIndexOf(" = '"));
098: }
099:
100: return text;
101: }
102:
103: private void join(TableExpression te, ObjectExpression fromExpr,
104: ObjectExpression toExpr) {
105: joins.add(te);
106: andCondition(fromExpr.eq(toExpr));
107: }
108:
109: public void innerJoin(QueryColumn from, QueryColumn to) {
110: assertNotFrozen();
111:
112: ObjectExpression fromExpr = new ObjectExpression(this , from);
113: ObjectExpression toExpr = new ObjectExpression(this , to);
114:
115: join(to.te, fromExpr, toExpr);
116: }
117:
118: public void leftOuterJoin(QueryColumn from, QueryColumn to) {
119: assertNotFrozen();
120:
121: ObjectExpression fromExpr = new ObjectExpression(this , from);
122: ObjectExpression toExpr = new ObjectExpression(this , to, "(+)");
123:
124: join(to.te, fromExpr, toExpr);
125: }
126:
127: public void rightOuterJoin(QueryColumn from, QueryColumn to) {
128: assertNotFrozen();
129:
130: ObjectExpression fromExpr = new ObjectExpression(this , from,
131: "(+)");
132: ObjectExpression toExpr = new ObjectExpression(this , to);
133:
134: join(to.te, fromExpr, toExpr);
135: }
136:
137: public StatementText toStatementText() {
138: if (stmtText == null) {
139: stmtText = new StatementText("SELECT ");
140:
141: if (distinctResults)
142: stmtText.append("DISTINCT ");
143:
144: Iterator i = selected.iterator();
145:
146: while (i.hasNext()) {
147: stmtText.append(i.next());
148:
149: if (i.hasNext())
150: stmtText.append(',');
151: }
152:
153: stmtText.append(" FROM ").append(initialTableExpr);
154:
155: i = joins.iterator();
156:
157: while (i.hasNext())
158: stmtText.append(',').append(i.next());
159:
160: if (whereExpr != null)
161: stmtText.append(" WHERE ").append(whereExpr);
162:
163: if (orderByList != null)
164: stmtText.append(" ORDER BY ").append(orderByList);
165: }
166:
167: return stmtText;
168: }
169:
170: public String toString() {
171: return toStatementText().toString();
172: }
173:
174: }
|