1 10 11 package com.triactive.jdo.store; 12 13 import javax.jdo.JDOFatalInternalException; 14 import java.util.Iterator ; 15 16 17 class OracleQueryStatement extends QueryStatement 18 { 19 25 public static final String NLS_SORT_ORDER_PROPERTY = "com.triactive.jdo.oracle.nlsSortOrder"; 26 27 28 private final String 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 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 j = exprs[i].toStatementText().getReferencedColumns().iterator(); 78 79 while (j.hasNext()) 80 { 81 String columnRef = j.next().toString(); 82 83 if (!selected.contains(columnRef)) 84 selected.add(columnRef); 85 } 86 } 87 } 88 } 89 90 97 private static String stripTruthTest(BooleanCharColumnExpression expr) 98 { 99 String 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 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 toString() 182 { 183 return toStatementText().toString(); 184 } 185 186 } 187 | Popular Tags |