KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sqlmagic > tinysql > tsRow


1 /*
2  *
3  * tsRow.java - Row object for tinySQL.
4  *
5  * Copyright 1996, Brian C. Jepson
6  * (bjepson@ids.net)
7  * $Author: davis $
8  * $Date: 2004/12/18 21:25:20 $
9  * $Revision: 1.1 $
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  *
25  */

26
27 package com.sqlmagic.tinysql;
28
29 import java.util.*;
30 import java.lang.*;
31 import java.io.*;
32
33 /*
34  *
35  * tsRow - an extension to Hashtable to hold a given row.
36  *
37  */

38 class tsRow extends Hashtable implements Comparable JavaDoc
39 {
40    Vector orderByColumns = (Vector)null;
41    boolean debug=false;
42   /**
43    *
44    * Given a column name, returns its value as a string.
45    *
46    * @param column the name of the column
47    *
48    */

49   public void setOrderBy(Vector inputColumns)
50   {
51      orderByColumns = inputColumns;
52   }
53   public String JavaDoc columnAsString (String JavaDoc column) {
54     return (String JavaDoc) get(column);
55   }
56
57   /**
58    *
59    * Given a tsColumn object, returns its value as a String.
60    *
61    * @param column the tsColumn object
62    *
63    */

64    public String JavaDoc columnAsString (tsColumn column)
65    {
66       String JavaDoc outputString,valueString,functionName,argList,nextArg;
67       StringBuffer JavaDoc functionBuffer;
68       FieldTokenizer ft1,ft2;
69 /*
70  * Next try to retrieve as a group function, which will not have a
71  * tablename prefix.
72  */

73       outputString = (String JavaDoc)get(column.name);
74       if ( outputString != (String JavaDoc)null ) return outputString;
75       ft1 = new FieldTokenizer(column.name,'(',false);
76       if ( ft1.countFields() == 2 )
77       {
78 /*
79  * The column is a function. If it is a group function, the value
80  * will be stored in the record. Otherwise, the function value
81  * will be determined here by retrieving and processing all the
82  * columns in the function arguments.
83  */

84          outputString = (String JavaDoc)get(column.name);
85          if ( outputString != (String JavaDoc)null ) return outputString;
86          functionName = ft1.getField(0);
87          argList = ft1.getField(1);
88          ft2 = new FieldTokenizer(argList,',',false);
89          functionBuffer = new StringBuffer JavaDoc();
90 /*
91  * Function arguments must consist of table.column names or constants.
92  */

93          while ( ft2.hasMoreFields() )
94          {
95             nextArg = ft2.nextField();
96             valueString = (String JavaDoc)get(nextArg);
97             if ( debug) System.out.println("Function " + functionName
98             + " argument " + nextArg + " has value " + valueString);
99 /*
100  * If the valueString is null then it is a constant rather than
101  * a database column. Remove enclosing quotes.
102  */

103             if ( valueString == (String JavaDoc)null )
104                valueString = UtilString.removeQuotes(nextArg);
105             else
106                valueString = valueString.trim();
107             if ( functionName.equals("CONCAT") )
108                functionBuffer.append(valueString);
109          }
110          outputString = functionBuffer.toString();
111       } else if ( column.table == (String JavaDoc)null ) {
112 /*
113  * This is a constant. Return the table name which will be equal to
114  * the constant value.
115  */

116          outputString = UtilString.removeQuotes(column.name);
117       } else {
118 /*
119  * Retrieve as a simple database column.
120  */

121          outputString = (String JavaDoc)get(column.table + "." + column.name);
122       }
123       return outputString;
124    }
125    public int compareTo(Object JavaDoc inputObj)
126    {
127       String JavaDoc tableColumnName,thisString,inputString;
128       tsColumn columnObject;
129       tsRow inputRow;
130       int i,columnType;
131       double thisValue,inputValue;
132       if ( orderByColumns == (Vector)null ) return 0;
133       inputRow = (tsRow)inputObj;
134       for ( i = 0; i < orderByColumns.size(); i++ )
135       {
136          columnObject = (tsColumn)orderByColumns.elementAt(i);
137          tableColumnName = columnObject.name;
138          columnType = columnObject.type;
139          thisString = (String JavaDoc)get(tableColumnName);
140          inputString = (String JavaDoc)inputRow.get(tableColumnName);
141          if ( Utils.isCharColumn(columnType) |
142               Utils.isDateColumn(columnType) )
143          {
144             if ( thisString == (String JavaDoc)null | inputString == (String JavaDoc)null )
145                continue;
146             if ( thisString.compareTo(inputString) != 0 )
147                return thisString.compareTo(inputString);
148          } else if ( Utils.isNumberColumn(columnType) ) {
149             thisValue = UtilString.doubleValue(thisString);
150             inputValue = UtilString.doubleValue(inputString);
151             if ( thisValue > inputValue ) return 1;
152             else if ( thisValue < inputValue ) return -1;
153          } else {
154             System.out.println("Cannot sort unknown type");
155          }
156       }
157       return 0;
158    }
159    public String JavaDoc toString()
160    {
161       StringBuffer JavaDoc outputBuffer = new StringBuffer JavaDoc();
162       String JavaDoc columnName,columnValue;
163       Enumeration e;
164       for ( e = this.keys(); e.hasMoreElements(); )
165       {
166          columnName = (String JavaDoc)e.nextElement();
167          columnValue = (String JavaDoc)this.get(columnName);
168          outputBuffer.append(columnName + "=" + columnValue + " ");
169       }
170       return outputBuffer.toString();
171    }
172 }
173
174
Popular Tags