KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > sql > exp > UnaryOpExp


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.sql.exp;
13
14 import com.versant.core.jdbc.sql.SqlDriver;
15 import com.versant.core.jdbc.sql.conv.DummyPreparedStmt;
16 import com.versant.core.util.CharBuf;
17 import com.versant.core.jdo.query.UnaryOpNode;
18
19 import java.util.Map JavaDoc;
20 import java.sql.SQLException JavaDoc;
21
22 import com.versant.core.common.BindingSupportImpl;
23
24 /**
25  * An unary operator (+, -, ~, !) expression.
26  */

27 public class UnaryOpExp extends UnaryExp {
28
29     public static final int OP_MINUS = UnaryOpNode.OP_MINUS;
30     public static final int OP_PLUS = UnaryOpNode.OP_PLUS;
31     public static final int OP_COMPLEMENT = UnaryOpNode.OP_TILDE;
32     public static final int OP_NOT = UnaryOpNode.OP_BANG;
33
34     private int op;
35
36     public UnaryOpExp(SqlExp child, int op) {
37         super(child);
38         this.op = op;
39     }
40
41     public UnaryOpExp() {
42     }
43
44     public SqlExp createInstance() {
45         return new UnaryOpExp();
46     }
47
48     public SqlExp getClone(SqlExp clone, Map JavaDoc cloneMap) {
49         super.getClone(clone, cloneMap);
50
51         ((UnaryOpExp)clone).op = op;
52
53         return clone;
54     }
55
56     public String JavaDoc toString() {
57         String JavaDoc s = super.toString() + " ";
58         switch (op) {
59             case OP_PLUS:
60                 return s + "+";
61             case OP_MINUS:
62                 return s + "-";
63             case OP_NOT:
64                 return s + "not";
65             case OP_COMPLEMENT:
66                 return s + "~";
67         }
68         return s + "unknown(" + op + ")";
69     }
70
71     /**
72      * Normalize this node i.e. transform it into its simplist possible form.
73      * This will turn sub selects into joins and so on.
74      */

75     public SqlExp normalize(SqlDriver driver, SelectExp sel, boolean convertExists) {
76         if (childList instanceof UnaryOpExp && ((UnaryOpExp)childList).op == op) {
77             // if our child is the same as us then skip both of us as the
78
// operations will cancel each other out
79
SqlExp r = childList.childList.normalize(driver, sel, convertExists);
80             if (r != null) {
81                 return r;
82             } else {
83                 return childList.childList;
84             }
85         } else {
86             SqlExp r = null;
87             if ( childList != null )
88                 r = childList.normalize(driver, sel, convertExists);
89             if (r != null) childList = r;
90             return null;
91         }
92     }
93
94     /**
95      * Append SQL for this node to s.
96      *
97      * @param driver The driver being used
98      * @param s Append the SQL here
99      * @param leftSibling
100      */

101     public void appendSQLImp(SqlDriver driver, CharBuf s, SqlExp leftSibling) {
102         // if our child is the same as us then skip both of us as the
103
// operations will cancel each other out
104
if (childList instanceof UnaryOpExp && ((UnaryOpExp)childList).op == op) {
105             childList.childList.appendSQL(driver, s, null);
106             return;
107         }
108
109         boolean brackets;
110         switch (op) {
111             case OP_PLUS:
112                 // do not need to do anything else
113
childList.appendSQL(driver, s, null);
114                 break;
115             case OP_MINUS:
116                 s.append('-');
117                 s.append(' ');
118                 // SAP DB does not like extra brackets around numbers
119
brackets = !(childList instanceof LiteralExp);
120                 if (brackets) s.append('(');
121                 childList.appendSQL(driver, s, null);
122                 if (brackets) s.append(')');
123                 break;
124             case OP_NOT:
125                 if (childList instanceof ColumnExp) {
126                     ColumnExp cExp = (ColumnExp)childList;
127                     childList.appendSQL(driver, s, null);
128                     s.append(" = ");
129                     if (cExp.col.converter != null) {
130                         DummyPreparedStmt pstmt = new DummyPreparedStmt();
131                         try {
132                             cExp.col.converter.set(pstmt, 0, cExp.col, new Boolean JavaDoc("false"));
133                         } catch (SQLException JavaDoc e) {
134                             //ignore
135
}
136                         if (pstmt.toQuote) {
137                             s.append("'" + pstmt.value + "'");
138                         } else {
139                             s.append(pstmt.value);
140                         }
141                     } else {
142                         s.append("false");
143                     }
144                 } else {
145                     s.append("not ");
146                     s.append('(');
147                     childList.appendSQL(driver, s, null);
148                     s.append(')');
149                 }
150                 break;
151             case OP_COMPLEMENT:
152                 s.append('(');
153                 s.append('-');
154                 s.append(' ');
155                 // SAP DB does not like extra brackets around numbers
156
brackets = !(childList instanceof LiteralExp);
157                 if (brackets) s.append('(');
158                 childList.appendSQL(driver, s, null);
159                 if (brackets) s.append(')');
160                 s.append(')');
161                 s.append(' ');
162                 s.append('-');
163                 s.append(' ');
164                 s.append('1');
165                 break;
166             default:
167                 throw BindingSupportImpl.getInstance().internal("Unknown UnaryOpExp op: " + op);
168         }
169     }
170
171     /**
172      * Can this expression be removed and its child be converted into a join?
173      *
174      * @see ExistsExp
175      * @see #NO
176      * @see #YES
177      * @see #YES_DISTINCT
178      */

179     public int getConvertToJoin() {
180         if ( childList == null )
181             return YES;
182         if (op == OP_NOT) {
183             int ans = childList.getConvertToJoin();
184             if (ans == YES_DISTINCT) return YES_DISTINCT_NOT;
185             return ans;
186         } else {
187             return NO;
188         }
189     }
190
191 }
192
Popular Tags