001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdbc.sql.exp;
012:
013: import com.versant.core.util.CharBuf;
014: import com.versant.core.jdbc.sql.SqlDriver;
015: import com.versant.core.jdbc.sql.conv.DummyPreparedStmt;
016: import com.versant.core.metadata.MDStatics;
017:
018: import java.sql.Types;
019: import java.sql.SQLException;
020: import java.util.Map;
021:
022: /**
023: * A literal value (String, number etc).
024: */
025: public class LiteralExp extends LeafExp {
026:
027: public static final int TYPE_STRING = 1;
028: public static final int TYPE_OTHER = 2;
029: public static final int TYPE_NULL = 3;
030: public static final int TYPE_BOOLEAN = 4;
031:
032: public int type;
033: public String value;
034:
035: public LiteralExp(int type, String value) {
036: this .type = type;
037: this .value = value;
038: }
039:
040: public LiteralExp() {
041: }
042:
043: public SqlExp createInstance() {
044: return new LiteralExp();
045: }
046:
047: public SqlExp getClone(SqlExp clone, Map cloneMap) {
048: super .getClone(clone, cloneMap);
049:
050: ((LiteralExp) clone).type = type;
051: ((LiteralExp) clone).value = value;
052:
053: return clone;
054: }
055:
056: public LiteralExp(int value) {
057: this (TYPE_OTHER, Integer.toString(value));
058: }
059:
060: public String toString() {
061: StringBuffer s = new StringBuffer();
062: s.append(super .toString());
063: s.append(' ');
064: if (type == TYPE_STRING)
065: s.append('\'');
066: s.append(value);
067: if (type == TYPE_STRING)
068: s.append('\'');
069: return s.toString();
070: }
071:
072: /**
073: * Append SQL for this node to s.
074: *
075: * @param driver The driver being used
076: * @param s Append the SQL here
077: * @param leftSibling
078: */
079: public void appendSQLImp(SqlDriver driver, CharBuf s,
080: SqlExp leftSibling) {
081: if (type == TYPE_BOOLEAN && leftSibling instanceof ColumnExp) {
082: // The idea here is to convert the boolean literal 'true' or
083: // 'false' via the converter on the field that it is being compared
084: // to.
085: ColumnExp cExp = (ColumnExp) leftSibling;
086: if (cExp.col.converter != null) {
087: DummyPreparedStmt pstmt = new DummyPreparedStmt();
088: try {
089: cExp.col.converter.set(pstmt, 0, cExp.col,
090: new Boolean(value));
091: } catch (SQLException e) {
092: //ignore
093: }
094: value = pstmt.value;
095: if (pstmt.toQuote) {
096: type = LiteralExp.TYPE_STRING;
097: } else {
098: type = LiteralExp.TYPE_OTHER;
099: }
100: }
101: }
102: driver.appendSqlLiteral(type, value, s);
103: }
104:
105: /**
106: * What is the JDBC type of this expression (0 if unknown)?
107: */
108: public int getJdbcType() {
109: if (type == TYPE_STRING)
110: return Types.VARCHAR;
111: return 0;
112: }
113:
114: /**
115: * What is the java type code of this expression (0 if unknown)?
116: */
117: public int getJavaTypeCode() {
118: if (type == TYPE_STRING)
119: return MDStatics.STRING;
120: return 0;
121: }
122: }
|