KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > OracleQueryStatement


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: OracleQueryStatement.java,v 1.7 2003/08/11 16:01:52 pierreg0 Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import javax.jdo.JDOFatalInternalException;
14 import java.util.Iterator JavaDoc;
15
16
17 class OracleQueryStatement extends QueryStatement
18 {
19     /**
20      * The system property that selects the "linguistic definition" to be used
21      * for native language sorting of String fields. This is the string
22      * "com.triactive.jdo.oracle.nlsSortOrder". The default value is "LATIN".
23      * A value of "BINARY" disables native language sorting.
24      */

25     public static final String JavaDoc NLS_SORT_ORDER_PROPERTY = "com.triactive.jdo.oracle.nlsSortOrder";
26
27
28     private final String JavaDoc nlsSortOrder = System.getProperty(NLS_SORT_ORDER_PROPERTY, "LATIN").toUpperCase();
29
30     public OracleQueryStatement(Table initialTable)
31     {
32         super(initialTable);
33     }
34
35     public OracleQueryStatement(Table initialTable, SQLIdentifier initialRangeVar)
36     {
37         super(initialTable, initialRangeVar);
38     }
39
40     public void setOrdering(SQLExpression[] exprs, boolean[] descending)
41     {
42         assertNotFrozen();
43
44         boolean needsSelect = dba.includeOrderByColumnsInSelect();
45
46         orderByList = new StatementText();
47
48         for (int i = 0; i < exprs.length; ++i)
49         {
50             if (i > 0)
51                 orderByList.append(',');
52
53             if (exprs[i] instanceof CharacterExpression && !nlsSortOrder.equals("BINARY"))
54                 orderByList.append("NLSSORT(").append(exprs[i]).append(", 'NLS_SORT = ").append(nlsSortOrder).append("')");
55             else
56             {
57                 String JavaDoc exprText = exprs[i].toStatementText().toString();
58
59                 if (exprs[i] instanceof BooleanExpression)
60                 {
61                     if (false == (exprs[i] instanceof BooleanCharColumnExpression))
62                     {
63                         throw new JDOFatalInternalException("OracleAdapter only supports boolean char expressions.");
64                     }
65
66                     exprText = stripTruthTest((BooleanCharColumnExpression)exprs[i]);
67                 }
68
69                 orderByList.append(exprText);
70             }
71
72             if (descending[i])
73                 orderByList.append(" DESC");
74
75             if (needsSelect)
76             {
77                 Iterator JavaDoc j = exprs[i].toStatementText().getReferencedColumns().iterator();
78
79                 while (j.hasNext())
80                 {
81                     String JavaDoc columnRef = j.next().toString();
82
83                     if (!selected.contains(columnRef))
84                         selected.add(columnRef);
85                 }
86             }
87         }
88     }
89
90     /**
91      * Return the StatementText as a String with the truth test stripped off.
92      * This is a hack to account for the fact that Oracle does't support boolean
93      * expressions in ORDER BY clauses.
94      * @param expr The BooleanCharColumnExpression to strip to truth test from.
95      * @return The StatementText as a String with the truth test stripped off.
96      */

97     private static String JavaDoc stripTruthTest(BooleanCharColumnExpression expr)
98     {
99         String JavaDoc text = expr.toStatementText().toString();
100
101         if (null != text)
102         {
103             text = text.substring(0, text.lastIndexOf(" = '"));
104         }
105
106         return text;
107     }
108     
109     private void join(TableExpression te, ObjectExpression fromExpr, ObjectExpression toExpr)
110     {
111         joins.add(te);
112         andCondition(fromExpr.eq(toExpr));
113     }
114
115     public void innerJoin(QueryColumn from, QueryColumn to)
116     {
117         assertNotFrozen();
118
119         ObjectExpression fromExpr = new ObjectExpression(this, from);
120         ObjectExpression toExpr = new ObjectExpression(this, to);
121
122         join(to.te, fromExpr, toExpr);
123     }
124
125     public void leftOuterJoin(QueryColumn from, QueryColumn to)
126     {
127         assertNotFrozen();
128
129         ObjectExpression fromExpr = new ObjectExpression(this, from);
130         ObjectExpression toExpr = new ObjectExpression(this, to, "(+)");
131
132         join(to.te, fromExpr, toExpr);
133     }
134
135     public void rightOuterJoin(QueryColumn from, QueryColumn to)
136     {
137         assertNotFrozen();
138
139         ObjectExpression fromExpr = new ObjectExpression(this, from, "(+)");
140         ObjectExpression toExpr = new ObjectExpression(this, to);
141
142         join(to.te, fromExpr, toExpr);
143     }
144
145     public StatementText toStatementText()
146     {
147         if (stmtText == null)
148         {
149             stmtText = new StatementText("SELECT ");
150
151             if (distinctResults)
152                 stmtText.append("DISTINCT ");
153
154             Iterator JavaDoc i = selected.iterator();
155
156             while (i.hasNext())
157             {
158                 stmtText.append(i.next());
159
160                 if (i.hasNext())
161                     stmtText.append(',');
162             }
163
164             stmtText.append(" FROM ").append(initialTableExpr);
165
166             i = joins.iterator();
167
168             while (i.hasNext())
169                 stmtText.append(',').append(i.next());
170
171             if (whereExpr != null)
172                 stmtText.append(" WHERE ").append(whereExpr);
173
174             if (orderByList != null)
175                 stmtText.append(" ORDER BY ").append(orderByList);
176         }
177
178         return stmtText;
179     }
180
181     public String JavaDoc toString()
182     {
183         return toStatementText().toString();
184     }
185
186 }
187
Popular Tags