001: /*
002: *
003: * tsRow.java - Row object for tinySQL.
004: *
005: * Copyright 1996, Brian C. Jepson
006: * (bjepson@ids.net)
007: * $Author: davis $
008: * $Date: 2004/12/18 21:25:20 $
009: * $Revision: 1.1 $
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser General Public
022: * License along with this library; if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: *
025: */
026:
027: package com.sqlmagic.tinysql;
028:
029: import java.util.*;
030: import java.lang.*;
031: import java.io.*;
032:
033: /*
034: *
035: * tsRow - an extension to Hashtable to hold a given row.
036: *
037: */
038: class tsRow extends Hashtable implements Comparable {
039: Vector orderByColumns = (Vector) null;
040: boolean debug = false;
041:
042: /**
043: *
044: * Given a column name, returns its value as a string.
045: *
046: * @param column the name of the column
047: *
048: */
049: public void setOrderBy(Vector inputColumns) {
050: orderByColumns = inputColumns;
051: }
052:
053: public String columnAsString(String column) {
054: return (String) get(column);
055: }
056:
057: /**
058: *
059: * Given a tsColumn object, returns its value as a String.
060: *
061: * @param column the tsColumn object
062: *
063: */
064: public String columnAsString(tsColumn column) {
065: String outputString, valueString, functionName, argList, nextArg;
066: StringBuffer functionBuffer;
067: FieldTokenizer ft1, ft2;
068: /*
069: * Next try to retrieve as a group function, which will not have a
070: * tablename prefix.
071: */
072: outputString = (String) get(column.name);
073: if (outputString != (String) null)
074: return outputString;
075: ft1 = new FieldTokenizer(column.name, '(', false);
076: if (ft1.countFields() == 2) {
077: /*
078: * The column is a function. If it is a group function, the value
079: * will be stored in the record. Otherwise, the function value
080: * will be determined here by retrieving and processing all the
081: * columns in the function arguments.
082: */
083: outputString = (String) get(column.name);
084: if (outputString != (String) null)
085: return outputString;
086: functionName = ft1.getField(0);
087: argList = ft1.getField(1);
088: ft2 = new FieldTokenizer(argList, ',', false);
089: functionBuffer = new StringBuffer();
090: /*
091: * Function arguments must consist of table.column names or constants.
092: */
093: while (ft2.hasMoreFields()) {
094: nextArg = ft2.nextField();
095: valueString = (String) get(nextArg);
096: if (debug)
097: System.out.println("Function " + functionName
098: + " argument " + nextArg + " has value "
099: + valueString);
100: /*
101: * If the valueString is null then it is a constant rather than
102: * a database column. Remove enclosing quotes.
103: */
104: if (valueString == (String) null)
105: valueString = UtilString.removeQuotes(nextArg);
106: else
107: valueString = valueString.trim();
108: if (functionName.equals("CONCAT"))
109: functionBuffer.append(valueString);
110: }
111: outputString = functionBuffer.toString();
112: } else if (column.tableName == (String) null) {
113: /*
114: * This is a constant. Return the table name which will be equal to
115: * the constant value.
116: */
117: outputString = UtilString.removeQuotes(column.name);
118: } else {
119: /*
120: * Retrieve as a simple database column.
121: */
122: outputString = (String) get(column.tableName + "."
123: + column.name);
124: if (Utils.isDateColumn(column.type)) {
125: /*
126: * Format dates as DD-MON-YY for output. Note that the value
127: * stored in the database may already be in this format because
128: * of incorrect storage of date strings prior to version 2.3.
129: */
130: try {
131: outputString = UtilString
132: .toStandardDate(outputString);
133: } catch (Exception dateEx) {
134: System.out.println(dateEx.getMessage());
135: }
136: }
137: }
138: return outputString;
139: }
140:
141: public int compareTo(Object inputObj) {
142: String tableColumnName, this String, inputString;
143: tsColumn columnObject;
144: tsRow inputRow;
145: int i, columnType;
146: double this Value, inputValue;
147: if (orderByColumns == (Vector) null)
148: return 0;
149: inputRow = (tsRow) inputObj;
150: for (i = 0; i < orderByColumns.size(); i++) {
151: columnObject = (tsColumn) orderByColumns.elementAt(i);
152: tableColumnName = columnObject.name;
153: columnType = columnObject.type;
154: this String = (String) get(tableColumnName);
155: inputString = (String) inputRow.get(tableColumnName);
156: if (Utils.isCharColumn(columnType)
157: | Utils.isDateColumn(columnType)) {
158: if (this String == (String) null
159: | inputString == (String) null)
160: continue;
161: if (this String.compareTo(inputString) != 0)
162: return this String.compareTo(inputString);
163: } else if (Utils.isNumberColumn(columnType)) {
164: this Value = UtilString.doubleValue(this String);
165: inputValue = UtilString.doubleValue(inputString);
166: if (this Value > inputValue)
167: return 1;
168: else if (this Value < inputValue)
169: return -1;
170: } else {
171: System.out.println("Cannot sort unknown type");
172: }
173: }
174: return 0;
175: }
176:
177: public String toString() {
178: StringBuffer outputBuffer = new StringBuffer();
179: String columnName, columnValue;
180: Enumeration e;
181: for (e = this .keys(); e.hasMoreElements();) {
182: columnName = (String) e.nextElement();
183: columnValue = (String) this .get(columnName);
184: outputBuffer.append(columnName + "=" + columnValue + " ");
185: }
186: return outputBuffer.toString();
187: }
188: }
|