001: /*
002: * Copyright 2004 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: StatementText.java,v 1.5 2004/01/18 03:01:06 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import com.triactive.jdo.PersistenceManager;
014: import java.sql.Connection;
015: import java.sql.PreparedStatement;
016: import java.sql.SQLException;
017: import java.util.ArrayList;
018: import java.util.Collections;
019: import java.util.HashMap;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.Set;
023: import javax.jdo.JDOFatalInternalException;
024: import org.apache.log4j.Category;
025:
026: class StatementText {
027: private static final Category LOG = Category
028: .getInstance(StatementText.class);
029:
030: private static int nextParamID = 0;
031:
032: private StringBuffer sql;
033: private ArrayList parameterNames = null;
034: private HashMap parameterMappingsByName = null;
035: private HashMap parameterValuesByName = null;
036: private HashSet referencedColumns = null;
037:
038: public StatementText() {
039: sql = new StringBuffer();
040: }
041:
042: public StatementText(String initialSql) {
043: sql = new StringBuffer(initialSql);
044: }
045:
046: private void initParameters() {
047: if (parameterNames == null) {
048: parameterNames = new ArrayList();
049: parameterMappingsByName = new HashMap();
050: parameterValuesByName = new HashMap();
051: }
052: }
053:
054: private void initReferencedColumns() {
055: if (referencedColumns == null)
056: referencedColumns = new HashSet();
057: }
058:
059: public Set getReferencedColumns() {
060: if (referencedColumns == null)
061: return Collections.EMPTY_SET;
062: else
063: return Collections.unmodifiableSet(referencedColumns);
064: }
065:
066: public StatementText prepend(char c) {
067: sql.insert(0, c);
068: return this ;
069: }
070:
071: public StatementText prepend(String s) {
072: sql.insert(0, s);
073: return this ;
074: }
075:
076: public StatementText append(char c) {
077: sql.append(c);
078: return this ;
079: }
080:
081: public StatementText append(String s) {
082: sql.append(s);
083: return this ;
084: }
085:
086: public StatementText append(QueryStatement.QueryColumn qsc) {
087: sql.append(qsc);
088:
089: initReferencedColumns();
090: referencedColumns.add(qsc);
091: return this ;
092: }
093:
094: public StatementText append(StatementText st) {
095: sql.append(st);
096:
097: if (st.parameterNames != null) {
098: initParameters();
099: parameterNames.addAll(st.parameterNames);
100: parameterMappingsByName.putAll(st.parameterMappingsByName);
101: parameterValuesByName.putAll(st.parameterValuesByName);
102: }
103:
104: if (st.referencedColumns != null) {
105: initReferencedColumns();
106: referencedColumns.addAll(st.referencedColumns);
107: }
108:
109: return this ;
110: }
111:
112: public StatementText append(SQLExpression expr) {
113: return append(expr.toStatementText());
114: }
115:
116: public StatementText append(Object o) {
117: sql.append(o);
118: return this ;
119: }
120:
121: public String appendParameter(ColumnMapping mapping, Object value) {
122: String name = "param-" + nextParamID++;
123:
124: sql.append('?');
125:
126: initParameters();
127: parameterNames.add(name);
128: parameterMappingsByName.put(name, mapping);
129: parameterValuesByName.put(name, value);
130:
131: return name;
132: }
133:
134: public void setParameterValue(String name, Object value) {
135: initParameters();
136:
137: if (!parameterValuesByName.containsKey(name))
138: throw new JDOFatalInternalException(
139: "No such statement parameter: " + name);
140:
141: parameterValuesByName.put(name, value);
142: }
143:
144: public PreparedStatement prepareStatement(PersistenceManager pm,
145: Connection conn) throws SQLException {
146: String stmtText = toString();
147:
148: LOG.debug(stmtText);
149:
150: PreparedStatement ps = conn.prepareStatement(stmtText);
151: boolean done = false;
152:
153: try {
154: setParameters(pm, ps);
155: done = true;
156: } finally {
157: if (!done)
158: ps.close();
159: }
160:
161: return ps;
162: }
163:
164: public PreparedStatement prepareStatement(PersistenceManager pm,
165: Connection conn, int resultSetType, int resultSetConcurrency)
166: throws SQLException {
167: String stmtText = toString();
168:
169: LOG.debug(stmtText);
170:
171: PreparedStatement ps = conn.prepareStatement(stmtText,
172: resultSetType, resultSetConcurrency);
173: boolean done = false;
174:
175: try {
176: setParameters(pm, ps);
177: done = true;
178: } finally {
179: if (!done)
180: ps.close();
181: }
182:
183: return ps;
184: }
185:
186: private void setParameters(PersistenceManager pm,
187: PreparedStatement ps) {
188: if (parameterNames != null) {
189: Iterator i = parameterNames.iterator();
190: int stmtParamNum = 1;
191:
192: while (i.hasNext()) {
193: String name = (String) i.next();
194: ColumnMapping mapping = (ColumnMapping) parameterMappingsByName
195: .get(name);
196: Object value = parameterValuesByName.get(name);
197:
198: mapping.setObject(pm, ps, stmtParamNum++, value);
199: }
200: }
201: }
202:
203: public String toString() {
204: return sql.toString();
205: }
206: }
|