KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > internetcds > jdbc > tds > Columns


1 //
2
// Copyright 1998 CDS Networks, Inc., Medford Oregon
3
//
4
// All rights reserved.
5
//
6
// Redistribution and use in source and binary forms, with or without
7
// modification, are permitted provided that the following conditions are met:
8
// 1. Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// 2. Redistributions in binary form must reproduce the above copyright
11
// notice, this list of conditions and the following disclaimer in the
12
// documentation and/or other materials provided with the distribution.
13
// 3. All advertising materials mentioning features or use of this software
14
// must display the following acknowledgement:
15
// This product includes software developed by CDS Networks, Inc.
16
// 4. The name of CDS Networks, Inc. may not be used to endorse or promote
17
// products derived from this software without specific prior
18
// written permission.
19
//
20
// THIS SOFTWARE IS PROVIDED BY CDS NETWORKS, INC. ``AS IS'' AND
21
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
// ARE DISCLAIMED. IN NO EVENT SHALL CDS NETWORKS, INC. BE LIABLE
24
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
// SUCH DAMAGE.
31
//
32

33
34
35
36 package com.internetcds.jdbc.tds;
37
38 import java.util.Vector JavaDoc;
39 import java.sql.Types JavaDoc;
40 import com.internetcds.jdbc.tds.Column;
41
42
43 /**
44  * Information about the columns in a result set.
45  *
46  * @author Craig Spannring
47  */

48 public class Columns
49 {
50    public static final String JavaDoc cvsVersion = "$Id: Columns.java,v 1.1 2006/06/23 10:39:04 sinisa Exp $";
51
52
53    private Vector JavaDoc columns = null;
54    private int columnCount = 0;
55
56    public Columns()
57    {
58       columns = new Vector JavaDoc();
59       columnCount = 0;
60    }
61
62    /**
63     * merge the data from two instances of Columns.
64     *
65     * The 4.2 TDS protocol gives the column information in multiple pieces.
66     * Each piece gives us a specific piece of information for all the
67     * columns in the result set. We must join those pieces of information
68     * together to use as the basis for the ResultSetMetaData class.
69     *
70     * @exception TdsException thrown if the two instances of Columns can't
71     * be merged. This can happen if the number of
72     * columns isn't identical or if there is
73     * conflicting data.
74     */

75    public Columns merge(Columns other)
76       throws TdsException
77    {
78       int tmp;
79       int i;
80
81       if (this.columns.size() != other.columns.size())
82       {
83          throw new TdsException("Confused. Mismatch in number of columns");
84       }
85
86       for(i=1; i<=columnCount; i++)
87       {
88          if (this.getName(i) == null)
89          {
90             this.setName(i, other.getName(i));
91          }
92          else if (other.getName(i) == null)
93          {
94             // fine
95
}
96          else
97          {
98             throw new TdsException("Trying to merge two non-null columns");
99          }
100
101          if (this.getDisplaySize(i) == -1)
102          {
103             this.setDisplaySize(i, other.getDisplaySize(i));
104          }
105          else if (other.getDisplaySize(i) == -1)
106          {
107             // fine
108
}
109          else
110          {
111             throw new TdsException("Trying to merge two non-null columns");
112          }
113
114          if (this.getLabel(i) == null)
115          {
116             this.setLabel(i, other.getLabel(i));
117          }
118          else if (other.getLabel(i) == null)
119          {
120             // fine
121
}
122          else
123          {
124             throw new TdsException("Trying to merge two non-null columns");
125          }
126
127          if (this.getType(i) == -1)
128          {
129             this.setType(i, other.getType(i));
130          }
131          else if (other.getType(i) == -1)
132          {
133             // fine
134
}
135          else
136          {
137             throw new TdsException("Trying to merge two non-null columns");
138          }
139
140          if (this.getPrecision(i) == -1)
141          {
142             this.setPrecision(i, other.getPrecision(i));
143          }
144          else if (other.getPrecision(i) == -1)
145          {
146             // fine
147
}
148          else
149          {
150             throw new TdsException("Trying to merge two non-null columns");
151          }
152
153          if (this.getScale(i) == -1)
154          {
155             this.setScale(i, other.getScale(i));
156          }
157          else if (other.getScale(i) == -1)
158          {
159             // fine
160
}
161          else
162          {
163             throw new TdsException("Trying to merge two non-null columns");
164          }
165
166          if ((this.nullableWasSet(i)) && (other.nullableWasSet(i)))
167          {
168             throw new TdsException("Trying to merge two non-null columns");
169          }
170          else if ((! this.nullableWasSet(i)) && (other.nullableWasSet(i)))
171          {
172             this.setNullable (i, other.isNullable(i));
173          }
174          else
175          {
176             // fine
177
}
178
179          if ((this.readOnlyWasSet(i)) && (other.readOnlyWasSet(i)))
180          {
181             throw new TdsException("Trying to merge two non-null columns");
182          }
183          else if ((! this.readOnlyWasSet(i)) && (other.readOnlyWasSet(i)))
184          {
185             this.setReadOnly(i, other.isReadOnly(i));
186          }
187          else
188          {
189             // fine
190
}
191
192          if ((this.autoIncrementWasSet(i)) && (other.autoIncrementWasSet(i)))
193          {
194             throw new TdsException("Trying to merge two non-null columns");
195          }
196          else if ((! this.autoIncrementWasSet(i))
197                   && (other.autoIncrementWasSet(i)))
198          {
199             this.setAutoIncrement(i, other.isAutoIncrement(i));
200          }
201          else
202          {
203             // fine
204
}
205       }
206       return this;
207    } /* merge() */
208
209
210    private void resize(int columnNumber)
211    {
212       if (columnNumber > columnCount)
213       {
214          columnCount = columnNumber;
215       }
216
217       if (columns.size() <= columnNumber)
218       {
219          columns.setSize(columnNumber+1);
220       }
221
222       if (columns.elementAt(columnNumber-1) == null)
223       {
224          columns.setElementAt(new Column(), columnNumber-1);
225       }
226
227    }
228
229
230    /**
231     *
232     */

233    public int getColumnCount()
234    {
235       return columnCount;
236    }
237
238
239    public void setName(int columnNumber, String JavaDoc value)
240    {
241       resize(columnNumber);
242       if (columns.elementAt(columnNumber-1) == null)
243       {
244          columns.setElementAt(new Column(), columnNumber-1);
245       }
246       ((Column)(columns.elementAt(columnNumber-1))).setName(value);
247    }
248
249    public String JavaDoc getName(int columnNumber)
250    {
251       return ((Column)columns.elementAt(columnNumber-1)).getName();
252    }
253
254    public void setDisplaySize(int columnNumber, int value)
255    {
256       resize(columnNumber);
257       ((Column)(columns.elementAt(columnNumber-1))).setDisplaySize(value);
258    }
259
260    public int getDisplaySize(int columnNumber)
261    {
262       return ((Column)columns.elementAt(columnNumber-1)).getDisplaySize();
263    }
264
265    public void setLabel(int columnNumber, String JavaDoc value)
266    {
267       ((Column)(columns.elementAt(columnNumber-1))).setLabel(value);
268    }
269
270    public String JavaDoc getLabel(int columnNumber)
271    {
272       return ((Column)columns.elementAt(columnNumber-1)).getLabel();
273    }
274
275    public void setType(int columnNumber, int value)
276    {
277       // remeber that this is the native type
278
resize(columnNumber);
279       ((Column)(columns.elementAt(columnNumber-1))).setType(value);
280    }
281
282    public int getType(int columnNumber)
283    {
284       // remeber that this is the native type
285
return ((Column)columns.elementAt(columnNumber-1)).getType();
286    }
287
288    public void setPrecision(int columnNumber, int value)
289    {
290       resize(columnNumber);
291       ((Column)(columns.elementAt(columnNumber-1))).setPrecision(value);
292    }
293
294    public int getPrecision(int columnNumber)
295    {
296       return ((Column)columns.elementAt(columnNumber-1)).getPrecision();
297    }
298
299    public void setScale(int columnNumber, int value)
300    {
301       resize(columnNumber);
302       ((Column)(columns.elementAt(columnNumber-1))).setScale(value);
303    }
304    
305    public int getScale(int columnNumber)
306    {
307       return ((Column)columns.elementAt(columnNumber-1)).getScale();
308    }
309    
310    public boolean isAutoIncrement(int columnNumber)
311    {
312       return ((Column)columns.elementAt(columnNumber-1)).isAutoIncrement();
313    }
314    
315    public void setAutoIncrement(int columnNumber, boolean value)
316    {
317       resize(columnNumber);
318       ((Column)(columns.elementAt(columnNumber-1))).setAutoIncrement(value);
319    }
320    
321    public boolean autoIncrementWasSet(int columnNumber)
322    {
323       return ((Column)(columns.elementAt(columnNumber-1))).autoIncrementWasSet();
324    }
325
326    public int isNullable(int columnNumber)
327    {
328       return ((Column)columns.elementAt(columnNumber-1)).isNullable();
329    }
330    
331    public void setNullable(int columnNumber, int value)
332    {
333       resize(columnNumber);
334       ((Column)(columns.elementAt(columnNumber-1))).setNullable (value);
335    }
336    
337    public boolean nullableWasSet(int columnNumber)
338    {
339       return (isNullable(columnNumber)
340               != java.sql.ResultSetMetaData.columnNullableUnknown);
341    }
342    
343    public boolean isReadOnly(int columnNumber)
344    {
345       return ((Column)columns.elementAt(columnNumber-1)).isReadOnly();
346    }
347    
348    public void setReadOnly(int columnNumber, boolean value)
349    {
350       resize(columnNumber);
351       ((Column)columns.elementAt(columnNumber-1)).setReadOnly(value);
352    }
353    
354    public boolean readOnlyWasSet(int columnNumber)
355    {
356       return ((Column)(columns.elementAt(columnNumber-1))).readOnlyWasSet();
357    }
358    
359 }
360
361
362
363
Popular Tags