View Javadoc

1   package com.workingdogs.village;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Vector;
23  
24  /***
25   * A KeyDef is a way to define the key columns in a table. The KeyDef is generally used in conjunction with a <a
26   * href="TableDataSet.html">TableDataSet</a>. Essentially a KeyDef is what forms the WHERE clause for an UPDATE or DELETE.
27   *
28   * <P>
29   * In order to use the KeyDef, you simply use it like this:
30   * <PRE>
31   *  KeyDef kd = new KeyDef().addAttrib("key_column_a");
32   *  TableDataSet tds = new TableDataSet ( connection, "table", kd );
33   *  tds.fetchRecords();
34   *  Record rec = tds.getRecord(0);
35   *  rec.setValue("column_name", "new value" );
36   *  rec.save();
37   *  tds.close();
38   *  </PRE>
39   * In the above example, Record 0 is retrieved from the database table and the following update statement is generated:
40   * </p>
41   *
42   * <P>
43   * UPDATE table SET column_name=? WHERE key_column_a=?
44   * </p>
45   *
46   * <P></p>
47   *
48   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
49   * @version $Revision: 568 $
50   */
51  public class KeyDef
52  {
53      /*** TODO: DOCUMENT ME! */
54      private Vector mySelf = null;
55  
56      /***
57       * Constructor for KeyDef. Make sure to always initialize KeyDef with an initial element because it is 1 based.
58       */
59      public KeyDef()
60      {
61          mySelf = new Vector();
62          mySelf.addElement("");
63      }
64  
65      /***
66       * Adds the named attribute to the KeyDef.
67       *
68       * @param name TODO: DOCUMENT ME!
69       *
70       * @return a copy of itself
71       */
72      public KeyDef addAttrib(String name)
73      {
74          mySelf.addElement(name);
75  
76          return this;
77      }
78  
79      /***
80       * Determines if the KeyDef contains the requested Attribute.
81       *
82       * @param name TODO: DOCUMENT ME!
83       *
84       * @return true if the attribute has been defined. false otherwise.
85       */
86      public boolean containsAttrib(String name)
87      {
88          return (mySelf.indexOf((Object) name) == -1) ? false : true;
89      }
90  
91      /***
92       * getAttrib is 1 based. Setting pos to 0 will attempt to return pos 1.
93       *
94       * @param pos TODO: DOCUMENT ME!
95       *
96       * @return value of Attribute at pos as String. null if value is not found.
97       */
98      public String getAttrib(int pos)
99      {
100         if (pos == 0)
101         {
102             pos = 1;
103         }
104 
105         try
106         {
107             return (String) mySelf.elementAt(pos);
108         }
109         catch (ArrayIndexOutOfBoundsException e)
110         {
111             return null;
112         }
113     }
114 
115     /***
116      * KeyDef's are 1 based, returns size - 1
117      *
118      * @return the number of elements in the KeyDef that were set by addAttrib()
119      *
120      * @see #addAttrib(java.lang.String)
121      */
122     public int size()
123     {
124         return mySelf.size() - 1;
125     }
126 }