001: package jimm.datavision.source;
002:
003: import jimm.datavision.Writeable;
004: import jimm.util.XMLWriter;
005:
006: /**
007: * A join represents the relationship between two columns in the database.
008: * It is used by a query to build the SQL string necessary to retrieve data.
009: *
010: * @see jimm.datavision.source.Query
011: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
012: */
013: public class Join implements Writeable, Cloneable {
014:
015: public static final String[] RELATIONS = { "=", "!=", "<", "<=",
016: ">", ">=", "like", "not like", // "null", "not null",
017: "in", "not in" };
018:
019: protected Column from;
020: protected String relation;
021: protected Column to;
022:
023: /**
024: * Constructor.
025: *
026: * @param fromCol a database column
027: * @param relation a string like "=" or "<" used to join the two database
028: * columns
029: * @param toCol another database column
030: * @see Column
031: */
032: public Join(Column fromCol, String relation, Column toCol) {
033: from = fromCol;
034: this .relation = relation;
035: to = toCol;
036: }
037:
038: public Object clone() {
039: return new Join(from, relation, to);
040: }
041:
042: /**
043: * Returns the "from" column.
044: *
045: * @return a column
046: */
047: public Column getFrom() {
048: return from;
049: }
050:
051: /**
052: * Sets the "from" column.
053: *
054: * @param newFrom the new column
055: */
056: public void setFrom(Column newFrom) {
057: from = newFrom;
058: }
059:
060: /**
061: * Returns the relation string (for example, "=" or "<").
062: *
063: * @return a string used to define the relationship between the two
064: * database columns
065: */
066: public String getRelation() {
067: return relation;
068: }
069:
070: /**
071: * Sets the relation string (for example, "=" or "<").
072: *
073: * @param newRelation the new string
074: */
075: public void setRelation(String newRelation) {
076: relation = newRelation;
077: }
078:
079: /**
080: * Returns the "to" column.
081: *
082: * @return a column
083: */
084: public Column getTo() {
085: return to;
086: }
087:
088: /**
089: * Sets the "to" column.
090: *
091: * @param newTo the new column
092: */
093: public void setTo(Column newTo) {
094: to = newTo;
095: }
096:
097: /**
098: * Returns a string representation of this join, usable as a where
099: * clause in a SQL query.
100: */
101: public String toString() {
102: return from.fullName() + " " + relation + " " + to.fullName();
103: }
104:
105: /**
106: * Writes this join as an XML tag.
107: *
108: * @param out a writer that knows how to write XML
109: */
110: public void writeXML(XMLWriter out) {
111: out.startElement("join");
112: out.attr("from", from.fullName());
113: out.attr("relation", relation);
114: out.attr("to", to.fullName());
115: out.endElement();
116: }
117:
118: }
|