001: package jimm.datavision.source.sql;
002:
003: import jimm.datavision.*;
004: import jimm.datavision.source.*;
005: import jimm.util.XMLWriter;
006: import java.util.*;
007: import java.sql.PreparedStatement;
008: import java.sql.SQLException;
009:
010: /**
011: * Queries build SQL query strings. They contain tables, joins, and
012: * where clauses.
013: *
014: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
015: * @see ParserHelper
016: */
017: public class SubreportQuery extends SQLQuery {
018:
019: protected Subreport subreport;
020: protected ArrayList subreportJoins;
021:
022: /**
023: * Constructor.
024: *
025: * @param report the report for which this query will generate SQL
026: */
027: public SubreportQuery(Subreport report) {
028: super (report);
029: subreport = report;
030: subreportJoins = new ArrayList();
031: }
032:
033: public void addSubreportJoin(Join join) {
034: subreportJoins.add(join);
035: }
036:
037: public void addSubreportJoins(Collection coll) {
038: subreportJoins.addAll(coll);
039: }
040:
041: /**
042: * Returns an iterator over all of the columns that need to be included in
043: * the parent report's query so that the values are available to this
044: * subreport when it builds its query.
045: *
046: * @return an iterator over selectables
047: */
048: public Iterator parentColumns() {
049: ArrayList list = new ArrayList();
050: for (Iterator iter = subreportJoins.iterator(); iter.hasNext();)
051: list.add(((Join) iter.next()).getFrom());
052: return list.iterator();
053: }
054:
055: protected void buildWhereClause(StringBuffer str, boolean forDisplay) {
056: // We always have a where clauses, even if the user didn't specify one
057: str.append(" where ");
058: if (!joins.isEmpty()) {
059: buildJoins(str);
060: str.append(" and ");
061: }
062: buildUserWhereClause(str, forDisplay);
063: }
064:
065: public String getWhereClauseForDisplay() {
066: StringBuffer buf = new StringBuffer();
067: if (whereClause != null && whereClause.length() > 0) {
068: buf.append('(');
069: buf.append(super .getWhereClauseForPreparedStatement());
070: buf.append(") and (");
071: }
072: for (Iterator iter = subreportJoins.iterator(); iter.hasNext();) {
073: Join j = (Join) iter.next();
074: buf.append(((Column) j.getFrom()).fullName());
075: buf.append(' ');
076: buf.append(j.getRelation());
077: buf.append(' ');
078: buf.append(quoted(((Column) j.getTo()).fullName()));
079: if (iter.hasNext())
080: buf.append(" and ");
081: }
082: if (whereClause != null && whereClause.length() > 0)
083: buf.append(')');
084: return buf.toString();
085: }
086:
087: public String getWhereClauseForPreparedStatement() {
088: StringBuffer buf = new StringBuffer();
089: if (whereClause != null && whereClause.length() > 0) {
090: buf.append('(');
091: buf.append(super .getWhereClauseForPreparedStatement());
092: buf.append(") and (");
093: }
094: for (Iterator iter = subreportJoins.iterator(); iter.hasNext();) {
095: Join j = (Join) iter.next();
096: buf.append("? ");
097: buf.append(j.getRelation());
098: buf.append(' ');
099: buf.append(quoted(((Column) j.getTo()).fullName()));
100: if (iter.hasNext())
101: buf.append(" and ");
102: }
103: if (whereClause != null && whereClause.length() > 0)
104: buf.append(')');
105: return buf.toString();
106: }
107:
108: public void setParameters(PreparedStatement stmt)
109: throws SQLException {
110: super .setParameters(stmt);
111:
112: // Continue with parameters after those filled in by superclass.
113: // Remember that param indices start at 1.
114: int i = preparedStmtValues.size() + 1;
115: for (Iterator iter = subreportJoins.iterator(); iter.hasNext(); ++i) {
116: // In Oracle, Java Dates are turned into timestamps, or something
117: // like that. This is an attempt to fix this problem.
118: Column from = ((Join) iter.next()).getFrom();
119: Object val = subreport.getParentReport().columnValue(from);
120: if (val instanceof java.util.Date)
121: stmt.setDate(i, new java.sql.Date(
122: ((java.util.Date) val).getTime()));
123: else
124: stmt.setObject(i, val);
125: }
126: }
127:
128: protected void writeExtras(XMLWriter out) {
129: out.startElement("subreport-joins");
130: for (Iterator iter = subreportJoins.iterator(); iter.hasNext();)
131: ((Join) iter.next()).writeXML(out);
132: out.endElement();
133: }
134:
135: }
|