KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.CharBuf;
15 import com.versant.core.jdbc.sql.SqlDriver;
16 import com.versant.core.jdbc.sql.conv.DummyPreparedStmt;
17 import com.versant.core.metadata.MDStatics;
18
19 import java.sql.Types JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.util.Map JavaDoc;
22
23 /**
24  * A literal value (String, number etc).
25  */

26 public class LiteralExp extends LeafExp {
27
28     public static final int TYPE_STRING = 1;
29     public static final int TYPE_OTHER = 2;
30     public static final int TYPE_NULL = 3;
31     public static final int TYPE_BOOLEAN = 4;
32
33     public int type;
34     public String JavaDoc value;
35
36     public LiteralExp(int type, String JavaDoc value) {
37         this.type = type;
38         this.value = value;
39     }
40
41     public LiteralExp() {
42     }
43
44     public SqlExp createInstance() {
45         return new LiteralExp();
46     }
47
48     public SqlExp getClone(SqlExp clone, Map JavaDoc cloneMap) {
49         super.getClone(clone, cloneMap);
50
51         ((LiteralExp) clone).type = type;
52         ((LiteralExp) clone).value = value;
53
54         return clone;
55     }
56
57     public LiteralExp(int value) {
58         this(TYPE_OTHER, Integer.toString(value));
59     }
60
61     public String JavaDoc toString() {
62         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
63         s.append(super.toString());
64         s.append(' ');
65         if (type == TYPE_STRING) s.append('\'');
66         s.append(value);
67         if (type == TYPE_STRING) s.append('\'');
68         return s.toString();
69     }
70
71     /**
72      * Append SQL for this node to s.
73      *
74      * @param driver The driver being used
75      * @param s Append the SQL here
76      * @param leftSibling
77      */

78     public void appendSQLImp(SqlDriver driver, CharBuf s, SqlExp leftSibling) {
79         if (type == TYPE_BOOLEAN && leftSibling instanceof ColumnExp) {
80             // The idea here is to convert the boolean literal 'true' or
81
// 'false' via the converter on the field that it is being compared
82
// to.
83
ColumnExp cExp = (ColumnExp)leftSibling;
84             if (cExp.col.converter != null) {
85                 DummyPreparedStmt pstmt = new DummyPreparedStmt();
86                 try {
87                     cExp.col.converter.set(pstmt, 0, cExp.col,
88                             new Boolean JavaDoc(value));
89                 } catch (SQLException JavaDoc e) {
90                     //ignore
91
}
92                 value = pstmt.value;
93                 if (pstmt.toQuote) {
94                     type = LiteralExp.TYPE_STRING;
95                 } else {
96                     type = LiteralExp.TYPE_OTHER;
97                 }
98             }
99         }
100         driver.appendSqlLiteral(type, value, s);
101     }
102
103     /**
104      * What is the JDBC type of this expression (0 if unknown)?
105      */

106     public int getJdbcType() {
107         if (type == TYPE_STRING) return Types.VARCHAR;
108         return 0;
109     }
110
111     /**
112      * What is the java type code of this expression (0 if unknown)?
113      */

114     public int getJavaTypeCode() {
115         if (type == TYPE_STRING) return MDStatics.STRING;
116         return 0;
117     }
118 }
119
Popular Tags