KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > sqls > oracle > OraSQLGeneratorImpl


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.sqls.oracle;
18
19 import org.apache.ws.jaxme.sqls.ColumnReference;
20 import org.apache.ws.jaxme.sqls.DeleteStatement;
21 import org.apache.ws.jaxme.sqls.InsertStatement;
22 import org.apache.ws.jaxme.sqls.JoinReference;
23 import org.apache.ws.jaxme.sqls.SelectStatement;
24 import org.apache.ws.jaxme.sqls.SelectTableReference;
25 import org.apache.ws.jaxme.sqls.UpdateStatement;
26 import org.apache.ws.jaxme.sqls.impl.SQLGeneratorImpl;
27 import org.apache.ws.jaxme.sqls.impl.StatementMetaData;
28
29
30 /** Oracle specific extension of
31  * {@link org.apache.ws.jaxme.sqls.impl.SQLGeneratorImpl}.
32  */

33 public class OraSQLGeneratorImpl extends SQLGeneratorImpl implements OraSQLGenerator {
34     private boolean isOracle8Compatibility;
35     
36     protected boolean isTableAliasUsingAs() { return false; }
37     
38     /** <p>Enables compatibility mode for Oracle 8.</p>
39      */

40     public void setOracle8Compatibility(boolean pOracle8Compatibility) {
41         isOracle8Compatibility = pOracle8Compatibility;
42     }
43     
44     /** <p>Returns whether compatibility mode for Oracle 8 is enabled.</p>
45      */

46     public boolean isOracle8Compatibility() {
47         return isOracle8Compatibility;
48     }
49     
50     protected StatementMetaData newStatementMetaData(SelectStatement pQuery) {
51         return new OraStatementMetaData(pQuery);
52     }
53
54     protected StatementMetaData newStatementMetaData(DeleteStatement pQuery) {
55         return new OraStatementMetaData(pQuery);
56     }
57
58     protected StatementMetaData newStatementMetaData(UpdateStatement pQuery, ColumnReference[] pColumns) {
59         return new OraStatementMetaData(pQuery, pColumns);
60     }
61
62     protected StatementMetaData newStatementMetaData(InsertStatement pQuery, ColumnReference[] pColumns) {
63         return new OraStatementMetaData(pQuery, pColumns);
64     }
65
66     protected String JavaDoc getSelectQueryFromClause(SelectStatement pQuery, StatementMetaData pData) {
67         if (isOracle8Compatibility()) {
68             SelectTableReference tableReference = pQuery.getSelectTableReference();
69             if (tableReference == null) {
70                 return super.getSelectQueryFromClause(pQuery, pData);
71             }
72             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(" FROM ");
73             sb.append(getTableAlias(pData, tableReference));
74             for (JoinReference joinReference = tableReference.getRightJoinedTableReference();
75                  joinReference != null;
76                  joinReference = joinReference.getRightJoinedTableReference()) {
77                 sb.append(", ");
78                 sb.append(getTableAlias(pData, joinReference));
79             }
80             return sb.toString();
81         } else {
82             return super.getSelectQueryFromClause(pQuery, pData);
83         }
84     }
85
86     protected String JavaDoc getSelectQueryConstraints(SelectStatement pQuery, StatementMetaData pData,
87                                                StatementMetaData.LocalData pLocalData) {
88         String JavaDoc result;
89         if (isOracle8Compatibility()) {
90             SelectTableReference tableReference = pQuery.getSelectTableReference();
91             if (tableReference == null) {
92                 result = super.getSelectQuery(pQuery, pData);
93             } else {
94                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
95                 for (JoinReference joinReference = tableReference.getRightJoinedTableReference();
96                      joinReference != null;
97                      joinReference = joinReference.getRightJoinedTableReference()) {
98                     OraJoinReferenceImpl oraJoin = null;
99                     if (joinReference instanceof OraJoinReferenceImpl) {
100                         oraJoin = (OraJoinReferenceImpl) joinReference;
101                     }
102                     String JavaDoc onClause;
103                     if (oraJoin == null) {
104                         onClause = getWhereClause(pData, joinReference.getOn());
105                     } else {
106                         oraJoin.setIsOracle8LeftOuterJoin(true);
107                         onClause = getWhereClause(pData, joinReference.getOn());
108                         oraJoin.setIsOracle8LeftOuterJoin(false);
109                     }
110                     if (onClause != null && onClause.length() > 0) {
111                         if (pLocalData.hasWhereClause()) {
112                             sb.append(" AND ");
113                         } else {
114                             pLocalData.setWhereClause(true);
115                         }
116                         sb.append(onClause);
117                     }
118                 }
119                 String JavaDoc whereClause = getWhereClause(pData, pQuery.getWhere());
120                 if (whereClause != null && whereClause.length() > 0) {
121                     if (pLocalData.hasWhereClause()) {
122                         sb.append(" AND ");
123                     } else {
124                         pLocalData.setWhereClause(true);
125                     }
126                     sb.append(whereClause);
127                 }
128                 result = sb.toString();
129             }
130         } else {
131             result = super.getSelectQueryConstraints(pQuery, pData, pLocalData);
132         }
133
134         if (pQuery instanceof OraSelectStatement) {
135             OraSelectStatement oraStatement = (OraSelectStatement) pQuery;
136             String JavaDoc s = getWhereClause(pData, oraStatement.getStartWith());
137             if (s != null && s.length() > 0) {
138                 result += " START WITH " + s;
139             }
140             s = getWhereClause(pData, oraStatement.getConnectBy());
141             if (s != null && s.length() > 0) {
142                 result += " CONNECT BY " + s;
143             }
144         }
145
146         return result;
147     }
148
149     protected String JavaDoc getColumnAlias(StatementMetaData pData, ColumnReference pColumn) {
150         String JavaDoc s = super.getColumnAlias(pData, pColumn);
151         if (pColumn.getTableReference() instanceof OraJoinReferenceImpl) {
152             OraJoinReferenceImpl oraJoin = (OraJoinReferenceImpl) pColumn.getTableReference();
153             if (oraJoin.isOracle8LeftOuterJoin()) {
154                 s = s + "(+)";
155             }
156         }
157         if (pColumn instanceof OraColumnReference) {
158             OraColumnReference oraRef = (OraColumnReference) pColumn;
159             if (oraRef.isPrior()) {
160                 s = "PRIOR " + s;
161             }
162         }
163         return s;
164     }
165
166     protected String JavaDoc getOrderColumn(StatementMetaData pData, SelectStatement.OrderColumn pColumn) {
167         String JavaDoc s = super.getOrderColumn(pData, pColumn);
168         if (pColumn instanceof OraSelectStatement.OraOrderColumn) {
169             OraSelectStatement.OraOrderColumn ooc = (OraSelectStatement.OraOrderColumn) pColumn;
170             if (ooc.isNullsFirst()) {
171                 s += " NULLS FIRST";
172             }
173         }
174         return s;
175     }
176 }
177
Popular Tags