001: package com.workingdogs.village;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.Vector;
023:
024: /**
025: * A KeyDef is a way to define the key columns in a table. The KeyDef is generally used in conjunction with a <a
026: * href="TableDataSet.html">TableDataSet</a>. Essentially a KeyDef is what forms the WHERE clause for an UPDATE or DELETE.
027: *
028: * <P>
029: * In order to use the KeyDef, you simply use it like this:
030: * <PRE>
031: * KeyDef kd = new KeyDef().addAttrib("key_column_a");
032: * TableDataSet tds = new TableDataSet ( connection, "table", kd );
033: * tds.fetchRecords();
034: * Record rec = tds.getRecord(0);
035: * rec.setValue("column_name", "new value" );
036: * rec.save();
037: * tds.close();
038: * </PRE>
039: * In the above example, Record 0 is retrieved from the database table and the following update statement is generated:
040: * </p>
041: *
042: * <P>
043: * UPDATE table SET column_name=? WHERE key_column_a=?
044: * </p>
045: *
046: * <P></p>
047: *
048: * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
049: * @version $Revision: 568 $
050: */
051: public class KeyDef {
052: /** TODO: DOCUMENT ME! */
053: private Vector mySelf = null;
054:
055: /**
056: * Constructor for KeyDef. Make sure to always initialize KeyDef with an initial element because it is 1 based.
057: */
058: public KeyDef() {
059: mySelf = new Vector();
060: mySelf.addElement("");
061: }
062:
063: /**
064: * Adds the named attribute to the KeyDef.
065: *
066: * @param name TODO: DOCUMENT ME!
067: *
068: * @return a copy of itself
069: */
070: public KeyDef addAttrib(String name) {
071: mySelf.addElement(name);
072:
073: return this ;
074: }
075:
076: /**
077: * Determines if the KeyDef contains the requested Attribute.
078: *
079: * @param name TODO: DOCUMENT ME!
080: *
081: * @return true if the attribute has been defined. false otherwise.
082: */
083: public boolean containsAttrib(String name) {
084: return (mySelf.indexOf((Object) name) == -1) ? false : true;
085: }
086:
087: /**
088: * getAttrib is 1 based. Setting pos to 0 will attempt to return pos 1.
089: *
090: * @param pos TODO: DOCUMENT ME!
091: *
092: * @return value of Attribute at pos as String. null if value is not found.
093: */
094: public String getAttrib(int pos) {
095: if (pos == 0) {
096: pos = 1;
097: }
098:
099: try {
100: return (String) mySelf.elementAt(pos);
101: } catch (ArrayIndexOutOfBoundsException e) {
102: return null;
103: }
104: }
105:
106: /**
107: * KeyDef's are 1 based, returns size - 1
108: *
109: * @return the number of elements in the KeyDef that were set by addAttrib()
110: *
111: * @see #addAttrib(java.lang.String)
112: */
113: public int size() {
114: return mySelf.size() - 1;
115: }
116: }
|